当前位置:网站首页>【AI4Code】《CoSQA: 20,000+ Web Queries for Code Search and Question Answering》 ACL 2021
【AI4Code】《CoSQA: 20,000+ Web Queries for Code Search and Question Answering》 ACL 2021
2022-07-25 11:11:00 【chad_lee】
《CoSQA: 20,000+ Web Queries for Code Search and Question Answering》 ACL 2021
类似CLIP 做出了一个 NL-PL的query-key二分类数据集,然后类似CLIP一样双模态对齐训练,在此基础上添加了对比学习,并设计了两类数据扩增方法。双塔的encoder都是CodeBERT。
CoSQA数据集
文章想要实现的事情是我们可以像在网上搜图片一样,根据需求输入query,返回符合需求的代码实现(现在一般是返回博客)。文章花了很大力气构造一个这样的数据集,大概长这个样子。
还有大量的数据构造细节,比如部分满足query的需求,完全满足query的需求,满足少于50%的需求,仅和query有关等。

模型

模型的输入形式各为一条序列:[CLS] xxxxxxxx [SEP]。模型用的孪生网络,query和code都用同一个CodeBERT编码。模型的输出是 [CLS] 的表征。
q i = C o d e E R T ( q i ) , c i = C o d e B E R T ( c i ) \mathbf{q}_{i}=\mathbf{C o d e} \mathbf{E R T}\left(q_{i}\right), \quad \mathbf{c}_{i}=\mathbf{C o d e B} \mathbf{E R T}\left(c_{i}\right) qi=CodeERT(qi),ci=CodeBERT(ci)
模型不是简单的用q和c的内积计算相似度,而是再用一个MLP的计算二者的匹配关系。MLP的输出是向量,不是相似度分数
r ( i , i ) = tanh ( W 1 ⋅ [ q i , c i , q i − c i , q i ⨀ c i ] ) \mathbf{r}^{(i, i)}=\tanh \left(\mathbf{W}_{1} \cdot\left[\mathbf{q}_{i}, \mathbf{c}_{i}, \mathbf{q}_{i}-\mathbf{c}_{i}, \mathbf{q}_{i} \bigodot \mathbf{c}_{i}\right]\right) r(i,i)=tanh(W1⋅[qi,ci,qi−ci,qi⨀ci])
再用一个单层NN计算二者的相似度:
s ( i , i ) = sigmoid ( W 2 ⋅ r ( i , i ) ) s^{(i, i)}=\operatorname{sigmoid}\left(\mathbf{W}_{2} \cdot \mathbf{r}^{(i, i)}\right) s(i,i)=sigmoid(W2⋅r(i,i))
然后用BCE loss训练:
L b = − [ y i ⋅ log s ( i , i ) + ( 1 − y i ) log ( 1 − s ( i , i ) ) ] \mathcal{L}_{b}=-\left[y_{i} \cdot \log s^{(i, i)}+\left(1-y_{i}\right) \log \left(1-s^{(i, i)}\right)\right] Lb=−[yi⋅logs(i,i)+(1−yi)log(1−s(i,i))]
对比学习
除了BCE loss外,In-Batch Augmentation (IBA)和Query-Rewritten Augmentation (QRA)
IBA Loss
对于每一个query,同时选择当前batch内其他的code作为负样本,相当于对一个query扩增了多个code负样本
L i b = − 1 n − 1 ∑ j = 1 j ≠ i n log ( 1 − s ( i , j ) ) \mathcal{L}_{i b}=-\frac{1}{n-1} \sum_{\substack{j=1 \\ j \neq i}}^{n} \log \left(1-s^{(i, j)}\right) Lib=−n−11j=1j=i∑nlog(1−s(i,j))
QRA Loss
由于Web query通常都很短,并且不保证语法正确,因此对于一对标签为1的 query-code pair,对query做一些重写修改,包括:随机删一个单词、随机调换两个单词位置、随机复制一个单词。
这样相当于对一个code扩增了多个Query正样本。在QRA的基础上还会应用IBA loss:
L q r = L b ′ + L i b ′ \mathcal{L}_{q r}=\mathcal{L}_{b}^{\prime}+\mathcal{L}_{i b}^{\prime} Lqr=Lb′+Lib′
实验
本文提出code contrastive learning method (CoCLR) 的是一种学习方法,实验是在预训练好的CodeBERT的基础上继续训练。
两个task一个是Code Question Answering 直接从训练集里划分出测试集,一个是代码搜索。
CodeBERT+CoSQA就是在BERT的基础上显式地对齐两种语言,有一定提升,但是效果最好的还是对比学习的数据扩增。其中最有用的是batch内负样本。query增强中交换单词顺序的提升较大,这也比较符合直觉,因为一般换交两个字往往不影响读阅理解。
边栏推荐
- 已解决 Files‘ name is invalid or does not exist (1205)
- LeetCode 50. Pow(x,n)
- Video Caption(跨模态视频摘要/字幕生成)
- Signal and slot mechanism ==pyqt5
- 【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
- 浅谈低代码技术在物流管理中的应用与创新
- 图神经网络用于推荐系统问题(IMP-GCN,LR-GCN)
- PHP curl post length required error setting header header
- 微星主板前面板耳机插孔无声音输出问题【已解决】
- JS运算符
猜你喜欢

Teach you how to configure S2E as the working mode of TCP server through MCU
![[imx6ull notes] - a preliminary exploration of the underlying driver of the kernel](/img/0f/a0139be99c61fde08e73a5be6d6b4c.png)
[imx6ull notes] - a preliminary exploration of the underlying driver of the kernel

【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)

浅谈低代码技术在物流管理中的应用与创新

brpc源码解析(七)—— worker基于ParkingLot的bthread调度

Meta-learning(元学习与少样本学习)

brpc源码解析(六)—— 基础类socket详解
![[electronic device notes 5] diode parameters and selection](/img/4d/05c60641dbdbfbfa6c3cc19a24fa03.png)
[electronic device notes 5] diode parameters and selection
![There is no sound output problem in the headphone jack on the front panel of MSI motherboard [solved]](/img/e8/d663d0a3c26fce8940f91c6db4afdb.png)
There is no sound output problem in the headphone jack on the front panel of MSI motherboard [solved]

brpc源码解析(三)—— 请求其他服务器以及往socket写数据的机制
随机推荐
Teach you how to configure S2E to UDP working mode through MCU
How to solve the problem that "w5500 chip cannot connect to the server immediately after power failure and restart in tcp_client mode"
Varest blueprint settings JSON
[MySQL 17] installation exception: could not open file '/var/log/mysql/mysqld log‘ for error logging: Permission denied
Layout management ==pyqt5
Multi-Label Image Classification(多标签图像分类)
Functions in JS
对比学习的应用(LCGNN,VideoMoCo,GraphCL,XMC-GAN)
【高并发】我用10张图总结出了这份并发编程最佳学习路线!!(建议收藏)
[leetcode brush questions]
W5500 upload temperature and humidity to onenet platform
PHP curl post x-www-form-urlencoded
Introduction to redis
Video Caption(跨模态视频摘要/字幕生成)
【RS采样】A Gain-Tuning Dynamic Negative Sampler for Recommendation (WWW 2022)
pycharm连接远程服务器ssh -u 报错:No such file or directory
Maskgae: masked graph modeling meets graph autoencoders
软件测试阶段的风险
浅谈低代码技术在物流管理中的应用与创新
[MySQL learning 08]