Modern transformer models are incredibly powerful, but most function as black boxes: text goes in, predictions come out, and the internal reasoning process remains hidden behind layers of tensor operations.
TransformerScope is an interactive interpretability platform built to expose those internal mechanics in real time.
The project combines a GPT style transformer implemented entirely from scratch in PyTorch with a full stack visualization dashboard for exploring token embeddings, Q/K/V projections, causal attention masking, attention probability flow, and autoregressive token generation.
Rather than treating transformers as opaque APIs, TransformerScope focuses on making attention mechanics inspectable, understandable, and visually intuitive through direct interaction with the model’s internal representations.
- End-to-end ownership — tokenizer, embeddings, attention, FFN, residuals, layer norms, and LM head; no dependency on Hugging Face
transformersor hosted inference APIs for the core model. - Explicit attention — Q, K, V projections, scaled dot-product scores, causal masking, and softmax weights surfaced for visualization and API consumers (
models/attention.py). - Interpretability API — structured JSON for tokenization, embeddings, per-layer Q/K/V, attention probabilities, and next-token distributions (
backend/app/). - Interactive dashboard — token strip, layer/head controls, attention heatmaps, Q/K/V views, and next-token probability charts with concise on-page explanations.
- Reproducible training — Tiny Shakespeare pipeline with checkpointing and loss logging (
training/).
Dashboard — analyze and model context. Character-level forward pass with checkpoint metadata (embedding dimension, depth, heads, context length, vocabulary size) and inline educational copy.
Next-token distribution. Top-32 softmax probabilities at the final sequence position—useful for inspecting how the model allocates mass across the character vocabulary at small scale.
| Layer | Technologies |
|---|---|
| Model | PyTorch |
| API | FastAPI, Pydantic, Uvicorn |
| UI | Vite, React 18, TypeScript, Tailwind CSS, Plotly |
| Path | Description |
|---|---|
models/ |
Character-level tokenizer, manual CausalSelfAttention, TinyGPT |
training/ |
Dataset helpers, training loop, CLI entrypoint |
backend/app/ |
REST API, request/response schemas, model loading and analysis |
frontend/ |
Dashboard application |
visualizations/ |
Placeholder package for future export and analysis utilities |
docs/screenshots/ |
UI captures for documentation (see Gallery) |
flowchart LR
subgraph core [Model]
Tok[CharTokenizer]
GPT[TinyGPT]
end
subgraph api [API]
Svc[ModelService]
A1[POST_/api/analyze]
G1[POST_/api/generate]
end
subgraph ui [Dashboard]
Dash[React_UI]
end
Tok --> GPT
GPT --> Svc
Svc --> A1
Svc --> G1
Dash --> A1
Dash --> G1
- Python 3.10+
- Node.js 18+ (for the frontend)
cd transformer_interpretability
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txtA checkpoint bundles weights, vocabulary, and hyperparameters for the API to load. Example (downloads Tiny Shakespeare on first run):
PYTHONPATH=. python -m training.train --max_iters 2000Outputs:
checkpoints/checkpoint.pt— model state and metadatacheckpoints/loss_log.json— logged loss over training
Hyperparameters are adjustable via CLI flags in training/train.py.
cd frontend
npm installBackend (from repository root):
source .venv/bin/activate
PYTHONPATH=. uvicorn backend.app.main:app --reloadFrontend (from frontend/):
npm run devOpen the local URL shown in the terminal (Vite defaults to port 5173). The API is served on port 8000 unless you change it. To point the UI at a different API origin, set VITE_API_URL in frontend/.env.
| Method | Path | Purpose |
|---|---|---|
| GET | /health |
Service health |
| GET | /api/model/info |
Loaded checkpoint and model hyperparameters |
| POST | /api/analyze |
Forward pass with interpretability payload |
| POST | /api/generate |
Autoregressive generation with per-step probabilities |
Interactive documentation: http://127.0.0.1:8000/docs while the backend is running.
Note: Inference uses the character vocabulary induced during training. After training on Tiny Shakespeare, prompts should use characters present in that corpus so tokenization succeeds.
- Attention — Multi-head causal self-attention with explicit captures for tensors used in teaching and visualization (
models/attention.py). - Tokenizer — Character-level vocabulary derived from the training corpus (
models/tokenizer.py). - Serving layer — Converts model internals to JSON-friendly structures for the dashboard (
backend/app/services/model_service.py).
This project is released under the MIT License.

