A tensor is the central unit of data in TensorFlow. A tensor consists of a set of primitive values shaped into an array of any number of dimensions. It is basically a multidimensional array similar to a NumPy array. The number of dimensions defines the rank of a tensor. Let's see some of the following examples:
- 3: If we have a single number, the tensor will be considered a rank 0 tensor. This can be a scalar with shape[].
- [2., 2., 1.]: If we have a vector, it will be considered a rank 1 tensor, so this is what we call a vector of shape 3 because it has three elements.
- [[9., 5., 3.], [4., 5., 7]]: A matrix with shape [2, 3] would be a rank 2 tensor.
- [[[8., 3.]], [[7., 9.,]]]: A matrix with shape [2, 1, 2] would be a rank 3 tensor, as you can see in the outermost level we have two elements, then in the next level we have only one element, and in the last dimension, we have two elements. That's why we have 2, 1, and 2 as the values and these are all tensors.