-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
95 lines (69 loc) · 2.98 KB
/
train.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# call this script to train the model and show the evaluation results .
import os
import argparse
import torch
import transformers
from sklearn.model_selection import train_test_split
from dataset import dataset
from model import get_model,load_model
from configs import config
from engine import train_model,eval_model
from prepare import prepare
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--jsonl',type=str)
parser.add_argument('--train',type=str)
parser.add_argument('--val',type=str)
parser.add_argument('--test',type=str)
parser.add_argument('--batch_size',type=int)
parser.add_argument('--lr',type=float)
parser.add_argument('--epochs',type=int)
parser.add_argument('--model_path',type=str)
parser.add_argument('--model_type',type=str)
parser.add_argument('--shuffle',type=bool)
parser.add_argument('--save_model',type=str)
parser.add_argument('--training_method',type=int)
hp = parser.parse_args()
params = {'batch_size': config.BATCH_SIZE,
'shuffle': config.SHUFFLE,
'num_workers': config.NO_OF_WORKERS}
if hp.batch_size:
params['batch_size']=hp.batch_size
if hp.epochs:
config.EPOCHS=hp.epochs
if hp.lr:
config.LR=hp.lr
if hp.shuffle:
params['shuffle']=hp.shuffle
model_type=0
if hp.model_type and ('roberta' in hp.model_type.lower()):
model_type=1
if hp.model_path :
model=load_model(hp.model_path)
else:
model = get_model(model_type)
if hp.jsonl is None:
if not (hp.train and hp.val):
print('Pass the train.csv and val.csv path')
exit()
train_dataset = dataset(hp.train)
train_dataloader = torch.utils.data.DataLoader(dataset=train_dataset, **params)
valid_dataset = dataset(hp.val)
valid_dataloader = torch.utils.data.DataLoader(dataset=train_dataset, **params)
else:
prepare(hp.jsonl).run(hp.training_method)
train_dataset = dataset(
os.path.join(self.target_path,'train%d.csv'%(hp.training_method)))
train_dataloader = torch.utils.data.DataLoader(dataset=train_dataset, **params)
valid_dataset = dataset(
os.path.join(self.target_path,'val%d.csv'%(hp.training_method)))
valid_dataloader = torch.utils.data.DataLoader(dataset=train_dataset, **params)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Using device:", device)
train_model(train_dataloader,model,device,1,valid_dataloader)
eval_model(valid_dataloader,model,device)
#save the model to the config part
if not hp.save_model:
model.save_pretrained(os.path.join(config.SAVE_MODEL,'%s_model_%d'%(config.MODEL_LIST[model_type],config.EPOCHS)))
else:
model.save_pretrained(os.path.join(hp.save_model,'%s_model_%d'%(config.MODEL_LIST[model_type],config.EPOCHS)))