Learning to Execute

Wojciech Zaremba, Ilya SutskeverarXiv 2014

Abstract

This paper demonstrates that Recurrent Neural Networks (specifically LSTMs) can learn to evaluate short, simple computer programs character-by-character without any prior knowledge of programming syntax or mathematics. To achieve this, the authors introduce a novel "combined" curriculum learning strategy that drastically outperforms standard training, enabling an LSTM to accurately execute nested logic and even add two 9-digit numbers with 99% accuracy.

Introduction

Executing computer programs requires a system to understand highly compositional and nontrivial concepts: numerical operations, control flow (if-statements, for-loops), and variable assignments. Traditionally, this domain has been viewed as too complex and rigid for neural networks, which excel at fuzzy pattern recognition rather than strict symbolic logic.

The authors investigate whether a neural network can learn to map the character-level representation of a program (e.g., print(12+34)) directly to its correct output (e.g., 46). Because the network reads the program one character at a time, it starts with zero knowledge of what a digit is, what a + symbol means, or how a for loop operates. Training a network to discover these rules from scratch using standard stochastic gradient descent (SGD) is incredibly difficult due to the long-term dependencies involved in variable assignment and arithmetic.

Methodology

The authors frame program evaluation as a sequence-to-sequence learning problem. They use a deep Long Short-Term Memory (LSTM) network that reads the input string character-by-character, updates its hidden state, and then generates the output string character-by-character.

To overcome the difficulty of training, they heavily rely on Curriculum Learning—the idea of training a model on easier examples before moving to harder ones. They test four strategies:

  1. Baseline: No curriculum; training directly on the target distribution of hard problems.
  2. Naive: Start with the easiest problems (1-digit numbers, no nesting) and gradually increase difficulty as the model learns.
  3. Mix: A random mixture of all difficulty levels.
  4. Combined: A novel approach that mixes the Naive strategy with the Mix strategy. Every training sample is drawn either from the gradually increasing Naive pool or the fully randomized Mix pool.

Additionally, they explore two input transformations to make sequence learning easier: Input Reversing (feeding the sequence backwards) and Input Doubling (feeding the sequence twice, e.g., 123;123).

Experimental Setup

The authors generate synthetic datasets of short Python-style programs. The datasets are parameterized by two variables:

  • Length: The number of digits in the integers used (e.g., length=4 means numbers up to 9999).
  • Nesting: The depth of compositional operations (how many times operations are combined).

The programs include addition, subtraction, constrained multiplication, variable assignments, if-statements, and single for-loops. They also evaluate the models on two isolated sub-tasks: a pure Addition Task (adding two $n$-digit numbers) and a Memorization Task (reading a sequence of numbers and repeating it back).

Key Insights and Ideas

  • Naive Curriculum Learning Can Hurt: Surprisingly, the standard "start easy, get hard" (Naive) curriculum sometimes performed worse than the Baseline (no curriculum).
  • The "Hidden State Allocation" Hypothesis: The authors hypothesize why the Naive curriculum fails. On easy examples, the network has no incentive to be efficient, so it spreads the representation across its entire memory capacity. When faced with harder examples, it lacks free memory and must restructure its memory patterns.
  • The Power of the "Combined" Curriculum: By mixing easy examples with a random sampling of hard examples (the Combined strategy), the network is forced to use its memory efficiently from the very beginning. This prevents the need for massive memory restructuring later, leading to the best performance across all tasks.
Relative performance of different strategies compared to the baseline across varying program complexities.

Key Results

  • Addition: Using the Combined curriculum strategy, the LSTM achieved 99% accuracy on adding two 9-digit numbers. For context, the Naive curriculum completely failed at this length.
  • Program Evaluation: The Combined strategy consistently outperformed all other training methods across all program lengths and nesting depths. It successfully evaluated complex, nested logic (e.g., for loops updating variables based on if conditions) entirely from character-level patterns.
  • Memorization: Combining Input Doubling and Input Reversing allowed the LSTM to successfully memorize and reconstruct sequences up to 65 digits long.
Accuracy on the addition task across different strategies and number lengths.

Limitations and Future Work

The authors acknowledge several important limitations:

  • Computational Constraints: The LSTM only performs a single left-to-right pass, meaning it operates in $O(n)$ time and constant memory. Therefore, it cannot evaluate arbitrary programs (like generic multi-digit integer multiplication, which requires superlinear time).
  • True Understanding vs. Approximation: It is unclear if the model actually learned the correct underlying algorithms or if it just memorized massive lookup tables and heuristics.
  • Generalization: The training and test distributions in this paper were identical. Future work is needed to see if a model trained on these programs could generalize to entirely new, out-of-distribution program structures.

Summary updated

BibTeX
@misc{zaremba2014learning,
  author = {Wojciech Zaremba and Ilya Sutskever},
  title = {{Learning to Execute}},
  year = {2014},
  eprint = {1410.4615},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1410.4615},
}
Figure