Views
No views yet
1LoRA Configuration:
2 - Rank (r): 16
3 - Alpha: 32
4 - Dropout: 0.05
5 - Target Modules: c_attn, c_proj, c_fc
6 - Trainable Parameters: 6.29M (1.74%)
7
8Training Hyperparameters:
9 - Learning Rate: 3e-4
10 - Scheduler: Cosine
11 - Batch Size: 16 per GPU
12 - Gradient Accumulation: 4 steps
13 - Effective Batch Size: 128
14 - Epochs: 5
15 - Mixed Precision: FP16| Metric | Value |
|---|---|
| Validation Perplexity | 20.73 |
| Training Loss | 2.96 |
| Training Time | 1.81h |
| GPU Memory | ~8GB per GPU |
pip install transformers peft torch1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5# Load base model
6base_model = AutoModelForCausalLM.from_pretrained(
7 "gpt2-medium",
8 torch_dtype=torch.float16,
9 device_map="auto"
10)
11
12# Load LoRA weights
13model = PeftModel.from_pretrained(
14 base_model,
15 "shiva9876/gpt2-medium-wikitext2-lora"
16)
17
18# Load tokenizer
19tokenizer = AutoTokenizer.from_pretrained("gpt2-medium")
20
21# Generate text
22prompt = "The future of artificial intelligence"
23inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
24outputs = model.generate(
25 **inputs,
26 max_length=100,
27 temperature=0.8,
28 top_p=0.9,
29 do_sample=True
30)
31
32print(tokenizer.decode(outputs[0], skip_special_tokens=True))1# Merge and save
2merged_model = model.merge_and_unload()
3merged_model.save_pretrained("./merged_model")
4tokenizer.save_pretrained("./merged_model")
5
6# Load merged model directly
7model = AutoModelForCausalLM.from_pretrained("./merged_model")1@misc{gpt2-wikitext2-lora,
2 author = {Shiva Jaiswal},
3 title = {GPT-2 Medium Fine-tuned on WikiText-2 with LoRA},
4 year = {2025},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/shiva9876/gpt2-medium-wikitext2-lora}
7}