I'm trying to run the torch tutorial for the mnist dataset and don't quite understand the error. Here is my code (pretty much just the tutorial with mnist and added padding to correct for 28!=32):
load data
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
normalisation
mean = {}
stdv = {}
mean = trainset.data[{ {}, {}, {}}]:mean()
trainset.data[{ {}, {}, {} }]:add(-mean)
stdv = trainset.data[{ {}, {}, {} }]:std()
trainset.data[{ {}, {}, {} }]:div(stdv)
define network
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
training
criterion = nn.ClassNLLCriterion()
trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.001
trainer.maxIteration = 5 -- just do 5 epochs of training.
trainer:train(trainset)
and now i get the following error msg.
# 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
I don't quite understand whats wrong, but I am also still not sure about the torch data structures.
Thanks for your help.