Skip to main content
QUICK REVIEW

[Paper Review] InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees

Nghi D. Q. Bui, Yijun Yu|arXiv (Cornell University)|Dec 13, 2020
Software Engineering ResearchComputer Science65 references9 citations
TL;DR

InferCode proposes a self-supervised learning method for code representation by training a Tree-based Convolutional Neural Network (TBCNN) to predict subtrees within abstract syntax trees (ASTs) of code, enabling transferable, task-agnostic embeddings. Trained on large-scale unlabeled Java code, the model achieves state-of-the-art performance across diverse downstream tasks like code clustering, clone detection, and cross-language search, outperforming prior methods such as code2vec and ASTNN with significant margins.

ABSTRACT

Building deep learning models on source code has found many successful software engineering applications, such as code search, code comment generation, bug detection, code migration, and so on. Current learning techniques, however, have a major drawback that these models are mostly trained on datasets labeled for particular downstream tasks, and code representations may not be suitable for other tasks. While some techniques produce representations from unlabeled code, they are far from satisfactory when applied to downstream tasks. Although certain techniques generate representations from unlabeled code when applied to downstream tasks they are far from satisfactory. This paper proposes InferCode to overcome the limitation by adapting the self-supervised learning mechanism to build source code model. The key novelty lies in training code representations by predicting automatically identified subtrees from the context of the ASTs. Subtrees in ASTs are treated with InferCode as the labels for training code representations without any human labeling effort or the overhead of expensive graph construction, and the trained representations are no longer tied to any specific downstream tasks or code units. We trained an InferCode model instance using the Tree-based CNN as the encoder of a large set of Java code and applied it to downstream unsupervised tasks such as code clustering, code clone detection, cross-language code search or reused under a transfer learning scheme to continue training the model weights for supervised tasks such as code classification and method name prediction. Compared to previous code learning techniques applied to the same downstream tasks, such as Code2Vec, Code2Seq, ASTNN, higher performance results are achieved using our pre-trained InferCode model with a significant margin for most tasks including those involving different programming languages.

Motivation & Objective

  • To address the limitation of task-specific code representations that require manual labeling and lack generalizability across software engineering tasks.
  • To develop a self-supervised method that learns code representations from unlabeled source code without human-annotated labels or complex graph construction.
  • To enable flexible, reusable code embeddings for any code unit parseable into an AST, supporting diverse downstream tasks.
  • To improve transferability and performance of code representations across programming languages and tasks compared to existing supervised or weakly supervised approaches.
  • To explore whether subtree prediction in ASTs can serve as an effective pretext task for learning robust, general-purpose code embeddings.

Proposed method

  • The model uses a Tree-based Convolutional Neural Network (TBCNN) as the encoder to map abstract syntax trees (ASTs) into dense code vectors.
  • The pretext task involves predicting randomly sampled subtrees from the AST context, using the code vector as a context representation to reconstruct the subtree.
  • Subtrees are automatically identified from ASTs without human labeling, serving as self-generated labels for pre-training.
  • The model is pre-trained on a large-scale dataset of unlabeled Java code, learning structural and semantic patterns from the ASTs.
  • After pre-training, the encoder can be fine-tuned for supervised tasks or used directly for unsupervised tasks like code clustering and search.
  • The approach avoids expensive graph construction and is designed to generalize across programming languages due to the universal parseability of ASTs.

Experimental results

Research questions

  • RQ1Can subtree prediction in ASTs serve as an effective self-supervised pretext task for learning general-purpose code representations?
  • RQ2Does the proposed self-supervised method produce code embeddings that generalize better across diverse downstream tasks than task-specific or weakly supervised methods?
  • RQ3How does the performance of InferCode compare to state-of-the-art models like code2vec, code2seq, and ASTNN on code clustering, clone detection, and cross-language search?
  • RQ4Can the pre-trained InferCode model be effectively fine-tuned for supervised tasks such as code classification and method name prediction?
  • RQ5Is the model's performance robust across different programming languages, especially in cross-language code search?

Key findings

  • InferCode outperforms code2vec, code2seq, and ASTNN on code clustering (ARI: 0.70 vs. 0.58), achieving a 17% relative improvement.
  • On code clone detection (BigCloneBench), InferCode achieves a precision of 0.90, significantly higher than code2vec (0.81) and code2seq (0.45).
  • For cross-language code search (Rosetta Stone), InferCode achieves an MRR of 0.57, outperforming code2vec (0.41) and code2seq (0.32).
  • Fine-tuning the pre-trained InferCode model on supervised tasks leads to better performance than training from scratch, demonstrating strong transferability.
  • The model's performance is robust across different programming languages, indicating generalization beyond a single language.
  • Subtree prediction as a pretext task yields better representations than method name prediction, as the latter forces models to learn spurious correlations from similar names with different implementations.

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.