本文整理匯總了Python中torch.nn.TripletMarginLoss方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.TripletMarginLoss方法的具體用法?Python nn.TripletMarginLoss怎麽用?Python nn.TripletMarginLoss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.TripletMarginLoss方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import TripletMarginLoss [as 別名]
def __init__(self, config, net):
self.log_dir = config.log_dir
self.model_dir = config.model_dir
self.net = net
self.clock = TrainClock()
self.device = config.device
self.use_triplet = config.use_triplet
self.use_footvel_loss = config.use_footvel_loss
# set loss function
self.mse = nn.MSELoss()
self.tripletloss = nn.TripletMarginLoss(margin=config.triplet_margin)
self.triplet_weight = config.triplet_weight
self.foot_idx = config.foot_idx
self.footvel_loss_weight = config.footvel_loss_weight
# set optimizer
self.optimizer = optim.Adam(self.net.parameters(), config.lr)
self.scheduler = optim.lr_scheduler.ExponentialLR(self.optimizer, 0.99)
示例2: setup
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import TripletMarginLoss [as 別名]
def setup(model, opt):
if opt.criterion == "l1":
criterion = nn.L1Loss().cuda()
elif opt.criterion == "mse":
criterion = nn.MSELoss().cuda()
elif opt.criterion == "crossentropy":
criterion = nn.CrossEntropyLoss().cuda()
elif opt.criterion == "hingeEmbedding":
criterion = nn.HingeEmbeddingLoss().cuda()
elif opt.criterion == "tripletmargin":
criterion = nn.TripletMarginLoss(margin = opt.margin, swap = opt.anchorswap).cuda()
parameters = filter(lambda p: p.requires_grad, model.parameters())
if opt.optimType == 'sgd':
optimizer = optim.SGD(parameters, lr = opt.lr, momentum = opt.momentum, nesterov = opt.nesterov, weight_decay = opt.weightDecay)
elif opt.optimType == 'adam':
optimizer = optim.Adam(parameters, lr = opt.maxlr, weight_decay = opt.weightDecay)
if opt.weight_init:
utils.weights_init(model, opt)
return model, criterion, optimizer
示例3: build_criterion
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import TripletMarginLoss [as 別名]
def build_criterion(loss_dict):
if loss_dict.type == 'CrossEntropyLoss':
weight = loss_dict.weight
size_average = loss_dict.size_average
reduce = loss_dict.reduce
reduction = loss_dict.reduction
if loss_dict.use_sigmoid:
return nn.BCEWithLogitsLoss(
weight=weight,
size_average=size_average,
reduce=reduce,
reduction=reduction)
else:
return nn.CrossEntropyLoss(
weight=weight,
size_average=size_average,
reduce=reduce,
reduction=reduction)
elif loss_dict.type == 'TripletLoss':
return nn.TripletMarginLoss(margin=loss_dict.margin, p=loss_dict.p)
else:
raise TypeError('{} cannot be processed'.format(loss_dict.type))
示例4: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import TripletMarginLoss [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)
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import TripletMarginLoss [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)