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

Five functions from Torch

PyTorch is an amazing deep learning library for tensor operations and deep learning, here is a list of 5 amazing functions available in the torch module.

  • torch.complex
  • torch.polar
  • torch.heaviside
  • torch.chunk
  • torch.where

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

parameters:

  • real: a tensor of datatype float or double.
  • imag: a tensor of datatype float

Given a tensor real that represents the real part of a complex number, and imag that represents the imaginary part this function will return a new tensor that represents complex number that following rule:

real + (imag)j

torch.tensor([1,2], dtype=torch.float32).shape
torch.Size([2])