火炬7神经网络训练错误

时间:2015-03-25 06:12:18

标签: lua neural-network torch

我正在尝试在torch7中实现神经网络示例。我的数据存储在这种形式的文本文件中[19 cols x 10000 rows]:

11 38 20 44 11 38 21 44 29 42 30 44 34 38  6 34 45 42 1
11 38 20 44 11 38 27 44 31 42 18 44 34 38  6 34 45 42 2
6  42 20 44 11 38 21 44 29 42 30 44 34 38  6 34 45 42 3
...
34 40 20 44 11 38 21 44 29 38 30 38 34 45 38  0  0  0 100
...

标签位于最后一栏[100个标签]。

使用此代码:

require 'nn'
-- ======================================= --
--           Start loading data   
-- ======================================= --
print '[INFO] Loading data..'
dataset = {}
function dataset:size() return 10000 end 
local lin = 1

train_file = 'train_10000.t7'
local file = io.open(train_file)
if file then
    for line in file:lines() do
            local input = torch.Tensor(18);
            local output = torch.Tensor(1);

        local X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18, Y = unpack(line:split(" "))                

        input  = {X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18}
        output = Y

        dataset[lin] = {input, output}        
        lin = lin +1        
    end
end
-- ======================================= --
--                 Create NN   
-- ======================================= --
print '[INFO] Creating NN..'
mlp = nn.Sequential();  -- make a multi-layer perceptron
inputs = 18; outputs = 1; HUs = 25; -- parameters
mlp:add(nn.Linear(inputs, HUs))
mlp:add(nn.Tanh())
mlp:add(nn.Linear(HUs, outputs))
-- ======================================= --
--           MSE and Training  
-- ======================================= --
print '[INFO] MSE and train NN..'
criterion = nn.MSECriterion()  
trainer = nn.StochasticGradient(mlp, criterion)
trainer.learningRate = 0.01
trainer:train(dataset)   

我收到此错误消息:

# StochasticGradient: training  
/home/yosaikan/torch/install/share/lua/5.1/nn/Linear.lua:34: attempt to call method 'dim' (a nil value)
stack traceback:
    /home/yosaikan/torch/install/share/lua/5.1/nn/Linear.lua:34: in function 'updateOutput'
    ...e/yosaikan/torch/install/share/lua/5.1/nn/Sequential.lua:25: in function 'forward'
    ...an/torch/install/share/lua/5.1/nn/StochasticGradient.lua:35: in function 'train'
    iparseSchemeConversion.lua:45: in main chunk
    [C]: in function 'f'
    [string "local f = function() return dofile 'iparseSch..."]:1: in main chunk
    [C]: in function 'xpcall'
    /home/yosaikan/torch/install/share/lua/5.1/itorch/main.lua:174: in function </home/yosaikan/torch/install/share/lua/5.1/itorch/main.lua:140>
    /home/yosaikan/torch/install/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
    .../yosaikan/torch/install/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
    .../yosaikan/torch/install/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
    .../yosaikan/torch/install/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
    /home/yosaikan/torch/install/share/lua/5.1/itorch/main.lua:341: in main chunk
    [C]: in function 'require'
    (command line):1: in main chunk
    [C]: at 0x00405980

你能帮帮我吗?

谢谢。

1 个答案:

答案 0 :(得分:5)

  

我收到此错误消息[...]你能帮助我吗?

在您的数据集中inputoutput应为Tensor - s(此处input是一个简单的Lua表,这就是您获得此错误的原因,即没有dim方法)。

为简化数据加载,我建议您使用csv parser,例如,您可以使用csv2tensor将数据加载到Tensor

首先确保在文件中添加标题(作为第一行),如:

x001,x002,x003,x004,x005,x006,x007,x008,x009,x010,x011,x012,x013,x014,x015,x016,x017,x018,label

然后按如下方式加载您的数据:

local csv2tensor = require 'csv2tensor'

local inputs = csv2tensor.load("data.csv", {exclude={"label"}})
local labels = csv2tensor.load("data.csv", {include={"label"}})

local dataset = {}

for i=1,inputs:size(1) do
  dataset[i] = {inputs[i], torch.Tensor{labels[i]}}
end

dataset.size = function(self)
  return inputs:size(1)
end

并使用此数据集进行培训:

-- ...
trainer:train(dataset)