Introduction to Deep Learning with PyTorch
Chapter 3: PyTorch Tensors
Let's play with tensors!
Mathematical operations with a scalar
As with numpy arrays, it is easy to perform mathematical operations (addition/subtraction/multiplication/division) on all the components of a tensor with a scalar:
import torch
tensor = torch.Tensor([[1, 2],
[3, 4]])
tensor_add_item = tensor + 5
tensor_multiply_item = tensor / 2
...
print(tensor_add_item, tensor_multiply_item)
Mathematical operations with another tensor
And you can add, multiply, subtract, divide tensors together:
import torch
tensor = torch.Tensor([[1, 2],
[3, 4]])
tensor_add = tensor + tensor
tensor_multiply = tensor * tensor
...
print(tensor_add, tensor_multiply)
Even better, you can easily apply an operation to all the sub-tensors of a tensor. For example:
import torch
tensor = torch.Tensor([[1, 2],
[3, 4]])
row_tensor = torch.Tensor([1, 2])
tensor_subtract = tensor - row_tensor
print(tensor_subtract)
Mathematical functions
The torch
library provides several mathematical functions that can be applied to all elements of a tensor. For
example:
import torch
tensor = torch.Tensor([[1, 2],
[3, 4]])
tensor_exp = torch.exp(tensor)
tensor_log = torch.log(tensor)
... # Every single function you could imagine.
print(tensor_exp, tensor_log)
Statistical operations
There are also plenty of statistical operations available: min
, max
, mean
, … For example:
import torch
tensor = torch.Tensor([[1, 2],
[3, 4]])
tensor_min = torch.min(tensor)
tensor_max = torch.max(tensor)
tensor_mean = torch.mean(tensor)
... # Every single mathematical function you could imagine.
print(tensor_min, tensor_max, tensor_mean)
In practice, you will mostly apply those operations on a single axis. For instance, with the tensors above, we could find the minimal element for each row, or for each column.
import torch
tensor = torch.Tensor([[1, 2],
[3, 4],
[5, 6]]) # shape = (3, 2)
tensor_mean_row = torch.mean(tensor, dim=0) # shape = (2,) Averaging over 1st dimension (along columns)
tensor_mean_col = torch.mean(tensor, dim=1) # shape = (3,) Averaging over 2nd dimension (along rows)
print(tensor_mean_row, tensor_mean_col)