Momentum

As described in <>, SGD can be thought of as standing at the top of a mountain and working your way down by taking a step in the direction of the steepest slope at each point in time. But what if we have a ball rolling down the mountain? It won’t, at each given point, exactly follow the direction of the gradient, as it will have momentum. A ball with more momentum (for instance, a heavier ball) will skip over little bumps and holes, and be more likely to get to the bottom of a bumpy mountain. A ping pong ball, on the other hand, will get stuck in every little crevice.

So how can we bring this idea over to SGD? We can use a moving average, instead of only the current gradient, to make our step:

  1. weight.avg = beta * weight.avg + (1-beta) * weight.grad
  2. new_weight = weight - lr * weight.avg

Here beta is some number we choose which defines how much momentum to use. If beta is 0, then the first equation becomes weight.avg = weight.grad, so we end up with plain SGD. But if it’s a number close to 1, then the main direction chosen is an average of the previous steps. (If you have done a bit of statistics, you may recognize in the first equation an exponentially weighted moving average, which is very often used to denoise data and get the underlying tendency.)

Note that we are writing weight.avg to highlight the fact that we need to store the moving averages for each parameter of the model (they all have their own independent moving averages).

<> shows an example of noisy data for a single parameter, with the momentum curve plotted in red, and the gradients of the parameter plotted in blue. The gradients increase, then decrease, and the momentum does a good job of following the general trend without getting too influenced by noise.

In [ ]:

  1. #hide_input
  2. #id img_momentum
  3. #caption An example of momentum
  4. #alt Graph showing an example of momentum
  5. x = np.linspace(-4, 4, 100)
  6. y = 1 - (x/3) ** 2
  7. x1 = x + np.random.randn(100) * 0.1
  8. y1 = y + np.random.randn(100) * 0.1
  9. plt.scatter(x1,y1)
  10. idx = x1.argsort()
  11. beta,avg,res = 0.7,0,[]
  12. for i in idx:
  13. avg = beta * avg + (1-beta) * y1[i]
  14. res.append(avg/(1-beta**(i+1)))
  15. plt.plot(x1[idx],np.array(res), color='red');

Momentum - 图1

It works particularly well if the loss function has narrow canyons we need to navigate: vanilla SGD would send us bouncing from one side to the other, while SGD with momentum will average those to roll smoothly down the side. The parameter beta determines the strength of the momentum we are using: with a small beta we stay closer to the actual gradient values, whereas with a high beta we will mostly go in the direction of the average of the gradients and it will take a while before any change in the gradients makes that trend move.

With a large beta, we might miss that the gradients have changed directions and roll over a small local minima. This is a desired side effect: intuitively, when we show a new input to our model, it will look like something in the training set but won’t be exactly like it. That means it will correspond to a point in the loss function that is close to the minimum we ended up with at the end of training, but not exactly at that minimum. So, we would rather end up training in a wide minimum, where nearby points have approximately the same loss (or if you prefer, a point where the loss is as flat as possible). <> shows how the chart in <> varies as we change beta.

In [ ]:

  1. #hide_input
  2. #id img_betas
  3. #caption Momentum with different beta values
  4. #alt Graph showing how the beta value influences momentum
  5. x = np.linspace(-4, 4, 100)
  6. y = 1 - (x/3) ** 2
  7. x1 = x + np.random.randn(100) * 0.1
  8. y1 = y + np.random.randn(100) * 0.1
  9. _,axs = plt.subplots(2,2, figsize=(12,8))
  10. betas = [0.5,0.7,0.9,0.99]
  11. idx = x1.argsort()
  12. for beta,ax in zip(betas, axs.flatten()):
  13. ax.scatter(x1,y1)
  14. avg,res = 0,[]
  15. for i in idx:
  16. avg = beta * avg + (1-beta) * y1[i]
  17. res.append(avg)#/(1-beta**(i+1)))
  18. ax.plot(x1[idx],np.array(res), color='red');
  19. ax.set_title(f'beta={beta}')

Momentum - 图2

We can see in these examples that a beta that’s too high results in the overall changes in gradient getting ignored. In SGD with momentum, a value of beta that is often used is 0.9.

fit_one_cycle by default starts with a beta of 0.95, gradually adjusts it to 0.85, and then gradually moves it back to 0.95 at the end of training. Let’s see how our training goes with momentum added to plain SGD.

In order to add momentum to our optimizer, we’ll first need to keep track of the moving average gradient, which we can do with another callback. When an optimizer callback returns a dict, it is used to update the state of the optimizer and is passed back to the optimizer on the next step. So this callback will keep track of the gradient averages in a parameter called grad_avg:

In [ ]:

  1. def average_grad(p, mom, grad_avg=None, **kwargs):
  2. if grad_avg is None: grad_avg = torch.zeros_like(p.grad.data)
  3. return {'grad_avg': grad_avg*mom + p.grad.data}

To use it, we just have to replace p.grad.data with grad_avg in our step function:

In [ ]:

  1. def momentum_step(p, lr, grad_avg, **kwargs): p.data.add_(-lr, grad_avg)

In [ ]:

  1. opt_func = partial(Optimizer, cbs=[average_grad,momentum_step], mom=0.9)

Learner will automatically schedule mom and lr, so fit_one_cycle will even work with our custom Optimizer:

In [ ]:

  1. learn = get_learner(opt_func=opt_func)
  2. learn.fit_one_cycle(3, 0.03)
epochtrain_lossvalid_lossaccuracytime
02.8560002.4934290.24611500:10
12.5042052.4638130.34828000:10
22.1873871.7556700.41885300:10

In [ ]:

  1. learn.recorder.plot_sched()

Momentum - 图3

We’re still not getting great results, so let’s see what else we can do.