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

Exploring PyTorch tensor functions

This is a short exploration on some of the functions used in a PyTorch tensor

A short introduction about PyTorch and about the chosen functions.

  • torch.linspace
  • torch.log
  • torch.mean
  • torch.reshape
  • torch.t
# Import torch and other required modules
import torch

Function 1 - torch.linspace

The torch.linspace function returns a 1-D tensor with points between a starting parameter and ending parameter. The number of points depends on your steps parameter.

# Example 1 - working
torch.linspace(5,60,steps=4,dtype=torch.int32)
tensor([ 5, 23, 41, 60], dtype=torch.int32)

This example returns a tensor with 4 integers from 5 to 60 evenly spaced. Notice how the integers are rounded since the desired data type is an Integer (torch.int32).