N EURAL P ROGRAM S YNTHESIS WITH Q UERY
A BSTRACT
Aiming to find a program satisfying the user intent given input-output examples, program synthesis has attracted increasing interest in the area of machine learning.
Despite the promising performance of existing methods, most of their success comes from the privileged information of well-designed input-output examples.
However, providing such input-output examples is unrealistic because it requires the users to have the ability to describe the underlying program with a few inputoutput examples under the training distribution.
In this work, we propose a querybased framework that trains a query neural network to generate informative inputoutput examples automatically and interactively from a large query space.
The quality of the query depends on the amount of the mutual information between the query and the corresponding program, which can guide the optimization of the query framework.
To estimate the mutual information more accurately, we introduce the functional space (F-space) which models the relevance between the input-output examples and the programs in a differentiable way.
We evaluate the effectiveness and generalization of the proposed query-based framework on the
Karel task and the list processing task.
Experimental results show that the querybased framework can generate informative input-output examples which achieve and even outperform well-designed input-output examples.
I NTRODUCTION
Program synthesis is the task of automatically finding a program that satisfies the user intent expressed in the form of some specifications like input-output examples (Gulwani et al., 2017).
Recently, there has been an increasing interest in tackling it using neural networks in various domains, including string manipulation (Gulwani, 2011; Gulwani et al., 2012;
Devlin et al., 2017b), list processing (Balog et al., 2017; Zohar & Wolf, 2018) and graphic applications (Ellis et al., 2018; 2019).
Despite their promising performance, most of their success relies on the well-designed input-output examples, without which the performance of the program synthesis model drops heavily.
For example, in Karel (Devlin et al., 2017b; Bunel et al., 2018), the given input-output examples are required to have a high branch coverage ratio on the test program;
In list processing, the given input-output examples are guided by constraint propagation (Balog et al., 2017) to ensure their effectiveness on the test program.
However, providing such input-output examples is unrealistic because it requires the users to be experienced in programming to describe the underlying program with several inputoutput examples.
Worse, the users must be familiar with the distribution of the training dataset to prevent themselves from providing out-of-distribution examples (Shin et al., 2019).
In summary, how to generate informative input-output examples without expert experience is still an important problem that remains a challenge.
In this paper, we propose a query-based framework to automatically and efficiently generate informative input-output examples from a large space, which means that the underlying program can be easily distinguished with these examples.
This framework consists of two parts: the query network
[Figure]
and the synthesis network, and each part is trained separately.
The query network is trained to generate the input-output examples in an iterative manner, and then the synthesis network is trained to synthesize the program with the input-output examples generated by the query network.
It has three advantages: (1) There is no demand for well-designed input-output examples in both training and testing, which leads to a good generalization.
The query network works in an efficiently generative manner, which can essentially reduce the computation cost while facing problems with large input-output space.
The query network serves as a plug-and-play module with high scalability, for it is separated from the program synthesis process entirely.
To train the query network, the key idea is to model the query process as a decision tree and find out that the informativeness of the input-output examples is associated with the amount of mutual information ( i .
e .the information gain in the decision tree) between the input-output examples and the programs, and thus the query network can be optimized by maximizing the mutual information.
The mutual information can be approximated by the InfoNCE loss (van den Oord et al., 2018), which depends on the relevance between the samples.
However, due to the many-to-many relationship between the input-output examples and the programs, the relevance is difficult to be measured straightforwardly.
To this end, we introduce the functional space (F-space) .
In F-space, each program can be projected into a vector, and each input-output example corresponds to a set of candidate programs, which can be represented by a normal distribution (Sun & Nielsen, 2019).
Whenever a new example is queried, the new set of candidate programs is produced by the intersection of the two original sets of programs, and the new distribution is produced by the product of the two original distributions.
The more the queried examples, the smaller the size of the program set, the lower the entropy of the distribution.
With the InfoNCE loss and F-space, the query network is trained to generate the input-output examples whose F-space distribution can maximize the probability of the corresponding program and minimize the probability of the others.
Once the query network is trained, the training of the synthesis network is no different from what other researchers have done before, except that the input-output examples are generated by the query network.
We evaluate our method on the Karel task and the list processing task which have large input spaces.
Without utilizing the well-designed input-output examples both in training and testing, we achieve and even outperform the state-of-the-art performance on the Karel task and list processing task, which shows the effectiveness and generalization of our query-based framework.
P ROBLEM S TATEMENT
Intuitively, consider an oracle that contains some kind of symbolic rules ( e .
g .programs), our goal is to discover these symbolic rules inside this oracle.
To do this, the traditional program synthesis assumes that the oracle can provide some informative signals ( e .
g .input-output examples) automatically, based on which the synthesizer can find the rules.
However, this is a strong assumption with high requirements on the oracle that is impractical in many cases.
In this work, we consider a query problem where the informative signals are gained actively by querying the oracle under a much weaker assumption that the behavior of the oracle is deterministic ( i .
e .the same input always results in the same output).
Following the intuition, a reasonable solution for the query problem would be ”making the programs distinguishable with as few queries as possible”.
To make this statement concrete and practical, we introduce the following formulations.
First, we define the functional equivalence, which is consistent with the definition of the equivalence of functions in mathematics.
Definition 2.1 (Functional equivalence) .
Let I be the input example domain containing all valid input examples, O be the output example domain containing all possible output examples, and P be the program domain containing all valid programs under the domain specific language (DSL), each program p ∈ P can be seen as a function p :
I → O .
For two programs p i ∈ P and p j ∈ P , p i and p j are functional equivalent if and only if ∀ x ∈
I , p i ( x ) =
p j ( x ) .
Using the concept of functional equivalence, we can formulate the program synthesis task:
Definition 2.2 (Program synthesis) .
Suppose there is an underlying program p ∈ P and K inputoutput examples (cid:74) e (cid:75)
= { ( x k , y k ) | ( x k , y k ) ∈
I
×
O , k = 1 · · · K } generated by it, the program synthesis task aims to find a program ˆ p which is functional equivalent to p using (cid:74) e (cid:75) .
Following the definitions above, we can define the task of the query.
Definition 2.3 (Query) .
Given a program p ∈ P and a set of history K input-output examples (cid:74) e (cid:75) , the query process firstly generates a query by the query network f q : x K +1 =
f q ( (cid:74) e (cid:75) )
∈
I .
Then, a corresponding response is given by the program y K
+1 = p
( x K +1 ) ∈ O .
The query and response are added to the history input-output examples: (cid:74) e (cid:75) ← (cid:74) e (cid:75) ∪ { ( x K +1 , y K +1 ) } and this process repeats.
The query process aims to distinguish the underlying program with as few queries as possible, based on which we can define the optimal query strategy.
Definition 2.4 (The optimal query strategy) .
Given a set of programs P and a target program p ∗ , the optimal query strategy Q aims to distinguish the p ∗ by generating as few input-output examples (cid:74) e (cid:75) as possible.
When the query space is large, it is impractical to find the optimal query strategy by direct search.
Thus, we take the query process as the expansion of a decision tree where the leaves are the programs, the nodes are the queries, and the edges are the responses, and then the generation of queries can be guided by the information gain, which is also known as the mutual information:
[Equation]
M ETHODS
The mutual information is hard to calculate due to the large spaces of queries and programs.
To this end, we resort to the InfoNCE loss (van den Oord et al., 2018) which estimates the lower bound of the mutual information:
[Equation]
and
I
[Equation]
(3) where f ( · , · ) denotes a relevance function.
Maximizing the mutual information is equivalent to minimizing L NCE .
Intuitively, InfoNCE maximizes the relevance between the positive pairs and minimizes the relevance between the negative pairs.
Specifically, for a batch of data { ( (cid:74) e (cid:75) n , p n ) , p n } N , we construct positive pairs as { ( (cid:74) e (cid:75) i , p i ) } and negative pairs as { ( (cid:74) e (cid:75) i , p j ) | i ̸
= j } .
Traditionally, the relevance is defined as the dot product of the samples, which may be inaccurate for the manyto-many relationship between the input-output examples and the programs.
Thus next, we will introduce functional space (F-space) to model the relationship properly.
3. F- SPACE
Definition 3.1 (F-space and functional distance) .
F-space is a | I | dimensional space which consists of all valid programs that can be implemented by program domain P .
Each program is represented by | I | different output examples v =
( y 1 , y 2 , . .
y | I | ) .
The distance in F-space can be measured by the number of different output examples: d ( v i , v j ) =
| diff ( v i , v j ) | .
[Figure]
Intuitively, F-space ( P , d ) measures the functional differences between programs.
Each program can be represented by a vector in F-space, and if different programs are represented by the same vector v
= ( y 1 , y 2 , . .
y | I | ) , it indicates that these two programs get the same results for all inputs, and they are considered to be functionally equivalent.
This is also consistent with Definition 2.1.
In practice, a space with dimension | I | is too large to compute, and thus we utilize the sparsity of
F-space and learn an approximate space with dimension reduction by neural networks.
Regarding input-output examples, representing them by vectors is not appropriate.
Consider a set of input-output examples (cid:74) e (cid:75) =
{ ( x k , y k ) } K , we cannot find a vector that represents it when K < | I | because there are more than one programs that satisfies these K input-output examples.
Formally, we summarize the properties of the representation of input-output examples in F-space as follows:
• Each set of input-output examples (cid:74) e (cid:75) =
{ ( x k , y k ) } K should be represented by a set of F-space vectors (cid:74) r (cid:75)
= { v n } N .
• For two sets of input-output examples (cid:74) e (cid:75)
i and (cid:74) e (cid:75) j , if (cid:74) e (cid:75) i ⊆
(cid:74) e (cid:75) j , then their F-space representations (cid:74) r (cid:75)
i and (cid:74) r (cid:75)
j have the relationship (cid:74) r (cid:75) i ⊇ (cid:74) r (cid:75)
j . • For two sets of input-output examples (cid:74) e (cid:75)
i and (cid:74) e (cid:75) j , suppose (cid:74) e (cid:75) ′ =
(cid:74) e (cid:75) i ∩
(cid:74) e (cid:75)
j , then in F-space, their corresponding representations have the relationship
[Equation]
• Similarly, if (cid:74) e (cid:75) ′
= (cid:74) e (cid:75)
i ∪ (cid:74) e (cid:75) j , then in F-space, their corresponding representations have the relationship (cid:74) r (cid:75) ′
= (cid:74) r (cid:75) i ∩ (cid:74) r (cid:75)
j
Additionally, this representation should be differentiable and thus can be optimized by neural networks.
To this end, we model the representation of (cid:74) e (cid:75) as a Normal distribution (Ren & Leskovec, 2020; Sun & Nielsen, 2019), where the probability of the distribution indicates the possibility that it is the underlying program to be synthesized given input-output examples (cid:74) e (cid:75) .
We illustrate the projection, the subset relationship, and the union/intersection operation of distributions in Figure 2.
Under this representation, the query process becomes the process of reducing the uncertainty of the distribution.
To train the query network, we define several neural operators for the neural network training as follows.
Program projection.
To project a program p to a vector v in F-space, we traditionally use a sequence model as our encoder:
[Equation]
Note that the program synthesis model differs largely on different datasets, so we choose to vary our program encoder according to the specific program synthesis models on different datasets.
In practice, our program encoder is the reverse of the program synthesis decoder.
Input-output example projection.
Given a single input-output example (cid:74) e (cid:75) =
{ ( x, y ) } , we can project it into a Normal distribution using the same architecture of the corresponding program synthesis model, except that we add an MLP to output the two parameters of the Normal distribution: µ and log ( σ 2 ) .
[Equation]
(5) where MLP means a multi-layer perceptron.
Query 𝑥 1 ,𝑦…… 𝑥 𝑡−1 , 𝑦 𝑡− 𝑥 𝑡
Query 𝑥 𝑡+
Query 𝑥 𝑡+
InfoNCEloss
Program 𝑥 1 ,𝑦…… 𝑥 𝑡−1 , 𝑦 𝑡− 𝑥 𝑡 , 𝑦 𝑡 𝑥 𝑡+1 , 𝑦 𝑡+ 𝑥 1 ,𝑦…… 𝑥 𝑡−1 , 𝑦 𝑡− 𝑥 𝑡 , 𝑦 𝑡
Program
Program …… ……
InfoNCE loss
InfoNCEloss
Input-output examples intersection.
Given K input-output examples (cid:74) e (cid:75) =
{ ( x k , y k ) } K , each example { ( x k , y k ) } is represented by a Normal distribution Pr k = N ( µ k , σ k ) using the projector above.
The purpose of the intersection is to aggregate these Normal distributions into a new one, which represents (cid:74) e (cid:75) in F-space.
Under the assumption of independence, the probability of the intersection distribution should be:
[Equation]
Fortunately, the product of independent Normal distributions is still a Normal distribution, which means that we can represent it as [ µ ′ , log ( σ ′ 2 )] .
In practice, we utilize the attention mechanism with another MLP to let the neural network learn the new Normal distribution (Ren & Leskovec, 2020):
[Equation]
This formulation not only keeps the form of distributions closed but also approximates the mode of the effective support of Normal distributions, which satisfies our requirement on the intersection of distribution (Sun & Nielsen, 2019).
We give detailed proof on the reasonability of doing this in
Appendix C.1.
Inverse projection to query.
Finally, we need to generate a new query from the representation of input-output examples in F-space.
Using the projection and the intersection operation mentioned above, we can project the K input-output examples into the representation [ µ ′ , log ( σ ′ 2 )] .
To generate query x new from this representation, we introduce a decoder which has a similar architecture to
Encoder e but reversed:
[Equation]
With these operators, we can model the relevance between input-output examples and programs as the probability in the distributions in F-space, and rewrite the loss in Equation 2 as
[Equation]
Next, we will introduce how to train the neural network.
3. T RAINING
As mentioned above, the query network is optimized by maximizing the mutual information.
We observe that taking previous query examples as the condition and only optimizing the query strategy at the current query step is a greedy optimization, which may fail into the local minima more easily.
An example is shown in Appendix C.2.
Thus, a recurrent training process is adopted to grep the global mutual information on every step instead of the mutual information conditioned on the previous examples.
See Figure 3 and Algorithm 1.
Additionally, like the task of sequence generation, we need an input-output example to act as the start signal < sos > .
However, different datasets differ largely in program synthesis, which makes the design of a universal start signal difficult.
Thus, we design specific start signals for each dataset separately.
For example, in Karel, the signal is designed to be an empty map with the robot at the center of the map;
In list processing, the signal is just three lists full of NULL .
More training details such as the network architectures, the processing of datasets, training tricks, can be seen in
Appendix A.
E XPERIMENTS
We studied three problems in our experiments.
(1) The metric that is reasonable in our query-based framework.
The performance of the query-based framework.
The generalization ability on different tasks of the query-based framework.
To do this, first, we demonstrate a comparison among several program synthesis metrics.
Then, we present our results of the query on the Karel dataset and the list processing dataset to show our methods’ performance and generalization ability.
4. METRICS
There are three metrics in neural program synthesis.
• Semantics : Given 5 input-output examples, if the predicted program satisfies all these examples, then it is semantically correct.
• Generalization : Given 5 input-output examples and a held-out input-output example, if the predicted program satisfies all 6 examples, then it is generally correct.
• Exact match : If the predicted program is the same as the ground-truth program, then it matches the ground-truth exactly.
Among them, the exact match is the most strict metric and
generalization
is the second one.
In practice, the exact match has its drawback in that the predicted program may be different from the ground-truth program, but they are functionally equivalent.
Thus, the performance is measured by generalization on Karel and semantics on list processing traditionally.
However, generalization is not appropriate here for two reasons: (1) It can only measure the performance of the program synthesis process instead of the query process.
In the query process, the network chooses inputoutput examples independently, and judging them by the held-out example is meaningless.
(2) Even for the program synthesis process without query, generalization is not strict enough.
The program that satisfies a small set of input-output examples may fail on a larger set of input-output examples.
Thus, the best choice is to use functional equivalence as our metric.
Unfortunately, the judgment of functional equivalent is a notoriously difficult problem which makes it hard to be applied in evaluation.
To alleviate this problem, we generate 95 held-out input-output examples randomly to achieve a higher branch coverage than 1 held-out example, and make the generalization on these examples a proxy of the functional equivalence:
• Functional equivalence (proxy) :
Given 5 input-output examples and 95 held-out inputoutput examples, if the predicted program satisfies all 100 examples, then it is functional equivalent to the ground-truth program.
To illustrate the difference between functional equivalence and generalization further, we measured the average branch coverage of these two metrics on Karel’s validation set.
Given a set of inputoutput examples, branch coverage evaluates the percentage of program branches that are covered by the examples.
[Table]
The result is shown in Table 1.
Functional equivalence outperforms generalization by nearly 10%, which indicates that functional equivalence can represent the correctness of the predicted program much better than generalization.
4. K AREL TASK
Karel is an educational programming language used to control a robot living in a 2D grid world (Pattis et al., 1981).
The domain-specific language (DSL) and other details are included in Appendix B.1.
Settings.
Following Section 3, we split the training process into the training of the query network and the training of the synthesis network.
For the query network, we set Encoder e the same as the one of Bunel et al.
(2018) except for the output layer, and Encoder p a two-layer LSTM (see details in Appendix A.2).
For the synthesis network, all settings stay unchanged as in the original program synthesis method.
We report the top-1 results with beam size 100 for Bunel et al.
(2018) and 64 for
Chen et al.
(2019) which is the default setting for validation in the original code.
As in Section 4.1, exact match and functional equivalence are more appropriate than other metrics in the query task, so we save the checkpoints based on the best exact match instead of the best generalization (for the computation inefficiency of functional equivalence), which may cause differences in our baseline reproduction.
We generate the dataset with 5 input-output examples using three different methods: randomly selected (random), baseline models (well-designed), and our method (query).
Then, we train the synthesis network on these datasets with two state-of-the-art methods: Bunel et al.
and Chen et al.
Note that there is a slight difference between the program simulators used by them which will give different responses during the query process, and we choose the one in Bunel et al.
(2018) as ours.
Dataset Performance.
Table 2 presents the performance of the trained synthesis networks, from which we can conclude that (1) The queried dataset performs well on both two training methods, indicating that the query process is totally decoupled with the synthesis process and has a high generality.
(2) Our query method gets comparable results with both baseline methods and largely outperforms the random selection method with more than 10%, which shows its effectiveness.
   
[Figure]
      
[Figure]
Comparison on query.
We also compared our method with query by committee (QBC) Seung et al.
(1992) as another baseline, shown in Figure 4.
In QBC, we sample queries based on their diversity.
That is, we generate program candidates by beam search, and then select the query that can result in the most diverse outputs on these program candidates.
The diversity is measured by the output equivalence.
Algorithm details can be seen
[Figure]
[Table]
in Appendix B.4.
There are two strategies based on QBC: Crash-aware, which repeats the algorithm to sample another one if the query crashes; And crash-unaware, which samples queries regardless of the crash problem (see Appendix B.1 for a detailed explanation of crashes).
QBC-crash-unaware performs worse than Random because Random is chosen by filtering crashed IOs while QBC-crashunaware may contain crashes.
QBC-crash-aware performs much better than QBC-crash-unaware because it queries the underlying program multiple times to make sure that the query will not result in a crash, which is unfair.
Even though, out method still outperforms QBC-crash-aware, which shows its advantage.
Training process.
To do a further study, we plot the training curve in Figure 5.
It is shown that the Query dataset always has a quick start, which indicates that the query method can extract the features of the distinguishable programs effectively and makes them easier to be synthesized.
4. L IST P ROCESSING T ASK
To show the generalization ability of the query method, we conduct another experiment on the list processing task.
The list processing task takes 1-3 lists or integers as the input example, and then produces a list or an integer as the output example.
More details can be seen in Appendix B.2.
Settings.
Following PCCoder (Zohar & Wolf, 2018), we generate two datasets with program length 4 as dataset D 1 and program length up to 12 as dataset D 2 .
We set the Encoder e of the query network similar to PCCoder except for an MLP, and use a single layer LSTM as the Encoder p (see Appendix A.2).
The synthesis network and the parameters of complete anytime beam search (CAB) (Zhang, 1998) stay the same as PCCoder, except that the maximum time is set to 5 seconds instead of 5,000 seconds for reality.
Similar to the Karel task, we generate the dataset with 5 input-output examples with three different methods.
Furthermore, we use two end conditions of CAB:
The searching ends when all inputoutput examples are satisfied (searching for semantics), and the searching ends when all statements are true (searching for exact match).
Performance.
The results are presented in Table 3, from which we can conclude that: (1) Our query method results higher than well-designed input-output examples in searching for the exact match and consistently outperforms the random.
(2) When the length of programs increases, the performance decreases largely.
This results from the original algorithm of PCCoder that the inter- mediate variables are difficult to be saved when the ground-truth program is long.
However, the query method decreases slower than others, and the gap between the well-designed and the query is closed largely.
R ELATED W ORK
Programming by examples.
Synthesizing a program that satisfies the user intent using the provided input-output examples is a challenging problem that has been studied for years (Manna &
Waldinger, 1971; Lieberman, 2001; Solar-Lezama et al., 2006; Gulwani, 2011; Gulwani et al., 2012).
Recently, with the development of Deep Learning, more researchers tend to tackle this problem with neural networks in a variety of tasks, including string transformation (Devlin et al., 2017b), list processing (Balog et al., 2017; Zohar & Wolf, 2018), graphic generation (Ellis et al., 2018;
Tian et al., 2019), Karel (Devlin et al., 2017a; Bunel et al., 2018), policy abstraction (Sun et al., 2018;
Verma et al., 2018) and so on.
Additionally, techniques like program debugger (Balog et al., 2020;
Gupta et al., 2020), traces (Chen et al., 2019; Shin et al., 2018;
Ellis et al., 2019), property signatures (Odena & Sutton, 2020; Odena et al., 2021) are also used to improve the performance of program synthesis.
However, their promising performance relies on the well-designed input-output examples which is a high requirement for users.
If the examples provided are of poor quality, the performance will be affected severely.
Worse, if out-of-distribution examples are provided, the program synthesis model cannot finish the task as expected (Shin et al., 2019).
Interactive program synthesis.
Considering the high requirement of input-output examples, Le et al.
(2017) build an abstract framework in which the program synthesis system can interact with users to guide the process of synthesis.
Following this framework, multiple interactive synthesis systems are studied.
Among them, Mayer et al.
(2015), Wang et al.
(2017), and Laich et al.
(2020) tend to find counter-examples as queries to the users in a random manner.
Although they get rid of the restrictions on the users, the quantity of the input-output examples is not guaranteed, which may get the synthesis process into trouble.
Padhi et al.
(2018) select more than one query each time and let the users choose which one to answer.
This brings an additional burden on the users, and the users are unaware of which query will improve the program synthesized most.
Most recently,
Ji et al.
(2020) utilizes the minimax branch to select the question where the worst answer gives the best reduction of the program domain.
Theoretically, the performance of the selection is guaranteed.
However, this method is based on search and hardly be applied to tasks with large input-output space.
Worse, the query process and the synthesis process are bound, which results in its poor scalability.
In contrast, our method can be applied to problems with large spaces, and the query process is decoupled with the synthesis process, which makes its application more flexible.
Learning to acquire information.
Similar to our work, Pu et al.
and Pu et al.
(2017) also study the query problem from an information-theoretic perspective.
They show that maximizing the mutual information between the input-output examples and the corresponding program greedily is 1 − 1 e as good as the optimal solution that considers all examples globally.
However, they assume that the space of queries can be enumerated, which limits the application of their query algorithm on complex datasets like Karel.
By comparison, our work proposes a more general algorithm that can generate queries in a nearly infinite space.
Other related work including active learning and black-box testing can be seen in Appendix D.
C ONCLUSION
In this work, we propose a query-based framework to finish the program synthesis task more realistically.
To optimize this framework, we show the correlation between the query strategy and the mutual information.
Moreover, we model the relevance between the input-output examples and programs by introducing the F-space, where we represent the input-output examples as the distribution of the programs.
Using these techniques, we conduct a series of experiments that shows the effectiveness, generalization, and scalability of our query-based framework.
We believe that our methods work not only on the program synthesis tasks, but also on any task that aims to simulate an underlying oracle, including reverse engineering, symbolic regression, scientific discovery, and so on.
A CKNOWLEDGMENTS
This work is partially supported by the National Key Research and Development Program of China (under Grant 2020AAA0103802), the NSF of China (under Grants 61925208, 62102399, 62002338, 61906179, 61732020, U19B2019), Strategic Priority Research Program of Chinese Academy of
Science (XDB32050200), Beijing Academy of Artificial Intelligence (BAAI) and Beijing Nova Program of Science and Technology (Z191100001119093), CAS Project for Young Scientists in Basic
Research (YSBR-029), Youth Innovation Promotion Association CAS and Xplore Prize.
A T RAINING D ETAILS A
T RAINING ALGORITHM
1: function T RAIN ( ) 2:
Initialize max iterations N and max query times T 3: for i ∈ { 1 . . .
N } do 4: p ← Underlying programs 5: ( x 0 , y 0 )
← < sos > 6: (cid:74) e (cid:75) ← { ( x 0 , y 0 ) } 7:
L ←8: for t ∈ { 1 . . .
T } do 9: v ← Encoder p ( p ) ▷ Encode program, Equation (4) 10: µ , log ( σ 2 ) ← IO-E NCODER ( (cid:74) e (cid:75) )
▷ Encode input-output examples 11: x t ← Decoder ( µ , log ( σ 2 ))
▷ Query next input 12: y t ← p ( x t ) ▷ Get next output from the oracle 13: (cid:74) e (cid:75) ← (cid:74) e (cid:75) ∪ { ( x t , y t ) } 14: µ ′ , log ( σ ′ 2 ) ← IO-E NCODER ( (cid:74) e (cid:75) ) 15:
L ← Loss ( v , µ ′ , log ( σ ′ 2 ))
+ L ▷ InfoNCE loss, Equation (2) 16: end for 17:
Update parameters w.r.t.
L 18: end for 19: end function 1: function IO-E NCODER ( { x k , y k } K ) 2: for i ∈ { 1 . . .
K } do 3:
[ µ i , log ( σ 2 i )]
← MLP e ( Encoder e ( { x i , y i } )
▷ Encode single example, Equation (5) 4: end for 5: for i ∈ { 1 . . .
K } do 6: w i ← exp ( MLP attention ([ µ i ,log ( σ 2 i )]))
(cid:80) Kj =1 exp ( MLP attention ( µ j ,log ( σ 2 j )))
▷ Calculate attentions, Equation (8) 7: end for 8: [ µ , log ( σ 2 )]
= (cid:80)
Ki
=
w i [ µ i , log ( σ 2 i )]
▷ Intersection, Equation (7) 9: return µ , log ( σ 2 ) 10: end function
A. M ODEL DETAILS AND H YPER PARAMETERS
Karel.
The query encoder is the same as the one used by Bunel et al.
(2018) composed of a
Convolutional Neural Network (CNN) with the residual link.
After the query encoder, there is an
MLP to project the embedding into µ , log ( σ 2 ) .
The dimension of µ and σ is set to 256, and thus the hidden size of MLP is 512.
The query decoder is similar to the query encoder except that the number of channels is reversed, and additional batch normalization is added to keep the generation more stable.
The program encoder is a two-layer LSTM with a hidden size of 256.
Note that in the well-designed dataset, the size of the grid world can be changed from 2 to 16.
However, for the convenience of training, we set the size of the query world fixed to 16.
Moreover, to guarantee that all queries can be recognized by the program simulator, we split the query into three parts: boundaries (the map size, set to 16 × 16), agent position (where the agent is and towards), and map state (the placement of markers and obstacles), and generate them as follows:
• Boundaries : The boundaries indicate the map size, fixed to 16 × 16 .
• Agent position : generate a 4 × 16 × 16 one-hot vector where 4 indicates four facing directions and 16 × 16 indicates the position.
• Map state : generate a 12 one-hot vector on the 16 × 16 map including obstacle, 1markers, and empty grid.
[Figure]
Linear
IO embeddings
[Figure]
examples × N
IO embeddings
MLP
Linear
Linear 𝜇 log (𝜎 (cid:2870) )
Linear
Activation
ConvReLU ConvReLU
Conv
ReLU
[Figure]
layers Activation
ConvBNLReLU
ConvBNLReLU ConvBNLReLU
Next layers
Latent
[Figure]
Reshape
Decoder
Linear
[Figure]
GumbelSoftmax GumbelSoftmax
[Figure] [Figure]
Null for a better view).
Type filter chooses query between the list proposition and int proposition depends on the input types.
The architecture details are shown in figure 6.
For training, the learning rate of the query network is set to 10 − 4 with the Adam optimizer Kingma &
Ba (2014) while the learning rate of the synthesis network stays the same with the original methods.
The batch size is 128, and the random seed is set to 100.
List processing.
The query encoder is the same as the one in PCCoder.
The dimension of µ and σ is set to 256 for dataset D 1 and 128 for dataset D 2 without much tuning.
The query decoder is a single-layer linear network with dimension 256 for dataset D 1 and 128 for dataset D 2 .
The program encoder is a single layer LSTM with hidden size 256 for dataset D 1 and 128 for dataset D 2 .
The inputs of list processing consist of three types: INT, LIST, and NULL.
Thus, each query is
NULL or an integer or a list consisting of integers in the range of [ − 256 , 255] .
Each integer is represented by a 512 one-hot vector.
For the well-designed input-output examples, the length of a LIST is sampled stochastically with the maximum length of 20.
However, to make the query network simpler, we fix the length of queries to 20.
The query network generates all three types
Prog p :=
def run () : s
Stmt s := while ( b ) : s | repeat ( r ) : s | s 1 ; s 2 | a | if ( b ) : s | ifelse ( b ) : s 1 else : s
Cond b := frontIsClear () | leftIsClear () | rightIsClear () | markersPresent () | noMarkersPresent () | not b
Action a :=
move () | turnRight () | turnLeft () | pickMarker () | putMarker ()
Cste r :=
| 1 | · · · | separately by different networks and chooses among them according to the type of inputs using a type filter.
The details of the query network for list processing are shown in figure 7.
For training, the learning rate of the query network is set to 10 − 4 with a 0 .
1 decay every 40 epochs and the Adam optimizer Kingma & Ba (2014).
The learning rate of the synthesis network is 10 − 3 , which stays the same with the original methods with 0 .
1 decay every 4 epochs.
The batch size of the query process is 64, the batch size of synthesis is 32 for D 1 and 100 for D 2 .
The random seed is set to 100.
A. O THER TRAINING TECHNIQUES
Latent code.
The subsequent queries are easy to fail into the mode collapse, generating similar queries repetitively.
To tackle this problem, we introduce a latent code, like the one in InfoGAN (Chen et al., 2016), as another input, which indicates the current query step, and ask the encoder in the next query step to decode it accurately.
Specifically, given a latent code c which indicates the current query step, the query network is supposed to maximize the mutual information between c and the output query x to alleviate the mode collapse problem.
To generate x under the condition of c , we concatenate c with the encoded representation µ and log ( σ 2 ) , and then send it to the decoder (Equation (9)), shown in Figure 6(g) and Figure 7(e).
To maximize the mutual information I ( c ; x ) , we design a network aiming to classify c from x , which models the distribution Q ( c | x ) (details can be seen in Chen et al.
This network shares its parameters with the Encoder e (Equation (5)) except for the last layer, and it is optimized with the cross-entropy loss.
Gumbel-Softmax.
Note that the queries are discrete, which makes the query network cannot be optimized by back-propagation.
Thus, we take advantage of the Gumbel-Softmax distribution (Maddison et al., 2017; Jang et al., 2017) and make the query process differentiable.
Curriculum learning.
The query network is trained progressively.
At the beginning of the training, the query network generates one query only.
As the training goes on, the limit of the number of queries increases until it achieves five.
Specifically, this limit increases every two epochs.
Curriculum learning helps to make the training process more stable.
Kullback-Leibler (KL) divergence.
A failed attempt.
Similar to the latent code, we tried to add the reciprocal of the KL divergence between different queries on the same program as another loss to make the queries more diverse.
However, this loss does not have a significant impact on training most of the time and makes the training process unstable, and thus it is abandoned in our final version.
[Table]
def run(): move() putMarker() turnRight() move()
Input
Output
Program
B E XPERIMENT D ETAILS B. T HE K AREL TASK
Karel is an educational programming language, which can be used to control a robot to move in a 2D grid world (Pattis et al., 1981).
Figure 8 presents the domain language specification (DSL) of
Karel.
Figure 9 shows a Karel program example.
Each cell of the grid world is represented as a 16-dimensional vector corresponding to the features described in Table 4 (Bunel et al., 2018).
Handling of crashes.
The executor of Karel will get a ”CRASH” result and then terminates if the agent:
• Picks a marker while no marker is presented.
• Puts a marker down while the number of the markers in the cell exceeds the limit (the limit is set to 10).
• Walks up an obstacle or out of boundaries.
• Falls into an infinite loop (the loop with more than 10 5 API calls).
In the early stage of training, the query network generates the queries randomly, and thus the programs run into these crashed states easily, making the queries cannot be applied to training.
To avoid these situations, we modify the executor so that when crashes happen, the state stays still without change and the program keeps executing.
Basic Function := + 1 | − 1 | × 2 | ÷ 2 | × ( − 1 ) | ∗ ∗ 2 | × 3 | ÷ 3 | × 4 | ÷| > 0 | < 0 | % 2 | % 2 ==
First-order Function := HEAD | LAST | TAKE | DROP | ACCESS |
MINIMUM | MAXIMUN | REVERSE | SORT | SUM
Higher-order Function := MAP | FILTER | COUNT | ZIPWITH | SCANL
FILTER (<0)
MAP ( × 4)
SORT
REVERSE
Input
Output
Program [-17, -3, 4, 11, 0, -5, -9, 13, 6, 6, -8, 11] [-12, -20, -32, -36, -68]
B. T HE L IST P ROCESSING T ASK
The list processing task takes 1-3 lists or integers as input examples and produces a list or an integer as the output example.
An example is shown in Figure 11.
Figure 10 shows the DSL of list processing.
Following Zohar & Wolf (2018), we generate two datasets: D 1 with program length 4 and Dwith program length up to 12.
Handling of the out of range problem.
Constraint propagation guarantees that the execution of programs never obtains intermediate values that are out of range [ − 256 , 255] .
However, when we query randomly in the early stage of training, the out-of-range problem can easily occur.
To handle this problem, we truncate the intermediate values to [ − 256 , 255] while querying to ensure that all queries can yield legal responses.
B. T HE EVOLUTION OF DISTRIBUTION ENTROPY
To make a sanity check of the F-space formulation, we study how the entropy of the distribution changes over the step of the query.
The entropy of a multivariate Normal distribution N ( x ; µ, Σ)) is given by
[Equation]
(11) where Σ denotes the covariance matrix and D denotes the dimension of x .
In our case, we have assumed the independence of each dimension of x which means that Σ is a diagonal matrix.
Hence
[Equation]
where σ 2 i is the diagonal element of Σ , indicating the variance of each dimension.
D is a constant during querying, so we calculate the mean of log ( σ 2 ) as an equivalent substitute to show the change of the entropy.
Results are shown in Figure 12.
In our experiments, Karel performs best and this is also revealed by the entropy that Karel decreases much faster than list processing.
On the contrary, the entropy of list processing decreases slowly and has worse performance in our experiment.
This performance may be increased by tuning the query network more carefully.
     4XHU\VWHS     l o g i 1e.DUHO      4XHU\VWHS  

 l o g i 1e/LVW'      4XHU\VWHS      l o g i 1e/LVW'
[Table]
[Table]
B. Q UERY BY COMMITTEE
In this section, we present the details of our baseline algorithm: query by committee (Seung et al., 1992).
Algorithm 2 shows the crash-aware version of the QBC algorithm, which selects the query that can result in the most diverse outputs.
The crash-unaware version can be obtained simply by removing all CRASH judgments.
Note that, compared with the crash-unaware version, the crashaware version queries the underlying program more times for CRASH judgment (see Table 5), and thus results in a much better performance.
C T HEOREMS AND PROOFS C. T HE PRODUCT OF TWO NORMAL DISTRIBUTIONS
In this section, we will show that the product of two normal distributions is a scaled normal distribution.
Theorem C.1 (The product of two normal distributions) .
Given two normal distributions that satisfies p ( x ) =
[Equation]
e −
[Equation]
µ
[Equation]
σ 2 , the product of them is a scaled normal ”distribution” in the form of α
[Equation]
e
[Equation]
σ ′ 2 , where α is the scale factor.
Proof.
Suppose we have two normal distributions p a and p b :
[Equation]
The product of p a and p b :
[Equation]
1: function Q UERY ( ) 2:
Initialize trained model M , query pool Q and max query times T 3: p ← Underlying program ( oracle ) 4: repeat 5: x 1 ← samplefromQ 6: y 1 ← p ( x 1 ) 7: until y 1 not CRASH 8: (cid:74) e (cid:75) ← { ( x 1 , y 1 ) } 9: for t ∈ { 2 . . .
T } do 10: program candidates (cid:74) ˆ p (cid:75) ← M ( (cid:74) e (cid:75) )
▷ Get top K predictions by beam search 11: x t ← S ELECT -Q
UERY ( Q, p, (cid:74) ˆ p (cid:75) )
12: y t ← p ( x t ) 13: (cid:74) e (cid:75) ← (cid:74) e (cid:75) ∪ { ( x t , y t ) } 14: end for 15: return (cid:74) e (cid:75) 16: end function 1: function S ELECT -Q
UERY ( Q, p, (cid:74) ˆ p (cid:75) ) 2: repeat 3: queries ← sample 100 times from Q 4: score list ← [] ▷ Acquisition scores of queries 5: for q ∈ queries do 6: s q ←7: for ˆ p ∈ (cid:74) ˆ p (cid:75) do 8:
ˆ y ← ˆ p ( q ) 9: if ˆ y is unique then 10: s q ← s q
+▷ The more diversity the output, the better the query 11: end if 12: end for 13: score list.append (( q, s q )) 14: end for 15:
Sort score list in a descending order with s q 16: for q ∈ score list do ▷ Select the query with the highest score and without CRASH 17: y ← p ( q ) 18: if y not CRASH then 19: return q 20: end if 21: end for 22: until a query is found ▷ If all 100 queries result in CRASH, repeat this process.
23: end function
Consider the index part
[Equation]
where
[Equation]
Simplify λ :
[Equation]
Finally
[Equation]
we get
[Equation]
where
[Equation]
Next, we will show that approximating the new µ ′ and log ( σ ′ 2 ) with the weighted sum of the [ µ a , µ b ] and [ log ( σ 2 a ) , log ( σ 2 b )] is reasonable.
For µ ′ , it is obvious that it can be seen as
[Equation]
which is a weighted sum.
For log ( σ ′ 2 ) , we can rewrite it as follows
[Equation]
[Figure]
where h and k are two coefficients.
With suitable h and k learned, the last term can be ignored (
[Equation]
≈ 1 ) and thus log ( σ ′ 2 ) can be approximate by the weighted sum of [ log ( σ 2 a ) , log ( σ 2 b )] .
C. T HE RECURRENT TRAINING PROCESS
We adopt a recurrent training process to model the mutual information between the input-output examples and the programs more accurately.
Traditionally, the next query is gained by selecting the query with the maximum mutual information conditioned on the former ones:
[Equation]
However, this greedy strategy fails in some cases.
An example is shown in Figure 13.
Suppose that after a series of queries, and finally there are only 8 candidate programs P = { p 0 , ..., p 7 } distributed uniformly and 4 queries Q = { q A , ..., q D } each with 2 possible responses {√ , ×} left, our goal is to find out the underlying program with as few queries as possible (conditioned on the former queries which are omitted).
According to the definition of the mutual information I ( X ; Y )
= (cid:80) x ∈ X (cid:80)
y ∈ Y P ( x, y ) log P ( x,y ) P ( x ) P ( y ) , we can calculate the mutual information between the 4 queries and the programs as follows (we use q A − D = √ / × to represent p i ( q A − D ) = √ / × for simplicity):
[Equation]
Samilarly, I ( P ; q B ) =
I ( P ; q C ) =
I ( P ; q D ) = 1 .
Thus, Equation 22 suggests that 4 queries share the same priority.
When the query process continues, however, this is not the case.
For the second query (let q
=
( q A , q B ) ):
[Equation] [Equation]
[Equation]
where {√ , ×} 2 = {√ , ×} × {√ , ×} .
Similarly,
I
[Equation]
P ; ( q A
[Equation]
q C
[Equation]
P
[Equation]
q A
[Equation]
q D
[Equation]
I
[Equation]
P
[Equation]
( q C , q D )
[Equation]
=
[Equation] [Equation]
Equation 24 and Equation 25 show that considering the longer horizon, q B is the worst choice as the first query because it gets the minimum mutual information whichever the second query is.
This conclusion is also straightforward without calculation: if we choose q A,C,D as the first query, then we only need to query for two more times; And if we choose q B as the first query, then we need 3.
queries on average with 4 queries in the worst case.
To this end, we adopt a recurrent training process as shown in Figure 3.
In the recurrent training process, the gradient can be propagated through multiple query steps and thus the current query selection can be affected by future queries.
D A DDITIONAL RELATED WORK D. A CTIVE L EARNING
Active learning is a research domain that aims to reduce the cost of labeling by selecting the most representative samples iteratively from the unlabeled dataset and then asking them to an oracle for labeling while training.
According to Shui et al.
(2020), active learning can be divided into pool-based sampling (Angluin, 1988;
King et al., 2004), which judges whether a sample should be selected for query-based on the evaluation of the entire dataset; steam-based sampling (Dagan & Argamon, 1995; Krishnamurthy, 2002), which judges each sample independently compared to pool-based sampling; and membership query synthesis (Lewis & Gale, 1994), which means that the unlabeled sample can be generated by the learner instead of selecting from the dataset only.
Although active learning involves querying an oracle iteratively, which is similar to our framework, there are still two main differences between them.
For the final purpose, active learning aims to finish the training stage at a low cost, and the inference stage remains the same as other machine learning tasks without the query process, while our framework aims to find the oracle ( i .
e .the underlying program) in a symbolic form, and the query process is retained during inference.
For the query process, active learning assumes only one oracle ( i .
e .the same query always gets the same label).
In contrast, our framework assumes multiple oracles ( i .
e .the same query will get different responses if the underlying programs are different).
D. A UTOMATED B LACK - BOX TESTING
Black-box testing is a method of software testing aiming to examine the functionality of a blackbox (such as a piece of a program) by a large number of test cases without perceiving its internal structures.
The most famous automated test cases generation method is learning-based testing (LBT) (Meinke, 2004; Meinke & Niu, 2010; Meinke & Sindhu, 2011).
LBT is an iterative approach to generate test cases by interacting with the black-box, which sounds similar to the query problem mentioned in this paper.
However, the settings and the purpose of the black-box testing are totally different from ours.
In detail, the black-box testing assumes the existence of a target requirement (or target function) and a black-box implementation of this requirement (such as a piece of program which is unknown).
The purpose is to check the black-box implementation to ensure that there is no bug or difference between the target requirement and the black-box by generating test cases ( i .
e .input-output examples).
In a contrast, in our settings, the target function does not exist, and all we have is a black-box (or called oracle / underlying program in our paper).
Our goal is to query this black box and guess which program is hidden inside based on the experience learned from the training set.
E T WO QUERY EXAMPLES
In this section, we show two query examples that cover all branches of the program while the welldesigned dataset fails to do this.
Each example consists of the underlying program and the corresponding important queries that improve the branch coverage.
See Figure 14 and Figure 15.
[Figure]
O
Program def run(): if markersPresent: putMarkerif not rightIsClear: putMarkermove pickMarker turnRightrepeat R=2:
turnLeft putMarkerputMarkermoveputMarker
I
O
Program def run(): repeat R=2:
putMarkerturnRightif
frontIsClear: putMarker putMarker turnRight putMarkerwhile rightIsClear: movemovepickMarker turnRight turnLeft turnLeft
moveputMarkerturnLeft