Introduction to Deep Learning with PyTorch
Chapter 3: PyTorch Tensors
Let's create tensors!
There are plenty of ways to initialise a torch tensor. Let’s have a look at some of them.
Initialising from list
The most straightforward way to initialise a Tensor is to directly do it from a list:
import torch
tensor = torch.Tensor([[1, 2],
[3, 4]])
print(tensor)
Of course, if the list does not represent a valid multi-dimensional matrix (where all dimensions have the same length), the initialisation will return an error. For example,
import torch
tensor = torch.Tensor([[1, 2],
[3]])
ValueError: expected sequence of length 2 at dim 1 (got 1)
Initialising from numpy array
If you are used to the numpy
library, you’ll be happy to know that torch has already a function for automatically converting a numpy array to a torch tensor.
import numpy as np
numpy_array = np.array([[1, 2],
[3, 4]])
tensor = torch.from_numpy(numpy_array)
Some other functions
There are plenty of functions in torch
for initialising tensors.
Those functions are sometimes very similar to the ones available in numpy
.
Creating a tensor full of 0s
import torch
shape = (3, 4)
tensor_zeros_1 = torch.zeros(size=shape) # creating 3x4 tensor of 0s
tensor = torch.Tensor([[1, 2],
[3, 4]])
tensor_zeros_2 = torch.zeros_like(tensor) # creating 2x2 tensor of 0s
Creating a tensor full of 1s
import torch
shape = (3, 4)
tensor_ones_1 = torch.ones(size=shape) # creating 3x4 tensor of 1s
tensor = torch.Tensor([[1, 2],
[3, 4]])
tensor_ones_2 = torch.ones_like(tensor) # creating 2x2 tensor of 1s
Creating random tensors
import torch
shape = (3, 4)
tensor = torch.Tensor([[1, 2],
[3, 4]])
# creating 3x4 tensor where each component follows normal distribution with mean 0 and standard deviation 1
tensor_random_normal_1 = torch.randn(size=shape)
tensor_random_normal_2 = torch.randn_like(tensor)
# creating 3x4 tensor following uniform distribution between 0 and 1
tensor_random_uniform_1 = torch.rand(size=shape)
tensor_random_uniform_2 = torch.rand_like(tensor)