-
Notifications
You must be signed in to change notification settings - Fork 346
/
eval.lua
50 lines (39 loc) · 993 Bytes
/
eval.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
require 'e'
local tokenizer = require "tokenizer"
local list = require "pl.list"
if dataset == nil then
cmd = torch.CmdLine()
cmd:text('Options:')
cmd:option('--cuda', false, 'use CUDA. Training must be done on CUDA')
cmd:text()
options = cmd:parse(arg)
-- Data
dataset = e.DataSet()
-- Enabled CUDA
if options.cuda then
require 'cutorch'
require 'cunn'
end
end
if model == nil then
print("-- Loading model")
model = torch.load("data/model.t7")
end
-- Word ID tensor to words
function t2w(t)
local words = {}
for i = 1, t:size(1) do
table.insert(words, dataset.id2word[t[i]])
end
return words
end
function say(text)
local wordIds = {}
for t, word in tokenizer.tokenize(text) do
local id = dataset.word2id[word:lower()] or dataset.unknownToken
table.insert(wordIds, id)
end
local input = torch.Tensor(list.reverse(wordIds))
local output = model:eval(input)
print(">> " .. tokenizer.join(t2w(torch.Tensor(output))))
end