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

5 interesting functions in Torch API

An short introduction about PyTorch and about the chosen functions.

  • complex
  • sparse_coo_tensor
  • move_dim
  • hypot
  • 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 (real, imaginary)

Constructs a complex tensor with its real part equal to real and its imaginary part equal to imag.

# Example 1 - working (change this)
real = torch.tensor([1, 2], dtype=torch.float64)
imaginary = torch.tensor([3, 4], dtype=torch.float64)

z = torch.complex(real, imaginary)
print(z)
z.dtype
tensor([1.+3.j, 2.+4.j], dtype=torch.complex128)
torch.complex128