Code Index

August 18, 2026 ยท View on GitHub

This directory contains the companion code for each chapter of the course. It is recommended to first enter the code/ directory, then install dependencies and run scripts on a per-chapter basis.

Quick Start

python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate

# Full set of dependencies, suitable when working through the whole course
pip install -r requirements.txt

# Or install dependencies for just one chapter
pip install -r chapter01_cartpole/requirements.txt

Chapter 1 scripts accept --device auto|cuda|mps|cpu (auto prefers CUDA, then Apple MPS, then CPU). Shared helpers live in device_utils.py.

Chapter Code Overview

ChapterDirectoryMain CodeDescription
Ch01 CartPolechapter01_cartpole/1-ppo_cartpole.pySB3 PPO trains CartPole, logging SwanLab metrics
2-pytorch_ppo.pyPure PyTorch PPO: Actor-Critic, GAE, clipping, metric logging
plot_curves.pyPlots reviewable metric curves from the CSV exported by training
capture_frames.pyLoads a model and captures real Gymnasium rendered frames
Ch02 DPOchapter17_dpo/0-download_model.pyDownloads Qwen2.5-0.5B-Instruct
1-generate_data.pyGenerates preference data correcting excessive compliance
2-test_before.pyTest before fine-tuning
3-train_dpo.pyTRL DPO training
4-test_after.pyTest after fine-tuning
Ch03 MDPchapter03_mdp/two_armed_bandit.pyTwo-armed bandit policy comparison
bellman_equation_verify.pyNumerical verification of the Bellman equation
gridworld_q_learning.pyGridWorld Q-Learning and path visualization
Ch04 DQNchapter04_dqn/dqn_cartpole.pyDQN implemented from scratch to train CartPole
double_dqn_cartpole.pyDQN vs. Double DQN comparison
dqn_gym_sb3.pySB3 DQN trains CartPole, MountainCar, LunarLander, and other discrete environments, logging SwanLab and evaluation curves
dqn_atari_sb3.pySB3 DQN trains Atari for real, including wrapper, SwanLab, evaluation, and logging
export_dqn_curves.pyExports lecture figures from the Chapter 4 DQN eval CSV
dqn_pokemon_red_pyboy.pyPyBoy + SB3 DQN trains an early Pokemon exploration task
Ch05 Policy Gradientchapter05_policy_gradient/reinforce_cartpole.pyREINFORCE trains CartPole
reinforce_with_baseline.pyREINFORCE vs. baseline comparison
actor_critic_cartpole.pyActor-Critic and TD error
Ch07 PPOchapter07_ppo/ppo_lunar_lander.pySB3 PPO trains LunarLander-v3
ppo_from_scratch.pyPure PyTorch PPO
gae_visualization.pyGAE parameter visualization
Ch08 RLHFchapter08_rlhf/sft_pipeline.pySFT pipeline
reward_model_training.pyReward model training
rlhf_ppo_train.pySimplified PPO-RLHF training loop
chapter08_rlhf/verl_gsm8k/run_qwen2_5_0_5b_ppo_single_gpu.sh8.7 veRL + GSM8K external framework adaptation script
Ch09 Alignmentchapter09_alignment/dpo_hands_on.pyDPO alignment and beta comparison
dpo_math_reward.pyDPO experiments on math preference data
Ch09 GRPO/RLVRchapter09_grpo_rlvr/grpo_mechanism.pyGRPO mechanism demonstration
grpo_math_reasoning.pySmall GRPO experiment on math reasoning
rule_based_reward.pyRule-based reward function
Ch09 Continuous Controlchapter09_continuous_control/sac_halfcheetah.pySAC trains HalfCheetah-v4
ppo_td3_sac_comparison.pyPPO, TD3, SAC comparison
Ch10 Agentic RLchapter10_agentic_rl/tool_use_agent.pyTool selection policy training
multi_turn_rl.pyMulti-turn interaction credit assignment
generate_synthetic_data.pySynthetic trajectory data
mini_deep_research_grpo.pyMini Deep Research GRPO example
Ch11 VLM RLchapter11_vlm_rl/geometry_counting_dataset.pyGeometry counting dataset
multi_modal_reward.pyMulti-modal rule-based reward
vlm_grpo_train.pyVLM GRPO training example
Ch12 Future Trendschapter12_future_trends/tree_of_thought.pyTree of Thought search demonstration
multi_agent_marl.pyMulti-agent GridWorld
Appendix Pitfallsappendix_common_pitfalls/debug_reward_hacking.pyReward hacking reproduction
debug_training_collapse.pyTraining collapse diagnosis

Notes

  • The requirements.txt in each chapter directory contains the minimal dependencies for that chapter.
  • LLM-related chapters use small models by default, but running in a GPU environment is still recommended.
  • Some scripts will generate output/, model weights, or image files in the current working directory.
  • The Chapter 4 DQN scripts use SwanLab local mode by default; after running, you can view the curves with swanlab watch swanlog.