Source-linked AI summary

A Convolutional Attention Network for Extreme Summarization of Source Code

Miltiadis Allamanis, Hao Peng, Charles Sutton

arXiv:1602.03001v2cs.LGcs.CLcs.SE

TL;DR

The paper addresses the challenge of learning attention over variable-length, structured source code, including tokens absent from training vocabularies. It introduces convolutional attention with a copy mechanism for generating concise method-name summaries, and reports the best performance among competing methods on real-world source code.

  • Problem

    Previous attention architectures are not constructed to learn translation-invariant features specifically, while source code also contains out-of-vocabulary vocabulary that complicates summarization.

  • Method

    A convolutional attentional network detects context-dependent code patterns and combines vocabulary prediction with direct copying of input subtokens.

  • Results

    The model achieves the best performance among competing methods on real-world source code across 10 popular Java projects.

  • Takeaways & Limitations

    Extreme code summarization is presented as a step toward machine-learning representations that support probabilistic reasoning about code and software-engineering tools.

Abstract

from arXiv · show

Attention mechanisms in neural networks have proved useful for problems in which the input and output do not have fixed dimension. Often there exist features that are locally translation invariant and would be valuable for directing the model's attention, but previous attentional architectures are not constructed to learn such features specifically. We introduce an attentional neural network that employs convolution on the input tokens to detect local time-invariant and long-range topical attention features in a context-dependent way. We apply this architecture to the problem of extreme summarization of source code snippets into short, descriptive function name-like summaries. Using those features, the model sequentially generates a summary by marginalizing over two attention mechanisms: one that predicts the next summary token based on the attention weights of the input tokens and another that is able to copy a code token as-is directly into the summary. We demonstrate our convolutional attention neural network's performance on 10 popular Java projects showing that it achieves better performance compared to previous attentional mechanisms.

1. Introduction

The paper introduces convolutional attention for extreme source-code summarization, where short descriptive method names must be predicted from code tokens alone. It also addresses source-code vocabulary variation through a mechanism that copies important input tokens directly into summaries.

  • Structured prediction with variable-length inputs and outputs makes it difficult to determine relevant input locations for each generated output.
  • Previous attention architectures are not specifically designed to learn translation-invariant features that can identify useful attention locations.
  • The proposed model embeds a convolutional network within attention to detect local patterns and long-range features marking interesting code locations.
  • Extreme code summarization predicts a short, descriptive name for a source-code snippet using solely its tokens, supporting code understanding and search.
  • A copy mechanism identifies important out-of-vocabulary code tokens, allowing the decoder to copy them directly into the output sequence.
  • The paper evaluates four competing algorithms on real-world data and reports an advantage over standard attentional mechanisms.

2. Convolutional Attention Model

The model computes context-dependent convolutional attention features over code subtokens, then sequentially generates method-name subtokens with recurrent state. A second attention mechanism interpolates vocabulary prediction with direct copying from the input.

  • The encoder-decoder receives code subtokens and generates a concise method name as a sequence of subtokens.
  • 2.1. Learning Attention Features: Convolutions over code embeddings produce position- and context-dependent attention features, gated by the previous decoder state.
  • 2.1. Learning Attention Features: Padding and narrow convolutions yield an attention vector with exactly one component for each input-code position.
  • 2.2. Computing Attention Weights: A convolution followed by SOFTMAX converts attention features into non-negative, unit-norm weights over input positions.
  • 2. Convolutional Attention Model: The decoder predicts subtokens sequentially, updating its recurrent state from previously generated output embeddings.
  • 2.3. Copy Convolutional Attentional Model: The copy model uses separate attention weights and a meta-attention weight λ to interpolate vocabulary prediction with copying an input subtoken.

3. Evaluation

The evaluation compares code-summary models across Java projects using F1, exact match, and OOV accuracy, supplemented by shuffled-input and qualitative analyses. Convolutional attention models perform best overall, while copying improves OOV prediction and captures long-range code features.

  • Evaluation setup: The dataset uses developer-selected method names as short summaries of method bodies from open-source Java projects.Projects are trained and tested separately because their domains vary widely and cross-project training performed worse.
  • Evaluation setup: Models are evaluated with exact match and per-subtoken F1 at ranks 1 and 5, with tf-idf and standard attention as comparison algorithms.The study also measures OOV accuracy and tests shuffled subtokens to examine topical feature detection.
  • Quantitative evaluation: Convolutional attention and copy attention perform best, while standard attention performs worse than tf-idf; convolutional models achieve much higher precision despite similar exact match.The copy mechanism provides a good F1 improvement at rank 1 and a larger improvement at rank 5.
  • Quantitative evaluation: 10.5% OOV accuracy at rank 1 and 19.4% at rank 5 show that copy attention can predict unseen name subtokens.Across the test set, 4.4% of method-name subtokens are OOV, while tf-idf and standard attention have zero OOV accuracy.
  • Feature analysis: After shuffling input subtokens, copy attention remains within +1% F1 of tf-idf, whereas standard attention falls to 26.2% from 41.8%.This result suggests that the standard biRNN fails to capture long-range topical attention features.
  • Qualitative evaluation: Qualitative examples show copy attention combining time-invariant and topical features, focusing attention to copy relevant subtokens while ignoring project-specific noise.Examples include recognizing boolean-style prefixes, unseen abstractions, and reverseRange from code structure even without matching summary tokens.

4. Related Work

The paper situates its approach among convolutional models for text and code, attention mechanisms, pointer networks, and probabilistic software-engineering models. Unlike these related approaches, its examples focus on naming diverse real-world source code.

  • Convolutional and attentional models: Convolutional neural networks have been used for image classification and text classification, while this work applies convolution within an attentional mechanism.Related work includes convolutional document representations and sentence summarization, but not convolution for attention values.
  • Paper-specific evaluation context: The paper evaluates copy_attention using visualizations of method-name generation, including hand-picked short methods from the libgdx Android/Java graphics-library test set.The table emphasizes challenges the model handles or fails while receiving only method bodies, not signatures.
  • Convolutional and attentional models: Prior attention work addresses text and images without convolutional attention, while pointer networks use an RNN to provide attention for copying.The paper positions its copy mechanism as similar to pointer networks but distinguishes the attention architecture.
  • Software-engineering applications: Probabilistic software-engineering research has studied token- and syntax-level code models, code translation, comments, classification, and program embeddings.These related efforts generally do not consider naming, while the paper targets names for diverse real-world code.

5. Discussion & Conclusions

The paper presents extreme code summarization as an initial step toward machine-learning representations that can reason probabilistically about source code. Its convolutional attentional model learns localized and long-range features and achieves the best performance among competing methods on real-world source code.

  • Discussion and conclusions: Extreme code summarization is framed as a first step toward representations that support probabilistic reasoning about code for software construction and maintenance tools.The broader motivation is to model and understand source-code artifacts through machine learning.
  • Discussion and conclusions: The convolutional attentional model learns both long-range features and localized patterns in highly structured source code.The paper presents source code as a distinct machine-learning modality compared with images and natural language.
  • Discussion and conclusions: The model achieves the best performance among competing methods on real-world source code.This conclusion is stated as the paper’s comparative outcome for the proposed neural convolutional attentional model.
Loading 1602.03001v2…