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

First steps in familiarizing interesting PyTorch operations

PyTorch is an open souce machine learning framework that facilitates programming in machine learning. PyTorch, basically, is a vast library of functions that eases a developers work by using many pre-defined functions. The functions I chose vary vastly in their respective functions but are very useful nonetheless.

  • function 1 - torch.polar()
  • function 2 - torch.reshape()
  • function 3 - torch.argmax()
  • function 4 - torch.rsqrt()
  • function 5 - torch.allclose()

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.polar()

This function constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute value and angle.

Complex numbers play a very vital role in machine learning. We ought to know the ways we represent complex numbers too. The most common representation is the polar form. Simply put as:

Z = r(cos(angle) + j.sin(angle))

Instead of the conventional i for sqrt(-1), I took j because that's how python represents it.

r = torch.tensor(3.)
angle = torch.tensor(3.14)
z = torch.polar(r, angle)
z
tensor(-3.0000+0.0048j)