Tensorflow variable scope: reuse if variable exists

后端 未结 4 402
醉酒成梦
醉酒成梦 2020-12-02 15:17

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

4条回答
  •  甜味超标
    2020-12-02 16:06

    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])
    

提交回复
热议问题