global_reuse

tfsnippet.utils.global_reuse(method_or_scope=None, _sentinel=None, scope=None)

Decorate a function to reuse a variable scope automatically.

The first time to enter a function decorated by this utility will open a new variable scope under the root variable scope. This variable scope will be reused the next time to enter this function. For example:

@global_reuse
def foo():
    return tf.get_variable('bar', ...)

bar = foo()
bar_2 = foo()
assert(bar is bar_2)  # should be True

By default the name of the variable scope should be chosen according to the name of the decorated method. You can change this behavior by specifying an alternative name, for example:

@global_reuse('dense')
def dense_layer(inputs):
    w = tf.get_variable('w', ...)  # name will be 'dense/w'
    b = tf.get_variable('b', ...)  # name will be 'dense/b'
    return tf.matmul(w, inputs) + b

If you have two functions sharing the same scope name, they will not use the same variable scope. Instead, one of these two functions will have its scope name added with a suffix ‘_?’, for example:

@global_reuse('foo')
def foo_1():
    return tf.get_variable('bar', ...)

@global_reuse('foo')
def foo_2():
    return tf.get_variable('bar', ...)

assert(foo_1().name == 'foo/bar')
assert(foo_2().name == 'foo_1/bar')

The variable scope name will depend on the calling order of these two functions, so you should better not guess the scope name by yourself.

Note

If you use Keras, you SHOULD NOT create a Keras layer inside a global_reuse decorated function. Instead, you should create it outside the function, and pass it into the function.