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.
Let's begin by a short introduction to variable sharing. It is a mechanism in
TensorFlow
that allows for sharing variables accessed in different parts of the code without passing references to the variable around.The method
tf.get_variable
can be used with the name of the variable as the argument to either create a new variable with such name or retrieve the one that was created before. This is different from using thetf.Variable
constructor which will create a new variable every time it is called (and potentially add a suffix to the variable name if a variable with such name already exists).It is for the purpose of the variable sharing mechanism that a separate type of scope (variable scope) was introduced.
As a result, we end up having two different types of scopes:
tf.name_scope
tf.variable_scope
Both scopes have the same effect on all operations as well as variables created using
tf.Variable
, i.e., the scope will be added as a prefix to the operation or variable name.However, name scope is ignored by
tf.get_variable
. We can see that in the following example:The only way to place a variable accessed using
tf.get_variable
in a scope is to use a variable scope, as in the following example:This allows us to easily share variables across different parts of the program, even within different name scopes:
UPDATE
As of version r0.11,
op_scope
andvariable_op_scope
are both deprecated and replaced byname_scope
andvariable_scope
.