一句话:用GAN将代码生成与代码搜索互为生成器与判别器,通过对抗训练实现相互提升。
从自然语言描述自动生成代码是软件工程的长期目标,现有代码生成与代码搜索方法效果有限。本文洞察二者可互补:生成器需要生成代码的质量反馈,可由搜索器提供;搜索器需要更多训练数据,可由生成器扩充。为此提出基于生成对抗网络(GAN)的方法,将代码生成作为生成器、代码搜索作为判别器,并进行定制化设计,通过对抗训练实现相互提升。在八种设置下评估,两者性能均持续显著提升。例如以NatGen为生成器、GraphCodeBERT为判别器,在大规模Python数据集上,代码生成CodeBLEU提升32%,代码搜索MRR提升12%。
Automatically transforming developers' natural language descriptions into source code has been a longstanding goal in software engineering research. Two types of approaches have been proposed in the literature to achieve this: code generation, which involves generating a new code snippet, and code search, which involves reusing existing code. However, despite existing efforts, the effectiveness of the state-of-the-art techniques remains limited. To seek for further advancement, our insight is that code generation and code search can help overcome the limitation of each other: the code generator can benefit from feedback on the quality of its generated code, which can be provided by the code searcher, while the code searcher can benefit from the additional training data augmented by the code generator to better understand code semantics. Drawing on this insight, we propose a novel approach that combines code generation and code search techniques using a generative adversarial network (GAN), enabling mutual improvement through the adversarial training. Specifically, we treat code generation and code search as the generator and discriminator in the GAN framework, respectively, and incorporate several customized designs for our tasks. We evaluate our approach in eight different settings, and consistently observe significant performance improvements for both code generation and code search. For instance, when using NatGen, a state-of-the-art code generator, as the generator and GraphCodeBERT, a state-of-the-art code searcher, as the discriminator, we achieve a 32% increase in CodeBLEU score for code generation, and a 12% increase in mean reciprocal rank for code search on a large-scale Python dataset, compared to their original performances.
@article{paperbot1851,
title = {Two Birds with One Stone: Boosting Code Generation and Code Search via a Generative Adversarial Network},
author = {Shangwen Wang and Bo Lin and Zhensu Sun and Ming Wen and Yepang Liu and Yan Lei and Xiaoguang Mao},
journal = {Proceedings of the ACM on Programming Languages},
volume = {7},
number = {OOPSLA2},
year = {2023},
doi = {10.1145/3622815}
}