-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy patheval.lua
62 lines (48 loc) · 1.3 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
require 'e'
local tokenizer = require "tokenizer"
local list = require "pl.list"
if dataset == nil then
cmd = torch.CmdLine()
cmd:text('Options:')
cmd:option('--datafile', "data/cornell_movie_dialogs.t7", 'data file to load')
cmd:option('--cuda', false, 'use CUDA. Training must be done on CUDA')
cmd:text()
options = cmd:parse(arg)
-- Data
dataset = e.DataSet(options.datafile)
-- 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 IDs tensor to sentence
function t2s(t, reverse)
local words = {}
for i = 1, t:size(1) do
table.insert(words, dataset.id2word[t[i]])
end
if reverse then
words = list.reverse(words)
end
return table.concat(words, " ")
end
-- for i,example in ipairs(dataset.examples) do
-- print("-- " .. t2s(example[1], true))
-- print(">> " .. t2s(example[2]))
-- 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))
print("-- " .. t2s(input, true))
local output = model:eval(input)
print(">> " .. t2s(torch.Tensor(output)))
end