Tensors
Figure 1: Tensors Visualization

Tensors


?? What are tensors? ????

Tensors are fundamental mathematical objects used to describe various physical properties, similar to scalars and vectors. In fact, tensors are an extension of scalars and vectors, where a scalar represents a zero-rank tensor, and a vector represents a first-rank tensor.

The rank or order of a tensor is determined by the number of directions required to describe it, which also determines the dimensionality of the tensor array. For instance, a first-rank tensor (1D) can be fully described by a 3×1 column vector, while a second-rank tensor (2D) requires nine numbers arranged in a 3×3 matrix. In general, an nth rank tensor can be represented by 3n coefficients.

The necessity for second rank tensors arises when multiple directions are needed to describe certain physical properties. An illustrative example is the description of electrical conductivity in a general, anisotropic crystal. For isotropic conductors following Ohm's law, conductivity can be expressed as:

j = σE

Let's explore different types of tensors:

0D Tensors/Scalars:?

A tensor with zero dimensions is known as a 0D tensor or scalar. For example, (2) or (3) can be considered as 0D tensors. In practical terms, we can represent a 0D tensor using the following code:

import numpy as np?

a = np.array(2)

print(a.ndim)

Output:0

1D Tensors/Vectors:

A tensor with one dimension is referred to as a 1D tensor or vector. For example, [1, 2, 3, 4] can be considered a 1D tensor. In practical terms, we can represent a 1D tensor using the following code:

import numpy as np

a = np.array([1, 2, 3, 4])

print(a.ndim)

Output: 1

The dimension of the vector depends on the number of elements in the array.

2D Tensors/Matrices:

A collection of multiple vectors forms a 2D tensor, also known as a matrix. For example, if we have [1, 2, 3], [4, 5, 6], and [7, 8, 9], combining these vectors creates a 2D tensor. In practical terms, we can represent a 2D tensor using the following code:

import numpy as np?

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(a.ndim)

Output: 2

3D Tensors:?

A three-dimensional array can be referred to as a 3D tensor. A 3D tensor is also known as a cube. Here is an example code representing a 3D tensor:

import numpy as np?

x = np.array([[[56, 183, 1],?

[65, 164, 0]],?

[[85, 176, 1],?

[44, 164, 0]]])

The dimensions of the tensor x are (2, 3, 3), indicating a rank of 3. To access elements like 56, 176, or 44, you would use the respective indices: x[0][0][0], x[1][0][1], x[1][1][0]. As each number requires three indices for access, the rank of the tensor is 3.

No alt text provided for this image
Figure 2: 3D tensor visualization

4D Tensors:

?A vector of 3D tensors is called a 4D tensor. 4D tensors are often used in image analysis. Here's a practical example:

import numpy as np?

tensor_4D = np.array([[[[0, 1, 1],?

[2, 3, 3],

[1, 3, 2]],?

[[1, 3, 2],?

[2, 4, 2],?

[0, 1, 1]]],?

[[[0, 3, 1],?

[2, 4, 1],?

[1, 3, 2]],?

[[1, 1, 1],?

[2, 3, 4],?

[1, 3, 2]]],?

[[[2, 2, 4],?

[2, 1, 3],?

[0, 4, 2]],?

[[2, 4, 1],?

[2, 3, 0],?

[1, 3, 3]]]])?

print("Tensor dimensions: \n{}".format(tensor_4D.ndim))

Output: Tensor dimensions: 4

No alt text provided for this image
Figure 3: 4D tensor visualization

5D Tensors:

A matrix of 4D tensors is referred to as a 5D tensor. 5D tensors find their application in video data analysis. Let's consider an example of a 5-minute video with 1080 HD resolution. In this case, the data structure dimension can be calculated as follows: The pixel size is 1080 x 1920 pixels, and the video duration in seconds is 5 x 60 = 300 seconds. If the video is sampled at 10 frames/second, the total number of frames will be 300 x 10 = 3000. Assuming the video has a color depth of 3, the tensor representing this video should have 4 dimensions, with a shape of (3000, 1080, 1920, 3).

Therefore, a single video clip can be represented as a 4D tensor. If we want to store multiple videos, such as 10 video clips with 1080 HD resolution, we would need a 5D tensor. The shape of this 5D tensor would be (3000, 1080, 1920, 3, 10).

Conclusion:

  • A tensor can be thought of as a data container, similar to a multi-dimensional array.
  • Numpy's np.array can be used to create tensors of different dimensions, such as 1D, 2D, 3D, etc.
  • A vector is a 1D tensor, and a matrix is a 2D tensor. A 0D tensor is a scalar or a numerical value.
  • Accessing a specific value in a tensor is also called tensor slicing.
  • Two key attributes of tensors are: A. Rank or axes of the tensor B. Shape of the tensor

By using ndim and shape on a Numpy array, you can determine the rank and shape of the tensor, respectively.

#Tensors #DataScience #MachineLearning #DeepLearning #TensorAnalysis

#artificialintelligence



Abu Zar Zulfikar

AI Engineer | Passionate About Robotics

1 年

Great article! How do tensors differ from matrices and why are they important in deep learning algorithms?

回复
Niha Iqbal

Artificial Intelligence| Machine Learning| NLP

1 年

Great work keep it up!

回复
AqibRehman Pirzada

Data Scientist | AI Engineer | Data Engineer | AIOPS | LLM(NLP) | Data Analyst | 2X Kaggle Expert

1 年

Exceptional Work Keep it Up Zuhaib Ashraf

回复
Waqas Bilal

Data Scientist | R&D | Generative AI | Big Data | LLMs | AI Agents | ETL & Analytics | B2B Sales & Marketing

1 年

Great

回复
Asjad Hashmi

Cybersecurity & Privacy Enthusiast | Web & Mobile Developer | MLSA |

1 年

Amazing insights

回复

要查看或添加评论,请登录

Zuhaib Ashraf的更多文章

  • Feature Transformation Techniques

    Feature Transformation Techniques

    Introduction: Data preprocessing is an important step in machine learning projects. Real-life data can be messy and…

  • Column Transformer and Pipelines in Machine Learning

    Column Transformer and Pipelines in Machine Learning

    Introduction: When starting out or participating in competitions, it may seem beneficial to pre-process data in…

  • Encoding Features

    Encoding Features

    Introduction: Feature encoding is used for the transformation of categorical features into numerical features. Types of…

    9 条评论
  • Introduction to Feature Engineering

    Introduction to Feature Engineering

    Introduction: Feature Engineering is the process of using domain knowledge to extract features from raw data. These…

    2 条评论
  • Understanding Data and performing EDA

    Understanding Data and performing EDA

    Understanding data depends on 2 steps: Step 1: What basic question should be ask? Step 2: Exploratory Data Analysis…

  • How to frame a machine learning model?

    How to frame a machine learning model?

    Introduction: Framing a machine learning problem involves a series of steps to design and structure the problem…

    4 条评论

社区洞察

其他会员也浏览了