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

5 Basic Tensor Operations to Torch Your Deep Learning Models

Hey There ! As part of the ongoing Deep Learning with PyTorch Course, we have been asked to give a small insight into 5 basic functions that are available in PyTorch. But before we begin, what is PyTorch ? Well , PyTorch is an open source Machine Learning library. It is used for several applications such as Computer Vision and Natural Language Processing, to name a few. So, the five functions that I have chosen are as follows :-

  • torch.numel
  • torch.rand
  • torch.arange
  • torch.add
  • torch.acos

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch
import numpy as np

Function 1 - torch.numel

The Numel function is a very basic function that is part of the PyTorch library. As the name suggests, the function returns the number of elements that are present in the given tensor. There are several applications of this function, for example when you want to train only a particular amount of your data.

# Example 1 - working 
x=torch.tensor([[1, 2], [3, 4.]])
print(torch.numel(x))
4

As you can see here, we have given 4 elements in the form of a 2*2 tensor. The numel function checked for the number of elements present in all the rows and columns of the tensor and promptly returned 4.