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

Top 5 functions

PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook's AI Research lab. It is free and open-source software released under the Modified BSD license.

These are top 5 functions:

  • function 1 - torch.chunk()
  • function 2 - torch.randperm()
  • function 3 - torch.full_like()
  • function 4 - torch.index_select()
  • function 5 - torch.std_mean()

Before we begin, let's install and import PyTorch

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

Function 1 - torch.chunk


Implementation

torch.chunk(input, chunks, dim=0) → List of Tensors

Narrative

Splits a tensor into a specific number of chunks. Each chunk is a view of the input tensor.

Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by chunks.

Example 1

inputs = np.array([[73, 67, 43], 
                   [91, 88, 64], 
                   [92, 87, 64], 
                   [87, 135, 57],  
                   [68, 97, 70]], 
                  dtype='float32')

conv_inputs = torch.from_numpy(inputs)

torch.chunk(conv_inputs, 2)

(tensor([[73., 67., 43.],
         [91., 88., 64.],
         [92., 87., 64.]]), tensor([[ 87., 135.,  57.],
         [ 68.,  97.,  70.]]))