--------------------------------------------------------------------------- AttributeError Traceback (most re…
- User
AttributeError Traceback (most recent call last) Cell In[7], line 26 17 negative_style = model(false_embeddings, false_masks) 19 #combined = torch.cat((anchor_style, positive_style, negative_style), dim=0) 20 #print(combined.shape) 21 (...) 24 25 # loss = (loss1 + loss2) / 2 ---> 26 loss1 = criterion(anchor_style, positive_style, negative_style) 27 loss2 = criterion2(anchor_style, positive_style, negative_style) 29 loss = (loss1 + loss2) / 2
File c:\Users\slons\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py:1518, in Module._wrapped_call_impl(self, *args, **kwargs) 1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc] 1517 else: -> 1518 return self._call_impl(*args, **kwargs)
File c:\Users\slons\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py:1527, in Module._call_impl(self, *args, **kwargs) 1522 # If we don't have any hooks, we want to skip the rest of the logic in 1523 # this function, and just call forward. 1524 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks 1525 or _global_backward_pre_hooks or _global_backward_hooks 1526 or _global_forward_hooks or _global_forward_pre_hooks): -> 1527 return forward_call(*args, **kwargs) 1529 try: 1530 result = None
Cell In[3], line 39 36 positive_sim = F.cosine_similarity(anchor, positive).mean() 37 negative_sim = F.cosine_similarity(anchor, negative).mean() ---> 39 return self.loss(positive_sim, negative_sim, 1)
File c:\Users\slons\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py:1695, in Module.getattr(self, name) 1693 if name in modules: 1694 return modules[name] -> 1695 raise AttributeError(f"'{type(self).name}' object has no attribute '{name}'")
AttributeError: 'CustomMarginRankingLoss' object has no attribute 'loss'
class CustomMarginRankingLoss(nn.Module): def init(self, margin=0.0): super(CustomMarginRankingLoss, self).init() self.margin = margin self.loss = nn.MarginRankingLoss()
def forward(self, anchor, positive, negative): # Cosine Similarity positive_sim = F.cosine_similarity(anchor, positive).mean() negative_sim = F.cosine_similarity(anchor, negative).mean() return self.loss(positive_sim, negative_sim, 1) - Assistant
The error message you're encountering indicates that the
CustomMarginRankingLossobject does not have an attribute named 'loss'. This issue is likely due to an incorrect implementation of the__init__method of yourCustomMarginRankingLossclass.In Python, the
__init__method should be named with double underscores before and after (__init__), but in your code, it is written asinitwithout the leading and trailing double underscores.Here's the correct implementation:
import torch import torch.nn as nn import torch.nn.functional as F class CustomMarginRankingLoss(nn.Module): def __init__(self, margin=0.0): super(CustomMarginRankingLoss, self).__init__() self.margin = margin self.loss = nn.MarginRankingLoss(margin=margin) def forward(self, anchor, positive, negative): # Cosine Similarity positive_sim = F.cosine_similarity(anchor, positive).mean() negative_sim = F.cosine_similarity(anchor, negative).mean() # Create a tensor with the same device as the input to match the expected type target = torch.tensor([1.0], device=anchor.device) # Compute the loss return self.loss(positive_sim, negative_sim, target)Make sure you use
__init__instead ofinit. Additionally, thetargetvariable in theforwardmethod should be a tensor with the same device as the input tensors to avoid device mismatch errors.With these changes, your
CustomMarginRankingLossclass should work correctly, and you should be able to call it without encountering theAttributeError.