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

PyTorch

PyTorch is a Python-based scientific computing package that uses the power of graphics processing units. It is also one of the preferred deep learning research platforms built to provide maximum flexibility and speed. It is known for providing two of the most high-level features; namely, tensor computations with strong GPU acceleration support and building deep neural networks on a tape-based autograd systems.

There are many existing Python libraries which have the potential to change how deep learning and artificial intelligence are performed, and this is one such library. One of the key reasons behind PyTorch’s success is it is completely Pythonic and one can build neural network models effortlessly. It is still a young player when compared to its other competitors, however, it is gaining momentum fast.

list of some function on which we try to implement concept:-n

  • function 1:- torch.permute()
  • function 2:- torch.rand()
  • function 3:- torch.as_tensor()
  • function 4:- torch.add()
  • function 5:- torch.eye()

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

import numpy

Function 1 - torch.permute()

PyTorch torch.permute() rearranges the original tensor according to the desired ordering and returns a new multidimensional rotated tensor. The size of the returned tensor remains the same as that of the original.

# Example 1 - working (change this)


input_var = torch.randn(2,4)# create a tensor of size 2 x 4   
print(input_var.size()) 
  
print(input_var) 
  

input_var = input_var.permute(1, 0)# dimensions permuted  
  

print(input_var.size()) 
  
print(input_var)
torch.Size([2, 4]) tensor([[-0.6792, -1.7894, 0.8109, -0.8402], [-0.0377, 0.6165, 0.0390, -0.2741]]) torch.Size([4, 2]) tensor([[-0.6792, -0.0377], [-1.7894, 0.6165], [ 0.8109, 0.0390], [-0.8402, -0.2741]])