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

5 Pytorch functions which will make creating models easy

data-analysis-illustration_124046-146.jpg

Pytorch is a Python module generally used in creating Machine Learning models to predict future events based on previously collected data. It is very similar to Numpy module but it has support for working with GPUs with some speed optimizations which makes it an ideal choice to be used in Deep Learning.

  • torch.eye
  • torch.cat
  • torch.unbind
  • torch.reshape
  • torch.full

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch

Function 1 - torch.eye

torch.eye() is used to create tensors with all diagonal elemets as 1 and all rest of elements as 0.

  • It usually takes in two parameters m & n where n is the number of rows and m is the number of columns.
  • torch.eye() requires atleast on parameparameter for n.
  • If m parameter is not provided then a tensor of nxn will be created.
torch.eye(n=5, m=6)
tensor([[1., 0., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0., 0.],
        [0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 1., 0.]])

Here we have given two parameters ie m ie columns & n ie rows. Hence it creates a tensor of 5 rows and 5 columns.