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. #### Step 1 : Install and Import autograd in your python file or jupyter notebook

``````import autograd.numpy as np
``````

#### Step 2: Define function

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

``````grad_tanh = grad(tanh)
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()
`````` #### Step 5: Plot gradient over interval:

``````plt.plot(x, tanh(x), x, egrad(tanh)(x))
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)
``````

#### which Prints Results :

``````tensor(0.4200)
tensor(0.4202)
``````

#### Plot function and gradient over interval:

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