Train a character-level transformer on newline-delimited text with PyTorch, then generate names, words, or other short strings. Training supports CPU, CUDA and Apple Silicon (MPS).
Demo · Browser training counterpart
Requires Python 3.10 or later. Run these commands from the repository root:
python -m venv .venvActivate the environment with source .venv/bin/activate on macOS/Linux or .venv\Scripts\Activate.ps1 in PowerShell, then install:
python -m pip install -e . onnx onnxscriptThe ONNX packages are needed to export the JavaScript bundle when saving a run. For CUDA, install the appropriate PyTorch build using the PyTorch installation instructions.
Open the interactive menu:
python -m character_level_transformerOr train directly:
python -m character_level_transformer --dataset us_baby_names.txt --steps 3000Use one sample per line in a UTF-8 text file. Bare dataset names resolve inside datasets/; --dataset PATH also accepts a path to your own file.
| Command | Purpose |
|---|---|
python -m character_level_transformer --models |
Inspect, generate from, resume or delete saved runs |
python -m character_level_transformer --compare |
Compare CPU and accelerator training |
python -m character_level_transformer --help |
Show all training options |
The installed character-level-transformer command is an alternative to python -m character_level_transformer.
Runs save under models/ by default, with numbered folders when a name already exists. Use --output PATH to choose an output location or --no-save to disable saving.
| File | Contents |
|---|---|
.model.pt |
Model weights and tokenizer for Python inference |
.resume.pt |
Dataset, optimizer and training state needed to resume |
.model |
ONNX bundle and metadata for JavaScript inference |
The resume file requires its matching model file. To run an exported bundle with Node.js, install the root JavaScript dependencies:
npm ci
node scripts/run_js_bundle.js models/us_baby_names/us_baby_names.model --samples 40 --temperature 0.7Generation uses an embedded Bloom filter to reject source-line matches. False positives can also reject novel strings; accepted outputs are not a guarantee of real-world originality.
| File | Non-empty lines |
|---|---|
datasets/us_baby_names.txt |
104,819 |
datasets/english_words.txt |
370,105 |
The inference app is in site/. Requires Node.js 22.12 or later.
cd site
npm ci
npm run devFrom the repository root, with the Python environment activated:
python -m pip install -e ".[dev]"
python -m unittest discover -s tests
ruff check character_level_transformer scripts tests
ruff format --check character_level_transformer scripts tests
mdformat --check README.md docsNode.js and npm ci at the repository root are required for JavaScript bundle integration tests. The site has separate npm run check, npm run typecheck and npm run build commands, run from site/.
The default model uses causal self-attention, RMSNorm, SwiGLU and standard additive residual connections. Experimental options are documented separately:
- Attention Residuals: residual modes and benchmark usage.
- Character distillation: methodology, recorded measurements and reproduction commands.
MIT.
