Lua – Torch7 Neural Network Training Error

I am trying to implement a neural network example in torch7. My data is stored in a text file in this form [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
...

There are tags [100 tags] in the last column.

Use this code:

require'nn'
-- ================================ ====== --
-- Start loading data
-- ========================== ============ --
print'[INFO] Loading data..'
dataset = {}
function dataset:size() return 10000 end < br />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)< br />trainer.learningRate = 0.01
trainer:train(dataset)

I received this error message:

# 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/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

Can you help me?

Thank you.

I got this error message […] Can you please help me?

In your data set, the input and output should be Tensor-s (the input here is an ordinary Lua table, this is how you get this The reason for the error, that is, there is no dim method).

In order to simplify data loading, I suggest you use csv parser, for example, you can use csv2tensor to load data into Tensor.

First make sure to add a title (as the first line) in the file, such as:

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

Then load your data as follows:

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

And use this data set for training:

-- ...
trainer:train(dataset)< /pre>

I am trying to implement Here is an example of a neural network. My data is stored in a text file in this form [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
...

There are tags [100 tags] in the last column.

Use this code:

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, X1 8, 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:tr ain(dataset)

I received this error message:

# 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/lzmq/poller.lua:75: in function'poll'
.../yosaikan/torch/install/share/lua/ 5.1/lzmq/i mpl/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

Can you help me?

Thank you.

I got this error message […] Can you please help me?

In your data set, the input and output should be Tensor-s (the input here is an ordinary Lua table, which is why you get this error, that is, there is no dim method).< /p>

In order to simplify data loading, I suggest you use csv parser, for example, you can use csv2tensor to load data into Tensor.

First make sure to add a header (as the first line) in the file ), such as:

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

Then load your data as follows:

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

And use this data set for training:

-- ...
trainer:train(dataset)

Leave a Comment

Your email address will not be published.