10 most common Maths Operation with Pytorch Tensor

Nooras Fatima Ansari
5 min readMay 28, 2020

What is Pytorch ?

Pytorch
  • PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook’s AI Research lab (FAIR). It is free and open-source software released under the Modified BSD license. Although the Python interface is more polished and the primary focus of development, PyTorch also has a C++ interface.
  • A number of pieces of Deep Learning software are built on top of PyTorch, including Uber’s Pyro, HuggingFace’s Transformers, and Catalyst.
  • It is also one of the preferred deep learning research platforms built to provide maximum flexibility and speed. It is known for providing two of the most high-level features; namely, tensor computations with strong GPU acceleration support and building deep neural networks on a tape-based autograd systems.
  • Pytoch Official site .

What is Tensor ?

N-Dimensional Tensor
  • A PyTorch Tensor is basically the same as a numpy array: it does not know anything about deep learning or computational graphs or gradients, and is just a generic n-dimensional array to be used for arbitrary numeric computation.
  • The biggest difference between a numpy array and a PyTorch Tensor is that a PyTorch Tensor can run on either CPU or GPU.

Let’s see some Maths Operations in Pytorch tensors :

First you need to install torch library. Install it by following commands in jupyter notebook:

!conda install pytorch cpuonly -c pytorch -y

Import torch library :

import torch

Maths Operations are :

1. torch.abs()

torch.abs(input, out=None) → Tensor

This function computes the element-wise absolute value of the given input tensor i.e. it returns all positive value of input.

out = | input |

Parameters

  • input (Tensor) — the input tensor.
  • out (Tensor, optional) — the output tensor.

Example :

In[]  : torch.abs(torch.tensor([-10, -22, 3]))
Out[] : tensor([10, 22, 3])

2. torch.add()

torch.add(input, other, out=None)

This function adds the scalar other to each element of the input and returns a new resulting tensor.

out = input + other

Parameters

  • input (Tensor) — the input tensor.
  • value (Number) — the number to be added to each element of input

Example :

In[]  : a = torch.randn(3)
a

Out[] : tensor([-2.5680, -0.8406, 0.2862])
In[] : torch.add(a, 20)
Out[] : tensor([17.4320, 19.1594, 20.2862])

3. torch.sub()

torch.sub(input,other, out=None) → Tensor

This function Subtract the scalar other to each element of the input and returns a new resulting tensor.

out = input — other

Parameters

  • input (Tensor) — the input tensor.
  • other (Number) — the number to be subtracted to each element of input

Example :

In[]  : a = torch.randn(3)
a

Out[] : tensor([ 1.7225, 0.5430, -1.1199])
In[] : torch.sub(a,4)
Out[] : tensor([-2.2775, -3.4570, -5.1199])

4. torch.div()

torch.div(input, other, out=None) → Tensor

This function divides each element of the input with the scalar other and returns a new resulting tensor.

out = input / other

Parameters

  • input (Tensor) — the input tensor.
  • other (Number) — the number to be divided to each element of input

Example :

In[]  : a = torch.randn(5)
a

Out[] : tensor([ 0.1227, -0.0442, 2.6160, 1.6794, 1.5719])
In[] : torch.div(a, 0.2)
Out[] : tensor([ 0.6136, -0.2208, 13.0801, 8.3970, 7.8596])

5. torch.mul()

torch.mul(input, other, out=None)

This function multiplies each element of the input input with the scalar other and returns a new resulting tensor.

out = input * other

Parameters

  • input (Tensor) — the input tensor.
  • other (Number) — the number to be multiplied to each element of input

Example :

In[]  : a = torch.randn(3)
a

Out[] : tensor([ 0.2845, -1.0132, 0.2563])
In[] : torch.mul(a, 5)
Out[] : tensor([ 1.4227, -5.0661, 1.2814])

6. torch.neg()

torch.neg(input, out=None) → Tensor

This function returns a new tensor with the negative of the elements of input.

out = −1 × input

Parameters

  • input (Tensor) — the input tensor.
  • out (Tensor, optional) — the output tensor.

Example :

In[]  : a = torch.randn(3)
a

Out[] : tensor([ 1.0753, 0.5619, -2.2713])
In[] : torch.neg(a)
Out[] : tensor([-1.0753, -0.5619, 2.2713])

7. torch.pow()

torch.pow(input, exponent, out=None) → Tensor

It 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.

out = input^exponent

Parameters

  • input (Tensor) — the input tensor.
  • exponent (float or tensor) — the exponent value
  • out (Tensor, optional) — the output tensor.

Example :

In[]  : a = torch.arange(1., 6.)
a

Out[] : tensor([1., 2., 3., 4., 5.])
In[] : torch.pow(a, 2)
Out[] : tensor([ 1., 4., 9., 16., 25.])

8. torch.reciprocal()

torch.reciprocal(input, out=None) → Tensor

This function returns a new tensor with the reciprocal of the elements of input.

out = 1 / input

Parameters

  • input (Tensor) — the input tensor.
  • out (Tensor, optional) — the output tensor.

Example :

In[]  : a = torch.randn(5)
a

Out[] : tensor([ 0.2661, -1.2168, 1.6755, 0.5949, -0.2095])
In[] : torch.reciprocal(a)
Out[] : tensor([ 3.7579, -0.8218, 0.5968, 1.6811, -4.7739])

9. torch.remainder()

torch.remainder(input, other, out=None) → Tensor

This function computes the element-wise remainder of division. The divisor and dividend may contain both for integer and floating point numbers. The remainder has the same sign as the divisor.

out = input % other

Parameters

  • input (Tensor) — the dividend
  • other (Tensor or float) — the divisor that may be either a number or a Tensor of the same shape as the dividend
  • out (Tensor, optional) — the output tensor.

Example :

In[]  : torch.remainder(torch.tensor([-30., -12, -10, 11, 22, 3]), 2)
Out[] : tensor([0., 0., 0., 1., 0., 1.])

10. torch.square()

torch.square(input, out=None) → Tensor

This function returns a new tensor with the square of the elements of input.

out = input * input

Parameters

  • input (Tensor) — the input tensor.
  • out (Tensor, optional) — the output tensor.

Example :

In[]  : a = torch.randn(4)
a
Out[] : tensor([ 1.1735, -0.9121, 0.0543, -0.5389])
In[] : torch.square(a)
Out[] : tensor([1.3772, 0.8320, 0.0029, 0.2905])

Conclusion :

Now, we know 10 different Maths operations functions which are predefined in Pytorch tensors. These are just 10functions and there are much much more in the official documentation site of pytorch. I hope that with these functions helps to learn something new today.

Resource :

  1. You can learn more from Pytorch Official site
  2. My personal Jovain notebook

Note : Practice makes a man perfect. So practice these, all function so that you can familiar with it.

--

--