2016-05-15 6 views
2

Ich versuche, das Fackel-Tutorial für den Mnist-Datensatz auszuführen und verstehe den Fehler nicht ganz. Hier ist mein Code (so ziemlich genau das Tutorial mit mnist und hinzugefügt Polsterung für 28 zu korrigieren = 32!):Fackel mnist einfach

Lastdaten

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 

Normalisierung

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

definieren Netzwerk

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 

Schulung

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

und jetzt bekomme ich die folgende Fehlermeldung 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 

Ich verstehe nicht ganz, was ist falsch, aber ich bin auch noch nicht sicher über die Brennerdatenstrukturen.

Danke für Ihre Hilfe.

Antwort

2

Wie in der Fehlermeldung angegeben, wird ein 3D or 4D (batch mode) tensor erwartet. Hier sind die Daten von mnist 28x28 (= 2D Tensor).

Sie können eine zusätzliche Dimension, indem hinzufügen:

t.data[i] 

mit:

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

Dank! Ich änderte es jetzt mit trainset.data = trainset.data:view(60000,1,28,28), aber jetzt bekomme ich die folgenden errormsg. lua/5.1/nn/THNN.lua: 109: Eingangstensor sollte 1D oder 2D sein bei ... THNN/generic/ClassNLLCriterion.c: 21 Was soll ich tun? Nochmals vielen Dank – maggu

+0

Cut direkt vor dem Verlust (Klasse NLL) und untersuchen, was die Dimension des Tensors ist, die Sie nach einem Forward bekommen. Wie gesagt, sollte es 1D oder 2D (Batch-Modus) sein. Überprüfen Sie jede Schicht Ihres Netzwerks, um das Problem zu erkennen. – deltheil

+0

@maggu könnten Sie das Problem lösen? Wenn ja, könnten Sie uns etwas mitteilen? – zwlayer

Verwandte Themen