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

Magical 5 functions in PyTorch

Learn some interesing functions with it's concepts.

An short introduction about PyTorch and about the chosen functions.

  • torch.storage()
  • torch.stride()
  • torch.clone()
  • torch.transpose()
  • torch.contiguous()
# Import torch and other required modules
import torch

Function 1 - torch.storage()

A torch.Storage is a contiguous, one-dimensional array of a single data type.

# Example 1
points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])
points.storage()
 1.0
 4.0
 2.0
 1.0
 3.0
 5.0
[torch.FloatStorage of size 6]

As below example, Even though the tensor reports itself as having three rows and two columns, the storage under the hood is a contiguous array of size 6. In this sense, the tensor knows how to translate a pair of indices into a location in the storage.

You can’t index a storage of a 2D tensor by using two indices. The layout of a storage is always one-dimensional, irrespective of the dimensionality of any tensors that may refer to it.