forked from macournoyer/neuralconvo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.lua
265 lines (214 loc) · 6.99 KB
/
dataset.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
--[[
Format movie dialog data as a table of line 1:
{ {word_ids of character1}, {word_ids of character2} }
Then flips it around and get the dialog from the other character's perspective:
{ {word_ids of character2}, {word_ids of character1} }
Also builds the vocabulary.
]]--
local DataSet = torch.class("neuralconvo.DataSet")
local xlua = require "xlua"
local tokenizer = require "tokenizer"
local list = require "pl.List"
function DataSet:__init(loader, options)
options = options or {}
self.examplesFilename = "data/examples.t7"
-- Reject words once vocab size reaches this threshold
self.maxVocabSize = options.maxVocabSize or 0
-- Maximum number of words in an example sentence
self.maxExampleLen = options.maxExampleLen or 25
-- Load only first fews examples (approximately)
self.loadFirst = options.loadFirst
self.examples = {}
self.word2id = {}
self.id2word = {}
self.wordsCount = 0
self:load(loader)
end
function DataSet:load(loader)
local filename = "data/vocab.t7"
if path.exists(filename) then
print("Loading vocabulary from " .. filename .. " ...")
local data = torch.load(filename)
self.word2id = data.word2id
self.id2word = data.id2word
self.wordsCount = data.wordsCount
self.goToken = data.goToken
self.eosToken = data.eosToken
self.unknownToken = data.unknownToken
self.examplesCount = data.examplesCount
else
print("" .. filename .. " not found")
self:visit(loader:load())
print("Writing " .. filename .. " ...")
torch.save(filename, {
word2id = self.word2id,
id2word = self.id2word,
wordsCount = self.wordsCount,
goToken = self.goToken,
eosToken = self.eosToken,
unknownToken = self.unknownToken,
examplesCount = self.examplesCount
})
end
end
function DataSet:visit(conversations)
self.examples = {}
-- Add magic tokens
self.goToken = self:makeWordId("<go>") -- Start of sequence
self.eosToken = self:makeWordId("<eos>") -- End of sequence
self.unknownToken = self:makeWordId("<unknown>") -- Word dropped from vocabulary
print("-- Pre-processing data")
local total = self.loadFirst or #conversations * 2
for i, conversation in ipairs(conversations) do
if i > total then break end
self:visitConversation(conversation)
xlua.progress(i, total)
end
-- Revisit from the perspective of 2nd character
for i, conversation in ipairs(conversations) do
if #conversations + i > total then break end
self:visitConversation(conversation, 2)
xlua.progress(#conversations + i, total)
end
print("-- Shuffling ")
newIdxs = torch.randperm(#self.examples)
local sExamples = {}
for i, sample in ipairs(self.examples) do
sExamples[i] = self.examples[newIdxs[i]]
end
self.examples = sExamples
self.examplesCount = #self.examples
self:writeExamplesToFile()
self.examples = nil
collectgarbage()
end
function DataSet:writeExamplesToFile()
print("Writing " .. self.examplesFilename .. " ...")
local file = torch.DiskFile(self.examplesFilename, "w")
for i, example in ipairs(self.examples) do
file:writeObject(example)
xlua.progress(i, #self.examples)
end
file:close()
end
function DataSet:batches(size)
local file = torch.DiskFile(self.examplesFilename, "r")
file:quiet()
local done = false
return function()
if done then
return
end
local inputSeqs,targetSeqs = {},{}
local maxInputSeqLen,maxTargetOutputSeqLen = 0,0
for i = 1, size do
local example = file:readObject()
if example == nil then
done = true
file:close()
return examples
end
inputSeq,targetSeq = unpack(example)
if inputSeq:size(1) > maxInputSeqLen then
maxInputSeqLen = inputSeq:size(1)
end
if targetSeq:size(1) > maxTargetOutputSeqLen then
maxTargetOutputSeqLen = targetSeq:size(1)
end
table.insert(inputSeqs, inputSeq)
table.insert(targetSeqs, targetSeq)
end
local encoderInputs,decoderInputs,decoderTargets = nil,nil,nil
if size == 1 then
encoderInputs = torch.IntTensor(maxInputSeqLen):fill(0)
decoderInputs = torch.IntTensor(maxTargetOutputSeqLen-1):fill(0)
decoderTargets = torch.IntTensor(maxTargetOutputSeqLen-1):fill(0)
else
encoderInputs = torch.IntTensor(maxInputSeqLen,size):fill(0)
decoderInputs = torch.IntTensor(maxTargetOutputSeqLen-1,size):fill(0)
decoderTargets = torch.IntTensor(maxTargetOutputSeqLen-1,size):fill(0)
end
for samplenb = 1, #inputSeqs do
for word = 1,inputSeqs[samplenb]:size(1) do
eosOffset = maxInputSeqLen - inputSeqs[samplenb]:size(1) -- for left padding
if size == 1 then
encoderInputs[word] = inputSeqs[samplenb][word]
else
encoderInputs[word+eosOffset][samplenb] = inputSeqs[samplenb][word]
end
end
end
for samplenb = 1, #targetSeqs do
trimmedEosToken = targetSeqs[samplenb]:sub(1,-2)
for word = 1, trimmedEosToken:size(1) do
if size == 1 then
decoderInputs[word] = trimmedEosToken[word]
else
decoderInputs[word][samplenb] = trimmedEosToken[word]
end
end
end
for samplenb = 1, #targetSeqs do
trimmedGoToken = targetSeqs[samplenb]:sub(2,-1)
for word = 1, trimmedGoToken:size(1) do
if size == 1 then
decoderTargets[word] = trimmedGoToken[word]
else
decoderTargets[word][samplenb] = trimmedGoToken[word]
end
end
end
return encoderInputs,decoderInputs,decoderTargets
end
end
function DataSet:visitConversation(lines, start)
start = start or 1
for i = start, #lines, 2 do
local input = lines[i]
local target = lines[i+1]
if target then
local inputIds = self:visitText(input.text)
local targetIds = self:visitText(target.text, 2)
if inputIds and targetIds then
-- Revert inputs
inputIds = list.reverse(inputIds)
table.insert(targetIds, 1, self.goToken)
table.insert(targetIds, self.eosToken)
table.insert(self.examples, { torch.IntTensor(inputIds), torch.IntTensor(targetIds) })
end
end
end
end
function DataSet:visitText(text, additionalTokens)
local words = {}
additionalTokens = additionalTokens or 0
if text == "" then
return
end
for t, word in tokenizer.tokenize(text) do
table.insert(words, self:makeWordId(word))
-- Only keep the first sentence
if t == "endpunct" or #words >= self.maxExampleLen - additionalTokens then
break
end
end
if #words == 0 then
return
end
return words
end
function DataSet:makeWordId(word)
if self.maxVocabSize > 0 and self.wordsCount >= self.maxVocabSize then
-- We've reached the maximum size for the vocab. Replace w/ unknown token
return self.unknownToken
end
word = word:lower()
local id = self.word2id[word]
if not id then
self.wordsCount = self.wordsCount + 1
id = self.wordsCount
self.id2word[id] = word
self.word2id[word] = id
end
return id
end