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

Five Super Useful Mathematical Operations on Tensors

Different ways to initialize a tensor in PyTorch

PyTorch is a powerful, open-source machine learning framework. This notebook contains 5 super useful mathematical operations on a tensor.

  • torch.exp()
  • torch.abs()
  • torch.ceil()
  • torch.floor()
  • torch.clamp()
# Import torch and other required modules
import torch

Function 1 - torch.exp()

This function will return the exponents of the tensor elements.
torch.exp(input, out=None) → Tensor

# Example 1 - working 
a = torch.ones((4,5))
b = torch.exp(a)
b
tensor([[2.7183, 2.7183, 2.7183, 2.7183, 2.7183],
        [2.7183, 2.7183, 2.7183, 2.7183, 2.7183],
        [2.7183, 2.7183, 2.7183, 2.7183, 2.7183],
        [2.7183, 2.7183, 2.7183, 2.7183, 2.7183]])

Returns a new tensor with the exponential of the elements of the input tensor
yi​=exi