机器学习笔记 - 关于Contrastive Loss对比损失
一、对比损失
虽然二元交叉熵(下图公式)肯定是损失函数的有效选择,但它不是唯一的选择(甚至不是最佳选择)。
然而,实际上有一个更适合孪生网络的损失函数,称为对比损失。
其中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):
二、参考代码
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对比损失相关推荐
- 机器学习笔记之配分函数(三)对比散度
机器学习笔记之配分函数--对比散度 引言 回顾:随机最大似然求解模型参数的过程 随机最大似然的缺陷 吉布斯采样的缺陷与对比散度思想 对比散度名称的由来 从 K L \mathcal K\mathcal ...
- Contrastive Loss 对比损失函数及梯度的计算
Contrastive loss 最初源于 Yann LeCun "Dimensionality Reduction by Learning an Invariant Mapping&quo ...
- Contrastive Loss 对比损失函数
Contrastive Loss 定义 在caffe的孪生神经网络(siamese network)中,其采用的损失函数是contrastive loss,这种损失函数可以有效的处理孪生神经网络中的p ...
- 损失函数笔记(2)--对比损失
一些常用与分类问题的损失函数: 交叉熵损失: 是分类问题中默认使用的损失函数 交叉熵函数:计算了类概率与标准答案的距离 softamax:一般用作最后一层的激活函数,计算归一化的类概率. 前置知识: ...
- 机器学习笔记:triplet loss
1 Triplet loss Triplet Loss,即三元组损失,其中的三元是Anchor.Negative.Positive. 通过Triplet Loss的学习后使得Positive元和Anc ...
- Contrastive Loss(Siamese Network)
Contrastive Loss (对比损失) 孪生神经网络(siamese network)中,其采用的损失函数是contrastive loss,这种损失函数可以有效的处理孪生神经网络中的pair ...
- Understanding the Behaviour of Contrastive Loss理解对比损失的性质以及温度系数的作用
论文标题:Understanding the Behaviour of Contrastive Loss Understanding the Behaviour of Contrastive Loss ...
- 对比损失Contrastive Loss
[时间]2-019.01.21 [题目]对比损失Contrastive Loss PS:本文转载自Contrastive Loss Contrastive Loss 在传统的siamese netwo ...
- 论文笔记:Dual Contrastive Loss and Attention for GANs
目录 一.Dual Contrastive Loss and Attention for GANs 是什么? 1.损失函数 2.网络结构 3.总结 二.这项工作主要做了什么? 1.双重对比损失 2.生 ...
最新文章
- 自定义UISearchBar外观
- [Java]ping或扫描端口的工具类
- 完整iOS APP发布App Store上架流程
- 配置计算机系统doc,[计算机系统配置实用程序.doc
- 基础算法 —— 高精度计算
- java semaphore(0)_面试官:说说Java中的信号量?Semaphore
- 27.go list
- 基于OP放大器的有源模拟滤波器设计--一阶有源滤波器
- arcgis制作超链接
- 原生代码开发小米官网首页
- CF1437F Emotional Fishermen
- 利辛县腾讯服务器维护,腾讯内容开放平台
- 常见面试题及解答|计算机网络
- 比特大陆裁员潮据称已波及整个BCH开发团队
- 计算机能力测试power,PowerMAX(电脑拷机测试软件)
- 【实习日记】实习第N天 从零开始搭建一个tiktok puppet(一)
- win7隐藏任务栏图标和通知
- MySQL Authentications
- python培训班价格-少儿编程培训机构哪家好,儿童编程价格多少钱?家长知多少...
- 图片数据损坏了怎么恢复