Source-linked AI summary
Learning Python Code Suggestion with a Sparse Pointer Network
Avishkar Bhoopchand, Tim Rocktäschel, Earl Barr, Sebastian Riedel
TL;DR
Code suggestion for dynamic languages such as Python is less supported, and standard neural language models struggle to refer to identifiers introduced far earlier in a file. The paper introduces a sparse pointer network for Python code suggestion and reports the lowest perplexity and best accuracy among the top five predictions. Its methods were tested only within the same Python file.
Problem
Code suggestion for dynamic languages such as Python is less supported, while standard neural language models struggle to capture very long-range dependencies such as references to identifiers introduced earlier in a file.
Source: §1 · p. 1, §2.1 · p. 2Method
The paper introduces a sparse pointer network that uses syntax-aware attention over identifier representations for long-range dependencies and free-form generation for local phenomena.
Source: §1 · p. 2Results
The sparse pointer network achieves the lowest perplexity and best accuracy among the top five predictions, while attention yields an order of magnitude more accurate identifier prediction.
Source: §7 · p. 8Takeaways & Limitations
In a qualitative example, the sparse pointer network refers to a class attribute declared 67 tokens earlier, while the other models do not make a good suggestion.
Source: §5.1 · p. 6The methods were tested only for code suggestion within the same Python file.
Source: §7 · p. 8
Abstract
from arXiv · showhide
To enhance developer productivity, all modern integrated development environments (IDEs) include code suggestion functionality that proposes likely next tokens at the cursor. While current IDEs work well for statically-typed languages, their reliance on type annotations means that they do not provide the same level of support for dynamic programming languages as for statically-typed languages. Moreover, suggestion engines in modern IDEs do not propose expressions or multi-statement idiomatic code. Recent work has shown that language models can improve code suggestion systems by learning from software repositories. This paper introduces a neural language model with a sparse pointer network aimed at capturing very long-range dependencies. We release a large-scale code suggestion corpus of 41M lines of Python code crawled from GitHub. On this corpus, we found standard neural language models to perform well at suggesting local phenomena, but struggle to refer to identifiers that are introduced many tokens in the past. By augmenting a neural language model with a pointer network specialized in referring to predefined classes of identifiers, we obtain a much lower perplexity and a 5 percentage points increase in accuracy for code suggestion compared to an LSTM baseline. In fact, this increase in code suggestion accuracy is due to a 13 times more accurate prediction of identifiers. Furthermore, a qualitative analysis shows this model indeed captures interesting long-range dependencies, like referring to a class member defined over 60 tokens in the past.
1 INTRODUCTION
Python code suggestion is less supported than suggestion for statically typed languages, and standard neural language models struggle with long-range identifier references. The paper introduces a large Python corpus and a sparse pointer network that uses syntax-aware pointing for long-range dependencies and free-form generation for local phenomena.
1 INTRODUCTION
- Standard neural language models struggle to capture long-range semantic relationships, such as calling a function defined many tokens earlier.Their context must be stored in a fixed-dimensional internal vector representation.Source: §1 · p. 1
- The authors release a code-suggestion corpus of 41M lines of Python crawled from GitHub.They use a heuristic to crawl high-quality repositories.Source: §1 · pp. 1–2
- The sparse pointer network selectively attends to identifier introductions identified through the Python abstract syntax tree.It dynamically chooses between syntax-aware pointing for long-range dependencies and free-form generation for local phenomena, conditioned on the current context.Source: §1 · p. 2
- The paper also provides a qualitative analysis demonstrating that the model can learn long-range dependencies in Python code suggestion.Source: §1 · p. 2
2 METHODS
The methods formulate code suggestion as next-token language modeling, then extend LSTM context with attention and a sparse pointer network over previously introduced identifiers. The pointer network combines identifier reference with free-form language-model generation to address long-range dependencies.
2.1 NEURAL LANGUAGE MODEL
- The neural language model assigns probabilities to token sequences and predicts subsequent tokens from a Python-token history.Its LSTM estimates next-token probabilities from its output vector, while fixed-dimensional memory often limits it to local phenomena rather than distant identifier references.Source: §2.1 · p. 2
2.2 ATTENTION
- Standard attention uses a fixed window of previous LSTM outputs to form context for the next-token distribution.It combines this context with the current LSTM output before projecting to a vocabulary distribution.Source: §2.2 · p. 3
- Attention over large histories becomes computationally expensive and can introduce noise early in training.The authors turn to pointer networks and identifier-focused memory to support efficient retrieval from Python code history.Source: §2.2 · p. 3
2.3 SPARSE POINTER NETWORK
- The sparse pointer network attends over representations of previously introduced identifiers and combines identifier reference with language-model generation for next-token suggestions.A controller weights the two distributions; the identifier memory targets long-range usage.Source: §2.3 · pp. 3–4
- The controller chooses between identifier reference and vocabulary generation using input, language-model output, and attention-weighted context representations.These representations condition the weighting of the pointer and language-model distributions.Source: §2.3 · p. 4
3 LARGE-SCALE PYTHON CORPUS
The paper builds a large Python code corpus from GitHub projects and normalizes code before modeling. It describes the project-selection heuristic, corpus split, and normalization procedure.
3 LARGE-SCALE PYTHON CORPUS
- The corpus contains 41M lines of Python from GitHub projects selected using popularity and compatibility criteria.The authors selected projects with more than 100 stars, took the 1,000 most-forked, and retained 949 that compiled with Python 3.Source: §3 · pp. 4–5
3.1 NORMALIZATION OF IDENTIFIERS
- Figure 2 contrasts the original file on the left with its normalized version on the right.Source: Figure 2 · p. 5
3 LARGE-SCALE PYTHON CORPUS
- Projects were split into train, development, and test sets at the project level.Table 1 presents corpus statistics.Source: §3 · p. 5, Table 1 · p. 5
3.1 NORMALIZATION OF IDENTIFIERS
- Identifiers defined within a file are replaced with unique, scope-local names indicating their group, while external API and library references remain unchanged.Numerical constants become $NUM$, comments are removed, and tokens appearing fewer than five times become $OOV$.Source: §3.1 · p. 5
4 EXPERIMENTS
The experiments compare n-gram baselines with neural language models, including attention and a sparse pointer network, using shared model settings and evaluation metrics.
- The n-gram baselines use orders 3 through 6 with Modified Kneser-Ney smoothing.They were implemented with the Kyoto Language Modelling Toolkit.Source: §4 · p. 5
- The sparse pointer network is trained with mini-batch SGD and truncated backpropagation through time over a history of 20 identifier representations.Its batch size is 30; attention language models use fixed windows of 20 or 50 tokens and batch size 75.Source: §4 · pp. 5–6
- Neural models use 200-dimensional inputs and hidden states, dropout of 0.1, gradient clipping at 5, and sampled softmax with 1,000 samples.Training uses cross-entropy loss; recurrent state is carried across sequences within a file and reset between files.Source: §4 · p. 6
5 RESULTS
Attention substantially improves code suggestion, especially identifier prediction, while the sparse pointer network further improves perplexity and top-five accuracy. Qualitative examples show it can refer to identifiers introduced far earlier in the code.
5 RESULTS
- Attention lowers perplexity by 2.3 and increases accuracy by 3.4 percentage points; the sparse pointer network further improves perplexity and top-five accuracy.An LSTM with an attention window of 50 achieves the best top-prediction accuracy.Source: §5 · p. 6
- Identifier accuracy rises from 2.1% to 21.4% with attention.The paper attributes the overall accuracy increase to improved identifier prediction.Source: §5 · p. 6
5.1 QUALITATIVE ANALYSIS
- For an identifier-use example, attention and the sparse pointer network provide sensible predictions where the LSTM baseline is uncertain.The sparse pointer network also offers more reasonable alternatives beyond the correct top suggestion.Source: §5.1 · p. 6
- For a class attribute declared 67 tokens earlier, only the sparse pointer network makes a good suggestion.Its attention weights distinguish attributes from other identifier groups.Source: §5.1 · p. 6
6 RELATED WORK
Prior code suggestion work spans n-gram and neural language models, cache mechanisms, memory-augmented models, and grammar-based approaches. This paper extends the line of neural work with sparse attention over identifier representations.
- A code cache exploits locality, while the paper's sparse attention mechanism similarly targets source-code properties.The cache work is attributed to Tu et al. (2014).Source: §6 · p. 7
- Neural language models have outperformed n-gram models for code suggestion, and LSTMs improved on basic RNNs by learning longer dependencies.This paper extends that line with a sparse attention model intended to capture even longer dependencies.Source: §6 · p. 7
- Memory-augmented language models use fixed-length or vocabulary-wide memory, whereas this work provides a sparse view.The cited comparisons include Cheng et al. (2016) and Tran et al.Source: §6 · p. 7
- PCFGs exploit source-code grammars but cannot model context-dependent rules such as variables needing prior declaration.Allamanis & Sutton (2014) used PCFGs to extract idiomatic patterns.Source: §6 · p. 7
- The pointer-network controller chooses between language-model generation and identifier copying; unlike short textual inputs, code suggestion requires very long-range dependencies.The model filters the memory of previous identifier representations.Source: §6 · p. 8
7 CONCLUSIONS AND FUTURE WORK
The sparse pointer network achieved the lowest perplexity and best accuracy among the top five predictions for Python code suggestion. The methods were tested only within a single Python file, with project-wide scaling and IDE integration left as future work.
7 CONCLUSIONS AND FUTURE WORK
- The sparse pointer network achieved the lowest perplexity and best accuracy among the top five predictions.The evaluation covered code suggestion within Python files.Source: §7 · p. 8
- The paper released a 41M-line GitHub Python corpus and proposed sparse attention over previous identifier representations for efficient long-range dependency modeling.The corpus and code for reproducing the experiment were released.Source: §7 · p. 8
- Testing was limited to code suggestion within a single Python file; scaling to projects, IDE integration, and character-level code completion were future directions.Source: §7 · p. 8
APPENDIX
Figure 4 depicts a code suggestion example, showing identifier declarations, their memory representations, and the controller’s interpolation between language-model and pointer-network attention outputs.
APPENDIX
- Boldface tokens on the left mark each identifier’s first declaration.Source: Figure 4 · p. 11
- The middle of the figure visualizes memory representations of the identifiers.Source: Figure 4 · p. 11
- The right side shows controller output λ, used to interpolate between language-model output and pointer-network attention.Source: Figure 4 · p. 11