3

我仍然熟悉 Torch,到目前为止一切都很好。然而,我遇到了一个我不知道如何解决的死胡同:我怎样才能让 Torch7(或更具体地说是 dp 库)来评估单个输入并返回预测的输出?

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

  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)
4

1 回答 1

3

你有两个选择。

一。使用封装的nn.Module转发您的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)

二。将您的 torch.Tensor 封装到dp.ImageView并通过您的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')
于 2015-04-01T20:45:20.397 回答