What is the default variable_scope in Tensorflow?

爷,独闯天下 提交于 2019-12-11 12:49:54

问题


What is the default global variable_scope in Tensorflow? How can I inspect the object? Does anyone have ideas about that?


回答1:


Technically, there's no global variable scope for all variables. If you run

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

from the top level of your script, a new variable x without a variable scope will be created in the default graph.

However, the situation is a bit different for tf.get_variable() function:

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

The first thing it does is calls tf.get_variable_scope() function, which returns the current variable scope, which in turn looks up the scope from the local stack:

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

Note that this stack can be empty and in this case, a new scope is simply created and pushed on top of the stack.

If this is the object you need, you can access it just by calling:

scope = tf.get_variable_scope()

from the top level, or by going to ops.get_collection(_VARSCOPE_KEY) directly if you're inside a scope already. This is exactly the scope that a new variable will get by a call to tf.get_variable() function. It's an ordinary instance of class tf.VariableScope that you can easily inspect.



来源:https://stackoverflow.com/questions/46870535/what-is-the-default-variable-scope-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!