PigeonColumba compressa

Lesson 3 of 10

Train a two-layer network from scratch

Put lessons 1 and 2 together: a network x -> relu(x W1 + b1) -> logits = h W2 + b2 trained with plain mini-batch gradient descent on a two-spirals dataset (provided). No libraries beyond numpy.

The loop is the same one lab/train.py:fit runs, minus Adam and extras:

  1. Shuffle, take a batch.
  2. Forward: hidden activations, logits.
  3. dz = (softmax(logits) - onehot(y)) / N.
  4. Backward through the output layer, then the ReLU layer.
  5. param -= lr * grad for all four parameters.

Implement train(X, y, hidden, epochs, lr, seed) returning a predict(X) function. The test requires 90% training accuracy on the spirals, which a linear model cannot get (about 50%), so your hidden layer has to work.

Your code

Where this is used