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

Pytorch functions for specific use

Some interesting begineer level pytorch functions and their operations that Everyone should know!

  • torch.cat
  • torch.reshape
  • torch.complex
  • torch.chunk
  • torch.round

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.cat

It concatenates tensors across the given axis.

# Example 1 - working

a = torch.tensor([[1,2,3],[1,2,3]])

b = torch.tensor([[4,5,6],[4,5,6]])

torch.cat((a,b))
tensor([[1, 2, 3],
        [1, 2, 3],
        [4, 5, 6],
        [4, 5, 6]])