# sakthi82/ai-fundamentals architecture

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

- Interactive diagram: https://gitdiagram.com/sakthi82/ai-fundamentals
- Repository: https://github.com/sakthi82/ai-fundamentals

## Overview

This repository is a collection of runnable AI-learning demonstrations, not a single language-model application. The main workflow is to run a script and inspect its printed measurements or generated text; the scripts do not call one another. GPT-2-backed demonstrations explore tokenization, embeddings, attention, scoring, generation, and hallucination, while two independent scripts train tiny models from scratch. README-described links establish the script capabilities; source excerpts confirm the sampled implementations.

Main entry points: [`03_attention.py`](https://github.com/sakthi82/ai-fundamentals/blob/main/03_attention.py) (Inspect attention), [`01_tokenize.py`](https://github.com/sakthi82/ai-fundamentals/blob/main/01_tokenize.py) (Tokenize text), [`02_embeddings.py`](https://github.com/sakthi82/ai-fundamentals/blob/main/02_embeddings.py) (Inspect embeddings), [`04_predict_next_token.py`](https://github.com/sakthi82/ai-fundamentals/blob/main/04_predict_next_token.py) (Score next token)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_pretrained["GPT-2 explorations"]
  node_tokenize["Tokenize text<br/>[01_tokenize.py]"]
  node_embeddings["Inspect embeddings<br/>[02_embeddings.py]"]
  node_attention["Inspect attention<br/>[03_attention.py]"]
  node_score["Score next token"]
  node_generation["Sample token loop"]
end

subgraph group_g_scratch["From-scratch learning"]
  node_tiny06["Tiny Transformer"]
  node_train06["Train and generate"]
  node_tiny08["Tiny language model"]
  node_experiments08["Learning experiments"]
end

subgraph group_g_results["Hallucination analysis"]
  node_hallucination["Measure uncertainty"]
end

node_learner(("Learner"))
node_gpt2[("Pretrained GPT-2")]
node_results["Printed results"]
node_heatmap["Attention heatmap"]

node_learner -->|"runs"| node_tokenize
node_learner -->|"runs"| node_embeddings
node_learner -->|"runs"| node_attention
node_learner -->|"runs"| node_score
node_learner -->|"runs"| node_generation
node_learner -->|"runs"| node_train06
node_learner -->|"runs"| node_hallucination
node_learner -->|"runs"| node_experiments08
node_tokenize -->|"loads tokenizer"| node_gpt2
node_embeddings -->|"loads model"| node_gpt2
node_attention -->|"runs inference"| node_gpt2
node_score -->|"scores tokens"| node_gpt2
node_generation -->|"runs inference"| node_gpt2
node_hallucination -->|"scores and samples"| node_gpt2
node_train06 -->|"trains and samples"| node_tiny06
node_experiments08 -->|"trains and evaluates"| node_tiny08
node_tokenize -->|"prints tokens"| node_results
node_embeddings -->|"prints comparisons"| node_results
node_attention -->|"prints weights"| node_results
node_attention -->|"saves heatmap"| node_heatmap
node_score -->|"prints probabilities"| node_results
node_generation -->|"prints continuation"| node_results
node_train06 -->|"prints loss and text"| node_results
node_experiments08 -->|"prints accuracy"| node_results
node_hallucination -->|"prints uncertainty"| node_results

click node_tokenize "https://github.com/sakthi82/ai-fundamentals/blob/main/01_tokenize.py"
click node_embeddings "https://github.com/sakthi82/ai-fundamentals/blob/main/02_embeddings.py"
click node_attention "https://github.com/sakthi82/ai-fundamentals/blob/main/03_attention.py"
click node_score "https://github.com/sakthi82/ai-fundamentals/blob/main/04_predict_next_token.py"
click node_generation "https://github.com/sakthi82/ai-fundamentals/blob/main/05_manual_generation_loop.py"
click node_tiny06 "https://github.com/sakthi82/ai-fundamentals/blob/main/06_train_tiny_transformer_from_scratch.py"
click node_train06 "https://github.com/sakthi82/ai-fundamentals/blob/main/06_train_tiny_transformer_from_scratch.py"
click node_tiny08 "https://github.com/sakthi82/ai-fundamentals/blob/main/08_watch_it_learn.py"
click node_experiments08 "https://github.com/sakthi82/ai-fundamentals/blob/main/08_watch_it_learn.py"
click node_hallucination "https://github.com/sakthi82/ai-fundamentals/blob/main/07_why_it_hallucinates.py"

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_tokenize,node_embeddings,node_attention,node_score,node_generation toneBlue
class node_tiny06,node_train06,node_tiny08,node_experiments08,node_gpt2 toneAmber
class node_hallucination toneMint
class node_learner,node_results,node_heatmap toneIndigo
```

## Components

### GPT-2 explorations

- [Tokenize text](https://github.com/sakthi82/ai-fundamentals/blob/main/01_tokenize.py): `01_tokenize.py`
- [Inspect embeddings](https://github.com/sakthi82/ai-fundamentals/blob/main/02_embeddings.py): `02_embeddings.py`
- [Inspect attention](https://github.com/sakthi82/ai-fundamentals/blob/main/03_attention.py): `03_attention.py`
- [Score next token](https://github.com/sakthi82/ai-fundamentals/blob/main/04_predict_next_token.py): `04_predict_next_token.py`
- [Sample token loop](https://github.com/sakthi82/ai-fundamentals/blob/main/05_manual_generation_loop.py): `05_manual_generation_loop.py`

### From-scratch learning

- [Tiny Transformer](https://github.com/sakthi82/ai-fundamentals/blob/main/06_train_tiny_transformer_from_scratch.py): `06_train_tiny_transformer_from_scratch.py`
- [Train and generate](https://github.com/sakthi82/ai-fundamentals/blob/main/06_train_tiny_transformer_from_scratch.py): `06_train_tiny_transformer_from_scratch.py`
- [Tiny language model](https://github.com/sakthi82/ai-fundamentals/blob/main/08_watch_it_learn.py): `08_watch_it_learn.py`
- [Learning experiments](https://github.com/sakthi82/ai-fundamentals/blob/main/08_watch_it_learn.py): `08_watch_it_learn.py`

### Hallucination analysis

- [Measure uncertainty](https://github.com/sakthi82/ai-fundamentals/blob/main/07_why_it_hallucinates.py): `07_why_it_hallucinates.py`

### Other components

- **Learner**
- **Pretrained GPT-2**
- **Printed results**
- **Attention heatmap**

## Connections

- Learner → Tokenize text: runs (evidence: `README.md`)
- Learner → Inspect embeddings: runs (evidence: `README.md`)
- Learner → Inspect attention: runs (evidence: `README.md`)
- Learner → Score next token: runs (evidence: `README.md`)
- Learner → Sample token loop: runs (evidence: `README.md`)
- Learner → Train and generate: runs (evidence: `README.md`)
- Learner → Measure uncertainty: runs (evidence: `README.md`)
- Learner → Learning experiments: runs (evidence: `README.md`)
- Tokenize text → Pretrained GPT-2: loads tokenizer (evidence: `01_tokenize.py`)
- Inspect embeddings → Pretrained GPT-2: loads model (evidence: `02_embeddings.py`)
- Inspect attention → Pretrained GPT-2: runs inference (evidence: `03_attention.py`)
- Score next token → Pretrained GPT-2: scores tokens (evidence: `04_predict_next_token.py`)
- Sample token loop → Pretrained GPT-2: runs inference (evidence: `05_manual_generation_loop.py`)
- Measure uncertainty → Pretrained GPT-2: scores and samples (evidence: `07_why_it_hallucinates.py`)
- Train and generate → Tiny Transformer: trains and samples (evidence: `06_train_tiny_transformer_from_scratch.py`)
- Learning experiments → Tiny language model: trains and evaluates (evidence: `08_watch_it_learn.py`)
- Tokenize text → Printed results: prints tokens (evidence: `01_tokenize.py`)
- Inspect embeddings → Printed results: prints comparisons (evidence: `02_embeddings.py`)
- Inspect attention → Printed results: prints weights (evidence: `03_attention.py`)
- Inspect attention → Attention heatmap: saves heatmap (evidence: `03_attention.py`)
- Score next token → Printed results: prints probabilities (evidence: `04_predict_next_token.py`)
- Sample token loop → Printed results: prints continuation (evidence: `05_manual_generation_loop.py`)
- Train and generate → Printed results: prints loss and text (evidence: `06_train_tiny_transformer_from_scratch.py`)
- Learning experiments → Printed results: prints accuracy (evidence: `08_watch_it_learn.py`)
- Measure uncertainty → Printed results: prints uncertainty (evidence: `07_why_it_hallucinates.py`)
