1 of 56

Backpropagation pt. 1�

Prof. André E. Lazzaretti

lazzaretti@utfpr.edu.br

2 of 56

Loss function

  • Training dataset of I pairs of input/output examples:

​

​

  • Loss function or cost function measures how bad model is:

​

or for short:

​

Returns a scalar that is smaller when model maps inputs to outputs better

3 of 56

Example

4 of 56

Problem 1: Computing gradients

Loss: sum of individual terms:

​

​

​

​

SGD Algorithm:

​

​

​

Parameters:

​

​

​

​

Need to compute gradients

5 of 56

Why is this such a big deal?

  • A neural network is just an equation:

​

​

​

​

  • But it’s a huge equation, and we need to compute the derivative
    • for every parameter
    • for every point in the batch
    • for every iteration of gradient descent

​

​

6 of 56

Gradients

  • Backpropagation intuition
  • Toy model
  • Background mathematics
  • Backpropagation forward pass
  • Backpropagation backward pass
  • Algorithmic differentiation

​

​

​

7 of 56

Problem 1: Computing gradients

Loss: sum of individual terms:

​

​

​

​

SGD Algorithm:

​

​

​

Parameters:

​

​

​

​

Need to compute gradients

8 of 56

Algorithm to compute gradient efficiently

  • “Backpropagation algorithm”
  • Rumelhart, Hinton, and Williams (1986)

​

9 of 56

BackProp intuition #1: the forward pass

  • We want to know how a change in orange weight affects loss
  • We need to know the activations at each layer.

10 of 56

BackProp intuition #2: the backward pass

To calculate how a small change in a weight or bias feeding into hidden layer h3 modifies the loss, we need to know:�

    • how a change in layer h3 changes the model output f
    • how a change in model output changes the loss l

11 of 56

BackProp intuition #2: the backward pass

To calculate how a small change in a weight or bias feeding into hidden layer h2 modifies the loss, we need to know:

​

    • how a change in layer h2 affects h3
    • how h3 changes the model output
    • how this output changes the loss

12 of 56

BackProp intuition #2: the backward pass

To calculate how a small change in a weight or bias feeding into hidden layer h1 modifies the loss, we need to know:

​

    • how a change in layer h1 affects layer h2
    • how a change in layer h2 affects layer h3
    • how layer h3 changes the model output
    • how the model output changes the loss

13 of 56

Gradients

  • Backpropagation intuition
  • Toy model
  • Background mathematics
  • Backpropagation forward pass
  • Backpropagation backward pass
  • Algorithmic differentiation

​

​

​

14 of 56

Toy function

  • Consists of a series of functions that are composed with each other
  • Unlike in neural networks, it just uses scalars (not vectors)
  • “Activation functions” sin, exp, cos

15 of 56

Toy function

Derivatives

16 of 56

Gradients of toy function

We want to calculate:

 

17 of 56

Gradients of composed functions

Calculating expressions by hand:

    • some expressions very complicated.
    • obvious redundancy (look at sin terms in bottom equation)

18 of 56

Forward pass

1. Write this as a series of

intermediate calculations

​

2. Compute these intermediate quantities

19 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

20 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The first of these derivatives is trivial

21 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The second of these derivatives is computed via the chain rule

 

22 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The second derivative is computed via the chain rule

 

 

 

23 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The second of these derivatives is computed via the chain rule

Already computed!

ω3

 

24 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The remaining derivatives also calculated by further use of chain rule

25 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The remaining derivatives also calculated by further use of chain rule

Already computed!

-sin[f2]

26 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The remaining derivatives also calculated by further use of chain rule

27 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The remaining derivatives also calculated by further use of chain rule

28 of 56

Backward pass

1. Compute the derivatives of the loss with respect to these intermediate quantities, but in reverse order.

  • The remaining derivatives also calculated by further use of chain rule

29 of 56

Backward pass

2. Find how the loss changes as a function of the parameters β and ω.

  • Another application of the chain rule

 

 

 

30 of 56

Backward pass

2. Find how the loss changes as a function of the parameters β and ω.

  • Another application of the chain rule

Already calculated in part 1.

hk

 

31 of 56

Backward pass

2. Find how the loss changes as a function of the parameters β and ω.

  • Another application of the chain rule
  • Similarly for β parameters

32 of 56

Backward pass

2. Find how the loss changes as a function of the parameters β and ω.

33 of 56

Check!

Write the gradient equation to update ω2 (first, calculate δli / δω2)

Final answer: −2 h2 ω3 sin(f2) (f3 − yi)

34 of 56

Backpropagation

35 of 56

Backpropagation

36 of 56

Backpropagation

37 of 56

Backpropagation

38 of 56

Backpropagation

39 of 56

Backpropagation

40 of 56

Backpropagation

41 of 56

Backpropagation

42 of 56

Backpropagation

43 of 56

Backpropagation

44 of 56

Backpropagation

45 of 56

Backpropagation

46 of 56

Backpropagation

47 of 56

Backpropagation

48 of 56

Backpropagation

49 of 56

Backpropagation

50 of 56

Backpropagation

51 of 56

Backpropagation

52 of 56

Backpropagation

53 of 56

Backpropagation

54 of 56

Backpropagation

55 of 56

Backpropagation

56 of 56

Backpropagation