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

Basic Functions using Tensors

An short introduction about PyTorch and about the chosen functions.

  • pow
  • ceil
  • floor
  • gcd
  • lcm

Before we begin, let's install and import PyTorch

# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
# Import torch and other required modules
import torch

Function 1 - torch.pow

Takes the power of each element in input with exponent and returns a tensor with the result.

exponent can be either a single float number or a Tensor with the same number of elements as input.

Parameters
  • input (Tensor) – the input tensor.
  • exponent (float or tensor) – the exponent value
Keyword Arguments
  • out (Tensor, optional) – the output tensor.
# Example 1 - working 
a = torch.tensor([1, 2, 3 , 4])
torch.pow(a, 2)
tensor([ 1,  4,  9, 16])