Cómo se entrenan los LLM
Aprenderá las etapas del entrenamiento de un LLM: preentrenamiento con corpus masivos, fine-tuning supervisado y RLHF, así como la importancia de cada etapa para el comportamiento del modelo.
Cómo se entrenan los LLM es una lección gratuita de AI Engineering Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de AI Engineering Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de AI Engineering Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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. 💡
Preguntas frecuentes
¿La lección «Cómo se entrenan los LLM» es gratis?
Sí — el texto completo de «Cómo se entrenan los LLM» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de AI Engineering Academy, actualiza a CoddyKit PRO. El curso de AI Engineering Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Cómo se entrenan los LLM»?
Aprenderá las etapas del entrenamiento de un LLM: preentrenamiento con corpus masivos, fine-tuning supervisado y RLHF, así como la importancia de cada etapa para el comportamiento del modelo. Practicas AI Engineering Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar AI Engineering Academy?
No se requiere experiencia previa. AI Engineering Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Cómo se entrenan los LLM»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de AI Engineering Academy?
Sí. Cada lección de AI Engineering Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Del autocompletado a ChatGPT
- Transformers y attention en lenguaje sencillo
- Cómo se entrenan los LLM
- Capacidades y limitaciones de los LLM