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

5 PyTorch Functions And How To Use Them

PyTorch is free and an open-source software machine learning library built for python, it is based on the Torch library.

  • TORCH.IS_NONZERO
  • TORCH.RAND
  • TORCH.SQRT
  • TORCH.POW
  • TORCH.MEAN

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

Checks if the input is a single element tensor which is not equal to zero after type conversions.

torch.is_nonzero(torch.tensor([-2]))
torch.is_nonzero(torch.tensor([2.23]))
True