RLHF & Alignment
Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.
Setup
- Open a fresh terminal in a new folder, e.g.
mkdir lab-8 && cd lab-8. - Create a Python 3.11 venv:
python -m venv .venv && source .venv/bin/activate. - Install the dependencies noted on each starter snippet (the small pip install … line above the code).
- Copy each starter into a file with the suggested name, then run it and capture the output.
Starter code
from trl import DPOTrainer, DPOConfig
from datasets import load_dataset
ds = load_dataset("trl-lib/ultrafeedback_binarized", split="train[:5%]")
trainer = DPOTrainer(
model=model, ref_model=None, # ref_model=None → use base + adapter
args=DPOConfig(
output_dir="dpo-out",
beta=0.1, learning_rate=5e-7,
per_device_train_batch_size=2,
num_train_epochs=1, max_length=2048,
),
train_dataset=ds, tokenizer=tok,
)
trainer.train()python 08_01_lab.pydef rm_score(prompt, response, rm, tok):
x = tok(prompt + response, return_tensors="pt").to(rm.device)
with torch.no_grad():
return rm(**x).logits[0, 0].item()
print(rm_score("Q: 2+2?\n", "A: 4", rm, tok))python 08_02_lab.pyGuided steps
Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.
- 1DPO on UltraFeedback
Do — Run DPO on 5k preference pairs starting from a LoRA-SFT checkpoint. Train for 1 epoch.
Expected artifact — trainer logs showing reward margin increasing; win-rate vs base model >55% on 50 held-out prompts.
Run free: - 2Pairwise judge eval
Do — Use gpt-5-mini as a judge to compare base vs DPO model on 30 prompts.
Expected artifact — judge_results.json with per-prompt verdicts and an aggregate win-rate.
Run free: - 3Reward hacking probe
Do — Construct 5 prompts where your DPO model now gives sycophantic or evasive answers. Document them.
Expected artifact — reward_hacks.md listing prompts, model outputs, and what the model is gaming.
Run free:
Authoritative sources
Independent primary sources used to check and corroborate the guidance on this page.
- NIST National Initiative for Cybersecurity Education (NICE)
US national framework for cybersecurity work roles and competencies.
- Financial Action Task Force (FATF)
Global AML/CFT standards referenced by financial-crime curricula.
- FBI Internet Crime Complaint Center — Annual Reports
Primary loss and typology data used in fraud-investigation training.
- US Federal Trade Commission — Consumer Sentinel Data Book
Official complaint statistics for teaching fraud trend analysis.
