Prerequisites
No dependencies required. Game Grammar is built entirely in pure Python with a custom autograd implementation. No PyTorch, TensorFlow, or external frameworks needed. Requirements:- Python 3.10 or higher
- That’s it
The training process runs on CPU and takes approximately 30-40 minutes for 5000 steps. If you’re impatient, you can reduce
num_steps in scripts/train.py to 1000 for a quick test.Installation
Clone the repository:pip install needed — the pure Python implementation has zero dependencies.
Three-Step Workflow
Generate gameplay episodes
Run the agent mix to produce tokenized gameplay traces. This generates 200 episodes from a mix of three agent types:What this does:Expected output:This creates
- Random (40%): Chooses legal moves randomly
- Greedy (40%): Always moves toward food
- WallFollower (20%): Follows walls systematically
episodes.json with 200 tokenized gameplay sequences. Each episode is a list of token IDs from the 74-token vocabulary.The hybrid tokenizer uses periodic snapshots (SNAP) every 16 ticks plus delta events (TICK) for efficient encoding. Think of it like video compression: keyframes + deltas.
Train the transformer
Train a 2-layer, 32-dim, 4-head causal transformer on the generated episodes. The model learns game grammar through next-token prediction.What this does:Expected output:The loss should decrease from ~4.47 (random baseline: ln(74) ≈ 4.3) to ~0.25. Training takes approximately 30-40 minutes on CPU.
Sample and validate sequences
Generate novel gameplay sequences and validate them against physical and rule constraints. The model should produce valid Snake gameplay it has never seen before.What this does:Expected output:The validation system checks three tiers:
- Structural (S): Complete BOS→EOS episodes
- Physical (P): Adjacent moves, in-bounds positions
- Rule (R): EAT→GROW+FOOD_SPAWN, DIE→EOS
Low structural validity (45%) is expected — the model often generates gameplay longer than the 64-token context window, hitting the limit mid-sequence without producing EOS. The gameplay itself is still valid.
Understanding the Results
After running all three scripts, you should see: Physical validity: 95% — The model learned that:- Movement is one cell per tick (no jumping)
- Positions must be within the 10×10 grid
- The snake can only move to adjacent cells
- Eating food triggers growth (EAT → GROW)
- Eating food triggers food respawn (EAT → FOOD_SPAWN)
- Death ends the episode (DIE_WALL or DIE_SELF → EOS)
- Score increments after eating (EAT → SCORE increment)
What’s in Each File?
After running the workflow, you’ll have:episodes.json
Tokenized gameplay traces from 200 episodes. Each episode is a list of token IDs:
weights.txt
31K trained parameters for the transformer. Human-readable text format:
Next Steps
Explore the Theory
Learn about the Wittgensteinian foundation
Tokenization Strategies
Deep dive into the five encoding approaches
Transformer Architecture
Understand the model implementation
API Reference
Read the API documentation
Troubleshooting
Training is too slow
Training is too slow
The pure Python autograd is educational but slow. To speed up training:
- Reduce
num_stepsinscripts/train.pyfrom 5000 to 1000-2000 - Reduce the number of episodes in
scripts/generate.pyfrom 200 to 50-100 - For production use, consider reimplementing in PyTorch or JAX
ModuleNotFoundError: No module named 'game_grammar'
ModuleNotFoundError: No module named 'game_grammar'
Make sure you’re running the scripts from the repository root:The scripts add the parent directory to
sys.path to import the game_grammar package.Loss is not decreasing
Loss is not decreasing
If loss stays around 4.3 (random baseline):
- Check that
episodes.jsonexists and contains data - Verify the learning rate (default: 0.01)
- Try increasing training steps
- Check for numerical instability (NaNs in weights)
Low validity rates
Low validity rates
Expected validity rates after 5000 training steps:
- Physical: 90-95%
- Rule: 95-100%
- Structural: 40-50% (limited by context window)
- Train for more steps
- Increase the number of training episodes
- Check that the model architecture matches the pretrained weights
