本文整理匯總了Python中torch.nn.SoftMarginLoss方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.SoftMarginLoss方法的具體用法?Python nn.SoftMarginLoss怎麽用?Python nn.SoftMarginLoss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.SoftMarginLoss方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [as 別名]
def __init__(self, n_templates=25, reg_weight=1, pos_fraction=0.5):
super().__init__()
# We don't want per element averaging.
# We want to normalize over the batch or positive samples.
self.regression_criterion = nn.SmoothL1Loss(reduction='none')
self.classification_criterion = nn.SoftMarginLoss(reduction='none')
self.n_templates = n_templates
self.reg_weight = reg_weight
self.pos_fraction = pos_fraction
self.class_average = AvgMeter()
self.reg_average = AvgMeter()
self.masked_class_loss = None
self.masked_reg_loss = None
self.total_loss = None
示例2: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [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)
示例3: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [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: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [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()
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [as 別名]
def __init__(self):
super(TripletLoss_WRT, self).__init__()
self.ranking_loss = nn.SoftMarginLoss()
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [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()
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [as 別名]
def __init__(self, margin = None):
super(TripletLoss, self).__init__()
self.margin = margin
if self.margin is None: # use soft-margin
self.Loss = nn.SoftMarginLoss()
else:
self.Loss = nn.TripletMarginLoss(margin = margin, p = 2)
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [as 別名]
def __init__(self, margin=None, metric="euclidean"):
self.margin = margin
self.metric = metric
if margin is not None:
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
else:
self.ranking_loss = nn.SoftMarginLoss()
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SoftMarginLoss [as 別名]
def __init__(self, margin=None):
super(TripletLoss, self).__init__()
self.margin = margin
if self.margin is None: # if no margin assigned, use soft-margin
self.Loss = nn.SoftMarginLoss()
else:
self.Loss = nn.TripletMarginLoss(margin=margin, p=2)