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

Some Important functions from Pytorch Library

As we are into this course of deep learning, knowing some of the important functionalities offerred by PyTorch is very helpful in our studies, thus today, I will be telling you all about some of the basic yet useful functions and how to use them.

  • zeros()
  • max()
  • median()
  • sort()
  • bucketize()

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.zeros()

Starting with something very basic but heavily useful. While doing any operations with tensors or matrices, we do require a zero tensor. PyTorch provides a simple and time effective way to do just that.

###Syntax:
torch.zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
###Parameters:
size (int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.

a=torch.zeros(5)
print(a)
a.shape
tensor([0., 0., 0., 0., 0.])
torch.Size([5])