A tensor is a fundamental concept in mathematics and computer science, especially in fields like machine learning and deep learning. It is essentially a multidimensional array or generalization of matrices. You can think of it as a way to organize and represent data.
Scalar (0D tensor): A single number.
Example: 5
, -3.14
, 2.0
Vector (1D tensor): A one-dimensional array or list of numbers.
Example: [1, 2, 3, 4]
Matrix (2D tensor): A two-dimensional array, usually represented as a table with rows and columns.
Example:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Higher-dimensional tensors (3D, 4D, etc.): A tensor can have more than two dimensions, which could be used to represent more complex structures like images, videos, or batches of data.
Example of a 3D tensor (used in image processing):
```
[[[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9], [10, 11, 12]]] ```
For example: - A color image is typically represented as a 3D tensor: height, width, and the three color channels (RGB). - A batch of images can be represented as a 4D tensor: batch size, height, width, and the color channels (RGB).
import torch
# 0D tensor (scalar)
scalar = torch.tensor(5)
# 1D tensor (vector)
vector = torch.tensor([1, 2, 3, 4])
# 2D tensor (matrix)
matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])
# 3D tensor (example for image data)
tensor_3d = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(scalar)
print(vector)
print(matrix)
print(tensor_3d)
If you're working with image, video, or text data in machine learning, you're most likely working with tensors.