๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #249

Transformer Encoder

This note zooms into one single Transformer encoder layer โ€” the repeated building block that, stacked \(N\) times, forms the full encoder stack from Transformer Architecture.

The Structure of One Encoder Layer

  1. Multi-head self-attention over the input
  2. Add the attention output back to the original input (a residual connection), then layer-normalize
  3. A position-wise feed-forward network
  4. Add the feed-forward output back to its input (another residual connection), then layer-normalize again
\[ \mathbf{z} = \text{LayerNorm}(\mathbf{x} + \text{MultiHeadSelfAttention}(\mathbf{x})) \] \[ \text{output} = \text{LayerNorm}(\mathbf{z} + \text{FeedForward}(\mathbf{z})) \]

This exact pattern โ€” sublayer, then add-and-normalize โ€” repeats twice per encoder layer, and the whole layer repeats \(N\) times (commonly 6 or 12 in published architectures) to form the full encoder stack.

Why Self-Attention Here, Not Cross-Attention

The encoder's job is purely to build a rich representation of the input sequence โ€” every position needs to gather relevant context from every other position within that same input. This is exactly self-attention's job (see Self-Attention), not cross-attention, since there's no separate second sequence involved yet at this stage โ€” cross-attention only enters the picture in the decoder, where it lets the decoder look back at this encoder's finished output.

Diagram โ€” One Encoder Layer

Input x Multi-Head Self-Attention Add (residual) & Norm Feed-Forward Network Add (residual) & Norm

One complete encoder layer โ€” this exact block is stacked N times to form the full encoder.

Code

import torch
import torch.nn as nn

encoder_layer = nn.TransformerEncoderLayer(
    d_model=512, nhead=8, dim_feedforward=2048, batch_first=True
)
encoder = nn.TransformerEncoder(encoder_layer, num_layers=6)   # 6 stacked identical layers

x = torch.randn(1, 10, 512)   # 10 tokens, 512-dim embeddings
encoder_output = encoder(x)
print(encoder_output.shape)   # (1, 10, 512) -- same shape as input; every position now carries
                                # rich context gathered from every other position

Why the Output Shape Matches the Input Shape

Every layer in the encoder preserves the input's shape exactly โ€” this is by design, since it allows the same layer structure to be stacked \(N\) times without any dimension mismatches, and it means the final encoder output has one vector per input token, each vector now enriched with context gathered from the entire sequence via self-attention.

Common Mistakes

  • Assuming information only flows "forward" through the stack in a strictly hierarchical way, like a CNN's spatial feature hierarchy โ€” each encoder layer's self-attention can relate any two positions directly, at every layer, not just progressively wider receptive fields the way stacked convolutions do.
  • Forgetting that all \(N\) encoder layers have independently learned weights โ€” they share the same structure, but each layer learns its own distinct parameters.

Interview Relevance

Q: "What are the two main sublayers inside one Transformer encoder layer, and what role does each play?" Multi-head self-attention, which lets every position gather relevant context from every other position in the input; and a position-wise feed-forward network, which applies an additional non-linear transformation independently to each position. Both sublayers are wrapped with a residual connection and layer normalization.

Practice Question

If an encoder has 6 layers and each attends over the full input sequence, does layer 3 only see information gathered by layer 2, or can it directly relate any two original input positions on its own?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Transformer Encoder โ€“ FAQs

Quick answers about learning Transformer Encoder in Deep Learning.

This free note from CodingNow 2.0 explains Transformer Encoder in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Transformer Encoder, is 100% free with no signup required.
With focused practice, most students grasp Transformer Encoder in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now