From 96947556948310b4e977e4ebbc3c271041075c97 Mon Sep 17 00:00:00 2001 From: mhjensen Date: Thu, 24 Sep 2020 06:36:42 +0200 Subject: [PATCH] typo in week39 --- doc/pub/week39/html/._week39-bs000.html | 2 +- doc/pub/week39/html/._week39-bs025.html | 4 +- doc/pub/week39/html/._week39-bs026.html | 6 +- doc/pub/week39/html/._week39-bs028.html | 14 +-- doc/pub/week39/html/._week39-bs037.html | 2 +- doc/pub/week39/html/._week39-bs038.html | 22 ++-- doc/pub/week39/html/._week39-bs046.html | 2 +- doc/pub/week39/html/._week39-bs047.html | 4 +- doc/pub/week39/html/._week39-bs048.html | 14 +-- doc/pub/week39/html/._week39-bs049.html | 4 +- doc/pub/week39/html/._week39-bs050.html | 4 +- doc/pub/week39/html/._week39-bs051.html | 2 +- doc/pub/week39/html/._week39-bs052.html | 6 +- doc/pub/week39/html/._week39-bs053.html | 4 +- doc/pub/week39/html/._week39-bs054.html | 2 +- doc/pub/week39/html/._week39-bs055.html | 4 +- doc/pub/week39/html/._week39-bs065.html | 4 +- doc/pub/week39/html/week39-bs.html | 2 +- doc/pub/week39/html/week39-reveal.html | 100 +++++++++---------- doc/pub/week39/html/week39-solarized.html | 100 +++++++++---------- doc/pub/week39/html/week39.html | 100 +++++++++---------- doc/pub/week39/ipynb/ipynb-week39-src.tar.gz | Bin 197 -> 191 bytes doc/pub/week39/ipynb/week39.ipynb | 6 +- doc/src/week39/week39.do.txt | 2 +- 24 files changed, 205 insertions(+), 205 deletions(-) diff --git a/doc/pub/week39/html/._week39-bs000.html b/doc/pub/week39/html/._week39-bs000.html index ee689d11b..d1107c7b3 100644 --- a/doc/pub/week39/html/._week39-bs000.html +++ b/doc/pub/week39/html/._week39-bs000.html @@ -301,7 +301,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 22, 2020

+

Sep 24, 2020


diff --git a/doc/pub/week39/html/._week39-bs025.html b/doc/pub/week39/html/._week39-bs025.html index 2f5e565c5..8ae4a5c59 100644 --- a/doc/pub/week39/html/._week39-bs025.html +++ b/doc/pub/week39/html/._week39-bs025.html @@ -305,7 +305,7 @@ y = 4+3* xb = np.c_[np.ones((m,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print(beta_linreg) +print(beta_linreg) beta = np.random.randn(2,1) eta = 0.1 @@ -315,7 +315,7 @@ Niterations = 1 gradients = 2.0/m*xb.T.dot(xb.dot(beta)-y) beta -= eta*gradients -print(beta) +print(beta) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] ypredict = xbnew.dot(beta) diff --git a/doc/pub/week39/html/._week39-bs026.html b/doc/pub/week39/html/._week39-bs026.html index 47e982f89..73af92e68 100644 --- a/doc/pub/week39/html/._week39-bs026.html +++ b/doc/pub/week39/html/._week39-bs026.html @@ -298,10 +298,10 @@ y = 4+3* xb = np.c_[np.ones((100,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print(beta_linreg) -sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1) +print(beta_linreg) +sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) sgdreg.fit(x,y.ravel()) -print(sgdreg.intercept_, sgdreg.coef_) +print(sgdreg.intercept_, sgdreg.coef_)

diff --git a/doc/pub/week39/html/._week39-bs028.html b/doc/pub/week39/html/._week39-bs028.html index b9f46b937..9163594df 100644 --- a/doc/pub/week39/html/._week39-bs028.html +++ b/doc/pub/week39/html/._week39-bs028.html @@ -300,14 +300,14 @@ x = 2*np y = 4+3*x+np.random.randn(m,1) xb = np.c_[np.ones((m,1)), x] -XT_X = xb.T @ xb +XT_X = xb.T @ xb #Ridge parameter lambda lmbda = 0.001 Id = lmbda* np.eye(XT_X.shape[0]) -beta_linreg = np.linalg.inv(XT_X+Id) @ xb.T @ y -print(beta_linreg) +beta_linreg = np.linalg.inv(XT_X+Id) @ xb.T @ y +print(beta_linreg) # Start plain gradient descent beta = np.random.randn(2,1) @@ -315,12 +315,12 @@ eta = 0.1= 100 for iter in range(Niterations): - gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta + gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta beta -= eta*gradients -print(beta) -ypredict = xb @ beta -ypredict2 = xb @ beta_linreg +print(beta) +ypredict = xb @ beta +ypredict2 = xb @ beta_linreg plt.plot(x, ypredict, "r-") plt.plot(x, ypredict2, "b-") plt.plot(x, y ,'ro') diff --git a/doc/pub/week39/html/._week39-bs037.html b/doc/pub/week39/html/._week39-bs037.html index 032687147..b0a146d89 100644 --- a/doc/pub/week39/html/._week39-bs037.html +++ b/doc/pub/week39/html/._week39-bs037.html @@ -325,7 +325,7 @@ j = 0 gamma_j = step_length(t,t0,t1) j += 1 -print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j)) +print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j))

diff --git a/doc/pub/week39/html/._week39-bs038.html b/doc/pub/week39/html/._week39-bs038.html index 3b004e4a2..1c4d57d52 100644 --- a/doc/pub/week39/html/._week39-bs038.html +++ b/doc/pub/week39/html/._week39-bs038.html @@ -300,12 +300,12 @@ y = 4+3* xb = np.c_[np.ones((m,1)), x] theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print("Own inversion") -print(theta_linreg) -sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) +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_) +print("sgdreg from scikit") +print(sgdreg.intercept_, sgdreg.coef_) theta = np.random.randn(2,1) @@ -314,10 +314,10 @@ Niterations = 1 for iter in range(Niterations): - gradients = 2.0/m*xb.T @ ((xb @ theta)-y) + gradients = 2.0/m*xb.T @ ((xb @ theta)-y) theta -= eta*gradients -print("theta frm own gd") -print(theta) +print("theta frm own gd") +print(theta) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] @@ -337,11 +337,11 @@ theta = np.= np.random.randint(m) xi = xb[random_index:random_index+1] yi = y[random_index:random_index+1] - gradients = 2 * xi.T @ ((xi @ theta)-yi) + gradients = 2 * xi.T @ ((xi @ theta)-yi) eta = learning_schedule(epoch*m+i) theta = theta - eta*gradients -print("theta from own sdg") -print(theta) +print("theta from own sdg") +print(theta) plt.plot(xnew, ypredict, "r-") plt.plot(xnew, ypredict2, "b-") diff --git a/doc/pub/week39/html/._week39-bs046.html b/doc/pub/week39/html/._week39-bs046.html index 39104ee0b..5bda4b981 100644 --- a/doc/pub/week39/html/._week39-bs046.html +++ b/doc/pub/week39/html/._week39-bs046.html @@ -362,7 +362,7 @@ plt.legend() plt.show() -print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic)))) +print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic))))

diff --git a/doc/pub/week39/html/._week39-bs047.html b/doc/pub/week39/html/._week39-bs047.html index cd3d8e82b..ef9d36bc4 100644 --- a/doc/pub/week39/html/._week39-bs047.html +++ b/doc/pub/week39/html/._week39-bs047.html @@ -306,11 +306,11 @@ f1_grad = grad(f1) 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))) +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)) +print("The gradient of f1 evaluated at a = %g by finding the analytic expression is: %g"%(a,grad_analytical))

diff --git a/doc/pub/week39/html/._week39-bs048.html b/doc/pub/week39/html/._week39-bs048.html index 557adc05a..02361db35 100644 --- a/doc/pub/week39/html/._week39-bs048.html +++ b/doc/pub/week39/html/._week39-bs048.html @@ -306,8 +306,8 @@ f2_grad_x2 = grad(f2,= 1.0 x2 = 3.0 -print("Evaluating at x1 = %g, x2 = %g"%(x1,x2)) -print("-"*30) +print("Evaluating at x1 = %g, x2 = %g"%(x1,x2)) +print("-"*30) # Compare with the analytical derivatives: @@ -318,13 +318,13 @@ f2_grad_x1_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("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() -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) )) +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) ))

Note that the grad function will not produce the true gradient of the function. The true gradient of a function with two or more variables will produce a vector, where each element is the function differentiated w.r.t a variable. diff --git a/doc/pub/week39/html/._week39-bs049.html b/doc/pub/week39/html/._week39-bs049.html index 7e6074737..c16a02e3f 100644 --- a/doc/pub/week39/html/._week39-bs049.html +++ b/doc/pub/week39/html/._week39-bs049.html @@ -297,13 +297,13 @@ f3_grad = grad(f3) x = np.linspace(0,4,5) # Print the computed gradient: -print("The computed gradient of f3 is: ", f3_grad(x)) +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) +print("The analytical gradient of f3 is: ", f3_grad_analytical)

Note that in this case, when sending an array as input argument, the diff --git a/doc/pub/week39/html/._week39-bs050.html b/doc/pub/week39/html/._week39-bs050.html index 2729af8b2..b464865be 100644 --- a/doc/pub/week39/html/._week39-bs050.html +++ b/doc/pub/week39/html/._week39-bs050.html @@ -297,13 +297,13 @@ 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))) +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)) +print("The analytical gradient of f4 at x = %g is: %g"%(x,f4_grad_analytical))

diff --git a/doc/pub/week39/html/._week39-bs051.html b/doc/pub/week39/html/._week39-bs051.html index 8ae82d470..9ce938862 100644 --- a/doc/pub/week39/html/._week39-bs051.html +++ b/doc/pub/week39/html/._week39-bs051.html @@ -300,7 +300,7 @@ 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))) +print("The computed derivative of f5 at x = %g is: %g"%(x,f5_grad(x)))

diff --git a/doc/pub/week39/html/._week39-bs052.html b/doc/pub/week39/html/._week39-bs052.html index 7115363fe..a2953fc82 100644 --- a/doc/pub/week39/html/._week39-bs052.html +++ b/doc/pub/week39/html/._week39-bs052.html @@ -309,8 +309,8 @@ f6_while_grad = grad(f6_while) x = 0.5 # Print the computed derivaties of f6_for and f6_while -print("The computed derivative of f6_for at x = %g is: %g"%(x,f6_for_grad(x))) -print("The computed derivative of f6_while at x = %g is: %g"%(x,f6_while_grad(x))) +print("The computed derivative of f6_for at x = %g is: %g"%(x,f6_for_grad(x))) +print("The computed derivative of f6_while at x = %g is: %g"%(x,f6_while_grad(x)))

@@ -323,7 +323,7 @@ f6_grad_analytical = 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)) +print("The analytical derivative of f6 at x = %g is: %g"%(x,f6_grad_analytical))

diff --git a/doc/pub/week39/html/._week39-bs053.html b/doc/pub/week39/html/._week39-bs053.html index 13559746a..f899539ed 100644 --- a/doc/pub/week39/html/._week39-bs053.html +++ b/doc/pub/week39/html/._week39-bs053.html @@ -299,7 +299,7 @@ f7_grad = grad(f7) n = 2.0 -print("The computed derivative of f7 at n = %d is: %g"%(n,f7_grad(n))) +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: @@ -312,7 +312,7 @@ f7_grad_analytical = *= (n - k) f7_grad_analytical += tmp -print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical)) +print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical))

Note that if n is equal to zero or one, Autograd will give an error message. This message appears when the output is independent on input. diff --git a/doc/pub/week39/html/._week39-bs054.html b/doc/pub/week39/html/._week39-bs054.html index ce003f63f..aea05d9ec 100644 --- a/doc/pub/week39/html/._week39-bs054.html +++ b/doc/pub/week39/html/._week39-bs054.html @@ -300,7 +300,7 @@ f8_grad = grad(f8) x = 8.4 -print("The derivative of f8 is:",f8_grad(x)) +print("The derivative of f8 is:",f8_grad(x))

Here, Autograd tells us that an 'ArrayBox' does not support item assignment. The item assignment is done when the program tries to assign x[2] to the value 3. However, Autograd has implemented the computation of the derivative such that this assignment is not possible. diff --git a/doc/pub/week39/html/._week39-bs055.html b/doc/pub/week39/html/._week39-bs055.html index 40f96bf8b..247d23d72 100644 --- a/doc/pub/week39/html/._week39-bs055.html +++ b/doc/pub/week39/html/._week39-bs055.html @@ -296,7 +296,7 @@ f9_grad = grad(f9) x = np.array([1.0,0.0]) -print("The derivative of f9 is:",f9_grad(x)) +print("The derivative of f9 is:",f9_grad(x))

Here we are told that the 'dot' function does not belong to Autograd's @@ -316,7 +316,7 @@ f9_alternative_grad = grad(f9_alternative) x = np.array([3.0,0.0]) -print("The gradient of f9 is:",f9_alternative_grad(x)) +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). diff --git a/doc/pub/week39/html/._week39-bs065.html b/doc/pub/week39/html/._week39-bs065.html index 26c2773f9..ca4bb41cd 100644 --- a/doc/pub/week39/html/._week39-bs065.html +++ b/doc/pub/week39/html/._week39-bs065.html @@ -304,7 +304,7 @@ MathJax.Hub.Config({ fig = pt.figure() ax = fig.gca(projection="3d") -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] +xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] fmesh = f(np.array([xmesh, ymesh])) ax.plot_surface(xmesh, ymesh, fmesh) @@ -336,7 +336,7 @@ Run it! alpha_opt = sopt.golden(f1d) next_guess = x + alpha_opt * s guesses.append(next_guess) -print(next_guess) +print(next_guess)

What happened? diff --git a/doc/pub/week39/html/week39-bs.html b/doc/pub/week39/html/week39-bs.html index ee689d11b..d1107c7b3 100644 --- a/doc/pub/week39/html/week39-bs.html +++ b/doc/pub/week39/html/week39-bs.html @@ -301,7 +301,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 22, 2020

+

Sep 24, 2020


diff --git a/doc/pub/week39/html/week39-reveal.html b/doc/pub/week39/html/week39-reveal.html index e89e0b683..382f963eb 100644 --- a/doc/pub/week39/html/week39-reveal.html +++ b/doc/pub/week39/html/week39-reveal.html @@ -148,7 +148,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

 
-

Sep 22, 2020

+

Sep 24, 2020


@@ -790,7 +790,7 @@ y = 4+3* xb = np.c_[np.ones((m,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print(beta_linreg) +print(beta_linreg) beta = np.random.randn(2,1) eta = 0.1 @@ -800,7 +800,7 @@ Niterations = 1000 gradients = 2.0/m*xb.T.dot(xb.dot(beta)-y) beta -= eta*gradients -print(beta) +print(beta) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] ypredict = xbnew.dot(beta) @@ -834,10 +834,10 @@ y = 4+3* xb = np.c_[np.ones((100,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print(beta_linreg) -sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1) +print(beta_linreg) +sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) sgdreg.fit(x,y.ravel()) -print(sgdreg.intercept_, sgdreg.coef_) +print(sgdreg.intercept_, sgdreg.coef_) @@ -892,14 +892,14 @@ x = 2*np.random.rand(m,4+3*x+np.random.randn(m,1) xb = np.c_[np.ones((m,1)), x] -XT_X = xb.T @ xb +XT_X = xb.T @ xb #Ridge parameter lambda lmbda = 0.001 Id = lmbda* np.eye(XT_X.shape[0]) -beta_linreg = np.linalg.inv(XT_X+Id) @ xb.T @ y -print(beta_linreg) +beta_linreg = np.linalg.inv(XT_X+Id) @ xb.T @ y +print(beta_linreg) # Start plain gradient descent beta = np.random.randn(2,1) @@ -907,12 +907,12 @@ eta = 0.1 Niterations = 100 for iter in range(Niterations): - gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta + gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta beta -= eta*gradients -print(beta) -ypredict = xb @ beta -ypredict2 = xb @ beta_linreg +print(beta) +ypredict = xb @ beta +ypredict2 = xb @ beta_linreg plt.plot(x, ypredict, "r-") plt.plot(x, ypredict2, "b-") plt.plot(x, y ,'ro') @@ -1128,7 +1128,7 @@ j = 0 gamma_j = step_length(t,t0,t1) j += 1 -print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j)) +print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j)) @@ -1152,12 +1152,12 @@ y = 4+3* xb = np.c_[np.ones((m,1)), x] theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print("Own inversion") -print(theta_linreg) -sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) +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_) +print("sgdreg from scikit") +print(sgdreg.intercept_, sgdreg.coef_) theta = np.random.randn(2,1) @@ -1166,10 +1166,10 @@ Niterations = 1000 for iter in range(Niterations): - gradients = 2.0/m*xb.T @ ((xb @ theta)-y) + gradients = 2.0/m*xb.T @ ((xb @ theta)-y) theta -= eta*gradients -print("theta frm own gd") -print(theta) +print("theta frm own gd") +print(theta) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] @@ -1189,11 +1189,11 @@ theta = np.random.randn(2,1] yi = y[random_index:random_index+1] - gradients = 2 * xi.T @ ((xi @ theta)-yi) + gradients = 2 * xi.T @ ((xi @ theta)-yi) eta = learning_schedule(epoch*m+i) theta = theta - eta*gradients -print("theta from own sdg") -print(theta) +print("theta from own sdg") +print(theta) plt.plot(xnew, ypredict, "r-") plt.plot(xnew, ypredict2, "b-") @@ -1561,7 +1561,7 @@ plt.legend() plt.show() -print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic)))) +print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic)))) @@ -1591,11 +1591,11 @@ f1_grad = grad(f1) 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))) +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)) +print("The gradient of f1 evaluated at a = %g by finding the analytic expression is: %g"%(a,grad_analytical)) @@ -1625,8 +1625,8 @@ f2_grad_x2 = grad(f2,1) x1 = 1.0 x2 = 3.0 -print("Evaluating at x1 = %g, x2 = %g"%(x1,x2)) -print("-"*30) +print("Evaluating at x1 = %g, x2 = %g"%(x1,x2)) +print("-"*30) # Compare with the analytical derivatives: @@ -1637,13 +1637,13 @@ f2_grad_x1_analytical = 9*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("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() -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) )) +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) ))

Note that the grad function will not produce the true gradient of the function. The true gradient of a function with two or more variables will produce a vector, where each element is the function differentiated w.r.t a variable. @@ -1666,13 +1666,13 @@ f3_grad = grad(f3) x = np.linspace(0,4,5) # Print the computed gradient: -print("The computed gradient of f3 is: ", f3_grad(x)) +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) +print("The analytical gradient of f3 is: ", f3_grad_analytical)

Note that in this case, when sending an array as input argument, the @@ -1700,13 +1700,13 @@ 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))) +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)) +print("The analytical gradient of f4 at x = %g is: %g"%(x,f4_grad_analytical)) @@ -1730,7 +1730,7 @@ 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))) +print("The computed derivative of f5 at x = %g is: %g"%(x,f5_grad(x))) @@ -1763,8 +1763,8 @@ f6_while_grad = grad(f6_while) x = 0.5 # Print the computed derivaties of f6_for and f6_while -print("The computed derivative of f6_for at x = %g is: %g"%(x,f6_for_grad(x))) -print("The computed derivative of f6_while at x = %g is: %g"%(x,f6_while_grad(x))) +print("The computed derivative of f6_for at x = %g is: %g"%(x,f6_for_grad(x))) +print("The computed derivative of f6_while at x = %g is: %g"%(x,f6_while_grad(x)))

@@ -1777,7 +1777,7 @@ 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)) +print("The analytical derivative of f6 at x = %g is: %g"%(x,f6_grad_analytical)) @@ -1800,7 +1800,7 @@ f7_grad = grad(f7) n = 2.0 -print("The computed derivative of f7 at n = %d is: %g"%(n,f7_grad(n))) +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: @@ -1813,7 +1813,7 @@ f7_grad_analytical = 0 tmp *= (n - k) f7_grad_analytical += tmp -print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical)) +print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical))

Note that if n is equal to zero or one, Autograd will give an error message. This message appears when the output is independent on input. @@ -1839,7 +1839,7 @@ f8_grad = grad(f8) x = 8.4 -print("The derivative of f8 is:",f8_grad(x)) +print("The derivative of f8 is:",f8_grad(x))

Here, Autograd tells us that an 'ArrayBox' does not support item assignment. The item assignment is done when the program tries to assign x[2] to the value 3. However, Autograd has implemented the computation of the derivative such that this assignment is not possible. @@ -1861,7 +1861,7 @@ f9_grad = grad(f9) x = np.array([1.0,0.0]) -print("The derivative of f9 is:",f9_grad(x)) +print("The derivative of f9 is:",f9_grad(x))

Here we are told that the 'dot' function does not belong to Autograd's @@ -1881,7 +1881,7 @@ f9_alternative_grad = grad(f9_alternative) x = np.array([3.0,0.0]) -print("The gradient of f9 is:",f9_alternative_grad(x)) +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). @@ -2174,7 +2174,7 @@ $$ fig = pt.figure() ax = fig.gca(projection="3d") -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] +xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] fmesh = f(np.array([xmesh, ymesh])) ax.plot_surface(xmesh, ymesh, fmesh) @@ -2206,7 +2206,7 @@ Run it! alpha_opt = sopt.golden(f1d) next_guess = x + alpha_opt * s guesses.append(next_guess) -print(next_guess) +print(next_guess)

What happened? diff --git a/doc/pub/week39/html/week39-solarized.html b/doc/pub/week39/html/week39-solarized.html index f229262d6..b6409b568 100644 --- a/doc/pub/week39/html/week39-solarized.html +++ b/doc/pub/week39/html/week39-solarized.html @@ -216,7 +216,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 22, 2020

+

Sep 24, 2020












@@ -801,7 +801,7 @@ y = 4+3* xb = np.c_[np.ones((m,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print(beta_linreg) +print(beta_linreg) beta = np.random.randn(2,1) eta = 0.1 @@ -811,7 +811,7 @@ Niterations = 1000 gradients = 2.0/m*xb.T.dot(xb.dot(beta)-y) beta -= eta*gradients -print(beta) +print(beta) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] ypredict = xbnew.dot(beta) @@ -844,10 +844,10 @@ y = 4+3* xb = np.c_[np.ones((100,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print(beta_linreg) -sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1) +print(beta_linreg) +sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) sgdreg.fit(x,y.ravel()) -print(sgdreg.intercept_, sgdreg.coef_) +print(sgdreg.intercept_, sgdreg.coef_)

@@ -895,14 +895,14 @@ x = 2*np.random.rand(m,4+3*x+np.random.randn(m,1) xb = np.c_[np.ones((m,1)), x] -XT_X = xb.T @ xb +XT_X = xb.T @ xb #Ridge parameter lambda lmbda = 0.001 Id = lmbda* np.eye(XT_X.shape[0]) -beta_linreg = np.linalg.inv(XT_X+Id) @ xb.T @ y -print(beta_linreg) +beta_linreg = np.linalg.inv(XT_X+Id) @ xb.T @ y +print(beta_linreg) # Start plain gradient descent beta = np.random.randn(2,1) @@ -910,12 +910,12 @@ eta = 0.1 Niterations = 100 for iter in range(Niterations): - gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta + gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta beta -= eta*gradients -print(beta) -ypredict = xb @ beta -ypredict2 = xb @ beta_linreg +print(beta) +ypredict = xb @ beta +ypredict2 = xb @ beta_linreg plt.plot(x, ypredict, "r-") plt.plot(x, ypredict2, "b-") plt.plot(x, y ,'ro') @@ -1119,7 +1119,7 @@ j = 0 gamma_j = step_length(t,t0,t1) j += 1 -print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j)) +print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j))











@@ -1142,12 +1142,12 @@ y = 4+3* xb = np.c_[np.ones((m,1)), x] theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print("Own inversion") -print(theta_linreg) -sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) +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_) +print("sgdreg from scikit") +print(sgdreg.intercept_, sgdreg.coef_) theta = np.random.randn(2,1) @@ -1156,10 +1156,10 @@ Niterations = 1000 for iter in range(Niterations): - gradients = 2.0/m*xb.T @ ((xb @ theta)-y) + gradients = 2.0/m*xb.T @ ((xb @ theta)-y) theta -= eta*gradients -print("theta frm own gd") -print(theta) +print("theta frm own gd") +print(theta) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] @@ -1179,11 +1179,11 @@ theta = np.random.randn(2,1] yi = y[random_index:random_index+1] - gradients = 2 * xi.T @ ((xi @ theta)-yi) + gradients = 2 * xi.T @ ((xi @ theta)-yi) eta = learning_schedule(epoch*m+i) theta = theta - eta*gradients -print("theta from own sdg") -print(theta) +print("theta from own sdg") +print(theta) plt.plot(xnew, ypredict, "r-") plt.plot(xnew, ypredict2, "b-") @@ -1525,7 +1525,7 @@ plt.legend() plt.show() -print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic)))) +print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic))))

@@ -1554,11 +1554,11 @@ f1_grad = grad(f1) 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))) +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)) +print("The gradient of f1 evaluated at a = %g by finding the analytic expression is: %g"%(a,grad_analytical))











@@ -1587,8 +1587,8 @@ f2_grad_x2 = grad(f2,1) x1 = 1.0 x2 = 3.0 -print("Evaluating at x1 = %g, x2 = %g"%(x1,x2)) -print("-"*30) +print("Evaluating at x1 = %g, x2 = %g"%(x1,x2)) +print("-"*30) # Compare with the analytical derivatives: @@ -1599,13 +1599,13 @@ f2_grad_x1_analytical = 9*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("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() -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) )) +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) ))

Note that the grad function will not produce the true gradient of the function. The true gradient of a function with two or more variables will produce a vector, where each element is the function differentiated w.r.t a variable. @@ -1628,13 +1628,13 @@ f3_grad = grad(f3) x = np.linspace(0,4,5) # Print the computed gradient: -print("The computed gradient of f3 is: ", f3_grad(x)) +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) +print("The analytical gradient of f3 is: ", f3_grad_analytical)

Note that in this case, when sending an array as input argument, the @@ -1662,13 +1662,13 @@ 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))) +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)) +print("The analytical gradient of f4 at x = %g is: %g"%(x,f4_grad_analytical))











@@ -1691,7 +1691,7 @@ 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))) +print("The computed derivative of f5 at x = %g is: %g"%(x,f5_grad(x)))











@@ -1723,8 +1723,8 @@ f6_while_grad = grad(f6_while) x = 0.5 # Print the computed derivaties of f6_for and f6_while -print("The computed derivative of f6_for at x = %g is: %g"%(x,f6_for_grad(x))) -print("The computed derivative of f6_while at x = %g is: %g"%(x,f6_while_grad(x))) +print("The computed derivative of f6_for at x = %g is: %g"%(x,f6_for_grad(x))) +print("The computed derivative of f6_while at x = %g is: %g"%(x,f6_while_grad(x)))

@@ -1737,7 +1737,7 @@ 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)) +print("The analytical derivative of f6 at x = %g is: %g"%(x,f6_grad_analytical))











@@ -1759,7 +1759,7 @@ f7_grad = grad(f7) n = 2.0 -print("The computed derivative of f7 at n = %d is: %g"%(n,f7_grad(n))) +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: @@ -1772,7 +1772,7 @@ f7_grad_analytical = 0 tmp *= (n - k) f7_grad_analytical += tmp -print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical)) +print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical))

Note that if n is equal to zero or one, Autograd will give an error message. This message appears when the output is independent on input. @@ -1798,7 +1798,7 @@ f8_grad = grad(f8) x = 8.4 -print("The derivative of f8 is:",f8_grad(x)) +print("The derivative of f8 is:",f8_grad(x))

Here, Autograd tells us that an 'ArrayBox' does not support item assignment. The item assignment is done when the program tries to assign x[2] to the value 3. However, Autograd has implemented the computation of the derivative such that this assignment is not possible. @@ -1820,7 +1820,7 @@ f9_grad = grad(f9) x = np.array([1.0,0.0]) -print("The derivative of f9 is:",f9_grad(x)) +print("The derivative of f9 is:",f9_grad(x))

Here we are told that the 'dot' function does not belong to Autograd's @@ -1840,7 +1840,7 @@ f9_alternative_grad = grad(f9_alternative) x = np.array([3.0,0.0]) -print("The gradient of f9 is:",f9_alternative_grad(x)) +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). @@ -2113,7 +2113,7 @@ $$ fig = pt.figure() ax = fig.gca(projection="3d") -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] +xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] fmesh = f(np.array([xmesh, ymesh])) ax.plot_surface(xmesh, ymesh, fmesh) @@ -2145,7 +2145,7 @@ Run it! alpha_opt = sopt.golden(f1d) next_guess = x + alpha_opt * s guesses.append(next_guess) -print(next_guess) +print(next_guess)

What happened? diff --git a/doc/pub/week39/html/week39.html b/doc/pub/week39/html/week39.html index 3310762ca..23b999f95 100644 --- a/doc/pub/week39/html/week39.html +++ b/doc/pub/week39/html/week39.html @@ -221,7 +221,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 22, 2020

+

Sep 24, 2020












@@ -806,7 +806,7 @@ y = 4+3* xb = np.c_[np.ones((m,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print(beta_linreg) +print(beta_linreg) beta = np.random.randn(2,1) eta = 0.1 @@ -816,7 +816,7 @@ Niterations = 1 gradients = 2.0/m*xb.T.dot(xb.dot(beta)-y) beta -= eta*gradients -print(beta) +print(beta) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] ypredict = xbnew.dot(beta) @@ -849,10 +849,10 @@ y = 4+3* xb = np.c_[np.ones((100,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print(beta_linreg) -sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1) +print(beta_linreg) +sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) sgdreg.fit(x,y.ravel()) -print(sgdreg.intercept_, sgdreg.coef_) +print(sgdreg.intercept_, sgdreg.coef_)

@@ -900,14 +900,14 @@ x = 2*np y = 4+3*x+np.random.randn(m,1) xb = np.c_[np.ones((m,1)), x] -XT_X = xb.T @ xb +XT_X = xb.T @ xb #Ridge parameter lambda lmbda = 0.001 Id = lmbda* np.eye(XT_X.shape[0]) -beta_linreg = np.linalg.inv(XT_X+Id) @ xb.T @ y -print(beta_linreg) +beta_linreg = np.linalg.inv(XT_X+Id) @ xb.T @ y +print(beta_linreg) # Start plain gradient descent beta = np.random.randn(2,1) @@ -915,12 +915,12 @@ eta = 0.1= 100 for iter in range(Niterations): - gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta + gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta beta -= eta*gradients -print(beta) -ypredict = xb @ beta -ypredict2 = xb @ beta_linreg +print(beta) +ypredict = xb @ beta +ypredict2 = xb @ beta_linreg plt.plot(x, ypredict, "r-") plt.plot(x, ypredict2, "b-") plt.plot(x, y ,'ro') @@ -1124,7 +1124,7 @@ j = 0 gamma_j = step_length(t,t0,t1) j += 1 -print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j)) +print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j))











@@ -1147,12 +1147,12 @@ y = 4+3* xb = np.c_[np.ones((m,1)), x] theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -print("Own inversion") -print(theta_linreg) -sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) +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_) +print("sgdreg from scikit") +print(sgdreg.intercept_, sgdreg.coef_) theta = np.random.randn(2,1) @@ -1161,10 +1161,10 @@ Niterations = 1 for iter in range(Niterations): - gradients = 2.0/m*xb.T @ ((xb @ theta)-y) + gradients = 2.0/m*xb.T @ ((xb @ theta)-y) theta -= eta*gradients -print("theta frm own gd") -print(theta) +print("theta frm own gd") +print(theta) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] @@ -1184,11 +1184,11 @@ theta = np.= np.random.randint(m) xi = xb[random_index:random_index+1] yi = y[random_index:random_index+1] - gradients = 2 * xi.T @ ((xi @ theta)-yi) + gradients = 2 * xi.T @ ((xi @ theta)-yi) eta = learning_schedule(epoch*m+i) theta = theta - eta*gradients -print("theta from own sdg") -print(theta) +print("theta from own sdg") +print(theta) plt.plot(xnew, ypredict, "r-") plt.plot(xnew, ypredict2, "b-") @@ -1530,7 +1530,7 @@ plt.legend() plt.show() -print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic)))) +print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic))))

@@ -1559,11 +1559,11 @@ f1_grad = grad(f1) 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))) +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)) +print("The gradient of f1 evaluated at a = %g by finding the analytic expression is: %g"%(a,grad_analytical))











@@ -1592,8 +1592,8 @@ f2_grad_x2 = grad(f2,= 1.0 x2 = 3.0 -print("Evaluating at x1 = %g, x2 = %g"%(x1,x2)) -print("-"*30) +print("Evaluating at x1 = %g, x2 = %g"%(x1,x2)) +print("-"*30) # Compare with the analytical derivatives: @@ -1604,13 +1604,13 @@ f2_grad_x1_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("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() -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) )) +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) ))

Note that the grad function will not produce the true gradient of the function. The true gradient of a function with two or more variables will produce a vector, where each element is the function differentiated w.r.t a variable. @@ -1633,13 +1633,13 @@ f3_grad = grad(f3) x = np.linspace(0,4,5) # Print the computed gradient: -print("The computed gradient of f3 is: ", f3_grad(x)) +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) +print("The analytical gradient of f3 is: ", f3_grad_analytical)

Note that in this case, when sending an array as input argument, the @@ -1667,13 +1667,13 @@ 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))) +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)) +print("The analytical gradient of f4 at x = %g is: %g"%(x,f4_grad_analytical))











@@ -1696,7 +1696,7 @@ 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))) +print("The computed derivative of f5 at x = %g is: %g"%(x,f5_grad(x)))











@@ -1728,8 +1728,8 @@ f6_while_grad = grad(f6_while) x = 0.5 # Print the computed derivaties of f6_for and f6_while -print("The computed derivative of f6_for at x = %g is: %g"%(x,f6_for_grad(x))) -print("The computed derivative of f6_while at x = %g is: %g"%(x,f6_while_grad(x))) +print("The computed derivative of f6_for at x = %g is: %g"%(x,f6_for_grad(x))) +print("The computed derivative of f6_while at x = %g is: %g"%(x,f6_while_grad(x)))

@@ -1742,7 +1742,7 @@ f6_grad_analytical = 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)) +print("The analytical derivative of f6 at x = %g is: %g"%(x,f6_grad_analytical))











@@ -1764,7 +1764,7 @@ f7_grad = grad(f7) n = 2.0 -print("The computed derivative of f7 at n = %d is: %g"%(n,f7_grad(n))) +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: @@ -1777,7 +1777,7 @@ f7_grad_analytical = *= (n - k) f7_grad_analytical += tmp -print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical)) +print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical))

Note that if n is equal to zero or one, Autograd will give an error message. This message appears when the output is independent on input. @@ -1803,7 +1803,7 @@ f8_grad = grad(f8) x = 8.4 -print("The derivative of f8 is:",f8_grad(x)) +print("The derivative of f8 is:",f8_grad(x))

Here, Autograd tells us that an 'ArrayBox' does not support item assignment. The item assignment is done when the program tries to assign x[2] to the value 3. However, Autograd has implemented the computation of the derivative such that this assignment is not possible. @@ -1825,7 +1825,7 @@ f9_grad = grad(f9) x = np.array([1.0,0.0]) -print("The derivative of f9 is:",f9_grad(x)) +print("The derivative of f9 is:",f9_grad(x))

Here we are told that the 'dot' function does not belong to Autograd's @@ -1845,7 +1845,7 @@ f9_alternative_grad = grad(f9_alternative) x = np.array([3.0,0.0]) -print("The gradient of f9 is:",f9_alternative_grad(x)) +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). @@ -2118,7 +2118,7 @@ $$ fig = pt.figure() ax = fig.gca(projection="3d") -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] +xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] fmesh = f(np.array([xmesh, ymesh])) ax.plot_surface(xmesh, ymesh, fmesh) @@ -2150,7 +2150,7 @@ Run it! alpha_opt = sopt.golden(f1d) next_guess = x + alpha_opt * s guesses.append(next_guess) -print(next_guess) +print(next_guess)

What happened? diff --git a/doc/pub/week39/ipynb/ipynb-week39-src.tar.gz b/doc/pub/week39/ipynb/ipynb-week39-src.tar.gz index 7b4b0bd7d04a6caee96f35e4ff25cd32f91eaca3..e1e5227affc6fc1eee30898c3706695c6fa4bd50 100644 GIT binary patch literal 191 zcmV;w06_mAiwFQ1B5Yp(1MSaC3c@fD2H>uHia9}{g};8r^E}V{+5`F=LG1tt007*_SVRB- literal 197 zcmb2|=3r14&5CDWetUjn-XQ~#*2MERM{Aoj{A85eR3xlIoJ92#L{>*M@b(Fwy&b&k zK+_BXr@#B3@;vAHUUzo+=JS;k>o--+`gFY_M{nirWqZ!rN11zF&yAIgmAt$vD14Wp z*Qw`oOG@8t_EpLf4=#K7?_J>kWz(|npFW;+; \n", "**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n", "\n", - "Date: **Sep 22, 2020**\n", + "Date: **Sep 24, 2020**\n", "\n", "Copyright 1999-2020, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n", "\n", @@ -812,7 +812,7 @@ "xb = np.c_[np.ones((100,1)), x]\n", "beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)\n", "print(beta_linreg)\n", - "sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1)\n", + "sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1)\n", "sgdreg.fit(x,y.ravel())\n", "print(sgdreg.intercept_, sgdreg.coef_)" ] @@ -3043,5 +3043,5 @@ ], "metadata": {}, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/doc/src/week39/week39.do.txt b/doc/src/week39/week39.do.txt index 67959c123..9ed997293 100644 --- a/doc/src/week39/week39.do.txt +++ b/doc/src/week39/week39.do.txt @@ -553,7 +553,7 @@ y = 4+3*x+np.random.randn(100,1) xb = np.c_[np.ones((100,1)), x] beta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) print(beta_linreg) -sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1) +sgdreg = SGDRegressor(max_iter = 50, penalty=None, eta0=0.1) sgdreg.fit(x,y.ravel()) print(sgdreg.intercept_, sgdreg.coef_)