What Is A Tensor? An Overview In 4 Easy Points

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?

In this article let us look at:

  1. What is a Tensor?
  2. Tensors in Python
  3. Element-Wise Tensor Operations
  4. Tensor Product

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 Addition
  • 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:

# tensor addition

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.

ALSO READ

Related Articles

loader
Please wait while your application is being created.
Request Callback