some criterions not working with CudaTensor when doing criterion:backward

774 views
Skip to first unread message

Alexey Chernyavskiy

unread,
Apr 24, 2015, 6:45:14 AM4/24/15
to tor...@googlegroups.com
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.

Alexey Chernyavskiy

unread,
Apr 24, 2015, 7:01:32 AM4/24/15
to tor...@googlegroups.com
A quick follow-up:

I think I found the reason of this behavior. When L1HingeEmbeddingCriterion is transformed into cuda, its interior parameters do not change type.
Example: first with MSECriterion

th> criterion = nn.MSECriterion()
th> criterion.gradInput
[torch.FloatTensor with no dimension]

th> criterion = criterion:cuda()
th> criterion.gradInput
[torch.CudaTensor with no dimension]

So, the gradInput field of the criterion changes its type.
Now with L1:

th> criterion = nn.L1HingeEmbeddingCriterion()
th> criterion.gradInput
{
  1 : FloatTensor - empty
  2 : FloatTensor - empty
}
th> criterion = criterion:cuda()
th> criterion.gradInput
{
  1 : FloatTensor - empty
  2 : FloatTensor - empty
}

So no type change occurs.
I don't know if it's the only criterion that is currently implemented in Torch7 that has this behaviour or not.

Lei Yu

unread,
Apr 26, 2015, 11:30:02 AM4/26/15
to tor...@googlegroups.com
I have the same problem with MarginRankingCriterion.

Lei

Alexey Chernyavskiy

unread,
Apr 27, 2015, 3:18:13 AM4/27/15
to tor...@googlegroups.com
Bug in L1HingeEmbeddingCriterion has been fixed yesterday by Soumith.
Great thanks!

MarginRankingCriterion remains.

Alexey Chernyavskiy

unread,
Apr 27, 2015, 2:55:51 PM4/27/15
to tor...@googlegroups.com
It got fixed a couple of hours ago.
Reply all
Reply to author
Forward
0 new messages