I noticed that L1HingeEmbeddingCriterion does not work with CudaTensor, in the criterion:backward part. Here is the example:
-- create model with two parallel nets
mlp = nn.ParallelTable()
mlp:add(nn.Linear(10,2))
mlp:add(nn.Linear(10,2))
mlp = mlp:cuda()
-- create two inputs
A1 = torch.CudaTensor(10)
A2 = torch.CudaTensor(10)
-- create output
pred = mlp:forward({A1, A2})
-- gives a table with two outputs
--th> pred
--{
-- 1 : CudaTensor - size: 2
-- 2 : CudaTensor - size: 2
--}
-- create L1 hinge embedding criterion
criterionMain = nn.L1HingeEmbeddingCriterion(1)
criterionMain = criterionMain:cuda()
-- create same/not same information
SameNotSame = torch.CudaTensor(2)
SameNotSame[1] = 1
SameNotSame[2] = -1
-- compute error between two outputs - work ok
err = criterionMain:forward({pred[1], pred[2]}, SameNotSame[1])
-- now try to compute the gradient of the criterion with respect to input and get an error
gradCriterion = criterionMain:backward({pred[1], pred[2]}, SameNotSame[1])
...h/install/share/lua/5.1/nn/L1HingeEmbeddingCriterion.lua:26: bad argument #1 to 'resizeAs' (torch.FloatTensor expected, got torch.CudaTensor)
stack traceback:
[C]: in function 'resizeAs'
...h/install/share/lua/5.1/nn/L1HingeEmbeddingCriterion.lua:26: in function 'backward'
[string "gradCriterion = criterionMain:backward({pred[..."]:1: in main chunk
[C]: in function 'xpcall'
/opt/torch/install/share/lua/5.1/trepl/init.lua:598: in function 'repl'
/opt/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk
[C]: at 0x00406170
So the only way to get it work is to cast pred[1], pred[2] back from 'cuda' to regular float. I couldn't do it for the whole table, so I did this and it worked:
G1 = pred[1]:float()
G2 = pred[2]:float()
gradCriterion = criterionMain:backward({G1, G2}, SameNotSame[1])
I am not sure, maybe I missed something and have made false conclusions, but at least this is something to be aware of. Any ideas are welcome.