Project Structure

We’ll implement an Encoder-Decoder Transformer including Embedding, Positional Encoding, Multi-Head Attention, and Feed Forward layers.

Core Implementation

class MultiHeadAttention(nn.Module):
    def __init__(self, d_model, n_heads):
        super().__init__()
        self.d_k = d_model // n_heads
        self.n_heads = n_heads
        self.q_linear = nn.Linear(d_model, d_model)
        self.out = nn.Linear(d_model, d_model)

Read Transformer Architecture Explained first for the theory, then follow along with the code. This is also a prerequisite for Building GPT From Scratch.