当前位置: 首页>>代码示例>>Python>>正文


Python functional.multilabel_soft_margin_loss方法代码示例

本文整理汇总了Python中torch.nn.functional.multilabel_soft_margin_loss方法的典型用法代码示例。如果您正苦于以下问题:Python functional.multilabel_soft_margin_loss方法的具体用法?Python functional.multilabel_soft_margin_loss怎么用?Python functional.multilabel_soft_margin_loss使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在torch.nn.functional的用法示例。


在下文中一共展示了functional.multilabel_soft_margin_loss方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: validate

# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import multilabel_soft_margin_loss [as 别名]
def validate(model, data_loader):
    print('\nvalidating ... ', flush=True, end='')

    val_loss_meter = pyutils.AverageMeter('loss')

    model.eval()

    with torch.no_grad():
        for pack in data_loader:
            img = pack[1]
            label = pack[2].cuda(non_blocking=True)
            label = label.unsqueeze(2).unsqueeze(3)

            x = model(img)
            x = F.adaptive_avg_pool2d(x, (1,1))	
            loss = F.multilabel_soft_margin_loss(x, label)

            val_loss_meter.add({'loss': loss.item()})

    model.train()

    print('loss:', val_loss_meter.pop('loss'))

    return 
开发者ID:YudeWang,项目名称:SSENet-pytorch,代码行数:26,代码来源:train_cls_ser.py

示例2: calculate_loss

# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import multilabel_soft_margin_loss [as 别名]
def calculate_loss(answer, pred, method):
    """
    answer = [batch, 3129]
    pred = [batch, 3129]
    """
    if method == 'binary_cross_entropy_with_logits':
        loss = F.binary_cross_entropy_with_logits(pred, answer) * config.max_answers
    elif method == 'soft_cross_entropy':
        nll = -F.log_softmax(pred, dim=1)
        loss = (nll * answer).sum(dim=1).mean()   # this is worse than binary_cross_entropy_with_logits
    elif method == 'KL_divergence':
        pred = F.softmax(pred, dim=1)
        kl = ((answer / (pred + 1e-12)) + 1e-12).log()
        loss = (kl * answer).sum(1).mean()
    elif method == 'multi_label_soft_margin':
        loss = F.multilabel_soft_margin_loss(pred, answer)
    else:
        print('Error, pls define loss function')
    return loss 
开发者ID:KaihuaTang,项目名称:VQA2.0-Recent-Approachs-2018.pytorch,代码行数:21,代码来源:utils.py

示例3: validate

# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import multilabel_soft_margin_loss [as 别名]
def validate(model, data_loader):
    print('\nvalidating ... ', flush=True, end='')

    val_loss_meter = pyutils.AverageMeter('loss')

    model.eval()

    with torch.no_grad():
        for pack in data_loader:
            img = pack[1]
            label = pack[2].cuda(non_blocking=True)

            x = model(img)
            loss = F.multilabel_soft_margin_loss(x, label)

            val_loss_meter.add({'loss': loss.item()})

    model.train()

    print('loss:', val_loss_meter.pop('loss'))

    return 
开发者ID:jiwoon-ahn,项目名称:psa,代码行数:24,代码来源:train_cls.py

示例4: validate

# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import multilabel_soft_margin_loss [as 别名]
def validate(model, data_loader):
    print('validating ... ', flush=True, end='')

    val_loss_meter = pyutils.AverageMeter('loss1', 'loss2')

    model.eval()

    with torch.no_grad():
        for pack in data_loader:
            img = pack['img']

            label = pack['label'].cuda(non_blocking=True)

            x = model(img)
            loss1 = F.multilabel_soft_margin_loss(x, label)

            val_loss_meter.add({'loss1': loss1.item()})

    model.train()

    print('loss: %.4f' % (val_loss_meter.pop('loss1')))

    return 
开发者ID:jiwoon-ahn,项目名称:irn,代码行数:25,代码来源:train_cam.py

示例5: multilabel_soft_margin_loss

# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import multilabel_soft_margin_loss [as 别名]
def multilabel_soft_margin_loss(
    input: Tensor, 
    target: Tensor,
    weight: Optional[Tensor] = None,
    size_average: bool = True,
    reduce: bool = True,
    difficult_samples: bool = False) -> Tensor:
    """Multilabel soft margin loss.
    """

    if difficult_samples:
        # label 1: positive samples
        # label 0: difficult samples
        # label -1: negative samples
        gt_label = target.clone()
        gt_label[gt_label == 0] = 1
        gt_label[gt_label == -1] = 0
    else:
        gt_label = target
        
    return F.multilabel_soft_margin_loss(input, gt_label, weight, size_average, reduce) 
开发者ID:chuchienshu,项目名称:ultra-thin-PRM,代码行数:23,代码来源:losses.py

示例6: loss

# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import multilabel_soft_margin_loss [as 别名]
def loss(self, proposal_classes: Tensor,gt_proposal_classes: Tensor, batch_size,batch_indices) -> Tuple[Tensor, Tensor]:
            # assert np.any(np.isnan(np.array(proposal_classes)))==False
            # assert np.any(np.isnan(np.array(gt_proposal_classes))) == False
            cross_entropies = torch.zeros(batch_size, dtype=torch.float, device=proposal_classes.device).cuda()
            #batch_indices=torch.tensor(batch_indices,dtype=torch.float)
            for batch_index in range(batch_size):
                selected_indices = (batch_indices == batch_index).nonzero().view(-1)
                input=proposal_classes[selected_indices]
                target=gt_proposal_classes[selected_indices]
                if torch.numel(input)==0 or torch.numel(target)==0:
                    #print("Warning:None DATA:",batch_index)
                    continue
                assert torch.numel(input)==torch.numel(target)
                # print('input:',input)
                # print("input_sigmoid:", F.sigmoid(input))
                # print('target:',target)


                cross_entropy =F.multilabel_soft_margin_loss(input=proposal_classes[selected_indices],target=gt_proposal_classes[selected_indices],reduction="mean")

                # cross_entropy = F.binary_cross_entropy(input=F.sigmoid(proposal_classes[selected_indices]),
                #                                                target=gt_proposal_classes[selected_indices])
                torch.nn.MultiLabelSoftMarginLoss
                # print('cross_entropy:',cross_entropy)
                # print('cross_entropy:',cross_entropy)
                # cross_entropy = F.cross_entropy(input=proposal_classes[selected_indices],
                #                                 target=gt_proposal_classes[selected_indices])

                cross_entropies[batch_index] = cross_entropy
            return cross_entropies 
开发者ID:MagicChuyi,项目名称:SlowFast-Network-pytorch,代码行数:32,代码来源:model.py

示例7: multilabel_soft_margin

# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import multilabel_soft_margin_loss [as 别名]
def multilabel_soft_margin(y_pred, y_true):
    return F.multilabel_soft_margin_loss(y_pred, y_true) 
开发者ID:GRAAL-Research,项目名称:poutyne,代码行数:4,代码来源:batch_metrics.py


注:本文中的torch.nn.functional.multilabel_soft_margin_loss方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。