Skip to main content
QUICK REVIEW

[Paper Review] Learning Semantic Vector Representations of Source Code via a Siamese Neural Network

David Wehr, Halley Fede|arXiv (Cornell University)|Apr 26, 2019
Software Engineering ResearchComputer Science20 references3 citations
TL;DR

This paper proposes a Siamese RNN model that learns semantic vector representations of Python source code by training on functionally equivalent code snippets from programming competitions. The model significantly outperforms a bag-of-tokens baseline, achieving AUC scores of 0.98 and 0.89 on test sets, demonstrating strong semantic similarity learning beyond syntax.

ABSTRACT

The abundance of open-source code, coupled with the success of recent advances in deep learning for natural language processing, has given rise to a promising new application of machine learning to source code. In this work, we explore the use of a Siamese recurrent neural network model on Python source code to create vectors which capture the semantics of code. We evaluate the quality of embeddings by identifying which problem from a programming competition the code solves. Our model significantly outperforms a bag-of-tokens embedding, providing promising results for improving code embeddings that can be used in future software engineering tasks.

Motivation & Objective

  • To develop a deep learning model that learns semantic vector representations of source code that capture functional equivalence beyond syntactic structure.
  • To reduce reliance on expensive labeled data by leveraging large-scale unlabeled code in a pre-training autoencoder phase.
  • To improve code clone detection, bug detection, and code recommendation by learning general-purpose, semantically meaningful code embeddings.
  • To evaluate the model’s ability to generalize to unseen programming problems using real-world code from online competitions.

Proposed method

  • A Siamese recurrent neural network (RNN) architecture is used to embed pairs of code snippets, learning to minimize distance between semantically equivalent code.
  • The model is pre-trained on a large corpus of unlabeled Python code using an autoencoder objective to learn general code representations.
  • Labeled training data consists of multiple solutions to the same programming competition problem, treated as positive pairs.
  • The final model is fine-tuned on problem-specific positive pairs, with loss optimized via contrastive learning to enforce semantic similarity.
  • Token-level embeddings are learned using a shared RNN encoder, with output vectors used to compute cosine similarity for semantic matching.
  • A bag-of-words baseline model is used for comparison, representing code as a 1772-dimensional frequency histogram of tokens.

Experimental results

Research questions

  • RQ1Can a Siamese RNN model learn semantic code embeddings that capture functional equivalence even when code syntax differs?
  • RQ2How well does the model generalize to unseen programming problems not present in the training data?
  • RQ3To what extent does pre-training on unlabeled code improve the quality of learned code embeddings?
  • RQ4How does the model’s performance compare to a simple syntax-based baseline in identifying functionally equivalent code?

Key findings

  • The proposed Siamese RNN model achieved an AUC of 0.98 on the training dataset $S_{1}^{(test)}$, significantly outperforming the bag-of-words baseline, which scored 0.82.
  • On the unseen test set $S_{2}$, the model achieved an AUC of 0.89, demonstrating strong generalization capability despite being trained on only 44 unique problems.
  • Error analysis revealed that misclassifications were clustered among semantically similar problems, suggesting the model captures meaningful semantic relationships.
  • The model's performance indicates that learning from functionally equivalent code snippets enables robust semantic representation, even across syntactic variations.
  • The use of pre-training on unlabeled code reduced dependency on costly labeled data, supporting scalability and transferability.

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.