Backpropagate a two-layer MLP
Compute forward predictions and parameter gradients for a dense ReLU MLP.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Input x (nonempty batch×D), y (batch×O), w1 (D×H), b1 (H), w2 (H×O), b2 (O). h=ReLU(x·w1+b1), predictions=h·w2+b2. Calculate loss as mean squared error across all B×O scalar outputs, with ReLU slope zero at zero. Return predictions, loss and gradients with keys w1, b1, w2, b2 matching parameter shapes.
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Compute forward predictions and parameter gradients for a dense ReLU MLP.
Reproduction
{"x":[[1,2],[-3,1]],"y":[[2],[0]],"w1":[[1,0],[0,1]],"b1":[0,-2],"w2":[[1],[2]],"b2":[0.1]}{"predictions":[[1.1],[0.1]],"loss":0.41,"gradients":{"w1":[[-0.9,0],[-1.8,0]],"b1":[-0.9,0],"w2":[[-0.9],[0]],"b2":[-0.8]}}{"x":[[0]],"y":[[0]],"w1":[[0]],"b1":[0],"w2":[[0]],"b2":[0]}{"predictions":[[0]],"loss":0,"gradients":{"w1":[[0]],"b1":[0],"w2":[[0]],"b2":[0]}}{"x":[[1]],"y":[[3]],"w1":[[0]],"b1":[0],"w2":[[2]],"b2":[0]}{"predictions":[[0]],"loss":9,"gradients":{"w1":[[0]],"b1":[0],"w2":[[0]],"b2":[-6]}}Before you begin
- Python functions, lists and dictionaries
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.