-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_POS.py
190 lines (141 loc) · 7.15 KB
/
main_POS.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
import argparse
import time
from collections import defaultdict
import numpy as np
import torch
from transformers import BertConfig, BertTokenizer, TrainingArguments, Trainer, AutoTokenizer, AutoModel, BertModel
from utils import seed_everything, empty_cuda_cache, compute_metrics
from modeling import JointBERT, JointBERT_POS
from data_loader import LoadDataset
from data_tokenizer import TokenizeDataset, TokenizeDataset_POS
def main(args):
# Parse Argument
TASK = str(args.task)
EPOCH = int(args.epoch)
LR = float(args.lr)
BATCH_SIZE = int(args.batch)
SEED = int(args.seed)
BEST = bool(args.best)
print(f'============================================================')
print(f"{time.strftime('%c', time.localtime(time.time()))}")
print(f'TASK: {TASK}')
print(f'EPOCH: {EPOCH}')
print(f'LR: {LR}')
print(f'BATCH_SIZE: {BATCH_SIZE}')
print(f'SEED: {SEED}')
print(f'BEST: {BEST}\n')
# Set Random Seed
seed_everything(SEED)
# Load Dataset
seq_train = LoadDataset.load_dataset(f'./data/{TASK}/train/seq.in')
seq_dev = LoadDataset.load_dataset(f'./data/{TASK}/dev/seq.in')
seq_test = LoadDataset.load_dataset(f'./data/{TASK}/test/seq.in')
intent_train = LoadDataset.load_dataset(f'./data/{TASK}/train/label')
intent_dev = LoadDataset.load_dataset(f'./data/{TASK}/dev/label')
intent_test = LoadDataset.load_dataset(f'./data/{TASK}/test/label')
intent_labels = LoadDataset.load_dataset(f'./data/{TASK}/intent_label_vocab')
slot_train = LoadDataset.load_dataset(f'./data/{TASK}/train/seq.out', slot = True)
slot_dev = LoadDataset.load_dataset(f'./data/{TASK}/dev/seq.out', slot = True)
slot_test = LoadDataset.load_dataset(f'./data/{TASK}/test/seq.out', slot = True)
slot_labels = LoadDataset.load_dataset(f'./data/{TASK}/slot_label_vocab')
# Label Indexing
intent_word2idx = defaultdict(int, {k: v for v, k in enumerate(intent_labels)})
intent_idx2word = {v: k for v, k in enumerate(intent_labels)}
slot_word2idx = defaultdict(int, {k: v for v, k in enumerate(slot_labels)})
slot_idx2word = {v: k for v, k in enumerate(slot_labels)}
# Load Tokenizer & Model
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
pos_tokenizer = BertTokenizer.from_pretrained("vblagoje/bert-english-uncased-finetuned-pos")
pos_model = BertModel.from_pretrained("vblagoje/bert-english-uncased-finetuned-pos")
pos_model.eval()
pos_model.to('cuda')
for param in pos_model.parameters():
param.requires_grad = False
model_config = BertConfig.from_pretrained("bert-base-uncased", num_labels = len(intent_idx2word), problem_type = "single_label_classification", id2label = intent_idx2word, label2id = intent_word2idx)
model = JointBERT_POS.from_pretrained("bert-base-uncased", config = model_config, intent_labels = intent_labels, slot_labels = slot_labels, pos_model = pos_model)
model.to('cuda');
# Tokenize Datasets
train_dataset = TokenizeDataset_POS(seq_train, intent_train, slot_train, intent_word2idx, slot_word2idx, tokenizer, pos_tokenizer)
dev_dataset = TokenizeDataset_POS(seq_dev, intent_dev, slot_dev, intent_word2idx, slot_word2idx, tokenizer, pos_tokenizer)
test_dataset = TokenizeDataset_POS(seq_test, intent_test, slot_test, intent_word2idx, slot_word2idx, tokenizer, pos_tokenizer)
# Set Training Arguments and Train
arguments = TrainingArguments(
output_dir='checkpoints',
do_train=True,
do_eval=True,
num_train_epochs=EPOCH,
learning_rate = LR,
save_strategy="epoch",
save_total_limit=10,
evaluation_strategy="epoch",
load_best_model_at_end=BEST,
report_to = 'none',
per_device_train_batch_size=BATCH_SIZE,
per_device_eval_batch_size=64,
gradient_accumulation_steps=1,
dataloader_num_workers=0,
fp16=True,
)
trainer = Trainer(
model,
arguments,
train_dataset=train_dataset,
eval_dataset=dev_dataset
)
empty_cuda_cache()
trainer.train()
model.save_pretrained(f"checkpoints/{TASK}_ep{EPOCH}")
# last_model = JointBERT.from_pretrained("./checkpoints/checkpoint-1050", config = model_config, intent_labels = intent_labels, slot_labels = slot_labels)
# Get Intent, Slot Labels
intent_label_ids = []
slot_label_ids = []
with open(f'./data/{TASK}/test/label', 'r', encoding='utf-8') as intent_f, \
open(f'./data/{TASK}/test/seq.out', 'r', encoding='utf-8') as slot_f:
for line in intent_f:
line = line.strip()
intent_label_ids.append(line)
intent_label_ids = np.array(intent_label_ids)
for line in slot_f:
line = line.strip().split()
slot_label_ids.append(line)
# Predict
def predict(model, seqs):
model.to('cpu')
pred_intent_ids = []
pred_slot_ids = []
for i in range(len(seqs)):
input_seq = tokenizer(seqs[i], padding='max_length', max_length=50, truncation=True, return_tensors='pt')
pos_input_seq = pos_tokenizer(seqs[i], padding='max_length', max_length=50, truncation=True, return_tensors='pt')
model.eval()
with torch.no_grad():
_, (intent_logits, slot_logits) = model(input_ids = input_seq['input_ids'],
attention_mask = input_seq['attention_mask'],
token_type_ids = input_seq['token_type_ids'],
pos_input_ids = pos_input_seq['input_ids'],
pos_attention_mask = pos_input_seq['attention_mask'],
pos_token_type_ids = pos_input_seq['token_type_ids'],
)
# Intent
pred_intent_ids.append(intent_idx2word[intent_logits[0].argmax().item()])
# Slot
slot_logits_size = slot_logits[0].shape[0]
slot_logits_mask = np.array(test_dataset[i]['slot_label_ids'][:slot_logits_size]) != -100
slot_logits_clean = slot_logits[0][slot_logits_mask]
pred_slot_ids.append([slot_idx2word[i.item()] for i in slot_logits_clean.argmax(dim=1)])
return np.array(pred_intent_ids), pred_slot_ids
pred_intent_ids, pred_slot_ids = predict(model, seq_test)
print(f"\n{time.strftime('%c', time.localtime(time.time()))}")
res = compute_metrics(pred_intent_ids, intent_label_ids, pred_slot_ids, slot_label_ids)
for k, v in res.items():
print(f'{k:<20}: {v}')
print(f'============================================================\n\n\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--task', default='snips')
parser.add_argument('--epoch', default=30)
parser.add_argument('--lr', default=5e-5)
parser.add_argument('--batch', default=128)
parser.add_argument('--seed', default=1234)
parser.add_argument('--best', default=True)
args = parser.parse_args()
main(args)