I want a piece of code that creates a variable within a scope if it doesn\'t exist, and access the variable if it already exists. I need it to be the same c
We can write our abstraction over tf.varaible_scope than uses reuse=None on the first call and uses reuse=True on the consequent calls:
def variable_scope(name_or_scope, *args, **kwargs):
if isinstance(name_or_scope, str):
scope_name = tf.get_variable_scope().name + '/' + name_or_scope
elif isinstance(name_or_scope, tf.Variable):
scope_name = name_or_scope.name
if scope_name in variable_scope.scopes:
kwargs['reuse'] = True
else:
variable_scope.scopes.add(scope_name)
return tf.variable_scope(name_or_scope, *args, **kwargs)
variable_scope.scopes = set()
Usage:
with variable_scope("foo"): #create the first time
v = tf.get_variable("v", [1])
with variable_scope("foo"): #reuse the second time
v = tf.get_variable("v", [1])