Skip to main content
QUICK REVIEW

[论文解读] Towards Understanding What Code Language Models Learned

Toufique Ahmed, Dian Yu|arXiv (Cornell University)|Jun 20, 2023
Topic ModelingComputer Science被引用 3
一句话总结

本文研究了代码预训练语言模型是否在表面模式之外学习到计算语义,通过测试其在块交换和操作数重排等语义保持变换后重建代码的能力。结果显示,GraphCodeBERT 和 CodeBERT 等模型在变换后的代码上仍保持高准确率(F-score >0.73),优于基于频率的猜测,表明其仅从形式中即可获得稳健的语义理解。

ABSTRACT

Pre-trained language models are effective in a variety of natural language tasks, but it has been argued their capabilities fall short of fully learning meaning or understanding language. To understand the extent to which language models can learn some form of meaning, we investigate their ability to capture semantics of code beyond superficial frequency and co-occurrence. In contrast to previous research on probing models for linguistic features, we study pre-trained models in a setting that allows for objective and straightforward evaluation of a model's ability to learn semantics. In this paper, we examine whether such models capture the semantics of code, which is precisely and formally defined. Through experiments involving the manipulation of code fragments, we show that code pre-trained models of code learn a robust representation of the computational semantics of code that goes beyond superficial features of form alone

研究动机与目标

  • 探究预训练代码语言模型是否在词汇和句法模式之外学习到计算语义。
  • 评估模型是否能泛化到语义等价但句法不同的代码形式。
  • 确定模型预测是否由标记频率记忆驱动,还是由更深层的语义理解驱动。
  • 使用形式化程序变换,对代码模型中的语义鲁棒性进行客观、自动化的评估。

提出的方法

  • 应用语义保持的代码变换(如块交换和操作数重排),同时保留计算语义。
  • 使用 F-score 和对数似然,测量模型在变换前后对掩码操作符重建的性能。
  • 通过蒙特卡洛模拟结合基于频率的随机猜测,建立比较基线。
  • 分析语义等价与句法相似但语义不同的代码对之间的嵌入空间距离。
  • 在受限上下文和变量重命名条件下评估模型,以测试其对句法不自然性的鲁棒性。
  • 直接通过 API 使用预训练模型,不进行微调,以评估其内在语义编码能力。

实验结果

研究问题

  • RQ1预训练代码语言模型在经过语义保持的句法变换后,能否正确重建代码?
  • RQ2模型在变换后代码上的表现是否显著高于仅基于标记频率的预期?
  • RQ3模型预测是由表面模式的记忆驱动,还是由更深层的语义表征驱动?
  • RQ4嵌入表示如何反映代码中的语义等价性与句法相似性?

主要发现

  • GraphCodeBERT 和 CodeBERT 等模型在块交换后对掩码操作符预测的 F-score 仍保持在 0.73 以上,表明其具有稳健的语义理解能力。
  • 对于 "<=" 和 ">=" 操作符,模型的 F-score 在变换前为 0.10,但在 24 次中有 23 次仍显著优于基于频率的猜测(p < 0.01)。
  • 变换后,预测操作符的对数似然显著提高(例如,GraphCodeBERT 从 0.17 提升至 0.35),表明置信度提升,且不依赖于词汇频率。
  • 语义等价的代码对嵌入距离最小,而句法相似但语义不同的对距离更远,证实了表示中存在语义编码。
  • 即使在变量重命名和条件语句重新排列等使代码更不自然的变换下,模型仍保持高预测准确率。
  • 模型性能始终显著优于随机频率基线猜测,表明其捕捉的是功能语义,而非记忆标记共现模式。

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。