What actually Tensors are?

Som
3 min readSep 17, 2021

--

What’s Tensor?

Tensor is the concept in mathematics that’s basically a flexible data structure that can hold various types of data in different ways. An important term is the dimensionality of the tensors can be of any dimension.

Tensors

A single-dimensional tensor is called a scalar it contains only a single value while vectors are the list of 1-dimensional values and matrices are 2 dimensional (horizontal or vertical). Tensors can hold data of any dimension. regardless of any dimension tensor is a very flexible way of representing data.

Some type of Tensors

Variables: they are mutable types of tensors it means if we have to change values that are inside the tensors we can change them

Constants: They are immutable types of tensors it means that values once declared cannot be changed but the operations can be done in these types of tensors also and we can store results in another variable

Declaring Tensors in TensorFlow

for variables, we can just declare them as

x = tf.Variable([1,2,3],dtype = tf.float32)

for declaring constants we can declare them as

x = tf.constant([1,2,3],dtype = tf.float32)

here you can see just the difference between declaration is one is done with tf.Variable and other is done with tf.constant (note capital V in variable and c is small in constant)

dimensionality in tensors

here in the above example, we have declared you can see we have declared tensors from the single list they are 1-dimensional tensors (vectors what we called above). we can find the shape of the tensors by calling x.shape it’ll return its shape

what if we want to increase dimensionality how we can declare the 2d tensor

if we want to declare a tensor of 2 dimensions there are 2 options there for us either we can declare the tensor to be 2–dimensional list in python or convert the existing one tensor to a 2-dimensional tensor

  1. Declaring 2-dimensional tensor
y = tf.constant([[1,2,3],[4,5,6]], dtype = tf.float64)

Here you can see that tensor is declared with a 2-dimensional array here the shape will be 2-dimensional

2. Converting x tensor to 2 dimensional tensor

x= tf.cast(x, shape =(2,2))

here we have converted our tensor x of shape 1,4 to shape 2,2 here it’s converted to a 2-dimensional matrix

you have noticed in the examples that we coded above there is one more parameter which we are passing along with tf.Variable and tf.constant it’s a dtype parameter it requires for defining the datatype of the tensor

tf.flaot32 is used for declaring float of 32 bits size

tf.float64 used for declaring float tensor of 64 bits size

tf.uint8 used for declaring unsigned integer of 8 bit size

in similar way we have tf.string used to declare string values

Why use tensors ?

you may think why we should use tensors in the first place we can do it the same by using lists in python.

reasons for using tensors :
1. Tensors are highly flexible data structure and we can store many dimensions and many datatypes in tensors
2. We can use the same for python lists but they are slow. python approach for doing computation is quite slow compared to tensors you can notice it when you are using various dimensions of lists python can be really slow
3. You can say you can use the NumPy array for using the manipulating. actually yes you can use NumPy array for manipulation but still, your calculation is being done on CPU and it’s still not as fast as tensors
4. Main reason to use tensors is because they can run on GPU we can concurrently do multiple operations using various cores of GPU since GPU is really good at doing mathematical operations ( it’s almost 20x faster than standard CPU) for doing the same operations
5. We can use any data in tensors form we can represent images in form of pixel values, sound waves in form of a decoded array, textual data in form of strings, and many more.
6. With the use of tensors we can do analysis and get results really fast compared to other data structures which we have discussed above
7. In deep learning there is not any replacement for Tensors because of their compatibility of fast calculation and tremendous use

Thanks for reading.
You can reach out to me,
LinkedIn:https://www.linkedin.com/in/somesh-9188/
Twitter:https://twitter.com/someshfengde

--

--