(E) variable TensorFlow framework of the OP

 

Series blog link:

(A) TensorFlow framework Introduction: https://www.cnblogs.com/kongweisi/p/11038395.html

(B) and FIG. TensorFlow framework TensorBoard: https://www.cnblogs.com/kongweisi/p/11038517.html

(C) Session TensorFlow framework: https://www.cnblogs.com/kongweisi/p/11038550.html

(D) tensor TensorFlow framework: https://www.cnblogs.com/kongweisi/p/11039237.html

 

This paper outlines:

  • Description of the special role of variable op
  • Explain the role of trainable parameters of variable op
  • Application global_variables_initializer achieve variable initialization op

 

1, variable

TensorFlow variables is the best way to share persistent state program processing. Operated by the variable class-based and tf.Variable OP tf.get_variable ().

Variable characteristics:

  • Persistent storage
  • Value can be modified
  • Training can be specified

1.1 Creating Variables

  • tf.Variable(initial_value=None, trainable=True, collections=None, name=None)
    • initial_value: initialization value
    • trainable: whether training
    • collections: add a new variable to FIG set forth in collections, default [GraphKeys.GLOBAL_VARIABLES], trainable is True if variable is also added to the collection of graphics GraphKeys.TRAINABLE_VARIABLES
var = tf.Variable(tf.random_normal([2, 2], mean=0.0, stddev=1.0), name="var", trainable=True)

with tf.Session() as sess:
    sess.run(var)
  • Display initialize variables need to run value
# Add a variable initialization OP 
# show variable initialization
init_op = tf.global_variables_initializer () with tf.Session () AS sess: # run the initialization of variables OP sess.run (init_op)

1.2 OP process variables

  • new_var = assign(value)
    • Variable assignment of a new value
  • new_var = assign_add(delta)
tf.Variable = var (tf.random_normal ([2, 2], Mean = 0.0, STDDEV = 1.0), name = " var " , trainable = True) 
#
variable assignment of a new value
var1 = var.assign ([ [2, 3], [4, 5 ]])
# Initialize variables OP
init_op
= tf.global_variables_initializer ()
# To a variable on the basis of the original value, with a new value 
VA = var.assign_add ([[. 1,. 3], [. 4,. 5 ]])

with tf.Session () AS Sess:
  # Run the initialization OP    sess.run (init_op)   Print (sess.run (VA))   Print (sess.run (var)) 


About variables are trained, I will introduce later in the linear regression among cases

2, namespace shared variables

The main purpose of the shared variable parameters are shared among a number of networks , due in TensorFlow them, as long as we have different definitions of OP, even if the name parameter specifies the same, but in fact is not the same variable.

If you want to achieve the effect of repeated use of variables , we will use the tf.variable_scope()combination tf.get_variable()use with

2.1 definition of a variable with the same name

var = tf.Variable(name='var', initial_value=[4], dtype=tf.float32)
var_double = tf.Variable(name='var', initial_value=[4], dtype=tf.float32)

<tf.Variable 'var:0' shape=() dtype=float32_ref>
<tf.Variable 'var_1:0' shape=() dtype=float32_ref>

2.2 tf.variable_scope () to modify OP namespace

Will increase the namespace in front of the name specifies the name of the OP

with tf.variable_scope("name"):
    var = tf.Variable(name='var', initial_value=[4], dtype=tf.float32)
    var_double = tf.Variable(name='var', initial_value=[4], dtype=tf.float32)

<tf.Variable 'name/var:0' shape=() dtype=float32_ref>
<tf.Variable 'name/var_1:0' shape=() dtype=float32_ref>

2.2 tf.get_variable shared variables

By tf.get_variable Variable parameter initialization with the same, but if sharing the need to open tf.variable_scope ( "name") in parameter reuse = tf.AUTO_REUSE

# Open Shared Parameter 
# or 
#   with tf.variable_scope ( "name") AS scope: 
#   in front of the need to use a shared variable is defined: scope.reuse_variables () 
with tf.variable_scope ( " name " , Reuse = tf.AUTO_REUSE): 
    var = tf.Variable (initial_value = 4.0, name = " var " , DTYPE = tf.float32) 
    var_double = tf.Variable (initial_value = 4.0, name = " var " , DTYPE = tf.float32) 

    var1 = tf.get_variable ( = tf.random_normal initializer of ([2, 2], Mean = 0.0, STDDEV = 1.0 ), 
                           name ="var1",
                           dtype=tf.float32)
    var1_double = tf.get_variable(initializer=tf.random_normal([2, 2], mean=0.0, stddev=1.0),
                           name="var1",
                           dtype=tf.float32)



with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var1)
    print(var1_double)

 

Guess you like

Origin www.cnblogs.com/kongweisi/p/11039254.html