129 KiB
129 KiB
In [1]:
import numpy as np
n = 100 #100 datapoints
M = 5 #size of each minibatch
m = int(n/M) #number of minibatches
n_epochs = 10 #number of epochs
j = 0
for epoch in range(1,n_epochs+1):
for i in range(m):
k = np.random.randint(m) #Pick the k-th minibatch at random
#Compute the gradient using the data in minibatch Bk
#Compute new suggestion for
j += 1In [2]:
import numpy as np
def step_length(t,t0,t1):
return t0/(t+t1)
n = 100 #100 datapoints
M = 5 #size of each minibatch
m = int(n/M) #number of minibatches
n_epochs = 500 #number of epochs
t0 = 1.0
t1 = 10
gamma_j = t0/t1
j = 0
for epoch in range(1,n_epochs+1):
for i in range(m):
k = np.random.randint(m) #Pick the k-th minibatch at random
#Compute the gradient using the data in minibatch Bk
#Compute new suggestion for beta
t = epoch*m+i
gamma_j = step_length(t,t0,t1)
j += 1
print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j))In [3]:
%matplotlib inline
# Importing various packages
from math import exp, sqrt
from random import random, seed
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDRegressor
m = 100
x = 2*np.random.rand(m,1)
y = 4+3*x+np.random.randn(m,1)
X = np.c_[np.ones((m,1)), x]
theta_linreg = np.linalg.inv(X.T @ X) @ (X.T @ y)
print("Own inversion")
print(theta_linreg)
sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1)
sgdreg.fit(x,y.ravel())
print("sgdreg from scikit")
print(sgdreg.intercept_, sgdreg.coef_)
theta = np.random.randn(2,1)
eta = 0.1
Niterations = 1000
for iter in range(Niterations):
gradients = 2.0/m*X.T @ ((X @ theta)-y)
theta -= eta*gradients
print("theta from own gd")
print(theta)
xnew = np.array([[0],[2]])
Xnew = np.c_[np.ones((2,1)), xnew]
ypredict = Xnew.dot(theta)
ypredict2 = Xnew.dot(theta_linreg)
n_epochs = 50
t0, t1 = 5, 50
def learning_schedule(t):
return t0/(t+t1)
theta = np.random.randn(2,1)
for epoch in range(n_epochs):
for i in range(m):
random_index = np.random.randint(m)
xi = X[random_index:random_index+1]
yi = y[random_index:random_index+1]
gradients = 2 * xi.T @ ((xi @ theta)-yi)
eta = learning_schedule(epoch*m+i)
theta = theta - eta*gradients
print("theta from own sdg")
print(theta)
plt.plot(xnew, ypredict, "r-")
plt.plot(xnew, ypredict2, "b-")
plt.plot(x, y ,'ro')
plt.axis([0,2.0,0, 15.0])
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.title(r'Random numbers ')
plt.show()In [1]:
import autograd.numpy as np
# To do elementwise differentiation:
from autograd import elementwise_grad as egrad
# To plot:
import matplotlib.pyplot as plt
def f(x):
return np.sin(2*np.pi*x + x**2)
def f_grad_analytic(x):
return np.cos(2*np.pi*x + x**2)*(2*np.pi + 2*x)
# Do the comparison:
x = np.linspace(0,1,1000)
f_grad = egrad(f)
computed = f_grad(x)
analytic = f_grad_analytic(x)
plt.title('Derivative computed from Autograd compared with the analytical derivative')
plt.plot(x,computed,label='autograd')
plt.plot(x,analytic,label='analytic')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic))))The max absolute difference is: 1.77636e-15
In [2]:
import autograd.numpy as np
from autograd import grad
def f1(x):
return x**3 + 1
f1_grad = grad(f1)
# Remember to send in float as argument to the computed gradient from Autograd!
a = 1.0
# See the evaluated gradient at a using autograd:
print("The gradient of f1 evaluated at a = %g using autograd is: %g"%(a,f1_grad(a)))
# Compare with the analytical derivative, that is f1'(x) = 3*x**2
grad_analytical = 3*a**2
print("The gradient of f1 evaluated at a = %g by finding the analytic expression is: %g"%(a,grad_analytical))The gradient of f1 evaluated at a = 1 using autograd is: 3 The gradient of f1 evaluated at a = 1 by finding the analytic expression is: 3
In [3]:
import autograd.numpy as np
from autograd import grad
def f2(x1,x2):
return 3*x1**3 + x2*(x1 - 5) + 1
# By sending the argument 0, Autograd will compute the derivative w.r.t the first variable, in this case x1
f2_grad_x1 = grad(f2,0)
# ... and differentiate w.r.t x2 by sending 1 as an additional arugment to grad
f2_grad_x2 = grad(f2,1)
x1 = 1.0
x2 = 3.0
print("Evaluating at x1 = %g, x2 = %g"%(x1,x2))
print("-"*30)
# Compare with the analytical derivatives:
# Derivative of f2 w.r.t x1 is: 9*x1**2 + x2:
f2_grad_x1_analytical = 9*x1**2 + x2
# Derivative of f2 w.r.t x2 is: x1 - 5:
f2_grad_x2_analytical = x1 - 5
# See the evaluated derivations:
print("The derivative of f2 w.r.t x1: %g"%( f2_grad_x1(x1,x2) ))
print("The analytical derivative of f2 w.r.t x1: %g"%( f2_grad_x1(x1,x2) ))
print()
print("The derivative of f2 w.r.t x2: %g"%( f2_grad_x2(x1,x2) ))
print("The analytical derivative of f2 w.r.t x2: %g"%( f2_grad_x2(x1,x2) ))Evaluating at x1 = 1, x2 = 3 ------------------------------ The derivative of f2 w.r.t x1: 12 The analytical derivative of f2 w.r.t x1: 12 The derivative of f2 w.r.t x2: -4 The analytical derivative of f2 w.r.t x2: -4
In [4]:
import autograd.numpy as np
from autograd import grad
def f3(x): # Assumes x is an array of length 5 or higher
return 2*x[0] + 3*x[1] + 5*x[2] + 7*x[3] + 11*x[4]**2
f3_grad = grad(f3)
x = np.linspace(0,4,5)
# Print the computed gradient:
print("The computed gradient of f3 is: ", f3_grad(x))
# The analytical gradient is: (2, 3, 5, 7, 22*x[4])
f3_grad_analytical = np.array([2, 3, 5, 7, 22*x[4]])
# Print the analytical gradient:
print("The analytical gradient of f3 is: ", f3_grad_analytical)The computed gradient of f3 is: [ 2. 3. 5. 7. 88.] The analytical gradient of f3 is: [ 2. 3. 5. 7. 88.]
In [5]:
import autograd.numpy as np
from autograd import grad
def f4(x):
return np.sqrt(1+x**2) + np.exp(x) + np.sin(2*np.pi*x)
f4_grad = grad(f4)
x = 2.7
# Print the computed derivative:
print("The computed derivative of f4 at x = %g is: %g"%(x,f4_grad(x)))
# The analytical derivative is: x/sqrt(1 + x**2) + exp(x) + cos(2*pi*x)*2*pi
f4_grad_analytical = x/np.sqrt(1 + x**2) + np.exp(x) + np.cos(2*np.pi*x)*2*np.pi
# Print the analytical gradient:
print("The analytical gradient of f4 at x = %g is: %g"%(x,f4_grad_analytical))The computed derivative of f4 at x = 2.7 is: 13.8759 The analytical gradient of f4 at x = 2.7 is: 13.8759
In [6]:
import autograd.numpy as np
from autograd import grad
def f5(x):
if x >= 0:
return x**2
else:
return -3*x + 1
f5_grad = grad(f5)
x = 2.7
# Print the computed derivative:
print("The computed derivative of f5 at x = %g is: %g"%(x,f5_grad(x)))The computed derivative of f5 at x = 2.7 is: 5.4
In [7]:
import autograd.numpy as np
from autograd import grad
# Both of the functions are implementation of the sum: sum(x**i) for i = 0, ..., 9
# The analytical derivative is: sum(i*x**(i-1))
f6_grad_analytical = 0
for i in range(10):
f6_grad_analytical += i*x**(i-1)
print("The analytical derivative of f6 at x = %g is: %g"%(x,f6_grad_analytical))The analytical derivative of f6 at x = 2.7 is: 37732.5
In [8]:
import autograd.numpy as np
from autograd import grad
def f7(n): # Assume that n is an integer
if n == 1 or n == 0:
return 1
else:
return n*f7(n-1)
f7_grad = grad(f7)
n = 2.0
print("The computed derivative of f7 at n = %d is: %g"%(n,f7_grad(n)))
# The function f7 is an implementation of the factorial of n.
# By using the product rule, one can find that the derivative is:
f7_grad_analytical = 0
for i in range(int(n)-1):
tmp = 1
for k in range(int(n)-1):
if k != i:
tmp *= (n - k)
f7_grad_analytical += tmp
print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical))The computed derivative of f7 at n = 2 is: 1 The analytical derivative of f7 at n = 2 is: 1
In [9]:
import autograd.numpy as np
from autograd import grad
def f8(x): # Assume x is an array
x[2] = 3
return x*2
f8_grad = grad(f8)
x = 8.4
print("The derivative of f8 is:",f8_grad(x))[0;31m---------------------------------------------------------------------------[0m
[0;31mTypeError[0m Traceback (most recent call last)
[0;32m<ipython-input-9-cabc613b8702>[0m in [0;36m<module>[0;34m[0m
[1;32m 9[0m [0mx[0m [0;34m=[0m [0;36m8.4[0m[0;34m[0m[0;34m[0m[0m
[1;32m 10[0m [0;34m[0m[0m
[0;32m---> 11[0;31m [0mprint[0m[0;34m([0m[0;34m"The derivative of f8 is:"[0m[0;34m,[0m[0mf8_grad[0m[0;34m([0m[0mx[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/wrap_util.py[0m in [0;36mnary_f[0;34m(*args, **kwargs)[0m
[1;32m 18[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 19[0m [0mx[0m [0;34m=[0m [0mtuple[0m[0;34m([0m[0margs[0m[0;34m[[0m[0mi[0m[0;34m][0m [0;32mfor[0m [0mi[0m [0;32min[0m [0margnum[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m---> 20[0;31m [0;32mreturn[0m [0munary_operator[0m[0;34m([0m[0munary_f[0m[0;34m,[0m [0mx[0m[0;34m,[0m [0;34m*[0m[0mnary_op_args[0m[0;34m,[0m [0;34m**[0m[0mnary_op_kwargs[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 21[0m [0;32mreturn[0m [0mnary_f[0m[0;34m[0m[0;34m[0m[0m
[1;32m 22[0m [0;32mreturn[0m [0mnary_operator[0m[0;34m[0m[0;34m[0m[0m
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/differential_operators.py[0m in [0;36mgrad[0;34m(fun, x)[0m
[1;32m 23[0m [0marguments[0m [0;32mas[0m[0;31m [0m[0;31m`[0m[0mfun[0m[0;31m`[0m[0;34m,[0m [0mbut[0m [0mreturns[0m [0mthe[0m [0mgradient[0m [0minstead[0m[0;34m.[0m [0mThe[0m [0mfunction[0m[0;31m [0m[0;31m`[0m[0mfun[0m[0;31m`[0m[0;34m[0m[0;34m[0m[0m
[1;32m 24[0m should be scalar-valued. The gradient has the same type as the argument."""
[0;32m---> 25[0;31m [0mvjp[0m[0;34m,[0m [0mans[0m [0;34m=[0m [0m_make_vjp[0m[0;34m([0m[0mfun[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 26[0m [0;32mif[0m [0;32mnot[0m [0mvspace[0m[0;34m([0m[0mans[0m[0;34m)[0m[0;34m.[0m[0msize[0m [0;34m==[0m [0;36m1[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 27[0m raise TypeError("Grad only applies to real scalar-output functions. "
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/core.py[0m in [0;36mmake_vjp[0;34m(fun, x)[0m
[1;32m 8[0m [0;32mdef[0m [0mmake_vjp[0m[0;34m([0m[0mfun[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 9[0m [0mstart_node[0m [0;34m=[0m [0mVJPNode[0m[0;34m.[0m[0mnew_root[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m---> 10[0;31m [0mend_value[0m[0;34m,[0m [0mend_node[0m [0;34m=[0m [0mtrace[0m[0;34m([0m[0mstart_node[0m[0;34m,[0m [0mfun[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 11[0m [0;32mif[0m [0mend_node[0m [0;32mis[0m [0;32mNone[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 12[0m [0;32mdef[0m [0mvjp[0m[0;34m([0m[0mg[0m[0;34m)[0m[0;34m:[0m [0;32mreturn[0m [0mvspace[0m[0;34m([0m[0mx[0m[0;34m)[0m[0;34m.[0m[0mzeros[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/tracer.py[0m in [0;36mtrace[0;34m(start_node, fun, x)[0m
[1;32m 8[0m [0;32mwith[0m [0mtrace_stack[0m[0;34m.[0m[0mnew_trace[0m[0;34m([0m[0;34m)[0m [0;32mas[0m [0mt[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 9[0m [0mstart_box[0m [0;34m=[0m [0mnew_box[0m[0;34m([0m[0mx[0m[0;34m,[0m [0mt[0m[0;34m,[0m [0mstart_node[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m---> 10[0;31m [0mend_box[0m [0;34m=[0m [0mfun[0m[0;34m([0m[0mstart_box[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 11[0m [0;32mif[0m [0misbox[0m[0;34m([0m[0mend_box[0m[0;34m)[0m [0;32mand[0m [0mend_box[0m[0;34m.[0m[0m_trace[0m [0;34m==[0m [0mstart_box[0m[0;34m.[0m[0m_trace[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 12[0m [0;32mreturn[0m [0mend_box[0m[0;34m.[0m[0m_value[0m[0;34m,[0m [0mend_box[0m[0;34m.[0m[0m_node[0m[0;34m[0m[0;34m[0m[0m
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/wrap_util.py[0m in [0;36munary_f[0;34m(x)[0m
[1;32m 13[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 14[0m [0msubargs[0m [0;34m=[0m [0msubvals[0m[0;34m([0m[0margs[0m[0;34m,[0m [0mzip[0m[0;34m([0m[0margnum[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m---> 15[0;31m [0;32mreturn[0m [0mfun[0m[0;34m([0m[0;34m*[0m[0msubargs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 16[0m [0;32mif[0m [0misinstance[0m[0;34m([0m[0margnum[0m[0;34m,[0m [0mint[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 17[0m [0mx[0m [0;34m=[0m [0margs[0m[0;34m[[0m[0margnum[0m[0;34m][0m[0;34m[0m[0;34m[0m[0m
[0;32m<ipython-input-9-cabc613b8702>[0m in [0;36mf8[0;34m(x)[0m
[1;32m 2[0m [0;32mfrom[0m [0mautograd[0m [0;32mimport[0m [0mgrad[0m[0;34m[0m[0;34m[0m[0m
[1;32m 3[0m [0;32mdef[0m [0mf8[0m[0;34m([0m[0mx[0m[0;34m)[0m[0;34m:[0m [0;31m# Assume x is an array[0m[0;34m[0m[0;34m[0m[0m
[0;32m----> 4[0;31m [0mx[0m[0;34m[[0m[0;36m2[0m[0;34m][0m [0;34m=[0m [0;36m3[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 5[0m [0;32mreturn[0m [0mx[0m[0;34m*[0m[0;36m2[0m[0;34m[0m[0;34m[0m[0m
[1;32m 6[0m [0;34m[0m[0m
[0;31mTypeError[0m: 'ArrayBox' object does not support item assignmentIn [10]:
import autograd.numpy as np
from autograd import grad
def f9(a): # Assume a is an array with 2 elements
b = np.array([1.0,2.0])
return a.dot(b)
f9_grad = grad(f9)
x = np.array([1.0,0.0])
print("The derivative of f9 is:",f9_grad(x))[0;31m---------------------------------------------------------------------------[0m
[0;31mAttributeError[0m Traceback (most recent call last)
[0;32m<ipython-input-10-1ec254875e1a>[0m in [0;36m<module>[0;34m[0m
[1;32m 9[0m [0mx[0m [0;34m=[0m [0mnp[0m[0;34m.[0m[0marray[0m[0;34m([0m[0;34m[[0m[0;36m1.0[0m[0;34m,[0m[0;36m0.0[0m[0;34m][0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[1;32m 10[0m [0;34m[0m[0m
[0;32m---> 11[0;31m [0mprint[0m[0;34m([0m[0;34m"The derivative of f9 is:"[0m[0;34m,[0m[0mf9_grad[0m[0;34m([0m[0mx[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/wrap_util.py[0m in [0;36mnary_f[0;34m(*args, **kwargs)[0m
[1;32m 18[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 19[0m [0mx[0m [0;34m=[0m [0mtuple[0m[0;34m([0m[0margs[0m[0;34m[[0m[0mi[0m[0;34m][0m [0;32mfor[0m [0mi[0m [0;32min[0m [0margnum[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m---> 20[0;31m [0;32mreturn[0m [0munary_operator[0m[0;34m([0m[0munary_f[0m[0;34m,[0m [0mx[0m[0;34m,[0m [0;34m*[0m[0mnary_op_args[0m[0;34m,[0m [0;34m**[0m[0mnary_op_kwargs[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 21[0m [0;32mreturn[0m [0mnary_f[0m[0;34m[0m[0;34m[0m[0m
[1;32m 22[0m [0;32mreturn[0m [0mnary_operator[0m[0;34m[0m[0;34m[0m[0m
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/differential_operators.py[0m in [0;36mgrad[0;34m(fun, x)[0m
[1;32m 23[0m [0marguments[0m [0;32mas[0m[0;31m [0m[0;31m`[0m[0mfun[0m[0;31m`[0m[0;34m,[0m [0mbut[0m [0mreturns[0m [0mthe[0m [0mgradient[0m [0minstead[0m[0;34m.[0m [0mThe[0m [0mfunction[0m[0;31m [0m[0;31m`[0m[0mfun[0m[0;31m`[0m[0;34m[0m[0;34m[0m[0m
[1;32m 24[0m should be scalar-valued. The gradient has the same type as the argument."""
[0;32m---> 25[0;31m [0mvjp[0m[0;34m,[0m [0mans[0m [0;34m=[0m [0m_make_vjp[0m[0;34m([0m[0mfun[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 26[0m [0;32mif[0m [0;32mnot[0m [0mvspace[0m[0;34m([0m[0mans[0m[0;34m)[0m[0;34m.[0m[0msize[0m [0;34m==[0m [0;36m1[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 27[0m raise TypeError("Grad only applies to real scalar-output functions. "
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/core.py[0m in [0;36mmake_vjp[0;34m(fun, x)[0m
[1;32m 8[0m [0;32mdef[0m [0mmake_vjp[0m[0;34m([0m[0mfun[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 9[0m [0mstart_node[0m [0;34m=[0m [0mVJPNode[0m[0;34m.[0m[0mnew_root[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m---> 10[0;31m [0mend_value[0m[0;34m,[0m [0mend_node[0m [0;34m=[0m [0mtrace[0m[0;34m([0m[0mstart_node[0m[0;34m,[0m [0mfun[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 11[0m [0;32mif[0m [0mend_node[0m [0;32mis[0m [0;32mNone[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 12[0m [0;32mdef[0m [0mvjp[0m[0;34m([0m[0mg[0m[0;34m)[0m[0;34m:[0m [0;32mreturn[0m [0mvspace[0m[0;34m([0m[0mx[0m[0;34m)[0m[0;34m.[0m[0mzeros[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/tracer.py[0m in [0;36mtrace[0;34m(start_node, fun, x)[0m
[1;32m 8[0m [0;32mwith[0m [0mtrace_stack[0m[0;34m.[0m[0mnew_trace[0m[0;34m([0m[0;34m)[0m [0;32mas[0m [0mt[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 9[0m [0mstart_box[0m [0;34m=[0m [0mnew_box[0m[0;34m([0m[0mx[0m[0;34m,[0m [0mt[0m[0;34m,[0m [0mstart_node[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m---> 10[0;31m [0mend_box[0m [0;34m=[0m [0mfun[0m[0;34m([0m[0mstart_box[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 11[0m [0;32mif[0m [0misbox[0m[0;34m([0m[0mend_box[0m[0;34m)[0m [0;32mand[0m [0mend_box[0m[0;34m.[0m[0m_trace[0m [0;34m==[0m [0mstart_box[0m[0;34m.[0m[0m_trace[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 12[0m [0;32mreturn[0m [0mend_box[0m[0;34m.[0m[0m_value[0m[0;34m,[0m [0mend_box[0m[0;34m.[0m[0m_node[0m[0;34m[0m[0;34m[0m[0m
[0;32m~/anaconda3/lib/python3.6/site-packages/autograd/wrap_util.py[0m in [0;36munary_f[0;34m(x)[0m
[1;32m 13[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 14[0m [0msubargs[0m [0;34m=[0m [0msubvals[0m[0;34m([0m[0margs[0m[0;34m,[0m [0mzip[0m[0;34m([0m[0margnum[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m---> 15[0;31m [0;32mreturn[0m [0mfun[0m[0;34m([0m[0;34m*[0m[0msubargs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 16[0m [0;32mif[0m [0misinstance[0m[0;34m([0m[0margnum[0m[0;34m,[0m [0mint[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m
[1;32m 17[0m [0mx[0m [0;34m=[0m [0margs[0m[0;34m[[0m[0margnum[0m[0;34m][0m[0;34m[0m[0;34m[0m[0m
[0;32m<ipython-input-10-1ec254875e1a>[0m in [0;36mf9[0;34m(a)[0m
[1;32m 3[0m [0;32mdef[0m [0mf9[0m[0;34m([0m[0ma[0m[0;34m)[0m[0;34m:[0m [0;31m# Assume a is an array with 2 elements[0m[0;34m[0m[0;34m[0m[0m
[1;32m 4[0m [0mb[0m [0;34m=[0m [0mnp[0m[0;34m.[0m[0marray[0m[0;34m([0m[0;34m[[0m[0;36m1.0[0m[0;34m,[0m[0;36m2.0[0m[0;34m][0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;32m----> 5[0;31m [0;32mreturn[0m [0ma[0m[0;34m.[0m[0mdot[0m[0;34m([0m[0mb[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m[1;32m 6[0m [0;34m[0m[0m
[1;32m 7[0m [0mf9_grad[0m [0;34m=[0m [0mgrad[0m[0;34m([0m[0mf9[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;31mAttributeError[0m: 'ArrayBox' object has no attribute 'dot'In [11]:
import autograd.numpy as np
from autograd import grad
def f9_alternative(x): # Assume a is an array with 2 elements
b = np.array([1.0,2.0])
return np.dot(x,b) # The same as x_1*b_1 + x_2*b_2
f9_alternative_grad = grad(f9_alternative)
x = np.array([3.0,0.0])
print("The gradient of f9 is:",f9_alternative_grad(x))
# The analytical gradient of the dot product of vectors x and b with two elements (x_1,x_2) and (b_1, b_2) respectively
# w.r.t x is (b_1, b_2).The gradient of f9 is: [1. 2.]
In [15]:
a += b
a -= b
a*= b
a /=bWarning:
Output truncated. This notebook contains too many cells to display efficiently.