【问题标题】:What is the default variable_scope in Tensorflow?Tensorflow 中默认的 variable_scope 是什么?
【发布时间】:2017-10-22 04:36:21
【问题描述】:

Tensorflow 中默认的全局 variable_scope 是什么?如何检查对象?有人对此有想法吗?

【问题讨论】:

    标签: variables tensorflow machine-learning scope


    【解决方案1】:

    从技术上讲,所有变量都没有全局变量范围。如果你运行

    x = tf.Variable(0.0, name='x')
    

    从脚本的顶层开始,将在默认图表中创建一个没有变量范围的新变量x。

    不过,tf.get_variable() 函数的情况有点不同:

    x = tf.get_variable(name='x')
    

    它做的第一件事是调用tf.get_variable_scope()函数,它返回当前变量作用域,然后从本地堆栈中查找作用域:

    def get_variable_scope():
      """Returns the current variable scope."""
      scope = ops.get_collection(_VARSCOPE_KEY)
      if scope:  # This collection has at most 1 element, the default scope at [0].
        return scope[0]
      scope = VariableScope(False)
      ops.add_to_collection(_VARSCOPE_KEY, scope)
      return scope
    

    请注意,此堆栈可以为空,在这种情况下,只需创建一个新范围并将其推送到堆栈顶部。

    如果this是你需要的对象,你可以通过调用来访问它:

    scope = tf.get_variable_scope()
    

    从顶层开始,或者如果您已经在范围内,则直接转到ops.get_collection(_VARSCOPE_KEY)。这正是通过调用tf.get_variable() 函数获得的新变量的范围。这是tf.VariableScope 类的一个普通实例,您可以轻松检查。

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      相关资源
      最近更新 更多