NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

On the Embeddings of Variables in Recurrent Neural Networks for Source Code

Nadezhda Chirkova

Abstract

Traditional models for source code often rely on static embeddings for variable names, failing to capture the renaming invariance and local functional roles inherent in programming. This paper introduces dynamic embeddings, a recurrent mechanism that allows a variable's representation to evolve as a model processes code. By updating embeddings based on local context through a dual-RNN architecture, the proposed method significantly improves performance in code completion and bug fixing, even in fully anonymized settings.

Introduction

In Natural Language Processing (NLP), words typically have static embeddings capturing general semantic meaning. However, applying this directly to source code is problematic due to renaming invariance: the logic of a program remains identical regardless of whether a variable is named x or foo. Standard models often fail when names are changed because they rely on the "global" meaning of a name rather than its structural role.

Furthermore, static embeddings are often too broad. A common variable name like y might serve thousands of different purposes across a dataset. A static lookup table cannot capture the specific role y plays within a unique function, such as storing a specific calculation result. The authors propose dynamic embeddings to bridge this gap, creating representations that adapt to a variable's usage within a specific snippet.

Methodology

The authors propose a dual-recurrent architecture to process code represented as a sequence of Abstract Syntax Tree (AST) nodes. The system consists of two primary components:

  1. The Main RNN (LSTM_main): Functions as a standard language model, maintaining a hidden state as it traverses the AST node sequence.
  2. The Dynamic RNN (LSTM_dyn): Instead of a fixed lookup table, the model maintains a "local" embedding for every variable in the snippet. Each time a variable appears, LSTM_dyn updates its specific embedding based on the current hidden state of the main RNN.
Figure 3: Overview of the processing pipeline, from input code and AST traversal to the dual-LSTM architecture.

The model can be initialized using either Full Data (starting with static embeddings based on names) or an Anonymized setting (starting with a constant vector). The latter forces the model to learn a variable's identity purely from its behavior and interaction with operators.

Experimental Evaluation

The study utilizes the Python150k and JavaScript150k benchmarks. To ensure rigorous evaluation, the authors employed a repository-based split and deduplicated the data, preventing the model from memorizing project-specific patterns.

A key stress test involved anonymizing code (renaming variables to var1, var2, etc.). This demonstrated the model's ability to understand complex logic without descriptive name "hints," effectively replacing the need for complex pointer mechanisms often used to handle out-of-vocabulary variables.

Key Results

The dynamic embedding approach yielded substantial improvements across two major tasks: Code Completion and Variable Misuse (bug fixing).

Figure 6: Performance comparison between static and dynamic embeddings on Python (PY) and JavaScript (JS) datasets.

As shown in the results table, dynamic embeddings consistently outperform static baselines. In the anonymized Python setting, dynamic embeddings achieved 66.90% accuracy in code completion, surpassing even standard models trained on full data with names (~64%). For variable misuse detection, the improvement was even more dramatic: dynamic embeddings reached 63.64% joint accuracy on Python, while static embeddings struggled at just 25.17%.

Conclusion

Dynamic embeddings capture "program semantics" by prioritizing a variable's role over its label. The architecture is more parameter-efficient, achieving superior results with 500 units compared to the 1200 units required for static models. While currently implemented for RNNs, the authors suggest this logic is a vital component for future syntax-based models of source code, potentially extending to Transformer architectures.

Summary updated

BibTeX
@inproceedings{chirkova2021embeddings,
  author = {Nadezhda Chirkova},
  title = {{On the Embeddings of Variables in Recurrent Neural Networks for Source Code}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2021},
  doi = {10.18653/v1/2021.naacl-main.213},
}
Figure