/
dialog.py
325 lines (251 loc) · 9.89 KB
/
dialog.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
import sys
import torch
import argparse
from data_utils import load_dialog_task, vectorize_data, load_candidates, vectorize_candidates, tokenize
from six.moves import range, reduce
from itertools import chain
import numpy as np
import os
from sklearn import metrics
from torch.autograd import Variable as V
from model.mem_cnn_sim import MemCnnSim
def init(data_dir, task_id, OOV=False):
# load candidates
candidates, candid2indx = load_candidates(
data_dir, task_id)
n_cand = len(candidates)
print("Candidate Size", n_cand)
indx2candid = dict(
(candid2indx[key], key) for key in candid2indx)
# load task data
train_data, test_data, val_data = load_dialog_task(
data_dir, task_id, candid2indx, OOV)
data = train_data + test_data + val_data
# build parameters
word_idx, sentence_size, \
candidate_sentence_size, memory_size, \
vocab_size = build_vocab(data, candidates)
# Variable(torch.from_numpy(candidates_vec)).view(len(candidates), sentence_size)
candidates_vec = vectorize_candidates(
candidates, word_idx, candidate_sentence_size)
return candid2indx, \
indx2candid, \
candidates_vec, \
word_idx, \
sentence_size, \
candidate_sentence_size, \
memory_size, \
vocab_size, \
train_data, test_data, val_data
def build_vocab(data, candidates, memory_size=50):
vocab = reduce(lambda x, y: x | y, (set(
list(chain.from_iterable(s)) + q) for s, q, a in data))
vocab |= reduce(lambda x, y: x | y, (set(candidate)
for candidate in candidates))
vocab = sorted(vocab)
word_idx = dict((c, i + 1) for i, c in enumerate(vocab))
max_story_size = max(map(len, (s for s, _, _ in data)))
mean_story_size = int(np.mean([len(s) for s, _, _ in data]))
sentence_size = max(map(len, chain.from_iterable(s for s, _, _ in data)))
candidate_sentence_size = max(map(len, candidates))
query_size = max(map(len, (q for _, q, _ in data)))
memory_size = min(memory_size, max_story_size)
vocab_size = len(word_idx) + 1 # +1 for nil word
sentence_size = max(query_size, sentence_size) # for the position
# params
print("vocab size:", vocab_size)
print("Longest sentence length", sentence_size)
print("Longest candidate sentence length", candidate_sentence_size)
print("Longest story length", max_story_size)
print("Average story length", mean_story_size)
return word_idx, \
sentence_size, \
candidate_sentence_size, \
memory_size, \
vocab_size
def eval(utter_batch, memory_batch, answer__batch, dialog_idx, mem_cnn_sim, cuda=False):
mem_cnn_sim.eval()
total_loss = []
preds = []
dialog_len = len(utter_batch)
for start in range(dialog_len):
end = start
loss_per_diaglo = []
for j in range(start, end + 1):
memory = V(torch.from_numpy(memory_batch[j])).unsqueeze(0)
utter = V(torch.from_numpy(utter_batch[j])).unsqueeze(0)
if cuda:
memory = transfer_to_gpu(memory)
utter = transfer_to_gpu(utter)
context, cand_ = mem_cnn_sim(utter, memory, cands_tensor)
pred = mem_cnn_sim.predict(context, cand_)
preds.append(pred.data[0])
# loss_per_diaglo.append(loss.data[0])
total_loss += loss_per_diaglo
accuracy = metrics.accuracy_score(answer__batch[:len(preds)], preds)
print()
print('Validation accuracy: {}'.format(accuracy))
print('Validation loss: {}'.format(sum(total_loss)))
return accuracy
def interactive(model, indx2candid, cands_tensor, word_idx, sentence_size, memory_size, cuda=False):
context = []
u = None
r = None
nid = 1
while True:
line = input('--> ').strip().lower()
if line == 'exit':
break
if line == 'restart':
context = []
nid = 1
print("clear memory")
continue
u = tokenize(line)
data = [(context, u, -1)]
s, q, a, entity_dict = vectorize_data(data, word_idx, sentence_size, memory_size)
memory = V(torch.from_numpy(np.stack(s)))
utter = V(torch.from_numpy(np.stack(q)))
if cuda:
memory = transfer_to_gpu(memory)
utter = transfer_to_gpu(utter)
context_, cand_ = model(utter, memory, cands_tensor)
preds = model.predict(context_, cand_)
r = indx2candid[preds.data[0]]
print(r)
r = tokenize(r)
u.append('$u')
u.append('#' + str(nid))
r.append('$r')
r.append('#' + str(nid))
context.append(u)
context.append(r)
nid += 1
def transfer_to_gpu(tensor, dtype=torch.LongTensor):
tensor_cuda = dtype(tensor.size()).cuda()
tensor_cuda = V(tensor_cuda)
tensor_cuda.data.copy_(tensor.data)
return tensor_cuda
def save_checkpoint(state, filename='checkpoint.pth.tar'):
torch.save(state, filename)
def load_checkpoit(model, optimizer, path_to_model):
if os.path.isfile(path_to_model):
print("=> loading checkpoint '{}'".format(path_to_model))
checkpoint = torch.load(path_to_model)
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(path_to_model, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(path_to_model))
def load_model(model, model_dir):
load_checkpoit(model, model.optimizer, model_dir+'best_model')
def test_model(mem_cnn_sim):
for i in range(20):
utter = V(torch.LongTensor([1,1,1])).unsqueeze(0)
memory = V(torch.LongTensor([[1,2,3], [4,5,6]])).unsqueeze(0)
cand = V(torch.LongTensor([[7,8,9], [10,11,12], [13,14,15], [16,17,18]]))
flag = V(torch.FloatTensor([0,0,0,1]))
context, cand_ = mem_cnn_sim(utter, memory, cand)
loss = mem_cnn_sim.loss_op(context, cand_, flag)
pred = mem_cnn_sim.predict(context, cand_)
mem_cnn_sim.optimize(loss)
print('loss: {}, pred: {}'.format(loss.data[0], pred.data[0]))
if __name__ == '__main__':
def get_bool(arg):
if arg.lower() in ('true', 't', 'yes'):
return True
else:
return False
parser = argparse.ArgumentParser()
parser.add_argument("-interactive", type=get_bool, default=False)
args = parser.parse_args()
data_dir = "data/dialog-bAbI-tasks/"
task_id = 1
epochs = 20
model_dir = "task" + str(task_id) + "_model/"
if not os.path.exists(model_dir):
os.makedirs(model_dir)
test_ = False
cuda = torch.cuda.is_available()
print('task id: {}'.format(task_id))
if cuda: print('Cuda is available.')
candid2indx, \
indx2candid, \
candidates_vec, \
word_idx, \
sentence_size, \
candidate_sentence_size, \
memory_size, \
vocab_size, \
train_data, test_data, val_data = init(data_dir, task_id)
trainS, trainQ, trainA, dialog_idx = vectorize_data(
train_data, word_idx, sentence_size, memory_size)
valS, valQ, valA, dialog_idx_val = vectorize_data(
val_data, word_idx, sentence_size, memory_size)
n_train = len(trainS)
n_val = len(valS)
print("Training Size", n_train)
print("Validation Size", n_val)
param = {
'hops': 3,
"vocab_size": vocab_size,
"embedding_size": 100,
'num_filters': 100,
"cand_vocab_size": vocab_size,
'max_grad_norm': 40.0
}
mem_cnn_sim = MemCnnSim(param)
if test_:
test_model(mem_cnn_sim)
input()
best_validation_accuracy = 0
time = []
cands_tensor = V(torch.from_numpy(candidates_vec))
num_cand = cands_tensor.size(0)
num_dialog = len(dialog_idx)
if cuda:
mem_cnn_sim.cuda()
cands_tensor = transfer_to_gpu(cands_tensor)
if args.interactive:
load_model(mem_cnn_sim, 'task{}_model/'.format(task_id))
interactive(mem_cnn_sim, indx2candid, cands_tensor, word_idx, sentence_size, memory_size, cuda)
for i in range(1, epochs+1):
num_ = [x for x in range(len(trainS))]
np.random.shuffle(num_)
mem_cnn_sim.train()
# for j, (start, end) in enumerate(dialog_idx):
#
# if j%99 == 0:
# print('[{}/{}]\r'.format(j+1, num_dialog))
#
# loss_per_diaglo = []
for j, k in enumerate(num_):
ans = trainA[k]
memory = V(torch.from_numpy(trainS[k])).unsqueeze(0)
utter = V(torch.from_numpy(trainQ[k])).unsqueeze(0)
flag = -1 * torch.ones(num_cand)
flag[ans] = 1
flag = V(flag)
if cuda:
memory = transfer_to_gpu(memory)
utter = transfer_to_gpu(utter)
flag = transfer_to_gpu(flag, dtype=torch.FloatTensor)
context, cand_ = mem_cnn_sim(utter, memory, cands_tensor)
loss = mem_cnn_sim.loss_op(context, cand_, flag)
mem_cnn_sim.optimize(loss)
if j % 100 == 0:
sys.stdout.write('\r{}/{}'.format(j, len(trainS)))
# loss_per_diaglo.append(loss.data[0])
# print('loss: {}'.format(sum(loss_per_diaglo)/len(loss_per_diaglo)))
print('\ntrain accuracy')
accuracy = eval(trainQ, trainS, trainA, dialog_idx, mem_cnn_sim, cuda)
print('eval accuracy')
accuracy = eval(valQ, valS, valA, dialog_idx_val, mem_cnn_sim, cuda)
if accuracy > best_validation_accuracy:
best_validation_accuracy = accuracy
save_checkpoint({
'epoch': i + 1,
'state_dict': mem_cnn_sim.state_dict(),
'optimizer': mem_cnn_sim.optimizer.state_dict(),
}, filename=model_dir + 'best_model')