TransformCode: A Contrastive Learning Framework for Code Embedding via Subtree Transformation

📄 arXiv: 2311.08157v2 📥 PDF

作者: Zixiang Xian, Rubing Huang, Dave Towey, Chunrong Fang, Zhenyu Chen

分类: cs.SE, cs.AI

发布日期: 2023-11-10 (更新: 2024-04-24)

备注: To be published in IEEE Transactions on Software Engineering

DOI: 10.1109/TSE.2024.3393419


💡 一句话要点

提出TransformCode框架以解决代码嵌入的学习效率问题

🎯 匹配领域: 支柱二:RL算法与架构 (RL & Architecture)

关键词: 代码嵌入 对比学习 抽象语法树 数据增强 软件工程 预训练模型 机器学习

📋 核心要点

  1. 现有的预训练模型在代码嵌入学习中存在训练成本高、对标注数据依赖性强等问题,限制了其在软件工程中的应用。
  2. TransformCode框架通过对比学习方式学习代码嵌入,采用抽象语法树变换技术生成多样化样本,具有编码器和语言无关性。
  3. 在多个代码相关任务的实验中,TransformCode展示了优于SourcererCC、Code2vec和InferCode等现有方法的效果。

📝 摘要(中文)

人工智能(AI)已在软件工程(SE)中带来了革命性的变化,提高了软件开发效率。现有的预训练模型(PTMs)在处理单个代码标记时存在训练成本高、对标注数据依赖性强等问题。本文提出了TransformCode,一个通过对比学习方式学习代码嵌入的新框架。该框架具有编码器无关性和语言无关性,能够利用任何编码器模型并处理任何编程语言。此外,本文还提出了一种新的数据增强技术——抽象语法树(AST)变换,通过对原始代码片段进行语法和语义变换,生成更多样化和鲁棒的样本。我们的框架在多个代码相关任务上进行了评估,展示了其相较于现有方法的有效性和优越性。

🔬 方法详解

问题定义:本文旨在解决现有预训练模型在代码嵌入学习中的高成本和对标注数据的强依赖性问题。现有方法在处理代码时往往只能依赖于单一的代码标记,导致灵活性不足。

核心思路:TransformCode框架通过对比学习的方式来学习代码嵌入,利用抽象语法树(AST)变换技术生成多样化的训练样本,从而提高模型的鲁棒性和适应性。

技术框架:该框架是编码器无关和语言无关的,能够与任何编码器模型配合使用,并支持多种编程语言。主要流程包括数据预处理、AST变换、对比学习和模型训练等模块。

关键创新:TransformCode的核心创新在于引入了AST变换作为数据增强技术,能够通过语法和语义的变换生成多样化样本,显著提升了对比学习的效果。与现有方法相比,该框架在灵活性和适应性上具有本质区别。

关键设计:框架设计中,损失函数采用对比损失,能够有效地拉近相似样本的嵌入距离,并拉远不相似样本的嵌入距离。模型参数的调整也根据计算资源进行灵活配置,以适应不同的应用场景。

🖼️ 关键图片

fig_0

📊 实验亮点

在多个代码相关任务的实验中,TransformCode框架表现出色,相较于SourcererCC、Code2vec和InferCode等基线方法,提升幅度达到XX%,显示出其在代码嵌入学习中的有效性和优越性。

🎯 应用场景

TransformCode框架在代码克隆检测、代码分类等下游任务中具有广泛的应用潜力。其灵活性和高效性使得开发者能够在不同的编程语言和任务中快速部署和应用该框架,从而提升软件开发的效率和质量。未来,该框架可能会推动更多基于AI的代码分析和生成工具的发展。

📄 摘要(原文)

Artificial intelligence (AI) has revolutionized software engineering (SE) by enhancing software development efficiency. The advent of pre-trained models (PTMs) leveraging transfer learning has significantly advanced AI for SE. However, existing PTMs that operate on individual code tokens suffer from several limitations: They are costly to train and fine-tune; and they rely heavily on labeled data for fine-tuning on task-specific datasets. In this paper, we present TransformCode, a novel framework that learns code embeddings in a contrastive learning manner. Our framework is encoder-agnostic and language-agnostic, which means that it can leverage any encoder model and handle any programming language. We also propose a novel data-augmentation technique called abstract syntax tree (AST) transformation, which applies syntactic and semantic transformations to the original code snippets, to generate more diverse and robust samples for contrastive learning. Our framework has several advantages over existing methods: (1) It is flexible and adaptable, because it can easily be extended to other downstream tasks that require code representation (such as code-clone detection and classification); (2) it is efficient and scalable, because it does not require a large model or a large amount of training data, and it can support any programming language; (3) it is not limited to unsupervised learning, but can also be applied to some supervised learning tasks by incorporating task-specific labels or objectives; and (4) it can also adjust the number of encoder parameters based on computing resources. We evaluate our framework on several code-related tasks, and demonstrate its effectiveness and superiority over the state-of-the-art methods such as SourcererCC, Code2vec, and InferCode.