如何使用由Torch生成的模型进行预测?

我已经执行了neuralnetwork_tutorial.lua。现在我有了模型,我想用一些我自己手写的图像进行测试。但是我已经尝试了许多种保存权重的方法,现在我尝试使用torch save和load方法来保存整个模型。

然而,当我尝试使用model: forward(testImageTensor)来预测我自己手写的图像(转换为28X28 DoubleTensor)时,

...ches/torch/install/share/lua/5.1/dp/model/sequential.lua:30: attempt to index local 'carry' (a nil value)
stack traceback:
        ...ches/torch/install/share/lua/5.1/dp/model/sequential.lua:30: in function '_forward'
        ...s/torches/torch/install/share/lua/5.1/dp/model/model.lua:60: in function 'forward'
        [string "model:forward(testImageTensor)"]:1: in main chunk
        [C]: in function 'xpcall'
        ...aries/torches/torch/install/share/lua/5.1/trepl/init.lua:588: in function 'repl'
        ...ches/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk
        [C]: at 0x0804d650
点赞
用户49985
用户49985

你有两个选择。

一. 使用封装的 nn.Module 来正向传播你的 torch.Tensor:

mlp2 = mlp:toModule(datasource:trainSet():sub(1,2))
input = testImageTensor:view(1, 1, 32, 32)
output = mlp2:forward(input)

二. 将你的 torch.Tensor 封装到 dp.ImageView 中,然后将其通过你的 dp.Model 进行正向传播:

inputView = dp.ImageView('bchw', testImageTensor:view(1, 1, 32, 32))
outputView = mlp:forward(inputView, dp.Carry{nSample=1})
output = outputView:forward('b')
2015-03-02 16:09:32