对比学习 Contrastive Learning

对比学习允许模型从未标记的数据中提取有意义的表示。通过利用相似性和不相似性,对比学习使模型能够在潜在空间中将相似的实例紧密地映射在一起,同时将那些不同的实例分开(cluster?)。这种方法已被证明在计算机视觉、自然语言处理 (NLP) 和强化学习等不同领域都是有效的。


对比学习在软件工程领域的常用目的是better embedding(It is of great significance to represent patches in suitable embeddings because it will particularly affect whether the patches with the highest similarity scores are semantically similar to the test patch or not. To embed patches into embeddings of good quality, we train an unsupervised patch representation model by leveraging contrastive learning)1

背景

随着深度学习的广泛应用,传统的监督学习方法逐渐暴露出其局限性。对于许多实际任务,标注数据的获取成本高,并且在某些领域甚至是不可行的。这促使研究者们寻找无监督学习的方法来从大规模未标注数据中提取有价值的信息。对比学习正是在这种背景下应运而生,它利用对象之间的对比关系来建立有效的表示。

在众多技术中,对比学习(Contrastive Learning)逐渐成为一项重要的无监督学习方法。它通过在没有标注数据的情况下,利用数据之间的相似性和差异性,进行有效的特征学习。对比学习的核心理念在于,通过对比样本对的方式,尽可能将相似的样本拉近,而将不相似的样本推远。这样一来,模型可以在没有标签的情况下学习到有意义的特征表示。

什么是对比学习?

对比学习是一种学习方法,侧重于通过对比正反两方面的实例来提取有意义的表征。它利用的假设是,在学习到的嵌入空间中,相似的实例应靠得更近,而不相似的实例应离得更远。通过将学习作为一项辨别任务,对比学习允许模型捕捉数据中的相关特征和相似性。

对比学习中的关键操作是构建“正样本对”和“负样本对”。

正样本对:两个样本具有相似的特征,比如在图片中,同一物体的不同角度或不同的光照条件。
负样本对:两个样本具有不同的特征,比如不同物体的图像。
通过学习模型将正样本对的距离缩小,负样本对的距离扩大,促进模型对数据的深入理解。

对比学习在深度学习中的应用

对比学习作为一种强大的特征学习技术,被广泛应用于多个领域。

  1. 无监督学习
    对比学习为无监督学习开辟了新天地。传统的无监督学习很难利用大量的未标注数据,而对比学习通过有效的对样本进行比较,使得模型能够从未标注的数据中学习到有用的特征。

例如,在图像分类任务中,可以使用对比学习从未标注的图像中学习特征,然后再将这些特征输入到监督学习模型中,进行分类。在某些情况下,模型的表现甚至可以媲美使用标注数据的情况。

  1. 表示学习
    表示学习是深度学习中的一个重要课题,目标是学习数据的有效表示,以便后续的任务更容易处理。对比学习通过对数据样本的对比关系,使得模型能够学习到更加鲁棒的表示。

例如,利用对比学习,模型可以学习到应该将相似对象的特征向量聚集在一起,而将不相似对象的特征拉开。这促进了模型对复杂数据集的理解,例如自然语言处理(NLP)中的文本嵌入和图像分类等任务。

对比学习在软件工程中的使用

Leveraging Large Language Model for Automatic Patch Correctness Assessment, TSE’24 1

这篇文章使用了RAG的方式来进行Automated Patch Correctness Assessment(APAC),即retrieve和test patch相似的similar patches并concentrate到prompt 中,让LLMs判断输出test patch是否是plausible patches。在RAG的过程中,使用Contrastive Learning训练models(本文中是CodeBERT)来更好地表示patch embedding,这样才能准确的使用model来represent embedding,再通过计算similarity scores来retrieve。
在这里插入图片描述

文章中的方法参考论文:Simcse: Simple contrastive learning of sentence embeddings, EMNLP’212。过程包括:Triplet data construction

  1. Triplet data construction
    训练集是三元组 < p , p + , p − > <p, p^+, p^-> <p,p+,p> p p p是一个patch, p + p^+ p+ p p p的similar patch, p − p^- p p p p的dissimilar patch。因此, < p , p + > <p, p^+> <p,p+>是 similar pair, < p , p − > <p, p^-> <p,p>是unsimilar pair。The main training objective of contrastive representation learning is to learn such an embedding space in which similar patch pairs stay closer to each other while dissimilar ones push out far away from each other 1.

为了表示 p p p, p + p^+ p+ p − p^- p的embedding,generate the embedding of a triplet t i = < e m b ( p i ) , e m b ( p i + ) , e m b ( p i − ) > t_i = <emb(p_i), emb(p^+_i ), emb(p^-_i)> ti=<emb(pi),emb(pi+),emb(pi)>

  • e m b ( p i ) emb(p_i) emb(pi):将 p i p_i pi输入CodeBERT,经过第一个Dropout Module,得到 e m b ( p i ) emb(p_i) emb(pi)
  • e m b ( p i − ) emb(p_i^-) emb(pi):从数据集中sample一个不同于 p i p_i pi的patch p j p_j pj,把 p j p_j pj输入CodeBERT并经过第一个Dropout Module,得到 e m b ( p i − ) emb(p^-_i) emb(pi)
  • e m b ( P i + ) emb(P_i^+) emb(Pi+):用以下算法(a semantic-preserving transformation2)对 p i p_i pi进行转换,相当于把 p i p_i pi输入CodeBERT,经过第二个Dropout Module得到的embedding视为 p i + p_i^+ pi+
    算法
  1. Training
    Use the constructed triplet dataset to perform the training of the Contrastive Learning (CL) based patch embedding model. A transformer-based pretrained model is used as the base model for genearting embeddings, and a multi-layer perceptron is added on the top of the base model. (For text, use RoBERTa as the base model, while for code, use CodeBERT. The base model should not have too many parameters as we need to fully fine-tune it.)

输入 < p i , p i + , p i − > <p_i, p_i^+, p_i^-> <pi,pi+,pi>,用以下的loss function进行训练:
Loss function for Contrastive Learning
N N N is the number of patches in a mini-batch; τ τ τ is a temperature hyper-parameter; and c o s cos cos is the cosine similarity function. By minimizing the loss above, the model learns such an embedding space in which similar patch pairs stay closer to each other while dissimilar ones push out far away from each other. Then, we can utilize the trained CL-based model to assist in identifying similar patches from the training set when presented with a test patch.

ThinkRepair: Self-Directed Automated Program Repair, ISSTA’243

也是用Contrastive Learning来进行相似性embedding retrieve

参考

AI学习指南深度学习篇-对比学习(Contrastive Learning)简介
【深度学习:(Contrastive Learning) 对比学习】深入浅出讲解对比学习


  1. Leveraging Large Language Model for Automatic Patch Correctness Assessment, TSE’24 ↩︎ ↩︎ ↩︎

  2. Simcse: Simple contrastive learning of sentence embeddings, EMNLP’21 ↩︎ ↩︎

  3. ThinkRepair: Self-Directed Automated Program Repair, ISSTA’24 ↩︎

对比学习是一种自监督的、任务无关的深度学习技术,它允许模型学习数据,即使没有标签。该方法通过学习哪些类型的图像相似,哪些不同,来学习数据集的一般特征。对比学习的目标是使相似的图像具有相似的表示,从而允许模型学习如何区分图像。这种方法在标签稀缺的情况下尤为有用,因为预训练模型可以通过对数据有一般理解来提高标签效率,并在特定任务上进行微调,例如图像分类。对比学习方法的一个例子是SimCLRv2,它学习如何表示图像,使得相似的图像具有相似的表示。通过对比学习,模型可以更好地理解数据集的特征,并在特定任务上取得更好的性能。\[1\] \[2\] 生成式方法和对比学习是两种不同的方法。生成式方法主要关注像素级别的重构,通过自编码器等模型将数据样本编码成特征再解码重构,通过衡量重构的效果来评估模型学习到的特征表达的好坏。而对比学习则着重于学习同类实例之间的共同特征,区分非同类实例之间的不同之处。对比学习不需要关注实例上的细节,只需要在抽象语义级别的特征空间上学会对数据进行区分,因此模型的优化变得更加简单,且具有更强的泛化能力。\[3\] #### 引用[.reference_title] - *1* *2* [对比学习contrastive learning)](https://blog.csdn.net/cziun/article/details/119118768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [对比学习介绍Contrastive Learning](https://blog.csdn.net/weijie_home/article/details/119600296)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值