torch / nn

Other
1.33k stars 969 forks source link

Error with CrossEntropyCriterion with cuda tensor #1173

Open Amit12690 opened 7 years ago

Amit12690 commented 7 years ago

CrossEntropyCriterion() seems to be giving problems when using cuda tensor . Same goes with Using 1D float-tensor works fine though. My input and target are : inp = torch.Tensor({5 ,1 ,4, 2, 5}) tgt = torch.Tensor({5 , 5, 2, 2, 3}) I tried reshaping the input to (5,1) , still the error is the same.

The error I get is :

install/share/lua/5.1/nn/THNN.lua:110: bad argument #2 to 'v' (mismatch between the batch size of input (1) and that of target (5) at /mnt/qnap/installed_softwares/torch_common/extra/cunn/lib/THCUNN/generic/ClassNLLCriterion.cu:39) stack traceback: [C]: at 0x7f942e5609e0 [C]: in function 'v' ...softwares/torch_common/install/share/lua/5.1/nn/THNN.lua:110: in function 'ClassNLLCriterion_updateOutput' ...ch_common/install/share/lua/5.1/nn/ClassNLLCriterion.lua:44: in function 'updateOutput' ...ommon/install/share/lua/5.1/nn/CrossEntropyCriterion.lua:20: in function 'forward'

How can I fix this issue ? I am running torch on ubuntu-14.0.4 . The torch commit is 0446dd965454afb6a4e13165fb935cfeb73fd1a0 . I am using cuda-8.0.44 with cudnn_v5.0 .

The reference code is as follows :

require 'cunn'
require 'nn'
require 'cutorch'

inp = torch.Tensor({5 ,1 ,4, 2, 5})
tgt = torch.Tensor({5 , 5, 2, 2, 3})
nll = nn.ClassNLLCriterion()
ce = nn.CrossEntropyCriterion()
nll_c = nll:clone():cuda()
ce_c = ce:clone():cuda()

print("Testing without cuda ... ") 
print("ClassNLL: ", nll:forward(inp,tgt))
print("CrossEntropy: ", ce:forward(inp,tgt))
print("Testing with cuda ... ") 
print("CrossEntropy: ", ce_c:forward(inp:cuda(),tgt:cuda()))
iamalbert commented 7 years ago

If you are using mini-batch, the size of input should be Nx5 and the size of target should be Nx1. Otherwise, the size of input and target should be 5 and 1, respectively