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

5 PyTorch functions which may be you misunderstood

An short introduction about PyTorch and about the chosen functions.

  • Linspace
  • Arange
  • Clamp
  • Argmax
  • Var_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.LINSPACE

The function torch.linspace() returns a one-dimensional tensor of steps equally spaced points between start and end (both values are inclusive).

Basically it creates the tensor list from start value to end value with number of elements is step you provideed.

Syntax:

torch.linspace(start, end, steps=100, out=None)

Parameters:

start: the starting value for the set of point. (inclusive)

end: the ending value for the set of points. (inclusive)

steps: the gap between each pair of adjacent points. Default: 100.

requires_grad(bool, optional) – If autograd should record operations on the returned tensor. Default: False.

# Example 1 - linspace
torch.linspace(start=-40, end=80, steps=13)
tensor([-40., -30., -20., -10.,   0.,  10.,  20.,  30.,  40.,  50.,  60.,  70.,
         80.])