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

Exploring PyTorch tensor functions

This is a short introduction to some of the functions used on a PyTorch tensor

PyTorch is a library to work with tensors.

  • torch.reshape()
  • torch.mean()
  • torch.t()
  • torch.eye()
  • torch.zeros()
# Import torch and other required modules
import torch

Function 1 - torch.reshape

It takes a tensor and the shape we want to change the tensor's shape to,as parameters

# Example 1 - working
x=torch.tensor([1.,2.])
#print(x.shape)
#print(x)
torch.reshape(x,(2,1))
tensor([[1.],
        [2.]])

In the above example we start by creating a tensor of shape([2]) and use the reshape function to reshape it to have a shape of (2,1)