Calculus & Gradients interview question

Probability & MathCalculus & GradientsRLHF & Alignmenthard

In RLHF you need the gradient of J(θ) = E[R(y)], where y is a token sequence sampled from the policy LLM π_θ and R is a reward model's score treated as a black box. Which gradient estimator fits this setting, and why?

  1. AThe reparameterisation estimator: write each sampled token as y = μ_θ + σ_θ·ε with ε ∼ N(0, 1) and backpropagate through R, which gives lower-variance gradients than the score-function estimator.
  2. BThe score-function (REINFORCE) estimator E[R(y)·∇θ log π_θ(y)]: it needs only the rewards and log-probabilities of sampled sequences, never ∂R/∂y, which does not exist for discrete tokens.
  3. CThe score-function estimator written as E[∇θR(y)·π_θ(y)]: it differentiates the reward through each sampled token and weights the result by the sequence probability, which keeps it unbiased for discrete outputs.
  4. DNo unbiased gradient estimator exists here: a non-differentiable R makes every sampling-based estimate biased, so the policy can only improve by fine-tuning on its own highest-reward samples.
Show the answer and explanation

Correct answer: B, The score-function (REINFORCE) estimator E[R(y)·∇θ log π_θ(y)]: it needs only the rewards and log-probabilities of sampled sequences, never ∂R/∂y, which does not exist for discrete tokens.

Explanation Calculus & Gradients

The log-derivative (score-function) trick moves the gradient inside the expectation without ever differentiating R: ∇θ E[R(y)] = Σ_y R(y)·∇θ π_θ(y) = Σ_y R(y)·π_θ(y)·∇θ log π_θ(y) = E[R(y)·∇θ log π_θ(y)].

You sample sequences, score them, and weight each sequence's log-probability gradient (the sum of its per-token log-prob gradients) by its reward. R can be any black box: a reward model, a unit-test pass rate, a human rating. This is REINFORCE, the root of the policy-gradient methods used in RLHF.

Its weakness is variance. Because E[∇θ log π_θ(y)] = 0, subtracting a baseline b that does not depend on the sampled y (a learned value estimate, or the mean reward of the other samples drawn for the same prompt) keeps the estimator unbiased while reducing variance: E[(R(y) − b)·∇θ log π_θ(y)].

Score functionReparameterisation
Needs ∂R/∂ynoyes
Sample spacediscrete or continuouscontinuous, z = μ + σ·ε
Variancehigh, needs a baselineusually low
Typical useRL, RLHF, discrete latentsVAEs, continuous policies

Why the others fail: reparameterisation needs a differentiable path from θ through the sample to R, but tokens are discrete, so there is no y = μ + σ·ε form (Gumbel-softmax relaxations exist, but they are biased and still need a differentiable R). The formula with ∇θR(y) differentiates the wrong factor, since R does not depend on θ except through the sample. Score-function estimates are unbiased even with a black-box R, so fine-tuning on the best samples (rejection-sampling fine-tuning) is a useful complement, not the only option.

Interview tip: derive the trick in one line (∇π = π·∇log π) and name baselines or group-normalised rewards as the standard variance fix.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Probability & Math questions

  1. What is the difference between independent events and mutually exclusive events?
  2. What is the dot product of the vectors [1, 2, 3] and [4, 5, 6]?
  3. In the gradient descent update θ ← θ − η∇L(θ), what is the role of the learning rate η?
  4. An image-augmentation step applies a 2 × 2 linear map M to pixel coordinates. The plot shows the unit square (dashed) and its image under M (solid), with the…
  5. The table shows a telecom operator's customer mix and the average monthly revenue per user (ARPU) in each segment. What is the expected monthly revenue of a…