import numpy as np
import torch
= np.array([1, 2, 3])
array print(f'NumPy Array: {array}')
= torch.tensor([1, 2, 3])
tensor print(f'PyTorch Tensor: {tensor}')
NumPy Array: [1 2 3]
PyTorch Tensor: tensor([1, 2, 3])
The difference between tensors, arrays, and matrices
Maxime Lbonne
March 28, 2022
What is a tensor, exactly?
Most deep learning practitioners know about them but can’t pinpoint an exact definition.
TensorFlow, PyTorch: every deep learning framework relies on the same basic object: tensors. They’re used to store almost everything in deep learning: input data, weights, biases, predictions, etc.
And yet, their definition is incredibly fuzzy: the Wikipedia category alone has over 100 pages related to tensors.
In this article, we’ll give a definitive answer to the following question: what is a tensor in neural networks?
So why are there so many definitions?
It’s quite simple: different fields have different definitions. Tensors in mathematics are not quite the same as tensors in physics, which are different from tensors in computer science.
These definitions can be divided into two categories: tensors as a data structure or as objects (in an object-oriented programming sense).
This is why you see a lot of people (sometimes quite pedantically) saying “tensors are not n-dimensional arrays/matrices”: they don’t talk about data structures, but about objects with properties.
Even the same words have different meanings. For instance, in computer science, a 2D tensor is a matrix (it’s a tensor of rank 2). In linear algebra, a tensor with 2 dimensions means it only stores two values. The rank also has a completely different definition: it is the maximum number of its linearly independent column (or row) vectors.
In computer science, we’re only interested in a definition focused on the data structure. From this point of view, tensors truly are a generalization in n dimensions of matrices.
But we’re still missing an important nuance when talking about tensors specifically in the context of deep learning…
So why are they called “tensors” instead of “multidimensional arrays”? Ok, it is shorter, but is it all there is to it? Actually, people make an implicit assumption when they talk about tensors.
PyTorch’s official documentation gives us a practical answer:
The biggest difference between a numpy array and a PyTorch Tensor is that a PyTorch Tensor can run on either CPU or GPU.
In deep learning, we need performance to compute a lot of matrix multiplications in a highly parallel way. These matrices (and n-dimensional arrays in general) are generally stored and processed on GPUs to speed up training and inference times.
This is what was missing in our previous definition: tensors in deep learning are not just n-dimensional arrays, there’s also the implicit assumption they can be run on a GPU.
Let’s see the difference between NumPy arrays and PyTorch tensors.
These two objects are very similar: we can initialize a 1D array and a 1D tensor with nearly the same syntax. They also share a lot of methods and can be easily converted into one another.
You can find the code used in this article at this address
import numpy as np
import torch
array = np.array([1, 2, 3])
print(f'NumPy Array: {array}')
tensor = torch.tensor([1, 2, 3])
print(f'PyTorch Tensor: {tensor}')
NumPy Array: [1 2 3]
PyTorch Tensor: tensor([1, 2, 3])
Initializing 2D arrays and 2D tensors is not more complicated.
x = np.array([[1, 2, 3],
[4, 5, 6]])
print(f'NumPy Array:\n{x}')
x = torch.tensor([[1, 2, 3],
[4, 5, 6]])
print(f'\nPyTorch Tensor:\n{x}')
NumPy Array:
[[1 2 3]
[4 5 6]]
PyTorch Tensor:
tensor([[1, 2, 3],
[4, 5, 6]])
We said that the only difference between tensors and arrays was the fact that tensors can be run on GPUs. So in the end, this distinction is based on performance. But is this boost that important?
Let’s compare the performance between NumPy arrays and PyTorch tensors on matrix multiplication. In the following example, we randomly initialize 4D arrays/tensors and multiply them.
As we can see, PyTorch tensors completed outperformed NumPy arrays: they completed the multiplication 52 times faster!
We could attribute this performance to different factors, such as:
float64
format, whereas PyTorch tensors leverage the more efficient float32
format. However, even when NumPy arrays are converted to float32
, PyTorch tensors are still 40 times faster.Even when combining both factors, PyTorch tensors prove to be 1.4 times faster, showing that NumPy arrays are truly less performant for matrix multiplication.
This is the true power of tensors: they’re blazingly fast! Performance might vary depending on the dimensions, the implementation, and the hardware, but this speed is the reason why tensors (and not arrays) are so common in deep learning.
In this article, we wrote a definition of tensors based on:
Here’s how we can summarize it in one sentence:
Tensors are n-dimensional arrays with the implicit assumption that they can run on a GPU.
Finally, we saw the difference in performance between tensors and arrays, which motivates the need for tensors in deep learning.
So next time someone tries to explain to you that tensors are not exactly a generalization of matrices, you’ll know that they’re right in a particular definition of tensors, but not in the computer science/deep learning one.
If you’re looking for more data science and machine learning content in n-dimensions, please follow me on twitter @maximelabonne. 📣