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

Assignment 1: 5 Interesting PyTorch Functions

An short introduction about PyTorch and about the chosen 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 release in 2016 and is written in C++, Python. It is free and open-source software released under the Modified BSD license. PyTorch is very popular in research labs of Facebook, Microsoft, Uber etc. Not yet on many production servers — that are ruled by fromeworks like TensorFlow (Backed by Google). TensorFlow, use static computation graphs, PyTorch uses dynamic computation, which allows greater flexibility in building complex architectures.

  • torch.unique()
  • torch.where()
  • torch.randperm()
  • torch.combinations()
  • torch.chunk()

Before we begin, let's install and import PyTorch

# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
# Import torch and other required modules
import torch

Function 1 - torch.unique

This function returns the unique elements of the input tensor.

Format:
torch.unique(*args, **kwargs)

Parameters:

  • input (Tensor) – the input tensor
  • sorted (bool) – Whether to sort the unique elements in ascending order before returning as output.
  • return_inverse (bool) – Whether to also return the indices for where elements in the original input ended up in the returned unique list.
  • return_counts (bool) – Whether to also return the counts for each unique element.
  • dim (int) – the dimension to apply unique. If None, the unique of the flattened input is returned. default: None

Returns:
A tensor or a tuple of tensors containing

  • output (Tensor): the output list of unique scalar elements.
  • inverse_indices (Tensor): (optional) if return_inverse is True, there will be an additional returned tensor (same shape as input) representing the indices for where elements in the original input map to in the output; otherwise, this function will only return a single tensor.
  • counts (Tensor): (optional) if return_counts is True, there will be an additional returned tensor (same shape as output or output.size(dim), if dim was specified) representing the number of occurrences for each unique value or tensor.

Return type:
(Tensor, Tensor (optional), Tensor (optional))

# Example 1 - working
output1, inverse_indices = torch.unique(torch.tensor([[50, -10], [-5, 14],[-10, 23]], dtype=torch.long), sorted=True, return_inverse=True)
print(output1)
print(inverse_indices)
tensor([-10, -5, 14, 23, 50]) tensor([[4, 0], [1, 2], [0, 3]])