diff --git a/doc/pub/week39/html/week39-bs.html b/doc/pub/week39/html/week39-bs.html index e853e960d..c7edb4c96 100644 --- a/doc/pub/week39/html/week39-bs.html +++ b/doc/pub/week39/html/week39-bs.html @@ -152,6 +152,14 @@ doconce format html week39.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'using-gradient-descent-methods-limitations'), + ('Improving gradient descent with momentum', + 2, + None, + 'improving-gradient-descent-with-momentum'), + ('Same code but now with momentum gradient descent', + 2, + None, + 'same-code-but-now-with-momentum-gradient-descent'), ('Overview video on Stochastic Gradient Descent', 2, None, @@ -310,41 +318,43 @@ MathJax.Hub.Config({
We discuss here some simple examples where we introduce what is called 'memory'about previous steps, or what is normally called momentum gradient descent. The mathematics is explained below in connection with Stochastic gradient descent.
+ + + +from numpy import asarray
+from numpy import arange
+from numpy.random import rand
+from numpy.random import seed
+from matplotlib import pyplot
+
+# objective function
+def objective(x):
+ return x**2.0
+
+# derivative of objective function
+def derivative(x):
+ return x * 2.0
+
+# gradient descent algorithm
+def gradient_descent(objective, derivative, bounds, n_iter, step_size):
+ # track all solutions
+ solutions, scores = list(), list()
+ # generate an initial point
+ solution = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
+ # run the gradient descent
+ for i in range(n_iter):
+ # calculate gradient
+ gradient = derivative(solution)
+ # take a step
+ solution = solution - step_size * gradient
+ # evaluate candidate point
+ solution_eval = objective(solution)
+ # store solution
+ solutions.append(solution)
+ scores.append(solution_eval)
+ # report progress
+ print('>%d f(%s) = %.5f' % (i, solution, solution_eval))
+ return [solutions, scores]
+
+# seed the pseudo random number generator
+seed(4)
+# define range for input
+bounds = asarray([[-1.0, 1.0]])
+# define the total iterations
+n_iter = 30
+# define the step size
+step_size = 0.1
+# perform the gradient descent search
+solutions, scores = gradient_descent(objective, derivative, bounds, n_iter, step_size)
+# sample input range uniformly at 0.1 increments
+inputs = arange(bounds[0,0], bounds[0,1]+0.1, 0.1)
+# compute targets
+results = objective(inputs)
+# create a line plot of input vs result
+pyplot.plot(inputs, results)
+# plot the solutions found
+pyplot.plot(solutions, scores, '.-', color='red')
+# show the plot
+pyplot.show()
+
+from numpy import asarray
+from numpy import arange
+from numpy.random import rand
+from numpy.random import seed
+from matplotlib import pyplot
+
+# objective function
+def objective(x):
+ return x**2.0
+
+# derivative of objective function
+def derivative(x):
+ return x * 2.0
+
+# gradient descent algorithm
+def gradient_descent(objective, derivative, bounds, n_iter, step_size, momentum):
+ # track all solutions
+ solutions, scores = list(), list()
+ # generate an initial point
+ solution = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
+ # keep track of the change
+ change = 0.0
+ # run the gradient descent
+ for i in range(n_iter):
+ # calculate gradient
+ gradient = derivative(solution)
+ # calculate update
+ new_change = step_size * gradient + momentum * change
+ # take a step
+ solution = solution - new_change
+ # save the change
+ change = new_change
+ # evaluate candidate point
+ solution_eval = objective(solution)
+ # store solution
+ solutions.append(solution)
+ scores.append(solution_eval)
+ # report progress
+ print('>%d f(%s) = %.5f' % (i, solution, solution_eval))
+ return [solutions, scores]
+
+# seed the pseudo random number generator
+seed(4)
+# define range for input
+bounds = asarray([[-1.0, 1.0]])
+# define the total iterations
+n_iter = 30
+# define the step size
+step_size = 0.1
+# define momentum
+momentum = 0.3
+# perform the gradient descent search with momentum
+solutions, scores = gradient_descent(objective, derivative, bounds, n_iter, step_size, momentum)
+# sample input range uniformly at 0.1 increments
+inputs = arange(bounds[0,0], bounds[0,1]+0.1, 0.1)
+# compute targets
+results = objective(inputs)
+# create a line plot of input vs result
+pyplot.plot(inputs, results)
+# plot the solutions found
+pyplot.plot(solutions, scores, '.-', color='red')
+# show the plot
+pyplot.show()
+
+We discuss here some simple examples where we introduce what is called 'memory'about previous steps, or what is normally called momentum gradient descent. The mathematics is explained below in connection with Stochastic gradient descent.
+ + + +from numpy import asarray
+from numpy import arange
+from numpy.random import rand
+from numpy.random import seed
+from matplotlib import pyplot
+
+# objective function
+def objective(x):
+ return x**2.0
+
+# derivative of objective function
+def derivative(x):
+ return x * 2.0
+
+# gradient descent algorithm
+def gradient_descent(objective, derivative, bounds, n_iter, step_size):
+ # track all solutions
+ solutions, scores = list(), list()
+ # generate an initial point
+ solution = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
+ # run the gradient descent
+ for i in range(n_iter):
+ # calculate gradient
+ gradient = derivative(solution)
+ # take a step
+ solution = solution - step_size * gradient
+ # evaluate candidate point
+ solution_eval = objective(solution)
+ # store solution
+ solutions.append(solution)
+ scores.append(solution_eval)
+ # report progress
+ print('>%d f(%s) = %.5f' % (i, solution, solution_eval))
+ return [solutions, scores]
+
+# seed the pseudo random number generator
+seed(4)
+# define range for input
+bounds = asarray([[-1.0, 1.0]])
+# define the total iterations
+n_iter = 30
+# define the step size
+step_size = 0.1
+# perform the gradient descent search
+solutions, scores = gradient_descent(objective, derivative, bounds, n_iter, step_size)
+# sample input range uniformly at 0.1 increments
+inputs = arange(bounds[0,0], bounds[0,1]+0.1, 0.1)
+# compute targets
+results = objective(inputs)
+# create a line plot of input vs result
+pyplot.plot(inputs, results)
+# plot the solutions found
+pyplot.plot(solutions, scores, '.-', color='red')
+# show the plot
+pyplot.show()
+
+from numpy import asarray
+from numpy import arange
+from numpy.random import rand
+from numpy.random import seed
+from matplotlib import pyplot
+
+# objective function
+def objective(x):
+ return x**2.0
+
+# derivative of objective function
+def derivative(x):
+ return x * 2.0
+
+# gradient descent algorithm
+def gradient_descent(objective, derivative, bounds, n_iter, step_size, momentum):
+ # track all solutions
+ solutions, scores = list(), list()
+ # generate an initial point
+ solution = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
+ # keep track of the change
+ change = 0.0
+ # run the gradient descent
+ for i in range(n_iter):
+ # calculate gradient
+ gradient = derivative(solution)
+ # calculate update
+ new_change = step_size * gradient + momentum * change
+ # take a step
+ solution = solution - new_change
+ # save the change
+ change = new_change
+ # evaluate candidate point
+ solution_eval = objective(solution)
+ # store solution
+ solutions.append(solution)
+ scores.append(solution_eval)
+ # report progress
+ print('>%d f(%s) = %.5f' % (i, solution, solution_eval))
+ return [solutions, scores]
+
+# seed the pseudo random number generator
+seed(4)
+# define range for input
+bounds = asarray([[-1.0, 1.0]])
+# define the total iterations
+n_iter = 30
+# define the step size
+step_size = 0.1
+# define momentum
+momentum = 0.3
+# perform the gradient descent search with momentum
+solutions, scores = gradient_descent(objective, derivative, bounds, n_iter, step_size, momentum)
+# sample input range uniformly at 0.1 increments
+inputs = arange(bounds[0,0], bounds[0,1]+0.1, 0.1)
+# compute targets
+results = objective(inputs)
+# create a line plot of input vs result
+pyplot.plot(inputs, results)
+# plot the solutions found
+pyplot.plot(solutions, scores, '.-', color='red')
+# show the plot
+pyplot.show()
+
+We discuss here some simple examples where we introduce what is called 'memory'about previous steps, or what is normally called momentum gradient descent. The mathematics is explained below in connection with Stochastic gradient descent.
+ + + +from numpy import asarray
+from numpy import arange
+from numpy.random import rand
+from numpy.random import seed
+from matplotlib import pyplot
+
+# objective function
+def objective(x):
+ return x**2.0
+
+# derivative of objective function
+def derivative(x):
+ return x * 2.0
+
+# gradient descent algorithm
+def gradient_descent(objective, derivative, bounds, n_iter, step_size):
+ # track all solutions
+ solutions, scores = list(), list()
+ # generate an initial point
+ solution = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
+ # run the gradient descent
+ for i in range(n_iter):
+ # calculate gradient
+ gradient = derivative(solution)
+ # take a step
+ solution = solution - step_size * gradient
+ # evaluate candidate point
+ solution_eval = objective(solution)
+ # store solution
+ solutions.append(solution)
+ scores.append(solution_eval)
+ # report progress
+ print('>%d f(%s) = %.5f' % (i, solution, solution_eval))
+ return [solutions, scores]
+
+# seed the pseudo random number generator
+seed(4)
+# define range for input
+bounds = asarray([[-1.0, 1.0]])
+# define the total iterations
+n_iter = 30
+# define the step size
+step_size = 0.1
+# perform the gradient descent search
+solutions, scores = gradient_descent(objective, derivative, bounds, n_iter, step_size)
+# sample input range uniformly at 0.1 increments
+inputs = arange(bounds[0,0], bounds[0,1]+0.1, 0.1)
+# compute targets
+results = objective(inputs)
+# create a line plot of input vs result
+pyplot.plot(inputs, results)
+# plot the solutions found
+pyplot.plot(solutions, scores, '.-', color='red')
+# show the plot
+pyplot.show()
+
+from numpy import asarray
+from numpy import arange
+from numpy.random import rand
+from numpy.random import seed
+from matplotlib import pyplot
+
+# objective function
+def objective(x):
+ return x**2.0
+
+# derivative of objective function
+def derivative(x):
+ return x * 2.0
+
+# gradient descent algorithm
+def gradient_descent(objective, derivative, bounds, n_iter, step_size, momentum):
+ # track all solutions
+ solutions, scores = list(), list()
+ # generate an initial point
+ solution = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
+ # keep track of the change
+ change = 0.0
+ # run the gradient descent
+ for i in range(n_iter):
+ # calculate gradient
+ gradient = derivative(solution)
+ # calculate update
+ new_change = step_size * gradient + momentum * change
+ # take a step
+ solution = solution - new_change
+ # save the change
+ change = new_change
+ # evaluate candidate point
+ solution_eval = objective(solution)
+ # store solution
+ solutions.append(solution)
+ scores.append(solution_eval)
+ # report progress
+ print('>%d f(%s) = %.5f' % (i, solution, solution_eval))
+ return [solutions, scores]
+
+# seed the pseudo random number generator
+seed(4)
+# define range for input
+bounds = asarray([[-1.0, 1.0]])
+# define the total iterations
+n_iter = 30
+# define the step size
+step_size = 0.1
+# define momentum
+momentum = 0.3
+# perform the gradient descent search with momentum
+solutions, scores = gradient_descent(objective, derivative, bounds, n_iter, step_size, momentum)
+# sample input range uniformly at 0.1 increments
+inputs = arange(bounds[0,0], bounds[0,1]+0.1, 0.1)
+# compute targets
+results = objective(inputs)
+# create a line plot of input vs result
+pyplot.plot(inputs, results)
+# plot the solutions found
+pyplot.plot(solutions, scores, '.-', color='red')
+# show the plot
+pyplot.show()
+
+