-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMemNN-2-supporting-facts.jl
460 lines (405 loc) · 12.8 KB
/
MemNN-2-supporting-facts.jl
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using Knet, AutoGrad, Compat, ArgParse
function parse_commandline()
s = ArgParseSettings()
@add_arg_table s begin
("--datafiles"; nargs = '+'; help = "If provided, use first file for training, second for test.")
("--winit"; arg_type=Float64; default=0.01; help="Initial weights set to winit*randn().")
("--seed"; arg_type=Int; default=38; help="Random number seed.")
("--atype"; default = (gpu() >= 0 ? "KnetArray{Float64}" : "Array{Float64}");
help = "Array type: Array for CPU, KnetArray for GPU")
end
return parse_args(s; as_symbols = true)
end
function main(args = ARGS)
#Configurations of the model
embedding_dimension = 100
learning_rate = 0.01
margin = 0.1
total_epochs = 100
#User settings are added.
settings = parse_commandline()
println("settings", [(symbol, value) for (symbol, value) in settings]...)
settings[:seed] > 0 && srand(settings[:seed])
settings[:atype] = eval(parse(settings[:atype]))
#Classification of datafiles as training and test.
training_data = settings[:datafiles][1]
if length(settings[:datafiles]) > 1
test_data = settings[:datafiles][2]
end
#Creating the dictionary of words in the data.
vocabDict = createDict([training_data, test_data])
vocabDict_length = length(vocabDict)
info("$vocabDict_length unique words.")
feature_space = 4 * vocabDict_length
model = initWeights(settings[:atype], feature_space, embedding_dimension, settings[:winit])
for epoch = 1:total_epochs
@time training_avg_loss = train(training_data, model[:uo], model[:ur], vocabDict, learning_rate, margin, settings[:atype])
o_accuracy, r_accuracy = trainingAccuracy(training_data, model[:uo], model[:ur], vocabDict, settings[:atype])
@time test_avg_loss, test_accuracy = test(test_data, model[:uo], model[:ur], vocabDict, margin, settings[:atype])
println("[Training => (epoch: $epoch, loss: $training_avg_loss, o_accuracy: $o_accuracy %, r_accuracy: $r_accuracy %)] , [Test => (epoch: $epoch, loss: $test_avg_loss, accuracy: $test_accuracy %)]")
end
end
function createDict(datafiles)
dict = Dict{String, Int}()
number = 1
for file in datafiles
f = open(file)
while !eof(f)
str = readline(f)
number, dict = parseLineAddDict(number, str, dict)
end
close(f)
end
return dict
end
function parseLineAddDict(number, line, dict)
words = split(line)
if words[end][end] == '.'
for i = 2:length(words)
str = words[i]
if str[end] == '.'
str = str[1:end - 1]
end
if !haskey(dict, str)
dict[str] = number
number = number + 1
end
end
else
for i = 2:length(words) - 2
str = words[i]
if str[end] == '?'
str = str[1:end - 1]
end
if !haskey(dict, str)
dict[str] = number
number = number + 1
end
end
end
return number, dict
end
function initWeights(atype, feature_space, embedding_dimension, winit)
weights = Dict{Symbol, Any}()
uo = winit * randn(embedding_dimension, feature_space)
ur = winit * randn(embedding_dimension, feature_space)
weights[:uo] = uo
weights[:ur] = ur
for k in keys(weights)
weights[k] = convert(atype, weights[k])
end
return weights
end
function I(x, dict, atype)
words = split(x)
feature_rep = zeros(Float64, length(dict), 1)
for w in words
if w[end] == '?' || w[end] == '.'
w = w[1:end - 1]
end
onehot = word2OneHot(w, dict)
feature_rep = feature_rep .+ onehot
end
feature_rep = convert(atype, feature_rep)
return feature_rep
end
function word2OneHot(word, dict)
onehot = zeros(Float64, length(dict), 1)
for w in keys(dict)
if w == word
onehot[dict[w], 1] = 1.0
break
end
end
return onehot
end
function G(feature_rep, memory)
push!(memory, feature_rep)
return memory
end
function O(x_feature_rep, memory, uo, atype)
x_feature_rep_list = [x_feature_rep]
scoreDict1 = so(x_feature_rep_list, memory, uo, atype)
o1 = scoreDict1[maximum(keys(scoreDict1))]
mo1 = memory[o1]
x_feature_rep_list = [x_feature_rep, mo1]
scoreDict2 = so(x_feature_rep_list, memory, uo, atype)
o2 = scoreDict2[maximum(keys(scoreDict2))]
mo2 = memory[o2]
return [x_feature_rep, mo1, mo2]
end
function phix(feature_rep_list, atype)
mapped = zeros(Float64, 4 * length(feature_rep_list[1]), 1)
for i = 1:length(feature_rep_list)
feature_rep = feature_rep_list[i]
for j = 1:length(feature_rep)
if i == 1
mapped[j] = feature_rep[j]
else
if i == 2
mapped[length(feature_rep) + j] = feature_rep[j]
else
mapped[2 * length(feature_rep) + j] = feature_rep[j]
end
end
end
end
mapped = convert(atype, mapped)
return mapped
end
function phiy(feature_rep, atype)
mapped = zeros(Float64, 4 * length(feature_rep), 1)
for i = 1:length(feature_rep)
mapped[3 * length(feature_rep) + i] = feature_rep[i]
end
mapped = convert(atype, mapped)
return mapped
end
function s(x_feature_rep_list, y_feature_rep, u, atype)
phi_y = phiy(y_feature_rep, atype)
phi_x = phix(x_feature_rep_list, atype)
score = sum(phi_x' * u' * u * phi_y)
return score
end
function so(x_feature_rep_list, memory, uo, atype)
scoreDict = Dict{Float64, Int}()
for i = 1:length(memory)
score = s(x_feature_rep_list, memory[i], uo, atype)
scoreDict[score] = i
end
return scoreDict
end
function R(input_list, vocabDict, ur, atype)
scoreDict = sr(input_list, vocabDict, ur, atype)
answer = scoreDict[maximum(keys(scoreDict))]
return answer
end
function sr(x_feature_rep_list, vocabDict, ur, atype)
scoreDict = Dict{Float64, String}()
for k in keys(vocabDict)
y_feature_rep = word2OneHot(k, vocabDict)
score = s(x_feature_rep_list, y_feature_rep, ur, atype)
scoreDict[score] = k
end
return scoreDict
end
function marginRankingLoss(comb, x_feature_rep, memory, vocabDict, gold_labels, margin, atype)
uo = comb[1]
ur = comb[2]
total_loss = 0
m1_loss = 0
m2_loss = 0
r_loss = 0
correct_m1 = gold_labels[1]
correct_m2 = gold_labels[2]
correct_r = gold_labels[3]
input_1 = [x_feature_rep]
for i = 1:length(memory)
if memory[i] != correct_m1
m1l = max(0, margin - s(input_1, correct_m1, uo, atype) + s(input_1, memory[i], uo, atype))
m1_loss = m1_loss + m1l
end
end
input_2 = [x_feature_rep, correct_m1]
for j = 1:length(memory)
if memory[j] != correct_m2
m2l = max(0, margin - s(input_2, correct_m2, uo, atype) + s(input_2, memory[j], uo, atype))
m2_loss = m2_loss + m2l
end
end
correct_r_feature_rep = word2OneHot(correct_r, vocabDict)
input_r = [x_feature_rep, correct_m1, correct_m2]
for k in keys(vocabDict)
if k != correct_r
k_feature_rep = word2OneHot(k, vocabDict)
rl = max(0, margin - s(input_r, correct_r_feature_rep, ur, atype) + s(input_r, k_feature_rep, ur, atype))
r_loss = r_loss + rl
end
end
total_loss = m1_loss + m2_loss + r_loss
return total_loss
end
marginRankingLossGradient = grad(marginRankingLoss)
function train(data_file, uo, ur, vocabDict, lr, margin, atype)
total_loss = 0
numq = 0
memory = resetMemory()
f = open(data_file)
while !eof(f)
str = readline(f)
words = split(str)
if words[end][end] == '.'
line_number = words[1]
line_number = parse(Int, line_number)
sentence = words[2]
for i = 3:length(words)
if words[i][end] == '?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
sentence = sentence * " " * words[i]
end
if line_number == 1
memory = resetMemory()
end
sentence_feature_rep = I(sentence, vocabDict, atype)
G(sentence_feature_rep, memory)
else
line_number = words[1]
line_number = parse(Int, line_number)
question = words[2]
for i = 3:(length(words) - 3)
if words[i][end] == '?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
question = question * " " * words[i]
end
question_feature_rep = I(question, vocabDict, atype)
G(question_feature_rep, memory)
correct_r = words[end - 2]
correct_m1_index = words[end - 1]
correct_m1_index = parse(Int, correct_m1_index)
correct_m1 = memory[correct_m1_index]
correct_m2_index = words[end]
correct_m2_index = parse(Int, correct_m2_index)
correct_m2 = memory[correct_m2_index]
gold_labels = [correct_m1, correct_m2, correct_r]
comb = [uo, ur]
loss = marginRankingLoss(comb, question_feature_rep, memory, vocabDict, gold_labels, margin, atype)
lossGradient = marginRankingLossGradient(comb, question_feature_rep, memory, vocabDict, gold_labels, margin, atype)
copy!(uo, uo - lr * lossGradient[1])
copy!(ur, ur - lr * lossGradient[2])
total_loss = total_loss + loss
numq = numq + 1
end
end
close(f)
avg_loss = total_loss / numq
return avg_loss
end
function resetMemory()
return Any[]
end
function trainingAccuracy(data_file, uo, ur, vocabDict, atype)
numsup = 0
numcorr = 0
numq = 0
memory = resetMemory()
f = open(data_file)
while !eof(f)
str = readline(f)
words = split(str)
if words[end][end] == '.'
line_number = words[1]
line_number = parse(Int, line_number)
sentence = words[2]
for i = 3:length(words)
if words[i][end] == '?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
sentence = sentence * " " * words[i]
end
if line_number == 1
memory = resetMemory()
end
sentence_feature_rep = I(sentence, vocabDict, atype)
G(sentence_feature_rep, memory)
else
line_number = words[1]
line_number = parse(Int, line_number)
question = words[2]
for i = 3:(length(words) - 3)
if words[i][end] == '?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
question = question * " " * words[i]
end
question_feature_rep = I(question, vocabDict, atype)
G(question_feature_rep, memory)
correct_r = words[end - 2]
correct_m1_index = words[end - 1]
correct_m1_index = parse(Int, correct_m1_index)
correct_m1 = memory[correct_m1_index]
correct_m2_index = words[end]
correct_m2_index = parse(Int, correct_m2_index)
correct_m2 = memory[correct_m2_index]
output = O(question_feature_rep, memory, uo, atype)
if in(correct_m1, output) && in(correct_m2, output)
numsup = numsup + 1
end
response = R([question_feature_rep, correct_m1, correct_m2], vocabDict, ur, atype)
if response == correct_r
numcorr = numcorr + 1
end
numq = numq + 1
end
end
close(f)
output_accuracy = numsup / numq * 100
response_accuracy = numcorr / numq * 100
return output_accuracy, response_accuracy
end
function test(data_file, uo, ur, vocabDict, margin, atype)
numcorr = 0
total_loss = 0
numq = 0
memory = resetMemory()
f = open(data_file)
while !eof(f)
str = readline(f)
words = split(str)
if words[end][end] == '.'
line_number = words[1]
line_number = parse(Int, line_number)
sentence = words[2]
for i = 3:length(words)
if words[i][end] == '?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
sentence = sentence * " " * words[i]
end
if line_number == 1
memory = resetMemory()
end
sentence_feature_rep = I(sentence, vocabDict, atype)
G(sentence_feature_rep, memory)
else
line_number = words[1]
line_number = parse(Int, line_number)
question = words[2]
for i = 3:(length(words) - 3)
if words[i][end] == '?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
question = question * " " * words[i]
end
question_feature_rep = I(question, vocabDict, atype)
G(question_feature_rep, memory)
correct_r = words[end - 2]
correct_m1_index = words[end - 1]
correct_m1_index = parse(Int, correct_m1_index)
correct_m1 = memory[correct_m1_index]
correct_m2_index = words[end]
correct_m2_index = parse(Int, correct_m2_index)
correct_m2 = memory[correct_m2_index]
gold_labels = [correct_m1, correct_m2, correct_r]
comb = [uo, ur]
loss = marginRankingLoss(comb, question_feature_rep, memory, vocabDict, gold_labels, margin, atype)
response = answer(question_feature_rep, memory, vocabDict, uo, ur, atype)
if response == correct_r
numcorr = numcorr + 1
end
total_loss = total_loss + loss
numq = numq + 1
end
end
close(f)
test_accuracy = numcorr / numq * 100
avg_loss = total_loss / numq
return avg_loss, test_accuracy
end
function answer(x_feature_rep, memory, vocabDict, uo, ur, atype)
output = O(x_feature_rep, memory, uo, atype)
answer = R(output, vocabDict, ur, atype)
return answer
end
main()