aboutsummaryrefslogtreecommitdiff
path: root/nerv/examples/lmptb/lm_sampler.lua
diff options
context:
space:
mode:
Diffstat (limited to 'nerv/examples/lmptb/lm_sampler.lua')
-rw-r--r--nerv/examples/lmptb/lm_sampler.lua20
1 files changed, 13 insertions, 7 deletions
diff --git a/nerv/examples/lmptb/lm_sampler.lua b/nerv/examples/lmptb/lm_sampler.lua
index c165127..c25a75c 100644
--- a/nerv/examples/lmptb/lm_sampler.lua
+++ b/nerv/examples/lmptb/lm_sampler.lua
@@ -37,14 +37,16 @@ function LMSampler:load_dagL(dagL)
self.store[i][1].id = self.sen_end_id
self.store[i][1].p = 0
end
+ self.repo = {}
end
function LMSampler:sample_to_store(smout)
for i = 1, self.batch_size do
local ran = math.random()
- local s = 0, id = self.vocab:size()
+ local s = 0
+ local id = self.vocab:size()
for j = 0, self.vocab:size() - 1 do
- s = s + smout[i][j]
+ s = s + smout[i - 1][j]
if s >= ran then
id = j + 1
break
@@ -56,7 +58,7 @@ function LMSampler:sample_to_store(smout)
local tmp = {}
tmp.w = self.vocab:get_word_id(id).str
tmp.id = id
- tmp.p = smout[i][id]
+ tmp.p = smout[i - 1][id - 1]
table.insert(self.store[i], tmp)
end
end
@@ -66,9 +68,8 @@ function LMSampler:lm_sample_rnn_dagL(sample_num, p_conf)
local dagL = self.dagL
local inputs = self.dagL_inputs
local outputs = self.dagL_outputs
-
- local res = {}
- while #res < sample_num do
+
+ while #self.repo < sample_num do
dagL:propagate(inputs, outputs)
inputs[2]:copy_fromd(outputs[2]) --copy hidden activation
@@ -80,7 +81,7 @@ function LMSampler:lm_sample_rnn_dagL(sample_num, p_conf)
inputs[1][i - 1][0] = self.store[i][#self.store[i]].id - 1
if self.store[i][#self.store[i]].id == self.sen_end_id then --meet a sentence end
if #self.store[i] >= 3 then
- res[#res + 1] = self.store[i]
+ self.repo[#self.repo + 1] = self.store[i]
end
inputs[2][i - 1]:fill(0)
self.store[i] = {}
@@ -94,5 +95,10 @@ function LMSampler:lm_sample_rnn_dagL(sample_num, p_conf)
collectgarbage("collect")
end
+ local res = {}
+ for i = 1, sample_num do
+ res[i] = self.repo[#self.repo]
+ self.repo[#self.repo] = nil
+ end
return res
end