Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua5.2:Torch教程给出了一个基于索引的堆栈溢出_Lua_Stack Overflow_Torch_Metatable - Fatal编程技术网

Lua5.2:Torch教程给出了一个基于索引的堆栈溢出

Lua5.2:Torch教程给出了一个基于索引的堆栈溢出,lua,stack-overflow,torch,metatable,Lua,Stack Overflow,Torch,Metatable,我一直想进入火炬,从这个开始。但是,当使用setmetatable函数运行代码时,我遇到了堆栈溢出。我相信这是因为50000个图像输入文件很大,但我可能错了。我试图编辑luaconf.h文件,试图修复它,但没有效果。除此之外,我正在使用lua5.2运行torch,而不使用iTorch,因为我在设置它时遇到了问题 以下是错误: /home/student/torch/install/bin/lua: C stack overflow stack traceback: [C]: in functio

我一直想进入
火炬
,从这个开始。但是,当使用
setmetatable
函数运行代码时,我遇到了堆栈溢出。我相信这是因为50000个图像输入文件很大,但我可能错了。我试图编辑
luaconf.h
文件,试图修复它,但没有效果。除此之外,我正在使用
lua5.2
运行
torch
,而不使用
iTorch
,因为我在设置它时遇到了问题

以下是错误:

/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 ?
否则,我的代码应该与1教程中的代码相同。加载数据并将其规格化为4。训练神经网络

这是我的代码,很抱歉最初没有

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)

setmetatable中有一个输入错误,t.lable而不是t.label,在这里发布您的代码,我们无法猜测第56行是什么,因此盲猜测是
t.data
t.label
\uu index
调用中是nil,所以它在调用
setmetatable
前后递归,或者他设置了调用\uu newindex的东西,这反过来又建立了索引。