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

Top 5 interesting Torch functions to work with-

PyTorch is another library function of Python, which is mainly used for Machine Learning. It is easy to use and efficient, thanks to an easy and fast scripting language, LuaJIT, and an underlying C/CUDA implementation

  • torch.as_strided
  • torch.heaviside
  • torch.bernoulli
  • torch.eig
  • torch.pinverse

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch

Function 1 - torch.as_strided

"Stride" in English vocabulary means "steps" or "jumps", so if suppose we want to print some of the elements within a tensor which is at a regular interval of suppose 2 or any other number, we will use this function.

# Example 1 - 
#Taking random values for our tensor "a" of size 3x3
a = torch.randn(3,3)
a
tensor([[-0.6512, -1.5664,  0.5275],
        [ 0.9953,  1.1550, -0.3242],
        [ 1.2212, -0.4989, -1.7724]])
#Now, using the as_strided function
strided_a = torch.as_strided(a,(2,2),(1,2))
strided_a
tensor([[-0.6512,  0.5275],
        [-1.5664,  0.9953]])