Create train.py
Browse files
train.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
|
| 3 |
+
import evaluate
|
| 4 |
+
|
| 5 |
+
# 1. Load BiScope dataset
|
| 6 |
+
dataset = load_dataset("HanxiGuo/BiScope_Data")
|
| 7 |
+
|
| 8 |
+
# 2. Tokenizer
|
| 9 |
+
MODEL = "microsoft/deberta-v3-small"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 11 |
+
|
| 12 |
+
def preprocess(examples):
|
| 13 |
+
return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=256)
|
| 14 |
+
|
| 15 |
+
encoded_dataset = dataset.map(preprocess, batched=True)
|
| 16 |
+
|
| 17 |
+
# 3. Load model
|
| 18 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL, num_labels=2)
|
| 19 |
+
|
| 20 |
+
# 4. Metrics
|
| 21 |
+
accuracy = evaluate.load("accuracy")
|
| 22 |
+
|
| 23 |
+
def compute_metrics(eval_pred):
|
| 24 |
+
logits, labels = eval_pred
|
| 25 |
+
predictions = logits.argmax(axis=-1)
|
| 26 |
+
return accuracy.compute(predictions=predictions, references=labels)
|
| 27 |
+
|
| 28 |
+
# 5. Training args
|
| 29 |
+
training_args = TrainingArguments(
|
| 30 |
+
output_dir="./results",
|
| 31 |
+
evaluation_strategy="epoch",
|
| 32 |
+
save_strategy="epoch",
|
| 33 |
+
learning_rate=2e-5,
|
| 34 |
+
per_device_train_batch_size=16,
|
| 35 |
+
per_device_eval_batch_size=16,
|
| 36 |
+
num_train_epochs=2,
|
| 37 |
+
weight_decay=0.01,
|
| 38 |
+
push_to_hub=True, # ✅ Upload to HF
|
| 39 |
+
hub_model_id="your-username/biscope-detector"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# 6. Trainer
|
| 43 |
+
trainer = Trainer(
|
| 44 |
+
model=model,
|
| 45 |
+
args=training_args,
|
| 46 |
+
train_dataset=encoded_dataset["train"],
|
| 47 |
+
eval_dataset=encoded_dataset["validation"],
|
| 48 |
+
tokenizer=tokenizer,
|
| 49 |
+
compute_metrics=compute_metrics,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# 7. Train & Push
|
| 53 |
+
trainer.train()
|
| 54 |
+
trainer.push_to_hub()
|