Skip to content

Backprop by Hand

You'll write a small neural network with nothing but array math, derive and check its gradients, and train it to read handwritten digits.

About

Frameworks hide backpropagation so well that you can train networks for a year without knowing what the gradient of a layer looks like, and writing one two-layer network in plain array code, with no autograd, fixes that permanently. You need numpy or the equivalent, the MNIST digits (free, 60,000 training images), and a few evenings of patience. What counts is forward and backward passes you wrote, a gradient check against finite differences that agrees to about six decimal places, and at least 95 percent on the 10,000 test digits. If you reach for a framework's autograd at any point, that's a different badge.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Three or four evenings
Skill level
Advanced (years of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Machine Learning to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Write the forward pass for a two-layer network and check its first loss. Input of 784 pixels, a hidden layer of 128 with ReLU, an output of 10 with softmax, and cross-entropy loss. Initialize the weights small and random; all zeros will never learn, because every hidden unit stays identical. Feed in a batch of 64 digits and print the loss, which should be about 2.3 at the start since that's the log of 10. Take a screenshot of the code and the first loss. Photo
  2. 2
    Derive the gradients on paper, layer by layer, from the loss back to the first weights. Softmax with cross-entropy gives you the neat result that the output gradient is the prediction minus the one-hot label. From there it's the chain rule through each matrix and through the ReLU mask. Write it out with the shapes next to every term, because shapes are where it goes wrong. Take a photo of the page. Photo
  3. 3
    Implement the backward pass and check it numerically. Nudge one weight up and then down by a tiny amount, about 0.00001, recompute the loss each time, and compare the difference quotient to your analytic gradient. Do this for a handful of weights in each layer. Agreement to about six decimal places means you've got it; disagreement by a factor of two or a sign usually means a missed mean over the batch. Take a screenshot of the comparison. Photo
  4. 4
    Train with minibatch gradient descent and plot loss and validation accuracy per epoch. Hold out 10,000 of the training images as a validation set. Batches of 64, a learning rate around 0.1, ten epochs. Scale the pixels to the range 0 to 1 first or it'll crawl. You should cross 90 percent on validation after one epoch and reach 95 to 97 after ten. Run the 10,000 test images once, at the end, and write that number next to the final validation score. Take a screenshot of the curves with the test score on them. Photo
  5. 5
    Show the hidden units as images and the digits it gets wrong. Reshape each of the 128 first-layer weight vectors into a 28 by 28 image and tile them. Many will look like blurry strokes. Then display 20 misclassified test digits with the true and predicted labels; a few will be ones you'd get wrong too. Take a screenshot of both grids. Photo
  6. 6
    Change one thing and say what it did. Double the hidden layer, swap ReLU for sigmoid, or drop the learning rate by ten. Retrain and compare the curves. Then write a paragraph on what surprised you about writing it yourself, and what you'd now trust a framework to handle. Post the curves with the paragraph. Photo