>>
 >>

Tensor Dimensions

There are some import concepts that you will need to understand in order to work with AI programming. A good starting point is to understand tensors and their associated dimensining. This knowledge is critical for training, deploying and inferring from AI models.

What is a tensor?

From a purely mathematical standpoint a tensor is an n-dimensional sized list of numbers with n being any positive integer. The commonly used terminology differs a little in that the term "tensor" is used for a matrix with 3 or more dimensions.

  • A 0-dimensional array is usually referred to as a scalar (a single number)
  • A 1-dimensional array is usually referred to as a vector (e.g [x, y, z])
  • A 2-dimensional array is usually referred to as a matrix e.g:
    [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
  • A higher dimensional array is usually referred to as a tensor

When working with programming packages such a PyTorch a tensor data type can be used for any sized array. Let's have a look at what a tensor looks like in PyTorch:

import torch # Define a 1-dimensional tensor that contains 3 values my_tensor = torch.tensor([1, 2, 3]) # Print the tensor object and data type print(my_tensor) # Print the tensor data type print(my_tensor.dtype) >> tensor([1, 2, 3]) torch.int64

We have successfully created a PyTorch tensor of type torch.int64. Generally when we are dealing with AI models we will be using floating point numbers rather than integers. This can be achieved in PyTorch by defining a float tensor, denoted as one of torch.float, torch.float32 or torch.FloatTensor for 32-bit float tensors. For more information on the available data types see the PyTorch documentation.

# Define a 1-dimensional tensor that contains 5 values my_tensor = torch.tensor([1, 2, 3, 4.0, 5.5], dtype=torch.float) # Print the tensor object print(my_tensor) # Print the tensor data type print(my_tensor.dtype) >> tensor([1.0000, 2.0000, 3.0000, 4.0000, 5.5000]) torch.float32

What is a Dimension??

Let's start by thinking about the difference between 1-dimensional, 2-dimensional and 3-dimensional spaces. A one dimensional space is a space that can have any point represented by only single number e.g. 25. A 2 dimensional space is a space where the points are referenced by a pair of numbers e.g. [25, 32]. A 3 dimensional space is a space where each coordinate is referenced by 3 numbers e.g.[25, 32, 45]. Note that in every case only a 1-dimensional array of numbers is needed to reference a point in the n-dimensional space.

The qustion is, what do these numbers represent? They can actually represent whatever we want them to. If we want to define map coordinates for example, then the values would represent the coordinates in each direction on the map. If we had a 2-dimensional map (like Google earth) we could define a 1-d array containing 2 values to represent X and Y coordinates, or Latitude and Longitude. If we had a 3-dimensional map (like a computer game or rendering software such as Blender) we could define a 1-dimensional array containing 3 numbers which represent X, Y and Z locations.

But what if we don't want to represent coordinates? Well that's fine as we can define the tensor to represent whatever we like. For examle we could define a 2-dimensional array to represent a paragraph in a book ([5, 3] could be page 5, paragraph 3).

Bonus: What is the 4th Dimension? 🔽

(Use arrow to expand section)

Working With Tensors

Now that we understand what tensors are we need to understand how to work with them in PyTorch. The first thing worth learning is the .shape attribute. This is used to return an array which specifies the size of every dimension in the tensor. # Define a 2-dimensional tensor that contains 6 values my_tensor = torch.rand((2,3), dtype=torch.float) # Print the tensor object print(my_tensor) # Print the tensor shape print(my_tensor.shape) # Print the size of the first dimension print("First dimension size:", my_tensor.shape[0]) >> tensor([[0.1812, 0.4027, 0.9661], [0.5826, 0.2785, 0.8558]]) torch.Size([2, 3]) First dimension size: 2