
In this post we walk through the design, training, and testing of a neural network with multiple hidden layers, trained with minibatch stochastic gradient descent and momentum. The network is used for logistic regression, an older name for binary classification. Our question is practical: how much faster does training get when we add minibatches and momentum, and does accuracy suffer?
The task: is it a 9?
We classify handwritten digits from the MNIST data set, and we only care about one digit, the last digit of the author's student ID, which happens to be 9. Every image labeled 9 becomes a 1, and every other digit becomes a 0, so the network outputs a probability that an image is a 9.
Each 28 x 28 image is flattened into a 784-value vector and scaled by 1/255. We use the first 50,000 images of the 60,000-image training file for training and the next 10,000 for testing, reshuffling the training images at the start of every epoch. A smaller 500-image training and 100-image test split served as a quick check that the code worked before the long runs.
The network
Each layer applies a linear map and an activation; the hidden layers use ReLU and the output uses the sigmoid so it reads as a probability:
Z⁽ˡ⁾ = W⁽ˡ⁾ · A⁽ˡ⁻¹⁾ + B⁽ˡ⁾
A⁽ˡ⁾ = max(Z⁽ˡ⁾, 0) (hidden), A⁽ⁿ⁾ = 1 / (1 + e⁻ᶻ) (output)
The loss is binary cross-entropy, and backpropagation begins at the output with dZ⁽ⁿ⁾ = A⁽ⁿ⁾ − Y, passing back through each hidden layer with the ReLU derivative. Weights and biases start uniform in (−0.1, 0.1), and the learning rate is 0.01.
Minibatches
Instead of computing the gradient over all 50,000 images before every step, we compute it on a small batch and step right away. With a batch of 27 images the network takes 1,852 steps per pass over the data instead of 100 steps with a batch of 500. The gradients are noisier, but there are many more of them, and the noise also acts as a mild regulariser.
Why momentum helps
Plain gradient descent has trouble in a long, narrow valley: the gradient points mostly across the steep walls, so the path zig-zags and makes slow progress along the valley floor. Momentum keeps a running velocity, so steps that point the same way on consecutive iterations add up while the side-to-side parts cancel. In the notebook's form the update is:
v ← β · v + α · ∇J
w ← w − v
Here β is the momentum coefficient, and the notebook tries 0.9 and 0.1. This is equivalent to the more common form that averages gradients, up to a rescaled learning rate.
if(momentum == True):
V_dW[layer] = np.multiply(momentum_coef, V_dW[layer]) + np.multiply(alpha, _dW)
V_dB[layer] = np.multiply(momentum_coef, V_dB[layer]) + np.multiply(alpha, _dB)
W[layer] = W[layer] - V_dW[layer]
B[layer] = B[layer] - V_dB[layer]
Drag the slider to change the momentum coefficient β and compare plain gradient descent with momentum in a narrow valley.
In a long, narrow valley, plain gradient descent zig-zags across the steep direction, while momentum averages those swings out and keeps moving along the floor.
Results
We trained four configurations on the 50,000 training images and measured wall-clock time and accuracy on the 10,000 test images. The learning rate was 0.01 in every run.
- Batch 500, no momentum: 2,118 s, 99.15% test accuracy.
- Batch 27, no momentum: 745 s, 99.18%.
- Batch 27, momentum 0.1: 262 s, 99.16%.
- Batch 27, momentum 0.9: 230 s, 99.22%.
The headline is time. The best run needed about a tenth of the time of the batch-500 run for the same accuracy. Smaller batches account for most of the first speedup, and momentum with a coefficient of 0.9 cuts the time further. The accuracy differences are tiny: 99.22% versus 99.16% is six images out of 10,000, and each configuration ran once from one random seed, so we do not rank them on accuracy.
The four runs also used different numbers of epochs (about 1,500, 500, 100 and 100), so the times compare whole experiments rather than time per epoch.
A note from 2026
The reported cost is the loss on the last minibatch of each epoch, not an average over the data, so the logged costs swing by orders of magnitude between epochs and should be read as rough. The training loop also skips the final minibatch of each epoch, and the bias gradient sums over all units to one scalar, where a per-unit sum would be more standard. None of this stops the network from reaching about 99.2% accuracy, but a cleaner implementation would be worth rerunning.
Takeaway
Minibatches and momentum together made this training roughly nine times faster without hurting accuracy. The Adam post that follows uses the same network and data with a different optimiser. The full notebook, with every cell and plot, is on GitHub.