惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
N
News and Events Feed by Topic
T
Tor Project blog
AI
AI
S
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News: Ask HN
Hacker News: Ask HN
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog
B
Blog
G
GRAHAM CLULEY
Recent Commits to openclaw:main
Recent Commits to openclaw:main
W
WeLiveSecurity
GbyAI
GbyAI
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Check Point Blog
SecWiki News
SecWiki News
Y
Y Combinator Blog
I
Intezer
S
Securelist
WordPress大学
WordPress大学
小众软件
小众软件
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
Schneier on Security
Schneier on Security
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
P
Privacy International News Feed
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threatpost
H
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
Google Developers Blog
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
Forbes - Security
Forbes - Security
C
Cisco Blogs
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Build Your Own Shakespearean LLM
Michael Mathews · 2026-06-13 · via DEV Community

You know about LLMs (Large Language Models), but how are they created? Let's build our own to find out!

What You'll Make

By the end of this guide, you'll have trained your very own working LLM from scratch on Shakespeare's complete works (about 1MB of text). The model will learn character-level patterns and generate text that sounds like Shakespeare, not particularly coherent, but with a similarish rhythm and style. The whole process takes about 15 minutes.

Our goal isn't to end up with anything genuinely useful. Big AI vendors spend millions of dollars and months of compute time to achieve that. Our goal is to step through the process at a scale that fits on a typical consumer-grade desktop computer. As we go, we'll learn by doing the same basic steps that every LLM creator (from tiny to mammoth) follows.

Note: This guide was written for the author's MacBook Pro M4 Pro with 24 GB RAM. You may need to adjust specific settings to match your own hardware.

Step 1: Install Prerequisites

Open Terminal and run these commands.

First, make sure you have Python 3.10 or later:

python3 --version # Python 3.11.0

If you don't have Python 3.10+, install it (on Mac) via Homebrew:

brew install python@3.11

Now clone the nanoGPT repository and set up a virtual environment:

# Clone the repository
git clone https://github.com/karpathy/nanoGPT.git
cd nanoGPT

# Create and activate a virtual environment
python3 -m venv venv
source venv/bin/activate

Step 2: Install PyTorch with MPS Support (Apple Silicon)

Note: This guide focuses on Apple Silicon execution using Metal Performance Shaders (MPS). For Compute Unified Device Architecture (CUDA), modify this guide to suit your hardware (e.g., swapping --device=mps for --device=cuda).

Grab PyTorch:

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

Verify MPS is working before proceeding:

python -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'MPS available: {torch.backends.mps.is_available()}')"
  PyTorch version: 2.5.1
  MPS available: True

You should see MPS available: True. If you see False, reinstall PyTorch using the command above.

Then install the remaining dependencies:

pip install numpy transformers datasets tiktoken wandb tqdm

Step 3: Prepare the Shakespeare Dataset

Run the preparation script to fetch and process the dataset we intend to train our model on:

python data/shakespeare_char/prepare.py

You should see output like this:

length of dataset in characters: 1,115,394
all the unique characters:
 !$&',-.3:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
vocab size: 65
train has 1,003,854 tokens
val has 111,540 tokens

What did the preparation actually do?

The prepare script did four things in sequence:

  1. Downloaded the Tiny Shakespeare dataset (~1.1 million characters of play text).
  2. Built a vocabulary — it scanned the text and found 65 unique characters (letters, punctuation, and a space). That list is the model's entire alphabet.
  3. Tokenised — it assigned each character a number from 0 to 64. Every character in Shakespeare is now an integer. This is what "tokens" means at the character level.
  4. Split and saved — 90% of the tokens went into train.bin and 10% into val.bin. The .bin format stores raw integers efficiently, so loading during training is fast.

The 90/10 train/validation split is intentional: the model learns from the training set and is periodically tested against the validation set — data it has never seen — to check whether it's genuinely learning or just memorising.

Working with characters

The log from the prepare script reveals some interesting details about the dataset. For example, there were 65 unique characters used throughout the source text: upper and lower-case letters, the number three, some punctuation, and a space. This is particularly relevant to us because we're building a character-level language model: one that selects a candidate for each of those 65 characters to be the next one.

For example, imagine the model starts with the text "fare". Drawing on everything it has learned from the training data, it assigns a probability to each of the 65 available characters being next: for example, it would assign a high probability to the character "w", a low probability for "Q" or "3", and some medium probability for other plausible options like space and "s".

Step 4: Train Your Model

Now we are finally ready to start training our LLM!

Run the training script with the settings shown (optimised for a MacBook Pro M4 Pro with 24 GB RAM):

# --device=mps: Uses the Apple Silicon GPU
# --compile=False: Required as MPS does not fully support torch.compile
python train.py config/train_shakespeare_char.py \
  --device=mps \
  --compile=False \
  --eval_iters=20 \
  --log_interval=1 \
  --block_size=64 \
  --batch_size=18 \
  --n_layer=4 \
  --n_head=4 \
  --n_embd=128 \
  --max_iters=2000 \
  --lr_decay_iters=2000 \
  --dropout=0.0

What these flags mean:

  • --device=mps → Uses Mac M-series GPU
  • --compile=False → Required for MPS, as torch.compile doesn't support it yet
  • --block_size=64 → Context window (64 characters at a time)
  • --n_layer=4, --n_head=4, --n_embd=128 → A small but capable 4-layer Transformer
  • --max_iters=2000 → Training steps (about 10 minutes on a Pro-series Apple Silicon computer)
  • --dropout=0.0 → Less regularization for a small network

Training progress updates will rapidly scroll by showing information similar to:

iter 0: loss 4.1234, time 0.45s
iter 100: loss 2.3456, time 0.43s
iter 200: loss 1.9876, time 0.44s
...

The loss value decreasing steadily down to below 1.6 means the model is learning. When it finishes, checkpoints will be saved to out-shakespeare-char/.

Step 5: Generate Text

Once training is complete, generate samples from your trained model:

python sample.py --out_dir=out-shakespeare-char --device=mps --compile=False

You'll get output a bit like this:

SICINIUS:
Wall, my lords mean:
The death a just save.

KING RICHARD III:
The cannot still the shall sweet the given arm.

VIRGILIA:
Romeo, say, this me, what, he that the gracious,
And jullow'd me were beace him,
Nor dear meased unnor so to this first:
Had, looks, to you. who arms my say modeurly in!

MENENIUS:
Marry him greaten that be shall cousing;
Putilance might vilentiw to him in.

Provost:
I proud the doubt fore for 'tis answel?

It's not perfect (or even very coherent) Shakespeare, but after only about 10 minutes of training on a laptop, it's still impressive.

Step 6: Train on Your Favourite Author

Once you've got Shakespeare working, swap in any public domain author:

  1. Find a text file from Project Gutenberg (e.g., Charles Darwin, Jane Austen, Charles Dickens)
  2. Replace the input file:

    # Backup the Shakespeare file
    mv data/shakespeare_char/input.txt data/shakespeare_char/input_shakespeare.txt
    
    # Copy your author's text
    cp ~/Downloads/on-the-origin-of-species.txt data/shakespeare_char/input.txt
    
  3. Re-run preparation (this builds a new character vocabulary based on your author):

    python data/shakespeare_char/prepare.py
    

    You'll see a different vocab size: Darwin used a wider range of characters than Shakespeare, for example.

  4. Train again using the same command from Step 4

    Tweaks you may try:

    Setting Value Why
    --device=mps Required Uses GPU, 2-3x faster than CPU
    --compile=False Required MPS doesn't support torch.compile
    --batch_size 12-24 Increase to 24 if training is stable (24GB RAM can handle it)
    --n_layer 4-6 Start with 4, try 6 if you want better results (slower)
    --max_iters 2000-5000 2000 is quick (~10 min); 5000 gives noticeably better text
  5. Regenerate the output

    Notice that the output from the model trained on Charles Darwin's On the Origin of Species By Means of Natural Selection is dramatically different than the one trained on Shakespearean text.

    Have groups conothy extinct and character this dunder
    of the quability and thus common to some caustful
    modifications, will arctic plaid, and the all genus of
    the continents of increase (are HaGuare and distinct
    with placed instincts of transports any present
    continents in the same genus, and this tendency, for
    the same groups? I doe not as our consequent bur worn,
    are constantly to marked, if these cases to which it
    would such two series to the same genus
    

Troubleshooting

Training is slow Make sure --device=mps is in your command line, without it, you're running on the CPU, and training will take 3-4x longer.

Memory errors Reduce --batch_size from 12 to 8. 24GB RAM is fine, but MPS GPU memory has different limits.

The generated text is gibberish That's expected! After only a few thousand iterations, you're seeing a "baby GPT" just starting to learn patterns. Run for --max_iters=5000 or 10000 for better results.

Zoom Out: the Same Mathematics but at Scale

When comparing our 10-minute hobby project to commercial LLM products, it is easy to assume they must be built using fundamentally different technologies. But the basic principles behind both are identical.

The massive performance leap achieved by commercial LLMs comes down to three specific scaling vectors:

1. Tokenisation (Characters vs Sub-words)

In this guide, our model breaks the text down character by character. It sees the word "the" as three separate atomic parts: the letters t, h, and e. Commercial models use Byte-Pair Encoding (BPE) tokenisation instead, grouping common letter sequences into single tokens and assigning them unique identifiers (e.g., the word "the" becomes token 1169). This vastly increases efficiency and allows the model to grasp greater meaning versus just spelling patterns.

2. Context Window (Block Size)

Our tiny model has a --block_size=64, meaning it can only look at a mere 64 characters at a time to predict the next one. Commercial models use vastly larger context windows, ranging from many thousands to over a million tokens. This means they can maintain context across entire codebases or lengthy documentation files rather than just a few sentences.

3. Dimensionality (Layers and Embedding Sizes)

Our model uses 4 layers and an embedding size of 128; compare that to a base production model that might use 32 layers and over 4,000 dimensions.

Feature Our Lab Model (llmlab.dev) Commercial Base Model
Token Unit Single Character Sub-word Fragments (BPE)
Context Window 64 characters 32,000+ tokens
Layers (n_layer) 4 32 to 80+
Embedding Size (n_embd) 128 4,096 to 8,192+

Next Steps

Once you master creating your own LLMs from scratch, try:

  • Character vs subword tokenisation — Use data/shakespeare/ (BPE tokeniser) instead of shakespeare_char/
  • Larger models — Increase --n_layer=6 --n_head=6 --n_embd=384 (closer to GPT-2 small)
  • Multiple authors — Concatenate Austen, Dickens, and Twain into one file
  • Fine-tuning — Start from a pretrained GPT-2 and train on your author (nanoGPT supports this)

Photo credit: Brett Sayles