-
Notifications
You must be signed in to change notification settings - Fork 2
/
softmax-n.jl
536 lines (483 loc) · 15.4 KB
/
softmax-n.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
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
uo_lr = 0.001
ur_lr = 0.001
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.
d_factor = findMaxSupFact([training_data, test_data]) + 2
vocabDict = createDict([training_data, test_data])
vocabDict_length = length(vocabDict)
info("$vocabDict_length unique words.")
feature_space = d_factor * vocabDict_length
model = initWeights(settings[:atype], feature_space, embedding_dimension, settings[:winit])
uo_adam = Adam(;lr = uo_lr)
ur_adam = Adam(;lr = ur_lr)
for epoch = 0:total_epochs
@time o_avg_loss, r_avg_loss = train(training_data, model[:uo], model[:ur], vocabDict, uo_adam, ur_adam, d_factor, settings[:atype], epoch)
o_accuracy, r_accuracy = trainingAccuracy(training_data, model[:uo], model[:ur], vocabDict, d_factor, settings[:atype])
@time test_o_avg_loss, test_r_avg_loss, test_accuracy = test(test_data, model[:uo], model[:ur], vocabDict, d_factor, settings[:atype])
println("Epoch: $epoch => [o_loss: $o_avg_loss, r_loss: $r_avg_loss, o_accuracy: $o_accuracy %, r_accuracy: $r_accuracy %], [o_loss: $test_o_avg_loss, r_loss: $test_r_avg_loss, accuracy: $test_accuracy %]")
end
end
function findMaxSupFact(datafiles)
maxSupCount = 0
for file in datafiles
f = open(file)
while !eof(f)
str = readline(f)
words = split(str)
if words[end][end] != '.'
count = 0
for w in words
if isnumber(w)
count = count + 1
end
end
count = count - 1 #line number is not counted as a supporting fact.
if count > maxSupCount
maxSupCount = count
end
end
end
end
return maxSupCount
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)
str = words[i]
if str[end] == '?'
str = str[1:end - 1]
end
if !isnumber(str)
if !haskey(dict, str)
dict[str] = number
number = number + 1
end
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, q_list, uo, d_factor, atype)
x_feature_rep_list = [x_feature_rep]
for i = 1:(d_factor - 2)
scoreArray = so(x_feature_rep_list, memory, q_list, uo, d_factor, atype)
scoreArray = scoreArray .- maximum(scoreArray, 1)
scoreProb = exp(scoreArray) ./ sum(exp(scoreArray), 1)
o = indmax(scoreProb)
mo = memory[o]
temp_mo = [mo]
x_feature_rep_list = vcat(x_feature_rep_list, temp_mo)
end
return x_feature_rep_list
end
function phix(feature_rep_list, d_factor, atype)
mapped = copy(feature_rep_list[1])
for i = 2:d_factor
if i <= length(feature_rep_list)
mapped = vcat(mapped, feature_rep_list[i])
else
mapped = vcat(mapped, zeros(Float64, length(feature_rep_list[1]), 1))
end
end
mapped = convert(atype, mapped)
return mapped
end
function phiy(feature_rep, d_factor, atype)
mapped = copy(feature_rep)
mapped = vcat(zeros(Float64, (d_factor - 1) * length(feature_rep), 1), mapped)
mapped = convert(atype, mapped)
return mapped
end
function s(x_feature_rep_list, y_feature_rep, u, d_factor, atype)
phi_y = phiy(y_feature_rep, d_factor, atype)
phi_x = phix(x_feature_rep_list, d_factor, atype)
score = sum(phi_x' * u' * u * phi_y)
return score
end
function so(x_feature_rep_list, memory, q_list, uo, d_factor, atype)
scoreArray = s(x_feature_rep_list, memory[1], uo, d_factor, atype)
for i = 2:length(memory)
if in(i, q_list)
score = -Inf
else
score = s(x_feature_rep_list, memory[i], uo, d_factor, atype)
end
scoreArray = vcat(scoreArray, score)
end
return scoreArray
end
function R(input_list, vocabDict, ur, d_factor, atype)
scoreArray = sr(input_list, vocabDict, ur, d_factor, atype)
scoreArray = scoreArray .- maximum(scoreArray, 1)
scoreProb = exp(scoreArray) ./ sum(exp(scoreArray), 1)
index = indmax(scoreArray)
for k in keys(vocabDict)
if vocabDict[k] == index
return k
end
end
end
function sr(x_feature_rep_list, vocabDict, ur, d_factor, atype)
w = findWord(vocabDict, 1)
y_feature_rep = word2OneHot(w, vocabDict)
scoreArray = s(x_feature_rep_list, y_feature_rep, ur, d_factor, atype)
for i = 2:length(vocabDict)
w = findWord(vocabDict, i)
y_feature_rep = word2OneHot(w, vocabDict)
score = s(x_feature_rep_list, y_feature_rep, ur, d_factor, atype)
scoreArray = vcat(scoreArray, score)
end
return scoreArray
end
function findWord(vocabDict, i)
for k in keys(vocabDict)
if vocabDict[k] == i
return k
end
end
end
function uoSoftloss(uo, x_feature_rep_list, memory, q_list, vocabDict, golds, d_factor, atype)
uoLoss = 0
for i = 1:length(golds)
uoArray = so(x_feature_rep_list, memory, q_list, uo, d_factor, atype)
uoArray = uoArray .- maximum(uoArray, 1)
uoProb = exp(uoArray) ./ sum(exp(uoArray), 1)
o = indmax(uoProb)
mo = memory[o]
temp_mo = [mo]
x_feature_rep_list = vcat(x_feature_rep_list, temp_mo)
uoLoss = uoLoss + (-1) * log(uoProb[golds[i]])
end
return uoLoss
end
uoSoftlossGrad = grad(uoSoftloss)
function urSoftloss(ur, x_feature_rep_list, memory, vocabDict, gold, d_factor, atype)
urArray = sr(x_feature_rep_list, vocabDict, ur, d_factor, atype)
urArray = urArray .- maximum(urArray, 1)
urProb = exp(urArray) ./ sum(exp(urArray), 1)
urLoss = (-1) * log(urProb[vocabDict[gold]])
return urLoss
end
urSoftlossGrad = grad(urSoftloss)
function train(data_file, uo, ur, vocabDict, uo_adam, ur_adam, d_factor, atype, epoch)
uo_total_loss = 0
ur_total_loss = 0
numq = 0
memory = resetMemory()
q_list = 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()
q_list = 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]
sup_res = 0
for i = 3:length(words)
if words[i][end] =='?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
if !isnumber(words[min(i + 1, length(words))])
question = question * " " * words[i]
else
sup_res = sup_res + 1
end
end
question_feature_rep = I(question, vocabDict, atype)
G(question_feature_rep, memory)
push!(q_list, line_number)
correct_r = words[end - sup_res + 1]
correct_ms = Any[]
correct_ms_index = Any[]
for i = (length(words) - sup_res + 2):length(words)
correct_m_index = words[i]
correct_m_index = parse(Int, correct_m_index)
correct_m = memory[correct_m_index]
push!(correct_ms, correct_m)
push!(correct_ms_index, correct_m_index)
end
uoLoss = uoSoftloss(uo, [question_feature_rep], memory, q_list, vocabDict, correct_ms_index, d_factor, atype)
uoLossGradient = uoSoftlossGrad(uo, [question_feature_rep], memory, q_list, vocabDict, correct_ms_index, d_factor, atype)
urInput = [question_feature_rep]
urInput = vcat(urInput, correct_ms)
urLoss = urSoftloss(ur, urInput, memory, vocabDict, correct_r, d_factor, atype)
urLossGradient = urSoftlossGrad(ur, urInput, memory, vocabDict, correct_r, d_factor, atype)
if epoch != 0
update!(uo, uoLossGradient, uo_adam)
update!(ur, urLossGradient, ur_adam)
end
uo_total_loss = uo_total_loss + uoLoss
ur_total_loss = ur_total_loss + urLoss
numq = numq + 1
end
end
close(f)
uo_avg_loss = uo_total_loss / numq
ur_avg_loss = ur_total_loss / numq
return uo_avg_loss, ur_avg_loss
end
function resetMemory()
return Any[]
end
function trainingAccuracy(data_file, uo, ur, vocabDict, d_factor, atype)
numsup = 0
numcorr = 0
numq = 0
memory = resetMemory()
q_list = 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()
q_list = 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]
sup_res = 0
for i = 3:length(words)
if words[i][end] =='?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
if !isnumber(words[min(i + 1, length(words))])
question = question * " " * words[i]
else
sup_res = sup_res + 1
end
end
question_feature_rep = I(question, vocabDict, atype)
G(question_feature_rep, memory)
push!(q_list, line_number)
correct_r = words[end - sup_res + 1]
correct_ms = Any[]
correct_ms_index = Any[]
for i = (length(words) - sup_res + 2):length(words)
correct_m_index = words[i]
correct_m_index = parse(Int, correct_m_index)
correct_m = memory[correct_m_index]
push!(correct_ms, correct_m)
push!(correct_ms_index, correct_m_index)
end
output = O(question_feature_rep, memory, q_list, uo, d_factor, atype)
corr = true
for i = 1:length(correct_ms)
if !in(correct_ms[i], output)
corr = false
end
end
if corr
numsup = numsup + 1
end
RInput = [question_feature_rep]
RInput = vcat(RInput, correct_ms)
response = R(RInput, vocabDict, ur, d_factor, 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, d_factor, atype)
numcorr = 0
uo_total_loss = 0
ur_total_loss = 0
numq = 0
memory = resetMemory()
q_list = 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()
q_list = 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]
sup_res = 0
for i = 3:length(words)
if words[i][end] =='?' || words[i][end] == '.'
words[i] = words[i][1:end - 1]
end
if !isnumber(words[min(i + 1, length(words))])
question = question * " " * words[i]
else
sup_res = sup_res + 1
end
end
question_feature_rep = I(question, vocabDict, atype)
G(question_feature_rep, memory)
push!(q_list, line_number)
correct_r = words[end - sup_res + 1]
correct_ms = Any[]
correct_ms_index = Any[]
for i = (length(words) - sup_res + 2):length(words)
correct_m_index = words[i]
correct_m_index = parse(Int, correct_m_index)
correct_m = memory[correct_m_index]
push!(correct_ms, correct_m)
push!(correct_ms_index, correct_m_index)
end
uoLoss = uoSoftloss(uo, [question_feature_rep], memory, q_list, vocabDict, correct_ms_index, d_factor, atype)
urInput = [question_feature_rep]
urInput = vcat(urInput, correct_ms)
urLoss = urSoftloss(ur, urInput, memory, vocabDict, correct_r, d_factor, atype)
response = answer(question_feature_rep, memory, q_list, vocabDict, uo, ur, d_factor, atype)
if response == correct_r
numcorr = numcorr + 1
end
uo_total_loss = uo_total_loss + uoLoss
ur_total_loss = ur_total_loss + urLoss
numq = numq + 1
end
end
close(f)
test_accuracy = numcorr / numq * 100
uo_avg_loss = uo_total_loss / numq
ur_avg_loss = ur_total_loss / numq
return uo_avg_loss, ur_avg_loss, test_accuracy
end
function answer(x_feature_rep, memory, q_list, vocabDict, uo, ur, d_factor, atype)
output = O(x_feature_rep, memory, q_list, uo, d_factor, atype)
answer = R(output, vocabDict, ur, d_factor, atype)
return answer
end
main()