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

Tensor functions are cool!

5 most useful functions I have come across.

PyTorch is an open source machine learning library based on the Torch library. It is used for computer vision, natural language processing and many other applications. This library is developed by Facebook's AI Research lab. Following is the list of 5 PyTorch functions -

  • torch.linspace
  • torch.full
  • torch.reshape
  • torch.fmod
  • torch.max

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch

Function 1 - torch.linspace

This function is used to create one dimensional evenly spaced tensor. Syntax of this function is torch.linspace(start, end, steps) where 'start' represents initial value of the tensor and 'end' represents last value of the tensor, 'steps' represents that how many values we want in this tensor.

# Example 1 - working
x = torch.linspace(4,10,20)
x
tensor([ 4.0000,  4.3158,  4.6316,  4.9474,  5.2632,  5.5789,  5.8947,  6.2105,
         6.5263,  6.8421,  7.1579,  7.4737,  7.7895,  8.1053,  8.4211,  8.7368,
         9.0526,  9.3684,  9.6842, 10.0000])

x is a tensor which has starting value 4 and last value 10. It has total 20 evenly spaced values as we gave 20 in the 3rd input which is the 'steps' argument.