SkCoder:一种基于草图(Sketch)的自动代码生成方法
SkCoder: A Sketch-based Approach for Automatic Code Generation
AI总结:
本文提出基于草图的代码生成方法SkCoder,通过检索相似代码并提取草图再编辑成目标代码,在多个数据集上显著优于现有基线并提升多种模型性能。
摘要:
近年来,深度学习技术在自动代码生成中取得了巨大成功。受代码复用启发,一些研究者提出了基于复制的方法,可以从相似代码片段中复制内容以获得更好的性能。在实践中,人类开发者会识别相似代码中与其需求相关的内容,这可以被视为代码草图(code sketch)。该草图会被进一步编辑成所需的代码。然而,现有的基于复制的方法忽略了代码草图,倾向于不加必要修改地重复相似代码,这会导致生成错误的结果。在本文中,我们提出了一种基于草图的代码生成方法,名为 SkCoder,以模仿开发者的代码复用行为。给定自然语言需求,SkCoder 检索相似代码片段,提取相关部分作为代码草图,并将该草图编辑成所需的代码。我们的动机是,提取的草图为模型提供了一个结构良好的模式,告诉模型“如何编写”。后期的编辑进一步为草图添加特定于需求的细节,并输出完整的代码。我们在两个公开数据集和本工作收集的一个新数据集上进行了实验。我们使用 5 个广泛使用的指标将我们的方法与 20 个基线进行了比较。实验结果表明:(1)SkCoder 能够生成更正确的程序,并在三个数据集上分别比最先进的 CodeT5-base 高出 30.30%、35.39% 和 29.62%。(2)我们的方法对多种代码生成模型均有效,并在 Pass@1 上使它们提高了高达 120.1%。(3)我们研究了三种合理的代码草图并讨论了草图的重要性。(4)我们手动评估了生成的代码,并在三个方面证明了我们的 SkCoder 的优越性。
英文摘要:
Recently, deep learning techniques have shown great success in automatic code generation. Inspired by the code reuse, some researchers propose copy-based approaches that can copy the content from similar code snippets to obtain better performance. Practically, human developers recognize the content in the similar code that is relevant to their needs, which can be viewed as a code sketch. The sketch is further edited to the desired code. However, existing copy-based approaches ignore the code sketches and tend to repeat the similar code without necessary modifications, which leads to generating wrong results. In this paper, we propose a sketch-based code generation approach named SkCoder to mimic developers' code reuse behavior. Given a natural language requirement, SkCoder retrieves a similar code snippet, extracts relevant parts as a code sketch, and edits the sketch into the desired code. Our motivations are that the extracted sketch provides a well-formed pattern for telling models "how to write". The post-editing further adds requirement-specific details to the sketch and outputs the complete code. We conduct experiments on two public datasets and a new dataset collected by this work. We compare our approach to 20 baselines using 5 widely used metrics. Experimental results show that (1) SkCoder can generate more correct programs, and outperforms the state-of-the-art - CodeT5-base by 30.30%, 35.39%, and 29.62% on three datasets. (2) Our approach is effective to multiple code generation models and improves them by up to 120.1% in Pass@1. (3) We investigate three plausible code sketches and discuss the importance of sketches. (4) We manually evaluate the generated code and prove the superiority of our SkCoder in three aspects.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。