You are on page 1of 28

!

Navigation

Click here Take the FREE Optimization Crash-Course

Search... "

Code Adam Optimization Algorithm From


Scratch
by Jason Brownlee on January 13, 2021 in Optimization

Tweet Tweet Share Share

Last Updated on October 12, 2021

Gradient descent is an optimization algorithm that follows the negative gradient of an


objective function in order to locate the minimum of the function.

A limitation of gradient descent is that a single step size (learning rate) is used for all input
variables. Extensions
Start Machine to gradient descent like AdaGrad and RMSProp update the algorithm
Learning
to use a separate step size for each input variable but may result in a step size that rapidly
decreases to very small values.

The Adaptive Movement Estimation algorithm, or Adam for short, is an extension to


Start descent
gradient Machine and aLearning ×
natural successor to techniques like AdaGrad and RMSProp that
automatically adapts a learning rate for each input variable for the objective function and
You cansmooths
further master applied Machine
the search Learning
process by using an exponentially decreasing moving average of
without math or fancy degrees.
the gradient to make updates to variables.
Find out how in this free and practical course.

In this tutorial, you will discover how to develop gradient descent with Adam optimization
Email Address
algorithm from scratch.

After completing this tutorial, you will know:


I consent to receive information about services and

Gradient
special descent
offers by is an
email. For optimization
more information, algorithm
see the that uses the gradient of the objective
function
Privacy Policy.to navigate the search space.
Gradient descent can be updated to use an automatically adaptive step size for each
START MY EMAIL COURSE
:
input variable using a decaying average of partial derivatives, called Adam.
How to implement the Adam optimization algorithm from scratch and apply it to an
objective function and evaluate the results.

Kick-start your project with my new book Optimization for Machine Learning, including
step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

Gradient Descent Optimization With Adam From Scratch


Photo by Don Graham, some rights reserved.

Tutorial Overview
This tutorial is divided into three parts; they are:

1. Gradient Descent
2. Adam Optimization Algorithm
3. Gradient Descent With Adam
1. Two-Dimensional Test Problem
2. Gradient Descent Optimization With Adam
3. Visualization of Adam
:
Gradient Descent
Gradient descent is an optimization algorithm.

It is technically referred to as a first-order optimization algorithm as it explicitly makes use of


the first-order derivative of the target objective function.

First-order methods rely on gradient information to help direct the search for a minimum

— Page 69, Algorithms for Optimization, 2019.

The first-order derivative, or simply the “derivative,” is the rate of change or slope of the
target function at a specific point, e.g. for a specific input.

If the target function takes multiple input variables, it is referred to as a multivariate function
and the input variables can be thought of as a vector. In turn, the derivative of a multivariate
target function may also be taken as a vector and is referred to generally as the gradient.

Gradient: First-order derivative for a multivariate objective function.

The derivative or the gradient points in the direction of the steepest ascent of the target
function for a specific input.

Gradient descent refers to a minimization optimization algorithm that follows the negative of
the gradient downhill of the target function to locate the minimum of the function.

The gradient descent algorithm requires a target function that is being optimized and the
derivative function for the objective function. The target function f() returns a score for a given
set of inputs, and the derivative function f'() gives the derivative of the target function for a
given set of inputs.

The gradient descent algorithm requires a starting point (x) in the problem, such as a
randomly selected point in the input space.

The derivative is then calculated and a step is taken in the input space that is expected to
result in a downhill movement in the target function, assuming we are minimizing the target
function.

A downhill movement is made by first calculating how far to move in the input space,
calculated as the step size (called alpha or the learning rate) multiplied by the gradient. This is
then subtracted from the current point, ensuring we move against the gradient, or down the
target function.
:
x(t) = x(t-1) – step_size * f'(x(t-1))

The steeper the objective function at a given point, the larger the magnitude of the gradient
and, in turn, the larger the step taken in the search space. The size of the step taken is scaled
using a step size hyperparameter.

Step Size (alpha): Hyperparameter that controls how far to move in the search space
against the gradient each iteration of the algorithm.

If the step size is too small, the movement in the search space will be small and the search
will take a long time. If the step size is too large, the search may bounce around the search
space and skip over the optima.

Now that we are familiar with the gradient descent optimization algorithm, let’s take a look at
the Adam algorithm.

Want to Get Started With Optimization Algorithms?


Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Start your FREE Mini-Course now!

Adam Optimization Algorithm


Adaptive Movement Estimation algorithm, or Adam for short, is an extension to the gradient
descent optimization algorithm.

The algorithm was described in the 2014 paper by Diederik Kingma and Jimmy Lei Ba titled
“Adam: A Method for Stochastic Optimization.”

Adam is designed to accelerate the optimization process, e.g. decrease the number of
function evaluations required to reach the optima, or to improve the capability of the
optimization algorithm, e.g. result in a better final result.

This is achieved by calculating a step size for each input parameter that is being optimized.
Importantly, each step size is automatically adapted throughput the search process based on
:
the gradients (partial derivatives) encountered for each variable.

We propose Adam, a method for efficient stochastic optimization that only requires
# first-order gradients with little memory requirement. The method computes
individual adaptive learning rates for different parameters from estimates of first
and second moments of the gradients; the name Adam is derived from adaptive
moment estimation

— Adam: A Method for Stochastic Optimization

This involves maintaining a first and second moment of the gradient, e.g. an exponentially
decaying mean gradient (first moment) and variance (second moment) for each input variable.

The moving averages themselves are estimates of the 1st moment (the mean) and
# the 2nd raw moment (the uncentered variance) of the gradient.

— Adam: A Method for Stochastic Optimization

Let’s step through each element of the algorithm.

First, we must maintain a moment vector and exponentially weighted infinity norm for each
parameter being optimized as part of the search, referred to as m and v (really the Greek
letter nu) respectively. They are initialized to 0.0 at the start of the search.

m=0
v=0

The algorithm is executed iteratively over time t starting at t=1, and each iteration involves
calculating a new set of parameter values x, e.g. going from x(t-1) to x(t).

It is perhaps easy to understand the algorithm if we focus on updating one parameter, which
generalizes to updating all parameters via vector operations.

First, the gradient (partial derivatives) are calculated for the current time step.

g(t) = f'(x(t-1))

Next, the first moment is updated using the gradient and a hyperparameter beta1.

m(t) = beta1 * m(t-1) + (1 – beta1) * g(t)

Then the second moment is updated using the squared gradient and a hyperparameter
:
beta2.

v(t) = beta2 * v(t-1) + (1 – beta2) * g(t)^2

The first and second moments are biased because they are initialized with zero values.

… these moving averages are initialized as (vectors of) 0’s, leading to moment
# estimates that are biased towards zero, especially during the initial timesteps, and
especially when the decay rates are small (i.e. the betas are close to 1). The good
news is that this initialization bias can be easily counteracted, resulting in bias-
corrected estimates …

— Adam: A Method for Stochastic Optimization

Next the first and second moments are bias-corrected, starring with the first moment:

mhat(t) = m(t) / (1 – beta1(t))

And then the second moment:

vhat(t) = v(t) / (1 – beta2(t))

Note, beta1(t) and beta2(t) refer to the beta1 and beta2 hyperparameters that are decayed on
a schedule over the iterations of the algorithm. A static decay schedule can be used,
although the paper recommend the following:

beta1(t) = beta1^t
beta2(t) = beta2^t

Finally, we can calculate the value for the parameter for this iteration.

x(t) = x(t-1) – alpha * mhat(t) / (sqrt(vhat(t)) + eps)

Where alpha is the step size hyperparameter, eps is a small value (epsilon) such as 1e-8 that
ensures we do not encounter a divide by zero error, and sqrt() is the square root function.

Note, a more efficient reordering of the update rule listed in the paper can be used:

alpha(t) = alpha * sqrt(1 – beta2(t)) / (1 – beta1(t))


x(t) = x(t-1) – alpha(t) * m(t) / (sqrt(v(t)) + eps)

To review, there are three hyperparameters for the algorithm, they are:

alpha: Initial step size (learning rate), a typical value is 0.001.


:
beta1: Decay factor for first momentum, a typical value is 0.9.
beta2: Decay factor for infinity norm, a typical value is 0.999.

And that’s it.

For full derivation of the Adam algorithm in the context of the Adam algorithm, I recommend
reading the paper.

Adam: A Method for Stochastic Optimization

Next, let’s look at how we might implement the algorithm from scratch in Python.

Gradient Descent With Adam


In this section, we will explore how to implement the gradient descent optimization algorithm
with Adam.

Two-Dimensional Test Problem


First, let’s define an optimization function.

We will use a simple two-dimensional function that squares the input of each dimension and
define the range of valid inputs from -1.0 to 1.0.

The objective() function below implements this function

1 # objective function
2 def objective(x, y):
3 return x**2.0 + y**2.0

We can create a three-dimensional plot of the dataset to get a feeling for the curvature of the
response surface.

The complete example of plotting the objective function is listed below.

1 # 3d plot of the test function


2 from numpy import arange
3 from numpy import meshgrid
4 from matplotlib import pyplot
5
6 # objective function
7 def objective(x, y):
8 return x**2.0 + y**2.0
9
10 # define range for input
11 r_min, r_max = -1.0, 1.0
12 # sample input range uniformly at 0.1 increments
13 xaxis = arange(r_min, r_max, 0.1)
14 yaxis = arange(r_min, r_max, 0.1)
:
15 # create a mesh from the axis
16 x, y = meshgrid(xaxis, yaxis)
17 # compute targets
18 results = objective(x, y)
19 # create a surface plot with the jet color scheme
20 figure = pyplot.figure()
21 axis = figure.gca(projection='3d')
22 axis.plot_surface(x, y, results, cmap='jet')
23 # show the plot
24 pyplot.show()

Running the example creates a three-dimensional surface plot of the objective function.

We can see the familiar bowl shape with the global minima at f(0, 0) = 0.

Three-Dimensional Plot of the Test Objective Function

Three-Dimensional Plot of the Test Objective Function

We can also create a two-dimensional plot of the function. This will be helpful later when we
want to plot the progress of the search.

The example below creates a contour plot of the objective function.

1 # contour plot of the test function


2 from numpy import asarray
:
3 from numpy import arange
4 from numpy import meshgrid
5 from matplotlib import pyplot
6
7 # objective function
8 def objective(x, y):
9 return x**2.0 + y**2.0
10
11 # define range for input
12 bounds = asarray([[-1.0, 1.0], [-1.0, 1.0]])
13 # sample input range uniformly at 0.1 increments
14 xaxis = arange(bounds[0,0], bounds[0,1], 0.1)
15 yaxis = arange(bounds[1,0], bounds[1,1], 0.1)
16 # create a mesh from the axis
17 x, y = meshgrid(xaxis, yaxis)
18 # compute targets
19 results = objective(x, y)
20 # create a filled contour plot with 50 levels and jet color scheme
21 pyplot.contourf(x, y, results, levels=50, cmap='jet')
22 # show the plot
23 pyplot.show()

Running the example creates a two-dimensional contour plot of the objective function.

We can see the bowl shape compressed to contours shown with a color gradient. We will use
this plot to plot the specific points explored during the progress of the search.
:
Two-Dimensional Contour Plot of the Test Objective Function

Two-Dimensional Contour Plot of the Test Objective Function

Now that we have a test objective function, let’s look at how we might implement the Adam
optimization algorithm.

Gradient Descent Optimization With Adam


We can apply the gradient descent with Adam to the test problem.

First, we need a function that calculates the derivative for this function.

f(x) = x^2
f'(x) = x * 2

The derivative of x^2 is x * 2 in each dimension. The derivative() function implements this
below.

1 # derivative of objective function


2 def derivative(x, y):
3 return asarray([x * 2.0, y * 2.0])

Next, we can implement gradient descent optimization.


:
First, we can select a random point in the bounds of the problem as a starting point for the
search.

This assumes we have an array that defines the bounds of the search with one row for each
dimension and the first column defines the minimum and the second column defines the
maximum of the dimension.

1 ...
2 # generate an initial point
3 x = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
4 score = objective(x[0], x[1])

Next, we need to initialize the first and second moments to zero.

1 ...
2 # initialize first and second moments
3 m = [0.0 for _ in range(bounds.shape[0])]
4 v = [0.0 for _ in range(bounds.shape[0])]

We then run a fixed number of iterations of the algorithm defined by the “n_iter”
hyperparameter.

1 ...
2 # run iterations of gradient descent
3 for t in range(n_iter):
4 ...

The first step is to calculate the gradient for the current solution using the derivative()
function.

1 ...
2 # calculate gradient
3 gradient = derivative(solution[0], solution[1])

The first step is to calculate the derivative for the current set of parameters.

1 ...
2 # calculate gradient g(t)
3 g = derivative(x[0], x[1])

Next, we need to perform the Adam update calculations. We will perform these calculations
one variable at a time using an imperative programming style for readability.

In practice, I recommend using NumPy vector operations for efficiency.

1 ...
2 # build a solution one variable at a time
3 for i in range(x.shape[0]):
4 ...

First, we need to calculate the moment.


:
1 ...
2 # m(t) = beta1 * m(t-1) + (1 - beta1) * g(t)
3 m[i] = beta1 * m[i] + (1.0 - beta1) * g[i]

Then the second moment.

1 ...
2 # v(t) = beta2 * v(t-1) + (1 - beta2) * g(t)^2
3 v[i] = beta2 * v[i] + (1.0 - beta2) * g[i]**2

Then the bias correction for the first and second moments.

1 ...
2 # mhat(t) = m(t) / (1 - beta1(t))
3 mhat = m[i] / (1.0 - beta1**(t+1))
4 # vhat(t) = v(t) / (1 - beta2(t))
5 vhat = v[i] / (1.0 - beta2**(t+1))

Then finally the updated variable value.

1 ...
2 # x(t) = x(t-1) - alpha * mhat(t) / (sqrt(vhat(t)) + eps)
3 x[i] = x[i] - alpha * mhat / (sqrt(vhat) + eps)

This is then repeated for each parameter that is being optimized.

At the end of the iteration we can evaluate the new parameter values and report the
performance of the search.

1 ...
2 # evaluate candidate point
3 score = objective(x[0], x[1])
4 # report progress
5 print('>%d f(%s) = %.5f' % (t, x, score))

We can tie all of this together into a function named adam() that takes the names of the
objective and derivative functions as well as the algorithm hyperparameters, and returns the
best solution found at the end of the search and its evaluation.

This complete function is listed below.

1 # gradient descent algorithm with adam


2 def adam(objective, derivative, bounds, n_iter, alpha, beta1, beta2, eps=1e-8):
3 # generate an initial point
4 x = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
5 score = objective(x[0], x[1])
6 # initialize first and second moments
7 m = [0.0 for _ in range(bounds.shape[0])]
8 v = [0.0 for _ in range(bounds.shape[0])]
9 # run the gradient descent updates
10 for t in range(n_iter):
11 # calculate gradient g(t)
12 g = derivative(x[0], x[1])
13 # build a solution one variable at a time
:
14 for i in range(x.shape[0]):
15 # m(t) = beta1 * m(t-1) + (1 - beta1) * g(t)
16 m[i] = beta1 * m[i] + (1.0 - beta1) * g[i]
17 # v(t) = beta2 * v(t-1) + (1 - beta2) * g(t)^2
18 v[i] = beta2 * v[i] + (1.0 - beta2) * g[i]**2
19 # mhat(t) = m(t) / (1 - beta1(t))
20 mhat = m[i] / (1.0 - beta1**(t+1))
21 # vhat(t) = v(t) / (1 - beta2(t))
22 vhat = v[i] / (1.0 - beta2**(t+1))
23 # x(t) = x(t-1) - alpha * mhat(t) / (sqrt(vhat(t)) + eps)
24 x[i] = x[i] - alpha * mhat / (sqrt(vhat) + eps)
25 # evaluate candidate point
26 score = objective(x[0], x[1])
27 # report progress
28 print('>%d f(%s) = %.5f' % (t, x, score))
29 return [x, score]

Note: we have intentionally used lists and imperative coding style instead of vectorized
operations for readability. Feel free to adapt the implementation to a vectorized
implementation with NumPy arrays for better performance.

We can then define our hyperparameters and call the adam() function to optimize our test
objective function.

In this case, we will use 60 iterations of the algorithm with an initial steps size of 0.02 and
beta1 and beta2 values of 0.8 and 0.999 respectively. These hyperparameter values were
found after a little trial and error.

1 ...
2 # seed the pseudo random number generator
3 seed(1)
4 # define range for input
5 bounds = asarray([[-1.0, 1.0], [-1.0, 1.0]])
6 # define the total iterations
7 n_iter = 60
8 # steps size
9 alpha = 0.02
10 # factor for average gradient
11 beta1 = 0.8
12 # factor for average squared gradient
13 beta2 = 0.999
14 # perform the gradient descent search with adam
15 best, score = adam(objective, derivative, bounds, n_iter, alpha, beta1, beta2)
16 print('Done!')
17 print('f(%s) = %f' % (best, score))

Tying all of this together, the complete example of gradient descent optimization with Adam is
listed below.

1 # gradient descent optimization with adam for a two-dimensional test function


2 from math import sqrt
3 from numpy import asarray
4 from numpy.random import rand
5 from numpy.random import seed
6
:
7 # objective function
8 def objective(x, y):
9 return x**2.0 + y**2.0
10
11 # derivative of objective function
12 def derivative(x, y):
13 return asarray([x * 2.0, y * 2.0])
14
15 # gradient descent algorithm with adam
16 def adam(objective, derivative, bounds, n_iter, alpha, beta1, beta2, eps=1e-8):
17 # generate an initial point
18 x = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
19 score = objective(x[0], x[1])
20 # initialize first and second moments
21 m = [0.0 for _ in range(bounds.shape[0])]
22 v = [0.0 for _ in range(bounds.shape[0])]
23 # run the gradient descent updates
24 for t in range(n_iter):
25 # calculate gradient g(t)
26 g = derivative(x[0], x[1])
27 # build a solution one variable at a time
28 for i in range(x.shape[0]):
29 # m(t) = beta1 * m(t-1) + (1 - beta1) * g(t)
30 m[i] = beta1 * m[i] + (1.0 - beta1) * g[i]
31 # v(t) = beta2 * v(t-1) + (1 - beta2) * g(t)^2
32 v[i] = beta2 * v[i] + (1.0 - beta2) * g[i]**2
33 # mhat(t) = m(t) / (1 - beta1(t))
34 mhat = m[i] / (1.0 - beta1**(t+1))
35 # vhat(t) = v(t) / (1 - beta2(t))
36 vhat = v[i] / (1.0 - beta2**(t+1))
37 # x(t) = x(t-1) - alpha * mhat(t) / (sqrt(vhat(t)) + eps)
38 x[i] = x[i] - alpha * mhat / (sqrt(vhat) + eps)
39 # evaluate candidate point
40 score = objective(x[0], x[1])
41 # report progress
42 print('>%d f(%s) = %.5f' % (t, x, score))
43 return [x, score]
44
45 # seed the pseudo random number generator
46 seed(1)
47 # define range for input
48 bounds = asarray([[-1.0, 1.0], [-1.0, 1.0]])
49 # define the total iterations
50 n_iter = 60
51 # steps size
52 alpha = 0.02
53 # factor for average gradient
54 beta1 = 0.8
55 # factor for average squared gradient
56 beta2 = 0.999
57 # perform the gradient descent search with adam
58 best, score = adam(objective, derivative, bounds, n_iter, alpha, beta1, beta2)
59 print('Done!')
60 print('f(%s) = %f' % (best, score))

Running the example applies the Adam optimization algorithm to our test problem and
reports the performance of the search for each iteration of the algorithm.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation
:
procedure, or differences in numerical precision. Consider running the example a few times
and compare the average outcome.

In this case, we can see that a near-optimal solution was found after perhaps 53 iterations of
the search, with input values near 0.0 and 0.0, evaluating to 0.0.

1 ...
2 >50 f([-0.00056912 -0.00321961]) = 0.00001
3 >51 f([-0.00052452 -0.00286514]) = 0.00001
4 >52 f([-0.00043908 -0.00251304]) = 0.00001
5 >53 f([-0.0003283 -0.00217044]) = 0.00000
6 >54 f([-0.00020731 -0.00184302]) = 0.00000
7 >55 f([-8.95352320e-05 -1.53514076e-03]) = 0.00000
8 >56 f([ 1.43050285e-05 -1.25002847e-03]) = 0.00000
9 >57 f([ 9.67123406e-05 -9.89850279e-04]) = 0.00000
10 >58 f([ 0.00015359 -0.00075587]) = 0.00000
11 >59 f([ 0.00018407 -0.00054858]) = 0.00000
12 Done!
13 f([ 0.00018407 -0.00054858]) = 0.000000

Visualization of Adam
We can plot the progress of the Adam search on a contour plot of the domain.

This can provide an intuition for the progress of the search over the iterations of the
algorithm.

We must update the adam() function to maintain a list of all solutions found during the search,
then return this list at the end of the search.

The updated version of the function with these changes is listed below.

1 # gradient descent algorithm with adam


2 def adam(objective, derivative, bounds, n_iter, alpha, beta1, beta2, eps=1e-8):
3 solutions = list()
4 # generate an initial point
5 x = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
6 score = objective(x[0], x[1])
7 # initialize first and second moments
8 m = [0.0 for _ in range(bounds.shape[0])]
9 v = [0.0 for _ in range(bounds.shape[0])]
10 # run the gradient descent updates
11 for t in range(n_iter):
12 # calculate gradient g(t)
13 g = derivative(x[0], x[1])
14 # build a solution one variable at a time
15 for i in range(bounds.shape[0]):
16 # m(t) = beta1 * m(t-1) + (1 - beta1) * g(t)
17 m[i] = beta1 * m[i] + (1.0 - beta1) * g[i]
18 # v(t) = beta2 * v(t-1) + (1 - beta2) * g(t)^2
19 v[i] = beta2 * v[i] + (1.0 - beta2) * g[i]**2
20 # mhat(t) = m(t) / (1 - beta1(t))
21 mhat = m[i] / (1.0 - beta1**(t+1))
:
22 # vhat(t) = v(t) / (1 - beta2(t))
23 vhat = v[i] / (1.0 - beta2**(t+1))
24 # x(t) = x(t-1) - alpha * mhat(t) / (sqrt(vhat(t)) + ep)
25 x[i] = x[i] - alpha * mhat / (sqrt(vhat) + eps)
26 # evaluate candidate point
27 score = objective(x[0], x[1])
28 # keep track of solutions
29 solutions.append(x.copy())
30 # report progress
31 print('>%d f(%s) = %.5f' % (t, x, score))
32 return solutions

We can then execute the search as before, and this time retrieve the list of solutions instead
of the best final solution.

1 ...
2 # seed the pseudo random number generator
3 seed(1)
4 # define range for input
5 bounds = asarray([[-1.0, 1.0], [-1.0, 1.0]])
6 # define the total iterations
7 n_iter = 60
8 # steps size
9 alpha = 0.02
10 # factor for average gradient
11 beta1 = 0.8
12 # factor for average squared gradient
13 beta2 = 0.999
14 # perform the gradient descent search with adam
15 solutions = adam(objective, derivative, bounds, n_iter, alpha, beta1, beta2)

We can then create a contour plot of the objective function, as before.

1 ...
2 # sample input range uniformly at 0.1 increments
3 xaxis = arange(bounds[0,0], bounds[0,1], 0.1)
4 yaxis = arange(bounds[1,0], bounds[1,1], 0.1)
5 # create a mesh from the axis
6 x, y = meshgrid(xaxis, yaxis)
7 # compute targets
8 results = objective(x, y)
9 # create a filled contour plot with 50 levels and jet color scheme
10 pyplot.contourf(x, y, results, levels=50, cmap='jet')

Finally, we can plot each solution found during the search as a white dot connected by a line.

1 ...
2 # plot the sample as black circles
3 solutions = asarray(solutions)
4 pyplot.plot(solutions[:, 0], solutions[:, 1], '.-', color='w')

Tying this all together, the complete example of performing the Adam optimization on the test
problem and plotting the results on a contour plot is listed below.

1 # example of plotting the adam search on a contour plot of the test function
2 from math import sqrt
3 from numpy import asarray
:
4 from numpy import arange
5 from numpy.random import rand
6 from numpy.random import seed
7 from numpy import meshgrid
8 from matplotlib import pyplot
9 from mpl_toolkits.mplot3d import Axes3D
10
11 # objective function
12 def objective(x, y):
13 return x**2.0 + y**2.0
14
15 # derivative of objective function
16 def derivative(x, y):
17 return asarray([x * 2.0, y * 2.0])
18
19 # gradient descent algorithm with adam
20 def adam(objective, derivative, bounds, n_iter, alpha, beta1, beta2, eps=1e-8):
21 solutions = list()
22 # generate an initial point
23 x = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
24 score = objective(x[0], x[1])
25 # initialize first and second moments
26 m = [0.0 for _ in range(bounds.shape[0])]
27 v = [0.0 for _ in range(bounds.shape[0])]
28 # run the gradient descent updates
29 for t in range(n_iter):
30 # calculate gradient g(t)
31 g = derivative(x[0], x[1])
32 # build a solution one variable at a time
33 for i in range(bounds.shape[0]):
34 # m(t) = beta1 * m(t-1) + (1 - beta1) * g(t)
35 m[i] = beta1 * m[i] + (1.0 - beta1) * g[i]
36 # v(t) = beta2 * v(t-1) + (1 - beta2) * g(t)^2
37 v[i] = beta2 * v[i] + (1.0 - beta2) * g[i]**2
38 # mhat(t) = m(t) / (1 - beta1(t))
39 mhat = m[i] / (1.0 - beta1**(t+1))
40 # vhat(t) = v(t) / (1 - beta2(t))
41 vhat = v[i] / (1.0 - beta2**(t+1))
42 # x(t) = x(t-1) - alpha * mhat(t) / (sqrt(vhat(t)) + ep)
43 x[i] = x[i] - alpha * mhat / (sqrt(vhat) + eps)
44 # evaluate candidate point
45 score = objective(x[0], x[1])
46 # keep track of solutions
47 solutions.append(x.copy())
48 # report progress
49 print('>%d f(%s) = %.5f' % (t, x, score))
50 return solutions
51
52 # seed the pseudo random number generator
53 seed(1)
54 # define range for input
55 bounds = asarray([[-1.0, 1.0], [-1.0, 1.0]])
56 # define the total iterations
57 n_iter = 60
58 # steps size
59 alpha = 0.02
60 # factor for average gradient
61 beta1 = 0.8
62 # factor for average squared gradient
63 beta2 = 0.999
:
64 # perform the gradient descent search with adam
65 solutions = adam(objective, derivative, bounds, n_iter, alpha, beta1, beta2)
66 # sample input range uniformly at 0.1 increments
67 xaxis = arange(bounds[0,0], bounds[0,1], 0.1)
68 yaxis = arange(bounds[1,0], bounds[1,1], 0.1)
69 # create a mesh from the axis
70 x, y = meshgrid(xaxis, yaxis)
71 # compute targets
72 results = objective(x, y)
73 # create a filled contour plot with 50 levels and jet color scheme
74 pyplot.contourf(x, y, results, levels=50, cmap='jet')
75 # plot the sample as black circles
76 solutions = asarray(solutions)
77 pyplot.plot(solutions[:, 0], solutions[:, 1], '.-', color='w')
78 # show the plot
79 pyplot.show()

Running the example performs the search as before, except in this case, a contour plot of the
objective function is created.

In this case, we can see that a white dot is shown for each solution found during the search,
starting above the optima and progressively getting closer to the optima at the center of the
plot.

Contour Plot of the Test Objective Function With Adam Search Results Shown

Contour Plot of the Test Objective Function With Adam Search Results Shown
:
Further Reading
This section provides more resources on the topic if you are looking to go deeper.

Papers
Adam: A Method for Stochastic Optimization, 2014.

Books
Algorithms for Optimization, 2019.
Deep Learning, 2016.

APIs
numpy.random.rand API.
numpy.asarray API.
Matplotlib API.

Articles
Gradient descent, Wikipedia.
Stochastic gradient descent, Wikipedia.
An overview of gradient descent optimization algorithms, 2016.

Summary
In this tutorial, you discovered how to develop gradient descent with Adam optimization
algorithm from scratch.

Specifically, you learned:

Gradient descent is an optimization algorithm that uses the gradient of the objective
function to navigate the search space.
Gradient descent can be updated to use an automatically adaptive step size for each
input variable using a decaying average of partial derivatives, called Adam.
How to implement the Adam optimization algorithm from scratch and apply it to an
objective function and evaluate the results.

Do you have any questions?


Ask your questions in the comments below and I will do my best to answer.
:
Get a Handle on Modern Optimization Algorithms!
Develop Your Understanding of Optimization
...with just a few lines of python code

Discover how in my new Ebook:


Optimization for Machine Learning

It provides self-study tutorials with full working code on:


Gradient Descent, Genetic Algorithms, Hill Climbing, Curve Fitting,
RMSProp, Adam, and much more...

Bring Modern Optimization Algorithms to


Your Machine Learning Projects

SEE WHAT'S INSIDE

Tweet Tweet Share Share

More On This Topic

Gentle Introduction to the Adam Optimization…

Why Optimization Is Important in Machine Learning


:
How to Implement Bayesian Optimization from Scratch…

Gradient Descent Optimization With AMSGrad From Scratch

Gradient Descent Optimization With Nadam From Scratch

Gradient Descent Optimization With AdaMax From Scratch

About Jason Brownlee


Jason Brownlee, PhD is a machine learning specialist who teaches developers how to
get results with modern machine learning methods via hands-on tutorials.
View all posts by Jason Brownlee →

$ 3 Books on Optimization for Machine Learning Visualization for Function Optimization in Python %
:
28 Responses to Code Adam Optimization Algorithm From Scratch

REPLY &
Rick Qiu January 13, 2021 at 2:10 pm #

It is even better if you could use a difficult objective function to test the Adam
implementation.

REPLY &
Jason Brownlee January 14, 2021 at 6:11 am #

Great suggestion!

I wanted to focus on the algorithm in this case.

REPLY &
M. Shobana January 14, 2021 at 4:09 am #

Is it possible to implement this optimization technique in antennna design

REPLY &
Jason Brownlee January 14, 2021 at 6:15 am #

Maybe.

Perhaps try it and see.

REPLY &
Cindrella January 14, 2021 at 10:18 pm #

If it’s possible to use emotion classification

REPLY &
Jason Brownlee January 15, 2021 at 5:57 am #

No, you would use a neural network for that task. Adam could be used to train
your neural network.
:
REPLY &
auraham January 15, 2021 at 5:16 am #

Great article, Jason. I wonder if you discuss about this and other optimization
algorithms in any of your books.

REPLY &
Jason Brownlee January 15, 2021 at 5:59 am #

Thanks!

I hope to write a book on the topic of optimization soon.

REPLY &
Dhanya January 15, 2021 at 5:06 pm #

Thank you so much..

REPLY &
Jason Brownlee January 16, 2021 at 6:53 am #

You’re welcome.

REPLY &
Elie January 16, 2021 at 2:04 am #

Hi Jason, the following equation is wrong.

x(t) = x(t-1) – alpha * mhat(t) / sqrt(vhat(t)) + eps

Due to the precedence of division over addition, it should read:

x(t) = x(t-1) – alpha * mhat(t) / (sqrt(vhat(t)) + eps)

REPLY &
Jason Brownlee January 16, 2021 at 6:56 am #

Thanks, agreed!

Lucky it was just the comment – the code was correct.


:
REPLY &
AndyLucny January 22, 2021 at 1:07 am #

I would recommend the following fix:


# sample input range uniformly at 0.1 increments
xaxis = arange(bounds[0,0], bounds[0,1]+0.1, 0.1)
yaxis = arange(bounds[1,0], bounds[1,1]+0.1, 0.1)
# equalize axis
pyplot.axis(‘equal’)

REPLY &
Jason Brownlee January 22, 2021 at 7:23 am #

Thanks for the tip!

REPLY &
Colin Jenkins March 18, 2021 at 9:23 am #

Hi Jason,

Do you have an example of Adam with mini-batching. For example would you calculate the 2
moment vectors for each sample and then average them (like the gradients) before the end-
of-batch weight update. Or maybe treat each batch as one iteration WRT t. Or something
else…?

REPLY &
Jason Brownlee March 19, 2021 at 6:11 am #

I don’t think so.

Off the cuff, I believe you sum/average the terms across multiple samples.

REPLY &
Novato March 29, 2021 at 8:44 am #

Hey Jason,

Thanks for the nice article. I was wondering how you would proceed when there is no
analytical solution for the derivative of the objective function? As is the case for many neural
networks. How does Adam unfold in this case?

Thanks again.
:
REPLY &
Jason Brownlee March 30, 2021 at 5:53 am #

GD requires a gradient.

If no such gradient is available, you can use a different algorithm that does not require a
gradient, so-called direct search algorithms (nelder mead and friends), or even stochastic
algorithms (simulated annealing, genetic algorithms and friends).

REPLY &
vipul April 23, 2021 at 7:45 pm #

How to use adam optimization to design an FIR filter using objective function-

J2 = ∑ abs [abs (| Η (ω) | -1) -δp] + ∑ [abs (-δs)]

in Matlab,plz help or give some references

REPLY &
Jason Brownlee April 24, 2021 at 5:19 am #

I don’t have any matlab examples sorry.

REPLY &
Nishant Gaurav August 7, 2021 at 11:19 am #

Hey Jason,
Which book of yours has all the optimization algorithms explained as above?

REPLY &
Jason Brownlee August 8, 2021 at 5:07 am #

There’s no optimization book yet, hopefully soon.

REPLY &
Eitan Mizrahi October 17, 2021 at 7:29 am #

I can use ADAM optimization for adjusting weights.


Each weight in in iteration is adjusted.

There may be an error that is the different between the expected output and the calculated
:
output (which must use an input and a weight for calculating).

I must know the error, because I need to adjust properly (in gradient I adjust by the error
delta).
I don’t understand how the error delta is corrected for ADAM optimization algorithm. Maybe
this is missed by the example.

REPLY &
Adrian Tam October 20, 2021 at 9:02 am #

If I understand correctly, the error correction you mentioned is g(t) in the formula.

REPLY &
Marc January 14, 2022 at 9:37 am #

I think it’s not \beta (t) but \beta ^t in \hat{m}

REPLY &
James Carmichael February 20, 2022 at 12:58 pm #

Thank you for the feedback Marc! You are correct.

REPLY &
Marc January 14, 2022 at 9:37 am #

I think it’s not \beta (t) but \beta ^t in \hat{m}.

REPLY &
James Carmichael January 15, 2022 at 11:31 am #

Thank you for the feedback Marc!

Leave a Reply
:
Name (required)

Email (will not be published) (required)

SUBMIT COMMENT

Welcome!
I'm Jason Brownlee PhD
and I help developers get results with machine learning.
Read more

Never miss a tutorial:

Picked for you:

Simple Genetic Algorithm From Scratch in Python

Curve Fitting With Python

Simulated Annealing From Scratch in Python

How to Choose an Optimization Algorithm

Differential Evolution from Scratch in Python


:
Loving the Tutorials?

The Optimization for Machine Learning EBook


is where you'll find the Really Good stuff.

>> SEE WHAT'S INSIDE

© 2022 Machine Learning Mastery. All Rights Reserved.


LinkedIn | Twitter | Facebook | Newsletter | RSS

Privacy | Disclaimer | Terms | Contact | Sitemap | Search


:

You might also like