-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemnn.py
executable file
·283 lines (220 loc) · 8.91 KB
/
memnn.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
#!/usr/bin/env python
import argparse
import collections
import copy
import glob
import sys
import numpy
import six
import chainer
from chainer import cuda
import chainer.functions as F
from chainer import initializers
import chainer.links as L
from chainer import optimizers
from chainer import training
from chainer.training import extensions
import babi
class BoWEncoder(object):
"""BoW sentence encoder.
It is defined as:
.. math::
m = \sum_j A x_j,
where :math:`A` is an embed matrix, and :math:`x_j` is :math:`j`-th word ID.
"""
def __call__(self, embed, sentences):
xp = cuda.get_array_module(sentences)
e = embed(sentences)
s = F.sum(e, axis=-2)
return s
class PositionEncoder(object):
"""Position encoding.
It is defined as:
.. math::
m = \sum_j l_j A x_j,
where :math:`A` is an embed matrix, :math:`x_j` is :math:`j`-th word ID and
.. math::
l_{kj} = (1 - j / J) - (k / d)(1 - 2j / J).
:math:`J` is length of a sentence and :math:`d` is the dimension of the
embedding.
"""
def __call__(self, embed, sentences):
xp = cuda.get_array_module(sentences)
e = embed(sentences)
ndim = e.ndim
n_words, n_units = e.shape[-2:]
# To avoid 0/0, we use max(length, 1) here.
# Note that when the length is zero, its embedding is always zero and
# is igrenod.
length = xp.maximum(
xp.sum((sentences != 0).astype('f'), axis=-1), 1)
length = length.reshape((length.shape + (1, 1)))
k = xp.arange(1, n_units + 1, dtype=numpy.float32) / n_units
i = xp.arange(1, n_words + 1, dtype=numpy.float32)[:, None]
coeff = (1 - i / length) - k * (1 - 2.0 * i / length)
e = coeff * e
s = F.sum(e, axis=-2)
return s
class Memory(object):
"""Memory component in a memory network.
Args:
A (chainer.links.EmbedID): Embed matrix for input. Its shape is
``(n_vocab, n_units)``.
C (chainer.links.EmbedID): Embed matrix for output. Its shape is
``(n_vocab, n_units)``.
TA (chainer.links.EmbedID): Embed matrix for temporal encoding for
input. Its shape is ``(max_memory, n_units)``.
TC (chainer.links.EmbedID): Embed matrix for temporal encoding for
output. Its shape is ``(max_memory, n_units)``.
encoder (callable): It encodes given stentences to embed vectors.
"""
def __init__(self, A, C, TA, TC, encoder):
self.A = A
self.C = C
self.TA = TA
self.TC = TC
self.encoder = encoder
def register_all(self, sentences):
self.m = self.encoder(self.A, sentences)
self.c = self.encoder(self.C, sentences)
def query(self, u):
xp = cuda.get_array_module(u)
size = self.m.shape[1]
inds = xp.arange(size - 1, -1, -1, dtype=numpy.int32)
tm = self.TA(inds)
tc = self.TC(inds)
tm = F.broadcast_to(tm, self.m.shape)
tc = F.broadcast_to(tc, self.c.shape)
p = F.softmax(F.batch_matmul(self.m + tm, u))
o = F.batch_matmul(F.swapaxes(self.c + tc, 2, 1), p)
o = F.squeeze(o, -1)
u = o + u
return u
class MemNN(chainer.Chain):
def __init__(self, n_units, n_vocab, encoder, max_memory, hops):
super(MemNN, self).__init__()
with self.init_scope():
self.embeds = chainer.ChainList()
self.temporals = chainer.ChainList()
normal = initializers.Normal()
# Shares both embeded matrixes in adjacent layres
for _ in six.moves.range(hops + 1):
self.embeds.append(L.EmbedID(n_vocab, n_units, initialW=normal))
self.temporals.append(
L.EmbedID(max_memory, n_units, initialW=normal))
self.memories = [
Memory(self.embeds[i], self.embeds[i + 1],
self.temporals[i], self.temporals[i + 1], encoder)
for i in six.moves.range(hops)
]
# The question embedding is same as the input embedding of the
# first layer
self.B = self.embeds[0]
# The answer prediction matrix W is same as the final output layer
self.W = lambda u: F.linear(u, self.embeds[-1].W)
self.encoder = encoder
def fix_ignore_label(self):
for embed in self.embeds:
embed.W.data[0, :] = 0
def register_all(self, sentences):
for memory in self.memories:
memory.register_all(sentences)
def query(self, question):
u = self.encoder(self.B, question)
for memory in self.memories:
u = memory.query(u)
a = self.W(u)
return a
def __call__(self, sentences, question):
self.register_all(sentences)
a = self.query(question)
return a
def convert_data(train_data, max_memory):
all_data = []
sentence_len = max(max(len(s.sentence) for s in story)
for story in train_data)
for story in train_data:
mem = numpy.zeros((max_memory, sentence_len), dtype=numpy.int32)
i = 0
for sent in story:
if isinstance(sent, babi.Sentence):
if i == max_memory:
mem[0:i - 1, :] = mem[1:i, :]
i -= 1
mem[i, 0:len(sent.sentence)] = sent.sentence
i += 1
elif isinstance(sent, babi.Query):
query = numpy.zeros(sentence_len, dtype=numpy.int32)
query[0:len(sent.sentence)] = sent.sentence
all_data.append({
'sentences': mem.copy(),
'question': query,
'answer': numpy.array(sent.answer, 'i'),
})
return all_data
def main():
parser = argparse.ArgumentParser(
description='Chainer example: End-to-end memory networks')
parser.add_argument('data', help='Path to bAbI dataset')
parser.add_argument('--batchsize', '-b', type=int, default=100,
help='Number of images in each mini batch')
parser.add_argument('--epoch', '-e', type=int, default=100,
help='Number of sweeps over the dataset to train')
parser.add_argument('--gpu', '-g', type=int, default=-1,
help='GPU ID (negative value indicates CPU)')
parser.add_argument('--unit', '-u', type=int, default=20,
help='Number of units')
parser.add_argument('--hop', '-H', type=int, default=3,
help='Number of hops')
parser.add_argument('--max-memory', type=int, default=50,
help='Maximum number of memory')
parser.add_argument('--sentence-repr',
choices=['bow', 'pe'], default='bow',
help='Sentence representation. '
'Select from BoW ("bow") or position encoding ("pe")')
args = parser.parse_args()
vocab = collections.defaultdict(lambda: len(vocab))
vocab['<unk>'] = 0
for data_id in six.moves.range(1, 21):
train_data = babi.read_data(
vocab,
glob.glob('%s/qa%d_*train.txt' % (args.data, data_id))[0])
test_data = babi.read_data(
vocab,
glob.glob('%s/qa%d_*test.txt' % (args.data, data_id))[0])
print('Training data: %d' % len(train_data))
train_data = convert_data(train_data, args.max_memory)
test_data = convert_data(test_data, args.max_memory)
if args.sentence_repr == 'bow':
encoder = BoWEncoder()
elif args.sentence_repr == 'pe':
encoder = PositionEncoder()
else:
print('Unknonw --sentence-repr option: "%s"' % args.sentence_repr)
sys.exit(1)
memnn = MemNN(args.unit, len(vocab), encoder, args.max_memory, args.hop)
model = L.Classifier(memnn, label_key='answer')
opt = optimizers.Adam()
if args.gpu >= 0:
chainer.cuda.get_device(args.gpu).use()
model.to_gpu()
opt.setup(model)
train_iter = chainer.iterators.SerialIterator(
train_data, args.batchsize)
test_iter = chainer.iterators.SerialIterator(
test_data, args.batchsize, repeat=False, shuffle=False)
updater = training.StandardUpdater(train_iter, opt, device=args.gpu)
trainer = training.Trainer(updater, (args.epoch, 'epoch'))
@training.make_extension()
def fix_ignore_label(trainer):
memnn.fix_ignore_label()
trainer.extend(fix_ignore_label)
trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport(
['epoch', 'main/loss', 'validation/main/loss',
'main/accuracy', 'validation/main/accuracy']))
trainer.extend(extensions.ProgressBar(update_interval=10))
trainer.run()
if __name__ == '__main__':
main()