KTO (Kahneman-Tversky) Loss
Background
KTO (Kahneman-Tversky Optimisation) is an alignment loss that — unlike DPO — does not require paired preferences. Each example is labelled independently as desirable or undesirable. The objective is informed by prospect theory's gain/loss asymmetry: pull desirables above a reference threshold, push undesirables below.
Problem statement
Implement kto_loss(rewards, is_desirable, kl_ref, beta=0.1) returning the mean loss:
where is kl_ref (the reference KL term). Use the numerically stable log-sigmoid identity -logaddexp(0, -x).
Input
rewards— 1-D array of floats, length . Per-example reward =log_policy − log_ref.is_desirable— 1-D array of bool, length .kl_ref—float, the reference KL value .beta—float, temperature; default .
Output
Returns a Python float.
Examples
Example 1 — desirable with high reward → low loss
Input: rewards=[10.0], is_desirable=[True], beta=0.1
Output: small positive value (sigmoid(1.0) ≈ 0.73 → -log ≈ 0.31)
Example 2 — desirable with low reward → higher loss
Input: rewards=[-10.0], is_desirable=[True]
Output: significantly higher than Example 1
Example 3 — undesirable mirror case
Undesirable + very-positive reward gets penalised hard.
Constraints
- Length mismatch raises ValueError.
is_desirableshould be boolean (or 0/1).- Returns a plain Python
float.
Notes
- No pairs needed. DPO requires
(chosen, rejected)pairs from the same prompt; KTO needs only a single per-example label. Makes data collection much easier. - The role of . acts as a learned (or fixed) reference threshold. Setting near the typical reward keeps desirable and undesirable gradients balanced.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference desirable high reward low loss
- •Example undesirable high reward high loss
- •Sample mixed batch matches reference