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

5 Simple Math operations available within PyTorch

PyTorch is an open source neural network library that looks very much pythonic and it is also very similar to numpy but with more features and especially GPUs.

Some of the functions that are available in PyTorch are:

  • torch.exp
  • torch.neg
  • torch.rsqrt
  • torch.round
  • torch.sin

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

It is a function that returns a new tensor with the exponential of the elements of the input tensor.

# Example 1 - working
new_tensor = torch.tensor(3.)
torch.exp(new_tensor)
tensor(20.0855)