Newton Methods for Neural Networks: Gauss Newton Matrix-vector - - PowerPoint PPT Presentation

newton methods for neural networks gauss newton matrix
SMART_READER_LITE
LIVE PREVIEW

Newton Methods for Neural Networks: Gauss Newton Matrix-vector - - PowerPoint PPT Presentation

Newton Methods for Neural Networks: Gauss Newton Matrix-vector Product Chih-Jen Lin National Taiwan University Last updated: June 1, 2020 Chih-Jen Lin (National Taiwan Univ.) 1 / 81 Outline Backward setting 1 Jacobian evaluation


slide-1
SLIDE 1

Newton Methods for Neural Networks: Gauss Newton Matrix-vector Product

Chih-Jen Lin

National Taiwan University Last updated: June 1, 2020

Chih-Jen Lin (National Taiwan Univ.) 1 / 81

slide-2
SLIDE 2

Outline

1

Backward setting Jacobian evaluation Gauss-Newton Matrix-vector products

2

Forward + backward settings R operator Gauss-Newton matrix-vector product

Chih-Jen Lin (National Taiwan Univ.) 2 / 81

slide-3
SLIDE 3

Backward setting

Outline

1

Backward setting Jacobian evaluation Gauss-Newton Matrix-vector products

2

Forward + backward settings R operator Gauss-Newton matrix-vector product

Chih-Jen Lin (National Taiwan Univ.) 3 / 81

slide-4
SLIDE 4

Backward setting Jacobian evaluation

Outline

1

Backward setting Jacobian evaluation Gauss-Newton Matrix-vector products

2

Forward + backward settings R operator Gauss-Newton matrix-vector product

Chih-Jen Lin (National Taiwan Univ.) 4 / 81

slide-5
SLIDE 5

Backward setting Jacobian evaluation

Jacobian Evaluation: Convolutional Layer I

For an instance i the Jacobian can be partitioned into L blocks according to layers Ji =

  • J1,i J2,i . . . JL,i

, m = 1, . . . , L, (1) where Jm,i =

  • ∂③L+1,i

∂vec(W m)T ∂③L+1,i ∂(❜m)T

  • .

The calculation seems to be very similar to that for the gradient.

Chih-Jen Lin (National Taiwan Univ.) 5 / 81

slide-6
SLIDE 6

Backward setting Jacobian evaluation

Jacobian Evaluation: Convolutional Layer II

For the convolutional layers, recall for gradient we have ∂f ∂W m = 1 C W m + 1 l

l

  • i=1

∂ξi ∂W m and ∂ξi ∂vec(W m)T = vec ∂ξi ∂Sm,i φ(pad(Z m,i))T T

Chih-Jen Lin (National Taiwan Univ.) 6 / 81

slide-7
SLIDE 7

Backward setting Jacobian evaluation

Jacobian Evaluation: Convolutional Layer III

Now we have ∂③L+1,i ∂vec(W m)T =    

∂zL+1,i

1

∂vec(W m)T

. . .

∂zL+1,i

nL+1

∂vec(W m)T

    =     vec(∂zL+1,i

1

∂Sm,i φ(pad(Z m,i))T)T

. . . vec(

∂zL+1,i

nL+1

∂Sm,i φ(pad(Z m,i))T)T

   

Chih-Jen Lin (National Taiwan Univ.) 7 / 81

slide-8
SLIDE 8

Backward setting Jacobian evaluation

Jacobian Evaluation: Convolutional Layer IV

If ❜m is considered, the result is

  • ∂③L+1,i

∂vec(W m)T ∂③L+1,i ∂(❜m)T

  • =

      vec

  • ∂zL+1,i

1

∂Sm,i

  • φ(pad(Z m,i))T 1am

convbm conv

T . . . vec

  • ∂zL+1,i

nL+1

∂Sm,i

  • φ(pad(Z m,i))T 1am

convbm conv

T       .

Chih-Jen Lin (National Taiwan Univ.) 8 / 81

slide-9
SLIDE 9

Backward setting Jacobian evaluation

Jacobian Evaluation: Convolutional Layer V

We can see that it’s more complicated than gradient. Gradient is a vector but Jacobian is a matrix

Chih-Jen Lin (National Taiwan Univ.) 9 / 81

slide-10
SLIDE 10

Backward setting Jacobian evaluation

Jacobian Evaluation: Backward Process I

For gradient, earlier we need a backward process to calculate ∂ξi ∂Sm,i Now what we need are ∂zL+1,i

1

∂Sm,i , . . . , ∂zL+1,i

nL+1

∂Sm,i The process is similar

Chih-Jen Lin (National Taiwan Univ.) 10 / 81

slide-11
SLIDE 11

Backward setting Jacobian evaluation

Jacobian Evaluation: Backward Process II

If with RELU activation function and max pooling, for gradient we had ∂ξi ∂vec(Sm,i)T =

  • ∂ξi

∂vec(Z m+1,i)T ⊙ vec(I[Z m+1,i])T

  • Pm,i

pool.

Chih-Jen Lin (National Taiwan Univ.) 11 / 81

slide-12
SLIDE 12

Backward setting Jacobian evaluation

Jacobian Evaluation: Backward Process III

Assume that ∂③L+1,i ∂vec(Z m+1,i) are available. ∂zL+1,i

j

∂vec(Sm,i)T =

  • ∂zL+1,i

j

∂vec(Z m+1,i)T ⊙ vec(I[Z m+1,i])T

  • Pm,i

pool,

j = 1, . . . , nL+1.

Chih-Jen Lin (National Taiwan Univ.) 12 / 81

slide-13
SLIDE 13

Backward setting Jacobian evaluation

Jacobian Evaluation: Backward Process IV

These row vectors can be written together as a matrix ∂③L+1,i ∂vec(Sm,i)T =

  • ∂③L+1,i

∂vec(Z m+1,i)T ⊙

  • 1nL+1vec(I[Z m+1,i])T
  • Pm,i

pool.

Chih-Jen Lin (National Taiwan Univ.) 13 / 81

slide-14
SLIDE 14

Backward setting Jacobian evaluation

Jacobian Evaluation: Backward Process V

For gradient, we use ∂ξi ∂Sm,i to have ∂ξi ∂vec(Z m,i)T = vec

  • (W m)T ∂ξi

∂Sm,i T Pm

φ Pm pad

and pass it to the previous layer

Chih-Jen Lin (National Taiwan Univ.) 14 / 81

slide-15
SLIDE 15

Backward setting Jacobian evaluation

Jacobian Evaluation: Backward Process VI

Now we need to generate ∂③L+1,i ∂vec(Z m,i)T and pass it to the previous layer. Now we have ∂③L+1,i ∂vec(Z m,i)T =       vec

  • (W m)T ∂zL+1,i

1

∂Sm,i

T Pm

φ Pm pad

. . . vec

  • (W m)T ∂zL+1,i

nL+1

∂Sm,i

T Pm

φ Pm pad

      .

Chih-Jen Lin (National Taiwan Univ.) 15 / 81

slide-16
SLIDE 16

Backward setting Jacobian evaluation

Jacobian Evaluation: Fully-connected Layer I

We do not discuss details, but list all results below ∂③L+1,i ∂vec(W m)T =

  • vec
  • ∂zL+1,i

1

∂sm,i (③m,i)T

  • . . .

vec

  • ∂zL+1,i

nL+1

∂sm,i (③m,i)T T

Chih-Jen Lin (National Taiwan Univ.) 16 / 81

slide-17
SLIDE 17

Backward setting Jacobian evaluation

Jacobian Evaluation: Fully-connected Layer II

∂③L+1,i ∂(❜m)T = ∂③L+1,i ∂(sm,i)T , ∂③L+1,i ∂(sm,i)T = ∂③L+1,i ∂(③m+1,i)T ⊙

  • 1nL+1I[③m+1,i]T

∂③L+1,i ∂(③m,i)T = ∂③L+1,i ∂(sm,i)T W m

Chih-Jen Lin (National Taiwan Univ.) 17 / 81

slide-18
SLIDE 18

Backward setting Jacobian evaluation

Jacobian Evaluation: Fully-connected Layer III

For layer L + 1, if using the squared loss and the linear activation function, we have ∂③L+1,i ∂(sL,i)T = InL+1.

Chih-Jen Lin (National Taiwan Univ.) 18 / 81

slide-19
SLIDE 19

Backward setting Jacobian evaluation

Gradient versus Jacobian I

Operations for gradient ∂ξi ∂vec(Sm,i)T =

  • ∂ξi

∂vec(Z m+1,i)T ⊙ vec(I[Z m+1,i])T

  • Pm,i

pool.

∂ξi ∂W m = ∂ξi ∂Sm,i φ(pad(Z m,i))T ∂ξi ∂vec(Z m,i)T = vec

  • (W m)T ∂ξi

∂Sm,i T Pm

φ Pm pad,

Chih-Jen Lin (National Taiwan Univ.) 19 / 81

slide-20
SLIDE 20

Backward setting Jacobian evaluation

Gradient versus Jacobian II

For Jacobian we have ∂③L+1,i ∂vec(Sm,i)T =

  • ∂③L+1,i

∂vec(Z m+1,i)T ⊙

  • 1nL+1vec(I[Z m+1,i])T
  • Pm,i

pool.

∂③L+1,i ∂vec(W m)T =     vec(∂zL+1,i

1

∂Sm,i φ(pad(Z m,i))T)T

. . . vec(

∂zL+1,i

nL+1

∂Sm,i φ(pad(Z m,i))T)T

   

Chih-Jen Lin (National Taiwan Univ.) 20 / 81

slide-21
SLIDE 21

Backward setting Jacobian evaluation

Gradient versus Jacobian III

∂③L+1,i ∂vec(Z m,i)T =       vec

  • (W m)T ∂zL+1,i

1

∂Sm,i

T Pm

φ Pm pad

. . . vec

  • (W m)T ∂zL+1,i

nL+1

∂Sm,i

T Pm

φ Pm pad

      .

Chih-Jen Lin (National Taiwan Univ.) 21 / 81

slide-22
SLIDE 22

Backward setting Jacobian evaluation

Implementation I

For gradient we did ∆ ← mat(vec(∆)TPm,i

pool)

∂ξi ∂W m = ∆ · φ(pad(Z m,i))T ∆ ← vec

  • (W m)T∆

T Pm

φ Pm pad

∆ ← ∆ ⊙ I[Z m,i] Now for Jacobian we have similar settings but there are some differences

Chih-Jen Lin (National Taiwan Univ.) 22 / 81

slide-23
SLIDE 23

Backward setting Jacobian evaluation

Implementation II

We don’t really store the Jacobian: ∂③L+1,i ∂vec(W m)T =     vec(∂zL+1,i

1

∂Sm,i φ(pad(Z m,i))T)T

. . . vec(

∂zL+1,i

nL+1

∂Sm,i φ(pad(Z m,i))T)T

    Recall Jacobian is used for matrix-vector products G S✈ = 1 C ✈ + 1 |S|

  • i∈S
  • (Ji)T

Bi(Ji✈)

  • (2)

Chih-Jen Lin (National Taiwan Univ.) 23 / 81

slide-24
SLIDE 24

Backward setting Jacobian evaluation

Implementation III

The form ∂③L+1,i ∂vec(W m)T =     vec(∂zL+1,i

1

∂Sm,i φ(pad(Z m,i))T)T

. . . vec(

∂zL+1,i

nL+1

∂Sm,i φ(pad(Z m,i))T)T

    is like the product of two things

Chih-Jen Lin (National Taiwan Univ.) 24 / 81

slide-25
SLIDE 25

Backward setting Jacobian evaluation

Implementation IV

If we have ∂zL+1,i

1

∂Sm,i , . . . , ∂zL+1,i

nL+1

∂Sm,i , and φ(pad(Z m,i)) probably we can do the matrix-vector product without multiplying these two things out We will talk about this again later Thus our Jacobian evaluation is solely on obtaining ∂zL+1,i

1

∂Sm,i , . . . , ∂zL+1,i

nL+1

∂Sm,i

Chih-Jen Lin (National Taiwan Univ.) 25 / 81

slide-26
SLIDE 26

Backward setting Jacobian evaluation

Implementation V

Further we need to take all data (or data in the selected subset) into account In the end what we have is the following procedure In the beginning ∆ ∈ Rdm+1am+1bm+1×nL+1×l This corresponds to ∂③L+1,i ∂vec(Z m+1,i)T ⊙

  • 1nL+1vec(I[Z m+1,i])T

, ∀i = 1, . . . , l

Chih-Jen Lin (National Taiwan Univ.) 26 / 81

slide-27
SLIDE 27

Backward setting Jacobian evaluation

Implementation VI

We then calculate ∆ ← mat       (Pm,1

pool)Tvec(∆:,:,1)

. . . (Pm,l

pool)Tvec(∆:,:,l)

     

dm+1×am

convbm convnL+1l

Recall that the pooling matrices are different across instances

Chih-Jen Lin (National Taiwan Univ.) 27 / 81

slide-28
SLIDE 28

Backward setting Jacobian evaluation

Implementation VII

The above operation corresponds to ∂③L+1,i ∂vec(Sm,i)T =

  • ∂③L+1,i

∂vec(Z m+1,i)T ⊙

  • 1nL+1vec(I[Z m+1,i])T
  • Pm,i

pool.

Now we get

  • ∂zL+1,1

1

∂Sm,1

. . .

∂zL+1,1

nL+1

∂Sm,1

. . .

∂zL+1,l

nL+1

∂Sm,l

  • ∈ Rdm+1×am

convbm convnL+1l

Chih-Jen Lin (National Taiwan Univ.) 28 / 81

slide-29
SLIDE 29

Backward setting Jacobian evaluation

Implementation VIII

Next V ← vec((W m)T∆) ∈ Rhhdmam

convbm convnL+1l×1

This is same as vec

  • (W m)T

∂zL+1,1

1

∂Sm,1

. . .

∂zL+1,1

nL+1

∂Sm,1

. . .

∂zL+1,l

nL+1

∂Sm,l

  • .

Chih-Jen Lin (National Taiwan Univ.) 29 / 81

slide-30
SLIDE 30

Backward setting Jacobian evaluation

Implementation IX

Now V is a big vector like        ✈ 1

1

. . . ✈ 1

nL+1

. . . ✈ l

nL+1

       Note that “✈” here is not the vector in matrix-vector products. We happen to use the same symbol

Chih-Jen Lin (National Taiwan Univ.) 30 / 81

slide-31
SLIDE 31

Backward setting Jacobian evaluation

Implementation X

We then calculate ∆ ← mat               (✈ 1

1)TPm φ Pm pad

. . . (✈ 1

nL+1)TPm φ Pm pad

. . . (✈ l

nL+1)TPm φ Pm pad

             

dmambm×nL+1×l

This corresponds to ∂③L+1,i ∂vec(Z m,i)T , i = 1, . . . , l

Chih-Jen Lin (National Taiwan Univ.) 31 / 81

slide-32
SLIDE 32

Backward setting Jacobian evaluation

Implementation XI

Finally, ∆ ←∆⊙

  • I[Z m,1] · · · I[Z m,1]
  • nL+1

· · · I[Z m,l] · · · I[Z m,l]

  • nL+1
  • (3)

This means ∂③L+1,i ∂vec(Z m,i)T ⊙

  • 1nL+1vec(I[Z m,i])T

, ∀i = 1, . . . , l Let’s check the code

Chih-Jen Lin (National Taiwan Univ.) 32 / 81

slide-33
SLIDE 33

Backward setting Jacobian evaluation

Implementation XII

dzdS{m} = vTP(model, net, m, num_data, dzdS{m}, ’pool_Jacobian’); dzdS{m} = reshape(dzdS{m}, model.ch_input(m+1), []); V = model.weight{m}’ * dzdS{m}; dzdS{m-1} = vTP(model, net, m, num_data, V, ’phi_Jacobian’); % vTP_pad

Chih-Jen Lin (National Taiwan Univ.) 33 / 81

slide-34
SLIDE 34

Backward setting Jacobian evaluation

Implementation XIII

dzdS{m-1} = reshape(dzdS{m-1}, model.ch_input(m), model.ht_pad(m), model.wd_pad(m), []); p = model.wd_pad_added(m); dzdS{m-1} = dzdS{m-1}(:, p+1:p+model.ht_input(m), p+1:p+model.wd_input(m), :); dzdS{m-1} = reshape(dzdS{m-1}, [], nL, num_data) .* reshape(net.Z{m} > 0, [], 1, num_data);

Chih-Jen Lin (National Taiwan Univ.) 34 / 81

slide-35
SLIDE 35

Backward setting Jacobian evaluation

Implementation XIV

In the last line for doing (3), we don’t need to repeat each I[Z m,i] nL+1 times. For .*, MATLAB does the expansion automatically

Chih-Jen Lin (National Taiwan Univ.) 35 / 81

slide-36
SLIDE 36

Backward setting Jacobian evaluation

Discussion I

For doing several CG steps, we should store ∂zL+1,i

1

∂Sm,i , . . . , ∂zL+1,i

nL+1

∂Sm,i The memory cost is l ×nL+1× Lc

  • m=1

dm+1am

convbm conv + L

  • m=Lc+1

nm+1

  • (4)

It is proportional to Number of classes

Chih-Jen Lin (National Taiwan Univ.) 36 / 81

slide-37
SLIDE 37

Backward setting Jacobian evaluation

Discussion II

Number of data for the subsampled Hessian The reason is that it’s used for all CG steps (Jacobian matrix remains the same) Recalculating them at each CG is too expensive We will show some complexity analysis later Thus subsequently we will consider a different approach to reduce the memory consumption

Chih-Jen Lin (National Taiwan Univ.) 37 / 81

slide-38
SLIDE 38

Backward setting Gauss-Newton Matrix-vector products

Outline

1

Backward setting Jacobian evaluation Gauss-Newton Matrix-vector products

2

Forward + backward settings R operator Gauss-Newton matrix-vector product

Chih-Jen Lin (National Taiwan Univ.) 38 / 81

slide-39
SLIDE 39

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products I

We check G✈ though the situation of using G S (i.e., a subset of data) is the same The Gauss-Newton matrix G = 1 C I + 1 l

l

  • i=1

  (J1,i)T . . . (JL,i)T   Bi J1,i . . . JL,i

Chih-Jen Lin (National Taiwan Univ.) 39 / 81

slide-40
SLIDE 40

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products II

The Gauss-Newton matrix vector product G✈ = 1 C ✈ + 1 l

l

  • i=1

  (J1,i)T . . . (JL,i)T   Bi J1,i . . . JL,i   ✈ 1 . . . ✈ L   = 1 C ✈ + 1 l

l

  • i=1

  (J1,i)T . . . (JL,i)T  

  • Bi

L

  • m=1

Jm,i✈ m

  • ,

(5)

Chih-Jen Lin (National Taiwan Univ.) 40 / 81

slide-41
SLIDE 41

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products III

where ✈ =   ✈ 1 . . . ✈ L   Each ✈ m, m = 1, . . . , L has the same length as the number of variables (including bias) at the mth layer.

Chih-Jen Lin (National Taiwan Univ.) 41 / 81

slide-42
SLIDE 42

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products IV

For the convolutional layers, Jm,i✈ m =       vec

  • ∂zL+1,i

1

∂Sm,i

  • φ(pad(Z m,i))T 1am

convbm conv

T ✈ m . . . vec

  • ∂zL+1,i

nL+1

∂Sm,i

  • φ(pad(Z m,i))T 1am

convbm conv

T ✈ m       ∈ RnL+1×1 This formulation is fine, but we need

Chih-Jen Lin (National Taiwan Univ.) 42 / 81

slide-43
SLIDE 43

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products V

a for loop to generate nL+1 vectors the product between a matrix and a vector ✈ m Is there a way to avoid a for loop? For a language like MATLAB/Octave, we hope to avoid for loops Also we hope the code can be simpler and shorter We use the following property vec(AB)Tvec(C) = vec(A)Tvec(CBT)

Chih-Jen Lin (National Taiwan Univ.) 43 / 81

slide-44
SLIDE 44

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products VI

The first element is vec   ∂zL+1,i

1

∂Sm,i

A

  • φ(pad(Z m,i))T 1am

convbm conv

  • B

  

T

✈ m

  • vec(C)

= ∂zL+1,i

1

∂vec(Sm,i)T × vec

  • mat(✈ m)dm+1×(hmhmdm+1)

φ(pad(Z m,i)) 1T

am

convbm conv

  • .

Chih-Jen Lin (National Taiwan Univ.) 44 / 81

slide-45
SLIDE 45

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products VII

If all elements are considered together Jm,i✈ m = ∂③L+1,i ∂vec(Sm,i)T × vec

  • mat(✈ m)dm+1×(hmhmdm+1)

φ(pad(Z m,i)) 1T

am

convbm conv

  • .

(6) This involves One matrix-matrix product

Chih-Jen Lin (National Taiwan Univ.) 45 / 81

slide-46
SLIDE 46

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products VIII

One matrix-vector product After deriving (6), from (5), we sum results of all layers

L

  • m=1

Jm,i✈ m Next we calculate qi = Bi(

L

  • m=1

Jm,i✈ m). (7)

Chih-Jen Lin (National Taiwan Univ.) 46 / 81

slide-47
SLIDE 47

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products IX

This is usually easy We mentioned earlier that if the squared loss is used Bi =   2 . . . 2   is a diagonal matrix

Chih-Jen Lin (National Taiwan Univ.) 47 / 81

slide-48
SLIDE 48

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products X

Finally, we calculate (Jm,i)Tqi =

  • vec
  • ∂zL+1,i

1

∂Sm,i

  • φ(pad(Z m,i))T 1am

convbm conv

  • · · ·

vec

  • ∂zL+1,i

nL+1

∂Sm,i

  • φ(pad(Z m,i))T 1am

convbm conv

  • qi

Chih-Jen Lin (National Taiwan Univ.) 48 / 81

slide-49
SLIDE 49

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products XI

=

nL+1

  • j=1

qi

jvec

  • ∂zL+1,i

j

∂Sm,i

  • φ(pad(Z m,i))T 1am

convbm conv

  • = vec

 

nL+1

  • j=1

qi

j

  • ∂zL+1,i

j

∂Sm,i

  • φ(pad(Z m,i))T 1am

convbm conv

 = vec    

nL+1

  • j=1

qi

j

∂zL+1,i

j

∂Sm,i   φ(pad(Z m,i))T 1am

convbm conv

Chih-Jen Lin (National Taiwan Univ.) 49 / 81

slide-50
SLIDE 50

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products XII

= vec

  • mat
  • ∂③L+1,i

∂vec(Sm,i)T T qi

  • dm+1×am

convbm conv

×

  • φ(pad(Z m,i))T 1am

convbm conv

  • .

(8) A matrix-vector product and then a matrix-matrix product

Chih-Jen Lin (National Taiwan Univ.) 50 / 81

slide-51
SLIDE 51

Backward setting Gauss-Newton Matrix-vector products

Gauss-Newton Matrix-Vector Products XIII

Similar to the results of the convolutional layers, for the fully-connected layers we have Jm,i✈ m = ∂③L+1,i ∂(sm,i)T mat(✈ m)nm+1×(nm+1)

  • ③m,i

11

  • .

(Jm,i)Tqi = vec ∂③L+1,i ∂(sm,i)T T qi (③m,i)T 11

  • .

Chih-Jen Lin (National Taiwan Univ.) 51 / 81

slide-52
SLIDE 52

Backward setting Gauss-Newton Matrix-vector products

Implementation I

As before, we must handle all instances together We discuss only    L

m=1 Jm,1✈ m

. . . L

m=1 Jm,l✈ m

   ∈ RnL+1l×1 Following earlier derivation

Chih-Jen Lin (National Taiwan Univ.) 52 / 81

slide-53
SLIDE 53

Backward setting Gauss-Newton Matrix-vector products

Implementation II

  Jm,1✈ m . . . Jm,l✈ m   =       

∂③L+1,1 ∂vec(Sm,1)T vec

  • mat(✈ m)

φ(pad(Z m,1)) 1T

am

convbm conv

  • .

. .

∂③L+1,l ∂vec(Sm,l)T vec

  • mat(✈ m)

φ(pad(Z m,l)) 1T

am

convbm conv

      =   

∂③L+1,1 ∂vec(Sm,1)T ♣m,1

. . .

∂③L+1,l ∂vec(Sm,l)T ♣m,l

   ,

Chih-Jen Lin (National Taiwan Univ.) 53 / 81

slide-54
SLIDE 54

Backward setting Gauss-Newton Matrix-vector products

Implementation III

We have mat(✈ m) ∈ Rdm+1×(hmhmdm+1) and ♣m,i = vec

  • mat(✈ m)

φ(pad(Z m,i)) 1T

am

convbm conv

  • .

(9)

Chih-Jen Lin (National Taiwan Univ.) 54 / 81

slide-55
SLIDE 55

Backward setting Gauss-Newton Matrix-vector products

Implementation IV

All ♣m,i, i = 1, . . . , l can be calculated by a matrix-matrix product mat(✈ m) φ(pad(Z m,1)) · · · φ(pad(Z m,l)) 1T

am

convbm conv

· · · 1T

am

convbm conv

  • ∈ Rdm+1×am

convbm convl;

Chih-Jen Lin (National Taiwan Univ.) 55 / 81

slide-56
SLIDE 56

Backward setting Gauss-Newton Matrix-vector products

Implementation V

To get   

∂③L+1,1 ∂vec(Sm,1)T ♣m,1

. . .

∂③L+1,l ∂vec(Sm,l)T ♣m,l

   , we need l matrix-vector products There is no good way to transform it to matrix-matrix operations

Chih-Jen Lin (National Taiwan Univ.) 56 / 81

slide-57
SLIDE 57

Backward setting Gauss-Newton Matrix-vector products

Implementation VI

At this moment we calculate Jm,i✈ m = ∂③L+1,i ∂vec(Sm,i)T ♣m,i, i = 1, . . . , l. (10) by summing up all rows of the following matrix

  • ∂zL+1,i

1

∂vec(Sm,i) · · · ∂zL+1,i

nL+1

∂vec(Sm,i)

  • dm+1am

convbm conv×nL+1

  • ♣m,i · · · ♣m,i

dm+1am

convbm conv×nL+1 .

and extend this to cover all instances together

Chih-Jen Lin (National Taiwan Univ.) 57 / 81

slide-58
SLIDE 58

Backward setting Gauss-Newton Matrix-vector products

Implementation VII

The code (convolutional layers) is like for m = LC : -1 : 1 var_range = var_ptr(m) : var_ptr(m+1) - 1; ab = model.ht_conv(m)*model.wd_conv(m); d = model.ch_input(m+1); p = reshape(v(var_range), d, []) * [net.phiZ{m}; ones(1, ab*num_data)]; p = sum(reshape(net.dzdS{m}, d*ab, nL, []) .* reshape(p, d*ab, 1, []),1);

Chih-Jen Lin (National Taiwan Univ.) 58 / 81

slide-59
SLIDE 59

Backward setting Gauss-Newton Matrix-vector products

Implementation VIII

Jv = Jv + p(:); end

Chih-Jen Lin (National Taiwan Univ.) 59 / 81

slide-60
SLIDE 60

Forward + backward settings

Outline

1

Backward setting Jacobian evaluation Gauss-Newton Matrix-vector products

2

Forward + backward settings R operator Gauss-Newton matrix-vector product

Chih-Jen Lin (National Taiwan Univ.) 60 / 81

slide-61
SLIDE 61

Forward + backward settings R operator

Outline

1

Backward setting Jacobian evaluation Gauss-Newton Matrix-vector products

2

Forward + backward settings R operator Gauss-Newton matrix-vector product

Chih-Jen Lin (National Taiwan Univ.) 61 / 81

slide-62
SLIDE 62

Forward + backward settings R operator

Reverse versus Forward Autodiff I

We mentioned before that two types of autodiff are forward and reverse modes For the Jacobian evaluation, at layer m, Jm,i =

  • ∂③L+1,i

∂vec(W m)T ∂③L+1,i ∂(❜m)T

  • ,

naturally we follow the gradient calculation to use the reverse mode But this may not be a good decision We will show a solution of using the forward mode

Chih-Jen Lin (National Taiwan Univ.) 62 / 81

slide-63
SLIDE 63

Forward + backward settings R operator

R Operator I

Consider g(θ) ∈ Rk×1. Following Pearlmutter (1994), we define R✈{g(θ)} ≡ ∂g(θ) ∂θT ✈ =   ∇g1(θ)T✈ . . . ∇gk(θ)T✈   . (11) Note that   ∇g1(θ)T . . . ∇gk(θ)T   is the Jacobian of g(θ)

Chih-Jen Lin (National Taiwan Univ.) 63 / 81

slide-64
SLIDE 64

Forward + backward settings R operator

R Operator II

This definition can be extended to a matrix M(θ) ∈ Rk×t by R✈{M(θ)} ≡ mat (R✈{vec(M(θ))})k×t =mat ∂vec(M(θ)) ∂θT ✈

  • k×t

=   ∇MT

11✈

· · · ∇MT

1t✈

. . . ... . . . ∇MT

k1✈

· · · ∇MT

kt✈

  Clearly, R✈{M(θ)} =

  • R✈{M(θ)T}

T . (12)

Chih-Jen Lin (National Taiwan Univ.) 64 / 81

slide-65
SLIDE 65

Forward + backward settings R operator

R Operator III

If h(·) is a scalar function, we let h(M(θ)) =   h(M11) · · · h(M1t) . . . ... . . . h(Mk1) · · · h(Mkt)   and h′(M(θ)) =   h′(M11) · · · h′(M1t) . . . ... . . . h′(Mk1) · · · h′(Mkt)   .

Chih-Jen Lin (National Taiwan Univ.) 65 / 81

slide-66
SLIDE 66

Forward + backward settings R operator

R Operator IV

Because ∇(h(Mij(θ)))T✈ = h′(Mij)∇(Mij)T✈, we have R✈{h(M(θ))} = h′(M(θ)) ⊙ R✈{M(θ)}, (13) where ⊙ stands for the Hadamard product. If M(θ) and T(θ) have the same size, R✈{M(θ) + T(θ)} = R✈{M(θ)} + R✈{T(θ)}. (14)

Chih-Jen Lin (National Taiwan Univ.) 66 / 81

slide-67
SLIDE 67

Forward + backward settings R operator

R Operator V

Lastly, we have R✈{U(θ)M(θ)} = R✈{U(θ)}M(θ)+U(θ)R✈{M(θ)} (15) Proof: Note that (R{U(θ)M(θ)})ij = ∇ ((U(θ)M(θ))ij)T ✈. (16) With (U(θ)M(θ))ij =

m

  • p=1

UipMpj, (17)

Chih-Jen Lin (National Taiwan Univ.) 67 / 81

slide-68
SLIDE 68

Forward + backward settings R operator

R Operator VI

we have both Uip ∈ R1 and Mpj ∈ R1. Then, ∇ (UipMpj)T ✈ =

  • (∇Uip)T✈
  • Mpj+Uip
  • (∇Mpj)T✈
  • .

For simplicity, subsequently we use R{g(θ)} to be R✈{g(θ)}

Chih-Jen Lin (National Taiwan Univ.) 68 / 81

slide-69
SLIDE 69

Forward + backward settings R operator

R Operator for Ji✈ I

We have Ji✈ = R{③L+1,i}. Now assume R{Z m,i} is available from the previous layer We consider the following forward operations From (15), we have R{φ(pad(Z m,i))} =mat

  • Pm,i

φ Pm,i padR{vec

  • Z m,i

}

  • hmhmdm×am

convbm conv

Chih-Jen Lin (National Taiwan Univ.) 69 / 81

slide-70
SLIDE 70

Forward + backward settings R operator

R Operator for Ji✈ II

From (14), (15), we have R{Sm,i} =R{W mφ(pad(Z m,i)) + ❜m1T

am

convbm conv}

=R{W mφ(pad(Z m,i))} + R{❜m1T

am

convbm conv}

=R{W m}φ(pad(Z m,i)) + W mR{φ(pad(Z m,i))}+ R{❜m}1T

am

convbm conv

=V m

Wφ(pad(Z m,i)) + W mR{φ(pad(Z m,i))}+

✈ m

b 1T am

convbm conv,

Chih-Jen Lin (National Taiwan Univ.) 70 / 81

slide-71
SLIDE 71

Forward + backward settings R operator

R Operator for Ji✈ III

where we use R{W m} =V m

W,

R{❜m} =✈ m

b .

Note that ✈ =   ✈ 1 . . . ✈ L   , and each ✈ m, m = 1, . . . , L has the same length as the number of variables (including bias) at the mth layer.

Chih-Jen Lin (National Taiwan Univ.) 71 / 81

slide-72
SLIDE 72

Forward + backward settings R operator

R Operator for Ji✈ IV

We further split ✈ m to V m

W (a matrix form) and ✈ m b

From (13), we have R{σ(Sm,i)} = σ′(Sm,i) ⊙ R{Sm,i}. (18) From (15), we have R{Z m+1,i} =R{Pm,i

poolσ(Sm,i)}

=mat

  • Pm,i

poolR{vec

  • σ(Sm,i)
  • }
  • dm+1×am+1bm+1 .

Chih-Jen Lin (National Taiwan Univ.) 72 / 81

slide-73
SLIDE 73

Forward + backward settings R operator

R Operator for Ji✈ V

We can continue this process until we get Ji✈ = R{③L+1,i}. Clearly, we do not need to store ∂zL+1,i

1

∂Sm,i , . . . , ∂zL+1,i

nL+1

∂Sm,i as before

Chih-Jen Lin (National Taiwan Univ.) 73 / 81

slide-74
SLIDE 74

Forward + backward settings Gauss-Newton matrix-vector product

Outline

1

Backward setting Jacobian evaluation Gauss-Newton Matrix-vector products

2

Forward + backward settings R operator Gauss-Newton matrix-vector product

Chih-Jen Lin (National Taiwan Univ.) 74 / 81

slide-75
SLIDE 75

Forward + backward settings Gauss-Newton matrix-vector product

Gauss-Newton Matrix-vector Product I

From the above discussion, we have known how to calculate Ji✈ Calculate Bi(Ji✈) is known to be easy

Chih-Jen Lin (National Taiwan Univ.) 75 / 81

slide-76
SLIDE 76

Forward + backward settings Gauss-Newton matrix-vector product

Gauss-Newton Matrix-vector Product II

Now for (Ji)T(BiJi✈), if we define ✉ = BiJi✈, then (Ji)T✉ = ∂③L+1,i ∂θT T ✉. But earlier the gradient calculation is (Ji)T∇③L+1,iξ(③L+1,i; ② i, Z 1,i) = ∂③L+1,i ∂θT T ∂ξi ∂③L+1,i .

Chih-Jen Lin (National Taiwan Univ.) 76 / 81

slide-77
SLIDE 77

Forward + backward settings Gauss-Newton matrix-vector product

Gauss-Newton Matrix-vector Product III

Thus the same backward procedure can be used All we need is to replace ∂ξi ∂③L+1,i with ✉

Chih-Jen Lin (National Taiwan Univ.) 77 / 81

slide-78
SLIDE 78

Forward + backward settings Gauss-Newton matrix-vector product

Complexity Analysis I

We have known from past slides that matrix-matrix products are the bottleneck (though in our cases some slow MATLAB functions are also bottlenecks in practice) For simplicity, in our analysis we just count the number of matrix-matrix products Approaches solely by backward settings: if ∂zL+1,i

1

∂Sm,i , . . . , ∂zL+1,i

nL+1

∂Sm,i

Chih-Jen Lin (National Taiwan Univ.) 78 / 81

slide-79
SLIDE 79

Forward + backward settings Gauss-Newton matrix-vector product

Complexity Analysis II

stored, then nL+1 × 3 + #CG × 2 If not, then #CG × (nL+1 × 3 + 2) Note that “3” comes from one product in the forward process and two in the backward process (the same as the situation in Gradient calculation)

Chih-Jen Lin (National Taiwan Univ.) 79 / 81

slide-80
SLIDE 80

Forward + backward settings Gauss-Newton matrix-vector product

Complexity Analysis III

If using R operators, then #CG × (3 + 2), where “3” are from the forward process W mφ(pad(Z m,i)), and V m

Wφ(pad(Z m,i)), W mR{φ(pad(Z m,i))},

and “2” are from the backward process

Chih-Jen Lin (National Taiwan Univ.) 80 / 81

slide-81
SLIDE 81

Forward + backward settings Gauss-Newton matrix-vector product

Discussion I

At this moment in the Python code we are not using the forward mode for J✈ It was not available before However, since version 2.10 released in January 2020, this functionality is provided: https://www.tensorflow.org/api_docs/ python/tf/autodiff/ForwardAccumulator It will be interesting to do the implementation and make a comparison

Chih-Jen Lin (National Taiwan Univ.) 81 / 81