TensorFlow Advanced (iii)---creation and initialization of variables

Source: Internet
Author: User

Creation, initialization, saving, and loading of variables

In fact, the role of variables in the language equivalent, have to store some temporary value of the role or long-term storage. When training a model in TensorFlow, variables are used to store and update parameters. The variable contains the amount of tensor (Tensor) stored in the memory buffer. They need to be explicitly initialized when modeling, and they must be stored to disk after the model is trained. The values can be after model training and analysis is loaded.

Variable class
Tf. Variable.init (Initial_value, Trainable=true, Collections=none, Validate_shape=true, Name=None)

Create a new variable with a value Initial_value

    • Initial_value:a Tensor or Python objects can be converted to a Tensor. The initial value of the variable. You must have the specified shape, unless the Validate_shape is set to false.

    • Trainable: If True, the default value also adds the variable to the drawing collection Graphkeys.trainable_variables, which is used as the default list of variables to be used by the optimizer class

    • Collections: A list of chart collection keys, with new variables added to these collections. Default = [Graphkeys.variables]

    • Validate_shape: If False allows the variable to be initialized with the value of an unknown shape, if true, the default shape initial_value must be supplied.

    • Name: Optional name of the variable, default ' Variable ' and get automatically

Creation of variables

When you create a variable, you pass in a tensor as the initial value into the constructor variable (). TensorFlow provides a series of operators to initialize tensor, the initial value of the English constant, or the random value. As with any tensor, the created variable variable () can be used as input to other operations in the diagram. In addition, all operators that tensor this class overload are reproduced in the variable, so you can also add the nodes to the drawing by arithmetic the variables.

x = tf. Variable (5.0,name="x"= tf. Variable (Tf.random_normal ([784, $], stddev=0.35), name="weights"= tf. Variable (Tf.zeros ([+]), name="biases")

Call TF. Variable () adds several actions to the diagram:

    • A variable op holds the variable value.
    • The initializer OP sets the variable to its initial value. This is actually a tf.assign operation.
    • The initial value of the OPS, such as the zeros op of the biases variable in the example, is also added to the diagram.
Initialization of variables
    • The initialization of a variable must be done explicitly before the other operation of the model is run. The simplest way is to add an operation to initialize all variables and run the operation first before using the model. The most common initialization pattern is to use the convenience function initialize_all_variables () to add op to the graph that initializes all the variables.
Init_op = Tf.global_variables_initializer () with TF. Session () as Sess:  sess.run (INIT_OP)
    • You can also initialize the variable by running its initialization function OP, restore the variable from the save file, or simply run the assign to assign the value to the Op. In fact, the variable initializer op is just a ASSIGNOP that assigns the initial value of the variable to the variable itself. Assign is a method that will be mentioned in the following method
With TF. Session () as Sess:    sess.run (W.initializer)

Assigning a value through another variable

You will sometimes need to initialize the current variable with the initialization value of another variable, because Tf.global_variables_initializer () initializes all variables, so be aware of the use of this method.

is to assign the value of the initialized variable to another new variable!

weights = tf. Variable (Tf.random_normal ([784, $], stddev=0.35), name="weights"= tf. Variable (Weights.initialized_value (), name="W2"= tf. Variable (Weights.initialized_value () * 0.2, name="w_twice")

All variables are automatically collected in the drawing that creates them. By default, the constructor adds a new variable to the drawing collection graphkeys.global_variables. The convenience function global_variables () returns the contents of the collection.

Property

Name

Returns the name of the variable

weights = tf. Variable (Tf.random_normal ([784, $], stddev=0.35), name="weights")Print (Weights.name)

Op

Return OP action

weights = tf. Variable (Tf.random_normal ([784, $], stddev=0.35))print(WEIGHTS.OP)
Method

Assign

Assigns a new value to the variable.

x = tf. Variable (5.0,name="x"+ 1.0)

Eval

In the session, the value of this variable is computed and returned. This is not a graphical construction method, it does not add operations to graphics. Easy Printing Results

v = tf. Variable ([1, 2= Tf.global_variables_initializer () with TF. Session () as Sess:    sess.run (init)    #  Specify session    print(V.eval (sess))     #  Use default session    print(V.eval ())

static shapes and dynamic shapes of variables

In TensorFlow, the tensor has a static (speculative) shape and a dynamic (real) shape

    • Static shape:

The shape of the initial state when creating a tensor or deriving a tensor from an operation

    • Tf. Tensor.get_shape: getting static shapes
    • Tf. Tensor.set_shape (): Updates the static shape of a Tensor object, usually for the case where it cannot be inferred directly
    • Dynamic shape:

A shape describing the original tensor in the course of execution

    • Tf.shape (TF. Tensor): If you want to know what the none is at run time, only through Tf.shape (Tensor) [0] this way to get
    • Tf.reshape: Create a new opening volume with different dynamic shapes
Points

1, when converting static shapes, 1-d to 1-d,2-d to 2-d, can not change the shape of the number of steps

2, for fixed or set the static shape of tensor/variable, can not set the static shape again

3, Tf.reshape () dynamic creation of new opening amount, the number of elements can not be mismatched

4, the operation time, the dynamic acquisition of the shape value of tensor, can only be through Tf.shape (tensor) []

Manage the variables collected in the diagram
Tf.global_variables ()

Returns all the variables collected in the graph

weights = tf. Variable (Tf.random_normal ([784, $], stddev=0.35))print(Tf.global_variables ())

TensorFlow Advanced (iii)---creation and initialization of variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.