



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Derivatives...
Automatic Differentiation
Lecture Notes #
Calculating Derivatives — Automatic Differentiation
Peter Blomgren,
Department of Mathematics and Statistics
Dynamical Systems Group
Computational Sciences Research Center
San Diego State University San Diego, CA 92182-
http://terminus.sdsu.edu/
Fall 2009
Peter Blomgren,
〈
〉
Automatic Differentiation
— (1/24)
Derivatives...
Automatic Differentiation
Outline
1
Derivatives...
Recap — Finite Differences Automatic Differentiation
2
Automatic Differentiation
Extended Example Comments, Extensions, and Limitations
Peter Blomgren,
〈
〉
Automatic Differentiation
— (2/24)
Derivatives...
Automatic Differentiation
Recap — Finite DifferencesAutomatic Differentiation
Derivatives Needed!!! — Continued
So far, we looked at using
finite difference
methods for
computing good numerical estimates for “missing” derivatives.The finite difference approach comes at the price of —
(i)
introducing
some
numerical
error
in
the
derivative
expressions
(ii)
a need for extra evaluations of the function (objective)and/or the gradient.
Finite difference approximations work very well, but if theanalytical expressions for the gradient and Hessian can be providedit is the way to go!Next, we look at
Automatic Differentiation Techniques.
Peter Blomgren,
〈
〉
Automatic Differentiation
— (3/24)
Derivatives...
Automatic Differentiation
Recap — Finite DifferencesAutomatic Differentiation
Automatic Differentiation
Math/CS White Magic
http://www-unix.mcs.anl.gov/autodiff/AD
Tools/
says the following
about automatic differentiation: “Automatic differentiation (AD) is a technique for augmenting computerprograms with derivative computations. It exploits the fact that everycomputer program, no matter how complicated, executes a sequence ofelementary arithmetic operations such as additions or elementaryfunctions such as exp(). By applying the chain rule of derivative calculusrepeatedly to these operations, derivatives of arbitrary order can becomputed automatically, and accurate to working precision.” The same site provides links and references to usable tools for ADfor Fortran 77, Fortran 90 and ANSI-C and C++.We will here take a very brief look at AD. Even more referencesand pointers can be found at
http://www.AutoDiff.org/
Peter Blomgren,
〈
〉
Automatic Differentiation
— (4/24)
Derivatives...
Automatic Differentiation
Recap — Finite DifferencesAutomatic Differentiation
The Chain Rule — Forgotten(?) Calculus
The
Chain Rule
in its full vector glory (gory?
) takes the form
The Chain Rule If
h
m
, and
˜y
n
m
, then for
˜x
n
we can write
˜x
h
˜y
˜x
m
i
=
h
y
i
y
i
x
Automatic differentiation is essentially applying the chain rule
at
the code level
. — There are two
modes
of AD, the
forward
and
the
reverse
mode. We follow the example from Nocedal-Wright.
Peter Blomgren,
〈
〉
Automatic Differentiation
— (5/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
Automatic Differentiation — Example
1 of 12
We consider a function
f
3
f
˜x
x
1
x
2
sin(
x
3
e
x
1
x
2
x
3
This function can be evaluated in several different ways.Certain rules (restrictions) apply,
e.g.
the multiplication of
x
1
x
2
must take place before the exponentiation
e
x
1
x
2
. We illustrate one
possibility with a (partially ordered)
computational graph
and
several
intermediate variables.
Some
graph theoretical
language:
node#i
is the
parent
of
node#j
(and hence
node#j
the
child
of
node#i
) if there is a
directed arc from
i
to
j
. A node can be evaluated when all its
parents are known, so the computation flows left-to-right (see nextslide). This is known as a
forward sweep
Peter Blomgren,
〈
〉
Automatic Differentiation
— (6/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
Automatic Differentiation — Example
2 of 12
x1 x2 x
x4 x
x6 x
x
x
sin
exp
+
/
Figure:
The computational graph associates with the function
f
(
˜x
) = (
x
1
x
2
sin(
x
3
) +
e
x
1
x
2
)
/
x
3
We have introduced the intermediate variables
x
4
=
x
1
∗
x
2
,
x
5
= sin(
x
3
),
x
6
=
e
x
4
,
x
7
=
x
4
∗
x
5
,
x
8
=
x
6
x
7
,
and
x
9
=
x
8
/
x
3
.
Note:
In AD, the
software
not the user identifies (either explicitly or implicitly) the
intermediate steps.
Peter Blomgren,
〈
〉
Automatic Differentiation
— (7/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
AD — Example / Forward Mode
3 of 12
In forward mode, a
directional derivative
, in the direction
˜p
n
, of each intermediate value
x
i
is evaluated and carried
forward simultaneously with the evaluation of
x
i
itself.
Notation The directional derivative for
˜p
n
associated with
x
i
˜p
x
i
def
x
i
T
˜p
n
j
=
x
i
x
j
p
j
i
In our example
n
i
9, and our goal is to evaluate
˜p
x
9
f
x
T
˜p
Note:
˜p
x
1
p
1
˜p
x
2
p
2
, and
˜p
x
3
p
3
˜p
is known as a
seed vector
Peter Blomgren,
〈
〉
Automatic Differentiation
— (8/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
AD — Example / Reverse Mode
8 of 12
2
1
exp(2)
2
sin
exp
+
/
1
pi/
2
2+exp(2)
(4+2exp(2))/pi
Figure: Forward sweep
, the computational graph at level 4; — Sweep complete.
After the forward sweep is complete we initialize the adjoint variables, in particular x
9
= 1, and node#9 is finalized (it has no children).
Peter Blomgren,
〈
〉
Automatic Differentiation
— (13/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
AD — Example / Reverse Mode
9 of 12
The reverse sweep
x
1
= 0
,
x
2
= 0
,
x
3
= 0
,
x
4
= 0
,
x
5
= 0
,
x
6
= 0
,
x
7
= 0
,
x
8
= 0
,
x
9
=
1
Node#9 is the child of nodes #3 and#8, we update the associated adjoint vari-ables, finalizing node#8. (
x
9
=
x
8
/
x
3
)
2
1
exp(2)
2
sin
exp
+
/
1
pi/
2
2+exp(2)
(4+2exp(2))/pi
x
3
+=
∂
f
∂
x
9
∂
x
9
∂
x
3
=
−
2 +
e
2
(
π/
2
=
−
8
−
4
e
2
π
2
,
x
8
+=
∂
f
∂
x
9
∂
x
9
∂
x
8
=
1
π/
2
=
2 π
8 <:
x
1
= 0
,
x
2
= 0
,
x
3
=
−
8
−
4
e
2
π
2
,
x
4
= 0
,
x
5
= 0
,
x
6
= 0
,
x
7
= 0
x
8
=
2 π
,
x
9
=
1
9 =;
Peter Blomgren,
〈
〉
Automatic Differentiation
— (14/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
AD — Example / Reverse Mode
10 of 12
8 <:
x
1
= 0
,
x
2
= 0
,
x
3
=
−
8
−
4
e
2
π
2
,
x
4
= 0
,
x
5
= 0
,
x
6
= 0
,
x
7
= 0
x
8
=
2 π
,
x
9
=
1
9 =;
We now update the parents of #8 — #6and #7, finalizing both. (
x
8
=
x
6
x
7
).
2
1
exp(2)
2
sin
exp
+
/
1
pi/
2
2+exp(2)
(4+2exp(2))/pi
x
6
+=
∂
f
∂
x
8
∂
x
8
∂
x
6
=
x
8
·
1 =
2 π
,
x
7
+=
∂
f
∂
x
8
∂
x
8
∂
x
7
=
x
8
·
1 =
2 π
8 >><>>:
x
1
= 0
,
x
2
= 0
,
x
3
=
−
8
−
4
e
2
π
2
,
x
4
= 0
,
x
5
= 0
x
6
=
2 π
,
x
7
=
2 π
,
x
8
=
2 π
,
x
9
=
1
9 >>=>>;
Peter Blomgren,
〈
〉
Automatic Differentiation
— (15/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
AD — Example / Reverse Mode
11 of 12
Next, we use #6 and #7, to fi-nalize #4 and #5.
x
6
e
x
4
x
7
x
4
x
5
2
1
exp(2)
2
sin
exp
+
/
1
pi/
2
2+exp(2)
(4+2exp(2))/pi
x
4
f
x
6
x
6
x
4
f
x
7
x
7
x
4
x
6
e
2
x
7
e
2
π
x
5
f
x
7
x
7
x
5
x
7
π
8 >><>>:
x
1
= 0
,
x
2
= 0
,
x
3
=
−
8
−
4
e
2
π
2
x
4
=
2e
2
2
π
,
x
5
=
4 π
,
x
6
=
2 π
,
x
7
=
2 π
,
x
8
=
2 π
,
x
9
=
1
9 >>=>>;
Peter Blomgren,
〈
〉
Automatic Differentiation
— (16/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
AD — Example / Reverse Mode
12 of 12
We now have the final piecesto complete the reverse sweep.(
x
4
x
1
x
2
x
5
= sin(
x
3
2
1
exp(2)
2
sin
exp
+
/
1
pi/
2
2+exp(2)
(4+2exp(2))/pi
x
3
f
x
5
x
5
x
3
x
5
cos(
π/
x
2
+=
∂
f
∂
x
4
∂
x
4
∂
x
2
=
x
4
·
1 =
2
e
2
π
,
x
1
+=
∂
f
∂
x
4
∂
x
4
∂
x
1
=
x
4
·
2 =
4
e
2
π
8 >>><>>>:
x
1
=
4e
2
4
π
,
x
2
=
2e
2
2
π
,
x
3
=
−
8
−
4e
2
π
2
x
4
=
2e
2
2
π
,
x
5
=
4 π
,
x
6
=
2 π
,
x
7
=
2 π
,
x
8
=
2 π
,
x
9
=
1
9 >>>=>>>;
We have
∇
f
(
˜x
)
˛˛
˜x
=[
,
2
,π/
2]
T
= [
x
1
,
x
2
,
x
3
]
.
Peter Blomgren,
〈
〉
Automatic Differentiation
— (17/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
Reverse Mode Automatic Differentiation: Comments
Main appeal of AD/RM:
low computational complexity for
scalar functions. Extra arithmetic is at most 4–5 times the “pure”function evaluation. Main drawback of AD/RM:
the entire computational graph
must be stored for the reverse sweep. Implementation and accesspatterns are quite straight-forward, but storage can be a problem:—
If each node can be stored in 20 bytes, then a function thatrequires one second of evaluation on a 100 megaflop computermay generate a graph which requires 2 Gb of store!
The storage requirement can be reduced at the cost of extraarithmetics by partial forward and reverse sweeps; re-evaluatingportions instead of storing the whole graph. This is known as checkpointing
Peter Blomgren,
〈
〉
Automatic Differentiation
— (18/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
Extending AD to Vector Functions
The idea of automatic differentiation is quite easily extended tovector valued functions,
n
m
so that the
Jacobian matrix
of first derivatives
x
j
x
i
j
= 1
,
2
,... ,
m
i
= 1
,
2
,... ,
n
can be formed using automatic differentiation.All we need is additional bookkeeping for the
m
components of
Peter Blomgren,
〈
〉
Automatic Differentiation
— (19/24)
Derivatives...
Automatic Differentiation
Extended ExampleComments, Extensions, and Limitations
Extending AD to the Hessian (
nd
Derivatives)
Fwd. Mode
The technique can also be adapted to evaluate the Hessian. In forward mode
we need
2 seed vectors ˜
p
and
˜q
, and we define
˜p˜
q
x
i
˜p
T
2
x
i
˜q
these 2nd derivative quantities are propagated (using the chainrule) just like the 1st derivatives were propagated in our previousexample.At the end of the sweep the terminal node contains the value of
˜ p˜
q
x
n
˜p
T
2
x
i
˜q
˜p
T
2
f
˜x
q
Each pair (
˜p
˜q
e
i
˜e
j
) gives the
ij
entry of the Hessian matrix.
Peter Blomgren,
〈
〉
Automatic Differentiation
— (20/24)