本文整理匯總了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)
示例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]
示例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()
示例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()
示例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)
示例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)
示例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()
示例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)
示例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']
示例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)
示例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)
示例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()
示例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)
示例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
示例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)