如何使用Torch7获得预测 [英] How to get a prediction using Torch7

查看:106
本文介绍了如何使用Torch7获得预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我仍然很熟悉Torch.但是,我遇到了一个死胡同,我不确定该如何解决:如何才能使Torch7(或更具体地说是dp库)评估单个输入并返回预测的输出?

I'm still familiarizing myself with Torch and so far so good. I have however hit a dead end that I'm not sure how to get around: how can I get Torch7 (or more specifically the dp library) to evaluate a single input and return the predicted output?

这是我的设置(基本上是dp演示):

Here's my setup (basically the dp demo):

  require 'dp'
--[[hyperparameters]]--
opt = {
    nHidden = 100, --number of hidden units
    learningRate = 0.1, --training learning rate
    momentum = 0.9, --momentum factor to use for training
    maxOutNorm = 1, --maximum norm allowed for output neuron weights
    batchSize = 128, --number of examples per mini-batch
    maxTries = 100, --maximum number of epochs without reduction in validation error.
    maxEpoch = 1000 --maximum number of epochs of training
}
--[[data]]--
datasource = dp.Mnist{input_preprocess = dp.Standardize()}
print("feature size: ", datasource:featureSize())

--[[Model]]--
model = dp.Sequential{
    models = {
        dp.Neural{
            input_size = datasource:featureSize(),
            output_size = opt.nHidden,
            transfer = nn.Tanh(),
            sparse_init = true
        },
        dp.Neural{
            input_size = opt.nHidden,
            output_size = #(datasource:classes()),
            transfer = nn.LogSoftMax(),
            sparse_init = true
        }
    }
}

--[[Propagators]]--
train = dp.Optimizer{
    loss = dp.NLL(),
    visitor = { -- the ordering here is important:
        dp.Momentum{momentum_factor = opt.momentum},
        dp.Learn{learning_rate = opt.learningRate},
        dp.MaxNorm{max_out_norm = opt.maxOutNorm}
    },
    feedback = dp.Confusion(),
    sampler = dp.ShuffleSampler{batch_size = opt.batchSize},
    progress = true
}
valid = dp.Evaluator{
    loss = dp.NLL(),
    feedback = dp.Confusion(),
    sampler = dp.Sampler{}
}
test = dp.Evaluator{
    loss = dp.NLL(),
    feedback = dp.Confusion(),
    sampler = dp.Sampler{}
}

--[[Experiment]]--
xp = dp.Experiment{
    model = model,
    optimizer = train,
    validator = valid,
    tester = test,
    observer = {
        dp.FileLogger(),
        dp.EarlyStopper{
            error_report = {'validator','feedback','confusion','accuracy'},
            maximize = true,
            max_epochs = opt.maxTries
        }
    },
    random_seed = os.time(),
    max_epoch = opt.maxEpoch
}

xp:run(datasource)

推荐答案

您有两个选择.

一个.使用封装的 nn.Module 转发您的 torch.Tensor :

One. Use the encapsulated nn.Module to forward your torch.Tensor:

mlp = model:toModule(datasource:trainSet():sub(1,2))
mlp:float()
input = torch.FloatTensor(1, 1, 32, 32) -- replace this with your input
output = mlp:forward(input)

两个.将您的割炬.Tensor封装到 dp.ImageView 中,然后通过您的 dp.Model 转发:

Two. Encapsulate your torch.Tensor into a dp.ImageView and forward that through your dp.Model :

input = torch.FloatTensor(1, 1, 32, 32) -- replace with your input
inputView = dp.ImageView('bchw', input)
outputView = mlp:forward(inputView, dp.Carry{nSample=1})
output = outputView:forward('b')

这篇关于如何使用Torch7获得预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆