Updated 4 years ago
Torch - Tensor operations
Playing with the dimensions and shape of the tensor
PyTorch is an open source machine learning library based on the Torch library. Here are five functions that alter the shape and/or dimensions of the given tensor -
- repeat
- repeat_interleave
- expand
- reshape
- squeeze
# Import torch and other required modules
import torch
import sys
Function 1 - torch.Tensor.repeat
Returns the tensor repeated along the specified dimensions, like tiling.
torch.Tensor.repeat(*sizes)
- sizes - torch.Size or int, that specifies the number of times each dimension has to be repeated.
The storage size of the resulting tensor varies from the original tensor.
Any changes in the new tensor are not reflected in its every tile.
# Example 1 - working
x = torch.tensor([[3, 4, 5], [6, 7, 8]])
print(x.size(), "Size: ", sys.getsizeof(x.storage()))
y = x.repeat(1, 2)
print(y, y.size(), "Size: ", sys.getsizeof(y.storage()))
# changing a value
y[0][2] = 0
print(y)
torch.Size([2, 3]) Size: 112
tensor([[3, 4, 5, 3, 4, 5],
[6, 7, 8, 6, 7, 8]]) torch.Size([2, 6]) Size: 160
tensor([[3, 4, 0, 3, 4, 5],
[6, 7, 8, 6, 7, 8]])
The tensor is repeated once in dim 0 and twice in dim 1. The shape of tensor y is an element wise multiplication between the original shape and the input to the repeat function.
Shape of y: [2 * 1, 3 * 2] = [2, 6]
Changing value at y[0][2] does not change the value of 5 at y[0][5].