liveBook · Manning

Reinforcement learning

As explained in Deep Learning with R, Third Edition

Reinforcement learning is a machine-learning technique in which an agent learns behavior through interaction with an environment and feedback such as rewards.

Reinforcement learning improves a model using feedback on what it generates. For a language model, the model produces a response, receives a signal about its quality, and uses that signal to guide further training. This is useful when writing every ideal response by hand would be too slow or expensive. section 16.4.1 section 16.4.4

Listing 16.15 Pseudocode for the simplest possible RLHF algorithm
for (prompts in dataset) {
  responses <- model$generate(prompts)  #1
  rewards <- reward_model |> predict(responses)  #2
  good_responses <- responses[rewards > cutoff]
  model |> fit(good_responses)  #3
}

This simplified RLHF loop generates responses, scores them with a reward model, and trains on responses above a cutoff. Practical methods use more complex updates that can also learn from lower-scoring responses. section 16.4.1 section 16.4.4

With human feedback, the quality signal can be learned from preferences:

  • Start with supervised fine-tuning on handwritten prompts and responses to establish a baseline. section 16.4.1
  • Ask people to rank several responses to the same prompt, then train a reward model to assign a score to a prompt–response pair. section 16.4.1
  • Use reward scores to guide further training toward preferred responses. section 16.4.1

Feedback without human rankings

A math problem can provide its own check. Generate several answers, mark each final answer with a predictable label, and check it against the known result. Correct responses—including their intermediate work—can become training examples. The same approach can test generated code by running it. Here, answer-checking acts as the environment and generated responses are the actions. section 16.4.4

Verifiable problems also offer a way to continue training on a model’s own output when high-quality human-written pretraining data is scarce. Neither human preferences nor automatic checks are a complete solution to the difficulty of training language models efficiently. section 16.5

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through a simplified generate-score-train loop and a verifiable-answer procedure, while leaving practical gradient updates unspecified.
Examples
Uses reward-filtering pseudocode, a labeled math answer, and executable code as examples of feedback.
What it assumes
Builds on prompts, generated responses, and supervised fine-tuning; introduces preference ranking and reward scores through a simple workflow.
What you can do after
Evaluate/understand it by tracing how generated responses receive preference scores or answer checks and feed into further training.
How it connects
Connects supervised fine-tuning, reward models, human preference ranking, chain-of-thought training, and code testing.

Also explained in

Compare how 18 books explain reinforcement learning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.