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

5 Pytorch functions and the way of uing them

All of deep learning is computations on tensors, which are generalizations of a matrix that can be indexed in more than 2 dimensions.Many real world problems can be solved using machine learning.So with the help of pytorch we can undertstand how machine learning works and we can also build few models.
As part of this assignment I had chosen 5 functions which are there in pytorch library, and I am going to illustrate those 5 functions with examples.

  • torch.arccos
  • torch.cat
  • torch.pow
  • torch.reciprocal
  • torch.log

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.arccos

This function is used to compute the inverse cosine of each element of the input.Also if we understand how to use this function, then we can automatically understand using torch.arcsin function also.Both are almost similar. Also a point to be noted , the output value will be in 'radians' not in degrees or gradians.

Also a point to be noted is ,if we enter any value >1 or <-1 then output would be nan.

# Example 1:-
input1=torch.tensor([[0.1, 0.5], [0.03, 0.04]])
output1=torch.arccos(input1)
output1
tensor([[1.4706, 1.0472],
        [1.5408, 1.5308]])