Apile un bloque de encoder Transformer
Atención, feedforward y normalizaciones
Apile un bloque de encoder Transformer es una lección gratuita de Deep Learning Academy en CoddyKit. Esta es la lección 4 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 Deep Learning Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Deep Learning Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
The Building Block
A transformer is just one encoder block repeated. Learn the block and you understand the whole tower, from tiny models to giant ones.
Two Sub-Layers
Each block has two parts: a multi-head attention sub-layer, then a small feedforward network. Both wrapped with residuals and normalization.
Attention First
The block starts with self-attention, letting every token mix in context from the rest of the sequence before any further processing.
attn_out, _ = self.attn(x, x, x)The Residual Connection
A residual adds the sub-layer's input back to its output. This shortcut lets gradients flow and keeps deep stacks trainable.
x = x + attn_outLayer Normalization
After adding the residual, layer norm rescales each token's features to a stable distribution, steadying training across layers.
x = self.norm1(x)The Feedforward Net
Next comes a position-wise feedforward network: expand to a wider hidden size, apply a nonlinearity, then project back down.
ff = nn.Sequential(nn.Linear(d, 4*d), nn.GELU(), nn.Linear(4*d, d))Per-Token Processing
The feedforward layer treats each token independently. Attention shared information; this step refines each token on its own.
Second Residual and Norm
The feedforward output gets the same treatment: a residual add plus another layer norm, finishing the block.
x = self.norm2(x + ff(x))Stack Them Deep
Stack many identical blocks and the model builds richer representations layer by layer. Depth is where transformer power comes from.
layers = nn.ModuleList([Block(d) for _ in range(N)])Pre-Norm vs Post-Norm
Many modern models apply layer norm before each sub-layer instead of after. Pre-norm trains more stably in very deep stacks.
Use the Built-in
PyTorch gives you nn.TransformerEncoderLayer and nn.TransformerEncoder, so you can assemble a full stack in just a couple of lines.
layer = nn.TransformerEncoderLayer(d_model, nhead)
enc = nn.TransformerEncoder(layer, num_layers=6)Quick Check
Let's review the parts of an encoder block.
Recap
You assembled an encoder block: attention, residual, norm, feedforward, residual, norm. Stack it deep and you have a transformer. Amazing work!
Preguntas frecuentes
¿La lección «Apile un bloque de encoder Transformer» es gratis?
Sí — el texto completo de «Apile un bloque de encoder Transformer» 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 Deep Learning Academy, actualiza a CoddyKit PRO. El curso de Deep Learning Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Apile un bloque de encoder Transformer»?
Atención, feedforward y normalizaciones Practicas Deep Learning 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 Deep Learning Academy?
No se requiere experiencia previa. Deep Learning 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 4 de 4.
¿Cuánto tiempo toma la lección «Apile un bloque de encoder Transformer»?
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 Deep Learning Academy?
Sí. Cada lección de Deep Learning 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
- Self-attention: query, key y value
- Producto punto escalado y multi-head
- Codificación posicional para representar el orden
- Apile un bloque de encoder Transformer