More on Tensor

Prepared by Sanasam Ranbir Singh

Variable tensor

Unlike constant tensor, you can change the value of a tensor using tf.assign() method. However, variable tensor should be initialized while creating it.

In [1]:
import tensorflow as tf

x = tf.Variable([1,2,3,4])   # initialize with [1,2,3,4]
print(x)
<tf.Variable 'Variable:0' shape=(4,) dtype=int32, numpy=array([1, 2, 3, 4])>
In [109]:
x = tf.Variable([[1,2,3,4],[5,6,7,8]])
print(x)
<tf.Variable 'Variable:0' shape=(2, 4) dtype=int32, numpy=
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])>
In [111]:
x = tf.Variable([[1,2,3,4],[5,6,7,8]], dtype=tf.float32)
print(x)
<tf.Variable 'Variable:0' shape=(2, 4) dtype=float32, numpy=
array([[1., 2., 3., 4.],
       [5., 6., 7., 8.]], dtype=float32)>
In [31]:
x = tf.Variable([1,2,3,4])
print(x.name)
 
print(x.shape)
 
print(x.dtype)
 
print(x.numpy())
 
Variable:0
(4,)
<dtype: 'int32'>
[1 2 3 4]

Show attributes of a tensor

In [32]:
x = tf.constant([1,2,3,4])
#print(x.name)      #possible, when eager execution is disabled
 
print(x.shape)
 
print(x.dtype)
 
print(x.numpy())
(4,)
<dtype: 'int32'>
[1 2 3 4]
In [55]:
x = tf.Variable([[1,2,3,4],[5,6,7,8]])
print(x.name)     
 
print(x.shape)
 
print(x.dtype)
 
print(x.numpy())
Variable:0
(2, 4)
<dtype: 'int32'>
[[1 2 3 4]
 [5 6 7 8]]

Convert a content tensor to a variable tensor and vice versa

In [33]:
x_con = tf.constant([1,2,3,4])
 
x_var = tf.Variable(t_con)
print(x_var)
<tf.Variable 'Variable:0' shape=(4,) dtype=int32, numpy=array([1, 2, 3, 4])>
In [35]:
x_var = tf.Variable([1,2,3,4])
x_con = tf.constant(x_var)
print(x_con)
tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)

Reshape a tensor

You can chage the sape of the tensor after creating it. but, the number of element of the source tensor and target sensor should be same.

In [71]:
x = tf.constant([1,2,3,4,5,6,7,8], shape=(2,4))
print(x)
tf.Tensor(
[[1 2 3 4]
 [5 6 7 8]], shape=(2, 4), dtype=int32)
In [112]:
x = tf.constant([1,2,3,4,5,6,7,8], shape=(2,4))
tf.reshape(x, (4,2))
<tf.Tensor: shape=(4, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])>
In [87]:
x = tf.constant([[1,2,3,4],[5,6,7,8]])
tf.reshape(x, (8))
<tf.Tensor: shape=(8,), dtype=int32, numpy=array([1, 2, 3, 4, 5, 6, 7, 8])>
In [5]:
x = tf.constant([[1,2,3,4],[5,6,7,8]], shape=(2,4))
tf.reshape(x, (-1))  # flatten the tensor in 1D
<tf.Tensor: shape=(8,), dtype=int32, numpy=array([1, 2, 3, 4, 5, 6, 7, 8])>

Use of -1

The -1 is like a don't care. When you reshape with (x,-1), it generate a new 2D tensor with 4 number of 1D sensors of equal shape. The number of 0D tensor in each 1D tensor depends on the number of elements in the original tensor. Note that the number of elements in the original tensor and new tensor should be same.

In [6]:
tf.reshape(x, (4,-1))
<tf.Tensor: shape=(4, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])>
In [115]:
x = tf.constant([1,2,3,4,5,6,7,8])
tf.reshape(x, (2,-1,2))
<tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy=
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])>
In [65]:
x = tf.constant([[1,2,3,4],[5,6,7,8]])
tf.reshape(x, (2,2,-1))
<tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy=
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])>
In [76]:
x = tf.constant([[1,2,3,4],[5,6,7,8]])
tf.reshape(x, (-1,2,2))
<tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy=
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])>

Access the elements of a tensor

argmax()

Return the index of the maximum element in kD tendor and return a (k-1)D index tensor

In [4]:
x = tf.constant([[1,2,3,4], [5,6,7,8]])
tf.argmax(x)
<tf.Tensor: shape=(4,), dtype=int64, numpy=array([1, 1, 1, 1], dtype=int64)>
In [10]:
x = tf.constant([[1,2,3,4], [5,6,7,8]])
tf.argmin(x)
<tf.Tensor: shape=(4,), dtype=int64, numpy=array([0, 0, 0, 0], dtype=int64)>
In [9]:
x = tf.constant([[9,2,10,4],[5,6,7,8]])
print(tf.math.argmax(x))
tf.Tensor([0 1 0 1], shape=(4,), dtype=int64)
In [17]:
x = tf.constant([[2, 20, 30, 3, 6], [3, 11, 16, 1, 8],
                 [14, 45, 23, 5, 27]])
print(x)
print(tf.math.argmax(x))
tf.Tensor(
[[ 2 20 30  3  6]
 [ 3 11 16  1  8]
 [14 45 23  5 27]], shape=(3, 5), dtype=int32)
tf.Tensor([2 2 0 2 2], shape=(5,), dtype=int64)
In [16]:
x = tf.constant([[[2, 20, 30, 3, 6], [3, 11, 16, 1, 8]],[[1,1,1,1,1],
                 [14, 45, 23, 5, 27]]])
print(x)
print(tf.math.argmax(x))
tf.Tensor(
[[[ 2 20 30  3  6]
  [ 3 11 16  1  8]]

 [[ 1  1  1  1  1]
  [14 45 23  5 27]]], shape=(2, 2, 5), dtype=int32)
tf.Tensor(
[[0 0 0 0 0]
 [1 1 1 1 1]], shape=(2, 5), dtype=int64)

Define the axis of the application.

In [25]:
x = tf.constant([[2, 20, 30, 3, 6], [3, 11, 16, 1, 8],
                 [14, 45, 23, 5, 27]])
print(x)
print(tf.math.argmax(x,0))  # fine across the 0-axis which is the default
tf.Tensor(
[[ 2 20 30  3  6]
 [ 3 11 16  1  8]
 [14 45 23  5 27]], shape=(3, 5), dtype=int32)
tf.Tensor([2 2 0 2 2], shape=(5,), dtype=int64)
In [23]:
x = tf.constant([[2, 20, 30, 3, 6], [3, 11, 16, 1, 8],
                 [14, 45, 23, 5, 27]])
print(x)
print(tf.math.argmax(x,1))  # fine across the 1-axis i.e., across each 1D tensor
tf.Tensor(
[[ 2 20 30  3  6]
 [ 3 11 16  1  8]
 [14 45 23  5 27]], shape=(3, 5), dtype=int32)
tf.Tensor([2 2 1], shape=(3,), dtype=int64)
In [29]:
x = tf.constant([[[2, 20, 30, 3, 6], [3, 11, 16, 1, 8]],[[1,1,1,1,1],
                 [14, 45, 23, 5, 27]]])
print(x)
print(tf.math.argmax(x,1))  # across the 2D tensors
tf.Tensor(
[[[ 2 20 30  3  6]
  [ 3 11 16  1  8]]

 [[ 1  1  1  1  1]
  [14 45 23  5 27]]], shape=(2, 2, 5), dtype=int32)
tf.Tensor(
[[1 0 0 0 1]
 [1 1 1 1 1]], shape=(2, 5), dtype=int64)
In [30]:
x = tf.constant([[[2, 20, 30, 3, 6], [3, 11, 16, 1, 8]],[[1,1,1,1,1],
                 [14, 45, 23, 5, 27]]])
print(x)
print(tf.math.argmax(x,2))  # across the 1D tensors
tf.Tensor(
[[[ 2 20 30  3  6]
  [ 3 11 16  1  8]]

 [[ 1  1  1  1  1]
  [14 45 23  5 27]]], shape=(2, 2, 5), dtype=int32)
tf.Tensor(
[[2 2]
 [0 1]], shape=(2, 2), dtype=int64)

return the maximum element

In [11]:
x = tf.constant([[9,2,10,4],[5,6,7,50]])
print(tf.reduce_max(x))
tf.Tensor(50, shape=(), dtype=int32)
In [ ]: