A from-scratch decoder-only Transformer language model built in PyTorch, based on a Tiny Stories-style character-level example.
BumbleBee.ipynb- a Jupyter notebook that walks through:- loading and encoding a character-level TinyStories dataset
- implementing tokenization with character-to-index mappings
- training a simple bigram language model
- explaining the self-attention mechanism with toy examples
- building a full Transformer decoder block from scratch
- training the final
BumbleBeemodel and generating text
I tried to annotate the notebook so that the reader can have a better understanding (Of course it includes me also in a few years when I look back at this notebook). The notebook is designed to be educational and to help understand the inner workings of a Transformer language model.
The notebook downloads the TinyStories validation dataset from Hugging Face:
https://huggingface.co/datasets/roneneldan/TinyStories/resolve/main/TinyStories-valid.txt
The dataset is processed at the character level, with a vocabulary built from the unique characters in the file.
The final model is a small autoregressive Transformer language model with:
- character-level token embeddings
- positional embeddings
- multiple Transformer blocks
- multi-head self-attention with causal masking
- feed-forward network inside each block
- layer normalization and dropout
- output linear projection to vocabulary logits
block_size = 128batch_size = 32max_iters = 5000learning_rate = 1e-3n_embd = 64n_head = 8n_layer = 8dropout = 0.2
The notebook trains the model using AdamW on batches sampled from the training split. It also evaluates train and validation loss periodically using an estimate_loss() helper.
After training, the model can generate new text autoregressively from a starting context token.
- Install Python and PyTorch.
- Open
BumbleBee.ipynbin Jupyter or VS Code. - Run the notebook cells in order.
- The notebook downloads the dataset, prepares batches, builds the model, trains it, and then generates sample text.
- Python 3.8+
- PyTorch
- Jupyter Notebook / JupyterLab or VS Code Notebook support
- This is an educational implementation, not intended for production.
- The notebook includes exploratory sections on attention, matrix multiplication, and layer normalization.
- The smaller TinyStories dataset is used because of hardware constraints.