Size mismatch in function addmv in Torch7

2019-09-04 16:44发布

问题:

I'm working on a small Torch7/ Lua script to create and train a neural network, but I'm running into errors. Any ideas?

Here's my code:

require 'dp'
require 'csvigo'
require 'nn'
--[[hyperparameters]]--
opt = {
    nHidden = 100, --number of hidden units
    learningRate = 0.1, --training learning rate
    momentum = 0.9, --momentum factor to use for training
    maxOutNorm = 1, --maximum norm allowed for output neuron weights
    batchSize = 128, --number of examples per mini-batch
    maxTries = 100, --maximum number of epochs without reduction in validation error.
    maxEpoch = 1 --maximum number of epochs of training
}

csv2tensor = require 'csv2tensor'
-- inputs, outputs = csv2tensor.load("/Users/robertgrzesik/NodeJS/csv_export.csv")
inputs = csv2tensor.load("/Users/robertgrzesik/NodeJS/csv_export.csv", {exclude={"positive", "negative", "neutral"}})
outputs = csv2tensor.load("/Users/robertgrzesik/NodeJS/csv_export.csv", {include={"positive", "negative", "neutral"}}) -- "positive", "negative", "neutral"
print("outputs: ", outputs)
print("inputs: ", inputs)

local dataset = {}

print("inputs:size(1)", inputs:size(1))

inputSize = inputs:size(1)
outputSize = outputs:size(1)

for i=1,inputSize do
  dataset[i] = {inputs[i], outputs[i]}
end

dataset.size = function(self)
  return inputSize
end

-- ======================================= --
--                 Create NN   
-- ======================================= --
print '[INFO] Creating NN..'
mlp = nn.Sequential();  -- make a multi-layer perceptron
inputs = inputSize; outputs = outputSize; HUs = 300; -- 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)  

Here's the error:

# StochasticGradient: training  
/Users/robertgrzesik/torch/install/bin/luajit: .../robertgrzesik/torch/install/share/lua/5.1/nn/Linear.lua:37: size mismatch
stack traceback:
    [C]: in function 'addmv'
    .../robertgrzesik/torch/install/share/lua/5.1/nn/Linear.lua:37: in function 'updateOutput'
    ...ertgrzesik/torch/install/share/lua/5.1/nn/Sequential.lua:25: in function 'forward'
    ...ik/torch/install/share/lua/5.1/nn/StochasticGradient.lua:35: in function 'train'
    /Users/robertgrzesik/Lua/async-master/tests/dp-test.lua:53: in main chunk
    [C]: in function 'dofile'
    ...esik/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:131: in main chunk
    [C]: at 0x01028bc780

And here's a sample of my data:

positive,negative,basketball,neutral,the,be,and,of,a,in,to,have,it,I,for,that,he,you,with,on,do,this,they,at,who,if,her,people,take,your,like,our,new,because,woman,great,show,million,money,job,little,important,lose,include,rest,fight,perfect
0,0,0,1,3,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Basically my aim is to create a deep neural network linking the frequency of words used in a sentence and tie it to the user rating it as either "positive", "negative" or "neutral" (my outputs, which are binary). Please also let me know if my thinking is correct on this.

Thank you!

回答1:

Found the problem!

The issue was that I was giving the wrong sizes when creating the network. I was passing in "inputs:size(1)" instead of "inputs:size(2)". Here's the fix

mlp:add(nn.Linear(inputs:size(2), HUs))
mlp:add(nn.Tanh())
mlp:add(nn.Linear(HUs, outputs:size(2)))

Feel like I'm slowly starting to get the hang of Lua/ Torch! Score



标签: lua torch