As far as I know, there is no cleaner way. The best thing we can do is wrap tf.layers.dense in our abstraction and use it as an object, hiding the scope variable variable trunk:
def my_dense(*args, **kwargs): scope = tf.variable_scope(None, default_name='dense').__enter__() def f(input): r = tf.layers.dense(input, *args, name=scope, **kwargs) scope.reuse_variables() return r return f a = [[1,2,3], [4,5,6]] a = tf.constant(a, dtype=tf.float32) layer = my_dense(3) a = layer(a) a = layer(a) print(*[[int(a) for a in v.get_shape()] for v in tf.trainable_variables()])
Alexp source share