私はPyTorchのhttp://anthology.aclweb.org/W16-1617に損失関数を実装しようとしています。しかしコントラストコサインロス関数でのByteTensorの使用方法を教えてください。
class CosineContrastiveLoss(nn.Module):
"""
Cosine contrastive loss function.
Based on: http://anthology.aclweb.org/W16-1617
Maintain 0 for match, 1 for not match.
If they match, loss is 1/4(1-cos_sim)^2.
If they don't, it's cos_sim^2 if cos_sim < margin or 0 otherwise.
Margin in the paper is ~0.4.
"""
def __init__(self, margin=0.4):
super(CosineContrastiveLoss, self).__init__()
self.margin = margin
def forward(self, output1, output2, label):
cos_sim = F.cosine_similarity(output1, output2)
loss_cos_con = torch.mean((1-label) * torch.div(torch.pow((1.0-cos_sim), 2), 4) +
(label) * torch.pow(cos_sim * torch.lt(cos_sim, self.margin), 2))
return loss_cos_con
、私はというエラーを取得しています: TypeError: mul received an invalid combination of arguments - got (torch.cuda.ByteTensor), but expected one of: * (float value) didn't match because some of the arguments have invalid types: (torch.cuda.ByteTensor) * (torch.cuda.FloatTensor other) didn't match because some of the arguments have invalid types: (torch.cuda.ByteTensor)
私がいることを知っている次のように
は私が損失を実装しました:これは次のように示されていますtorch.lt()
はByteTensorを返しますが、torch.Tensor.float()
でFloatTensorに強制しようとするとAttributeError: module 'torch.autograd.variable' has no attribute 'FloatTensor'
になります。
ここからどこに行くのか本当に分かりません。これは、コサイン類似度テンソルと小数点以下のルールに基づいて0または1のテンソルとの間で要素的な乗算を行うことは論理的です。