machine learning - Torch CrossEntropyCriterion error -
i'm trying train simple test network on xor function in torch. works when use msecriterion, when try crossentropycriterion fails following error message:
/home/a/torch/install/bin/luajit: /home/a/torch/install/share/lua/5.1/nn/thnn.lua:699: assertion `cur_target >= 0 && cur_target < n_classes' failed. @ /tmp/luarocks_nn-scm-1-6937/nn/lib/thnn/generic/classnllcriterion.c:31 stack traceback: [c]: in function 'v' /home/a/torch/install/share/lua/5.1/nn/thnn.lua:699: in function 'classnllcriterion_updateoutput' ...e/a/torch/install/share/lua/5.1/nn/classnllcriterion.lua:41: in function 'updateoutput' ...torch/install/share/lua/5.1/nn/crossentropycriterion.lua:13: in function 'forward' .../a/torch/install/share/lua/5.1/nn/stochasticgradient.lua:35: in function 'train' a.lua:34: in main chunk [c]: in function 'dofile' /home/a/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk [c]: @ 0x00406670
i same error message when decomposing logsoftmax , classnllcriterion. code is:
dataset={}; function dataset:size() return 100 end -- 100 examples i=1,dataset:size() local input = torch.randn(2); -- distributed example in 2d local output = torch.tensor(2); if input[1]<0 input[1]=-1 else input[1]=1 end if input[2]<0 input[2]=-1 else input[2]=1 end if input[1]*input[2]>0 -- calculate label xor function output[2] = 1; else output[1] = 1 end dataset[i] = {input, output} end require "nn" mlp = nn.sequential(); -- make multi-layer perceptron inputs = 2; outputs = 2; hus = 20; -- parameters mlp:add(nn.linear(inputs, hus)) mlp:add(nn.tanh()) mlp:add(nn.linear(hus, outputs)) criterion = nn.crossentropycriterion() trainer = nn.stochasticgradient(mlp, criterion) trainer.learningrate = 0.01 trainer:train(dataset) x = torch.tensor(2) x[1] = 1; x[2] = 1; print(mlp:forward(x)) x[1] = 1; x[2] = -1; print(mlp:forward(x)) x[1] = -1; x[2] = 1; print(mlp:forward(x)) x[1] = -1; x[2] = -1; print(mlp:forward(x))
mse criterion designed regression problems. when it's used classification tasks, targets should one-hot vectors. cross entropy / negative log likelihood criteria used exclusively classification; therefore, there's no need explicitly represent target class vector. in torch
target such criteria index of assigned class (1 number of classes).
Comments
Post a Comment