Skip to content

Latest commit

 

History

27 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Transformers From First Principles

A Complete Mathematical Implementation of the Transformer Architecture Using Only NumPy

No PyTorch.No TensorFlow.No Hidden Abstractions.
Just Mathematics → NumPy → Transformer.


Python NumPy Transformer Documentation License


Transformers From First Principles is an educational repository that explains the complete Transformer architecture by implementing every major component from scratch using only NumPy.

Unlike most repositories that rely on deep learning frameworks, this project focuses on how Transformers actually work internally, translating the mathematical equations directly into NumPy code.

Whether you're a student, researcher, or ML enthusiast, this repository is designed to help you understand the mechanics behind modern Transformer models.


Why This Repository?

Most tutorials teach how to use Transformers.

Very few teach how they work.

This repository bridges that gap by providing:

  • Beginner-friendly documentation
  • Mathematical derivations with equations
  • Pure NumPy implementation
  • Complete Encoder & Decoder implementation
  • Manual forward and backward propagation
  • Structured learning roadmap
  • Visual explanations with diagrams
  • Modular and easy-to-follow source code

Repository Goals

This project aims to help readers:

  • Understand every component of the Transformer architecture.
  • Translate mathematical equations into NumPy code.
  • Learn how forward and backward propagation work internally.
  • Explore Encoder, Decoder, and Attention mechanisms.
  • Gain intuition behind modern Large Language Models.

Learning Roadmap

flowchart TD

subgraph Level_1["Level 1 — Foundations"]
A["Introduction"]
B["Why Transformers"]
C["Matrix Multiplication"]
end

subgraph Level_2[" Level 2 — Input Representation"]
D["Embeddings"]
E["Positional Encoding"]
end

subgraph Level_3[" Level 3 — Attention Mechanism"]
F["Attention"]
G["Scaled Dot-Product"]
H["Multi-Head Attention"]
end

subgraph Level_4[" Level 4 — Transformer Blocks"]
I["Residual + LayerNorm"]
J["Feed Forward Network"]
K["Encoder"]
L["Decoder"]
end

subgraph Level_5[" Level 5 — Complete Architecture"]
M["Full Transformer"]
N["Training"]
O["GPT vs BERT vs Transformer"]
end

A --> B --> C --> D --> E --> F --> G --> H --> I --> J --> K --> L --> M --> N --> O
Loading

Repository Structure

A complete explanation of every file is available in :

REPOSITORY_GUIDE.md


Documentation

The documentation gradually builds your understanding of the Transformer architecture from basic concepts to the complete model.

Topics include:

  • Introduction
  • Why Transformers
  • Matrix Multiplication
  • Embeddings
  • Positional Encoding
  • Attention
  • Scaled Dot-Product Attention
  • Multi-Head Attention
  • Residual Connections & LayerNorm
  • Feed Forward Network
  • Encoder
  • Decoder
  • Full Transformer
  • Training
  • GPT vs BERT vs Transformer

Source Code

The implementation is organized into modular components.

Basic neural network layers implemented using NumPy.

  • Linear Layer

  • Embedding Layer

  • Layer Normalization

  • Positional Encoding

  • Softmax

  • ReLU

  • attention/

Attention mechanisms.

  • Self Attention

  • Multi Head Attention

  • Masked Attention

  • Cross Attention

  • Scaled Dot Product Attention

  • encoder/

Complete Encoder implementation.

Complete Decoder implementation.

Complete Encoder–Decoder Transformer architecture.


Quick Start

Clone the repository

git clone https://github.com/KARTHIK1749/transformers-from-first-principles.git

Move into the project

cd transformers-from-first-principles

Install dependencies

pip install -r requirements.txt

Who Is This Repository For?

This repository is intended for:

  • Students learning Deep Learning
  • Machine Learning enthusiasts
  • AI Researchers
  • Interview preparation
  • Anyone curious about Transformer internals
  • Developers who want to understand what happens under the hood of modern frameworks

Roadmap

Current progress can be found in :

ROADMAP.md

Upcoming improvements include:

  • Vision Transformer (ViT)
  • Rotary Positional Embeddings (RoPE)
  • Flash Attention
  • KV Cache
  • Tiny GPT implementation
  • Additional worked mathematical examples

Contributing

Contributions are always welcome!

If you'd like to improve the repository, fix bugs, enhance documentation, or add educational examples, please read

CONTRIBUTION.md

before opening a Pull Request.


Author

Karthik Panuganti

Connect with me

If you have suggestions, spot an issue, or simply want to discuss AI, feel free to open an issue or connect with me.


⭐ If this repository helped you learn something new, consider giving it a Star!

"The best way to truly understand a concept is to build it from scratch."

About

A complete Transformer implementation from first principles using only NumPy, with mathematical explanations , modular code and Documentation

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages