火炬mnist简单

时间:2016-05-15 18:15:42

标签: lua torch

我正在尝试运行mnist数据集的火炬教程,并且不太了解错误。这是我的代码(几乎只是带有mnist的教程,并添加了填充以纠正28!= 32):

加载数据

mnist = require 'mnist'

trainset = mnist.traindataset()
testset = mnist.testdataset()

setmetatable(trainset, 
    {__index = function(t, i) 
                    return {t.data[i], t.label[i]} 
                end}
);
trainset.data = trainset.data:double() -- convert the data from a ByteTensor to a DoubleTensor.

function trainset:size() 
    return self.data:size(1) 
end

归一化

mean = {}
stdv  = {}
mean = trainset.data[{ {}, {}, {}}]:mean()
trainset.data[{ {}, {}, {}  }]:add(-mean)
stdv = trainset.data[{ {}, {}, {}  }]:std()
trainset.data[{ {}, {}, {}  }]:div(stdv)

定义网络

require 'nn'

net = nn.Sequential()
net:add(nn.SpatialConvolution(1, 6, 5, 5, 1, 1, 2, 2)) -- 1 input image channel, 6 output channels, 5x5 convolution kernel
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.SpatialMaxPooling(2,2,2,2))     -- A max-pooling operation that looks at 2x2 windows and finds the max.
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5))                    -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5
net:add(nn.Linear(16*5*5, 120))             -- fully connected layer (matrix multiplication between input and weights)
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.Linear(120, 84))
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.Linear(84, 10))                   -- 10 is the number of outputs of the network (in this case, 10 digits)
net:add(nn.LogSoftMax())                     -- converts the output to a log-probability. Useful for classification problems

训练

criterion = nn.ClassNLLCriterion()
trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.001
trainer.maxIteration = 5 -- just do 5 epochs of training.
trainer:train(trainset)

现在我收到以下错误信息。

    # StochasticGradient: training .../torch/install/share/lua/5.1/nn/Container.lua:67: 
    In 1 module of nn.Sequential:
   .../torch/install/share/lua/5.1/nn/THNN.lua:109: bad argument #2 to 'v' (3D or 4D (batch mode) tensor expected at .../torch/extra/nn/lib/THNN/generic/SpatialConvolutionMM.c:70)
    stack traceback:
        [C]: in function 'v'
        .../torch/install/share/lua/5.1/nn/THNN.lua:109: in function 'SpatialConvolutionMM_updateOutput'
        ...sm/torch/install/share/lua/5.1/nn/SpatialConvolution.lua:111: in function <...sm/torch/install/share/lua/5.1/nn/SpatialConvolution.lua:107>
        [C]: in function 'xpcall'
        .../torch/install/share/lua/5.1/nn/Container.lua:63: in function 'rethrowErrors'
        .../torch/install/share/lua/5.1/nn/Sequential.lua:44: in function 'forward'
        ...sm/torch/install/share/lua/5.1/nn/StochasticGradient.lua:35: in function 'train'
        [string "trainer:train(trainset)..."]:1: in main chunk
        [C]: in function 'xpcall'
        .../torch/install/share/lua/5.1/itorch/main.lua:209: in function <.../torch/install/share/lua/5.1/itorch/main.lua:173>
        .../torch/install/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
        ...rs/.../install/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
        ...rs/.../install/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
        ...rs/.../install/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
        /Users/.../install/share/lua/5.1/itorch/main.lua:381: in main chunk
        [C]: in function 'require'
        (command line):1: in main chunk
        [C]: at 0x0109becd10

    WARNING: If you see a stack trace below, it doesn't point to the place where this error occured. Please use only the one above.
    stack traceback:
        [C]: in function 'error'
        .../torch/install/share/lua/5.1/nn/Container.lua:67: in function 'rethrowErrors'
        v/torch/install/share/lua/5.1/nn/Sequential.lua:44: in function 'forward'
        ...sm/torch/install/share/lua/5.1/nn/StochasticGradient.lua:35: in function 'train'
        [string "trainer:train(trainset)..."]:1: in main chunk
        [C]: in function 'xpcall'
        .../torch/install/share/lua/5.1/itorch/main.lua:209: in function </Users/.../install/share/lua/5.1/itorch/main.lua:173>
        .../torch/install/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
        ...rs/.../install/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
        ...rs/.../install/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
        ...rs/.../install/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
        .../torch/install/share/lua/5.1/itorch/main.lua:381: in main chunk
        [C]: in function 'require'
        (command line):1: in main chunk
        [C]: at 0x0109becd10

我不太明白什么是错的,但我仍然不确定火炬数据结构。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

如错误消息所述,预计会3D or 4D (batch mode) tensor。这里mnist提供的数据是28x28(= 2D张量)。

您可以通过更改以下内容添加额外的尺寸:

t.data[i]

使用:

t.data[i]:view(1, 28, 28)