Skip to main content
QUICK REVIEW

[Paper Review] Retrieval-Based Neural Code Generation

Shirley Anugrah Hayati, Raphaël Olivier|arXiv (Cornell University)|Aug 29, 2018
Software Engineering ResearchComputer Science19 references8 citations
TL;DR

This paper proposes ReCode, a retrieval-based neural code generation model that improves syntactic code generation by retrieving and reusing action subtree templates from similar training examples. By leveraging dynamic-programming-based sentence alignment and n-gram subtree retrieval, ReCode boosts performance by up to +2.6 BLEU on two code generation tasks, enhancing generalization for rare or complex code patterns.

ABSTRACT

In models to generate program source code from natural language, representing this code in a tree structure has been a common approach. However, existing methods often fail to generate complex code correctly due to a lack of ability to memorize large and complex structures. We introduce ReCode, a method based on subtree retrieval that makes it possible to explicitly reference existing code examples within a neural code generation model. First, we retrieve sentences that are similar to input sentences using a dynamic-programming-based sentence similarity scoring method. Next, we extract n-grams of action sequences that build the associated abstract syntax tree. Finally, we increase the probability of actions that cause the retrieved n-gram action subtree to be in the predicted code. We show that our approach improves the performance on two code generation tasks by up to +2.6 BLEU.

Motivation & Objective

  • To address the challenge of generating complex, syntactically correct code for rare or low-frequency natural language descriptions in neural code generation.
  • To overcome limitations of existing tree-based models that struggle with memorizing large or complex code structures.
  • To adapt retrieval-based methods from neural machine translation to code generation by exploiting structural similarity in abstract syntax trees (ASTs).
  • To improve generalization and accuracy for rare or complex code patterns through explicit reuse of retrieved code templates.

Proposed method

  • Retrieves the top M training examples most similar to the input natural language description using edit distance-based sentence similarity scoring.
  • Extracts n-gram action subtrees from the ASTs of retrieved code examples, representing reusable structural patterns in code generation.
  • Applies a dynamic programming-based alignment method to substitute words in retrieved action sequences with corresponding terms from the input NL description, enabling partial match handling.
  • Modifies the decoding process to increase the probability of actions that lead to the inclusion of retrieved n-gram action subtrees in the predicted AST.
  • Uses a sequence-to-sequence model with action embeddings, context vectors, parent feeding, and a copy mechanism to generate ASTs via preorder traversal.
  • Integrates retrieval guidance into the attention mechanism by boosting probabilities of actions that align with retrieved subtrees, ensuring syntactic correctness and structural relevance.

Experimental results

Research questions

  • RQ1Can retrieval of structural code patterns improve neural code generation for rare or complex natural language descriptions?
  • RQ2How can subtree-level retrieval be effectively applied to AST-based code generation, given the lack of linear structure in trees?
  • RQ3Can dynamic programming-based sentence alignment improve retrieval performance for imperfect or partial matches in code generation?
  • RQ4To what extent does incorporating retrieved action subtrees enhance BLEU scores and syntactic correctness in code generation?
  • RQ5How does retrieval-based generation compare to state-of-the-art models in handling rare or complex code patterns?

Key findings

  • ReCode improves code generation performance by up to +2.6 BLEU points on two benchmark code generation tasks, demonstrating significant gains over prior state-of-the-art models.
  • The model successfully generates correct code for complex phrases that previous models fail to handle, such as correctly predicting the action timesince(d, now, reversed) in one example.
  • In cases where the reference code includes rare or complex constructs, ReCode outperforms the baseline model (YN17) by generating more accurate and structurally correct code despite not matching the reference exactly.
  • The retrieval mechanism enables better handling of complex code structures, as shown in the HS dataset example where ReCode correctly infers the overload attribute, while the baseline model fails to do so.
  • Despite improvements, ReCode occasionally fails to copy variables correctly or cast types properly, indicating limitations in copy mechanism generalization.
  • The method effectively generalizes to rare or out-of-vocabulary terms by reusing structural patterns from similar training examples, reducing reliance on memorization of rare sequences.

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.