Python ProgrammingPython Programming

This is the third tutorial in our TensorFlow tutorial series.

What is tensors in TensorFlow?

TensorFlow's central data type is the tensor. Tensors are the underlying components of computation and a fundamental data structure in TensorFlow. Without using complex mathematical interpretations, we can say a tensor (in TensorFlow) describes a multidimensional numerical array, with zero or n-dimensional collection of data, determined by rank, shape, and type.

A rank, shape and type are three parameters, by a tensors can be identified.

Rank: A tensor may have numerous dimensions, and the number of dimensions in a tensor is its rank.

Shape: The lengths of a tensor's dimensions form an array called the tensor's shape. In other words, the shape of a tensor is the number of rows and columns it has.

  • A zero-dimensional(rank zero) tensor is called a scalar; has shape of [1].
  • A one-dimensional(rank one) tensor is called a vector; has shape of [columns] or [rows].
  • A two-dimensional(rank two) tensor is called a matrix; has shape of [rows,columns].

Type: It is the data type assigned to the tensor's elements(items).

Need to remember below points about tensors:

  • Each tensor is an instance of the Tensor class.
  • A tensor may consist of numbers, strings, floating-point or Boolean values.
  • Each item or element of a tensor must have the same data type.
  • Using functions of the tf package we can create, process, transform and operate tensors.

Create tensors using different functions

1) Create tensor using "constant" function

The "constant" function is most popular function to create tensors. The only required argument is value or list of values used to create tensor.
The default data type is float32 is list of values are of floating type and int32 if values are integer type.

##
# TensorFlow program to create tensor using constant function.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
t1 = tf.constant([1, 2, 3])
t2 = tf.constant([[1.1, 2.2, 3.3], [4, 5, 6]])
t3 = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
t4 = tf.constant(["India", "Australia"])
t5 = tf.constant([15.5, 20.5, 35.5, 40.1, 80.5], tf.float16, [5], 'T5', False)
sess = tf.Session()
 
print(t1)
print(t2)
 
print("\n########################\n")
print(t3)
print(sess.run(t3))
print("\n########################\n")
 
print(t4)
 
print("\n########################\n")
print(t5)
print(sess.run(t5))
print("\n########################\n")
 
Sample output of above program, shown as below:
Create tensor using constant function

  • Here, t1 is a one-dimensional tensor containing three integer values [data-type int32].
  • t2 is a two-dimensional tensor containing floating-point values [data-type float32].
  • In t5 we have specified data-type as float16; name as T5; and shape as 6. Note [If we will pass more than 6 elements it will throw an error "Too many elements provided. Needed at most 6, but received 7"].

2) Create tensor using "zeros" function

The function zeros create tensors with all elements set to zero and the only required argument of function is shape of tensor.

##
# TensorFlow program to create tensor using zeros function.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
zero_tensor = tf.zeros([3])
zero_tensor_int = tf.zeros([3], tf.int32, 'ZeroTensor')
zero_tensor_4x_4x_4x = tf.zeros([4, 4, 4], tf.int8, '4X-ZeroTensor')
 
print(zero_tensor)
print(zero_tensor_int)
print(zero_tensor_4x_4x_4x)
 
sess = tf.Session()
print(sess.run(zero_tensor_4x_4x_4x))
 
Sample output of above program, shown as below:
Create tensor using zeros function

3) Create tensor using "ones" function

The function ones also create tensors with all elements set to one and the only required argument of function is shape of tensor.

##
# TensorFlow program to create tensor using ones function.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
one_tensor = tf.ones([3])
one_tensor_int = tf.ones([2, 2], tf.complex64, 'Complex-OneTensor')
one_tensor_4x_4x_4x = tf.ones([4, 4, 4], tf.int8, '4X-ZeroTensor')
 
print(one_tensor)
print(one_tensor_int)
 
sess = tf.Session()
print(sess.run(one_tensor_4x_4x_4x))
 
Sample output of above program, shown as below:
Create tensor using ones function

4) Create tensor using "fill" function

The function fill create tensors with all elements have same value pass in argument. Data-type of tensor depends upon value passed in argument.

##
# TensorFlow program to create tensor using fill function.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
fill_tensor = tf.fill([3], 10)
fill_tensor_2x_2x = tf.fill([2, 2], 15, '2x-Fill-Tensor')
fill_tensor_string = tf.fill([2, 2], "Japan", 'String-Fill-Tensor')
fill_tensor_4x_4x_4x = tf.fill([4, 4, 4], 10.15, '4X-Fill-Tensor')
sess = tf.Session()
 
print(fill_tensor)
print(sess.run(fill_tensor))
print("\n########################\n")
 
print(fill_tensor_2x_2x)
print(sess.run(fill_tensor_2x_2x))
print("\n########################\n")
 
print(fill_tensor_string)
print(sess.run(fill_tensor_string))
print("\n########################\n")
 
print(fill_tensor_4x_4x_4x)
print(sess.run(fill_tensor_4x_4x_4x))
print("\n########################\n")
 
Sample output of above program, shown as below:
Create tensor using fill function

5) Create tensor using "linspace" function

The linspace functions create tensors whose element change between start and end value in both increasing and decreasing order.

##
# TensorFlow program to create tensor using linspace function.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
linspace_tensor = tf.linspace(5.0, 10.0, 5, "Linspace-Tensor")
linspace_tensor_dec = tf.linspace(100.0, 20.0, 20, "Linspace-Tensor-Down")
sess = tf.Session()
 
print(linspace_tensor)
print(sess.run(linspace_tensor))
print("\n########################\n")
 
print(linspace_tensor_dec)
print(sess.run(linspace_tensor_dec))
 
Sample output of above program, shown as below:
Create tensor using

6) Create tensor using "range" function

The range functions create tensors without accepting number of elements, however it computes successive elements by adding a value called delta.

##
# TensorFlow program to create tensor using range function.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
range_tensor = tf.range(20.0, 100.0, delta=15.5)
range_tensor_delta = tf.range(100.0, delta=15.5, name="Range-Tensor")
sess = tf.Session()
 
print(range_tensor)
print(sess.run(range_tensor))
print("\n########################\n")
 
print(range_tensor_delta)
print(sess.run(range_tensor_delta))
print("\n########################\n")
 
Sample output of above program, shown as below:
Create tensor using range function

7) Creating tensors with random values

The random_normal function creates a tensor with normally distributed values.

The random_uniform function creates a tensor with uniformly distributed values between the minimum and maximum values.

##
# TensorFlow program to create tensors with random values.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
random_normal_tensor = tf.random_normal([10], mean=1.0, stddev=2.0,
                                        dtype=tf.float64, name="RNT")
 
random_uniform_tensor = tf.random_uniform([10], minval=0.0, maxval=5.0,
                                          dtype=tf.float64, name="RUT")
 
sess = tf.Session()
print("\n--------------------------     RANDOM NORMAL   ------------------\n")
print(random_normal_tensor)
print(sess.run(random_normal_tensor))
print("\n-----------------------------------------------------------------\n")
 
print("\n--------------------------     RANDOM UNIFROM  ------------------\n")
print(random_uniform_tensor)
print(sess.run(random_uniform_tensor))
print("---------------------------------------------------------------------")
 
Sample output of above program, shown as below:
Creating Tensors with Random Values

Each time you run the above program you will get different output.

Transforming tensors using different functions

8) Reshape a tensor

The function reshape used to change the shape of a tensor with same element and data-type.

##
# TensorFlow program to tranform a tensor using reshape function.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[1, 2, 3], [4, 5, 6]])
r1 = tf.reshape(t1, [3, 2])
 
t2 = tf.constant([1, 2, 3, 4, 5, 6, 7, 8])
r2 = tf.reshape(t2, [4, 2])
 
sess = tf.Session()
print("\n--------------SHAPE(2,3) TENSOR --------------\n")
print(t1)
print(sess.run(t1))
 
print("\n--------------RESHAPE(3,2) TENSOR ------------\n")
print(r1)
print(sess.run(r1))
 
print("\n--------------SHAPE(8) TENSOR --------------\n")
print(t2)
print(sess.run(t2))
 
print("\n--------------RESHAPE(4,2) TENSOR ------------\n")
print(r2)
print(sess.run(r2))
 
Sample output of above program, shown as below:
Reshape a tensor

9) Change data type of tensor

The cast function used to change the data-type of tensor.

##
# TensorFlow program to change tensor's data type.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[1, 2, 3], [4, 5, 6]])
c1 = tf.cast(t1, dtype=tf.float16)
 
t2 = tf.constant([1.2, 2, 3, 4, 5, 6, 7, 8])
c2 = tf.cast(t2, dtype=tf.int32)
 
print(t1)
print(c1)
print("\n----------------------------------\n")
 
print(t2)
print(c2)
print("\n----------------------------------\n")
Sample output of above program, shown as below:
Change data type of tensor

10) Reverse a tensor

The reverse function reverses given dimensions of the tensor. The following code demonstrates how reverse works:

##
# TensorFlow program to reverse tensor element.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
 
r1 = tf.reverse(t1, [0])
r2 = tf.reverse(t1, [0, 1])
r3 = tf.reverse(t1, [1])
 
sess = tf.Session()
 
print(sess.run(t1))
print("\n----------------------------\n")
 
print(sess.run(r1))
print("\n----------------------------\n")
 
print(sess.run(r2))
print("\n----------------------------\n")
 
print(sess.run(r3))
print("\n----------------------------\n")
Sample output of above program, shown as below:
Reverse a tensor

11) Slice a tensor

The slice function extracts sub-tensors from a tensor.
The following code demonstrates how slice works:

##
# TensorFlow program to extract a portion of tensor.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
 
s1 = tf.slice(t1, [1, 1], [1,1])
s2 = tf.slice(t1, [1, 1], [2, 2])
s3 = tf.slice(t1, [1, 2], [2, 1])
sess = tf.Session()
 
print(sess.run(t1))
 
print("\n-------SLICE 1,1 - 1,1-------------\n")
print(sess.run(s1))
 
print("\n-------SLICE 1,1 - 2,2-------------\n")
print(sess.run(s2))
 
print("\n-------SLICE 1,2 - 2,1-------------\n")
print(sess.run(s3))
Sample output of above program, shown as below:
Slice a tensor

This brings us to the end of this tutorial.

In the next tutorial, we will learn, how to perform mathematical operations tensors?