Coders In Hoods
  • Courses
  • Roles
  • Blog
  • Setup

Coders In Hoods AI Academy

Every course is free. Sign in to start, and your progress, quiz scores and bookmarks follow you.

AI Academy

  • Courses
  • Paths
  • Setup guides
  • How we teach

Role roadmaps

  • AI Engineer
  • Forward Deployed Engineer
  • Machine Learning Engineer
  • All roles

Coders In Hoods

  • The agency
  • Coaching for teams
  • Blog
  • LinkedIn
  • X (Twitter)

Contact

codersinhoods@gmail.com

© Coders In Hoods · since 2019

Course overview

Fine-Tuning and RL for Agents

Free · sign in to start and track progress

1 · Decide and prepare

  • 1Prompt, optimise or train?
  • 2Training data from traces

2 · Supervised and preference fine-tuning

  • 3SFT with LoRA on a free GPU
  • 4Preference optimisation

    Exercise answers

    • Answer: DPO the free-text fields with on-policy pairs
    • Answer: KTO from thumbs, against DPO on the same budget

3 · Reinforcement learning for agents

  • 5GRPO in practice
  • 6Designing rewards
  • 7Multi-turn RL environments
  • 8Reward hacking, regressions and shipping
CapstoneTrain a triage model, then decide whether to ship itReference implementation

Module 2 · Supervised and preference fine-tuning · Lesson 4

Preference optimisation

When you have comparisons or thumbs rather than gold outputs, train on them with DPO, KTO, ORPO or SimPO, build the pairs from your own model's samples ranked by a calibrated open-weight judge, and catch the model when it learns to please the judge instead of the user.

80 min 2 videos 3 exercises 7-question quiz Answers
Setup for the exercises:Install Claude CodePython with uvFree GPUs on KaggleHugging Face account and CLIGet an OpenRouter key

By the end of this lesson you can

  • Build on-policy preference pairs from a fine-tuned model's own samples, using code checks first and a calibrated open-weight judge in both orders for the rest
  • Turn binary thumbs-up and thumbs-down signals into KTO training data, correcting the class imbalance that selective feedback creates
  • Choose between DPO, KTO, ORPO and SimPO from the shape of the data, the need for a reference model and the risk of length bias, and set beta and the learning rate
  • Read implicit-reward margins, reward accuracy and log-probabilities during training, and explain why the loss and the margin disagree
  • Detect length and judge exploitation after training with paired, length-controlled comparisons and a fresh check of the judge against your own labels

On this page

  • Intuition: an editor's red and green pens
  • DPO and its siblings
  • Pairs from your own model's samples
  • Calibrate the judge before it labels anything
  • Thumbs instead of pairs: KTO
  • Setting beta and the learning rate
  • Reading a preference run
  • Did it help, or did it learn to please the judge?
  • When to reach for RL instead
  • Connections
  • Summary
  • Sources & further reading
  • Quiz

In this lesson

  1. 01Intuition: an editor's red and green pens
  2. 02DPO and its siblings
  3. 03Pairs from your own model's samples
  4. 04Thumbs instead of pairs: KTO
  5. 05Setting beta and the learning rate
  6. 06Reading a preference run
  7. 07Did it help, or did it learn to please the judge?
  8. 08When to reach for RL instead
  9. 09Connections
  10. 10Summary

Sources & further reading

  • KTO: Model Alignment as Prospect Theoretic OptimizationPaper · Ethayarajh, Xu, Muennighoff, Jurafsky, Kiela · 2024Preference training from a binary desirable or undesirable signal per output, no pairs needed. Read the abstract and the section on the loss; TRL's KTO docs give the weighting rule for imbalanced data.
  • SimPO: Simple Preference Optimization with a Reference-Free RewardPaper · Meng, Xia, Chen · 2024NeurIPS 2024. Uses the length-normalised average log-probability as the reward and a target margin, so no reference model is needed. The authors' repository adds the practical warning that a large learning rate damaged JSON output.
  • ORPO: Monolithic Preference Optimization without Reference ModelPaper · Hong, Lee, Thorne · 2024EMNLP 2024. Folds preference learning into SFT with an odds-ratio penalty on the disfavoured response, in one stage and with no reference model.
  • Preference Fine-Tuning of LLMs Should Leverage Suboptimal, On-Policy DataPaper · Tajwar, Singh, Sharma, Rafailov et al. · 2024ICML 2024. The evidence behind sampling pairs from your own policy - approaches that use on-policy sampling or push down the likelihood of bad responses beat offline maximum-likelihood objectives.
  • Disentangling Length from Quality in Direct Preference OptimizationPaper · Park, Rafailov, Ermon, Finn · 2024ACL 2024. Shows DPO exploits the verbosity bias in preference data and proposes a length-regularised variant. The reason to measure length after every preference run.
  • DPO TrainerDocs · Hugging FaceTRL v1.17.1. The loss, the alternative loss types (including a length-normalised one), how a PEFT model becomes its own reference, and the definitions of every logged reward and log-probability metric.
Free · sign in to start

Sign in to keep reading.

Free, no card. We use your account to save progress, quiz scores, bookmarks and occasionally send updates from our team.

  • 10 more sections
  • 3 hands-on exercises
  • A 7-question quiz
  • Progress, quiz scores and bookmarks