2016-05-22 4 views
1
require 'torch'; 
require 'nn'; 
require 'nnx'; 
mnist = require 'mnist'; 

fullset = mnist.traindataset() 
testset = mnist.testdataset() 
trainset = { 
    size = 50000, 
    data = fullset.data[{{1,50000}}]:double(), 
    label = fullset.label[{{1,50000}}] 
} 
validationset = { 
    size = 10000, 
    data = fullset.data[{{50001, 60000}}]:double(), 
    label = fullset.label[{{50001,60000}}] 
} 
-- MNIST Dataset has 28x28 images 
model = nn.Sequential() 

model:add(nn.SpatialConvolutionMM(1, 32, 5, 5))   -- 32x24x24 
model:add(nn.ReLU()) 
model:add(nn.SpatialMaxPooling(3, 3, 3, 3))    -- 32x8x8 

model:add(nn.SpatialConvolutionMM(32, 64, 5, 5))  -- 64x4x4 
model:add(nn.Tanh()) 
model:add(nn.SpatialMaxPooling(2, 2, 2, 2))    -- 64x2x2 
model:add(nn.Reshape(64*2*2)) 
model:add(nn.Linear(64*2*2, 200)) 
model:add(nn.Tanh()) 
model:add(nn.Linear(200, 10)) 

model:add(nn.LogSoftMax()) 

criterion = nn.ClassNLLCriterion() 

x, dldx = model:getParameters()   -- now x stores the trainable parameters and dldx stores the gradient wrt these params in the model above 

sgd_params = { 
    learningRate = 1e-2, 
    learningRateDecay = 1e-4, 
    weightDecay = 1e-3, 
    momentum = 1e-4 
} 

step = function (batchsize) 

    -- setting up variables 
    local count = 0 
    local current_loss = 0 
    local shuffle = torch.randperm(trainset.size) 

    -- setting default batchsize as 200 
    batchsize = batchsize or 200 

    -- setting inputs and targets for minibatches 
    for minibatch_number = 1, trainset.size, batchsize do 

     local size = math.min(trainset.size - minibatch_number + 1, batchsize) 
     local inputs = torch.Tensor(size, 28, 28) 
     local targets = torch.Tensor(size) 

     for index = 1, size do 
      inputs[index] = trainset.data[ shuffle[ index + minibatch_number ]] 
      targets[index] = trainset.label[ shuffle[ index + minibatch_number ] ] 
     end 

     -- defining feval function to return loss and gradients of loss w.r.t. params 
     feval = function(x_new) 
     --print ("---------------------------------safe--------------------") 

      if x ~= x_new then x:copy(x_new) end 

      -- initializing gradParsams to zero 
      dldx:zero() 

      -- calculating loss and param gradients 
      local loss = criterion:forward(model.forward(inputs), targets) 
      model:backward(inputs, criterion:backward(model.output, targets)) 

      return loss, dldx 
     end 

     -- getting loss 
     -- optim returns x*, {fx} where x* is new set of params and {fx} is { loss } => fs[ 1 ] carries loss from feval 

     print(feval ~= nil and x ~= nil and sgd_params ~= nil) 
     _,fs = optim.sgd(feval, x, sgd_params) 

     count = count + 1 
     current_loss = current_loss + fs[ 1 ] 
    end 

    --returning avg loss over the minibatch 
    return current_loss/count   

end 

max_iters = 30 

for i = 1 ,max_iters do 
    local loss = step() 
    print(string.format('Epoch: %d Current loss: %4f', i, loss)) 
end 

私はtorchとluaの新機能です。上記のコードではエラーが見つかりません。誰もそれをデバッグする方法を提案することはできますか?グローバル 'optim'(ゼロ値)のインデックスを作成しようとしました

エラー:

/home/afroz/torch/install/bin/luajit: /home/afroz/test.lua:88: attempt to index global 'optim' (a nil value) 
stack traceback: 
    /home/afroz/test.lua:88: in function 'step' 
    /home/afroz/test.lua:102: in main chunk 
    [C]: in function 'dofile' 
    ...froz/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk 
    [C]: at 0x00406670 
+0

?つまり、 'optim is is nil value'で、table/usedataではなく、' optim.sgd'のように使うことはできません。 – moteus

答えて

1

optimは、スクリプト内の任意の場所に割り当てられているので、スクリプトはoptim.sgdを参照するとき、その値はnilであり、あなたがあなたが示すエラーを取得していません。スクリプトをダブルチェックして、optimに正しい値が割り当てられていることを確認する必要があります。

2

optimは、スクリプトの範囲で定義されていません。あなたはoptim.sgdを呼び出すことを試みますが、あなたが見るエラーが発生します。

nnに似ていますが、optimはトーチの拡張パッケージです。

require 'torch'; 
require 'nn'; 
require 'nnx'; 

スクリプトの冒頭にこれらの行がありますか?彼らは基本的にそれらのパッケージの定義を実行します。 optimがインストールされていることを確認してから、それを要求してください。あなたはインデックスグローバル 'OPTIM' に `の試み(ゼロ値)`に理解していない何

https://github.com/torch/optim

関連する問題