Working with PyTorch Tensors
PyTorch is a popular,open source,optimized tensor library widely used in deep learning and AI Research, developed by a group of researchers at Facebook AI. The torch package contains data structures for multi-dimensional tensors and mathematical operations over these are defined.
Here, we seek to cover some useful functions that the torch package provides for manipulating tensors.
- TORCH.CAT- Concatenates the given sequence of tensors in the given dimension
- TORCH.UNBIND- Removes a tensor dimension
- TORCH.MOVEDIM- Moves the dimension(s) of input at the position(s) in source to the position(s) in destination
- TORCH.SQUEEZE- Returns a tensor with all the dimensions of input of size 1 removed.
- TORCH.UNSQUEEZE- Returns a new tensor with a dimension of size one inserted at the specified position.
Before we begin, let's install and import PyTorch
# Import torch and other required modules
import torch
FUNCTION 1- TORCH.CAT
torch.cat(tensors, dim=0, *, out=None)
-
Concatenates the given sequence of tensors in the given dimension.
-
All tensors must either have the same shape (except in the concatenating dimension) or be empty.
The argument tensors
denotes the sequence of tensors to be concatenated
dim
is an optional argument that specifies the dimension along which we want tensors
to be concatenated (default dim=0
)
out
is an optional keyword argument
# Example 1
ip_tensor_1=torch.tensor([[1,2,3],[4,5,6]])
ip_tensor_2=torch.tensor([[7,8,9],[10,11,12]])
torch.cat((ip_tensor_1,ip_tensor_2),dim=0)
tensor([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
As we specified dim=0
the input tensors have been concatenated along dimension 0.
The input tensors each had shape (2,3) and as the tensors were concatenated along dimension 0, the output tensor is of shape (4,3)