一、对比损失

虽然二元交叉熵(下图公式)肯定是损失函数的有效选择,但它不是唯一的选择(甚至不是最佳选择)。

然而,实际上有一个更适合孪生网络的损失函数,称为对比损失。

其中Y是我们的标签。如果图像对属于同一类,则为 1,如果图像对属于不同类,则为 0。

变量是孪生网络的输出之间的欧几里得距离。

max 函数取最大值 0 和边距 m 减去距离。

孪生网络的目标不是对一组图像对进行分类,而是区分它们。 从本质上讲,对比损失是评估孪生网络在图像对之间的区分程度。

如果能读得进去论文,可以更深入了解下。http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdfhttp://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf        在 Keras/TensorFlow 代码中实现的对比损失函数的公式(也是论文中说的exact loss functi):

使用 Keras 和 TensorFlow 实现对比损失函数

二、参考代码

Image similarity estimation using a Siamese Network with a contrastive lossKeras documentationhttps://keras.io/examples/vision/siamese_contrastive/        摘抄函数定义1

def loss(margin=1):"""Provides 'constrastive_loss' an enclosing scope with variable 'margin'.Arguments:margin: Integer, defines the baseline for distance for which pairsshould be classified as dissimilar. - (default is 1).Returns:'constrastive_loss' function with data ('margin') attached."""# Contrastive loss = mean( (1-true_value) * square(prediction) +#                         true_value * square( max(margin-prediction, 0) ))def contrastive_loss(y_true, y_pred):"""Calculates the constrastive loss.Arguments:y_true: List of labels, each label is of type float32.y_pred: List of predictions of same length as of y_true,each label is of type float32.Returns:A tensor containing constrastive loss as floating point value."""square_pred = tf.math.square(y_pred)margin_square = tf.math.square(tf.math.maximum(margin - (y_pred), 0))return tf.math.reduce_mean((1 - y_true) * square_pred + (y_true) * margin_square)return contrastive_loss

摘抄函数定义2

# import the necessary packages
import tensorflow.keras.backend as K
import tensorflow as tf
def contrastive_loss(y, preds, margin=1):# explicitly cast the true class label data type to the predicted# class label data type (otherwise we run the risk of having two# separate data types, causing TensorFlow to error out)y = tf.cast(y, preds.dtype)# calculate the contrastive loss between the true labels and# the predicted labelssquaredPreds = K.square(preds)squaredMargin = K.square(K.maximum(margin - preds, 0))loss = K.mean(y * squaredPreds + (1 - y) * squaredMargin)# return the computed contrastive loss to the calling functionreturn loss

机器学习笔记 - 关于Contrastive Loss对比损失相关推荐

  1. 机器学习笔记之配分函数(三)对比散度

    机器学习笔记之配分函数--对比散度 引言 回顾:随机最大似然求解模型参数的过程 随机最大似然的缺陷 吉布斯采样的缺陷与对比散度思想 对比散度名称的由来 从 K L \mathcal K\mathcal ...

  2. Contrastive Loss 对比损失函数及梯度的计算

    Contrastive loss 最初源于 Yann LeCun "Dimensionality Reduction by Learning an Invariant Mapping&quo ...

  3. Contrastive Loss 对比损失函数

    Contrastive Loss 定义 在caffe的孪生神经网络(siamese network)中,其采用的损失函数是contrastive loss,这种损失函数可以有效的处理孪生神经网络中的p ...

  4. 损失函数笔记(2)--对比损失

    一些常用与分类问题的损失函数: 交叉熵损失: 是分类问题中默认使用的损失函数 交叉熵函数:计算了类概率与标准答案的距离 softamax:一般用作最后一层的激活函数,计算归一化的类概率. 前置知识: ...

  5. 机器学习笔记:triplet loss

    1 Triplet loss Triplet Loss,即三元组损失,其中的三元是Anchor.Negative.Positive. 通过Triplet Loss的学习后使得Positive元和Anc ...

  6. Contrastive Loss(Siamese Network)

    Contrastive Loss (对比损失) 孪生神经网络(siamese network)中,其采用的损失函数是contrastive loss,这种损失函数可以有效的处理孪生神经网络中的pair ...

  7. Understanding the Behaviour of Contrastive Loss理解对比损失的性质以及温度系数的作用

    论文标题:Understanding the Behaviour of Contrastive Loss Understanding the Behaviour of Contrastive Loss ...

  8. 对比损失Contrastive Loss

    [时间]2-019.01.21 [题目]对比损失Contrastive Loss PS:本文转载自Contrastive Loss Contrastive Loss 在传统的siamese netwo ...

  9. 论文笔记:Dual Contrastive Loss and Attention for GANs

    目录 一.Dual Contrastive Loss and Attention for GANs 是什么? 1.损失函数 2.网络结构 3.总结 二.这项工作主要做了什么? 1.双重对比损失 2.生 ...

最新文章

  1. 自定义UISearchBar外观
  2. [Java]ping或扫描端口的工具类
  3. 完整iOS APP发布App Store上架流程
  4. 配置计算机系统doc,[计算机系统配置实用程序.doc
  5. 基础算法 —— 高精度计算
  6. java semaphore(0)_面试官:说说Java中的信号量?Semaphore
  7. 27.go list
  8. 基于OP放大器的有源模拟滤波器设计--一阶有源滤波器
  9. arcgis制作超链接
  10. 原生代码开发小米官网首页
  11. CF1437F Emotional Fishermen
  12. 利辛县腾讯服务器维护,腾讯内容开放平台
  13. 常见面试题及解答|计算机网络
  14. 比特大陆裁员潮据称已波及整个BCH开发团队
  15. 计算机能力测试power,PowerMAX(电脑拷机测试软件)
  16. 【实习日记】实习第N天 从零开始搭建一个tiktok puppet(一)
  17. win7隐藏任务栏图标和通知
  18. MySQL Authentications
  19. python培训班价格-少儿编程培训机构哪家好,儿童编程价格多少钱?家长知多少...
  20. 图片数据损坏了怎么恢复

热门文章

  1. closeEvent
  2. Game theory: Prison breakthrough
  3. Acrel-EIOT免调试抄表系统
  4. 海关数据有用吗?看看资深外贸怎么说!
  5. 超市收银系统服务器,超市收银系统
  6. 中断服务子程序 ISR
  7. 云数据库 MySQL使用规范 数据库开发人员一定要看!
  8. 六种难以启齿的真实离职原因,应该这样说
  9. 实现搜索框功能(将搜索关键字标红,点击就加入到文本框中)
  10. R语言数据合并与连接技巧