A minimal PyTorch implementation of a character-level GPT-style language model.
main.py- Training script that builds a small Transformer/GPT-like model, trains it oninput.txt, savesmodel.pt, and generates text intooutput.txt.input.txt- Training corpus used bymain.py.model.pt- Saved model weights after training.output.txt- Generated text output produced by the model after training.
main.py:
- loads text from
input.txt - builds a character-level vocabulary
- defines a small Transformer-based language model using self-attention blocks
- trains the model with next-character prediction
- saves the trained weights to
model.pt - generates sample text and writes 10,000 generated tokens to
output.txt
- Python 3.8+
- PyTorch
Install dependencies via pip:
pip install torchRun the training and generation script:
python main.pyAfter completion, the model weights are written to model.pt and generated text is saved to output.txt.
- The model is character-level, not token-level.
- It uses a small transformer with 6 layers, 6 heads, and a context window of 256 characters.
- Training runs for 5000 iterations by default.