def forward(self, prob, target):
"""
Args:
prob: (N, C)
target : (N, )
"""
N = target.size(0)
C = prob.size(1)
weight = Variable(self.weight).view((1, -1))
weight = weight.expand(N, C) # (N, C)
if prob.is_cuda:
weight = weight.cuda()
prob = weight * prob
one_hot = torch.zeros((N, C))
if prob.is_cuda:
one_hot = one_hot.cuda()
one_hot.scatter_(1, target.data.view((-1,1)), 1)
one_hot = one_hot.type(torch.ByteTensor)
one_hot = Variable(one_hot)
if prob.is_cuda:
one_hot = one_hot.cuda()
loss = torch.masked_select(prob, one_hot)
return -torch.sum(loss)
评论列表
文章目录