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

Exploration of Pytorch functions

Useful pytorch functions.

An short introduction about PyTorch and about the chosen functions.

  • torch.transpose
  • torch.as_tensor
  • torch.split
  • torch.randn
  • torch.cat
# Import torch and other required modules
import numpy
import torch

Function 1 - torch.transpose

Returns a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.

The resulting out tensor shares it’s underlying storage with the input tensor, so changing the content of one would change the content of the other.

# Example 1 - working (change this)
x = torch.randn(2, 3)
print(x)
t = torch.transpose(x, 0, 1)
t
tensor([[-0.2366, 1.4150, -1.1006], [ 0.5553, 0.1758, -0.8309]])
tensor([[-0.2366,  0.5553],
        [ 1.4150,  0.1758],
        [-1.1006, -0.8309]])

In this example, the dimension of the input tensor are swapped, so a 2x3 tensor becomes 3x2 tensor.