What's the difference of name scope and a variable scope in tensorflow?

Viewed 107302

What's the differences between these functions?

tf.variable_op_scope(values, name, default_name, initializer=None)

Returns a context manager for defining an op that creates variables. This context manager validates that the given values are from the same graph, ensures that that graph is the default graph, and pushes a name scope and a variable scope.


tf.op_scope(values, name, default_name=None)

Returns a context manager for use when defining a Python op. This context manager validates that the given values are from the same graph, ensures that that graph is the default graph, and pushes a name scope.


tf.name_scope(name)

Wrapper for Graph.name_scope() using the default graph. See Graph.name_scope() for more details.


tf.variable_scope(name_or_scope, reuse=None, initializer=None)

Returns a context for variable scope. Variable scope allows to create new variables and to share already created ones while providing checks to not create or share by accident. For details, see the Variable Scope How To, here we present only a few basic examples.

8 Answers

Let's make it simple: just use tf.variable_scope. Quoting a TF developer,:

Currently, we recommend everyone to use variable_scope and not use name_scope except for internal code and libraries.

Besides the fact that variable_scope's functionality basically extends those of name_scope, together they behave in a way that may surprises you:

with tf.name_scope('foo'):
  with tf.variable_scope('bar'):
    x = tf.get_variable('x', shape=())
    x2 = tf.square(x**2, name='x2')
print(x.name)
# bar/x:0
print(x2.name)
# foo/bar/x2:0

This behavior has its use and justifies the coexistance of both scopes -- but unless you know what you are doing, sticking to variable_scope only will avoid you some headaches due to this.

Tensorflow 2.0 Compatible Answer: The explanations of Andrzej Pronobis and Salvador Dali are very detailed about the Functions related to Scope.

Of the Scope Functions discussed above, which are active as of now (17th Feb 2020) are variable_scope and name_scope.

Specifying the 2.0 Compatible Calls for those functions, we discussed above, for the benefit of the community.

Function in 1.x:

tf.variable_scope

tf.name_scope

Respective Function in 2.x:

tf.compat.v1.variable_scope

tf.name_scope (tf.compat.v2.name_scope if migrated from 1.x to 2.x)

For more information about migration from 1.x to 2.x, please refer this Migration Guide.

Related