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

My_Pytorch_Five_Tensors

Pytorch is an open source library used for machine learning and Natural
Language Processing. It is also used in deep learning which involves artificial neural networks.

It is developed by Facebook and even Tesla's autopilot software is built making use of Pytorch.

Pytorch is faster than numpy as it is supported by the GPU and is preferred in Deep Learning.

Pytorch contains a class called 'Tensors' to perform operations on multidimensional rectangular arrays.

The five tensor operations performed in this notebook are:

  • torch.complex
  • torch.arange
  • torch.vstack and torch.hstack
  • torch.cat
  • torch.split

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

It creates a complex tensor having a real part and an imaginary part.

# Example 1 - A simple complex tensor
real = torch.tensor(2.)
img = torch.tensor(5.)
c = torch.complex(real, img)
c
tensor(2.+5.j)