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

Interesting 5 functions of pytorch

Let's familiarize ourselves with pytorch using few of it's basic functions.

An short introduction about PyTorch and about the chosen functions.

  • torch.transpose
  • torch.inverse
  • torch.mm
  • torch.arrange
  • torch.reshape
# Import torch and create one tesor
import torch
A = torch.rand(3,2)
print(A.shape)
print(A)
torch.Size([3, 2]) tensor([[0.4263, 0.1521], [0.4864, 0.1145], [0.3316, 0.6102]])

Function 1 - torch.transpose

This function will give you identity matrix. The identity matrix is the one where the elements of principle diagonal are one and everything else is zero.

A_transpose_one = torch.transpose(A, 0, 1)
print(A_transpose_one.shape)
print(A_transpose_one)
torch.Size([2, 3]) tensor([[0.4263, 0.4864, 0.3316], [0.1521, 0.1145, 0.6102]])

This function swaps rows to columns and columns to rows, as you can see shape of tensor A was 3 X 2, but transposing it is 2 X 3.