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. SeeGraph.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.
You can think them as two groups:
variable_op_scope
andop_scope
take a set of variables as input and are designed to create operations. The difference is in how they affect the creation of variables withtf.get_variable
:notice the name of the variable
v
in the two examples.same for
tf.name_scope
andtf.variable_scope
:You can read more about variable scope in the tutorial. A similar question was asked before on Stack Overflow.
As for API r0.11,
op_scope
andvariable_op_scope
are both deprecated.name_scope
andvariable_scope
can be nested:Namespaces is a way to organize names for variables and operators in hierarchical manner (e.g. "scopeA/scopeB/scopeC/op1")
tf.name_scope
creates namespace for operators in the default graph.tf.variable_scope
creates namespace for both variables and operators in the default graph.tf.op_scope
same astf.name_scope
, but for the graph in which specified variables were created.tf.variable_op_scope
same astf.variable_scope
, but for the graph in which specified variables were created.Links to the sources above help to disambiguate this documentation issue.
This example shows that all types of scopes define namespaces for both variables and operators with following differences:
tf.variable_op_scope
ortf.variable_scope
are compatible withtf.get_variable
(it ignores two other scopes)tf.op_scope
andtf.variable_op_scope
just select a graph from a list of specified variables to create a scope for. Other than than their behavior equal totf.name_scope
andtf.variable_scope
accordinglytf.variable_scope
andvariable_op_scope
add specified or default initializer.From the last section of this page of the tensorflow documentation: Names of ops in
tf.variable_scope()
Let's make it simple: just use
tf.variable_scope
. Quoting a TF developer,:Besides the fact that
variable_scope
's functionality basically extends those ofname_scope
, consider how they do not play so nice together:By sticking to
variable_scope
only you avoid some headaches due to this kind of incompatibility.Both variable_op_scope and op_scope are now deprecated and should not be used at all.
Regarding the other two, I also had problems understanding the difference between variable_scope and name_scope (they looked almost the same) before I tried to visualize everything by creating a simple example:
Here I create a function that creates some variables and constants and groups them in scopes (depending on the type I provided). In this function, I also print the names of all the variables. After that, I executes the graph to get values of the resulting values and save event-files to investigate them in TensorBoard. If you run this, you will get the following:
You see the similar pattern if you open TensorBoard (as you see
b
is outside ofscope_name
rectangular):This gives you the answer:
Now you see that
tf.variable_scope()
adds a prefix to the names of all variables (no matter how you create them), ops, constants. On the other handtf.name_scope()
ignores variables created withtf.get_variable()
because it assumes that you know which variable and in which scope you wanted to use.A good documentation on Sharing variables tells you that
The same documentation provides a more details how does Variable Scope work and when it is useful.