Can you list down important algebra operations commonly needed for various machine learning algorithms specially neural networks ? Most common ones are simple matrix/tensor addition, subtraction, multiplication, division , Apart from that you also need exponential , tanh etc.

But one of the most important of these operations are derivatives, You may have heard of the term gradient if you have read about neural network training, For training any neural networks, There are two passes, First one is forward pass , where you perform a series of linear and non linear operations on input matrix to come up with output. And then for actual training of these neural networks you need to do backward pass, Where you need to calculate gradient of forward pass operations so that you can update your weights based on these gradients, For calculating these gradients you need the ability to do efficient differentiation of arbitrary function, Historically these differentiation operations are done manually by calculating equations of gradient of functions, but this manual calculation of differentiation is often error prone, with risk of bugs in your code and numerical accuracy , It also costs lots of manual effort and productivity loss. To solve this problem now most of the library support automatic gradient calculation, with newer libraries e.g. Autograd, Jax, PyTorch, TensorFlow 2.0 even giving very good easy to understand native python support of auto-gradients in their Ecosystem. Take a look at these code snippets to understand gradients better using Autograd and Pytorch.

#### Step 2: Define function

# Define a function
def tanh(x):
y = np.exp(-2.0 * x)
return (1.0 - y) / (1.0 + y)

print((tanh(1.0001) - tanh(0.9999)) / 0.0002)

#### Output:

0.419974341614026
0.41997434264973155

#### Step 4: Plot function over an interval:

import matplotlib.pyplot as plt
x = np.linspace(-7, 7, 200)
plt.plot(x, tanh(x))
plt.show()

plt.show()

### Equivalent code in PyTorch:

import torch
import numpy as np
import matplotlib.pyplot as plt
# Define a function
def tanh(x):
y = torch.exp(-2.0 * x)
return (1.0 - y) / (1.0 + y)

x = torch.tensor(1.0, requires_grad = True)
z = tanh(x)
print(x.grad.data) #Prints '3' which is dz/dx
print((tanh(torch.tensor(1.0001)) - tanh(torch.tensor(0.9999))) / 0.0002)

tensor(0.4200)
tensor(0.4202)

#### Plot function and gradient over interval:

x = np.linspace(-7, 7, 200)
x = torch.from_numpy(x)