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

5 useful PyTorch functions you need to know as a data enthusiast

  1. torch.rand()
  2. torch.mean()
  3. torch.view()
  4. torch.linspace()
  5. torch.expand()

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

This function returns a tensor filled with random numbers from a normal distribution on the interval [0,1).
It takes the following arguments:

  1. size(int) - to define shape of output tensor
  2. dtype(torch.dtype, optional) - to define the datatype of returned tensor
  3. requires_grad(bool, optional) - to allow autograd to record operations on the returned tensor
# Example 1 - working example
torch.rand(9)