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

5 Pytorch Tensor basic operations must know.

An short introduction about PyTorch and about the chosen functions.

  • Reshaping a Tensor into required size
  • Index of Maximum value of a Tensor
  • Expanding a tensor
  • Running Maximum value of a Tensor
  • Concatenation Tensors
# Import torch and other required modules
import torch

Function 1 - torch.Tensor.reshape()

Reshaping the size of Tensor into different shapes such that it has same number of elements.

# Example 1 - working (change this)
e = torch.rand(4,3)
print("Original e:" , e)
print("Original e size :" ,e.shape)
print("---------------------------------------------")
# Reshaping into (2,6)
e_reshaped = e.reshape(2,6) 
print("Reshaped e:", e_reshaped)
print("Reshaped e size : ",e_reshaped.shape)
Original e: tensor([[0.7090, 0.9425, 0.3574], [0.9850, 0.3297, 0.2887], [0.0280, 0.0079, 0.3695], [0.7234, 0.8877, 0.9738]]) Original e size : torch.Size([4, 3]) --------------------------------------------- Reshaped e: tensor([[0.7090, 0.9425, 0.3574, 0.9850, 0.3297, 0.2887], [0.0280, 0.0079, 0.3695, 0.7234, 0.8877, 0.9738]]) Reshaped e size : torch.Size([2, 6])

A Tensor is reshaped into (2,6) from its original shape (4,3).