Assigning a value to a TensorFlow variable in Python

In this tutorial, you are going to learn how to assign a value to a TensorFlow variable with some examples of Python programs.

Quick Introduction to Variables in TensorFlow

Before we go forward, let’s have a quick introduction on TensorFlow variables. Below are given some key points related to variables of TensorFlow:

  • When you want to train a model, you have to use variables to hold and update parameters.
  • A variable in TensorFlow is the recommended way to represent a shared, persistent state, that your program manipulates.
  • These variables are created and tracked via the tf.Variable class.
  • Specific operations allow you to read and modify the values of tensor using this variable.

Before Starting, Let’s See What We Need…

  • Conda : We’ll be needing it to create a TensorFlow environment and to install Jupyter.
  • Python : Our main coding language for Machine Learning. Currently, Python is the most preferred language for machine learning and deep-learning related tasks.
  • TensorFlow : We’ll be installing TensorFlow 2.0 which is the latest version at the time of writing this tutorial.
  • Jupyter Notebook : To code efficiently.

Let’s Assign Value to Our TensorFlow Variable

These variable functions are similar to a Python variable, where you can assign or change the value of the variable.

Let’s start by importing TensorFlow in Python.

import tensorflow as tf
  • Now we’ll define the tf.Variable class and assign the variable an initial value, and, we will also give it a name.
  • Let’s name it as ‘number’.
state = tf.Variable(0,name='number')
print(state.name)
Your Output will be:
number=0

Now, version 2.0 of TensorFlow does not support some attributes of version 1.0.

Let’s check if it’s true.

import tensorflow as tf
state = tf.Variable(0,name='number')
##print(state.name)
one = tf.constant(1)
new_value= tf.add(state,one)
update=tf.assign(state,new_value)

Let’s run this code and see the output:

AttributeError                            Traceback (most recent call last)
<ipython-input-2-2c272fa2e037> in <module>
      4 one = tf.constant(1)
      5 new_value= tf.add(state,one)
----> 6 update=tf.assign(state,new_value)

AttributeError: module 'tensorflow' has no attribute 'assign'

As you can see, it say’s ‘tensorflow’ has no attribute called ‘assign’. While, ‘assign’ is an attribute of tensorflow 1.0, which is used to assign a new value to the variable.

How to solve this problem?

How about we tell TensorFlow 2.0 to act like TensorFlow 1.0?

Let’s Try it.

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

Here by using ‘tensorflow.compat.v1’ we are importing compatibility of version 1.o of our TensorFlow. Similarly, ‘tf.disable_v2_behavior()’ disable’s the working of TensorFlow 2.0.

That’s Machine Learning for you!

Now that we can use all the attributes, let’s start assigning the value.

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
state = tf.Variable(0,name='number')
print(state.name)

If you’re getting output same as this:

number=0

Then we are on the right track!

Now, let’s create a new value, where we will add a constant to our state, and then, we’ll be updating our value using tf.assign.

new_value= tf.add(state,1)
update=tf.assign(state,new_value)

After assigning a new value to state, and storing it in a new variable named ‘update’. We’ll be now getting our tf.Variable into action.

When you are defining a variable into your code, it’s mandatory to initialize it.

init = tf.global_variables_initializer()

‘tf.global_variables_initializer’ initializes our variable.

with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

Here, ‘with’ keyword is opening tf.Session as ‘sess’.  Now, our session is ready to run and give the output.

1

2

3

 

The whole code is as follow:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
state = tf.Variable(0,name='counter')
##print(state.name)

one = tf.constant(1)
new_value= tf.add(state,one)
update=tf.assign(state,new_value)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

I hope, you have understood the procedure of Assigning a value to a TensorFlow variable.

 

Leave a Reply

Your email address will not be published. Required fields are marked *