2017-11-04 2 views
0

나는 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)

내가 알고 다음과 같이

enter image description here

내가 손실을 구현했습니다 : 그것은 다음과 같이 표시됩니다 torch.lt()은 ByteTensor를 반환하지만, torch.Tensor.float()으로 FloatTensor에 강제 변환하려고하면 AttributeError: module 'torch.autograd.variable' has no attribute 'FloatTensor'이됩니다.

여기에서 어디로 가야할지 모르겠습니다. 그것은 코사인 유사도 텐서와 덜 룰을 기반으로 한 0 또는 1의 텐서를 요소 적으로 곱하는 것입니다.

답변

1

어쩌면 변수에 대해 float() 메서드를 직접 사용해 볼 수 있습니까? 변수 (torch.zeros (5)). float() - 나를 위해 작동합니다 (예 :

).
관련 문제