-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoreAnnotation.lua
176 lines (150 loc) · 6.76 KB
/
storeAnnotation.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
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
storeAnnotation = {}
function removeAnnotation(annotationDir)
for file in lfs.dir(annotationDir) do
os.remove(annotationDir.."/"..file)
end
end
STORE_ATTENTION_ANNOTATION = false
if DOING_EVALUATION_OUTPUT and (params.TASK == "autoencoding" or params.TASK == "combined" or params.TASK == "combined-soft" or params.TASK == "neat-qa") then
STORE_ATTENTION_ANNOTATION = true
end
ATTENTION_ANNOTATION_DIR = DATASET_DIR.."/annotation/att-"..arg[15].."/"
if STORE_ATTENTION_ANNOTATION then
if DO_TRAINING then
crash()
end
lfs.mkdir(ATTENTION_ANNOTATION_DIR)
print("STORING ATTENTION ANNOTATION IN "..ATTENTION_ANNOTATION_DIR)
removeAnnotation(ATTENTION_ANNOTATION_DIR)
end
WRITE_SURPRISAL_SCORES = false
if DOING_EVALUATION_OUTPUT and (params.TASK == "langmod" or params.TASK == "combined" or params.TASK == "combined-soft") then
WRITE_SURPRISAL_SCORES = true
end
SURPRISAL_ANNOTATION_DIR = DATASET_DIR.."/annotation/surp-"..arg[15].."/"
if WRITE_SURPRISAL_SCORES then
if DO_TRAINING then
crash()
end
lfs.mkdir(SURPRISAL_ANNOTATION_DIR)
print("WRITING SURPRISAL SCORES IN "..SURPRISAL_ANNOTATION_DIR)
removeAnnotation(SURPRISAL_ANNOTATION_DIR)
end
MAKE_SKIPPING_STATISTICS = false
if MAKE_SKIPPING_STATISTICS then
if DO_TRAINING then
crash()
end
total_encountered_statistics = {}
has_seen_in_skipping_statistics = {}
print("WRITING SKIPPING SCORES")
end
function printSkippingStatistics(corpus)
local skippingStatisticsTable = {}
for i=1,#has_seen_in_skipping_statistics do
if has_seen_in_skipping_statistics[i] ~= nil then
if total_encountered_statistics[has_seen_in_skipping_statistics[i]][1] > 100 then
table.insert(skippingStatisticsTable,{readDict.chars[i], ((100.0 * total_encountered_statistics[has_seen_in_skipping_statistics[i]][2]) / total_encountered_statistics[has_seen_in_skipping_statistics[i]][1])})
end
end
end
table.sort(skippingStatisticsTable, function(a1, a2)
return (a1[2] < a2[2])
end
)
for i=1, #skippingStatisticsTable do
print(skippingStatisticsTable[i][1].." "..skippingStatisticsTable[i][2])
end
end
function updateSkippingStatistics(corpus)
for i=1, params.seq_length do
for item = 1, params.batch_size do
local token = getFromData(corpus,item,i)
if has_seen_in_skipping_statistics[token] == nil then
has_seen_in_skipping_statistics[token] = (#total_encountered_statistics)+1
table.insert(total_encountered_statistics, {1,0})
else
total_encountered_statistics[has_seen_in_skipping_statistics[token]][1] = total_encountered_statistics[has_seen_in_skipping_statistics[token]][1] +1
end
if attention_decisions[i][item] == 0 then
total_encountered_statistics[has_seen_in_skipping_statistics[token]][2] = total_encountered_statistics[has_seen_in_skipping_statistics[token]][2] + 1
end
end
end
end
function storeAttentionAnnotationQA(corpus)
assert(params.TASK == "neat-qa")
for batchIndex = 1, params.batch_size do
local fileNameForBatchItem = readChunks.files[readChunks.corpusReading.currentFile-params.batch_size+batchIndex]
print(fileNameForBatchItem)
if(fileNameForBatchItem == nil) then
print("Warning: Batch item "..batchIndex.." does not belong to a file.")
goto continue
end
local outputFileNameForBatchItem = ATTENTION_ANNOTATION_DIR.."/"..fileNameForBatchItem..".att.anno"
local fileOut = io.open(outputFileNameForBatchItem, "a")
print("11214 "..outputFileNameForBatchItem)
fileOut:write("++\n")
for j=1, neatQA.maximalLengthOccurringInInput[1] do
local inputWordNumber = neatQA.inputTensors[j][batchIndex]
if inputWordNumber == 0 then
goto continueInner
end
local toPrint = j.." "..inputWordNumber.." "..readDict.chars[inputWordNumber].." "..attention_decisions[j][batchIndex][1].." "..attention_scores[j][batchIndex][1].."\n"
fileOut:write(toPrint)
::continueInner::
end
fileOut:close()
::continue::
end
end
function storeAttentionAnnotation(corpus)
assert(params.TASK ~= "neat-qa")
for batchIndex = 1, params.batch_size do
local fileOut = io.open(ATTENTION_ANNOTATION_DIR.."/"..readChunks.files[readChunks.corpusReading.locationsOfLastBatchChunks[batchIndex].file]..".att.anno", "a")
print(ATTENTION_ANNOTATION_DIR.."/"..readChunks.files[readChunks.corpusReading.locationsOfLastBatchChunks[batchIndex].file]..".att.anno")
fileOut:write("++\n")
for j=1, params.seq_length do
local toPrint
if params.TASK == "combined" then
toPrint = (readChunks.corpusReading.locationsOfLastBatchChunks[batchIndex].offset + j - 1).." "..getFromData(corpus,batchIndex,j).." "..attention_decisions[j][batchIndex].." "..attention_scores[j][batchIndex][1].."\n"
elseif params.TASK == "combined-soft" then
toPrint = (readChunks.corpusReading.locationsOfLastBatchChunks[batchIndex].offset + j - 1).." "..getFromData(corpus,batchIndex,j).." ".."1".." "..attention_scores[j][batchIndex][1].."\n"
else
crash()
end
fileOut:write(toPrint)
end
fileOut:close()
end
end
function storeSurprisalScores(corpus)
for batchIndex = 1, params.batch_size do
if(readChunks.files[readChunks.corpusReading.locationsOfLastBatchChunks[batchIndex].file] == nil) then
print("Warning: Batch item does not belong to a file.")
goto continue
end
local fileOut = io.open(SURPRISAL_ANNOTATION_DIR.."/"..readChunks.files[readChunks.corpusReading.locationsOfLastBatchChunks[batchIndex].file]..".surp.anno", "a")
if torch.uniform() > 0.99 then
print(SURPRISAL_ANNOTATION_DIR.."/"..readChunks.files[readChunks.corpusReading.locationsOfLastBatchChunks[batchIndex].file]..".surp.anno")
end
fileOut:write("++\n")
for j=1, params.seq_length do
local surprisal
if params.TASK == 'combined' or params.TASK == "combined-soft" then
if j==1 then
surprisal = 100
else
surprisal = reader_output[j-1][batchIndex][getFromData(corpus,batchIndex,j)]
end
elseif params.TASK == 'langmod' then
surprisal = actor_output[j][batchIndex][getFromData(corpus,batchIndex,j)]
else
crash()
end
fileOut:write((readChunks.corpusReading.locationsOfLastBatchChunks[batchIndex].offset + j - 1).." "..getFromData(corpus,batchIndex,j).." "..(surprisal).."\n")
end
fileOut:close()
::continue::
end
end