All projectsProject guide · CPU

Train and audit a policy from interaction

Extract the project ZIP and work from its top folder, which contains requirements-cpu.txt and projects/policy-evaluation. A full checkout is optional. Copy the starter scaffold projects/policy-evaluation/starter/model.py to projects/policy-evaluation/my_model.py, implement the stage functions in projects/policy-evaluation/my_model.py, and verify each stage with python3 projects/policy-evaluation/tests/check.py --implementation projects/policy-evaluation/my_model.py --stage 1 (through --stage 4).

Build a complete, small RL experiment: functional environment, stochastic trajectories, policy updates and held-out evaluation. A successful artifact trains policies from fresh interaction rather than optimizing a fabricated surrogate alone.

This is a CPU teaching project with an enumerable corridor, not a control benchmark. It implements a tabular REINFORCE agent and a clipped PPO-style policy-gradient agent without a critic. The environment and exact evaluator make mistakes inspectable before introducing larger neural agents.

Start

Use Python and the pinned requirements-cpu.txt from the course workspace or project download. From the extracted top-level folder:

# Run run command in terminal using the course Python environment
python3 -m pip install -r requirements-cpu.txt
cp projects/policy-evaluation/starter/model.py projects/policy-evaluation/my_model.py
python3 projects/policy-evaluation/tests/check.py --stage 1 --implementation projects/policy-evaluation/my_model.py

PowerShell preparation:

# Copy the starter template into your editable workspace file
Copy-Item projects/policy-evaluation/starter/model.py projects/policy-evaluation/my_model.py

Edit my_model.py. Checks are cumulative: choose stages 1 through 4. The reference implementation is separate in solution/model.py. Run it with --implementation solution; reading it is optional, and copying it does not demonstrate your own understanding.

Contract

Stage 1 — Test the environment before learning

Implement reset and step. Enumerate moves at both boundaries, a timeout, success at the deadline and repeated calls after finishing. Check the final observation survives; do not replace it with a reset observation. Save a hand-worked transition trace.

# Run run command in terminal using the course Python environment
python3 projects/policy-evaluation/tests/check.py --stage 1 --implementation projects/policy-evaluation/my_model.py

Stage 2 — Collect genuine trajectories

Implement stable log_probability, rollout and returns_to_go. Split keys across reset, time and environments. Use vmap within scan; preserve fixed carry shapes. Record observation, action, reward, active, terminated, truncated and old_logp. The checker reconstructs trajectories with independent Python rules and changes batch sizes, horizons and seeds.

# Run run command in terminal using the course Python environment
python3 projects/policy-evaluation/tests/check.py --stage 2 --implementation projects/policy-evaluation/my_model.py

Keep an active-count plot and explain why a fixed array can contain fewer live episodes at later times. Show that masking with the next-state done flag incorrectly discards the successful action.

Stage 3 — Derive and check objectives

Implement policy_loss and exact_return. REINFORCE weights log probabilities by frozen reward-to-go. The clipped objective uses the minimum of unclipped and clipped ratio-weighted targets, with frozen behavior log probabilities. Normalize the action sum by episode count. Work through both positive and negative targets.

# Run run command in terminal using the course Python environment
python3 projects/policy-evaluation/tests/check.py --stage 3 --implementation projects/policy-evaluation/my_model.py

The checks compare dynamic programming to a separate recursive enumeration, compare exact gradients to central differences, and compare a large sampled REINFORCE gradient to that exact gradient. They also verify that old log probabilities and return targets receive zero gradient.

Stage 4 — Train, evaluate and report

Implement train and evaluate. Every update collects fresh episodes with a new key. REINFORCE takes one gradient step per fresh batch; the clipped method takes three steps while preserving its behavior probabilities. Return the complete exact-return history including the initial policy.

# Run run command in terminal using the course Python environment
python3 projects/policy-evaluation/tests/check.py --stage 4 --implementation projects/policy-evaluation/my_model.py

The public checks run both methods across three training seeds. Your report should use at least five training seeds and four reserved evaluation streams. Save policy parameters, raw episode returns, source revision, package versions, settings and per-agent means. Compare uniform, always-left and always-right policies. Distinguish variability across trained agents from evaluation sampling noise. Shared evaluation streams create paired comparisons; do not treat every episode as an independent training run.

Reference evidence and limits

validation.json contains source hashes, environment and six executed reference training/evaluation results. In the recorded CPU environment the uniform exact return is 0.466875; the always-right ceiling is 0.985. Reference policies improve, but the public fixture is not a hidden exam or professional credential.

No GPU/TPU performance, general control capability, production PPO equivalence or independent learner review is claimed. PPO clipping is not a hard trust region or a guarantee of monotonic return. The project intentionally omits critic fitting, generalized advantage estimation, neural observation encoders and continuous action distributions. Those are extensions requiring new contracts and checks.

Synthesis assessment

After the project, use the RL synthesis questions and reviewer guide under assessments/rl.md and assessments/rl-reviewer.md in the complete course workspace. Explain one failure and its repair; passing public assertions alone is insufficient evidence of understanding.

Build and check your implementation