forked from cbitosc/HTF24-Team-155
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfine_tune_model.py
44 lines (35 loc) · 1.2 KB
/
fine_tune_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import pandas as pd
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
from datasets import load_dataset
# Load the dataset
dataset = load_dataset('csv', data_files='path/to/your/dataset.csv') # Update this path
# Load pre-trained model and tokenizer
model_name = "gpt2" # or another model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Tokenize the dataset
def tokenize_function(examples):
return tokenizer(examples['text'], truncation=True)
tokenized_datasets = dataset.map(tokenize_function, batched=True)
# Set training arguments
training_args = TrainingArguments(
output_dir='./results',
evaluation_strategy='epoch',
learning_rate=2e-5,
per_device_train_batch_size=2,
num_train_epochs=3,
weight_decay=0.01,
)
# Create Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets['train'],
eval_dataset=tokenized_datasets['test']
)
# Fine-tune the model
trainer.train()
# Save the model
model.save_pretrained('./fine_tuned_model')
tokenizer.save_pretrained('./fine_tuned_model')
print("Model fine-tuned and saved successfully.")