environment: Ubuntu14.04, tensorflow=1.4 (bazel Anaconda python=3.6

installation source), declare variables there are two main methods: tf.Variable and tf.get_variable, the biggest difference between the two is:

(1) tf.Variable is a class that comes with many attributes and functions; tf.get_variable is a function of
; (2) tf.Variable can only generate the one and only variables, namely if the name already exists, it will automatically modify the generated new variable name;
(3) tf.get_variable can be used to generate shared variables. By default, the function will check the name of the variable, and if there is a repetition, the function will be wrongly reported. When a

is declared to be shared in a variable in a specified variable domain, the variable can be reused (for example, parameter sharing in RNN).
simple example program is given below:

 import tensorflow as TF with tf.variable_scope ('scope1', reuse=tf.AUTO_REUSE) as scope1: X1 = tf.Variable (tf.ones ([1]), name='x1' X2 (tf.zeros) = tf.Variable ([1]), name='x1') Y1 = tf.get_variable ('y1', initializer=1.0) y2 = tf.get_variable ('y1', initializer=0.0) = init tf.global_variables_initializer (with) tf.Session (as) sess: sess.run (init) print (x1.name, x1.eval) (print) (x2.name, x2.eval) (print) (y1.name, y1.eval) (print) (y2.name), y2.eval (



 scope1/x1:0 output 1.] scope1/x1_1:0 [[0.] scope1/y1:0 1 scope1/y1:0 1 

1. (tf.Variable... )

tf.Variable (... Use a given initial value to create a new variable, which will be added to graph collections listed in collections, which defaults to [GraphKeys.GLOBAL_VARIABLES] by default.

if the trainable property is set to True, the variable will also be added to the graph collection GraphKeys.TRAINABLE_VARIABLES.

 tf.Variable __init__ # (initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None, expected_shape=None, import_scope=None, constraint=None 

2. tf.get_variable (

)... )

tf.get_variable (... ) the return value of two kinds:

using the specified initializer to create a new variable; when the variable
reuse, according to the variable name search returns a tf.get_variable created by the existing variable;

 get_variable (name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, caching_device=None. Partitioner=None, validate_shape=True, use_resource=None, custom_getter=None, constraint=None) 


according to the variable name lookup in creating a variable, even if we do not specify the name of a variable, the program will automatically be named. As a result, we can easily find variables based on names, which are very useful when grabbing parameters, finetune models, and so on.

example 1:

is searched by matching search in variable list of tf.global_variables () variables. In this way, you can find variables created by tf.Variable or tf.get_variable at the same time.

 import tensorflow as TF x = tf.Variable (1, name='x') y = tf.get_variable (name='y', shape=[1,2]) for VaR in tf.global_variables (if): var.name ='x:0': Print (VaR) 

by get_tensor_by_name (sample 2:) can also be obtained by tf.Variable or tf.get_variable to create variables. It is important to note that
is obtained at this time is Tensor, but not Variable, so x

 import tensorflow as is not x1.

TF x = tf.Variable (1, name='x') y = tf.get_variable (name='y', shape=[1,2]) = tf.get_default_graph (graph) X1 = graph.get_tensor_by_name ("x:0") Y1 = graph.get_tensor_by_name ("y:0")


tf.get_variable for example create variables that can be used to directly obtain variable reuse existing variables.

 with tf.variable_scope ("foo"): bar1 = tf.get_variable ("bar" (2,3), create with (tf.variable_scope) # "foo", reuse=True): bar2 = tf.get_variable ("bar") # reuse with tf.variable_scope ("reuse=", True root variable scope #): bar3 = tf.get_variable ("foo/bar" # (equivalent to reuse) the (above) print (bar1 is bar2) and (bar2 is bar3) 

) all above is the article, hope to help everyone to learn, I hope you will support a script.

you might be interested in this article:

This concludes the body part

This paper fixed link:http://www.script-home.com/tensorflow-creates-variables-and-finds-variables-based-on-names.html | Script Home | +Copy Link

Article reprint please specify:Tensorflow creates variables and finds variables based on names | Script Home

You may also be interested in these articles!