Docsity

Automatic Differentiation: Lecture Notes on Calculating Derivatives using AD Techniques - , Study notes of Mathematics

Toggle sidebar
bg1
Derivatives...
Automatic Differentiation
Numerical Optimization
Lecture Notes #17
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-7720
http://terminus.sdsu.edu/
Fall 2009
Peter Blomgren, h[email protected]iAutomatic Differentiation — (1/24)
Derivatives...
Automatic Differentiation
Outline
1Derivatives...
Recap — Finite Differences
Automatic Differentiation
2Automatic Differentiation
Extended Example
Comments, Extensions, and Limitations
Peter Blomgren, h[email protected]iAutomatic Differentiation — (2/24)
Derivatives...
Automatic Differentiation
Recap — Finite Differences
Automatic 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 the
analytical expressions for the gradient and Hessian can be provided
it is the way to go!
Next, we look at Automatic Differentiation Techniques.
Peter Blomgren, h[email protected]iAutomatic Differentiation — (3/24)
Derivatives...
Automatic Differentiation
Recap — Finite Differences
Automatic 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 computer
programs with derivative computations. It exploits the fact that every
computer program, no matter how complicated, executes a sequence of
elementary arithmetic operations such as additions or elementary
functions such as exp(). By applying the chain rule of derivative calculus
repeatedly to these operations, derivatives of arbitrary order can be
computed automatically, and accurate to working precision.”
The same site provides links and references to usable tools for AD
for Fortran 77, Fortran 90 and ANSI-C and C++.
We will here take a very brief look at AD. Even more references
and pointers can be found at http://www.AutoDiff.org/
Peter Blomgren, h[email protected]iAutomatic Differentiation — (4/24)
pf3
pf4
pf5

Partial preview of the text

Download Automatic Differentiation: Lecture Notes on Calculating Derivatives using AD Techniques - and more Study notes Mathematics in PDF only on Docsity!

Derivatives...

Automatic Differentiation

Numerical Optimization

Lecture Notes #

Calculating Derivatives — Automatic Differentiation

Peter Blomgren,

[email protected]

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,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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

R

m

R

, and

˜y

R

n

R

m

, then for

˜x

R

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,

〈

[email protected]

〉

Automatic Differentiation

— (5/24)

Derivatives...

Automatic Differentiation

Extended ExampleComments, Extensions, and Limitations

Automatic Differentiation — Example

1 of 12

We consider a function

f

R

3

R

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,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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

R

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

R

n

associated with

x

i

D

˜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

D

˜p

x

9

f

x

T

˜p

Note:

D

˜p

x

1

p

1

D

˜p

x

2

p

2

, and

D

˜p

x

3

p

3

˜p

is known as a

seed vector

Peter Blomgren,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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

  • 2

π

,

x

1

+=

∂

f

∂

x

4

∂

x

4

∂

x

1

=

x

4

·

2 =

4

e

2

  • 4

π

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,

〈

[email protected]

〉

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,

〈

[email protected]

〉

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,

F

R

n

R

m

so that the

Jacobian matrix

of first derivatives

J

x

[

F

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

F

Peter Blomgren,

〈

[email protected]

〉

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

D

˜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

D

˜ 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

H

ij

entry of the Hessian matrix.

Peter Blomgren,

〈

[email protected]

〉

Automatic Differentiation

— (20/24)