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

Assignment 1 - All About torch.Tensor

Deep Learning with PyTorch: Zero to GANs

About PyTorch

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 (FAIR). It is free and open-source software released under the Modified BSD license. Although the Python interface is more polished and the primary focus of development, PyTorch also has a C++ interface.

A number of pieces of Deep Learning software are built on top of PyTorch, including Uber's Pyro, HuggingFace's Transformers, and Catalyst.

PyTorch provides two high-level features:

Currently, PyTorch is competing against some renowned deep learning frameworks viz., Tensorflow, Apache MXNet, etc.

PyTorch tensors

PyTorch defines a class called Tensor (torch.Tensor) to store and operate on homogeneous multidimensional rectangular arrays of numbers. PyTorch Tensors are similar to NumPy Arrays, but can also be operated on a CUDA-capable Nvidia GPU. PyTorch supports various sub-types of Tensors.

This notebook is an attempt to explore some of the PyTorch functions which operates on tensors.

The functions explained in this notebook are:

  • torch.trace(input) → Tensor
  • torch.tril(input, diagonal=0, out=None) → Tensor
  • torch.tril_indices(row, col, offset=0, dtype=torch.long, device='cpu', layout=torch.strided) → Tensor
  • torch.addbmm(input, batch1, batch2, *, beta=1, alpha=1, out=None) → Tensor
  • torch.dot(input, tensor) → Tensor
# Import torch and other required modules
import torch

Function 1 - torch.trace(input) → Tensor

Returns the trace (i.e., sum of the elements of the diagonal) of the input 2-D matrix.

# Example 1 - working
x = torch.arange(34., 43.,).view(3, 3)
torch.trace(x)
tensor(114.)
  1. creates a 3 * 3 tensor with values in the range 34 to 43 (43 excluded) and stores it in a variable x

  2. prints the trace of the tensor x