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

Applying math operations to tensors

PyTorch is a library to carry out tensor operations used in deep learning. In this notebook, 5 PyTorch functions have been explored. Namely:

  • square(): Find the square of numbers in a tensor
  • pow(): Find the exponents of numbers in a tensor
  • log(): Find the logarithm of numbers in a tensor
  • sin(): Find the sine of numbers in a tensor
  • var(): Find the variance of numbers in a tensor
# Import torch and other required modules
import torch

Function 1 - square()

The square function is used to find the square of elements present in a tensor.

a = torch.tensor([[1., -7], [11, 3]])
a
tensor([[ 1., -7.],
        [11.,  3.]])
b = a.square()
b
tensor([[  1.,  49.],
        [121.,   9.]])