-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardAttention.lua
90 lines (84 loc) · 3.58 KB
/
hardAttention.lua
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
hardAttention = {}
function hardAttention.makeAttentionDecisions(i, inputTensor, surprisalValue, attInputTensor)
if attInputTensor == nil then
attInputTensor = inputTensor
end
local attendedInputTensor = torch.CudaTensor(params.batch_size):zero()
if use_attention_network then
local results = attentionNetworks[i]:forward({attInputTensor, reader_c[i-1], surprisalValue})
if USE_BASELINE_NETWORK then
attention_scores[i] , baseline_scores[i] = unpack(results)
else
attention_scores[i] = results
end
else
if params.EXTERNAL_ATTENTION_SOURCE == 'fixed' then
attention_scores[i]:fill(FIXED_ATTENTION)
elseif params.EXTERNAL_ATTENTION_SOURCE == 'WLEN' then
for l=1, params.batch_size do
wlen = string.len(readDict.chars[inputTensor[l]])
if wlen > 3 then
attention_scores[i][l] = 1
else
attention_scores[i][l] = 0
end
end
elseif params.EXTERNAL_ATTENTION_SOURCE == 'NUMERICAL_FILE' then
for l=1, params.batch_size do
value = numericalValues.numericalValuesImporter.values[l][i]
if NUMERICAL_VALUES_COLUMN == 3 then
if value > 0 then
attention_scores[i][l] = 1
elseif value > -1 then
attention_scores[i][l] = 0
else
attention_scores[i][l] = 0.7
end
elseif NUMERICAL_VALUES_COLUMN == 4 then
if value > 5.531714 then
attention_scores[i][l] = 1
elseif value > -1 then
attention_scores[i][l] = 0
else
attention_scores[i][l] = 0.62
end
elseif NUMERICAL_VALUES_COLUMN == 5 then
if value > 3 then
attention_scores[i][l] = 1
elseif value > 2 then
attention_scores[i][l] = 0.62
else
attention_scores[i][l] = 0
end
elseif NUMERICAL_VALUES_COLUMN == 9 then --surprisal
if value > 4.25 then
attention_scores[i][l] = 1
elseif value > -1 then
attention_scores[i][l] = 0
else
attention_scores[i][l] = 0.62
end
else
print(NUMERICAL_VALUES_COLUMN)
crash()
end
end
else
print(params.EXTERNAL_ATTENTION_SOURCE)
crash()
end
end
for item=1, params.batch_size do
local dice = torch.uniform()
if dice > attention_scores[i][item][1] then
attention_decisions[i][item] = 0
probabilityOfChoices[item] = probabilityOfChoices[item] * (1-attention_scores[i][item][1])
else
attention_decisions[i][item] = 1
attendedInputTensor[item] = inputTensor[item]
probabilityOfChoices[item] = probabilityOfChoices[item] * attention_scores[i][item][1]
end
totalAttentions[item] = totalAttentions[item] + attention_decisions[i][item]
end
return attendedInputTensor, probability
end