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

Introduction to Torch package

The torch package contains data structures for multi-dimensional tensors and mathematical operations over these are defined. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types, and other useful utilities.

We'll look into the below functions

  • torch.numel
  • torch.arange
  • torch.linspace
  • torch.logspace
  • torch.cat
# Import torch and other required modules
import torch

Function 1 - torch.numel(input) → int

Returns the total number of elements in the input tensor.

# Example 1
# https://pytorch.org/docs/stable/torch.html
a = torch.randn(1, 2, 3, 4, 5)
torch.numel(a)
120

The above example create a tensor of 120 elements