Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
5 Useful Tensor Operations in PyTorch
This notebook discusses about 5 useful tensor operations that help in matrix manipulation, conditional operation and reshaping techniques.
PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook's AI Research lab.
The functions discussed in this notebook are:
- torch.narrow
- torch.matmul
- torch.where
- torch.all
- torch.squeeze
# Import torch and other required modules
import torch
Function 1 - torch.narrow
The narrow
method returns a narrowed version of the original tensor i.e., used to slice the tensors by defining the dimension
, start
and length
parameters.
m = torch.randn(2,3,4)
print(m)
# torch.narrow(tensor, dimension, start, length)
torch.narrow(m,1,2,1)
tensor([[[ 1.4695, -0.0572, -0.8993, 1.0886],
[-0.1281, 1.5652, -0.0577, 0.5780],
[ 0.3634, -1.1730, -0.6827, 0.5164]],
[[-0.8064, -0.4728, -0.3439, -1.7098],
[ 0.8269, 0.6465, -1.5112, 0.9079],
[ 0.5152, -0.3883, -1.4499, 0.6858]]])
tensor([[[ 0.3634, -1.1730, -0.6827, 0.5164]],
[[ 0.5152, -0.3883, -1.4499, 0.6858]]])
The tensor m
is narrowed (sliced) along the dimension
(axis) = 1
starting from the index position start
= 2
to ending start
+length
= 2
+1
= 3
, with ending index exclusive. One important thing to notice is that the dimension
value ranges from -3
to 2
, both inclusive.