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

Interesting functions to perform basic operations with PyTorch tensors (with code)

A starter guide to perform basic arithmetic operations with PyTorch tensors in Python

This notebook is going to discuss the following functions/ methods in order, skip if you already know how to use one:

  • .add() vs .add_()
  • .cat()
  • .stack()
  • torch.split() and .chunk()
  • .squeeze() vs .unsqueeze()

What is PyTorch?

PyTorch is one of the leading Python frameworks and libraries for Deep Learning application. What makes it different from other Python frameworks is that it is a Python-first framework. This means that PyTorch is directly implemented in Python programming language rather than implementing a Python wrapper on a monolithic C/C++ engine like other frameworks do. Due to the fact that PyTorch is a Deep Learning framework, basic mathematical operations as well as numerical computing can be done through its abstract data structure (tensor) easily. The tensor object is the PyTorch's analog to NumPy's ndarray, and the conversion between them is O(1) operation. It's important to know that PyTorch tensors have overridden the already existing Python arithmatic and matrix operators (+, -, *, @, ...).

# Setting up and importing Jovian version control
# !pip install jovian --upgrade --quiet # Uncomment to install
import jovian

# Importing PyTorch 1.5 in Python 3.8
import torch

Basic Arithmetics: .add() vs .add_()

.add() method performs tensor-tensor addition operation (you can use the operator + for the same function); however, .add() methods adds the two tensors together and updated the first tensor with the result of the addition. This underscore convension is implemented in most tensor operational functions/ methods in PyTorch; take .sub() and .sub_() as another example.

# Example 1: .add()

x = torch.ones(2, 3) #creates a tensors of ones with the specified dimension
y = torch.randn(2, 3) #generates Gaussian distributed pseudorandomized tensor with the specified dimesion

z = x.add(y)

print("x is", x, "\ny is", y, "\nz, the addition, is", z)
x is tensor([[1., 1., 1.], [1., 1., 1.]]) y is tensor([[-0.6746, 0.0586, -0.0393], [ 0.0106, -0.7265, -1.9617]]) z, the addition, is tensor([[ 0.3254, 1.0586, 0.9607], [ 1.0106, 0.2735, -0.9617]])