What is the difference between Tensor constant and variable

There is one difference between a tensor variable and a tensor constant. We can change the tensor created with a variable method.

However, the tensors created with the tensor constant method are unchangeable. 

In other words, the tensor created by variable is mutable. And the tensor created by constant is immutable.

In our previous section we’ve seen tensor constants. Because we’ve created tensors with the tensor constant method. Now the same way, we can create the tensor variable method.

Let’s see the code.

import tensorflow as tf


tensor_that_can_be_changed = tf.Variable([5, 3])
tensor_that_can_be_changed

# output:
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([5,  3], dtype=int32)>

As we see it’s a vector. That means it has magnitude or size and a direction.

In short, it’s a single row consisting of numbers.

We’ve discussed vectors before.

tensor_that_can_not_be_changed = tf.constant([5, 3])
tensor_that_can_not_be_changed

# output
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([5,  3], dtype=int32)>

We can see the change in output. Although the value is the same. 

As a result, we can check it. Whether they are equal or unequal.

tensor_that_can_be_changed == tensor_that_can_not_be_changed

# output
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True,  True])>

Because the both are the same NumPy array with the same value, they are identical in shape and value.

Let’s change the value of the tensor created by the constant method.

After that we will check whether they are identically true or false.

tensor_that_can_be_changed = tf.Variable([5, 3])
tensor_that_can_not_be_changed = tf.constant([15, 7])

tensor_that_can_be_changed == tensor_that_can_not_be_changed

# output:
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([False, False])>

Since we have changed the value of the second tensor, they are not equal anymore.

Finally we will test whether the tensor created by the variable method is changeable or not. Right? 

We will use the assign method, to change one value of the first tensor.

tensor_that_can_be_changed[0]
# output:
<tf.Tensor: shape=(), dtype=int32, numpy=5>

tensor_that_can_be_changed[0].assign(3)
tensor_that_can_be_changed[0]
# output:
<tf.Tensor: shape=(), dtype=int32, numpy=3>
tensor_that_can_not_be_changed[0].assign(3)

In the first half of the code, we have seen the value is 5.

However, after assigning a new value we’ve got the changed tensor with new value 3.

But what we can do with the tensor variable, we cannot do with the tensor constant.

Let’s try to change the tensor constant.

tensor_that_can_not_be_changed[0].assign(3)

# output:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-6981025870a0> in <module>()
----> 1 tensor_that_can_not_be_changed[0].assign(3)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in __getattr__(self, name)
    511         from tensorflow.python.ops.numpy_ops import np_config
    512         np_config.enable_numpy_behavior()""".format(type(self).__name__, name))
--> 513     self.__getattribute__(name)
    514 
    515   @staticmethod

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign'

It gives us error.

Now you may ask, which one I’ll use?

The answer is, it depends. 

However, most of the time, we don’t have to create the tensor. Because TensorFlow automatically chooses it for us while loading or modeling data.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

3 responses to “What is the difference between Tensor constant and variable”

  1. […] Let’s jump in and write some code to see how we can make random tensor first. […]

  2. […] We can also use TensorFlow by using wrapper libraries that simplify the process built on top of TensorFlow. […]

  3. […] shuffle method in TensorFlow does one thing. It shuffles the samples in the data set. However, why do we need to shuffle […]

Leave a Reply