# omarelghamry1/gpt-from-scratch architecture

> Architecture diagram of the GitHub repository omarelghamry1/gpt-from-scratch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/omarelghamry1/gpt-from-scratch
- Repository: https://github.com/omarelghamry1/gpt-from-scratch

## Overview

This repository is a character-level GPT learning project: it trains on Tiny Shakespeare, evaluates next-character loss, and samples Shakespeare-like text. The main runnable workflow is `gpt.py`; `Bigram.py` provides a baseline, while the notebooks document the build and separate residual-network experiments. The source excerpts show the runnable scripts in detail; notebook capabilities and their dataset use are included as documented in the README, without inferring their internal flow.

Main entry points: [`gpt.py`](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py) (GPT model), [`gpt_from_scratch.ipynb`](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt_from_scratch.ipynb) (GPT build walkthrough), [`Residual Network .ipynb`](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/Residual%20Network%20.ipynb) (Residual network experiments), [`input.txt`](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/input.txt) (Tiny Shakespeare)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data preparation"]
  node_shakespeare["Tiny Shakespeare<br/>[input.txt]"]
  node_names["Names dataset<br/>[names.txt]"]
  node_charprep["Character encoding and splits<br/>[gpt.py]"]
  node_batches["Batch sampling<br/>[gpt.py]"]
end

subgraph group_models["Language models"]
  node_bigram["Bigram baseline<br/>[Bigram.py]"]
  node_gpt["GPT model<br/>[gpt.py]"]
  node_embeddings["Token and position embeddings<br/>[gpt.py]"]
  node_attention["Causal multi-head attention<br/>[gpt.py]"]
  node_feedforward["Feed-forward network<br/>[gpt.py]"]
  node_block["Transformer residual block<br/>[gpt.py]"]
end

subgraph group_training["Training and generation"]
  node_loss["Next-character loss<br/>[gpt.py]"]
  node_evaluation["Train and validation evaluation<br/>[gpt.py]"]
  node_optimizer["AdamW training loop<br/>[gpt.py]"]
  node_generation["Text generation<br/>[gpt.py]"]
  node_textout["Generated text"]
end

subgraph group_learning["Learning experiments"]
  node_buildnotebook["GPT build walkthrough"]
  node_residualnotebook["Residual network experiments"]
end

node_user(("Project user"))

node_user -->|"runs"| node_gpt
node_user -.->|"runs baseline"| node_bigram
node_user -->|"explores"| node_buildnotebook
node_user -->|"explores"| node_residualnotebook
node_shakespeare -->|"read by"| node_charprep
node_charprep -->|"prepares"| node_batches
node_batches -->|"feeds"| node_gpt
node_gpt -->|"looks up"| node_embeddings
node_embeddings -->|"feeds"| node_block
node_block -->|"applies"| node_attention
node_block -->|"applies"| node_feedforward
node_gpt -->|"computes"| node_loss
node_batches -->|"supplies batches"| node_evaluation
node_evaluation -->|"evaluates"| node_gpt
node_optimizer -->|"samples"| node_batches
node_optimizer -->|"trains"| node_gpt
node_optimizer -->|"backpropagates"| node_loss
node_gpt -->|"provides model"| node_generation
node_generation -->|"decodes"| node_textout
node_names -->|"used by"| node_residualnotebook
node_buildnotebook -->|"walks through"| node_gpt

click node_shakespeare "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/input.txt"
click node_names "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/names.txt"
click node_charprep "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_batches "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_bigram "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/Bigram.py"
click node_gpt "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_embeddings "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_attention "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_feedforward "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_block "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_loss "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_evaluation "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_optimizer "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_generation "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py"
click node_buildnotebook "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt_from_scratch.ipynb"
click node_residualnotebook "https://github.com/omarelghamry1/gpt-from-scratch/blob/main/Residual%20Network%20.ipynb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_shakespeare,node_names,node_charprep,node_batches,node_user toneBlue
class node_bigram,node_gpt,node_embeddings,node_attention,node_feedforward,node_block toneAmber
class node_loss,node_evaluation,node_optimizer,node_generation,node_textout toneMint
class node_buildnotebook,node_residualnotebook toneRose
```

## Components

### Data preparation

- [Tiny Shakespeare](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/input.txt): `input.txt`
- [Names dataset](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/names.txt): `names.txt`
- [Character encoding and splits](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- [Batch sampling](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`

### Language models

- [Bigram baseline](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/Bigram.py): `Bigram.py`
- [GPT model](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- [Token and position embeddings](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- [Causal multi-head attention](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- [Feed-forward network](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- [Transformer residual block](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`

### Training and generation

- [Next-character loss](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- [Train and validation evaluation](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- [AdamW training loop](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- [Text generation](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt.py): `gpt.py`
- **Generated text**

### Learning experiments

- [GPT build walkthrough](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/gpt_from_scratch.ipynb): `gpt_from_scratch.ipynb`
- [Residual network experiments](https://github.com/omarelghamry1/gpt-from-scratch/blob/main/Residual%20Network%20.ipynb): `Residual Network .ipynb`

### Other components

- **Project user**

## Connections

- Project user → GPT model: runs (evidence: `README.md`)
- Project user → Bigram baseline: runs baseline (evidence: `README.md`)
- Project user → GPT build walkthrough: explores (evidence: `README.md`)
- Project user → Residual network experiments: explores (evidence: `README.md`)
- Tiny Shakespeare → Character encoding and splits: read by (evidence: `gpt.py`)
- Character encoding and splits → Batch sampling: prepares (evidence: `gpt.py`)
- Batch sampling → GPT model: feeds (evidence: `gpt.py`)
- GPT model → Token and position embeddings: looks up (evidence: `gpt.py`)
- Token and position embeddings → Transformer residual block: feeds (evidence: `gpt.py`)
- Transformer residual block → Causal multi-head attention: applies (evidence: `gpt.py`)
- Transformer residual block → Feed-forward network: applies (evidence: `gpt.py`)
- GPT model → Next-character loss: computes (evidence: `gpt.py`)
- Batch sampling → Train and validation evaluation: supplies batches (evidence: `gpt.py`)
- Train and validation evaluation → GPT model: evaluates (evidence: `gpt.py`)
- AdamW training loop → Batch sampling: samples (evidence: `gpt.py`)
- AdamW training loop → GPT model: trains (evidence: `gpt.py`)
- AdamW training loop → Next-character loss: backpropagates (evidence: `gpt.py`)
- GPT model → Text generation: provides model (evidence: `gpt.py`)
- Text generation → Generated text: decodes (evidence: `gpt.py`)
- Names dataset → Residual network experiments: used by (evidence: `README.md`)
- GPT build walkthrough → GPT model: walks through (evidence: `README.md`)
