Skip to content

Language models, retrieval, prompting, and language adaptation · Instruction, preference, and parameter-efficient adaptation · LNG063 · Training method

InstructGPT / RLHF pipeline

Aligns a pretrained language model with human demonstrations and preference comparisons through supervised fine-tuning, reward modeling, and reinforcement learning.

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Aligns a pretrained language model with human demonstrations and preference comparisons through supervised fine-tuning, reward modeling, and reinforcement learning. The stages are distinct and need separate datasets and validation.

Question / objective

Can preference judgments improve generated responses beyond supervised demonstrations?

Architecture / computational structure

A policy, reward model, and reference policy occupy distinct roles; the reward model scores outputs and the policy is optimized against it. Learned policy/reward weights differ from fixed reference weights and optimization controls.

Assumptions

Human ratings are noisy and context-dependent; reward models can be exploited.

Formula / computational definition

LR=−log⁡σ(r(x,yw)−r(x,yl)),J=Eπθr−βKL⁡(πθ∥πref).\mathcal L_R=-\log\sigma(r(x,y_w)-r(x,y_l)),\qquad J=\mathbb E_{\pi_\theta}r-\beta\operatorname{KL}(\pi_\theta\|\pi_{\mathrm{ref}}).

A reward model often uses -log sigma(r(x,y_w)-r(x,y_l)) on preferred/rejected responses. Policy optimization maximizes expected reward while penalizing divergence from a reference policy, commonly through a KL term weighted by beta.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Demonstration quality, preference rubric, reward model, KL coefficient, policy optimizer, rollout budget, response length, and reward normalization.

Fitting, fine-tuning, and adaptation

SFT on demonstrations, fit reward preferences, then optimize policy with an explicit reference constraint. Full or adapter updates are possible if all trainable/frozen components are recorded. Keep independent preference and task evaluations, audit reward hacking, and monitor capability forgetting and policy divergence.

How to use and calibrate

Use held-out human comparisons, reward-model calibration, independent task/factuality tests, capability-retention checks, and adversarial evaluation. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Improve helpful response style using a clearly specified human preference rubric.

Interpretation / pitfalls

Watch out

Improved preference scores do not prove factuality, fairness, or universal alignment, and optimizing one rubric may degrade other capabilities. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: TRL supplies documented components for reward modeling and policy training, but exact historical reproduction requires the full original data and recipe. R bridges Python with reticulate. The native example calculates a pairwise reward loss and a KL-regularized utility, not full RLHF.

Example scope: RLHF pairwise reward loss and a discrete KL-regularized policy utility; no policy optimizer.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — RLHF pairwise reward loss and a discrete KL-regularized policy utility; no policy optimizer.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
p, reference = [0.7,0.3], [0.5,0.5]
kl = sum(a*math.log(a/b) for a,b in zip(p,reference))
reward_loss = -math.log(sigmoid(1.2-0.4))
utility = dot(p,[1.0,0.2])-0.1*kl
print(reward_loss,kl,utility)
R

Component demo, not a trained model — RLHF pairwise reward loss and a discrete KL-regularized policy utility; no policy optimizer.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
p <- c(0.7,0.3); reference <- c(0.5,0.5)
kl <- sum(p*log(p/reference))
reward_loss <- -log(plogis(1.2-0.4))
utility <- sum(p*c(1,0.2))-0.1*kl
print(c(reward_loss=reward_loss,kl=kl,utility=utility))

Sources

From the CodeSplash reference, entry LNG063.