What is the default variable_scope in Tensorflow?

2019-07-30 09:04发布

问题:

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.