Skip to main content
QUICK REVIEW

[Paper Review] BERT2Code: Can Pretrained Language Models be Leveraged for Code Search?

Abdullah Al Ishtiaq, Masum Hasan|arXiv (Cornell University)|Apr 16, 2021
Software Engineering ResearchComputer Science36 references6 citations
TL;DR

BERT2Code proposes a lightweight two-layer neural network that leverages pretrained sentence and code embedding models—SBERT, Code2Vec, and CodeBERT—for semantic code search. It achieves 15.478% MRR on code search by learning cross-embedding space relationships, but finds that code embedding quality is the primary bottleneck for performance.

ABSTRACT

Millions of repetitive code snippets are submitted to code repositories every day. To search from these large codebases using simple natural language queries would allow programmers to ideate, prototype, and develop easier and faster. Although the existing methods have shown good performance in searching codes when the natural language description contains keywords from the code, they are still far behind in searching codes based on the semantic meaning of the natural language query and semantic structure of the code. In recent years, both natural language and programming language research communities have created techniques to embed them in vector spaces. In this work, we leverage the efficacy of these embedding models using a simple, lightweight 2-layer neural network in the task of semantic code search. We show that our model learns the inherent relationship between the embedding spaces and further probes into the scope of improvement by empirically analyzing the embedding methods. In this analysis, we show that the quality of the code embedding model is the bottleneck for our model's performance, and discuss future directions of study in this area.

Motivation & Objective

  • To investigate whether pretrained language models can be effectively leveraged for semantic code search.
  • To evaluate the performance of a lightweight neural network in mapping natural language queries to source code using pre-trained embeddings.
  • To identify the limitations of current code embedding models in enabling high-accuracy semantic code search.
  • To empirically analyze the quality of sentence and code embeddings in capturing semantic similarity.
  • To highlight the need for more generalizable and higher-quality code embedding models in future research.

Proposed method

  • Fine-tunes Sentence-BERT (SBERT) for generating dense vector representations of natural language queries.
  • Utilizes Code2Vec and CodeBERT to generate code embeddings from source code snippets.
  • Employs a two-layer feedforward neural network to map sentence embeddings to code embeddings in a shared latent space.
  • Trains the model using contrastive learning with triplet loss to maximize semantic similarity between matching query-code pairs.
  • Evaluates model performance using Mean Reciprocal Rank (MRR) on the DeepCom dataset.
  • Performs manual semantic similarity scoring on 300 query-code pairs and correlates them with L2 distances in embedding spaces.

Experimental results

Research questions

  • RQ1Can pretrained language models like SBERT and Code2Vec be effectively combined for semantic code search?
  • RQ2How well does a lightweight neural network map natural language query embeddings to code embeddings?
  • RQ3What is the correlation between human-annotated semantic similarity and embedding space distances?
  • RQ4To what extent do the quality and generalization capability of code embeddings limit the performance of semantic code search models?
  • RQ5Are current code embedding models sufficient to fully exploit the potential of cross-embedding mapping architectures?

Key findings

  • BERT2Code achieves a Mean Reciprocal Rank (MRR) of 15.478% on the DeepCom dataset, demonstrating the feasibility of using pretrained embeddings for semantic code search.
  • The correlation between manual semantic similarity scores and L2 distances in the NL embedding space is -0.772 (p < 10^-6), indicating strong alignment between human judgment and embedding similarity.
  • The correlation between manual semantic similarity and code embedding distances is only -0.444 (p < 10^-6), suggesting that code embeddings are less reliable than sentence embeddings in capturing semantic meaning.
  • Statistical analysis confirms that the low correlation for code embeddings is not due to random chance, rejecting the null hypothesis with high confidence.
  • The study identifies the quality of code embedding models as the primary bottleneck for performance, indicating that better code embeddings are essential for future improvements.
  • Empirical analysis shows that current models like Code2Vec and CodeBERT fail to generalize well in semantic code search tasks, especially when semantic meaning is critical.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.