NightPrince commited on
Commit
8abaade
·
verified ·
1 Parent(s): da6e172

Create train.py

Browse files
Files changed (1) hide show
  1. train.py +46 -0
train.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
3
+ from peft import get_peft_model, LoraConfig, TaskType
4
+ import os
5
+
6
+ # Load SST2 dataset from GLUE (binary sentiment classification)
7
+ dataset = load_dataset("glue", "sst2")
8
+
9
+ # Use a small subset to stay within 25-minute budget
10
+ small_train = dataset["train"].select(range(500))
11
+
12
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
13
+
14
+ def tokenize_fn(batch):
15
+ return tokenizer(batch["sentence"], padding=True, truncation=True)
16
+
17
+ tokenized_train = small_train.map(tokenize_fn, batched=True)
18
+
19
+ # Load model and apply LoRA
20
+ model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=2)
21
+ peft_config = LoraConfig(task_type=TaskType.SEQ_CLS, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1)
22
+ model = get_peft_model(model, peft_config)
23
+
24
+ # Hugging Face token from environment or manually
25
+ hf_token = os.environ.get("HF_TOKEN") or "hf_xxx" # replace with real token or set in Space secrets
26
+
27
+ # Training arguments
28
+ training_args = TrainingArguments(
29
+ output_dir="results",
30
+ per_device_train_batch_size=8,
31
+ num_train_epochs=1,
32
+ logging_dir="./logs",
33
+ logging_steps=10,
34
+ save_strategy="epoch",
35
+ push_to_hub=True,
36
+ hub_model_id="NightPrince/peft-distilbert-sst2",
37
+ hub_token=hf_token,
38
+ )
39
+
40
+ trainer = Trainer(
41
+ model=model,
42
+ args=training_args,
43
+ train_dataset=tokenized_train,
44
+ )
45
+
46
+ trainer.train()