对比学习允许模型从未标记的数据中提取有意义的表示。通过利用相似性和不相似性,对比学习使模型能够在潜在空间中将相似的实例紧密地映射在一起,同时将那些不同的实例分开(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)逐渐成为一项重要的无监督学习方法。它通过在没有标注数据的情况下,利用数据之间的相似性和差异性,进行有效的特征学习。对比学习的核心理念在于,通过对比样本对的方式,尽可能将相似的样本拉近,而将不相似的样本推远。这样一来,模型可以在没有标签的情况下学习到有意义的特征表示。
什么是对比学习?
对比学习是一种学习方法,侧重于通过对比正反两方面的实例来提取有意义的表征。它利用的假设是,在学习到的嵌入空间中,相似的实例应靠得更近,而不相似的实例应离得更远。通过将学习作为一项辨别任务,对比学习允许模型捕捉数据中的相关特征和相似性。
对比学习中的关键操作是构建“正样本对”和“负样本对”。
正样本对:两个样本具有相似的特征,比如在图片中,同一物体的不同角度或不同的光照条件。
负样本对:两个样本具有不同的特征,比如不同物体的图像。
通过学习模型将正样本对的距离缩小,负样本对的距离扩大,促进模型对数据的深入理解。
对比学习在深度学习中的应用
对比学习作为一种强大的特征学习技术,被广泛应用于多个领域。
- 无监督学习
对比学习为无监督学习开辟了新天地。传统的无监督学习很难利用大量的未标注数据,而对比学习通过有效的对样本进行比较,使得模型能够从未标注的数据中学习到有用的特征。
例如,在图像分类任务中,可以使用对比学习从未标注的图像中学习特征,然后再将这些特征输入到监督学习模型中,进行分类。在某些情况下,模型的表现甚至可以媲美使用标注数据的情况。
- 表示学习
表示学习是深度学习中的一个重要课题,目标是学习数据的有效表示,以便后续的任务更容易处理。对比学习通过对数据样本的对比关系,使得模型能够学习到更加鲁棒的表示。
例如,利用对比学习,模型可以学习到应该将相似对象的特征向量聚集在一起,而将不相似对象的特征拉开。这促进了模型对复杂数据集的理解,例如自然语言处理(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
- 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+
- 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进行训练:
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) 对比学习】深入浅出讲解对比学习