9
from transformers import RobertaTokenizer, RobertaForMaskedLM, RobertaForSequenceClassification
10
from transformers.modeling_roberta import RobertaForMaskedLMDomainTask
11
from tqdm import tqdm, trange
12
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
13
from torch.utils.data.distributed import DistributedSampler
14
from transformers.file_utils import PYTORCH_PRETRAINED_BERT_CACHE
15
from transformers.optimization import AdamW, get_linear_schedule_with_warmup
19
logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s',
20
datefmt = '%m/%d/%Y %H:%M:%S',
22
logger = logging.getLogger(__name__)
24
def accuracy(out, labels):
25
outputs = np.argmax(out, axis=1)
26
return np.sum(outputs == labels), outputs
28
class InputFeatures(object):
29
"""A single set of features of data."""
31
def __init__(self, input_ids=None, attention_mask=None, segment_ids=None, label_id=None):
32
self.input_ids = input_ids
33
self.attention_mask = attention_mask
34
self.segment_ids = segment_ids
35
self.label_id = label_id
37
class InputExample(object):
38
"""A single training/test example for simple sequence classification."""
39
def __init__(self, guid, sentence, aspect, sentiment=None):
40
"""Constructs a InputExample.
43
guid: Unique id for the example.
44
text_a: string. The untokenized text of the first sequence. For single
45
sequence tasks, only this sequence must be specified.
46
text_b: (Optional) string. The untokenized text of the second sequence.
47
Only must be specified for sequence pair tasks.
48
label: (Optional) string. The label of the example. This should be
49
specified for train and dev examples, but not for test examples.
52
self.sentence = sentence
54
self.sentiment = sentiment
57
class DataProcessor(object):
58
"""Base class for data converters for sequence classification data sets."""
60
def get_train_examples(self, data_dir):
61
"""Gets a collection of `InputExample`s for the train set."""
62
raise NotImplementedError()
64
def get_dev_examples(self, data_dir):
65
"""Gets a collection of `InputExample`s for the dev set."""
66
raise NotImplementedError()
69
"""Gets the list of labels for this data set."""
70
raise NotImplementedError()
73
def _read_json(cls, input_file):
74
with open(input_file, "r", encoding='utf-8') as f:
75
return json.loads(f.read())
78
class Processor_1(DataProcessor):
79
"""Processor for the CoLA data set (GLUE version)."""
81
def get_train_examples(self, data_dir):
83
examples = self._create_examples(
84
self._read_json(os.path.join(data_dir, "train.json")), "train")
85
aspect = set([x.aspect for x in examples])
86
sentiment = set([x.sentiment for x in examples])
87
return examples, list(aspect), list(sentiment)
89
def get_dev_examples(self, data_dir):
91
examples = self._create_examples(
92
self._read_json(os.path.join(data_dir, "dev.json")), "dev")
93
aspect = set([x.aspect for x in examples])
94
sentiment = set([x.sentiment for x in examples])
95
return examples, list(aspect), list(sentiment)
97
def get_test_examples(self, data_dir):
99
examples = self._create_examples(
100
self._read_json(os.path.join(data_dir, "test.json")), "test")
101
aspect = set([x.aspect for x in examples])
102
sentiment = set([x.sentiment for x in examples])
103
return examples, list(aspect), list(sentiment)
105
def get_labels(self):
109
def _create_examples(self, lines, set_type):
110
"""Creates examples for the training and dev sets."""
112
for (i, line) in enumerate(lines):
113
guid = "%s-%s" % (set_type, i)
115
sentence = line["sentence"]
116
aspect = line["aspect"]
117
sentiment = line["sentiment"]
120
InputExample(guid=guid, sentence=sentence, aspect=aspect, sentiment=sentiment))
123
def convert_examples_to_features(examples, aspect_list, sentiment_list, max_seq_length, tokenizer, task_n):
125
"""Loads a data file into a list of `InputBatch`s."""
130
label_list = sorted(aspect_list)
132
label_list = sorted(sentiment_list)
137
print(w,tokenizer.encode(w))
140
label_map = {label : i for i, label in enumerate(label_list)}
147
for (ex_index, example) in enumerate(examples):
151
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
152
model = GPT2Model.from_pretrained('gpt2')
153
special_tokens_dict = {'cls_token': '<CLS>'}
154
num_added_toks = tokenizer.add_special_tokens(special_tokens_dict)
155
print('We have added', num_added_toks, 'tokens')
156
model.resize_token_embeddings(len(tokenizer))
160
print(tokenizer.all_special_tokens)
161
print(tokenizer.encode(tokenizer.all_special_tokens))
162
#['[PAD]', '[SEP]', '[CLS]', '[MASK]', '[UNK]']
163
#[ 0, 102, 101, 103, 100]
189
input_ids = tokenizer.encode(example.sentence,add_special_tokens=True)
190
segment_ids = [0] * len(input_ids)
195
input_ids += input_ids + [102]
203
attention_mask = [1] * len(input_ids)
206
padding = [0] * (max_seq_length - len(input_ids))
208
attention_mask += padding
209
segment_ids += padding
211
assert len(input_ids) == max_seq_length
212
assert len(attention_mask) == max_seq_length
213
assert len(segment_ids) == max_seq_length
216
label_id = label_map[example.aspect]
218
label_id = label_map[example.sentiment]
225
InputFeatures(input_ids=input_ids,
226
attention_mask=attention_mask,
231
InputFeatures(input_ids=input_ids,
232
attention_mask=attention_mask,
233
segment_ids=segment_ids,
236
print("Wrong in convert_examples")
243
parser = argparse.ArgumentParser()
246
parser.add_argument("--data_dir",
250
help="The input data dir. Should contain the .tsv files (or other data files) for the task.")
251
parser.add_argument("--output_dir",
255
help="The output directory where the model predictions and checkpoints will be written.")
256
parser.add_argument("--pretrain_model",
257
default='bert-case-uncased',
260
help="Pre-trained model")
261
parser.add_argument("--num_labels_task",
262
default=None, type=int,
264
help="num_labels_task")
265
parser.add_argument("--max_seq_length",
268
help="The maximum total input sequence length after WordPiece tokenization. \n"
269
"Sequences longer than this will be truncated, and sequences shorter \n"
270
"than this will be padded.")
271
parser.add_argument("--do_train",
274
help="Whether to run training.")
275
parser.add_argument("--do_eval",
278
help="Whether to run eval on the dev set.")
279
parser.add_argument("--do_lower_case",
282
help="Set this flag if you are using an uncased model.")
283
parser.add_argument("--eval_batch_size",
286
help="Total batch size for training.")
287
parser.add_argument("--learning_rate",
290
help="The initial learning rate for Adam.")
291
parser.add_argument("--num_train_epochs",
294
help="Total number of training epochs to perform.")
295
parser.add_argument("--warmup_proportion",
298
help="Proportion of training to perform linear learning rate warmup for. "
299
"E.g., 0.1 = 10%% of training.")
300
parser.add_argument("--no_cuda",
303
help="Whether not to use CUDA when available")
304
parser.add_argument("--local_rank",
307
help="local_rank for distributed training on gpus")
308
parser.add_argument('--seed',
311
help="random seed for initialization")
312
parser.add_argument('--gradient_accumulation_steps',
315
help="Number of updates steps to accumulate before performing a backward/update pass.")
316
parser.add_argument('--fp16',
319
help="Whether to use 16-bit float precision instead of 32-bit")
320
parser.add_argument('--loss_scale',
321
type=float, default=0,
322
help="Loss scaling to improve fp16 numeric stability. Only used when fp16 set to True.\n"
323
"0 (default value): dynamic loss scaling.\n"
324
"Positive power of 2: static loss scaling value.\n")
325
parser.add_argument("--weight_decay",
328
help="Weight decay if we apply some.")
329
parser.add_argument("--adam_epsilon",
332
help="Epsilon for Adam optimizer.")
333
parser.add_argument("--max_grad_norm",
336
help="Max gradient norm.")
337
parser.add_argument('--fp16_opt_level',
340
help="For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']."
341
"See details at https://nvidia.github.io/apex/amp.html")
342
parser.add_argument("--task",
349
args = parser.parse_args()
353
processors = Processor_1
355
num_labels = args.num_labels_task
357
if args.local_rank == -1 or args.no_cuda:
358
device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
359
n_gpu = torch.cuda.device_count()
361
torch.cuda.set_device(args.local_rank)
362
device = torch.device("cuda", args.local_rank)
365
torch.distributed.init_process_group(backend='nccl')
366
logger.info("device: {}, n_gpu: {}, distributed training: {}, 16-bits training: {}".format(
367
device, n_gpu, bool(args.local_rank != -1), args.fp16))
371
if args.gradient_accumulation_steps < 1:
372
raise ValueError("Invalid gradient_accumulation_steps parameter: {}, should be >= 1".format(
373
args.gradient_accumulation_steps))
377
random.seed(args.seed)
378
np.random.seed(args.seed)
379
torch.manual_seed(args.seed)
381
torch.cuda.manual_seed_all(args.seed)
384
raise ValueError("At least one of `do_train` or `do_eval` must be True.")
387
if os.path.exists(args.output_dir) and os.listdir(args.output_dir) and args.do_train:
388
raise ValueError("Output directory ({}) already exists and is not empty.".format(args.output_dir))
390
os.makedirs(args.output_dir, exist_ok=True)
394
tokenizer = RobertaTokenizer.from_pretrained(args.pretrain_model)
397
train_examples = None
398
num_train_steps = None
400
sentiment_list = None
401
processor = processors()
402
num_labels = num_labels
405
filenames = os.listdir(args.output_dir)
406
filenames = [x for x in filenames if "pytorch_model.bin_" in x]
411
file_mark.append([x, True])
412
file_mark.append([x, False])
416
test_examples, aspect_list, sentiment_list = processor.get_test_examples(args.data_dir)
418
num_labels = len(aspect_list)
420
num_labels = len(sentiment_list)
422
print("What's task?")
424
test = convert_examples_to_features(
425
test_examples, aspect_list, sentiment_list, args.max_seq_length, tokenizer, args.task)
426
eval_examples = test_examples
430
for x, mark in file_mark:
432
output_model_file = os.path.join(args.output_dir, x)
433
print(output_model_file)
436
model = RobertaForMaskedLMDomainTask.from_pretrained(args.pretrain_model, output_hidden_states=False, output_attentions=False, return_dict=True, num_labels=args.num_labels_task)
437
model.load_state_dict(torch.load(output_model_file), strict=False)
451
logger.info("***** Running evaluation *****")
452
logger.info(" Num examples = %d", len(eval_examples))
453
logger.info(" Batch size = %d", args.eval_batch_size)
456
all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
457
all_attention_mask = torch.tensor([f.attention_mask for f in eval_features], dtype=torch.long)
459
print("Excuting the task 1")
461
all_segment_ids = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
465
all_label_ids = torch.tensor([f.label_id for f in eval_features], dtype=torch.long)
468
eval_data = TensorDataset(all_input_ids, all_attention_mask, all_label_ids)
470
eval_data = TensorDataset(all_input_ids, all_attention_mask, all_segment_ids, all_label_ids)
474
if args.local_rank == -1:
475
eval_sampler = RandomSampler(eval_data)
477
eval_sampler = DistributedSampler(eval_data)
478
eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)
481
output_eval_file = os.path.join(args.output_dir, "eval_results_{}.txt".format(x.split("_")[-1]))
482
output_file_pred = os.path.join(args.output_dir, "eval_pred_{}.txt".format(x.split("_")[-1]))
483
output_file_glod = os.path.join(args.output_dir, "eval_gold_{}.txt".format(x.split("_")[-1]))
485
output_eval_file = os.path.join(args.output_dir, "test_results_{}.txt".format(x.split("_")[-1]))
486
output_file_pred = os.path.join(args.output_dir, "test_pred_{}.txt".format(x.split("_")[-1]))
487
output_file_glod = os.path.join(args.output_dir, "test_gold_{}.txt".format(x.split("_")[-1]))
489
fpred = open(output_file_pred, "w")
490
fgold = open(output_file_glod, "w")
493
eval_loss, eval_accuracy = 0, 0
494
nb_eval_steps, nb_eval_examples = 0, 0
497
for step, batch in enumerate(tqdm(eval_dataloader, desc="Iteration")):
499
batch = tuple(t.to(device) for i, t in enumerate(batch))
502
input_ids, attention_mask, label_ids = batch
504
input_ids, attention_mask, segment_ids, label_ids = batch
512
output = model(input_ids=input_ids, token_type_ids=None, attention_mask=attention_mask, labels=label_ids)
513
logits = output.logits
514
tmp_eval_loss = output.loss
517
tmp_eval_loss, logits = model(input_ids_org=input_ids, sentence_label=label_ids, attention_mask=attention_mask, func="task_class")
523
output = model(input_ids=input_ids, token_type_ids=None, attention_mask=attention_mask, labels=label_ids)
524
logits = output.logits
525
tmp_eval_loss = output.loss
528
tmp_eval_loss, logits = model(input_ids_org=input_ids, sentence_label=label_ids, attention_mask=attention_mask, func="task_class")
536
logits = logits.detach().cpu().numpy()
537
label_ids = label_ids.to('cpu').numpy()
538
tmp_eval_accuracy, pred = accuracy(logits, label_ids)
539
for a, b in zip(pred, label_ids):
540
fgold.write("{}\n".format(b))
541
fpred.write("{}\n".format(a))
543
eval_loss += tmp_eval_loss.mean().item()
544
eval_accuracy += tmp_eval_accuracy
546
nb_eval_examples += input_ids.size(0)
549
eval_loss = eval_loss / nb_eval_steps
550
eval_accuracy = eval_accuracy / nb_eval_examples
552
result = {'eval_loss': eval_loss,
553
'eval_accuracy': eval_accuracy
556
with open(output_eval_file, "w") as writer:
557
logger.info("***** Eval results *****")
558
for key in sorted(result.keys()):
559
logger.info(" %s = %s", key, str(result[key]))
560
writer.write("%s = %s\n" % (key, str(result[key])))
564
if __name__ == "__main__":