YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
- π Transformer From Scratch (PyTorch)
- ποΈ Overall Architecture
- π§© Transformer Components
- βοΈ Encoder Block
- βοΈ Decoder Block
- π Project Structure
- β‘ Model Configuration
- π Quick Start
- π Forward Pass
- ποΈ Training
- π What You'll Learn
- π§ Future Improvements
- π Reference Paper
- β Support
- π License
π Transformer From Scratch (PyTorch)
A complete implementation of the Transformer architecture from the paper Attention Is All You Need, built entirely with PyTorch.
π Overview
The Transformer changed Natural Language Processing by replacing recurrent networks with self-attention, allowing models to process entire sequences in parallel.
This repository implements every major component from scratch without using torch.nn.Transformer.
It is designed for:
- π Students learning Transformers
- π¨βπ» Deep Learning practitioners
- π¬ AI researchers
- πΌ Interview preparation
- π Building custom NLP models
β¨ Features
- Token Embeddings
- Sinusoidal Positional Encoding
- Multi-Head Self Attention
- Masked Multi-Head Attention
- EncoderβDecoder Attention
- Position-wise Feed Forward Network
- Residual Connections
- Layer Normalization
- Stacked Encoder Layers
- Stacked Decoder Layers
- Final Vocabulary Projection
ποΈ Overall Architecture
flowchart TD
A[Source Tokens]
B[Embedding]
C[Positional Encoding]
D["Encoder Γ N"]
E[Encoder Memory]
F[Target Tokens]
G[Embedding]
H[Positional Encoding]
I["Decoder Γ N"]
J[Linear Layer]
K[Vocabulary Probabilities]
A --> B --> C --> D --> E
F --> G --> H --> I
E --> I
I --> J --> K
π§© Transformer Components
graph TD
Transformer
Transformer --> Embedding
Transformer --> PositionalEncoding
Transformer --> Encoder
Transformer --> Decoder
Transformer --> Linear
Encoder --> MultiHeadAttention
Encoder --> FeedForward
Encoder --> LayerNorm
Decoder --> MaskedAttention
Decoder --> CrossAttention
Decoder --> FeedForward2
Decoder --> LayerNorm2
βοΈ Encoder Block
Each encoder layer consists of:
Input
β
βΌ
Multi-Head Self Attention
β
Add & LayerNorm
β
Feed Forward Network
β
Add & LayerNorm
β
Output
βοΈ Decoder Block
Each decoder layer consists of:
Input
β
βΌ
Masked Multi-Head Attention
β
Add & LayerNorm
β
Cross Attention
β
Add & LayerNorm
β
Feed Forward Network
β
Add & LayerNorm
β
Output
π Project Structure
transformer-from-scratch/
βββ model.py
βββ encoder.py
βββ decoder.py
βββ attention.py
βββ positional_encoding.py
βββ config.py
βββ train.py
βββ inference.py
βββ README.md
β
βββ notebooks/
β‘ Model Configuration
| Hyperparameter | Value |
|---|---|
| Encoder Layers | 6 |
| Decoder Layers | 6 |
| Attention Heads | 8 |
| Embedding Size | 512 |
| Feed Forward Size | 2048 |
| Maximum Sequence Length | 5000 |
π Quick Start
import torch
from model import Transformer
src = torch.randint(0, 10000, (64, 20))
tgt = torch.randint(0, 12000, (64, 15))
model = Transformer(
src_vocab_size=10000,
tgt_vocab_size=12000,
num_heads=8,
num_layers=6,
emb_dim=512,
nn_dim=2048
)
output = model(src, tgt)
print(output.shape)
Output
torch.Size([64, 15, 12000])
π Forward Pass
sequenceDiagram
participant Source
participant Encoder
participant Decoder
participant Output
Source->>Encoder: Source Tokens
Encoder->>Encoder: Self Attention
Encoder-->>Decoder: Encoder Memory
Decoder->>Decoder: Masked Self Attention
Decoder->>Encoder: Cross Attention
Decoder->>Output: Vocabulary Logits
ποΈ Training
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(
model.parameters(),
lr=1e-4
)
π What You'll Learn
After studying this repository, you'll understand:
- Self-Attention
- Multi-Head Attention
- Positional Encoding
- Encoder Architecture
- Decoder Architecture
- Residual Connections
- Layer Normalization
- Feed Forward Networks
- Sequence-to-Sequence Modeling
- Machine Translation Pipeline
π§ Future Improvements
- Greedy Decoding
- Beam Search
- Label Smoothing
- Learning Rate Scheduler
- Mixed Precision Training
- Flash Attention
- KV Cache
- Weight Sharing
- Byte Pair Encoding (BPE)
- Hugging Face Checkpoint Support
- ONNX Export
π Reference Paper
Attention Is All You Need
Ashish Vaswani et al.
NeurIPS 2017
β Support
If this project helped you understand Transformers, consider giving it a β on GitHub.
It helps others discover the project and motivates future improvements.
π License
Released under the MIT License.
- Downloads last month
- 4