Skip to content
Β 
Β 

Repository files navigation

nanoGPT Sampling Visualizer

Interactive GPT-2 inference explorer with token probability visualization, entropy curves, confidence heatmap, and sampling strategy comparison. Built on nanoGPT.

Python Gradio Model Device License Built On

A web dashboard that lets you watch a neural network think step-by-step as it generates text β€” showing exactly how confident it is about every single word it chooses.


Demo

Demo


What Is This?

Most people use language models as black boxes β€” type a prompt, text comes out. This project opens the black box.

It takes the raw mathematical output of GPT-2 and turns it into an intuitive visual experience. Instead of just showing you the final text, it reveals:

  • The probability assigned to every possible next word at each generation step
  • Where the model was confident vs uncertain throughout the sequence
  • How temperature, top-k, and top-p mathematically reshape what the model produces β€” shown visually and in real time

Runs entirely on CPU. No GPU required.


πŸ“Έ Screenshots

Tab 1 β€” Generate & Visualize

Type a prompt, adjust sliders, and watch the model generate with a live token probability chart and confidence heatmap.

Tab 1 β€” Generate

Tab 2 β€” Compare Sampling Strategies

Same prompt. Same model. Three different strategies. See how mathematics changes language.

Tab 2 β€” Compare

Tab 3 β€” How Sampling Works

A built-in theory guide with PyTorch code snippets, math explanations, and a preset reference table.

Tab 3 β€” Theory


Features

Tab 1: Generate & Visualize

Feature What It Does
🌑️ Confidence Heatmap Every generated word is color-coded β€” 🟒 green (model was certain), 🟑 yellow (uncertain), πŸ”΄ red (creative leap). Shows at a glance where the model played it safe vs took a risk
πŸ“Š Token Probability Chart Interactive bar chart of the top-10 candidate tokens at every generation step. Scrub the step slider to replay any moment of the generation process
πŸ“‰ Entropy Curve Area chart of Shannon entropy at each step. Spikes show exactly where the model faced peak uncertainty β€” these are the most "interesting" moments
🎯 Perplexity Score Standard NLP evaluation metric displayed in the metadata bar. Lower = more confident output. Higher = more creative or surprising output
πŸ”˜ Preset Buttons One-click parameter configs for four use cases: Code/Factual, Creative Writing, Brainstorming, Max Diversity
πŸ•“ Generation History Logs your last 5 generations with prompt excerpt, perplexity, and settings. One-click reload restores any previous run's exact parameters
πŸ“₯ Session Export Downloads a structured session_export.json with your prompt, output, all sampling settings, perplexity, and average entropy

Tab 2: Compare Strategies

Feature What It Does
Parallel Generation Same prompt β†’ model runs 3 times with Conservative, Balanced, and Creative settings simultaneously
Side-by-Side Output Instantly compare how coherence, repetition, and creativity change across strategies
πŸ† Winner Badge Calculates perplexity of all three outputs and automatically awards "Most Creative" to the most diverse result
Avg Probability Chart Bar chart comparing the average top-1 token probability across all three strategies

Tab 3: How Sampling Works

A built-in educational reference β€” explains the math behind every parameter with actual PyTorch code snippets, a comparison table, and a preset guide. No external docs needed.


How Sampling Works

Every time GPT-2 generates the next word, it produces 50,257 raw scores β€” one per vocabulary word β€” called logits. Three parameters control how a final token is sampled from those scores:

The Full Pipeline

Raw Logits β†’ Γ· Temperature β†’ Top-K Filter β†’ Top-P Filter β†’ Softmax β†’ Sample

Parameter Breakdown

Parameter What It Changes Low Value High Value
Temperature Divides all logits before softmax Sharpens the distribution β†’ model strongly prefers top tokens β†’ focused, repetitive output Flattens the distribution β†’ all tokens become more equally likely β†’ creative, unpredictable output
Top-K Keeps only the K highest-probability tokens; zeroes out the rest Very restricted vocabulary pool Large candidate pool β€” more variety, more risk
Top-P (Nucleus) Keeps the smallest set of tokens whose cumulative probability β‰₯ P Only the most certain choices Dynamically expands pool when model is uncertain, contracts when confident

Why Top-P is smarter than Top-K: When the model is very confident (one word has 90% probability), Top-K=50 still forces 50 candidates. Top-P=0.9 would correctly pick just that one word. When the model is uncertain, Top-P expands the pool automatically. Top-P adapts. Top-K doesn't.


Preset Reference

Preset Temperature Top-K Top-P Best For
Code / Factual 0.4 30 0.85 Code, facts, structured output where predictability is a feature
Creative Writing 0.9 50 0.95 Stories, dialogue, natural-sounding conversation
Brainstorming 1.3 0 0.98 Ideation where unexpected connections matter more than strict logic
Max Diversity 1.8 0 1.0 Stress-testing model limits β€” highly erratic, often incoherent

What I Actually Built

Built on top of Andrej Karpathy's minimalist PyTorch GPT-2 implementation. All original files are left completely untouched.

nanoGPT-visualizer/
β”‚
β”œβ”€β”€ app.py                βœ… NEW β€” Gradio 6 Blocks UI, tab handlers, chart builders, session state
β”œβ”€β”€ inference_engine.py   βœ… NEW β€” Model loading, Top-P sampling, perplexity & entropy calculations
β”œβ”€β”€ requirements.txt      βœ… NEW β€” Project dependencies
β”‚
β”œβ”€β”€ model.py              πŸ”§ ORIGINAL β€” Karpathy's GPT architecture (untouched)
β”œβ”€β”€ sample.py             πŸ”§ ORIGINAL β€” Karpathy's reference generation script (untouched)
β”œβ”€β”€ train.py              πŸ”§ ORIGINAL β€” Karpathy's training loop (untouched)
└── bench.py              πŸ”§ ORIGINAL β€” Benchmarking utilities (untouched)

Key Technical Contributions

  • Top-P Nucleus Sampling β€” Implemented from scratch; absent from the original nanoGPT codebase
  • Raw Logit Extraction β€” State-tracking mechanism captures logits at every autoregressive step without disrupting the generation loop
  • Perplexity Calculation β€” exp(-1/N Β· Ξ£ log P(tokenα΅’)) computed from raw logits and actual sampled token IDs
  • Shannon Entropy per Step β€” -Ξ£ P Β· logβ‚‚(P) over the full 50,257-word vocabulary distribution at each step
  • Confidence Heatmap β€” Custom HTML renderer that maps per-token probability directly to green/yellow/red inline color
  • Gradio 6 Blocks Architecture β€” Multi-tab layout with gr.State() for history tracking, Plotly for charting, and gr.File for export

Understanding the Metrics

Metric Formula Low Value High Value
Perplexity exp(-1/N Β· Ξ£ log P(tα΅’)) Model was confident throughout β€” output may feel repetitive Model was frequently surprised β€” output is diverse or creative
Entropy (per step) -Ξ£ P Β· logβ‚‚(P) Model strongly preferred one token at that step Model was torn between many options at that step
Avg Top-1 Probability mean(max(softmax(logitsβ‚œ))) High uncertainty across the sequence High confidence across the sequence

How to Run

# 1. Clone the repository
git clone https://github.com/YOUR_USERNAME/nanoGPT-visualizer
cd nanoGPT-visualizer

# 2. Install dependencies
pip install -r requirements.txt

# 3. Launch the app
python app.py

# 4. Open your browser
#    http://localhost:7860

First launch note: GPT-2 weights (~500 MB) download automatically from HuggingFace on first run. All subsequent launches load from local cache instantly.


πŸ› οΈ Tech Stack

Tool Purpose
PyTorch Core tensor operations and model inference
HuggingFace Transformers Loading pre-trained GPT-2 124M weights
Gradio 6 Interactive multi-tab web dashboard
Plotly Token probability bar chart + entropy area chart
tiktoken OpenAI BPE tokenizer
Python 3.x Core language

What You Learn By Using This

After exploring this visualizer, you will have an intuitive, hands-on understanding of:

  1. What temperature actually does mathematically β€” not "makes it creative" but literally divides every logit, sharpening or flattening the softmax curve
  2. Why Top-P outperforms Top-K β€” because it adapts the candidate pool size based on the model's real-time confidence at each step
  3. What perplexity actually measures β€” the geometric mean of inverse token probabilities; a standard NLP metric, not just a vibes score
  4. What entropy predicts about output quality β€” high-entropy steps are where creativity and incoherence both come from; low-entropy steps are grammar and predictable structure
  5. How autoregressive generation works β€” one token at a time, each sampled from a distribution conditioned on everything generated before it

Credits & Acknowledgements

  • Original GPT architecture and nanoGPT codebase by Andrej Karpathy
  • GPT-2 model weights (124M) by OpenAI, served via HuggingFace
  • Built as part of an AI/ML internship portfolio project focused on Generative AI and NLP interpretability

Built on nanoGPT by Andrej Karpathy Β Β·Β  GPT-2 124M Β Β·Β  Runs fully on CPU

About

Interactive GPT-2 inference explorer with token probability visualization, entropy curves, confidence heatmap, and sampling strategy comparison. Built on nanoGPT.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages