Prepared by Sanasam Ranbir Singh

How to define tensors?

***

Import TensorFlow module

For using tensorflow, we would need to import the tensorflow module first as follows.

In [ ]:
import tensorflow as tf                   # tensorflow is imported as object tf
print("The version is " + tf.__version__) # print the version. I have used 2.x



Types of Tensors

Four Types

> 1. Constant

> 2. Variable

> 3. Placeholder

> 4. SparseTensor

In today's lesson, we will define tensor using constant tensor type. Other types of tensors, we will discuss in the subsequent lessons.

Constant Tensor

___

A Constant tensor is created using tf.constant() method. As the name suggest, once created, the value of constant type tensor can not be changed.

Syntex

> tf.constant ( value, dtype=None, shape=None, name='Const')

Attributes:

> value: A constant value (or list) of output type dtype.

> dtype: The type of the elements of the resulting tensor.

> shape: Optional dimensions of resulting tensor.

> name: Optional name for the tensor.

Return

> It returns a Constant Tensor

Data Types



Define a Scalar Tensor

A scalar tensor stores a scalar value wich can be a number, a string, a boolean value.

It has no direction associated with it. That means,

  • Rank (dimension) = 0
  • Shape = 0

Some examples of scalar tensors are defined below.

In [42]:
x = tf.constant(5)  # Integer value
print(x)
tf.Tensor(5, shape=(), dtype=int32)
In [ ]:
x = tf.constant(5.0)
print(x)
In [ ]:
x = tf.constant("Tensor Definition")
print(x)
In [ ]:
x = tf.constant(True)
print(x)

Vector Tensor (1 Dimensional Tensor)

It has data with only one direction i.e., coordinate space. The following figure defines four data points in one direction, which can be realized by a vector of [1,3,6,8].




All the data points lies in a the same direction, it means,

  • Rank (dimention)= 1
  • Shape = $n$; $n$ is the number of the elements in the vector.

It defines vectors such as vector of,

> - Integers: [1, 2, 3]

> - Rank=1, Shape=3

> - Floats: [2.0, 4.0, 6.0, 8.0]

> - Rank=1, Shape=4

> - String: ["Tom", "John", "Sally"]

> - Rank=1, Shape=3

1D tensor is a vector of 0D Tensors. Few examples are defined below.

In [43]:
x = tf.constant([2, 4, 6])      # A Rank 1 tensor of shape 3 with integer values. 
print(x)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
In [44]:
x = tf.constant([2.0, 4.0, 6.0, 8.0])   # A Rank 1 tensor of shape 4  with real values.
print(x)
tf.Tensor([2. 4. 6. 8.], shape=(4,), dtype=float32)
In [45]:
x = tf.constant(["Tom", "John", "Sally"])   # A Rank 1 tensor of shape 4  with string values.
print(x)
tf.Tensor([b'Tom' b'John' b'Sally'], shape=(3,), dtype=string)

Matrix Tensor (2 Dimensional Tensor)

Rank = 2, shape = ($n$,$m$); $n$ and $m$ are positive natural numbers

2D tensor is a vector of 1D tensor

In [ ]:
y = tf.constant([[1, 2, 3, 4],[5,6,7,8]])
print(y)

3 Dimensional Tensor

Rank = 3, shape = ($l$,$n$,$m$); $l$, $n$ and $m$ are positive natural numbers

3D tensor is a vector of 2D tensor

In [ ]:
y = tf.constant([[[1, 2, 3, 4],[5,6,7,8]],[[1, 2, 3, 4],[5,6,7,8]],[[1, 2, 3, 4],[5,6,7,8]]])
print(y)

Generalization

We can define a tensor $x$ of dimension $k$ with the shape ($n_{k}$,$n_{k-1}$, $n_{k-2}$,$n_{k-3}$,...,$n_{1}$)

> as a vector with $n_{k}$ number of $k-1$ Dimemsional tensors of shape ($n_{k-1}$, $n_{k-2}$,$n_{k-3}$,...,$n_{1}$)

Recursively, a tensor of dimension $k-1$ shape ($n_{k-1}$, $n_{k-2}$,$n_{k-3}$,...,$n_{1}$) can be defined

> asas a vector with $n_{k-1}$ number of $k-2$ Dimemsional tensors of shape ($n_{k-2}$,$n_{k-3}$,...,$n_{1}$), and so on...

Summary

  1. Rank/Degree of a tensor defines the number of associated dirctions or dimenssions.
In [ ]:
y = tf.constant([[1, 2, 3, 4],[5,6,7,8]])
tf.rank(y)
  1. Shape of a tensor defines the number of element in each of the dirctions or dimenssions.
In [ ]:
y = tf.constant([[1, 2, 3, 4],[5,6,7,8]])
tf.shape(y)
  1. The value of a tensor can be printed as
In [ ]:
y = tf.constant([[1, 2, 3, 4],[5,6,7,8]])
tf.print(y)
In [ ]: