Skip to content

Latest commit

 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NanoGPT: Decoder-Only Transformer From Scratch

[Python] [PyTorch] [Architecture] [Dataset]

A GPT-style Decoder Transformer implemented completely from scratch in PyTorch, trained on the Tiny Shakespeare dataset to learn autoregressive character-level text generation.


Overview

This project implements the core architecture behind modern GPT models from scratch without relying on HuggingFace Transformer implementations.

The model learns to predict the next character given a sequence of previous characters and generates Shakespeare-style text through autoregressive decoding.

The implementation includes:

  • Character Embeddings
  • Positional Embeddings
  • Causal Self-Attention
  • Multi-Head Attention
  • Feed Forward Networks
  • Residual Connections
  • Layer Normalization
  • Dropout Regularization
  • Checkpoint Saving & Loading

Architecture

The original Transformer architecture consists of:

Encoder + Decoder

However GPT models only use the Decoder portion.

This project implements the Decoder stack shown below.

Transformer Architecture


Model Pipeline

graph TD

A[Input Characters]
--> B[Character Embeddings]

B --> C[Positional Embeddings]

C --> D[Transformer Block x6]

D --> E[Final LayerNorm]

E --> F[Linear Projection]

F --> G[Softmax]

G --> H[Next Character Prediction]
Loading

Transformer Block

Each Transformer block contains:

graph TD

A[Input]

A --> B[LayerNorm]

B --> C[Multi-Head Attention]

C --> D[Residual Add]

D --> E[LayerNorm]

E --> F[Feed Forward Network]

F --> G[Residual Add]

G --> H[Output]
Loading

Attention Mechanism

The model uses causal self-attention.

Each token creates:

  • Query (Q)
  • Key (K)
  • Value (V)

Attention scores are computed as:

Attention(Q,K,V)
=
Softmax(QKᵀ / √dₖ)V

A causal mask prevents the model from seeing future tokens during training.

1 0 0 0
1 1 0 0
1 1 1 0
1 1 1 1

This ensures autoregressive generation.


Training Workflow

graph TD

A[Tiny Shakespeare Dataset]
--> B[Character Tokenization]

B --> C[Training Batches]

C --> D[Transformer]

D --> E[Cross Entropy Loss]

E --> F[Backpropagation]

F --> G[Adam Optimizer]

G --> H[Updated Parameters]
Loading

Model Evolution

The project was developed incrementally:

Bigram Language Model
          ↓
Single Attention Head
          ↓
Multi-Head Attention
          ↓
Feed Forward Network
          ↓
Residual Connections
          ↓
Layer Normalization
          ↓
Dropout
          ↓
6-Layer Transformer

Hyperparameters

Parameter Value
Batch Size 64
Context Length 256
Embedding Size 384
Attention Heads 6
Transformer Layers 6
Dropout 0.2
Learning Rate 3e-4
Optimizer Adam
Training Steps 5000

Results

Training Progress

Step Train Loss Validation Loss
500 2.0010 2.0880
1000 1.5949 1.7786
1500 1.4397 1.6438
2000 1.3404 1.5714
2500 1.2786 1.5331
3000 1.2264 1.5101
3500 1.1820 1.4916
4000 1.1464 1.4904
4500 1.1095 1.4804
4999 1.0763 1.4873

Generated Sample

LUCIO:

We muse hath resistes him so soveree:
son't his other wrough stands of coverent sh'd:
he has here, and stand it and poor exceder...

The model successfully learns:

  • Shakespeare-style dialogue formatting
  • Character names
  • Sentence structure
  • English grammar patterns
  • Long-range context relationships

Project Structure

nano-gpt/
│
├── README.md
├── requirements.txt
├── .gitignore
│
├── data/
│   └── input.txt
│
├── checkpoints/
│   └── .gitkeep
│
├── samples/
│
└── src/
    ├── config.py
    ├── dataset.py
    ├── model.py
    ├── train.py
    └── generate.py

Installation

git clone https://github.com/yourusername/nano-gpt.git

cd nano-gpt

pip install -r requirements.txt

Training

python src/train.py

Generate Text

python src/generate.py

Future Improvements

  • GPT-2 Tokenizer (BPE)
  • GELU Activations
  • Flash Attention
  • Mixed Precision Training
  • Larger Datasets
  • HuggingFace Integration
  • GPT-2 Scale Architecture

References

  • Attention Is All You Need (2017)
  • Andrej Karpathy's NanoGPT
  • PyTorch Documentation

About

A decoder-only Transformer from scratch using PyTorch, implementing self-attention, transformer blocks, and autoregressive text generation without high-level NLP libraries.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages