Tf. Variable

Source: Internet
Author: User
Tags constructor

class TF. Variable

A variable maintains the state of the diagram by calling the run () method. You add a variable to the diagram by constructing an instance of the variable class.

The Variable () constructor requires an initial value, which can be a tensor of any type and shape. The initial value defines the type and shape of the variable. After the construction is complete, the variable's type and shape are fixed. You can use the Assign method to modify the value of a variable.

If you want to modify the shape of the variable, you must use the Assign operation, and Validate_shpe=false

Like any tensor, the variable created by variable () can be used as input to other operations nodes in the diagram. In addition, all operation-hosted tensor classes are passed to variables. So you can add nodes to the graph simply by performing arithmetic on the variables.

#!/usr/bin/env python
# coding=utf-8

import tensorflow as TF
#create a variable
w=tf. Variable (<initial-value>,name=<optional-name>) #use the Variable in the graph like any

Tensor 
y= Tf.matmul (W,... another variable or tensor ...)

#the overloaded operators is available too.
Z=tf.sigmoid (w+b)

#assign A new value to the variable with ' assign () ' or a related method.
W.assign (w+1.0)
W.assign_add (1.0)

When constructing a machine learning model, it is convenient to differentiate between variables that save training model parameters and other variables such as a global step variable for calculating the number of training steps. To make this easy, the variable constructor supports the trainable=<bool> parameter. If ture, the new variable is added to the Diagram collection Graphkeys.trainable_variables. A traversed function, trainable_variables (), returns the contents of this collection. The various optimizer classes use this set as the default list of variables to optimize.

Tf. Variable.__init__ (Initial_value=none, Trainable=true, Collections=none, Validate_shape=true, Caching_device=None, Name=none, Variable_def=none, Dtype=none)

Create a new variable with the initial value

The new variable is added to the graph collection listed in collections, which is added by default to [Graphkeys.variables]

If Trainable is true, the variable is also added to the graph collection graphkeys.trainable_variables.

The constructor creates two operation nodes, a variable operation, and an assignment operation to assign the initial value to the variable.

Initial_value:   A tensor, or a Python object that can be converted to tensor, which is the initial value of the variable. The initial value must specify shape unless Validate_shape is set to false. Trainable:   If True, the variable is also added by default to Graphkeys.trainable_variables. This is a list of default variables used by many optimizer classes. initialized by another variable you sometimes need to initialize the current variable with the initialization value of another variable. Because Tf.initialize_all_variables () initializes all variables in parallel, care needs to be taken in case of this requirement. When initializing a new variable with the value of another variable, use the Initialized_value () property of the other variable. You can either directly initialize the initialized value as the initial value of the new variable, or use it as a tensor calculation to give a value to the new variable.

#!/usr/bin/env python
# coding=utf-8

import tensorflow as TF

#create  A variable with a random value.
WEIGHTS=TF. Variable (Tf.random_normal ([5,3],stddev=0.35), name= "weights")
#Create another Variable with the same value as ' Weights '.
W2=TF. Variable (Weights.initialized_value (), name= "W2")
#Create another Variable with twice the value of ' weights '
w_ TWICE=TF. Variable (Weights.initialized_value () *0.2, name= "W_twice")

Init=tf.initialize_all_variables () with
TF. Session () as Sess:
    sess.run (init)
    weights_val,w2_val,w_twice_val=sess.run ([Weights,w2,w_twice])
    Print Weights_val
    print w2_val
    print W_twice_val



















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.