0

我正在编写一个小的 Torch7/Lua 脚本来创建和训练一个神经网络,但我遇到了错误。有任何想法吗?

这是我的代码:

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)  

这是错误:

# 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

这是我的数据示例:

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

基本上,我的目标是创建一个深度神经网络,将句子中使用的单词的频率联系起来,并将其与用户将其评为“正面”、“负面”或“中性”(我的输出是二进制的)联系起来。也请让我知道我的想法是否正确。

谢谢!

4

1 回答 1

1

发现问题了!

问题是我在创建网络时给出了错误的尺寸。我传入的是“inputs:size(1)”而不是“inputs:size(2)”。这是修复

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

感觉我正在慢慢开始掌握 Lua/Torch 的窍门!分数

于 2015-04-05T16:38:36.680 回答