-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepareData.lua
280 lines (258 loc) · 9.54 KB
/
prepareData.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
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
-- CNN data loading
-- Author: Minwei Feng (mfeng@us.ibm.com)
torch.setdefaulttensortype('torch.FloatTensor')
init_voc = {}
train_size = 0
valid_size = 0
test_size = 0
train_len = {}
valid_len = {}
test_len = {}
for line in io.lines(opt.trainFile) do
train_size = train_size + 1
local text = stringx.split(line, '\t')[2]
local w = stringx.split(text)
for i = 1,#w do
if not init_voc[w[i]] then init_voc[w[i]]=1 end
end
train_len[train_size] = #w
end
for line in io.lines(opt.validFile) do
valid_size = valid_size + 1
local text = stringx.split(line, '\t')[2]
local w = stringx.split(text)
for i = 1,#w do
if not init_voc[w[i]] then init_voc[w[i]]=1 end
end
valid_len[valid_size] = #w
end
for line in io.lines(opt.testFile) do
test_size = test_size + 1
local text = stringx.split(line, '\t')[2]
local w = stringx.split(text)
for i = 1,#w do
if not init_voc[w[i]] then init_voc[w[i]]=1 end
end
test_len[test_size] = #w
end
init_size = 0
for k,v in pairs(init_voc) do init_size = init_size + 1 end
init_size = init_size + 2
local train_cutoff = math.floor(2 * torch.std(torch.Tensor(train_len)) + torch.mean(torch.Tensor(train_len)))
if train_cutoff > opt.trainMaxLength then
train_cutoff = opt.trainMaxLength
end
if train_cutoff < opt.trainMinLength then
train_cutoff = opt.trainMinLength
end
local valid_cutoff = math.floor(3 * torch.std(torch.Tensor(valid_len)) + torch.mean(torch.Tensor(valid_len)))
if valid_cutoff > opt.testMaxLength then
valid_cutoff = opt.testMaxLength
end
if valid_cutoff < opt.testMinLength then
valid_cutoff = opt.testMinLength
end
if conf.rank == 0 then
print(string.format("%s %f", "Training data length standard deviation: ", torch.std(torch.Tensor(train_len)) ))
print(string.format("%s %f", "Training data length mean: ", torch.mean(torch.Tensor(train_len)) ))
print(string.format("%s %f", "Training data length cutoff: ", train_cutoff))
print(string.format("%s %f", "Valid data length standard deviation: ", torch.std(torch.Tensor(valid_len)) ))
print(string.format("%s %f", "Valid data length mean: ", torch.mean(torch.Tensor(valid_len)) ))
print(string.format("%s %f", "Valid data length cutoff: ", valid_cutoff))
end
--[[
local cutoff
if train_cutoff > valid_cutoff then
cutoff = train_cutoff
else
cutoff = valid_cutoff
end
opt.cutoff = cutoff
print(string.format("%s %f", "Final length cutoff: ", cutoff))
--]]
local trainDataTensor_ydim = train_cutoff + opt.contConvWidth + opt.contConvWidth - 1
local validDataTensor_ydim = valid_cutoff + opt.contConvWidth + opt.contConvWidth - 1
local testDataTensor_ydim = valid_cutoff + opt.contConvWidth + opt.contConvWidth - 1
--trainDataTensor_ydim,validDataTensor_ydim,testDataTensor_ydim = 30,30,30
trainDataTensor = torch.Tensor(math.ceil(train_size/opt.batchSize)*opt.batchSize, trainDataTensor_ydim)
trainDataTensor_y = torch.Tensor(math.ceil(train_size/opt.batchSize)*opt.batchSize)
validDataTensor = torch.Tensor(valid_size, validDataTensor_ydim)
testDataTensor = torch.Tensor(test_size, testDataTensor_ydim)
if opt.trainFile ~= 'none' then
trainFileHandle = assert(io.open(opt.trainFile, 'r'))
end
if opt.validFile ~= 'none' then
validFileHandle = assert(io.open(opt.validFile, 'r'))
end
if opt.testFile ~= 'none' then
testFileHandle = assert(io.open(opt.testFile, 'r'))
end
if opt.embeddingFile ~= 'none' then
embeddingFileHandle = assert(io.open(opt.embeddingFile, 'r'))
end
local BUFSIZE = 2^13
local zeroEmbedding1 = {}
local zeroEmbedding2 = {}
local zeroEmbedding = {}
mapWordIdx2Vector = torch.zeros(init_size, opt.embeddingDim)
function augmentWordIdx2Vector()
print("augmentWordIdx2Vector!!!!!!!!!!!!!")
local temp = mapWordIdx2Vector
mapWordIdx2Vector = torch.zeros(temp:size()[1] + 1000, opt.embeddingDim)
mapWordIdx2Vector:narrow(1,1,temp:size()[1]):copy(temp)
temp = nil
end
mapWordStr2WordIdx['SENTBEGIN'] = 1
mapWordStr2WordIdx['SENTEND'] = 2
mapWordIdx2WordStr[1] = 'SENTBEGIN'
mapWordIdx2WordStr[2] = 'SENTEND'
idx=3
while true do
local lines, rest = embeddingFileHandle:read(BUFSIZE, '*line')
if not lines then break end
if rest then lines = lines .. rest .. '\n' end
local b = 0
local e = 0
while true do
b = e + 1
e = string.find(lines, '\n', b)
if e == nil then break end
local line = string.sub(lines, b, e-1)
local k = string.sub(line, 1, string.find(line, '\t')-1 )
local v = string.sub(line, string.find(line, '\t')+1, -1 )
if init_voc[k] then
local temptable = {}
for m in string.gmatch(v, "%S+") do
temptable[#temptable+1] = tonumber(m)
end
mapWordIdx2Vector:narrow(1,idx,1):copy(torch.Tensor(temptable))
mapWordStr2WordIdx[k] = idx
mapWordIdx2WordStr[idx] = k
idx = idx + 1
end
end
end
ln = 0
for line in io.lines(opt.trainFile) do
ln = ln + 1
for i=1,opt.contConvWidth do trainDataTensor[ln][i] = mapWordStr2WordIdx['SENTBEGIN'] ; end
trainDataTensor_y[ln] = tonumber(stringx.split(line, '\t')[1])
local tempL = {}
tempL[#tempL+1] = tonumber(stringx.split(line, '\t')[1])
table.insert(trainDataTensor_y_2, tempL)
local text = stringx.split(line, '\t')[2]
local w = stringx.split(text)
local j = opt.contConvWidth + 1
for i = 1,#w do
if mapWordStr2WordIdx[w[i]] == nil then
if idx > mapWordIdx2Vector:size()[1] then
augmentWordIdx2Vector()
end
mapWordStr2WordIdx[w[i]] = idx
mapWordIdx2WordStr[idx] = w[i]
local oovEmbedding = {}
for i=1,opt.embeddingDim do oovEmbedding[i] = math.random(); end
mapWordIdx2Vector:narrow(1,idx,1):copy(torch.Tensor(oovEmbedding))
idx = idx + 1
end
if j <= trainDataTensor_ydim - opt.contConvWidth + 1 then
trainDataTensor[ln][j] = mapWordStr2WordIdx[w[i]]
j = j + 1
end
end
for r = j,trainDataTensor_ydim do
trainDataTensor[ln][r] = mapWordStr2WordIdx['SENTEND']
end
end
while ln < trainDataTensor:size()[1] do
-- print("############### " .. ln)
local i = math.random(1,ln)
ln = ln + 1
trainDataTensor_y[ln] = trainDataTensor_y[i]
trainDataTensor:narrow(1,ln,1):copy( trainDataTensor:narrow(1,i,1) )
local tempL = {}
tempL[#tempL+1] = trainDataTensor_y[i]
table.insert(trainDataTensor_y_2, tempL)
end
ln = 0
for line in io.lines(opt.validFile) do
ln = ln + 1
local tempL = {}
for i=1,opt.contConvWidth do validDataTensor[ln][i] = mapWordStr2WordIdx['SENTBEGIN'] ; end
for k in string.gmatch(stringx.split(line, '\t')[1], "%S+") do
tempL[#tempL+1] = tonumber(k)
end
table.insert(validDataTensor_y, tempL)
local text = stringx.split(line, '\t')[2]
local w = stringx.split(text)
local j = opt.contConvWidth + 1
for i = 1,#w do
if mapWordStr2WordIdx[w[i]] == nil then
if idx > mapWordIdx2Vector:size()[1] then
augmentWordIdx2Vector()
end
mapWordStr2WordIdx[w[i]] = idx
mapWordIdx2WordStr[idx] = w[i]
local oovEmbedding = {}
for i=1,opt.embeddingDim do oovEmbedding[i] = math.random(); end
mapWordIdx2Vector:narrow(1,idx,1):copy(torch.Tensor(oovEmbedding))
idx = idx + 1
end
if j <= validDataTensor_ydim - opt.contConvWidth + 1 then
validDataTensor[ln][j] = mapWordStr2WordIdx[w[i]]
j = j + 1
end
end
for r = j,validDataTensor_ydim do
validDataTensor[ln][r] = mapWordStr2WordIdx['SENTEND']
end
end
ln = 0
for line in io.lines(opt.testFile) do
ln = ln + 1
local tempL = {}
for i=1,opt.contConvWidth do testDataTensor[ln][i] = mapWordStr2WordIdx['SENTBEGIN'] ; end
for k in string.gmatch(stringx.split(line, '\t')[1], "%S+") do
tempL[#tempL+1] = tonumber(k)
end
table.insert(testDataTensor_y, tempL)
local text = stringx.split(line, '\t')[2]
local w = stringx.split(text)
local j = opt.contConvWidth + 1
for i = 1,#w do
if mapWordStr2WordIdx[w[i]] == nil then
if idx > mapWordIdx2Vector:size()[1] then
augmentWordIdx2Vector()
end
mapWordStr2WordIdx[w[i]] = idx
mapWordIdx2WordStr[idx] = w[i]
local oovEmbedding = {}
for i=1,opt.embeddingDim do oovEmbedding[i] = math.random(); end
mapWordIdx2Vector:narrow(1,idx,1):copy(torch.Tensor(oovEmbedding))
idx = idx + 1
end
if j <= testDataTensor_ydim - opt.contConvWidth + 1 then
testDataTensor[ln][j] = mapWordStr2WordIdx[w[i]]
j = j + 1
end
end
for r = j,testDataTensor_ydim do
testDataTensor[ln][r] = mapWordStr2WordIdx['SENTEND']
end
end
if conf.rank == 0 then
print(string.format('training data size: %s x %s', trainDataTensor:size()[1], trainDataTensor:size()[2]))
print(string.format('valid data size: %s x %s', validDataTensor:size()[1], validDataTensor:size()[2]))
print(string.format('test data size: %s x %s', testDataTensor:size()[1], testDataTensor:size()[2]))
print(string.format('mapWordIdx2WordStr size: %s', #mapWordIdx2WordStr))
print(string.format('mapWordIdx2Vector size: %s', mapWordIdx2Vector:size()[1]))
end
assert(trainFileHandle:close())
assert(validFileHandle:close())
assert(testFileHandle:close())
assert(embeddingFileHandle:close())
collectgarbage()
collectgarbage()
if conf.rank == 0 then
print("At the end of prepareData, amount of memory currently used in Kilobytes: ", collectgarbage("count"))
end