This is a self project where I build a GPT-style language model in PyTorch from the core architecture up, then fine-tune it for two practical tasks: SMS spam classification and instruction-following response generation.
- A GPT-style decoder-only Transformer implemented from scratch in
gpt2.py. - Core model blocks: token embeddings, positional embeddings, causal multi-head self-attention, feed-forward layers, residual connections, layer normalization, and language modeling head.
- A next-token prediction training loop in
training_and_evaluation.py. - Text generation with temperature and top-k sampling.
- GPT-2 checkpoint loading so pretrained OpenAI weights can be used with my own PyTorch model implementation.
- SMS spam classifier fine-tuning in
fine_tune_classifier.pyby replacing the language modeling head with a binary classification head. - Instruction fine-tuning in
fine_tune_instruction.pyusing instruction/input/response formatted examples. - LLM-based response evaluation in
fine_tune_instruction_llm_evaluation.py.
gpt2.py # GPT model architecture from scratch
dataset.py # Pretraining dataset and dataloader
training_and_evaluation.py # Training loop, evaluation, and text generation
gpt_download.py # Download and load GPT-2 checkpoint weights
fine_tune_classifier.py # Spam classifier fine-tuning
fine_tune_instruction.py # Instruction fine-tuning
fine_tune_instruction_llm_evaluation.py # LLM judge evaluation for generated responses
Install dependencies:
pip install torch tiktoken numpy pandas requests tensorflow tqdm openaiTrain the small GPT model from scratch:
python training_and_evaluation.pyFine-tune as a spam classifier:
python fine_tune_classifier.pyFine-tune for instruction following:
python fine_tune_instruction.py