Latent Predictor Networks for Code Generation

Wang Ling, Edward Grefenstette, Karl Moritz Hermann, Tomáš Kočiský, Andrew Senior, Fumin Wang, Phil Blunsom

Introduction

The generation of both natural and formal languages often requires models conditioned on diverse predictors [Koehn et al., 2007, Wong and Mooney, 2006]. Most models take the restrictive approach of employing a single predictor, such as a word softmax, to predict all tokens of the output sequence. To illustrate its limitation, suppose we wish to generate the answer to the question “Who wrote The Foundation?” as “The Foundation was written by Isaac Asimov”. The generation of the words “Issac Asimov” and “The Foundation” from a word softmax trained on annotated data is unlikely to succeed as these words are sparse. A robust model might, for example, employ one predictor to copy “The Foundation” from the input, and a another one to find the answer “Issac Asimov” by searching through a database. However, training multiple predictors is in itself a challenging task, as no annotation exists regarding the predictor used to generate each output token. Furthermore, predictors generate segments of different granularity, as database queries can generate multiple tokens while a word softmax generates a single token. In this work we introduce Latent Predictor Networks (LPNs), a novel neural architecture that fulfills these desiderata: at the core of the architecture is the exact computation of the marginal likelihood over latent predictors and generated segments allowing for scalable training.

We introduce a new corpus for the automatic generation of code for cards in Trading Card Games (TCGs), on which we validate our model Dataset available at https://deepmind.com/publications.html. TCGs, such as Magic the Gathering (MTG) and Hearthstone (HS), are games played between two players that build decks from an ever expanding pool of cards. Examples of such cards are shown in Figure 1. Each card is identified by its attributes (e.g., name and cost) and has an effect that is described in a text box. Digital implementations of these games implement the game logic, which includes the card effects. This is attractive from a data extraction perspective as not only are the data annotations naturally generated, but we can also view the card as a specification communicated from a designer to a software engineer.

This dataset presents additional challenges to prior work in code generation [Wong and Mooney, 2006, Jones et al., 2012, Lei et al., 2013, Artzi et al., 2015, Quirk et al., 2015], including the handling of structured input—i.e. cards are composed by multiple sequences (e.g., name and description)—and attributes (e.g., attack and cost), and the length of the generated sequences. Thus, we propose an extension to attention-based neural models [Bahdanau et al., 2014] to attend over structured inputs. Finally, we propose a code compression method to reduce the size of the code without impacting the quality of the predictions.

Experiments performed on our new datasets, and a further pre-existing one, suggest that our extensions outperform strong benchmarks.

The paper is structured as follows: We first describe the data collection process (Section 2) and formally define our problem and our baseline method (Section 3). Then, we propose our extensions, namely, the structured attention mechanism (Section 4) and the LPN architecture (Section 5). We follow with the description of our code compression algorithm (Section 6). Our model is validated by comparing with multiple benchmarks (Section 7). Finally, we contextualize our findings with related work (Section 8) and present the conclusions of this work (Section 9).

Dataset Extraction

We obtain data from open source implementations of two different TCGs, MTG in Javagithub.com/magefree/mage/ and HS in Python.github.com/danielyule/hearthbreaker/ The statistics of the corpora are illustrated in Table 1. In both corpora, each card is implemented in a separate class file, which we strip of imports and comments. We categorize the content of each card into two different groups: singular fields that contain only one value; and text fields, which contain multiple words representing different units of meaning. In MTG, there are six singular fields (attack, defense, rarity, set, id, and health) and four text fields (cost, type, name, and description), whereas HS cards have eight singular fields (attack, health, cost and durability, rarity, type, race and class) and two text fields (name and description). Text fields are tokenized by splitting on whitespace and punctuation, with exceptions accounting for domain specific artifacts (e.g., Green mana is described as “{G}” in MTG). Empty fields are replaced with a “NIL” token.

The code for the HS card in Figure 1 is shown in Figure 2. The effect of “drawing cards until the player has as many cards as the opponent” is implemented by computing the difference between the players’ hands and invoking the draw method that number of times. This illustrates that the mapping between the description and the code is non-linear, as no information is given in the text regarding the specifics of the implementation.

Problem Definition

Given the description of a card xx, our decoding problem is to find the code y^\hat{y} so that:

Here logP(yx)\log P(y\mid x) is estimated by a given model. We define y=y1..yyy=y_{1}..y_{|y|} as the sequence of characters of the code with length y|y|. We index each input field with k=1..xk={1..|x|}, where x|x| quantifies the number of input fields. xk|x_{k}| denotes the number of tokens in xkx_{k} and xkix_{ki} selects the ii-th token.

Structured Attention

When x=1|x|=1, the attention model of ?) applies. Following the chain rule, logP(yx)=t=1..ylogP(yty1..yt1,x)\log P(y|x)=\sum_{t=1..|y|}\log P(y_{t}|y_{1}..y_{t-1},x), each token yty_{t} is predicted conditioned on the previously generated sequence y1..yt1y_{1}..y_{t-1} and input sequence x1=x11..x1x1x_{1}=x_{11}..x_{1|x_{1}|}. Probability are estimated with a softmax over the vocabulary YY:

where ht\mathbf{h}_{t} is the Recurrent Neural Network (RNN) state at time stamp tt, which is modeled as g(yt1,ht1,zt)g(\mathbf{y}_{t-1},\mathbf{h}_{t-1},\mathbf{z}_{t}). g()g(\cdot) is a recurrent update function for generating the new state ht\mathbf{h}_{t} based on the previous token yt1\mathbf{y}_{t-1}, the previous state ht1\mathbf{h}_{t-1}, and the input text representation zt\mathbf{z}_{t}. We implement gg using a Long Short-Term Memory (LSTM) RNNs [Hochreiter and Schmidhuber, 1997].

The attention mechanism generates the representation of the input sequence x=x11..x1x1\mathbf{x}=x_{11}..x_{1|x_{1}|}, and zt\mathbf{z}_{t} is computed as the weighted sum zt=i=1..x1aih(x1i)\mathbf{z}_{t}=\sum_{i={1..|x_{1}|}}a_{i}h(x_{1i}), where aia_{i} is the attention coefficient obtained for token x1ix_{1i} and hh is a function that maps each x1ix_{1i} to a continuous vector. In general, hh is a function that projects x1ix_{1i} by learning a lookup table, and then embedding contextual words by defining an RNN. Coefficients aia_{i} are computed with a softmax over input tokens x11..x1x1x_{11}..x_{1|x_{1}|}:

Function vv computes the affinity of each token x1ix_{1i} and the current output context ht1\mathbf{h}_{t-1}. A common implementation of vv is to apply a linear projection from h(x1i):ht1h(x_{1i}):\mathbf{h}_{t-1} (where :: is the concatenation operation) into a fixed size vector, followed by a tanh\tanh and another linear projection.

Our Approach

We extend the computation of zt\mathbf{z}_{t} for cases when xx corresponds to multiple fields. Figure 3 illustrates how the MTG card “Serra Angel” is encoded, assuming that there are two singular fields and one text field. We first encode each token xkix_{ki} using the C2W model described in ?), which is a replacement for lookup tables where word representations are learned at the character level (cf. C2W row). A context-aware representation is built for words in the text fields using a bidirectional LSTM (cf. Bi-LSTM row). Computing attention over multiple input fields is problematic as each input field’s vectors have different sizes and value ranges. Thus, we learn a linear projection mapping each input token xkix_{ki} to a vector with a common dimensionality and value range (cf. Linear row). Denoting this process as f(xki)f(x_{ki}), we extend Equation 3 as:

Here a scalar coefficient akia_{ki} is computed for each input token xkix_{ki} (cf. “Tanh”, “Linear”, and “Softmax” rows). Thus, the overall input representation zt\mathbf{z}_{t} is computed as:

Latent Predictor Networks

In order to decode from xx to yy, many words must be copied into the code, such as the name of the card, the attack and the cost values. If we observe the HS card in Figure 1 and the respective code in Figure 2, we observe that the name “Divine Favor” must be copied into the class name and in the constructor, along with the cost of the card “3”. As explained earlier, this problem is not specific to our task: for instance, in the dataset of ?), a model must learn to map from timeout = int ( timeout ) to “convert timeout into an integer.”, where the name of the variable “timeout” must be copied into the output sequence. The same issue exists for proper nouns in machine translation which are typically copied from one language to the other. Pointer networks [Vinyals et al., 2015] address this by defining a probability distribution over a set of units that can be copied c=c1..ccc=c_{1}..c_{|c|}. The probability of copying a unit cic_{i} is modeled as:

As in the attention model (Equation 3), vv is a function that computes the affinity between an embedded copyable unit h(ci)h(c_{i}) and an arbitrary vector q\mathbf{q}.

Our Approach

Combining pointer networks with a character-based softmax is in itself difficult as these generate segments of different granularity and there is no ground truth of which predictor to use at each time stamp. We now describe Latent Predictor Networks, which model the conditional probability logP(yx)\log P(y|x) over the latent sequence of predictors used to generate yy.

We assume that our model uses multiple predictors rRr\in R, where each rr can generate multiple segments st=yt..yt+st1s_{t}=y_{t}..y_{t+|s_{t}|-1} with arbitrary length st|s_{t}| at time stamp tt. An example is illustrated in Figure 4, where we observe that to generate the code init(‘Tirion Fordring’,8,6,6), a pointer network can be used to generate the sequences y713y_{7}^{13}=Tirion and y1422y_{14}^{22}=Fordring (cf. “Copy From Name” row). These sequences can also be generated using a character softmax (cf. “Generate Characters” row). The same applies to the generation of the attack, health and cost values as each of these predictors is an element in RR. Thus, we define our objective function as a marginal log likelihood function over a latent variable ω\omega:

Formally, ω\omega is a sequence of pairs rt,str_{t},s_{t}, where rtRr_{t}\in R denotes the predictor that is used at timestamp tt and sts_{t} the generated string. We decompose P(y,ωx)P(y,\omega\mid x) as the product of the probabilities of segments sts_{t} and predictors rtr_{t}:

where the generation of each segment is performed in two steps: select the predictor rtr_{t} with probability P(rty1..yt1,x)P(r_{t}\mid y_{1}..y_{t-1},x) and then generate sts_{t} conditioned on predictor rtr_{t} with probability logP(sty1..yt1,x,rt)\log P(s_{t}\mid y_{1}..y_{t-1},x,r_{t}). The probability of each predictor is computed using a softmax over all predictors in RR conditioned on the previous state ht1\mathbf{h_{t-1}} and the input representation zt\mathbf{z}_{t} (cf. “Select Predictor” box). Then, the probability of generating the segment sts_{t} depends on the predictor type. We define three types of predictors:

Character Generation

Generate a single character from observed characters from the training data. Only one character is generated at each time stamp with probability given by Equation 2.

Copy Singular Field

For singular fields only the field itself can be copied, for instance, the value of the attack and cost attributes or the type of card. The size of the generated segment is the number of characters in the copied field and the segment is generated with probability 11.

Copy Text Field

For text fields, we allow each of the words xkix_{ki} within the field to be copied. The probability of copying a word is learned with a pointer network (cf. “Copy From Name” box), where h(ci)h(c_{i}) is set to the representation of the word f(xki)f(x_{ki}) and q\mathbf{q} is the concatenation ht1:zt\mathbf{h}_{t-1}:\mathbf{z}_{t} of the state and input vectors. This predictor generates a segment with the size of the copied word.

It is important to note that the state vector ht1\mathbf{h}_{t-1} is generated by building an RNN over the sequence of characters up until the time stamp t1t-1, i.e. the previous context yt1\mathbf{y}_{t-1} is encoded at the character level. This allows the number of possible states to remain tractable at training time.

1 Inference

At training time we use back-propagation to maximize the probability of observed code, according to Equation 7. Gradient computation must be performed with respect to each computed probability P(rty1..yt1,x)P(r_{t}\mid y_{1}..y_{t-1},x) and P(sty1..yt1,x,rt)P(s_{t}\mid y_{1}..y_{t-1},x,r_{t}). The derivative logP(yx)P(rty1..yt1,x)\frac{\partial\log P(y\mid x)}{\partial P(r_{t}\mid y_{1}..y_{t-1},x)} yields:

Here αt\alpha_{t} denotes the cumulative probability of all values of ω\omega up until time stamp tt and αy+1\alpha_{|y|+1} yields the marginal probability P(yx)P(y\mid x). βt,rt=P(sty1..yt1)βt+st1\beta_{t,r_{t}}=P(s_{t}\mid y_{1}..y_{t-1})\beta_{t+|s_{t}|-1} denotes the cumulative probability starting from predictor rtr_{t} at time stamp tt, exclusive. This includes the probability of the generated segment P(sty1..yt1,x,rt)P(s_{t}\mid y_{1}..y_{t-1},x,r_{t}) and the probability of all values of ω\omega starting from timestamp t+st1t+|s_{t}|-1, that is, all possible sequences that generate segment yy after segment sts_{t} is produced. For completeness, ξr\xi_{r} denotes the cumulative probabilities of all ω\omega that do not include rtr_{t}. To illustrate this, we refer to Figure 4 and consider the timestamp t=14t=14, where the segment s14=s_{14}=Fordring is generated. In this case, the cumulative probability α14\alpha_{14} is the sum of the path that generates the sequence init(‘Tirion with characters alone, and the path that generates the word Tirion by copying from the input. β21\beta_{21} includes the probability of all paths that follow the generation of Fordring, which include 2×3×32\times 3\times 3 different paths due to the three decision points that follow (e.g. generating 8 using a character softmax vs. copying from the cost). Finally, ξr\xi_{r} refers to the path that generates Fordring character by character.

While the number of possible paths grows exponentially, α\alpha and β\beta can be computed efficiently using the forward-backward algorithm for Semi-Markov models [Sarawagi and Cohen, 2005], where we associate P(rty1..yt1,x)P(r_{t}\mid y_{1}..y_{t-1},x) to edges and P(sty1..yt1,x,rt)P(s_{t}\mid y_{1}..y_{t-1},x,r_{t}) to nodes in the Markov chain.

The derivative logP(yx)P(sty1..yt1,x,rt)\frac{\partial\log P(y\mid x)}{\partial P(s_{t}\mid y_{1}..y_{t-1},x,r_{t})} can be computed using the same logic:

Once again, we denote αt,rt=αtP(rty1..yt1,x)\alpha_{t,r_{t}}=\alpha_{t}P(r_{t}\mid y_{1}..y_{t-1},x) as the cumulative probability of all values of ω\omega that lead to sts_{t}, exclusive.

An intuitive interpretation of the derivatives is that gradient updates will be stronger on probability chains that are more likely to generate the output sequence. For instance, if the model learns a good predictor to copy names, such as Fordring, other predictors that can also generate the same sequences, such as the character softmax will allocate less capacity to the generation of names, and focus on elements that they excel at (e.g. generation of keywords).

2 Decoding

Decoding is performed using a stack-based decoder with beam search. Each state SS corresponds to a choice of predictor rtr_{t} and segment sts_{t} at a given time stamp tt. This state is scored as V(S)=logP(sty1..yt1,x,rt)+logP(rty1..yt1,x)+V(prev(S))V(S)=\log P(s_{t}\mid y_{1}..y_{t-1},x,r_{t})+\log P(r_{t}\mid y_{1}..y_{t-1},x)+V(prev(S)), where prev(S)prev(S) denotes the predecessor state of SS. At each time stamp, the nn states with the highest scores VV are expanded, where nn is the size of the beam. For each predictor rtr_{t}, each output sts_{t} generates a new state. Finally, at each timestamp tt, all states which produce the same output up to that point are merged by summing their probabilities.

Code Compression

As the attention-based model traverses all input units at each generation step, generation becomes quite expensive for datasets such as MTG where the average card code contains 1,080 characters. While this is not the essential contribution in our paper, we propose a simple method to compress the code while maintaining the structure of the code, allowing us to train on datasets with longer code (e.g., MTG).

The idea behind that method is that many keywords in the programming language (e.g., public and return) as well as frequently used functions and classes (e.g., Card) can be learned without character level information. We exploit this by mapping such strings onto additional symbols XiX_{i} (e.g., public class copy() \toX1X_{1} X2X_{2} X3X_{3}()”). Formally, we seek the string v^\hat{v} among all strings V(max)V(max) up to length maxmax that maximally reduces the size of the corpus:

where C(v)C(v) is the number of occurrences of vv in the training corpus and len(v)len(v) its length. (len(v)1)C(v)(len(v)-1)C(v) can be seen as the number of characters reduced by replacing vv with a non-terminal symbol. To find q(v)q(v) efficiently, we leverage the fact that C(v)C(v)C(v)\leq C(v^{\prime}) if vv contains vv^{\prime}. It follows that (max1)C(v)(max1)C(v)(max-1)C(v)\leq(max-1)C(v^{\prime}), which means that the maximum compression obtainable for vv at size maxmax is always lower than that of vv^{\prime}. Thus, if we can find a vˉ\bar{v} such that (len(vˉ)1)C(vˉ)>(max1)C(v)(len(\bar{v})-1)C(\bar{v})>(max-1)C(v^{\prime}), that is vˉ\bar{v} at the current size achieves a better compression rate than vv^{\prime} at the maximum length, then it follows that all sequences that contain vv can be discarded as candidates. Based on this idea, our iterative search starts by obtaining the counts C(v)C(v) for all segments of size s=2s=2, and computing the best scoring segment vˉ\bar{v}. Then, we build a list L(s)L(s) of all segments that achieve a better compression rate than vˉ\bar{v} at their maximum size. At size s+1s+1, only segments that contain a element in L(s1)L(s-1) need to be considered, making the number of substrings to be tested to be tractable as ss increases. The algorithm stops once ss reaches maxmax or the newly generated list L(s)L(s) contains no elements.

Once v^\hat{v} is obtained, we replace all occurrences of v^\hat{v} with a new non-terminal symbol. This process is repeated until a desired average size for the code is reached. While training is performed on the compressed code, the decoding will undergo an additional step, where the compressed code is restored by expanding the all XiX_{i}. Table 2 shows the first 10 replacements from the MTG dataset, reducing its average size from 1080 to 794.

Experiments

Tests are performed on the two datasets provided in this paper, described in Table 1. Additionally, to test the model’s ability of generalize to other domains, we report results in the Django dataset [Oda et al., 2015], comprising of 16000 training, 1000 development and 1805 test annotations. Each data point consists of a line of Python code together with a manually created natural language description.

Neural Benchmarks

We implement two standard neural networks, namely a sequence-to-sequence model [Sutskever et al., 2014] and an attention-based model [Bahdanau et al., 2014]. The former is adapted to work with multiple input fields by concatenating them, while the latter uses our proposed attention model. These models are denoted as “Sequence” and “Attention”.

Machine Translation Baselines

Our problem can also be viewed in the framework of semantic parsing [Wong and Mooney, 2006, Lu et al., 2008, Jones et al., 2012, Artzi et al., 2015]. Unfortunately, these approaches define strong assumptions regarding the grammar and structure of the output, which makes it difficult to generalize for other domains [Kwiatkowski et al., 2010]. However, the work in ?) provides evidence that using machine translation systems without committing to such assumptions can lead to results competitive with the systems described above. We follow the same approach and create a phrase-based [Koehn et al., 2007] model and a hierarchical model (or PCFG) [Chiang, 2007] as benchmarks for the work presented here. As these models are optimized to generate words, not characters, we implement a tokenizer that splits on all punctuation characters, except for the “_” character. We also facilitate the task by splitting CamelCase words (e.g., class TirionFordring \to class Tirion Fordring). Otherwise all class names would not be generated correctly by these methods. We used the models implemented in Moses to generate these baselines using standard parameters, using IBM Alignment Model 4 for word alignments [Och and Ney, 2003], MERT for tuning [Sokolov and Yvon, 2011] and a 4-gram Kneser-Ney Smoothed language model [Heafield et al., 2013]. These models will be denoted as “Phrase” and “Hierarchical”, respectively.

Retrieval Baseline

It was reported in [Quirk et al., 2015] that a simple retrieval method that outputs the most similar input for each sample, measured using Levenshtein Distance, leads to good results. We implement this baseline by computing the average Levenshtein Distance for each input field. This baseline is denoted “Retrieval”.

Evaluation

A typical metric is to compute the accuracy of whether the generated code exactly matches the reference code. This is informative as it gives an intuition of how many samples can be used without further human post-editing. However, it does not provide an illustration on the degree of closeness to achieving the correct code. Thus, we also test using BLEU-4 [Papineni et al., 2002] at the token level. There are clearly problems with these metrics. For instance, source code can be correct without matching the reference. The code in Figure 2, could have also been implemented by calling the draw function in an cycle that exists once both players have the same number of cards in their hands. Some tasks, such as the generation of queries [Zelle and Mooney, 1996], have overcome this problem by executing the query and checking if the result is the same as the annotation. However, we shall leave the study of these methologies for future work, as adapting these methods for our tasks is not trivial. For instance, the correctness cards with conditional (e.g. if player has no cards, then draw a card) or non-deterministc (e.g. put a random card in your hand) effects cannot be simply validated by running the code.

Setup

The multiple input types (Figure 3) are hyper-parametrized as follows: The C2W model (cf. “C2W” row) used to obtain continuous vectors for word types uses character embeddings of size 100 and LSTM states of size 300, and generates vectors of size 300. We also report on results using word lookup tables of size 300, where we replace singletons with a special unknown token with probability 0.50.5 during training, which is then used for out-of-vocabulary words. For text fields, the context (cf. “Bi-LSTM” row) is encoded with a Bi-LSTM of size 300 for the forward and backward states. Finally, a linear layer maps the different input tokens into a common space with of size 300 (cf. “Linear” row). As for the attention model, we used an hidden layer of size 200 before applying the non-linearity (row “Tanh”). As for the decoder (Figure 4), we encode output characters with size 100 (cf. “output (y)” row), and an LSTM state of size 300 and an input representation of size 300 (cf. “State(h+z)” row). For each pointer network (e.g., “Copy From Name” box), the intersection between the input units and the state units are performed with a vector of size 200. Training is performed using mini-batches of 20 samples using AdaDelta [Zeiler, 2012] and we report results using the iteration with the highest BLEU score on the validation set (tested at intervals of 5000 mini-batches). Decoding is performed with a beam of 1000. As for compression, we performed a grid search over compressing the code from 0% to 80% of the original average length over intervals of 20% for the HS and Django datasets. On the MTG dataset, we are forced to compress the code up to 80% due to performance issues when training with extremely long sequences.

1 Results

Results are reported in Table 3. Regarding the retrieval results (cf. “Retrieval” row), we observe the best BLEU scores among the baselines in the card datasets (cf. “MTG” and “HS” columns). A key advantage of this method is that retrieving existing entities guarantees that the output is well formed, with no syntactic errors such as producing a non-existent function call or generating incomplete code. As BLEU penalizes length mismatches, generating code that matches the length of the reference provides a large boost. The phrase-based translation model (cf. “Phrase” row) performs well in the Django (cf. “Django” column), where mapping from the input to the output is mostly monotonic, while the hierarchical model (cf. “Hierarchical” row) yields better performance on the card datasets as the concatenation of the input fields needs to be reordered extensively into the output sequence. Finally, the sequence-to-sequence model (cf. “Sequence” row) yields extremely low results, mainly due to the lack of capacity needed to memorize whole input and output sequences, while the attention based model (cf. “Attention” row) produces results on par with phrase-based systems. Finally, we observe that by including all the proposed components (cf. “Our System” row), we obtain significant improvements over all baselines in the three datasets and is the only one that obtains non-zero accuracies in the card datasets.

Component Comparison

We present ablation results in order to analyze the contribution of each of our modifications. Removing the C2W model (cf. “– C2W” row) yields a small deterioration, as word lookup tables are more susceptible to sparsity. The only exception is in the HS dataset, where lookup tables perform better. We believe that this is because the small size of the training set does not provide enough evidence for the character model to scale to unknown words. Surprisingly, running our model compression code (cf. “– Compress” row) actually yields better results. Table 4 provides an illustration of the results for different compression rates. We obtain the best results with an 80% compression rate (cf. “BLEU Scores” block), while maximising the time each card is processed (cf. “Seconds Per Card” block). While the reason for this is uncertain, it is similar to the finding that language models that output characters tend to under-perform those that output words [Józefowicz et al., 2016]. This applies when using the regular optimization process with a character softmax (cf. “Softmax” rows), but also when using the LPN (cf. “LPN” rows). We also note that the training speed of LPNs is not significantly lower as marginalization is performed with a dynamic program. Finally, a significant decrease is observed if we remove the pointer networks (cf. “– LPN” row). These improvements also generalize to sequence-to-sequence models (cf. “– Attention” row), as the scores are superior to the sequence-to-sequence benchmark (cf. “Sequence” row).

Result Analysis

Examples of the code generated for two cards are illustrated in Figure 5. We obtain the segments that were copied by the pointer networks by computing the most likely predictor for those segments. We observe from the marked segments that the model effectively copies the attributes that match in the output, including the name of the card that must be collapsed. As expected, the majority of the errors originate from inaccuracies in the generation of the effect of the card. While it is encouraging to observe that a small percentage of the cards are generated correctly, it is worth mentioning that these are the result of many cards possessing similar effects. The “Madder Bomber” card is generated correctly as there is a similar card “Mad Bomber” in the training set, which implements the same effect, except that it deals 3 damage instead of 6. Yet, it is a promising result that the model was able to capture this difference. However, in many cases, effects that radically differ from seen ones tend to be generated incorrectly. In the card “Preparation”, we observe that while the properties of the card are generated correctly, the effect implements a unrelated one, with the exception of the value 3, which is correctly copied. Yet, interestingly, it still generates a valid effect, which sets a minion’s attack to 3. Investigating better methods to accurately generate these effects will be object of further studies.

Related Work

While we target widely used programming languages, namely, Java and Python, our work is related to studies on the generation of any executable code. These include generating regular expressions [Kushman and Barzilay, 2013], and the code for parsing input documents [Lei et al., 2013]. Much research has also been invested in generating formal languages, such as database queries [Zelle and Mooney, 1996, Berant et al., 2013], agent specific language [Kate et al., 2005] or smart phone instructions [Le et al., 2013]. Finally, mapping natural language into a sequence of actions for the generation of executable code [Branavan et al., 2009]. Finally, a considerable effort in this task has focused on semantic parsing [Wong and Mooney, 2006, Jones et al., 2012, Lei et al., 2013, Artzi et al., 2015, Quirk et al., 2015]. Recently proposed models focus on Combinatory Categorical Grammars [Kushman and Barzilay, 2013, Artzi et al., 2015], Bayesian Tree Transducers [Jones et al., 2012, Lei et al., 2013] and Probabilistic Context Free Grammars [Andreas et al., 2013]. The work in natural language programming [Vadas and Curran, 2005, Manshadi et al., 2013], where users write lines of code from natural language, is also related to our work. Finally, the reverse mapping from code into natural language is explored in [Oda et al., 2015].

Character-based sequence-to-sequence models have previously been used to generate code from natural language in [Mou et al., 2015]. Inspired by these works, LPNs provide a richer framework by employing attention models [Bahdanau et al., 2014], pointer networks [Vinyals et al., 2015] and character-based embeddings [Ling et al., 2015]. Our formulation can also be seen as a generalization of ?), who implement a special case where two predictors have the same granularity (a sub-token softmax and a pointer network). Finally, HMMs have been employed in neural models to marginalize over label sequences in [Collobert et al., 2011, Lample et al., 2016] by modeling transitions between labels.

Conclusion

We introduced a neural network architecture named Latent Prediction Network, which allows efficient marginalization over multiple predictors. Under this architecture, we propose a generative model for code generation that combines a character level softmax to generate language-specific tokens and multiple pointer networks to copy keywords from the input. Along with other extensions, namely structured attention and code compression, our model is applied on on both existing datasets and also on a newly created one with implementations of TCG game cards. Our experiments show that our model out-performs multiple benchmarks, which demonstrate the importance of combining different types of predictors.

References