-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfine_tune.py
42 lines (35 loc) · 1.28 KB
/
fine_tune.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
from transformers import AutoTokenizer, AutoModelForCausalLM, TextDataset, DataCollatorForLanguageModeling
from transformers import Trainer, TrainingArguments
# Load pre-trained model and tokenizer
model_name = "EleutherAI/gpt-neo-125M" # You can change this to a different model if desired
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Prepare your dataset
def load_dataset(file_path, tokenizer):
return TextDataset(
tokenizer=tokenizer,
file_path=file_path,
block_size=128)
train_dataset = load_dataset("path_to_your_training_data.txt", tokenizer)
eval_dataset = load_dataset("path_to_your_eval_data.txt", tokenizer)
# Set up the trainer
training_args = TrainingArguments(
output_dir="./results",
overwrite_output_dir=True,
num_train_epochs=3,
per_device_train_batch_size=4,
save_steps=10_000,
save_total_limit=2,
)
trainer = Trainer(
model=model,
args=training_args,
data_collator=DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False),
train_dataset=train_dataset,
eval_dataset=eval_dataset,
)
# Start fine-tuning
trainer.train()
# Save the fine-tuned model
model.save_pretrained("./fine_tuned_model")
tokenizer.save_pretrained("./fine_tuned_model")