Popular understanding tf.name_scope (), tf.variable_scope ()

Introduction: Recently an experiment, met TensorFlow variable scope problems, tf.name_scope (), tf.variable_scope (), etc. were more profound compared to record relevant notes:
tf.name_scope (), tf.variable_scope () It is a function of two scopes, generally two to create / call variable function tf.variable () and tf.get_variable () with the use. Used to:
1) a shared variable; 2) tensorboard Videos flowchart variable visualization package.
Popular understanding is: tf.name_scope (), tf.variable_scope () will open up their own space in the model, of which variables are managed in this space, but the reason why there are two, mainly have their differences.
1.name_scope and variable_scope:
name_scope and variable_scope mainly used for shared variables. Wherein the shared variable involves two main functions: tf.variable () and tf.get_variable (); i.e., is the need to use tf.get_variable () function in the scope tf.variable_scope. Here with tf.get_variable () instead tf.Variable (), because the former has a variable inspection mechanism detects existing variable is set to the shared variable, if the variable is not set to existing shared variable, TensorFlow to run the second variable has the same name and they will error.
Note, tf.variable () and tf.get_variable () There are different ways to create variables: tf.Variable () every time a new variable. If you want to reuse (share) a number of variables, you need to use the get_variable (), it will go to search for variable names, there are the direct use, no longer new. Further, in order to distinguish the different positions of the shared range or, on the introduction of a domain name. Since it uses the variable name, and to relate to the concept of a domain name. That's why there's the concept of scope. name_scope scope operation, variable_scope reuse can be influenced by setting flag and initialized variables in the domain, because the shared variables to achieve desired results, it is necessary to use at tf.get_variable tf.variable_scope () scopes () This way to produce and extract variables. unlike tf.Variable () every time a new variable, if you encounter a variable name that already exists, it will simply extract the same name of the variable tf.get_variable (), if there is no the name of the variable and then created.
For example:
  1. [/ Code] Output:
  2. [code]
Copy the code
2.name scope and variable scope difference
TF There are two types of scopes: domain name (name scope), or by tf.name_scope create tf.op_scope;
variable domain (variable scope), or created by tf.variable_scope tf.variable_op_scope ;
these two scopes, using variable tf.Variable () way to create, with the same effect, will be in front of the variable name, plus the domain name. For variables tf.get_variable () way to create, the only variable scope name will be added before the variable name, not as a prefix name scope.
Example 1:
  1. [/ Code] Output:
  2. [code]
Copy the code
Example 2:
  1. [/ Code] Output:
  2. [code]
Copy the code
Summary:
1, name_scope not as a prefix tf.get_variable variables, but will be prefixed tf.Variable of. (Example 1)
2, at variable_scope scope, tf.get_variable () and tf.Variable () are added scope_name prefix. Thus, in the scope tf.variable_scope by get_variable () you can use the variable has been created, to achieve the shared variables, i.e., variables may be shared by get_variable () within the scope range tf.variable_scope set. (Example 2)
3, at the time of repeated use, must be stressed scope.reuse_variables () in the code

reference links:
[. 1] scope nomenclature - Tensorflow | mo annoying the Python
[2] tf.name_scope () and tf.variable_scope () - AI-FUTURE - CSDN blog

Guess you like

Origin www.cnblogs.com/jfdwd/p/11184243.html