attack_carlini_wagner_l2.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:pytorch-nips2017-attack-example 作者: rwightman 项目源码 文件源码
def _loss(self, output, target, dist, scale_const):
        # compute the probability of the label class versus the maximum other
        real = (target * output).sum(1)
        other = ((1. - target) * output - target * 10000.).max(1)[0]
        if self.targeted:
            # if targeted, optimize for making the other class most likely
            loss1 = torch.clamp(other - real + self.confidence, min=0.)  # equiv to max(..., 0.)
        else:
            # if non-targeted, optimize for making this class least likely.
            loss1 = torch.clamp(real - other + self.confidence, min=0.)  # equiv to max(..., 0.)
        loss1 = torch.sum(scale_const * loss1)

        loss2 = dist.sum()

        loss = loss1 + loss2
        return loss
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号