Create train.py
Browse files
train.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import Trainer, TrainingArguments, AutoModelForSequenceClassification, AutoTokenizer
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load datasets
|
| 6 |
+
def load_train_data():
|
| 7 |
+
# Example dataset
|
| 8 |
+
train_dataset = load_dataset('csv', data_files={"train": "datasets/Canstralian/ShellCommands.csv"})
|
| 9 |
+
return train_dataset
|
| 10 |
+
|
| 11 |
+
# Load model and tokenizer
|
| 12 |
+
def load_model_and_tokenizer(model_name):
|
| 13 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2) # Adjust labels
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
+
return model, tokenizer
|
| 16 |
+
|
| 17 |
+
# Preprocessing function
|
| 18 |
+
def preprocess_function(examples, tokenizer):
|
| 19 |
+
return tokenizer(examples['text'], padding=True, truncation=True)
|
| 20 |
+
|
| 21 |
+
# Fine-tuning function
|
| 22 |
+
def fine_tune(model_name="WhiteRabbitNeo/WhiteRabbitNeo-13B-v1"):
|
| 23 |
+
train_data = load_train_data()
|
| 24 |
+
model, tokenizer = load_model_and_tokenizer(model_name)
|
| 25 |
+
|
| 26 |
+
# Tokenize the dataset
|
| 27 |
+
train_data = train_data.map(lambda x: preprocess_function(x, tokenizer), batched=True)
|
| 28 |
+
train_data.set_format(type="torch", columns=["input_ids", "attention_mask", "labels"])
|
| 29 |
+
|
| 30 |
+
# Training arguments
|
| 31 |
+
training_args = TrainingArguments(
|
| 32 |
+
output_dir='./results',
|
| 33 |
+
evaluation_strategy="epoch",
|
| 34 |
+
learning_rate=2e-5,
|
| 35 |
+
per_device_train_batch_size=16,
|
| 36 |
+
num_train_epochs=3,
|
| 37 |
+
weight_decay=0.01,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
trainer = Trainer(
|
| 41 |
+
model=model,
|
| 42 |
+
args=training_args,
|
| 43 |
+
train_dataset=train_data['train'],
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
trainer.train()
|
| 47 |
+
|
| 48 |
+
# Call fine-tuning
|
| 49 |
+
fine_tune()
|