Update README.md
Browse files1234567891011121314151617181920212223242526272829303132333435362 unmodified lines39404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139---language: enlicense: apache-2.0tags:- gpt2- instruction-tuning- text-generation- causal-lm- pytorch- safetensorslibrary_name: transformers---# GPT-2 Medium Instruct**406M parameter GPT-2 model fine-tuned for instruction following.**This is a fine-tuned version of **GPT-2 Medium** trained on the Alpaca dataset to improve its ability to follow user instructions and generate helpful responses.## Model Details- **Model Name**: `snehangshu511/gpt2-medium-instruct`- **Base Model**: [gpt2-medium](https://huggingface.co/gpt2-medium)- **Parameters**: 406 Million- **Context Length**: 1,024 tokens- **Vocabulary Size**: 50,257- **Training Dataset**: Alpaca (cleaned) β 52K instruction-response pairs- **Fine-tuning Framework**: PyTorch Lightning- **License**: Apache 2.0## Quick Start### Load and Generate```pythonfrom transformers import GPT2LMHeadModel, GPT2Tokenizer2 unmodified linesmodel_id = "snehangshu511/gpt2-medium-instruct"tokenizer = GPT2Tokenizer.from_pretrained(model_id)model = GPT2LMHeadModel.from_pretrained(model_id)# Move to GPU if availabledevice = "cuda" if torch.cuda.is_available() else "cpu"model = model.to(device)prompt = """Below is an instruction that describes a task. Write a response that appropriately completes the request.### Instruction:What is machine learning?### Response:"""inputs = tokenizer(prompt, return_tensors="pt").to(device)outputs = model.generate(**inputs,max_new_tokens=150,temperature=0.7,top_p=0.9,top_k=50,repetition_penalty=1.2,do_sample=True,pad_token_id=tokenizer.eos_token_id,eos_token_id=tokenizer.eos_token_id,)print(tokenizer.decode(outputs[0], skip_special_tokens=True))```## Training Information- **Dataset**: Alpaca (cleaned version)- **Epochs**: 1- **Steps**: 5,000- **Hardware**: Google Colab (T4 GPU)- **Final Training Metrics**:- Train Loss (step): 1.549- Train Loss (eval): 1.240- Val Loss (eval): 1.413- Val Loss (step): 1.561## Intended UseThis model is suitable for:- Instruction following tasks- Question answering- Creative writing assistance- Educational demonstrations- Research on instruction tuning of smaller language models## Limitations- Based on the older GPT-2 architecture (2019)- Limited context window (1024 tokens)- Smaller capacity compared to modern models (Llama, Mistral, Gemma, etc.)- May generate factually incorrect or biased content- Primarily trained on English data## Bias and Safety NoticeThis model has **not** been aligned using RLHF or safety fine-tuning. It may produce harmful, biased, or inappropriate content. Use responsibly and apply additional filtering when needed.## Example Outputs**Input:**```### Instruction:Write a short poem about artificial intelligence.### Response:```(The model will generate a creative response.)## Citation```bibtex@misc{snehangshu5112026gpt2mediuminstruct,author = {Snehangshu},title = {GPT-2 Medium Instruct},year = {2026},publisher = {Hugging Face},howpublished = {\url{https://huggingface.co/snehangshu511/gpt2-medium-instruct}}}```## Acknowledgements- Original GPT-2 by OpenAI- Alpaca dataset by Stanford CRFM- Hugging Face ecosystem for easy sharing and inference---**Model Repository**: [https://huggingface.co/snehangshu511/gpt2-medium-instruct](https://huggingface.co/snehangshu511/gpt2-medium-instruct)Feel free to use this model for research, learning, and experimentation!1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021032 unmodified lines106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260---language: enlicense: mitbase_model: openai-community/gpt2-mediumtags:- gpt2- instruction-tuning- text-generation- alpaca- pytorch-lightning- causal-lmdatasets:- yahma/alpaca-cleanedpipeline_tag: text-generationmodel_name: gpt2-medium-instruct---# GPT-2 Medium InstructA **355M parameter GPT-2 Medium** model fine-tuned from scratch on the `yahma/alpaca-cleaned` instruction dataset. Built as a learning project following Sebastian Raschka's *"Build a Large Language Model from Scratch"* book, with a full custom training pipeline in PyTorch Lightning.---## Model Details| Property | Value ||---|---|| **Base model** | `openai-community/gpt2-medium` || **Parameters** | ~355M || **Architecture** | GPT-2 (decoder-only transformer) || **Fine-tuning dataset** | `yahma/alpaca-cleaned` (10,000 training samples) || **Context length** | 1,024 tokens || **Vocabulary size** | 50,257 tokens || **Embedding dim** | 1,024 || **Transformer layers** | 24 || **Attention heads** | 16 || **Tokenizer** | GPT-2 BPE (via `tiktoken` / HF `GPT2Tokenizer`) |---## Training Details### DatasetThe model was fine-tuned on the [`yahma/alpaca-cleaned`](https://huggingface.co/datasets/yahma/alpaca-cleaned) dataset β a cleaned version of Stanford Alpaca's 52K instruction-following data generated from `text-davinci-003`.| Split | Samples ||---|---|| Train | 10,000 || Validation | 1,000 || Test | 1,000 |### Prompt FormatThe model uses the standard **Alpaca prompt template**:```Below is an instruction that describes a task. Write a response that appropriately completes the request.### Instruction:{instruction}### Input:{input} β omitted if empty### Response:{output}```During training, the **instruction + input portion is masked** with `-100` in the targets so the loss is only computed on the response tokens. This is the standard technique to make the model learn *how to respond* rather than memorize the prompt structure.### Optimizer| Hyperparameter | Value ||---|---|| Optimizer | AdamW || Learning rate | `3e-5` || Weight decay | `0.1` || Beta1 / Beta2 | `0.9` / `0.95` || Gradient clip | `1.0` |### Training Config| Setting | Value ||---|---|| Framework | PyTorch Lightning || Epochs | 2 (+ 1 continuation epoch) || Batch size (per device) | 2 || Gradient accumulation steps | 4 || Effective batch size | 8 || Precision | `16-mixed` (FP16 + FP32) || Hardware | Single GPU (Colab) || Early stopping patience | 3 validation checks || Checkpoint metric | `val_loss_eval` (minimize) |---## Usage### Basic Inference```pythonfrom transformers import GPT2LMHeadModel, GPT2Tokenizer2 unmodified linesmodel_id = "snehangshu511/gpt2-medium-instruct"tokenizer = GPT2Tokenizer.from_pretrained(model_id)model = GPT2LMHeadModel.from_pretrained(model_id)model.eval()def build_prompt(instruction, input_text=""):base = ("Below is an instruction that describes a task. ""Write a response that appropriately completes the request.\n\n")if input_text.strip():return f"{base}### Instruction:\n{instruction}\n\n### Input:\n{input_text}\n\n### Response:\n"return f"{base}### Instruction:\n{instruction}\n\n### Response:\n"prompt = build_prompt("Explain what machine learning is in simple terms.")inputs = tokenizer(prompt, return_tensors="pt")with torch.no_grad():output_ids = model.generate(**inputs,max_new_tokens=200,do_sample=True,temperature=0.7,top_p=0.9,top_k=50,repetition_penalty=1.2,pad_token_id=tokenizer.eos_token_id,eos_token_id=tokenizer.eos_token_id,)# Decode only the newly generated tokens (strip the prompt)input_len = inputs["input_ids"].shape[1]response = tokenizer.decode(output_ids[0][input_len:], skip_special_tokens=True)print(response)```### With Optional Input Context```pythonprompt = build_prompt(instruction="Summarize the following text.",input_text="The Industrial Revolution began in Britain in the 18th century...")```### Recommended Generation Settings| Setting | Recommended range | Effect ||---|---|---|| `temperature` | 0.6 β 0.9 | Higher = more creative, lower = more deterministic || `top_p` | 0.85 β 0.95 | Nucleus sampling β limits token pool to top P% probability mass || `top_k` | 40 β 60 | Hard limits candidate tokens to top K at each step || `repetition_penalty` | 1.1 β 1.3 | Higher = less repetition in output || `max_new_tokens` | 100 β 300 | Keep under 800 to stay within the 1024 context window |---## Architecture NotesThis model was built **from scratch** using a custom `GPTModel` class (no `AutoModel` during training). The weights were converted from the custom format to HF-compatible `GPT2LMHeadModel` format for this Hub upload.Key architectural decisions:- **Weight tying disabled** (`tie_word_embeddings=False`): In standard GPT-2, the output head shares weights with the embedding layer. During conversion, `lm_head.weight` was explicitly cloned to avoid shared-memory issues with `safetensors`. The config reflects this.- **QKV separation**: The custom training model stores Q, K, V as separate linear layers. During HF conversion, they are re-fused into the standard `c_attn` format that `GPT2LMHeadModel` expects.- **Drop rate = 0.0**: Dropout is disabled during fine-tuning, which is standard practice when working with pretrained models on relatively small datasets.---## Files in This Repository| File | Description ||---|---|| `model.safetensors` | Model weights in safetensors format (recommended) || `pytorch_model.bin` | Model weights in legacy `.bin` format || `config.json` | GPT2Config β model architecture definition || `generation_config.json` | Default generation settings || `tokenizer.json` | Fast tokenizer file || `tokenizer_config.json` | Tokenizer configuration || `vocab.json` | BPE vocabulary || `merges.txt` | BPE merge rules || `checkpoints/model.ckpt` | Original PyTorch Lightning training checkpoint |---## Limitations- **Small training subset**: Only 10,000 of the available ~52,000 Alpaca samples were used. A full dataset run would likely yield noticeably better results.- **GPT-2 base**: GPT-2 Medium, while a solid model, is much smaller than modern instruction-tuned LLMs. Responses can be inconsistent or drift from the prompt on complex tasks.- **No RLHF**: The model is instruction-tuned via supervised fine-tuning only β no reinforcement learning from human feedback. It may produce responses that are grammatically correct but factually wrong.- **Context length**: Hard-limited to 1,024 tokens. Long prompts can get truncated.- **No safety alignment**: There is no safety filtering or RLHF alignment. Do not deploy in production without additional safety measures.---## Training Pipeline Summary```yahma/alpaca-cleaned (52K rows)β load 10K rowsAlpaca prompt formattingβtiktoken BPE tokenizationβ -100 masking on prompt tokensCustom PyTorch Dataset + DataLoader (dynamic padding)βGPT-2 Medium pretrained weights loaded from openai-community/gpt2-mediumβPyTorch Lightning fine-tuning- AdamW, lr=3e-5, 2 epochs- FP16 mixed precision- Gradient accumulation (eff. batch = 8)- Checkpoint on best val_lossβLightning prefix stripped β raw GPTModel state dictβCustom β HF format conversion (QKV fusing, key renaming)βSaved as model.safetensors + pytorch_model.binβPushed to snehangshu511/gpt2-medium-instruct```---## CitationIf you use this model, please also cite the resources it was built from:```bibtex@book{raschka2024llms,title = {Build a Large Language Model (From Scratch)},author = {Sebastian Raschka},year = {2024},publisher = {Manning Publications}}@misc{alpaca,title = {Stanford Alpaca: An Instruction-following LLaMA model},author = {Taori et al.},year = {2023},url = {https://github.com/tatsu-lab/stanford_alpaca}}```---## Author**Snehangshu Bhuin** β Data ScientistGitHub: [snehangshu2002](https://github.com/snehangshu2002)Built as part of ongoing LLM learning and portfolio development.
README.md+212-91