许可证: bsd-3-clause
标签:
- codet5
数据集:
- code_search_net
推理: true
用于代码摘要的CodeT5-base模型
CodeT5-base模型在多语言训练设置(Ruby/JavaScript/Go/Python/Java/PHP)下针对代码摘要任务在CodeSearchNet数据上进行了微调。该模型由Yue Wang、Weishi Wang、Shafiq Joty和Steven C.H. Hoi在EMNLP 2021论文CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation中提出。更多详情请查看此代码库。
使用方法
以下是使用该模型的示例代码:
from transformers import RobertaTokenizer, T5ForConditionalGeneration
if __name__ == '__main__':
tokenizer = RobertaTokenizer.from_pretrained('Salesforce/codet5-base-multi-sum')
model = T5ForConditionalGeneration.from_pretrained('Salesforce/codet5-base-multi-sum')
text = """def svg_to_image(string, size=None):
if isinstance(string, unicode):
string = string.encode('utf-8')
renderer = QtSvg.QSvgRenderer(QtCore.QByteArray(string))
if not renderer.isValid():
raise ValueError('Invalid SVG data.')
if size is None:
size = renderer.defaultSize()
image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
painter = QtGui.QPainter(image)
renderer.render(painter)
return image"""
input_ids = tokenizer(text, return_tensors="pt").input_ids
generated_ids = model.generate(input_ids, max_length=20)
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
微调数据
我们采用了来自CodeXGLUE基准的CodeSearchNet数据[Husain et al., 2019]的过滤版本进行代码摘要任务的微调。数据使用我们预训练的代码专用BPE(Byte-Pair Encoding)分词器进行分词。可以使用RobertaTokenizer配合codet5-base中的词汇文件为模型准备文本(或代码)。
数据统计
编程语言 |
训练集 |
验证集 |
测试集 |
Python |
251,820 |
13,914 |
14,918 |
PHP |
241,241 |
12,982 |
14,014 |
Go |
167,288 |
7,325 |
8,122 |
Java |
164,923 |
5,183 |
10,955 |
JavaScript |
58,025 |
3,885 |
3,291 |
Ruby |
24,927 |
1,400 |
1,261 |
训练过程
我们在多任务学习设置下对这六种编程语言(Ruby/JavaScript/Go/Python/Java/PHP)的codet5-base模型进行了微调。采用平衡采样以避免偏向高资源任务。更多细节请参阅论文。
评估结果
与论文中允许为不同编程语言(PLs)选择不同最佳检查点不同,此处我们对所有PLs使用单一检查点。此外,我们在训练和推理中去除了用于指定PL的任务控制前缀。测试集上的结果如下:
模型 |
Ruby |
Javascript |
Go |
Python |
Java |
PHP |
总体 |
Seq2Seq |
9.64 |
10.21 |
13.98 |
15.93 |
15.09 |
21.08 |
14.32 |
Transformer |
11.18 |
11.59 |
16.38 |
15.81 |
16.26 |
22.12 |
15.56 |
RoBERTa |
11.17 |
11.90 |
17.72 |
18.14 |
16.47 |
24.02 |
16.57 |
CodeBERT |
12.16 |
14.90 |
18.07 |
19.06 |
17.65 |
25.16 |
17.83 |
PLBART |
14.11 |
15.56 |
18.91 |
19.30 |
18.45 |
23.58 |
18.32 |
CodeT5-small |
14.87 |
15.32 |
19.25 |
20.04 |
19.92 |
25.46 |
19.14 |
CodeT5-base |
15.24 |
16.16 |
19.56 |
20.01 |
20.31 |
26.03 |
19.55 |
CodeT5-base-multi-sum |
15.24 |
16.18 |
19.95 |
20.42 |
20.26 |
26.10 |
19.69 |
引用
@inproceedings{
wang2021codet5,
title={CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation},
author={Yue Wang, Weishi Wang, Shafiq Joty, Steven C.H. Hoi},
booktitle={Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing, EMNLP 2021},
year={2021},
}