Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

🤖 Transformers From Scratch (PyTorch)

Python PyTorch Transformers License

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.


🧠 What is a Transformer?

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

🏗 Transformer Architecture Overview

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

📂 Project Structure

Transformers/
│
├── Transformers.ipynb      # Full transformer implementation
├── README.md
└── images/
    └── transformer_architecture.png

⚡ Quick Start

1️⃣ Clone the Repository

git clone https://github.com/solo938/Transformers.git

cd Transformers

2️⃣ Install Dependencies

pip install torch numpy matplotlib

3️⃣ Run the Notebook

jupyter notebook Transformers.ipynb

Or run it in Google Colab.


🧩 Components Implemented

This repository implements the following transformer components:

Token Embedding

Transforms tokens into dense vector representations.

Positional Encoding

Adds position information to embeddings since transformers lack recurrence.

Multi-Head Attention

Allows the model to attend to different positions simultaneously.

Feed Forward Network

Two fully connected layers applied to each token.

Encoder Layer

Stack of attention + feedforward blocks.

Decoder Layer

Adds masked attention and encoder-decoder attention.


🔑 Key Concepts

Self 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

📊 Transformer Pipeline

Text Input
     │
     ▼
Tokenization
     │
     ▼
Embedding Layer
     │
     ▼
Positional Encoding
     │
     ▼
Encoder Stack
     │
     ▼
Decoder Stack
     │
     ▼
Linear Layer
     │
     ▼
Softmax
     │
     ▼
Next Token Prediction

📈 Possible Improvements

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

🧪 Example Use Cases

This transformer implementation can be adapted for:

  • Language translation
  • Text generation
  • Question answering
  • Chatbots
  • Document summarization

📚 Learning Resources

Recommended papers and libraries to explore:

Papers

  • Attention Is All You Need
  • BERT: Pre-training of Deep Bidirectional Transformers
  • GPT: Generative Pre-trained Transformer

Libraries

  • PyTorch
  • Hugging Face Transformers
  • TensorFlow

🛣 Roadmap

  • Implement training loop
  • Train on a small dataset
  • Add attention visualization
  • Convert notebook into modular code
  • Build a mini GPT model

🤝 Contributing

Contributions are welcome!

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Submit a pull request

⭐ Support

If you found this project helpful:

⭐ Star the repository
🍴 Fork it
🧠 Experiment with your own transformer architectures


📜 License

MIT License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages