A minimal implementation of the Transformer architecture from scratch using PyTorch.
This project demonstrates the core concepts introduced in the groundbreaking paper:
Attention Is All You Need
The goal of this repository is to understand and implement the internal mechanics of transformers including attention, positional encoding, and encoder-decoder architecture.
Transformers are deep learning architectures designed to handle sequential data using attention mechanisms instead of recurrence or convolution.
They are widely used in:
- Natural Language Processing
- Large Language Models
- Vision Transformers
- Speech Recognition
- Multimodal AI
Popular models based on transformers include:
- GPT
- BERT
- T5
- Vision Transformers
Input Tokens
│
▼
Token Embedding
│
▼
Positional Encoding
│
▼
Transformer Encoder Layers
│
├── Multi-Head Attention
├── Feed Forward Network
├── Layer Normalization
└── Residual Connections
│
▼
Transformer Decoder Layers
│
├── Masked Multi-Head Attention
├── Encoder-Decoder Attention
├── Feed Forward Network
└── Layer Normalization
│
▼
Linear + Softmax
│
▼
Output Tokens
Transformers/
│
├── Transformers.ipynb # Full transformer implementation
├── README.md
└── images/
└── transformer_architecture.png
git clone https://github.com/solo938/Transformers.git
cd Transformerspip install torch numpy matplotlibjupyter notebook Transformers.ipynbOr run it in Google Colab.
This repository implements the following transformer components:
Transforms tokens into dense vector representations.
Adds position information to embeddings since transformers lack recurrence.
Allows the model to attend to different positions simultaneously.
Two fully connected layers applied to each token.
Stack of attention + feedforward blocks.
Adds masked attention and encoder-decoder attention.
Self-attention allows the model to determine which tokens are important relative to each other.
Core attention formula:
Attention(Q, K, V) = softmax(QKᵀ / √dₖ)V
Where:
- Q = Queries
- K = Keys
- V = Values
- dₖ = key dimension
Text Input
│
▼
Tokenization
│
▼
Embedding Layer
│
▼
Positional Encoding
│
▼
Encoder Stack
│
▼
Decoder Stack
│
▼
Linear Layer
│
▼
Softmax
│
▼
Next Token Prediction
You can extend this project by adding:
- Training loop
- Dataset loader
- Language modeling task
- Machine translation example
- Attention visualization
- Modular PyTorch implementation
- Integration with HuggingFace datasets
This transformer implementation can be adapted for:
- Language translation
- Text generation
- Question answering
- Chatbots
- Document summarization
Recommended papers and libraries to explore:
- Attention Is All You Need
- BERT: Pre-training of Deep Bidirectional Transformers
- GPT: Generative Pre-trained Transformer
- PyTorch
- Hugging Face Transformers
- TensorFlow
- Implement training loop
- Train on a small dataset
- Add attention visualization
- Convert notebook into modular code
- Build a mini GPT model
Contributions are welcome!
- Fork the repository
- Create a feature branch
- Commit your changes
- Submit a pull request
If you found this project helpful:
⭐ Star the repository
🍴 Fork it
🧠 Experiment with your own transformer architectures
MIT License