Abstract
Many language generation tasks require the production of text conditioned on both structured and unstructured inputs. We present a novel neural network architecture which generates an output sequence conditioned on an arbitrary number of input functions. Crucially, our approach allows both the choice of conditioning context and the granularity of generation, for example characters or tokens, to be marginalised, thus permitting scalable and effective training. Using this framework, we address the problem of generating programming code from a mixed natural language and structured specification. We create two new data sets for this paradigm derived from the collectible trading card games Magic the Gathering and Hearthstone. On these, and a third preexisting corpus, we demonstrate that marginalising multiple predictors allows our model to outperform strong benchmarks.
Introduction
Generating programming code from a specification is a fundamental challenge in artificial intelligence. It requires bridging the gap between unstructured natural language (e.g., "Deal 3 damage to a minion") and structured formal logic (e.g., target.take_damage(3)). Standard sequence-to-sequence (seq2seq) models often fail in this domain due to two primary issues:
- Sparsity: Code frequently contains specific identifiers, names, or numerical values that appear rarely in training data, making them difficult for vocabulary-based models to predict.
- Granularity: Code generation involves a mix of boilerplate syntax and direct copies from the input. Predicting everything token-by-token is inefficient for long, structured sequences.
Methods: Latent Predictor Networks
To address these challenges, we introduce Latent Predictor Networks (LPNs). Unlike standard models that rely on a single softmax output, LPNs utilize multiple "expert" predictors to generate segments of the output sequence.
Multiple Predictors and Granularity
The model can dynamically choose between different generation mechanisms, such as a character-level RNN for novel logic or pointer-based mechanisms for copying specific fields like card names or statistics. This allows the model to handle both the rigid syntax of code and the fluid nature of names and strings.
Marginalization over Latent Variables
A key innovation of the LPN is treating the choice of predictor and the length of the generated segment as latent variables. During training, the model uses a forward-backward algorithm to marginalize over all possible paths that could have generated the target code. This allows the model to autonomously discover the most efficient generation strategies without manual annotation of "copy" vs "generate" actions.
Structured Attention
Since the input consists of diverse fields (Name, Cost, Description, etc.), we employ a structured attention mechanism. This projects various input types into a common space, enabling the model to focus on the most relevant data field at each step of the generation process.
Datasets
We evaluated the LPN on three distinct datasets:
- Magic: The Gathering (MTG): 13,297 cards paired with Java implementations.
- Hearthstone (HS): 665 cards paired with Python implementations.
- Django: 18,805 lines of Python code with natural language descriptions.
The TCG datasets are particularly demanding as they require generating full class definitions from a mix of numerical stats and text effects.
Results
The LPN model significantly outperformed standard neural benchmarks across all metrics:
- MTG: Achieved a 61.4 BLEU score (vs. 50.1 for standard attention) and a 4.8% exact match accuracy.
- Hearthstone: Improved from 43.9 to 65.6 BLEU.
- Django: Reached 77.6 BLEU with a 62.3% exact match accuracy.
Qualitatively, the model excels at copying names and statistics into correct code slots, though it still faces challenges with unique, complex logic that lacks frequent patterns in the training set.
Conclusion
Latent Predictor Networks demonstrate that combining creation with copying through marginalized latent variables is a powerful approach for code generation. While the model achieves high surface-level accuracy (BLEU), future work will focus on ensuring logical correctness through execution-based validation and improving performance on "long-tail" unique effects.
Summary updated