autograd





import matplotlib.pyplot as plt
import autograd.numpy as np
from autograd import grad
def fun(x):
    curr = x
    ans = curr
    for i in xrange(1000):
            curr = - curr*x**2 / ((2*i+3)*(2*i+2))
            ans = ans + curr
            if np.abs(curr) < 0.2: break
    return ans

d_fun = grad(fun)
dd_fun = grad(d_fun)
x = np.linspace(-10, 10, 100)
plt.plot(x, map(fun, x),x, map(d_fun, x),x, map(dd_fun, x))
plt.show()





import matplotlib.pyplot as plt
import autograd.numpy as np
from autograd import grad
def tanh(x):
    return (1 - np.exp(-x)) / (1 + np.exp(-x))

d_fun = grad(tanh) # 1st derivative
dd_fun = grad(d_fun) # 2nd derivative
ddd_fun = grad(dd_fun) # 3rd derivative
dddd_fun = grad(ddd_fun) # 4th derivative
ddddd_fun = grad(dddd_fun) # 5th derivative
dddddd_fun = grad(ddddd_fun) # 6th derivative
x = np.linspace(-7, 7, 200)
plt.plot(x, map(tanh, x),
    x, map(d_fun, x),
    x, map(dd_fun, x),
    x, map(ddd_fun, x),
    x, map(dddd_fun, x),
    x, map(ddddd_fun, x),
    x, map(dddddd_fun, x))

plt.show()















Comments