Learn practical skills, build real-world projects, and advance your career

Tensor operations

the pytorch tensor operations that I were interested in are:

  • torch.stack:
  • torch.where:
  • torch.argmax:
  • torch.flatten
  • torch.linspace

Before we begin, let's install and import PyTorch

# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
# Import torch and other required modules
import torch

Function 1 - torch.stack

Concatenates a sequence of tensors along a new dimension.

#lets create tensors
a = torch.tensor(5.)
b = torch.tensor(5.)
c = torch.tensor(5.)
d = torch.tensor(5.)

var1 = [a,b,c,d]
var1
[tensor(5.), tensor(5.), tensor(5.), tensor(5.)]