2017-03-14 3 views
0

Je voulais entrer dans torch et j'ai commencé avec ce tutorial. Cependant, j'ai couru dans un débordement de pile lors de l'exécution du code spécifiquement avec la fonction setmetatable. Je crois que cela se produit en raison du grand fichier d'entrée d'image 50000 mais je peux me tromper. J'ai essayé d'éditer le fichier luaconf.h pour essayer de le réparer en vain. A part ça, je cours torch avec Lua 5.2 et sans iTorch car j'ai eu du mal à le configurer.Lua5.2: Tutoriel Torch donne un débordement de pile basé sur __index

Voici l'erreur:

/home/student/torch/install/bin/lua: C stack overflow 
stack traceback: 
[C]: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
... 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:56: in function '__index' 
Documents/TorchImageRecognition.lua:66: in main chunk 
[C]: in function 'dofile' 
...dent/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:150: in main chunk 
[C]: in ? 

Sinon mon code devrait être le même que dans le tutoriel de 1. Charger et normaliser les données à 4. Former le réseau de neurones

Voici mon code, désolé de ne pas l'avoir d'abord.

require 'torch' 
require 'nn' 
require 'paths' 

if (not paths.filep("cifar10torchsmall.zip")) then 
    os.execute('wget -c https://s3.amazonaws.com/torch7/data/cifar10torchsmall.zip') 
    os.execute('unzip cifar10torchsmall.zip') 
end 
trainset = torch.load('cifar10-train.t7') 
testset = torch.load('cifar10-test.t7') 
classes = {'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'} 

print(trainset) 
print(#trainset.data) 

--itorch.image(trainset.data[100]) 
--print(classes[trainset.label[100]]) 
-- -- -- -- -- -- -- -- -- -- -- 
-- This code is from the previous parts of the tutorial 
--net = nn.Sequential() 
--net:add(nn.SpatialConvolution(1, 6, 5, 5)) 
--neecognitiont:add(nn.ReLU()) 
--net:add(nn.SpatialMaxPooling(2, 2, 2, 2)) 
--net:add(nn.SpatialConvolution(6, 16, 5, 5)) 
--net:add(nn.ReLU()) 
--net:add(nn.SpatialMaxPooling(2, 2, 2, 2)) 
--net:add(nn.View(16*5*5)) 
--net:add(nn.Linear(16*5*5, 120)) 
--net:add(nn.ReLU()) 
--net:add(nn.Linear(120, 84)) 
--net:add(nn.ReLU()) 
--net:add(nn.Linear(84, 10)) 
--net:add(nn.LogSoftMax()) 

--print('Lenet5\n' .. net:__tostring()) 

--input = torch.rand(1, 32, 32) 
--output = net:forward(input) 
--print(output) 
--net:zeroGradParameters() 
--gradInput = net:backward(input, torch.rand(10)) 
--print(#gradInput) 

--criterion = nn.ClassNLLCriterion() 
--criterion:forward(output, 3) 
--gradients = criterion:backward(output, 3) 

--gradInput = net:backward(input, gradients) 

--m= nn.SpatialConvolution(1, 3, 2, 2) 
--print(m.weight) 
--print(m.bias) 
-- -- -- -- -- -- -- -- -- 

setmetatable(trainset, {__index = function(t, i) 
     return {t.data[i], t.lable[i]} 
end}) 
trainset.data = trainset.data:double() 

function trainset:size() 
    return self.data:size(1) 
end 

print(trainset:size()) 

print(trainset[33]) 

redChannel = trainset.data[{ {}, {1}, {}, {} }] 
print(#redChannel) 

mean = {} 
stdv = {} 
for i=1,3 do 
    mean[i] = trainset.data[{ {}, {i}, {}, {} }]:mean() 
    print('Channel ' .. i .. ', Mean: ' .. mean[i]) 
    trainset.data[{ {}, {i}, {}, {} }]:add(-mean[i]) 

    stdv[i] = trainset.data[{ {}, {i}, {}, {} }]:std() 
    print('Channel ' .. i .. ', Standard Deviation: ' .. stdv[i]) 
    trainset.data[{ {}, {i}, {}, {} }]:div(stdv[i]) 
end 

net = nn.Sequential() 
net:add(nn.SpatialConvolution(3, 6, 5, 5)) 
net:add(nn.ReLU()) 
net:add(nn.SpatialMaxPooling(2, 2, 2, 2)) 
net:add(nn.SpatialConvolution(6, 16, 5, 5)) 
net:add(nn.ReLU()) 
net:add(nn.SpatialMaxPooling(2, 2, 2, 2)) 
net:add(nn.View(16*5*5)) 
net:add(nn.Linear(16*5*5, 120)) 
net:add(nn.ReLU()) 
net:add(nn.Linear(120, 84)) 
net:add(nn.ReLU()) 
net:add(nn.Linear(84, 10)) 
net:add(nn.LogSoftMax()) 

criterion = nn.ClassNLLCriterion() 

trainer = nn.StochasticGradient(net, criterion) 
trainer.learningRate = 0.001 
trainer.maxIteration = 5 
trainer:train(trainset) 
+0

votre code postal ici, nous ne pouvons pas deviner ce qui est la ligne 56 y – user3125367

+0

estimation aveugle est que 't.data' ou 't.label' est nul dans' __index' autour de l'appel 'setmetatable', donc il récursive pour toujours – user3125367

+0

Ou il définit quelque chose qui appelle __newindex, qui à son tour indexe. – warspyking

Répondre

1

Il y a une faute de frappe dans SetMetaTable, t.lable au lieu de t.label