火炬mnist简单 [英] Torch mnist simple

查看:75
本文介绍了火炬mnist简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为mnist数据集运行割炬教程,但不太了解该错误.这是我的代码(几乎是带有mnist的教程,并添加了填充以更正28!= 32):

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

加载数据

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)

现在我得到以下错误消息.

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

我不太了解发生了什么问题,但是我仍然不确定Torch的数据结构.

I don't quite understand whats wrong, but I am also still not sure about the torch data structures.

感谢您的帮助.

推荐答案

如错误消息所述,期望为3D or 4D (batch mode) tensor.在这里, mnist 提供的数据为28x28(= 2D张量).

As stated by the error message, a 3D or 4D (batch mode) tensor is expected. Here the data provided by mnist is 28x28 (= 2D tensor).

您可以通过以下方式添加额外的尺寸:

You can add an extra dimension by changing:

t.data[i]

使用:

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

这篇关于火炬mnist简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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