# What Is A Tensor? An Overview In 4 Easy Points

Share

## Introduction

In machine learning, data should be represented numerically for better understanding. So what is a tensor? This is especially for a neutral network of data representation which is done by the repository known as the tensor. A tensor is the data structure in the form of a grid that can cope with data up to the Nth dimension. They have descriptions of valid linear transformations and can play a pivotal role by encoding multi-dimensional machine learning data. They collect and store data features in the context of machine learning and deep learning. In this article, we will see what is a tensor?

## 1. What is a Tensor?

Tensors are algebraic objects for higher dimensions of scalars, matrices and vectors and are referred to as tensor algebra. They are also known as a multi-dimensional array. It is represented in the form of a grid having an array of numbers and a variable number of axes. It is often interchangeably used for vectors and matrices. So, the difference between vector and matrix is that a vector is a single-dimensional tensor or first-order tensor, whereas a matrix is a two-dimensional tensor or second-order tensor.

## 2. Tensors in Python

Tensors can be used very much effective in pythons, just as vectors and matrices. This can be done by using the N-dimensional array. It can be defined as a list of lists as an in-line to the constructor. 3*3*3 is an example of NumPy ndarray and is easy to get into the brains for better understanding. Rows are first defined over here.

Further, a list of rows is stacked as columns, and a list of columns are categorized as levels in a cube. A three-dimensional tensor is printed in a series of matrices. Each dimension has a layer. In the case of a 3D tensor, the level is specified by axis 0, the row is specified by axis 1, and the column is specified by axis 2.

## 3. Element-Wise Tensor Operations

Tensors are operated in various arithmetic operations. This helps in improving functionality and speedy results. Following are the four main arithmetic operations of a tensor:

• Tensor Subtraction
• Tensor Product
• Tensor Division
• Tensor Addition – Like the basic addition function. Here, the addition of element-wise is with the same dimension is done, resulting in a new tensor with similar dimensions. This results in scalar value addition of the parent tensors.

x111, x121, c131     x112, x122, x132

A = (x211, x221, c231),  (x112, x122, x132)

y111, y121, y131     y112, y122, y132

B = (y211, y221, y231),  (y112, y122, y132)

C = A B

x111 y111, x121 y121, x131 y131     x112 y112, x122 y122, x132 y132

C = (x211 y211, x221 y221, x231 y231),  (x112 y112, x122 y122, x132 y132)

In NumPy, the following would be the data input:

from numpy import array

A = array([

[[1,2,3],    [4,5,6],    [7,8,9]],

[[11,12,13], [14,15,16], [17,18,19]],

[[21,22,23], [24,25,26], [27,28,29]],

])

B = array([

[[1,2,3],    [4,5,6],    [7,8,9]],

[[11,12,13], [14,15,16], [17,18,19]],

[[21,22,23], [24,25,26], [27,28,29]],

])

C = A B

print(C)

When you run, the following will appear

[[[ 2  4  6]

[ 8 10 12]

[14 16 18]]

[[22 24 26]

[28 30 32]

[34 36 38]]

[[42 44 46]

[48 50 52]

[54 56 58]]]

• Tensor Subtraction – Like the basic subtraction function, here, the minus element-wise is with the same dimension is done, resulting in a new tensor with similar dimensions. This results in a scalar value subtraction of the parent tensors.

C= A-B

x111 – y111, x121 – y121, x131 – y131     x112 – y112, x122 – y122, x132 – y132

C = (x211 – y211, x221 – y221, x231 – y231),  (x112 – y112, x122 – y122, x132 – y132)

The following will be the resultant for subtracting the first tensor from the second in NumPy

[[[0 0 0]

[0 0 0]

[0 0 0]]

[[0 0 0]

[0 0 0]

[0 0 0]]

[[0 0 0]

[0 0 0]

[0 0 0]]]

• Tensor Hadamard Product – Like the basic multiplication function, here multiplication of element-wise is with the same dimension is done, resulting in a new tensor with similar dimensions. This results in a scalar value multiplication of the parent tensors. The name Hadamard product is used to differentiate it from the operation between tensors, and the operator ‘o’ is used to indicate the product.

C= A o B

x111 * y111, x121 * y121, x131 * y131     x112 * y112, x122 * y122, x132 * y132

C = (x211 * y211,x221 * y221, x231 * y231),  (x112 * y112, x122 * y122, x132 * y132)

The following will be the resultant for multiplying the first tensor from the second in NumPy

[[[  1   4   9]

[ 16  25  36]

[ 49  64  81]]

[[121 144 169]

[196 225 256]

[289 324 361]]

[[441 484 529]

[576 625 676]

[729 784 841]]]

• Tensor Division – Like the basic division function, here division of element-wise is with the same dimension is done, resulting in a new tensor with similar dimensions. This results in a scalar value division of the parent tensors.

C= A / B

x111 / y111, x121 / y121, x131 / y131     x112 / y112, x122 / y122, x132 / y132

C = (x211 / y211, x221 / y221, x231 / y231),  (x112 / y112, x122 / y122,x132 / y132)

The following will be the resultant for multiplying the first tensor from the second in NumPy

[[[ 1.  1.  1.]

[ 1.  1.  1.]

[ 1.  1.  1.]]

[[ 1.  1.  1.]

[ 1.  1.  1.]

[ 1.  1.  1.]]

[[ 1.  1.  1.]

[ 1.  1.  1.]

[ 1.  1.  1.]]

## 4. Tensor Product

The tensor product can be performed on matrices and vectors as well, and it is denoted by “(x)”. In the case of a tensor a  with q dimension and tensor b with p dimension, the resultant product will be q r dimensions.

## Conclusion

Thus, in this article, we have learned what is a tensor? Tensor is a framework for enabling mathematical operations in an optimized way. They are dynamic and often change while interacting with other mathematical entities and offer quick and accurate results.

There are no right or wrong ways of learning AI and ML technologies – the more, the better! These valuable resources can be the starting point for your journey on how to learn Artificial Intelligence and Machine Learning. Do pursuing AI and ML interest you? If you want to step into the world of emerging tech, you can accelerate your career with this Machine Learning And AI Courses by Jigsaw Academy.

} }