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

Some simple and useful Pytorch functions

PyTorch is an open source library for performing machine learning operations, and was developed by Facebook AI. It has applications in natural language processing and computer vision. It's as very pythonic, and easy to learn.

  • torch.rand()
  • torch.chunk()
  • torch.argmax()
  • torch.neg()
  • torch.std()

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

Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)[0,1)

The shape of the tensor is defined by the variable argument size.

# Example 1 - Working
torch.rand(4)
tensor([0.7851, 0.9384, 0.3397, 0.7694])