In PyTorch and TensorFlow, Tensors are a very popular way of storing large amounts of data in artificial intelligence projects. Here, I will show you what they are, and how they work.
What makes a Tensor?
Tensors are made up of Scalars, Vectors, and Matrixes. Scalars are single numbers. Vectors are a line of numbers, and Matrixes are, as the name suggests, Matrixes, or tables, of numbers.
Here is an example: If you are making an image, you can think of Matrixes as images, Scalars as pixels or dots, and Vectors like rows. You can think of Tensors as a Matrix that contains Matrixes.
Yellow: Main tensor
Red: Matrix 1
Cyan/Light Blue: Matrix 2
Orange: Vectors
Green: Scalars
Matrix dimension
Matrixes are tables of numbers, so the number of rows and columns in the matrix is the matrix dimension. Below is an example.
1 | 2 |
3 | 4 |
There are two rows and two columns in this table of numbers or matrix, so the dimensions of this matrix are two by two. Below is another example.
1 | 2 | 3 | 4 |
1 | 2 | 3 | 4 |
1 | 2 | 3 | 4 |
1 | 2 | 3 | 4 |
What I showed you had four rows and columns, so the matrix above is a four-by-four matrix.
Tensor Dimension
Tensor dimensions are made up of three things. Earlier in this post, I mentioned how a tensor is a matrix containing matrixes. The first dimension of a tensor is how many matrixes the tensor should have in it. The next two dimensions are the dimensions you want each matrix to have. For example,
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 |
would be a 4×4 matrix. If you wanted four four-by-four matrixes, you would need to make the first dimension (the number of matrixes to be in the tensor, which, as I said, is a matrix full of matrixes) four. Then, you would want 4×4 matrixes, so you would input the next two dimensions as 4 and 4 for a 4×4 tensor.
Tips
• If you do not input your first dimension (the number of matrixes in the tensor) into a tensor, the number defaults to 1. |
• Tensors are useful for storing mass amounts of data. |
• One of the easiest ways to make a tensor with custom values would be to have a loop running into every scalar in the tensor, thus making every scalar something you choose. |
• Tensors, when stored, are stored unevaluated. This means that your actual data, typically the data you would be storing in a tensor would be numbers, is not actually stored raw, but rather compressed, which makes tensor storage much easier for the machine’s memory, since the data is significantly less complicated. This is what makes tensors so popular for the storage of mass data. If you want to see the actual, uncompressed data of a tensor, you must evaluate it. You can do this with a simple function in both PyTorch and TensorFlow. |
Ishan,
This was my first introduction to Tensors. Now I know the basics thanks to your simple and clear explanation.