當前位置: 首頁>>代碼示例>>Python>>正文


Python nn.MarginRankingLoss方法代碼示例

本文整理匯總了Python中torch.nn.MarginRankingLoss方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.MarginRankingLoss方法的具體用法?Python nn.MarginRankingLoss怎麽用?Python nn.MarginRankingLoss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在torch.nn的用法示例。


在下文中一共展示了nn.MarginRankingLoss方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, 
                 margin    : float = 1.0, 
                 reduction : str = None):
        r"""Initialize TripletLoss
        
        Args:
            margin (float, optional): size of margin. Defaults to 1.0.
            reduction (str, optional): method of reduction. Defaults to None.
        """
        # Refer to parent class
        super(TripletLoss, self).__init__()

        # Initialize module with input margin
        if margin:
            self.parser = margin_ranking_loss_parser
            self.loss = nn.MarginRankingLoss(margin=margin, reduction=reduction)
        else:
            self.parser = soft_margin_loss_parser
            self.loss = nn.SoftMarginLoss(reduction=reduction) 
開發者ID:p768lwy3,項目名稱:torecsys,代碼行數:21,代碼來源:pairwise_ranking_loss.py

示例2: mse_loss_plus_rank_loss

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def mse_loss_plus_rank_loss(output,target):

    cost = output
    target_cost = target

    if output.size()[0] > 1:
        inter = output[:-1]
        inter_1 = output[1:]
    else: #emulate no rank loss
        inter = torch.ones(1)
        inter_1 = 2 * torch.ones(1)

    target_rank = torch.ones(inter.size())

    loss_mse = nn.MSELoss(reduce = False)
    loss1 = torch.sqrt(loss_mse(cost, target_cost)) / (target_cost + 1e-3)
    loss1 = torch.mean(loss1)

    loss_rank = nn.MarginRankingLoss()
    loss2 = loss_rank(inter_1, inter, target_rank)

    return [loss1, loss2] 
開發者ID:ithemal,項目名稱:Ithemal,代碼行數:24,代碼來源:losses.py

示例3: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, args, margin=None, name=None, tri_sampler_type='CTL'):
        self.margin = margin
        self.args = args
        self.name = name
        self.tri_sampler_type = tri_sampler_type
        if margin is not None:
            if self.tri_sampler_type == 'CTL':
                self.ranking_loss = nn.MarginRankingLoss(margin=self.margin)
            elif self.tri_sampler_type == 'RTL':
                self.ranking_loss = SoftMarginTriplet(margin=self.margin)
            elif self.tri_sampler_type == 'CTL_RTL':
                if '_CTL' in name:
                    self.ranking_loss = nn.MarginRankingLoss(margin=self.margin)
                if '_RTL' in name:
                    self.ranking_loss = SoftMarginTriplet(margin=self.margin)
        else:
            self.ranking_loss = nn.SoftMarginLoss() 
開發者ID:zhangxinyu-xyz,項目名稱:PAST-ReID,代碼行數:19,代碼來源:triplet_loss.py

示例4: github_ucir_ranking_mr

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def github_ucir_ranking_mr(logits, targets, n_classes, task_size, nb_negatives=2, margin=0.2):
    gt_index = torch.zeros(logits.size()).to(logits.device)
    gt_index = gt_index.scatter(1, targets.view(-1, 1), 1).ge(0.5)
    gt_scores = logits.masked_select(gt_index)
    #get top-K scores on novel classes
    num_old_classes = logits.shape[1] - task_size
    max_novel_scores = logits[:, num_old_classes:].topk(nb_negatives, dim=1)[0]
    #the index of hard samples, i.e., samples of old classes
    hard_index = targets.lt(num_old_classes)
    hard_num = torch.nonzero(hard_index).size(0)
    #print("hard examples size: ", hard_num)
    if hard_num > 0:
        gt_scores = gt_scores[hard_index].view(-1, 1).repeat(1, nb_negatives)
        max_novel_scores = max_novel_scores[hard_index]
        assert (gt_scores.size() == max_novel_scores.size())
        assert (gt_scores.size(0) == hard_num)
        #print("hard example gt scores: ", gt_scores.size(), gt_scores)
        #print("hard example max novel scores: ", max_novel_scores.size(), max_novel_scores)
        loss = nn.MarginRankingLoss(margin=margin)(gt_scores.view(-1, 1), \
            max_novel_scores.view(-1, 1), torch.ones(hard_num*nb_negatives).to(logits.device))
        return loss
    return torch.tensor(0).float() 
開發者ID:arthurdouillard,項目名稱:incremental_learning.pytorch,代碼行數:24,代碼來源:base.py

示例5: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, margin=0.3, distance='euclidean', use_gpu=True):
        super(HeterogeneousTripletLoss, self).__init__()
        if distance not in ['euclidean', 'consine']:
            raise KeyError("Unsupported distance: {}".format(distance))
        self.distance = distance
        self.margin = margin
        self.use_gpu = use_gpu
        self.ranking_loss = nn.MarginRankingLoss(margin=margin) 
開發者ID:guxinqian,項目名稱:TKP,代碼行數:10,代碼來源:losses.py

示例6: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, margin=0.0):
        nn.Module.__init__(self)
        self.m = nn.MarginRankingLoss(margin=margin) 
開發者ID:THUDM,項目名稱:ScenarioMeta,代碼行數:5,代碼來源:loss.py

示例7: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, device, margin=None):
    self.margin = margin
    self.device = device
    if margin is not None:
      self.ranking_loss = nn.MarginRankingLoss(margin=margin)
    else:
      self.ranking_loss = nn.SoftMarginLoss() 
開發者ID:hwang1996,項目名稱:ACME,代碼行數:9,代碼來源:triplet_loss.py

示例8: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, margin=1):
        super(RawTripletLoss, self).__init__()
        self.margin = margin
        self.ranking_loss = nn.MarginRankingLoss(margin=margin) 
開發者ID:yolomax,項目名稱:person-reid-lib,代碼行數:6,代碼來源:triplet.py

示例9: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, num_classes, args, use_gpu=True):
        super(TripletLoss, self).__init__()
        margin = args['margin']
        self.margin = margin
        self.ranking_loss = nn.MarginRankingLoss(margin=margin)

        from .cross_entropy_loss import CrossEntropyLoss
        self.xent = CrossEntropyLoss(num_classes=num_classes, use_gpu=use_gpu, label_smooth=args['label_smooth'])
        self.lambda_xent = args['lambda_xent']
        self.lambda_htri = args['lambda_htri'] 
開發者ID:TAMU-VITA,項目名稱:ABD-Net,代碼行數:12,代碼來源:hard_mine_triplet_loss.py

示例10: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, batch_size, margin=0.3):
        super(OriTripletLoss, self).__init__()
        self.margin = margin
        self.ranking_loss = nn.MarginRankingLoss(margin=margin) 
開發者ID:mangye16,項目名稱:Cross-Modal-Re-ID-baseline,代碼行數:6,代碼來源:loss.py

示例11: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, margin=0):
        super(TripletLoss, self).__init__()
        self.margin = margin
        self.ranking_loss = nn.MarginRankingLoss(margin=margin) 
開發者ID:Cysu,項目名稱:open-reid,代碼行數:6,代碼來源:triplet.py

示例12: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, margin=None):
        self.margin = margin
        if margin is not None:
            self.ranking_loss = nn.MarginRankingLoss(margin=margin)
        else:
            self.ranking_loss = nn.SoftMarginLoss() 
開發者ID:yujheli,項目名稱:ARN,代碼行數:8,代碼來源:reid_loss.py

示例13: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, margin=0.3):
        super(TripletLoss, self).__init__()
        self.margin = margin
        self.ranking_loss = nn.MarginRankingLoss(margin=margin) 
開發者ID:SamvitJ,項目名稱:ReXCam,代碼行數:6,代碼來源:losses.py

示例14: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, margin=0.3, mutual_flag = False):
        super(TripletLoss, self).__init__()
        self.margin = margin
        self.ranking_loss = nn.MarginRankingLoss(margin=margin)
        self.mutual = mutual_flag 
開發者ID:michuanhaohao,項目名稱:AlignedReID,代碼行數:7,代碼來源:losses.py

示例15: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import MarginRankingLoss [as 別名]
def __init__(self, margin=0):
        super(OnlineTripletLoss, self).__init__()
        self.margin = margin
        self.ranking_loss = nn.MarginRankingLoss(margin=margin) 
開發者ID:zhangxinyu-xyz,項目名稱:PAST-ReID,代碼行數:6,代碼來源:triplet.py


注:本文中的torch.nn.MarginRankingLoss方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。