Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tanh(x) example + README #636

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ Example use:
>>> from autograd import grad # The only autograd function you may ever need
>>>
>>> def tanh(x): # Define a function
... return (1.0 - np.exp(-x)) / (1.0 + np.exp(-x))
... return (1.0 - np.exp((-2 * x))) / (1.0 + np.exp(-(2 * x)))
...
>>> grad_tanh = grad(tanh) # Obtain its gradient function
>>> grad_tanh(1.0) # Evaluate the gradient at x = 1.0
np.float64(0.39322386648296376)
np.float64(0.419974341614026)
>>> (tanh(1.0001) - tanh(0.9999)) / 0.0002 # Compare to finite differences
np.float64(0.39322386636453377)
np.float64(0.41997434264973155)
```

We can continue to differentiate as many times as we like, and use numpy's
Expand All @@ -40,14 +40,12 @@ vectorization of scalar-valued functions across many different input values:
```python
>>> from autograd import elementwise_grad as egrad # for functions that vectorize over inputs
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(-7, 7, 200)
>>> x = np.linspace(-7, 7, 700)
>>> plt.plot(x, tanh(x),
... x, egrad(tanh)(x), # first derivative
... x, egrad(egrad(tanh))(x), # second derivative
... x, egrad(egrad(egrad(tanh)))(x), # third derivative
... x, egrad(egrad(egrad(egrad(tanh))))(x), # fourth derivative
... x, egrad(egrad(egrad(egrad(egrad(tanh)))))(x), # fifth derivative
... x, egrad(egrad(egrad(egrad(egrad(egrad(tanh))))))(x)) # sixth derivative
>>> plt.show()
```

Expand Down
Binary file modified examples/tanh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 18 additions & 21 deletions examples/tanh.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import autograd.numpy as np
import matplotlib.pyplot as plt

import autograd.numpy as np
from autograd import elementwise_grad as egrad

"""
Expand All @@ -19,27 +20,23 @@


def tanh(x):
return (1.0 - np.exp(-x)) / (1.0 + np.exp(-x))

return (1.0 - np.exp((-2 * x))) / (1.0 + np.exp(-(2 * x)))

x = np.linspace(-7, 7, 200)
plt.plot(
x,
tanh(x),
x,
egrad(tanh)(x), # first derivative
x,
egrad(egrad(tanh))(x), # second derivative
x,
egrad(egrad(egrad(tanh)))(x), # third derivative
x,
egrad(egrad(egrad(egrad(tanh))))(x), # fourth derivative
x,
egrad(egrad(egrad(egrad(egrad(tanh)))))(x), # fifth derivative
x,
egrad(egrad(egrad(egrad(egrad(egrad(tanh))))))(x),
) # sixth derivative

plt.axis("off")
### Plotting
plt.figure(figsize=(12, 8))
x = np.linspace(-7, 7, 700)
plt.plot(x, tanh(x), label='tanh(x)')
plt.plot(x, egrad(tanh)(x), label="1st derivative")
plt.plot(x, egrad(egrad(tanh))(x), label="2nd derivative")
plt.plot(x, egrad(egrad(egrad(tanh)))(x), label="3rd derivative")
plt.plot(x, egrad(egrad(egrad(egrad(tanh))))(x), label="4th derivative")
plt.xlabel('x')
plt.ylabel('y')
plt.ylim(-5, 5)
plt.yticks(np.arange(-5, 6, 1))
plt.legend()
plt.grid(True)
plt.title("tanh(x) and its derivatives")
plt.savefig("tanh.png")
plt.show()