A beginner’s guide to 5 PyTorch Mathematical functions you didn’t know you needed

PyTorch is a magical deep learning framework. It is a magic tool for Zero to GAN’s Human.

Md. Harun-Ur-Rashid
Predict
4 min readMay 29, 2020

--

Introduction

Formal Intro is “PyTorch is an open-source deep learning framework for python, primarily developed by Facebook’s AI research lab. In simple terms, PyTorch is a library for processing tensors.”

I think that PyTorch is a magical deep learning framework. It has magical power which can help to accelerate the research that goes into deep learning models by making them computationally faster and less expensive (A Data Scientist’s magic tool!).

I found PyTorch framework really useful for my work. It’s worked like magic in my work. I suggested everyone it’s a very flexible framework in Deep learning and Computer Vision.

Functions

Here are the list and explanation of the most commonly used function in PyTorch

  • torch.abs
  • torch.acos
  • torch.add
  • torch.addcdiv
  • torch.addcmul

Function 1 — torch.abs(input, out=None)

Computes the element-wise absolute value of the given input tensor.

Parameters:

  • input (Tensor) — the input tensor.
  • out (Tensor, optional) — the output tensor.
Example of abs Function

We saw that we gave negative input tensor and the `torch.abs` function returned positive tensor as an output. I think that this function will be very helpful for our deep learning model as like magic.

Function 2 — torch.acos(input, out=None)

It returns a new tensor with the arccosine of the elements of the input.

Parameters:

  • input (Tensor) — the input tensor.
  • out (Tensor, optional) — the output tensor.
Example of acos Function

We got random cosine tensor output for using `torch.acos` function.

Example of acos Function

I think that this function also will be very helpful for our deep learning model as like magic.

Function 3 — torch.add(input, other, out=None)

Adds the scalar `other` to each element of the input `input` and returns a new resulting tensor.

If `input` is of type FloatTensor or DoubleTensor, `other` must be a real number, otherwise, it should be an integer.

Parameters:

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

Keyword Arguments:

  • out (Tensor, optional) — the output tensor.
Example of add function

Here we can easily addition by using `torch.add` function.

Example of add function

This function will be very helpful in a deep learning model for easily addition.

Function 4 — torch.addcdiv(input, tensor1, tensor2, *, value=1, out=None)

Performs the element-wise division of `tensor1` by `tensor2`, multiply the result by the scalar `value` and add it to `input`.

The shapes of `input`, `tensor1`, and `tensor2` must be broadcastable.

For inputs of type FloatTensor or DoubleTensor, `value` must be a real number, otherwise an integer.

Parameters:

  • input (Tensor) — the tensor to be added
  • tensor1 (Tensor) — the numerator tensor
  • tensor2 (Tensor) — the denominator tensor
  • value (Number, optional) — multiplier for tensor1/tensor2
  • out (Tensor, optional) — the output tensor.
Example of addcdiv Function
Example of addcdiv Function

Function 5 — torch.addcmul(input, tensor1, tensor2, *, value=1, out=None)

Performs the element-wise multiplication of `tensor1` by `tensor2`, multiply the result by the scalar `value ` and add it to `input`.

The shapes of `tensor`, `tensor1`, and `tensor2` must be broadcastable.

For inputs of type FloatTensor or DoubleTensor, `value` must be a real number, otherwise an integer.

Parameters:

  • input (Tensor) — the tensor to be added
  • tensor1 (Tensor) — the tensor to be multiplied
  • tensor2 (Tensor) — the tensor to be multiplied
  • value (Number, optional) — multiplier for tensor1 .* tensor2
  • out (Tensor, optional) — the output tensor.
Example of addcmul Function
Example of addcmul Function

Conclusion

PyTroch is a very useful deep learning framework. It works very faster as like magical tool. It also a magical tool for Data Scientists. So, I recommended every beginner’s to learn this framework. In this blog, I was tried to present 5 magical mathematical functions.

Reference Links

With that, we have come to the end of the article. You read this far, You Go! I am excited to know how it worked for you. The entire notebook is available here. And, Don’t forget to give your 👏 ! More cool articles lined up. Coming soon!

You can connect with me on LinkedIn or Twitter.

Twitter
Twitter
LinkedIn
LinkedIn

--

--

Md. Harun-Ur-Rashid
Predict

Software Engineer | | Writer @TDataScience | Notebook Master & Discussion Expert @Kaggle. https://www.linkedin.com/in/harunurrashid97/