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

Five intersting functions on Pytorch

Pytorch is an open source library used for deep learinig application and heavily optimized for vector operation.It some with out of the box support for GPU.

Here are five intersting function supported by Pytorch.

  • Bitwise operations
  • CPU vs GPU
  • Autograd
  • Histograms
  • Distributions
# Import torch and other required modules
import torch
import time
import matplotlib.pyplot as plt
import numpy as np

Bitwise operations avalible in Pytorch

In computer programming, a bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast and simple action, directly supported by the processor, and is used to manipulate values for comparisons and calculations.

Here I assigned two tensors (a & b) with some binary values and performed bitwise AND, OR and XOR operations.The results can be seen above

# Bitwise operations supported in Pytorch

a = torch.tensor([1, 0, 1])
b = torch.tensor([0,0,0])

and_op = torch.bitwise_and(a,b)
print("Output of 111 AND 000 is ", and_op)

or_op = torch.bitwise_or(a,b)
print("Output of 111 OR 000 is ", or_op)

xor_op = torch.bitwise_xor(a, b)
print("Exclusive OR of 111 and 000 is ", xor_op)
Output of 111 AND 000 is tensor([0, 0, 0]) Output of 111 OR 000 is tensor([1, 0, 1]) Exclusive OR of 111 and 000 is tensor([1, 0, 1])

CPU vs GPU Speedup

Pytorch allows us to use gpu for compute without installing cuDNN libraries. The following example uses CPU and GPU for multiplying two matrices and shows the speed gain from CPU to GPU.