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

PyTorch Functions that seize a newbie's attention

A short introduction about PyTorch and about the chosen functions.

  • torch.full
  • torch.gather
  • torch.narrow
  • torch.argmax
  • torch.lt

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch
import numpy as np

Function 1 - torch.full

Creates a tensor of size size filled with fill_value.

  • The dtype of tensor is inferred from fill_value by default.
    ** Function allows to specify custom dtype.
  • This function also supports specifying the memory layout of the tensor.
    ** Default memory layout is a strided tensor where each element of the tensor takes up as much memory space as its data size. So Kth element in tensor is really a jump to Kth memory stride.
# Example 1 - create a tensor of size (3,2) filled with pi value
e1 = torch.full((3,2), 22/7)
print(e1)
print(e1.type())
tensor([[3.1429, 3.1429], [3.1429, 3.1429], [3.1429, 3.1429]]) torch.FloatTensor

Example 1

Creating a 2-d tensor filled with the value of pi.