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

5 Functions For Easy Workflow in Pytorch

PyTorch is an optimized open source tensor library for deep learning using GPUs and CPUs. It is based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook's AI Research lab. It is highly popular for its Automatic Differentiation feature and CUDA support.

The torch package contains data structures for multi-dimensional tensors and mathematical operations over these are defined.

Let's explore the below chosen functions from the torch package and know their use:

  • torch.reciprocal
  • torch.unbind
  • torch.sort
  • torch.matrix_power
  • torch.unique

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

This function returns a new tensor with the reciprocal of the elements of input.

reciprocal.JPG

# Example 1 - working
input = torch.randn(4)
print(input)
torch.reciprocal(input)
tensor([-0.2622, -0.9938, -0.2657, 1.5732])
tensor([-3.8145, -1.0063, -3.7630,  0.6356])