Como os LLMs são treinados
Aprenda as etapas do treinamento de LLM: pré-treinamento em grandes coleções de textos, ajuste fino supervisionado e RLHF, além de entender por que cada etapa é importante para o comportamento do modelo.
Como os LLMs são treinados é uma aula grátis de AI Engineering Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de AI Engineering Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de AI Engineering Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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.3567Emergent 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 onRLHF 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. 💡
Perguntas Frequentes
A aula “Como os LLMs são treinados” é grátis?
Sim — o texto completo de “Como os LLMs são treinados” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de AI Engineering Academy, atualize para CoddyKit PRO. O curso de AI Engineering Academy inclui 4 aulas no total.
O que vou aprender em “Como os LLMs são treinados”?
Aprenda as etapas do treinamento de LLM: pré-treinamento em grandes coleções de textos, ajuste fino supervisionado e RLHF, além de entender por que cada etapa é importante para o comportamento do mod… Você pratica AI Engineering Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar AI Engineering Academy?
Nenhuma experiência prévia é necessária. AI Engineering Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Como os LLMs são treinados”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de AI Engineering Academy?
Sim. Cada aula de AI Engineering Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Do preenchimento automático ao ChatGPT
- Transformadores e atenção em linguagem simples
- Como os LLMs são treinados
- Capacidades e limitações dos LLMs