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

Tensor Operations

A short introduction about PyTorch and about the chosen functions.

  • torch.arange()
  • torch.where()
  • torch.combinations()
  • torch.histc()

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.arange(start, end, step)

This function returns 1-D tensor of size [(end-start)/step] with values from the interval [start, end) taken with common difference step beginning from the start.

start - the starting value of the set of points. Default: 0.

end - the end value for the set of points.

step - the gap between each pair of adjacent points (optional). Default : 1.

# Example 1 - working
torch.arange(2, 10)
tensor([2, 3, 4, 5, 6, 7, 8, 9])