0Pricing
AI Engineering Academy · 课时

LLM 如何训练

学习 LLM 训练的各个阶段:在海量语料库上进行预训练、监督式微调以及 RLHF,并了解每个阶段为何会影响模型行为。

LLM 如何训练 是 CoddyKit 上的免费 AI Engineering Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Engineering Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Engineering Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Three Stages of LLM Training

LLMs are built in three stages: pre-training teaches language and knowledge, fine-tuning teaches instruction-following, and RLHF aligns the model with what people want.

Pre-Training: Next Token Prediction at Scale

Pre-training feeds the model tons of text with one job: predict the next token. That simple goal is enough to teach grammar, facts, and reasoning along the way.

Training Data Quality and Curation

Raw internet text is messy. Teams clean it with data pipelines that filter, deduplicate, and score quality. The rule of thumb: better data beats a bigger model.

The Loss Function and Optimization

Training minimizes cross-entropy loss — how far the model's guess is from the right next token. Tiny weight nudges, repeated billions of times. The code shows the math.

# Illustrative cross-entropy loss for a single token prediction
import math

vocab_size = 50000
correct_token_index = 4217  # index for the word 'Paris'

# Model output probabilities (softmax of logits)
model_probs = [0.00002] * vocab_size  # simplified uniform base
model_probs[correct_token_index] = 0.70  # model is 70% confident in 'Paris'

loss = -math.log(model_probs[correct_token_index])
print(f'Cross-entropy loss: {loss:.4f}')  # ~0.3567

Emergent Capabilities from Scale

At enough scale, new emergent capabilities appear — like step-by-step reasoning — that small models simply don't have. Nobody trained them in directly; scale brought them.

Supervised Fine-Tuning on Instruction Pairs

A raw model just continues text. Supervised fine-tuning trains it on (instruction, ideal answer) pairs, so it learns to actually answer instead of rambling on.

# Illustrative SFT data format
training_example = {
    'messages': [
        {'role': 'system', 'content': 'You are a helpful assistant.'},
        {'role': 'user', 'content': 'What is the capital of France?'},
        {'role': 'assistant', 'content': 'The capital of France is Paris.'}
    ]
}
# During SFT, loss is computed only on the assistant turn tokens
# The system and user tokens are provided as context but not trained on

RLHF Phase 1: Training the Reward Model

RLHF starts with a reward model. People pick the better of two answers, and the reward model learns to predict those preferences — a stand-in for human taste.

RLHF Phase 2: PPO Fine-Tuning

Next, PPO tunes the LLM to score higher on the reward model. A KL penalty keeps it from drifting weird and gaming the reward instead of truly helping.

Direct Preference Optimization: Simpler RLHF

PPO is complex. DPO is a simpler alternative: it trains the model straight from preferred-vs-rejected answer pairs — no separate reward model needed.

Chinchilla Scaling Laws: Compute-Optimal Training

The Chinchilla finding: older models were undertrained. For a given budget, scale data and size together — roughly 20 tokens per parameter for the best results.

What Each Training Stage Affects

Each stage controls something: pre-training sets what it knows, fine-tuning sets how it behaves, and RLHF sets its values. That tells you which part to fix.

Quick Check

Test your understanding of AI Engineering concepts from this lesson.

Lesson Recap

Recap: pre-training builds knowledge, fine-tuning builds instruction-following, and RLHF or DPO aligns the model with human values. Next: what LLMs can and can't do. 💡

常见问题解答

「LLM 如何训练」课时是免费的吗?

是的 — 「LLM 如何训练」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Engineering Academy 课程的其余内容,请升级到 CoddyKit PRO。 AI Engineering Academy 课程共包含 4 节课。

「LLM 如何训练」这节课中我会学到什么?

学习 LLM 训练的各个阶段:在海量语料库上进行预训练、监督式微调以及 RLHF,并了解每个阶段为何会影响模型行为。 你通过在浏览器中直接运行的动手代码来练习 AI Engineering Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AI Engineering Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AI Engineering Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「LLM 如何训练」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AI Engineering Academy 课中编写并运行代码吗?

能。每节 AI Engineering Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 从自动补全到 ChatGPT
  2. 用通俗语言理解 Transformer 与注意力机制
  3. LLM 如何训练
  4. LLM 的能力与局限
← 返回 AI Engineering Academy