PyTorch Tensors

The basic building block for Pytorch is a tensor. A tensor is an n-dimensional array that support multiple numeric and boolean types for machine learning tasks. They are an extension of numpy arrays but have computational properties that make them suitable to high-dimensional mathematical operations which are required for Machine Learning and especially Deep Learning.

Creating a Tensor

The following example creates a Tensor using the torch.tensor function.

 # importing packages: torch and numpy
import torch
import numpy as np

x = torch.tensor([1, 2, 3])
x
tensor([1, 2, 3])

Size Property

The size property returns the number of elements in a tensor by the given dimension.

 x.size()
torch.Size([3])

Here is another example of a $2x2$ tensor creation.

# 2 x 2 tensor with size 2
y = torch.tensor([[1, 2], 
                  [3, 4]])

# 2 x 2 tensor with size 3
z = torch.tensor([[2, 3, 4], 
                  [6, 3, 3], 
                  [0, 1, 4]])

print("The size of Tensor y is:", y.size(), "\n" 
      "The size of Tensor z is:", z.size())
The size of Tensor y is: torch.Size([2, 2]) The size of Tensor z is: torch.Size([3, 3])
x.dim(), b.dim(), z.dim()
(1, 2, 2)
x.size(), y.size(), z.size()
(torch.Size([3]), torch.Size([2, 2]), torch.Size([3, 3]))

Float Tensors

As noted earlier, tensors can store different numeric types. This becomes important when training deep learning models as unexpected numeric types will result in errors during training or the development process.

torch.FloatTensor([[1, 2], [2, 1]])
tensor([[1., 2.], [2., 1.]])

Alternatively, you can define Float tensors

torch.tensor( [[1, 2], [2, 1]], dtype=torch.float32)
tensor([[1., 2.], [2., 1.]])

Boolean Tensors

We can also define tensors as bool by setting the dtype=torch.bool. All values set to $0$ will be False while every other value will be True.

torch.tensor( [[1, 0, 2], [5, -3, 0]], dtype=torch.bool)
tensor([[ True, False, True], [ True, True, False]])