Strong Generalization and Efficiency in Neural Programs

Yujia Li, Felix Gimeno, Pushmeet Kohli, Oriol VinyalsarXiv 2020

Abstract

We study the problem of learning efficient algorithms that strongly generalize in the framework of neural program induction. By carefully designing the input/output interfaces of the neural model and through imitation, we are able to learn models that produce correct results for arbitrary input sizes, achieving strong generalization. Moreover, by using reinforcement learning, we optimize for program efficiency metrics and discover new algorithms that surpass the teacher used in imitation. Our approach can learn to outperform custom-written solutions for a variety of problems, including sorting, searching in ordered lists, and the NP-complete 0/1 knapsack problem. Notably, our learned sorting model performs perfectly on any tested input size with $O(n \log n)$ complexity, outperforming hand-coded algorithms like Quicksort in the number of operations even for list sizes far beyond those seen during training.

The Challenge of Neural Program Induction

Teaching neural networks to learn and execute algorithms—a field known as neural program induction—has historically struggled with two massive hurdles:

  1. Strong Generalization: Models trained on small instances (e.g., sorting a list of 20 numbers) usually fail catastrophically when applied to larger instances (e.g., 100 numbers). They often memorize patterns rather than learning the underlying logic.
  2. Efficiency: Even when models learn a correct algorithm, they rarely learn an efficient one. Moving from an $O(n^2)$ algorithm to an $O(n \log n)$ algorithm is incredibly difficult for a neural network to discover from scratch.

This research tackles both issues by treating the neural network as a controller interacting with a structured environment, much like a CPU executing a discrete instruction set.

Methods: Architectural Design for Generalization

The framework frames the problem as a neural controller interacting with an environment through a specific interface.

The framework consists of a neural controller that outputs discrete instructions to an environment, which then updates the data and execution state.

Interface and Instruction Set Design

Instead of feeding the entire data structure into the network at once, the authors provide a "constant size" view. The network only sees local information, such as values at specific pointer indices. They defined a discrete set of instructions, including MoveVar (pointer movement), Swap, and crucially, FunctionCall and Return.

Externalizing Memory

By removing recurrent memory (like LSTMs) from the neural network and handling memory via the environment (e.g., an external function call stack), the system prevents the compounding mathematical errors that typically plague RNNs over long sequences. This architectural choice is vital for maintaining correctness over millions of execution steps.

Imitation + Reinforcement Learning (RL)

The model is first trained via supervised behavior cloning to imitate a "teacher" algorithm (e.g., Quicksort). Subsequently, training switches to Reinforcement Learning. The model receives a negative reward for every step taken, incentivizing it to explore the algorithmic search space for shortcuts that finish the task in fewer steps than the teacher.

Results: Surpassing Human-Coded Algorithms

The models were evaluated on classic computer science problems including sorting, searching, and the 0/1 knapsack problem.

Flawless Generalization and Efficiency in Sorting

The sorting model, trained only on lists of 30–50 elements, achieved a 100% solve rate on lists of up to 100,000 elements. More impressively, the RL-optimized model discovered an $O(n \log n)$ strategy that required fewer operations than a standard hand-coded Quicksort.

Performance comparison of various sorting algorithms and learned models across different instance sizes. The RL + imitation model consistently achieves the lowest step counts.

As shown in the results, for an array of 100,000 items, the learned model required approximately 338,947 steps, while the standard Quicksort implementation required 368,338 steps. This demonstrates that the neural controller did not just learn to imitate, but learned to optimize.

Searching and Knapsack

In searching tasks, the model discovered an $O(\log n)$ algorithm that outperformed standard binary search in average steps. For the NP-complete 0/1 Knapsack problem, the learned model matched the performance of a Depth-First Search (DFS) algorithm while using 5x fewer steps, marking a significant milestone for neural program induction in complex optimization spaces.

Discussion and Future Work

While the learned algorithms use fewer logical operations, the current wall-clock speed is limited by the overhead of neural network forward passes for every step. Future hardware, such as "Neural Logic Units," may be required to make these efficient logical steps translate into faster real-world execution. Additionally, while the model empirically never failed in testing, the lack of formal mathematical guarantees remains a challenge for safety-critical applications. Future research will focus on designing more universal instruction sets to reduce the need for manual interface design.

Summary updated

BibTeX
@misc{li2020strong,
  author = {Yujia Li and Felix Gimeno and Pushmeet Kohli and Oriol Vinyals},
  title = {{Strong Generalization and Efficiency in Neural Programs}},
  year = {2020},
  eprint = {2007.03629},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2007.03629},
}
Figure