一句话:评估代码嵌入模型的上下文性,发现静态与上下文化模型均不适配代码的混合多义性,并提出混合嵌入模型 Hycode。
机器学习模型已被广泛用于软件工程任务,代码嵌入也从静态模型发展到上下文化模型。上下文化嵌入可处理自然语言中的多义性,但源代码具有“混合多义性”:部分 token 和语句在不同上下文中含义不同,而关键字、分隔符、运算符等则保持不变。本文研究静态与上下文化嵌入何者更适合源代码的这一特性。作者在 10,222 个 Java 项目、超过 1400 万个方法的样本上,评估了 12 种基于序列、树和图的流行嵌入模型,并将自然语言中的上下文性度量适配到代码结构。实验发现,上下文性更高的模型能帮助缺陷检测模型优于静态模型;但静态与上下文化嵌入均不能很好适配源代码的混合多义性。为此,作者提出混合嵌入模型 Hycode,以更好地匹配该特性。
Advanced machine learning models (ML) have been successfully leveraged in several software engineering (SE) applications. The existing SE techniques have used the embedding models ranging from static to contextualized ones to build the vectors for program units. The contextualized vectors address a phenomenon in natural language texts called polysemy, which is the coexistence of different meanings of a word/phrase. However, due to different nature, program units exhibit the nature of mixed polysemy. Some code tokens and statements exhibit polysemy while other tokens (e.g., keywords, separators, and operators) and statements maintain the same meaning in different contexts. A natural question is whether static or contextualized embeddings fit better with the nature of mixed polysemy in source code. The answer to this question is helpful for the SE researchers in selecting the right embedding model. We conducted experiments on 12 popular sequence-/tree-/graph-based embedding models and on the samples of a dataset of 10,222 Java projects with +14M methods. We present several contextuality evaluation metrics adapted from natural-language texts to code structures to evaluate the embeddings from those models. Among several findings, we found that the models with higher contextuality help a bug detection model perform better than the static ones. Neither static nor contextualized embedding models fit well with the mixed polysemy nature of source code. Thus, we develop Hycode, a hybrid embedding model that fits better with the nature of mixed polysemy in source code.