-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
378 lines (294 loc) · 14 KB
/
main.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# -*- coding: utf-8 -*-
import argparse
import os
import logging
import time
import pickle
from tqdm import tqdm
import torch
from torch.utils.data import DataLoader
import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from transformers import AdamW, T5ForConditionalGeneration, T5Tokenizer
# from transformers import BertTokenizer, EncoderDecoderModel
from transformers import get_linear_schedule_with_warmup
from data_utils import ABSADataset
from data_utils import read_line_examples_from_file
from eval_utils import compute_scores
logger = logging.getLogger(__name__)
def init_args():
parser = argparse.ArgumentParser()
# basic settings
parser.add_argument("--task", default='asqp', type=str, required=True,
help="The name of the task, selected from: [asqp, tasd, aste]")
parser.add_argument("--dataset", default='rest15', type=str, required=True,
help="The name of the dataset, selected from: [rest15, rest16]")
parser.add_argument("--model_name_or_path", default='t5-base', type=str,
help="Path to pre-trained model or shortcut name")
parser.add_argument("--do_train", action='store_true',
help="Whether to run training.")
parser.add_argument("--do_eval", action='store_true',
help="Whether to run eval on the dev/test set.")
parser.add_argument("--do_direct_eval", action='store_true',
help="Whether to run eval on the dev/test set.")
parser.add_argument("--do_inference", action='store_true',
help="Whether to run inference with trained checkpoints")
# other parameters
parser.add_argument("--max_seq_length", default=128, type=int)
parser.add_argument("--n_gpu", default=0)
parser.add_argument("--train_batch_size", default=16, type=int,
help="Batch size per GPU/CPU for training.")
parser.add_argument("--eval_batch_size", default=16, type=int,
help="Batch size per GPU/CPU for evaluation.")
parser.add_argument('--gradient_accumulation_steps', type=int, default=1,
help="Number of updates steps to accumulate before performing a backward/update pass.")
parser.add_argument("--learning_rate", default=3e-4, type=float)
parser.add_argument("--num_train_epochs", default=30, type=int,
help="Total number of training epochs to perform.")
parser.add_argument('--seed', type=int, default=42,
help="random seed for initialization")
# training details
parser.add_argument("--weight_decay", default=0.0, type=float)
parser.add_argument("--adam_epsilon", default=1e-8, type=float)
parser.add_argument("--warmup_steps", default=0.0, type=float)
args = parser.parse_args()
# set up output dir which looks like './outputs/rest15/'
if not os.path.exists('./outputs'):
os.mkdir('./outputs')
output_dir = f"outputs/{args.dataset}"
if not os.path.exists(output_dir):
os.mkdir(output_dir)
args.output_dir = output_dir
return args
def get_dataset(tokenizer, type_path, args):
return ABSADataset(tokenizer=tokenizer, data_dir=args.dataset,
data_type=type_path, max_len=args.max_seq_length)
class T5FineTuner(pl.LightningModule):
"""
Fine tune a pre-trained T5 model
"""
def __init__(self, hparams, tfm_model, tokenizer):
super(T5FineTuner, self).__init__()
self.hparams = hparams
self.model = tfm_model
self.tokenizer = tokenizer
def is_logger(self):
return True
def forward(self, input_ids, attention_mask=None, decoder_input_ids=None,
decoder_attention_mask=None, labels=None):
return self.model(
input_ids,
attention_mask=attention_mask,
decoder_input_ids=decoder_input_ids,
decoder_attention_mask=decoder_attention_mask,
labels=labels,
)
def _step(self, batch):
lm_labels = batch["target_ids"]
lm_labels[lm_labels[:, :] == self.tokenizer.pad_token_id] = -100
outputs = self(
input_ids=batch["source_ids"],
attention_mask=batch["source_mask"],
labels=lm_labels,
decoder_attention_mask=batch['target_mask']
)
loss = outputs[0]
return loss
def training_step(self, batch, batch_idx):
loss = self._step(batch)
tensorboard_logs = {"train_loss": loss}
return {"loss": loss, "log": tensorboard_logs}
def training_epoch_end(self, outputs):
avg_train_loss = torch.stack([x["loss"] for x in outputs]).mean()
tensorboard_logs = {"avg_train_loss": avg_train_loss}
return {"avg_train_loss": avg_train_loss, "log": tensorboard_logs, 'progress_bar': tensorboard_logs}
def validation_step(self, batch, batch_idx):
loss = self._step(batch)
return {"val_loss": loss}
def validation_epoch_end(self, outputs):
avg_loss = torch.stack([x["val_loss"] for x in outputs]).mean()
tensorboard_logs = {"val_loss": avg_loss}
return {"avg_val_loss": avg_loss, "log": tensorboard_logs, 'progress_bar': tensorboard_logs}
def configure_optimizers(self):
""" Prepare optimizer and schedule (linear warmup and decay) """
model = self.model
no_decay = ["bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": self.hparams.weight_decay,
},
{
"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
"weight_decay": 0.0,
},
]
optimizer = AdamW(optimizer_grouped_parameters, lr=self.hparams.learning_rate, eps=self.hparams.adam_epsilon)
self.opt = optimizer
return [optimizer]
def optimizer_step(self, epoch, batch_idx, optimizer, optimizer_idx, second_order_closure=None):
if self.trainer.use_tpu:
xm.optimizer_step(optimizer)
else:
optimizer.step()
optimizer.zero_grad()
self.lr_scheduler.step()
def get_tqdm_dict(self):
tqdm_dict = {"loss": "{:.4f}".format(self.trainer.avg_loss), "lr": self.lr_scheduler.get_last_lr()[-1]}
return tqdm_dict
def train_dataloader(self):
train_dataset = get_dataset(tokenizer=self.tokenizer, type_path="train", args=self.hparams)
dataloader = DataLoader(train_dataset, batch_size=self.hparams.train_batch_size,
drop_last=True, shuffle=True, num_workers=4)
t_total = (
(len(dataloader.dataset) // (self.hparams.train_batch_size * max(1, len(self.hparams.n_gpu))))
// self.hparams.gradient_accumulation_steps
* float(self.hparams.num_train_epochs)
)
scheduler = get_linear_schedule_with_warmup(
self.opt, num_warmup_steps=self.hparams.warmup_steps, num_training_steps=t_total
)
self.lr_scheduler = scheduler
return dataloader
def val_dataloader(self):
val_dataset = get_dataset(tokenizer=self.tokenizer, type_path="dev", args=self.hparams)
return DataLoader(val_dataset, batch_size=self.hparams.eval_batch_size, num_workers=4)
class LoggingCallback(pl.Callback):
def on_validation_end(self, trainer, pl_module):
logger.info("***** Validation results *****")
if pl_module.is_logger():
metrics = trainer.callback_metrics
# Log results
for key in sorted(metrics):
if key not in ["log", "progress_bar"]:
logger.info("{} = {}\n".format(key, str(metrics[key])))
def on_test_end(self, trainer, pl_module):
logger.info("***** Test results *****")
if pl_module.is_logger():
metrics = trainer.callback_metrics
# Log and save results to file
output_test_results_file = os.path.join(pl_module.hparams.output_dir, "test_results.txt")
with open(output_test_results_file, "w") as writer:
for key in sorted(metrics):
if key not in ["log", "progress_bar"]:
logger.info("{} = {}\n".format(key, str(metrics[key])))
writer.write("{} = {}\n".format(key, str(metrics[key])))
def evaluate(data_loader, model, sents):
"""
Compute scores given the predictions and gold labels
"""
device = torch.device(f'cuda:{args.n_gpu}')
model.model.to(device)
model.model.eval()
outputs, targets = [], []
for batch in tqdm(data_loader):
# need to push the data to device
outs = model.model.generate(input_ids=batch['source_ids'].to(device),
attention_mask=batch['source_mask'].to(device),
max_length=128) # num_beams=8, early_stopping=True)
dec = [tokenizer.decode(ids, skip_special_tokens=True) for ids in outs]
target = [tokenizer.decode(ids, skip_special_tokens=True) for ids in batch["target_ids"]]
outputs.extend(dec)
targets.extend(target)
'''
print("\nPrint some results to check the sanity of generation method:", '\n', '-'*30)
for i in [1, 5, 25, 42, 50]:
try:
print(f'>>Target : {targets[i]}')
print(f'>>Generation: {outputs[i]}')
except UnicodeEncodeError:
print('Unable to print due to the coding error')
print()
'''
scores, all_labels, all_preds = compute_scores(outputs, targets, sents)
results = {'scores': scores, 'labels': all_labels, 'preds': all_preds}
# pickle.dump(results, open(f"{args.output_dir}/results-{args.dataset}.pickle", 'wb'))
return scores
# initialization
args = init_args()
print("\n", "="*30, f"NEW EXP: ASQP on {args.dataset}", "="*30, "\n")
# sanity check
# show one sample to check the code and the expected output
tokenizer = T5Tokenizer.from_pretrained(args.model_name_or_path)
print(f"Here is an example (from the dev set):")
dataset = ABSADataset(tokenizer=tokenizer, data_dir=args.dataset,
data_type='dev', max_len=args.max_seq_length)
data_sample = dataset[7] # a random data sample
print('Input :', tokenizer.decode(data_sample['source_ids'], skip_special_tokens=True))
print('Output:', tokenizer.decode(data_sample['target_ids'], skip_special_tokens=True))
# training process
if args.do_train:
print("\n****** Conduct Training ******")
# initialize the T5 model
tfm_model = T5ForConditionalGeneration.from_pretrained(args.model_name_or_path)
model = T5FineTuner(args, tfm_model, tokenizer)
# checkpoint_callback = pl.callbacks.ModelCheckpoint(
# filepath=args.output_dir, prefix="ckt", monitor='val_loss', mode='min', save_top_k=3
# )
# prepare for trainer
train_params = dict(
default_root_dir=args.output_dir,
accumulate_grad_batches=args.gradient_accumulation_steps,
gpus=args.n_gpu,
gradient_clip_val=1.0,
max_epochs=args.num_train_epochs,
callbacks=[LoggingCallback()],
)
trainer = pl.Trainer(**train_params)
trainer.fit(model)
# save the final model
# model.model.save_pretrained(args.output_dir)
# tokenizer.save_pretrained(args.output_dir)
print("Finish training and saving the model!")
# evaluation
if args.do_direct_eval:
print("\n****** Conduct Evaluating with the last state ******")
# model = T5FineTuner(args)
# print("Reload the model")
# model.model.from_pretrained(args.output_dir)
sents, _ = read_line_examples_from_file(f'data/{args.dataset}/test.txt')
print()
test_dataset = ABSADataset(tokenizer, data_dir=args.dataset,
data_type='test', max_len=args.max_seq_length)
test_loader = DataLoader(test_dataset, batch_size=32, num_workers=4)
# print(test_loader.device)
# compute the performance scores
scores = evaluate(test_loader, model, sents)
# write to file
log_file_path = f"results_log/{args.dataset}.txt"
local_time = time.asctime(time.localtime(time.time()))
exp_settings = f"Datset={args.dataset}; Train bs={args.train_batch_size}, num_epochs = {args.num_train_epochs}"
exp_results = f"F1 = {scores['f1']:.4f}"
log_str = f'============================================================\n'
log_str += f"{local_time}\n{exp_settings}\n{exp_results}\n\n"
if not os.path.exists('./results_log'):
os.mkdir('./results_log')
with open(log_file_path, "a+") as f:
f.write(log_str)
if args.do_inference:
print("\n****** Conduct inference on trained checkpoint ******")
# initialize the T5 model from previous checkpoint
print(f"Load trained model from {args.output_dir}")
print('Note that a pretrained model is required and `do_true` should be False')
tokenizer = T5Tokenizer.from_pretrained(args.output_dir)
tfm_model = T5ForConditionalGeneration.from_pretrained(args.output_dir)
model = T5FineTuner(args, tfm_model, tokenizer)
sents, _ = read_line_examples_from_file(f'data/{args.dataset}/test.txt')
print()
test_dataset = ABSADataset(tokenizer, data_dir=args.dataset,
data_type='test', max_len=args.max_seq_length)
test_loader = DataLoader(test_dataset, batch_size=32, num_workers=4)
# print(test_loader.device)
# compute the performance scores
scores = evaluate(test_loader, model, sents)
# write to file
log_file_path = f"results_log/{args.dataset}.txt"
local_time = time.asctime(time.localtime(time.time()))
exp_settings = f"Datset={args.dataset}; Train bs={args.train_batch_size}, num_epochs = {args.num_train_epochs}"
exp_results = f"F1 = {scores['f1']:.4f}"
log_str = f'============================================================\n'
log_str += f"{local_time}\n{exp_settings}\n{exp_results}\n\n"
if not os.path.exists('./results_log'):
os.mkdir('./results_log')
with open(log_file_path, "a+") as f:
f.write(log_str)