Where to put your code and how to run checks
Extract the project ZIP (or use the repository root) and activate the course virtual environment with requirements-cpu.txt installed. Run all commands from that top-level folder:
- Your implementation file (
projects/regression-audit/starter/model.py): Open this file in your editor and implement the five functions (predict,loss,gradient_check,train, andreport) one stage at a time. - Automated stage verifier (
projects/regression-audit/tests/check.py): Runs your code inprojects/regression-audit/starter/model.pyagainst each stage contract. - Reference solution (
projects/regression-audit/solution/model.py): Instructor reference (--implementation solution) for checking your environment or comparing after you complete a stage.
# Run run command in terminal using the course Python environment
python3 projects/regression-audit/tests/check.py --stage 1
python3 projects/regression-audit/tests/check.py --stage 2
python3 projects/regression-audit/tests/check.py --stage 3
python3 projects/regression-audit/tests/check.py --stage 4Stage 1 — Prediction and objective
Implement predict(params, x) and loss(params, x, y) in projects/regression-audit/starter/model.py:
- Compute the linear prediction
params['weight'] * x + params['bias']. - In
loss(params, x, y), verify thatx.shape == y.shapeand raiseValueErrorif a column vectory[:, None]is passed—otherwise(predict(params, x) - y)silently broadcasts into a 2D pairwise residual matrix. - Return the scalar mean squared error
jnp.mean((predict(params, x) - y) ** 2).
Stage 2 — Gradient evidence
Implement gradient_check(params, x, y, h=1e-2) in projects/regression-audit/starter/model.py:
- Compute automatic derivatives
automatic = jax.grad(loss)(params, x, y). - For each key in
params('weight'and'bias'), perturb that scalar parameter by+hand-hwhile holding the other parameter fixed, and compute the central difference(loss(plus, x, y) - loss(minus, x, y)) / (2 * h). - Return
(automatic, finite)and verify that both match the analytical derivatives at two distinct parameter settings.
Stage 3 — Compiled training
Implement train(params, x, y, rate=0.15, steps=200) in projects/regression-audit/starter/model.py:
- Validate that
steps >= 1(raiseValueErrorotherwise). - Define a pure scan body
step(p, _)that evaluatesvalue, gradients = jax.value_and_grad(loss)(p, x, y), updates the parameter pytree withjax.tree.map(lambda v, g: v - rate * g, p, gradients), and returns(updated, value). - Run
jax.lax.scan(step, params, None, length=steps)to recoverweight = 2.0andbias = 1.0while returning(fitted, history)with shape(steps,).
Stage 4 — Training audit
Implement report(params, x, y, rate=0.15, steps=200) in projects/regression-audit/starter/model.py:
- Call
fitted, history = train(params, x, y, rate, steps). - Evaluate held-out points
heldout_x = jnp.array([-0.8, 0.2, 0.8])andheldout_y = 2 * heldout_x + 1without mutatingfitted. - Return a dictionary containing
'parameters','initial_loss','training_loss','heldout_loss','finite','steps','learning_rate', and'backend'. The stage check verifies held-out fit, deterministic replay, and deliberate divergence at an unstable learning rate (rate=1.1).
Try your implementation before reading projects/regression-audit/solution/model.py. Verify the reference separately with python3 projects/regression-audit/tests/check.py --implementation solution --stage all; reference success does not grade your work.
Portfolio evidence
Keep your implementation in projects/regression-audit/starter/model.py, the commands, environment details, passing stage output, learning curve, unstable-rate failure run, and a brief explanation of what the checks establish. Passing public checks supports this bounded synthetic task; it is verified separately from production training, TPU operation, expert review, or career readiness.
Review handoff: explain a changed condition
Convert a target vector from shape (n,) to (n, 1) before subtraction. Predict the residual shape and explain why a decreasing broadcasted loss can still fit the wrong task.
Keep for review: A shape assertion before reduction, a hand-computed two-row loss, and the first incorrect intermediate. Restore one target per observation before comparing held-out predictions.
Write your prediction before opening the reference implementation. Distinguish what the existing checker verifies from your proposed extension; record any extension command, actual output and limitation separately. A reviewer should be able to locate the first failed contract from your saved evidence.