In the context of data science and deep learning (and some physics/engineering fields), a tensor can be thought of as a generalization of scalars, vectors, and matrices to potentially more dimensions.
It's a multi-dimensional array of numbers (scalars).
While tensors have a more rigorous definition in physics and differential geometry involving basis vector transformations, in ML/DL we primarily use them as containers for numerical data with multiple axes.
1. Hierarchy of Tensors by Rank (Order / Number of Dimensions) [[Tensor Rank]]¶
The rank of a tensor is its number of axes or dimensions.
Rank 0 Tensor: A Scalar (a single number). Has 0 axes. Shape: ()
Rank 1 Tensor: A Vector (a 1D array of numbers). Has 1 axis. Shape: (n,)
Rank 2 Tensor: A Matrix (a 2D array of numbers). Has 2 axes (rows, columns). Shape: (m, n)
Rank 3 Tensor: A 3D array of numbers (like a cube or cuboid). Has 3 axes. Shape: (d, m, n).
Example: A color image might be represented as a Rank 3 tensor (height, width, color_channels=3). A batch of grayscale images could be (batch_size, height, width).
Rank n Tensor: An n-dimensional array of numbers. Has \(n\) axes. Shape: \((d_1, d_2, ..., d_n)\).
A tensor \(\mathbf{T}\) requires multiple indices to access its elements. For a [[Tensor Rank|rank 3 tensor]], an element would be \(T_{ijk}\) (element at index \(i\) on axis 0, \(j\) on axis 1, \(k\) on axis 2).
Deep learning libraries (TensorFlow, PyTorch, JAX) use tensors as their fundamental data structure.
Operations like addition, scalar multiplication, and more complex tensor operations (e.g., tensor product, contraction, reshaping) are defined on tensors. These are implemented efficiently in libraries like NumPy, TensorFlow, and PyTorch.