Learn AI.
OPTIONAL IMPLEMENTATION STUDIO · NUMERICAL NETWORKS

Write the network update.

Turn a small network diagram into array operations. Verify each derivative before trusting a falling loss curve.

Browser practice requires no installation. The implementation task uses Python and NumPy on CPU. Supplied execution, learner code, objective checks and human review are separate outcomes. This optional studio does not change existing path completion. Human review and learner pilots remain pending.

Choose your entry point

Use P02 array shapes and backpropagation as preparation, or start with the live values below. Refresh slopes if “derivative” is unfamiliar: it describes how a small change affects a result. Experienced programmers can skip directly to the known-value and gradient checks.

The browser task has two inputs and one numerical target. Before applying an update, predict the sign of its change to b2, the output bias: does the current prediction need to rise or fall? With several examples, use their combined contribution rather than one row alone.

Try → predict → experiment

Trace a 2–3–1 network

Two input features feed three tanh units and one linear output: 13 parameters. These are the same initial values and worked rows as the Python starter, separate from its 16-row training grid. This tiny browser batch is practice with no held-out test.

Changing a control restores the initial model so comparisons start at the same checkpoint. Predict whether this fault is detectable on a one-row batch.

  1. X (3,2)
  2. × W1 (2,3) + b1 (3,)
  3. Z → tanh → H (3,3)
  4. × W2 (3,1) + b2 (1,)
  5. Prediction (3,1)
  6. Mean squared error: scalar

MSE: 0.21107853 · 0/50 browser updates. Essential operations are matrix multiplication, elementwise tanh, addition of each unit's bias to every row, and mean squared error.

Live pre-update arithmetic. D = 2 × error ÷ 3; dZ uses the current output weights.
Input → targetZH = tanh(Z)PredictionDdZ
[1, 2] → 1-0.1000, 0.1000, 1.8500-0.0997, 0.0997, 0.95170.330864-0.446090-0.088332, 0.132498, -0.016805
[-1, 1] → -0.5-0.6000, 1.0000, 0.6500-0.5370, 0.7616, 0.5717-0.1072200.2618530.037266, -0.032991, 0.070511
[2, -1] → 0.51.2000, -2.1000, -0.35000.8337, -0.9705, -0.33640.323316-0.117789-0.007186, 0.002057, -0.041785

For W1[0,0], multiply each row's first input by its first dZ and add: (1 × -0.088332) + (-1 × 0.037266) + (2 × -0.007186) = -0.139969. This is one entry of Xᵀ × dZ. The superscript T means transpose: turn rows into columns so the example dimension can be summed.

All proposals use the same current model. With the fault enabled, applied gradients are multiplied by batch size.
ParameterCurrentCorrect gradientApplied gradientProposed at rate 0.05
W1[0,0]0.400000-0.139969-0.1399690.406998
W1[0,1]-0.7000000.1696040.169604-0.708480
W1[0,2]0.200000-0.170885-0.1708850.208544
W1[1,0]-0.300000-0.132212-0.132212-0.293389
W1[1,1]0.5000000.2299470.2299470.488503
W1[1,2]0.8000000.0786860.0786860.796066
b1[0]0.100000-0.058252-0.0582520.102913
b1[1]-0.2000000.1015640.101564-0.205078
b1[2]0.0500000.0119220.0119220.049404
W2[0,0]0.200000-0.194363-0.1943630.209718
W2[1,0]-0.3000000.2692740.269274-0.313464
W2[2,0]0.400000-0.235250-0.2352500.411762
b2[0]0.000000-0.302026-0.3020260.015101

State is temporary; download before leaving. The small direct CPU calculation needs no worker, timer, animation or GPU.

Understand the forward and backward shapes

For n examples, d input features and h hidden units, X has shape (n,d). The first weights W1 have shape (d,h). Multiplication X @ W1 sums the d input contributions for each example and hidden unit. Add b1, a length-h bias vector, to each row. The result Z has shape (n,h); H = tanh(Z) applies a bounded nonlinear function to every element. The output is H @ W2 + b2, where W2 has shape (h,1) and b2 has one value.

The target Y must have shape (n,1), matching the prediction P exactly. A target of shape (n,) may accidentally broadcast into a matrix of pairwise errors. A program that runs has not necessarily computed the intended loss. The starter rejects this ambiguity.

The loss is L = mean((P − Y)²): square each prediction error and average all n values. Read its derivative D = 2(P − Y)/n as “twice each error, divided by the number of examples.” This D has shape (n,1) and already includes the averaging factor. Do not divide again later.

Backward operations: all use the same pre-update parameters
QuantityCalculationShape and meaning
dW2Hᵀ @ D(h,1): sum output-weight contributions over examples
db2sum D down rows(1,): one bias used by every example
dZ(D @ W2ᵀ) × (1 − H²)(n,h): send errors through output weights and tanh slopes
dW1Xᵀ @ dZ(d,h): sum input-weight contributions
db1sum dZ down rows(h,): one total per hidden bias

The prefix d means a derivative of L with respect to that quantity. T means transpose; @ means matrix multiplication; × and squaring in the dZ row act element by element. The slope of tanh at hidden value H is 1 − H². Multiplying by it expresses how much of an incoming change passes through that unit.

A hand-checkable backward pass

Use one input [1,2], target 1, zero W1/b1/b2 and output weights [2,−1]. Hidden values are [0,0], prediction is 0, and D = 2(0−1) = −2. Therefore dZ = [−4,2], dW1 = [[−4,2],[−8,4]], db1 = [−4,2], dW2 = [[0],[0]] and db2 = [−2]. The Python check uses this two-hidden-unit case to catch code hard-coded to the default three units.

After calculating every gradient, update every parameter θ with θ_new = θ_old − η × gradient. Here θ denotes any weight or bias and η (eta) is the learning rate. For the output bias above, η = 0.05 gives 0 − 0.05 × (−2) = 0.1. Updating W2 before using it to calculate dZ mixes two models and breaks this derivation.

Why compare with finite differences?

A central difference estimates a derivative by raising one parameter by a small amount ε, lowering it by the same amount, and dividing the two loss values' difference by 2ε. The script uses ε = 0.00001 and tests all 13 default parameters. Its tolerance is 0.000001 plus 0.0001 times the larger derivative magnitude; this allows floating-point approximation while exposing meaningful mistakes. Each check uses two forward evaluations per parameter, so it is a debugging tool rather than an efficient training method. Passing at one checkpoint does not prove every shape or failure case is correct.

Compare manual gradients with an automatic differentiation graph and revisit numerical stability.

Apply: a runnable NumPy implementation

Download numerical-network exercise Download pinned dependency

Use Python 3.11+ and the P02 isolated-environment instructions. On macOS/Linux:

python3 -m venv .venv
.venv/bin/python -m pip install -r network-requirements.txt
.venv/bin/python numerical-network.py --demo --updates 200 --out supplied-run.json
.venv/bin/python numerical-network.py --check
.venv/bin/python numerical-network.py --gradient-check
.venv/bin/python numerical-network.py --train --updates 200 --out learner-run.json
.venv/bin/python -m pip freeze > environment-freeze.txt

On Windows create the environment with py -3 -m venv .venv and use .venv\Scripts\python.exe. NumPy 2.3.5 is the pinned direct dependency. The reference environment is Python 3.12.14 on macOS ARM64; other platforms have not been verified. Only package installation needs network access.

  1. Run the supplied demo first. It is a complete working reference, explicitly labelled supplied in its output; it does not execute your TODO functions.
  2. Implement learner_forward and learner_backward, using the equations and validation contract. Preserve float64, exact shapes and original inputs. The public checks intentionally fail until you complete the functions.
  3. Run the known-value, finite-difference, duplicated-batch and mutation checks. Then deliberately omit the batch average: explain why one row hides the fault and repeated rows expose it.
  4. Run the full gradient-check command and keep every parameter result. Repair failures by tracing one multiplication or reduction, rather than loosening the tolerance until it passes.
  5. Train your implementation and compare the same initial parameters, rate and accepted update count with the supplied run. Report disagreements and all failed runs. Change one factor at a time.

What the training loop actually measures

The separate training experiment uses a 4×4 grid of input values −1, −1/3, 1/3, 1 and a disjoint 4×4 development grid using −0.75, −0.25, 0.25, 0.75. A supplied teacher formula generates targets: 0.4x₁ − 0.3x₂ + 0.2x₁x₂. The network learns from training examples; the target formula itself is programmed. No preprocessing is fitted in this deliberately bounded example.

Each update uses all 16 training rows. Runs cap at 1,000 updates with rates 0.001, 0.01, 0.05 or 0.1. Reports include every accepted checkpoint's train/development loss, final parameters, source hash, environment, processed training-example count and a training-mean baseline evaluated on development. These counts exclude forward-only monitoring and rejected proposals; they are not total-compute measurements. Unsafe proposals retain the preceding model and record why they stopped. An interrupted process may leave no report; rerun from the fixed initialization with a new filename.

Existing output files are not overwritten. Development observations are repeated public evidence, not a reserved test. A falling curve on this smooth synthetic target does not establish real-data performance or independent implementation skill. Continue to the real-data diagnosis lesson for messy input/target and evaluation-mode faults.

Evidence and next steps

  • Understand: trace one hidden activation, one gradient contribution and one parameter update. Explain why target shape and batch reduction affect meaning.
  • Implement: submit forward/backward code, environment freeze, all public checks, a new feature/hidden-width case, numerical gradient output and matched training reports. Have a reviewer supply an unfamiliar shape and update failure, and independently replay the code.
  • Decide: specify the evidence required to accept a repaired training implementation. Separate numerical correctness, optimization progress and generalization; explain what each test cannot establish.

Studio artifacts stay in local files; no draft or review state is saved to My Work. Visible checks are development practice and reviewed proficiency remains pending. Use the training-diagnosis project to organize your report, or continue to P05 framework debugging.

Learn AI · Understand by doing.Loading local progress…Copyright (c) 2026 Mobi Weave, Inc. All rights reserved