-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainer.py
212 lines (150 loc) · 7.72 KB
/
trainer.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import json
import os
import torch
import argparse
import numpy as np
from torch.utils.data import DataLoader
import torch.nn.functional as F
from datetime import datetime
from tqdm import tqdm
from transformers import AutoTokenizer
from sklearn.metrics import classification_report
from transformers import pipeline, set_seed
from os.path import join, abspath, dirname
from data import Classification_Dataset, SentimentPrompt, DetoxicDataset, Sentiment_Suffix
from discriminator import PTuneForLAMA
from prompt_tuning import Prompt_tuning
from distill_tuning import Distill_Tuning
class Trainer(object):
def __init__(self, args):
self.args = args
# self.label_token ={
# "positive":'good',
# "negative":'bad'
# }
self.label_token ={
"positive":'positive',
"negative":'negative'
}
if self.args.tuning_name == "prompt_tuning":
self.label_token ={"positive":'good',"negative":'bad'}
self.model = Prompt_tuning(args, args.template, label_token = self.label_token)
elif self.args.tuning_name == "distill_tuning":
self.label_token ={"positive":'good',"negative":'bad'}
self.model = Distill_Tuning(args, args.template, label_token = self.label_token)
elif self.args.tuning_name == "disc_tuning":
self.label_token ={"positive":'good',"negative":'bad'}
self.model = PTuneForLAMA(args, args.template, label_token = self.label_token)
self.tokenizer = self.model.tokenizer
data_path = args.data_path
if self.args.task_name == "sentiment":
print(self.args.tuning_name)
if self.args.tuning_name == "disc_tuning" or self.args.tuning_name == "distill_tuning":
all_dataset = Classification_Dataset(tokenizer = self.tokenizer, data_dir=data_path, max_length = 30, type_path="train", label_token = self.label_token)
else:
all_dataset = Sentiment_Suffix(tokenizer = self.tokenizer, data_dir=data_path, max_length = 30, task_type= self.args.corpus_type, label_token = self.label_token)
elif self.args.task_name == "detoxic":
print("load detoxic dataset!!!")
if self.args.tuning_name == "disc_tuning" or self.args.tuning_name == "distill_tuning":
all_dataset = DetoxicDataset(tokenizer = self.tokenizer, data_dir=data_path, max_length = 30, type_path="train", label_token = self.label_token)
else:
all_dataset = Sentiment_Suffix(tokenizer = self.tokenizer, data_dir=data_path, max_length = 30, task_type= self.args.corpus_type, label_token = self.label_token)
train_size = int(len(all_dataset) * 0.9)
test_size = len(all_dataset) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(all_dataset, [train_size, test_size])
self.train_loader = DataLoader(train_dataset, args.batch_size, num_workers=2, shuffle=True)
self.test_loader = DataLoader(test_dataset, args.batch_size, num_workers=2, shuffle=True)
def evaluate(self, epoch_idx, evaluate_type):
self.model.eval()
if evaluate_type == 'Test':
loader = self.test_loader
else:
loader = self.dev_loader
labels = []
preds = []
with torch.no_grad():
self.model.eval()
for batch in loader:
self.model.eval()
x = batch[0].cuda().squeeze(1)
musk = batch[1].cuda().long().squeeze(1)
y = batch[2]
pred_ids = self.model.predict(x,musk)
preds += pred_ids
labels += y.tolist()
result = classification_report(labels, preds, output_dict = True)
print(result)
return result
def get_save_path(self):
return join(self.args.out_dir, 'prompt_model')
def get_checkpoint(self, epoch_idx, f1_score):
ckpt_name = "{}_{}_temperature{}_scope_{}_epoch_{}_f1_{}_{}.ckpt".format(self.args.tuning_name ,self.args.corpus_type, self.args.temperature, self.args.ranking_scope, epoch_idx, str(f1_score), str(self.args.template).replace(" ",""))
return {'embedding': self.model.prompt_encoder.state_dict(),
'ckpt_name': ckpt_name,
'args': self.args}
def save(self, best_ckpt):
ckpt_name = best_ckpt['ckpt_name']
path = self.get_save_path()
os.makedirs(path, exist_ok=True)
torch.save(best_ckpt, join(path, ckpt_name))
if self.args.use_lm_finetune:
self.model.model.save_pretrained(str(join(path, ckpt_name))[:-5])
print("Checkpoint {} saved.".format(ckpt_name))
def train(self):
best_dev, early_stop, has_adjusted = 0, 0, True
best_ckpt = None
params = [{'params': self.model.prompt_encoder.parameters(), 'lr':self.args.lr}]
if self.args.use_lm_finetune:
params.append({'params': self.model.model.parameters(), 'lr': 1e-5})
optimizer = torch.optim.Adam(params, weight_decay=self.args.weight_decay)
my_lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=self.args.decay_rate)
stop_count = 0
best_result = 0.0
for epoch_idx in range(self.args.epoch):
tot_loss = 0
count =0
for batch_idx, batch in tqdm(enumerate(self.train_loader)):
self.model.train()
x = batch[0].cuda().squeeze(1)
musk = batch[1].long().cuda().squeeze(1)
y = batch[2].long().cuda()
loss = self.model(x, y, musk)
tot_loss += loss.item()
loss.backward()
torch.cuda.empty_cache()
optimizer.step()
torch.cuda.empty_cache()
optimizer.zero_grad()
print(f"epoch index is {epoch_idx}, and total loss is {tot_loss}")
my_lr_scheduler.step()
# if epoch_idx > -1:
# result = self.evaluate(epoch_idx, 'Test')
# weight_avg =result["weighted avg"]
# f1_score = weight_avg["f1-score"]
# if f1_score > best_result:
# best_ckpt = self.get_checkpoint(epoch_idx,best_result)
# best_result = f1_score
# stop_count = 0
# continue
# else:
# stop_count += 1
# if stop_count>5:
# self.save(best_ckpt)
# break
if epoch_idx >= -1:
if self.args.tuning_name == "prompt_tuning":
result = self.evaluate(epoch_idx, 'Test')
weight_avg =result["weighted avg"]
f1_score = round(weight_avg["f1-score"],2)
best_ckpt = self.get_checkpoint(epoch_idx,round(f1_score,2))
else:
best_ckpt = self.get_checkpoint(epoch_idx,round(tot_loss,2))
self.save(best_ckpt)
def main(relation_id=None):
args = construct_generation_args()
#train stage
trainer = Trainer(args)
trainer.train()
## generation stage
if __name__ == '__main__':
main()