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

Mathematical Functions you didn't know EXIST!

Pytorch is a python library that helps us work with nueral networks. It works by implementing functions in tensors, i.e., n-dimensional arrays, similar to numpy arrays. Pytorch calculates the gradients automatically while performing other calculations. This helps us save a lot of time and makes Pytorch one of the best libraries for Neural Networks.

The following mathematical functions are shown in this jupyter notebook along with their usage, test cases and cases in which they fail.

  • torch.angle
  • torch.clamp
  • torch.exp
  • torch.conj
  • torch.xlogy

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
import numpy as np

Function 1 - torch.angle

It computes the element-wise angle(in radians) of the input tensor

Note - The values in tensor would be considered in the Complex Plane

# Example 1 - working (change this)
arr = np.array([[1+2j,2+3j,3+3j],[4-4j,5+6j,6+8j]], dtype = np.complex)
T = torch.from_numpy(arr)
print(torch.angle(T))
# print(T)
tensor([[ 1.1071, 0.9828, 0.7854], [-0.7854, 0.8761, 0.9273]], dtype=torch.float64)