Variables

TensorFlow has Variable nodes too which can hold variable data. They are mainly used to hold and update parameters of a training model. Variables are in-memory buffers containing tensors. They must be explicitly initialized and can be saved to disk during and after training. You can later restore saved values to exercise or analyze the model. An important difference to note between a constant and Variable is:

A constant’s value is stored in the graph and its value is replicated wherever the graph is loaded. A variable is stored separately, and may live on a parameter server.

Given below is an example using Variable

Python




# importing tensorflow
import tensorflow as tf
 
# creating nodes in computation graph
node = tf.Variable(tf.zeros([2,2]))
 
# running computation graph
with tf.Session() as sess:
 
    # initialize all global variables
    sess.run(tf.global_variables_initializer())
 
    # evaluating node
    print("Tensor value before addition:\n",sess.run(node))
 
    # elementwise addition to tensor
    node = node.assign(node + tf.ones([2,2]))
 
    # evaluate node again
    print("Tensor value after addition:\n", sess.run(node))


Output:

Tensor value before addition:
[[ 0. 0.]
[ 0. 0.]]
Tensor value after addition:
[[ 1. 1.]
[ 1. 1.]]

In above program:

  • We define a node of type Variable and assign it some initial value.
node = tf.Variable(tf.zeros([2,2]))
  • To initialize the variable node in current session’s scope, we do:
sess.run(tf.global_variables_initializer())
  • To assign a new value to a variable node, we can use assign method like this:
node = node.assign(node + tf.ones([2,2]))

Introduction to TensorFlow

TensorFlow is an open-source machine learning library developed by Google. TensorFlow is used to build and train deep learning models as it facilitates the creation of computational graphs and efficient execution on various hardware platforms. The article provides an comprehensive overview of tensorflow.

Table of Content

  • TensorFlow
  • How to install TensorFlow?
  • The Computational Graph
  • Variables
  • Placeholders
  • Linear Regression model using TensorFlow
  • tf.contrib.learn
  • What are TensorFlow APIs?

Similar Reads

TensorFlow

TensorFlow is basically a software library for numerical computation using data flow graphs where:...

How to install TensorFlow?

An easy-to-follow guide for TensorFlow installation is available....

Computational Graph

Any TensorFlow Core program can be divided into two discrete sections:...

Variables

...

Placeholders

...

Linear Regression model using TensorFlow

TensorFlow has Variable nodes too which can hold variable data. They are mainly used to hold and update parameters of a training model. Variables are in-memory buffers containing tensors. They must be explicitly initialized and can be saved to disk during and after training. You can later restore saved values to exercise or analyze the model. An important difference to note between a constant and Variable is:...

tf.contrib.learn

...

What are TensorFlow APIs?

A graph can be parameterized to accept external inputs, known as placeholders. A placeholder is a promise to provide a value later. While evaluating the graph involving placeholder nodes, a feed_dict parameter is passed to the session’s run method to specify Tensors that provide concrete values to these placeholders. Consider the example given below:...

Contact Us