-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_main_sylgen.lua
executable file
·174 lines (147 loc) · 4.2 KB
/
demo_main_sylgen.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
--- Demo: Korean Syllable Generation
-- 2017.11.13
-- Hwaran Lee @ KAIST
require 'torch'
require 'nn'
require 'model.LSTM'
utf8 = require 'lua-utf8'
cmd = torch.CmdLine()
cmd:text()
cmd:text('Korean sentence generation (syllable)')
cmd:text('Options')
cmd:option('-model_dir','exp/lm-rnn2048x2-v2-wd0-dw1/lm_char_epoch43.00.t7','trained model directory')
cmd:option('-vocab', 'exp/vocab.t7', 'model checkpoint file')
-- GPU/CPU these params must be passed in because it affects the constructors
cmd:option('-gpuid', 0,'which gpu to use. -1 = use CPU')
cmd:option('-cudnn', 0,'use cudnn (1 = yes, 0 = no)')
cmd:text()
-- parse input params
opt = cmd:parse(arg)
-- global constants for certain tokens
tokens = {}
tokens.EOS = '<eos>' --<eos>
tokens.UNK = '<unk>' --<unk>
tokens.SOS = '<sos>' --<sos>'
tokens.BLK = '|'
-- load necessary packages depending on config options
dtype = 'torch.FloatTensor'
if opt.gpuid >= 0 then
print('using CUDA on GPU ' .. opt.gpuid .. '...')
require 'cutorch'
require 'cunn'
cutorch.setDevice(opt.gpuid + 1)
dtype = 'torch.CudaTensor'
end
--- vocabulary loading ---
print('--- Loading vocab : ' .. opt.vocab)
vocab = torch.load(opt.vocab)
idx2word, word2idx, _, _ = table.unpack(vocab)
print(string.format('Word vocab size: %d', #idx2word))
--- network loading ---
function resetStates_LSTM(protos)
for i, v in pairs(protos.net:findModules('nn.LSTM')) do
v:resetStates()
end
end
function rememberStates_LSTM(protos)
for i, v in pairs(protos.net:findModules('nn.LSTM')) do
v.remember_states = true
end
end
print('-- Building network --')
checkpoint = torch.load(opt.model_dir)
opt = checkpoint.opt
protos = checkpoint.protos
rememberStates_LSTM(protos)
if opt.gpuid >= 0 then
for k,v in pairs(protos) do v:cuda() end
end
function txt2tensor(line)
local word_num = 0
for word in utf8.gmatch(line, '.') do
word_num = word_num + 1
end
local x = torch.Tensor(word_num+1):long()
x[1] = word2idx[tokens.SOS]
word_num = 1
for word in utf8.gmatch(line, '.') do
word_num = word_num + 1
if word == ' ' then word = tokens.BLK end
if word2idx[word]==nil then
x[word_num] = word2idx[tokens.UNK]
else
x[word_num] = word2idx[word]
end
end
return x:resize(1,word_num)
end
function tensor2txt(seq)
line = ''
for t =1, seq:size(1) do
if idx2word[seq[t]] == tokens.UNK then break end
if idx2word[seq[t]] == tokens.BLK then
line = line .. ' '
else
line = line .. idx2word[seq[t]]
end
end
return line
end
function generation(preSeq, maxLen)
-- preSeq is tensor
local batch_size = 2
preSeq = torch.cat(preSeq, preSeq, 1)
preLen = preSeq:size(2)
seq = torch.LongTensor(2,maxLen):fill(1)
if opt.gpuid >= 0 then seq=seq:cuda() preSeq=preSeq:cuda() end
resetStates_LSTM(protos)
protos.net:evaluate()
-- (1) forward the preSeq
protos.view_in:resetSize(batch_size*preLen, -1)
protos.view_out:resetSize(batch_size, preLen, -1)
out = protos.net:forward(preSeq)
local function select_next(top)
i=1
while i < #idx2word do
if idx2word[top[i]] ~= tokens.UNK and idx2word[top[i]] ~= tokens.SOS then
break
end
i = i+1
end
return top[i]
end
_, top = torch.sort(out[{1,preLen}], 1, true)
next_tok = select_next(top)
seq:select(2,1):fill(top[1])
if next_tok == tokens.EOS then return seq[{1,{}}] end
seqLen = 1
-- (2) generate seq iteratively
for t = 1, maxLen-1 do
protos.view_in:resetSize(batch_size*1, -1)
protos.view_out:resetSize(batch_size, 1, -1)
out = protos.net:forward(seq[{{},t}]:resize(2,1))
_, top = torch.sort(out[{1,1,{}}], 1, true)
next_tok = select_next(top)
seq:select(2,t+1):fill(top[1])
seqLen = seqLen+1
if next_tok == word2idx[tokens.EOS] then break end
end
return seq[{1, {1, seqLen}}]
end
--- main ---
while(true)
do
print('======================================')
io.write("Please enter a front part of sentence : \n")
io.flush()
input = io.read()
if input == 'exit' then break end
maxLen = 100
preSeq = txt2tensor(input)
outseq = generation(preSeq, maxLen)
output = tensor2txt(outseq)
output = input .. output
print(' \n')
print(output)
print(' \n')
end