| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
A comprehensive, hands-on guide to building, training, and fine-tuning a Transformer architecture from the ground up using pure PyTorch.
Author: Simanga Mchunu
Original Article: Towards AI
This project teaches you how to build a GPT-style language model entirely from scratch—no high-level abstractions, no shortcuts. You'll understand every component of the Transformer architecture by implementing it yourself, from tokenization through attention mechanisms to the complete training pipeline.
By the end, you'll have a working LLM capable of generating Coldplay-style lyrics and producing coherent English text.
The model implements a GPT-style decoder-only Transformer with:
Implements scaled dot-product attention with causal masking to prevent the model from attending to future tokens.
Runs multiple attention heads in parallel, each focusing on different aspects of the data, then concatenates results.
Two-layer network with GELU activation that processes attention outputs.
Combines attention, feed-forward, and normalization layers with residual connections.
Complete Transformer model stacking multiple decoder blocks with embeddings and output projection.
After pretraining on IMDb, the model is fine-tuned on Coldplay lyrics with:
settings = {
"learning_rate": 3e-4,
"weight_decay": 0.1,
"num_epochs": 300,
"batch_size": 32,
"warmup_steps": 1500,
"max_lr": 3e-4,
"min_lr": 3e-5,
"eval_freq": 200,
"gradient_clip": 1.0,
"patience": 50,
}
settings_ft = {
"learning_rate": 1e-5,
"weight_decay": 0.01,
"num_epochs": 5,
"batch_size": 4,
"warmup_steps": 100,
}pip install torch transformers datasets tiktokenfrom datasets import load_dataset
import re
ds = load_dataset("stanfordnlp/imdb")
def keep_english_only(text):
return re.sub(r"[^\x00-\x7F]+", "", text)
train_text = " ".join([keep_english_only(t) for t in ds['train']['text']])from torch import nn
model = GPT(
num_heads=8,
vocab_size=5000,
embed_dim=256,
attention_dim=256,
num_blocks=8,
context_length=256,
dropout_rate=0.1
)train_losses, val_losses, tokens = train_model(
model, train_loader, val_loader, device, settings
)token_ids = generate(
model=model,
context=text_to_token_ids("I want something", tokenizer, device),
max_new_tokens=50,
context_length=256
)
print(token_ids_to_text(token_ids, tokenizer))After Pretraining (IMDb):
the movie starts slow and i thought it was going to be boring, but then going to be interesting. the acting is okay, some are boring felt like they just gave up.
After Fine-Tuning (Coldplay):
lights go out and the stars begin to fall i hear your voice across the night. lights are running in circles chasing the echoes. you are the star that keeps me alive. Oh-ooh-oh-ooh oh, oh
. ├── model.py # GPT model architecture ├── attention.py # Attention mechanisms ├── data.py # Data loading and preprocessing ├── train.py # Training loop and utilities ├── generate.py # Text generation ├── requirements.txt # Dependencies ├── checkpoints/ # Saved model weights └── README.md
Prevents the model from attending to future tokens during training, enforcing unidirectional attention for proper autoregressive generation.
Allow gradients to flow directly through skip connections, enabling training of very deep networks without vanishing gradients.
Normalizes activations across features for a single example, stabilizing training and enabling faster convergence.
Gradually increases learning rate from 0 to maximum over initial steps, preventing divergence during early training.
The model tracks:
[1] Vaswani et al. (2017). Attention is all you need. arXiv:1706.03762
[2] Radford et al. (2018). Improving Language Understanding by Generative Pre-Training
[3] Ba, Kiros, Hinton (2016). Layer Normalization. arXiv:1607.06450
Feel free to extend this project with:
MIT License - See LICENSE file for details
Ready to build your own LLM? Clone the repository, follow the installation steps, and start training!
| Back | FazBrowse Home | New Git URL |