Project 3 of 14, for Neil
Optimiser Benchmark
A time trial for optimisers. One network, one start, one log of 800 evenings, and three optimisers written from scratch: plain SGD, momentum and Adam, compared fairly, then Adam again from all zeros.
Sign up free to open itLog in
Act 7 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilMy network learnt Paul's whole log the plain way, and it did get there, eventually.
- NeilSo let's have a time trial with the same network, start and batches, and whoever's fastest down the loss wins.
- YahyaI'm calling it now, Adam wins.
The network from Neural Network from Scratch is in brain.py: forward, loss, backward, and batches, mended from Caitlin's version. In optimisers.py, plain SGD is already written, as the pattern.
init_params(n_in, n_hidden, n_out, rng):W1fromrng.normal(0, np.sqrt(2 / n_in), (n_in, n_hidden)), thenW2fromrng.normal(0, np.sqrt(1 / n_hidden), (n_hidden, n_out)), in that order. Both biases start at zeros. It returns the dict.Momentum(rate, beta=0.9): itsstep(params, grads)keeps a velocity for every weight, starting at zeros. Each step,velocity = beta * velocity + gradient, then the weight moves byratetimes the velocity.Adam(rate=0.001, beta1=0.9, beta2=0.999, eps=1e-8): itsstep(params, grads)is Adam, as in its Learn step.train(X, T, params, optimiser, epochs, batch_size, rng): each epoch, for each batch frombatches(X, T, batch_size, rng), forward and backward on that batch alone, thenoptimiser.step. After each epoch, the loss on every row ofX. It changesparamsin place and returns the losses.- Run as a program, it trains the network on all eight hundred evenings with each optimiser from the same start, then once from all zeros.
From small random weights, 30 epochs in batches of 32: sgd loss 0.0468, every answer right on 89% of evenings momentum loss 0.0119, every answer right on 96% of evenings adam loss 0.0092, every answer right on 97% of evenings From every weight at 0: adam loss 0.1756, every answer right on 40% of evenings
What you learn
What's in it
- Gathering speed
- The long gentle slope
- How Paul would do it
- Adam
- Adam's first step
- A handful at a time
- Fix Caitlin's version
- Where the weights start
- Sixteen of the same
- Training properly
- Ship it