-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mt5-fine-tuning.py
58 lines (41 loc) · 1.76 KB
/
Mt5-fine-tuning.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#Script that runs the Fine tuning separetely in order to create CUDA statistics
#Needs to be run separate in order to not take too much GPU memory.
#Author: JP Rivera
import torch
import pickle
import argparse
from transformers import TrainingArguments, Trainer
import numpy as np
from transformers import MT5ForConditionalGeneration, Trainer, TrainingArguments
# Define the compute metrics function
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
return {"accuracy": (predictions == labels).mean()}
def main(model_path: str, train_data_path: str, val_data_path: str, training_args_path: str):
#model = torch.load(model_path) # Load the model
#model = AutoModel.from_pretrained(model_path)
model = MT5ForConditionalGeneration.from_pretrained(model_path)
training_args = torch.load(training_args_path) # Load the training arguments
with open(train_data_path, 'rb') as f:
model_train_data = pickle.load(f)
with open(val_data_path, 'rb') as f:
model_val_data = pickle.load(f)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=model_train_data,
eval_dataset=model_val_data,
compute_metrics=compute_metrics
)
# Train the model
trainer.train()
#Main method acctps inpus args
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_path", type=str)
parser.add_argument("--train_data_path", type=str)
parser.add_argument("--val_data_path", type=str)
parser.add_argument("--training_args_path", type=str)
args = parser.parse_args()
main(args.model_path, args.train_data_path, args.val_data_path, args.training_args_path)