L EARNING TO R EPRESENT E DITS
A BSTRACT
We introduce the problem of learning distributed representations of edits.
By combining a “neural editor” with an “edit encoder”, our models learn to represent the salient information of an edit and can be used to apply edits to new inputs.
We experiment on natural language and source code edit data.
Our evaluation yields promising results that suggest that our neural network models learn to capture the structure and semantics of edits.
We hope that this interesting task and data source will inspire other researchers to work further on this problem.
I NTRODUCTION
One great advantage of electronic storage of documents is the ease with which we can edit them, and edits are performed in a wide variety of contents.
For example, right before a conference deadline, papers worldwide are ﬁnalized and polished, often involving common ﬁxes for grammar, clarity and style.
Would it be possible to automatically extract rules from these common edits?
Similarly, program source code is constantly changed to implement new features, follow best practices and ﬁx bugs.
With the widespread deployment of (implicit) version control systems, these edits are quickly archived, creating a major data stream that we can learn from.
In this work, we study the problem of learning distributed representations of edits.
We only look at small edits with simple semantics that are more likely to appear often and do not consider larger edits; i.e. , we consider “add deﬁnite articles” rather than “rewrite act 2, scene 3.”
Concretely, we focus on two questions: i) Can we group semantically equivalent edits together, so that we can automatically recognize common edit patterns?
ii) Can we automatically transfer edits from one context to another?
A solution to the ﬁrst question would yield a practical tool for copy editors and programmers alike, automatically identifying the most common changes.
By leveraging tools from program synthesis, such groups of edits could be turned into interpretable rules and scripts (Rolim et al., 2017).
When there is no simple hard rule explaining how to apply an edit, an answer to the second question would be of great use, e.g. , to automatically rewrite natural language following some stylistic rule.
We propose to handle edit data in an autoencoder-style framework, in which an “edit encoder”
f ∆ is trained to compute a representation of an edit x − → x + , and a “neural editor” α is trained to construct x + from the edit representation and x − .
This framework ensures that the edit representation is semantically meaningful, and a sufﬁciently strong neural editor allows this representation to not be speciﬁc to the changed element.
We experiment with various neural architectures that can learn to represent and apply edits and hope to direct the attention of the research community to this new and interesting data source, leading to better datasets and stronger models.
Brieﬂy, the contributions of our paper are: (a) in Sect.
2, we present a new and important machine learning task on learning representations of edits (b) we present a family of
Cambridge, UK.
[Figure]
[Figure]
UnderlyingNode)
[Figure]
(t
[Figure]
>
[Figure]
[Figure]
[Figure]
d i tx (cid:48)− : me =
((ue!=null) ?
ue.
Operand : null) as MemberExpression
[Figure]
x (cid:48)
[Figure]
: me = ue?.Operand as MemberExpression
[Figure]
d i t
[Figure]
models that capture the structure of edits and compute efﬁcient representations in Sect.(c)
we create a new source code edit dataset, and release the data extraction code at https://github.com/Microsoft/msrc-dpu-learning-to-represent-edits and the data at http://www.cs.cmu.edu/˜pengchey/githubedits.zip .
(d) we perform a set of experiments on the learned edit representations in Sect.
4 for natural language text and source code and present promising empirical evidence that our models succeed in capturing the semantics of edits.
T ASK
In this work, we are interested in learning to represent and apply edits on discrete sequential or structured data, such as text or source code parse trees 1 .
Figure 1 gives a graphical overview of the task, described precisely below.
Edit Representation Given a dataset of edits { x ( i ) −
→ x ( i )+ } Ni =1 , where x ( i ) − is the original version of some object and x ( i )+ its edited form (see upper half of Figure 1 for an example), our goal is to learn a representation function f ∆ that maps an edit operation x − → x + to a real-valued edit representation f ∆ ( x − , x + ) ∈ R n .
A desired quality of f ∆ is for the computed edit representations to have the property that semantically similar edits have nearby representations in R n .
Having distributed representations also allows other interesting downstream tasks, e.g. , unsupervised clustering and visualization of similar edits from large-scale data ( e.g. the GitHub commit stream), which would be useful for developing human-assistance toolkits for discovering and extracting emerging edit patterns ( e.g. new bug ﬁxes or emerging “best practices” of coding).
Neural Editor Given an edit representation function f ∆ , we want to learn to apply edits in a new context.
This can be achieved by learning a neural editor α that accepts an edit representation f ∆ ( x − , x + ) and a new input x (cid:48)− and generates x (cid:48) + .
This is illustrated in the lower half of Figure 1.
M ODEL
We cast the edit representation problem as an autoencoding task, where we aim to minimize the reconstruction error of α for the edited version x + given the edit representation f ∆ ( x − , x + ) and the original version x − .
By limiting the capacity of f ∆ ’s output and allowing the model to freely use information about x − , we are introducing a “bottleneck” that forces the overall framework to not simply treat f ∆ ( x − , x + ) as an encoder of x + .
The main difference from traditional autoencoders is that in our setup, an optimal solution requires to re-use as much information as possible from x −
AssignStmt u
=
Expr x + x h 1 root →
Expr h 2 Expr → Expr Op Expr h 3 T REE C P
Expr h 4 Op → −
h 5 Expr → IntLit h 6 IntLit → 23 (a) (b)
AST Child
Next Token h E XPAND R action h G EN T ERM action h T REE C P action
Action Flow
Parent Feed
Copied to make the most of the capacity of f ∆ . Formally, given a probabilistic editor function P α such as a neural network and a dataset { x ( i ) − → x ( i )+ } Ni =1 , we seek to minimize the negative likelihood loss [Equation] Note that this loss function can be interpreted in two ways: (1) as a conditional autoencoder that encodes the salient information of an edit, given x − and (2) as an encoder-decoder model that encodes x − and decodes x + conditioned on the edit representation f ∆ ( x − , x + ) .
In the rest of this section, we discuss our methods to model P α and f ∆ as neural networks.
3. N EURAL E DITOR
As discussed above, α should use as much information as possible from x − , and hence, an encoderdecoder architecture with the ability to copy from the input is most appropriate.
As we are primarily interested in edits on text and source code in this work, we explored two architectures: a sequenceto-sequence model for text, and a graph-to-tree model for source code, whose known semantics we can leverage both on the encoder as well as on the decoder side.
Other classes of edits, for example, image manipulation, would most likely be better served by convolutional neural models.
Sequence-to-Sequence Neural Editor First, we consider a standard sequence-to-sequence model with attention (over the tokens of x − ).
The architecture of our sequence-to-sequence model is similar to that of Luong et al.
(2015), with the difference that we use a bidirectional LSTM in the encoder and a token-level copying mechanism (Vinyals et al., 2015) that directly copies tokens into the decoded sequence.
Whereas in standard sequence-to-sequence models the decoder is initialized with the representation computed by the encoder, we initialize it with the concatenation of encoder output and the edit representation.
We also feed the edit representation as input to the decoder LSTM at each decoding time step.
This allows the LSTM decoder to take the edit representation into consideration while generating the output sequence.
Graph-to-Tree Neural Editor Our second model aims to take advantage of the additional structure of x − and x + .
To achieve this, we combine a graph-based encoder with a tree-based decoder.
We use
T ( x ) to denote a tree representation of an element, e.g. , the abstract syntax tree (AST) of a fragment of source code.
We extend T ( x ) into a graph form G ( x ) by encoding additional relationships ( e.g. , the “next token” relationship between terminal nodes, etc. )
(see Figure 2(a)).
To encode the elements of G ( x − ) into vector representations, we use a gated graph neural network (GGNN) (Li et al., 2015).
Similarly to recurrent neural networks for sequences (such as biRNNs), GGNNs compute a representation for each node in the graph, which can be used in the attention mechanisms of a decoder.
Additionally, we use them to obtain a representation of the full input x − , by computing their weighted average following the strategy of Gilmer et al.
(2017) ( i.e. , computing a score for each node, normalizing scores with a softmax, and using the resulting values as weights).
Our tree decoder follows the semantic parsing model of Yin & Neubig (2018), which sequentially generate a tree T ( x + ) as a series of expansion actions a 1 . . .
a N .
The probability of taking an action is modeled as p ( a t | a <t , s ) , where s is the input (a sequence of words in the original semantic
AssignStmt
FieldAccess v .
F =
Expr x + x
AssignStmt u
=
Expr x + x ↔ v u − .
∅ −
F ∅ = =
= =
x x = + + = x x (a) (
[Figure]
parsing setting) and a <t is the partial tree that has been generated so far. The model of Yin & Neubig (2018) mainly uses two types of actions: E XPAND R expands the current non-terminal using a grammar rule, and G EN T ERM generates a terminal token from a vocabulary or copies a token from s 3 .
The dependence on the partial tree a <t is modeled by an LSTM cell which is used to maintain state throughout the generation procedure.
Additionally, the LSTM receives the decoder state used to pick the action at the parent node as an additional input (“parent-feeding”).
This process illustrated in Figure 2(b).
We extend this model to our setting by replacing the input sequence s by x − ; concretely, we condition the decoder on the graph-level representation computed for G ( x − ) .
Additionally, we use the change representation f ∆ ( · ) as an additional input to the LSTM initial state and at every decoding step.
Based on the observation that edits to source code often manipulate the syntax tree by moving expressions around ( e.g. by nesting statements in a conditional, or renaming a function while keeping its arguments), we extend the decoding model of Yin & Neubig (2018) by adding a facility to copy entire subtrees from the input.
For this, we add a decoder action T REE C P .
This action is similar to standard copying mechanism known from pointer networks (Vinyals et al., 2015), but instead of copying only a single token, it copies the whole subtree pointed to.
However, adding the T REE C P action means that there are many correct generation sequences for a target tree.
This problem appears in token-copying as well, but can be easily circumvented by marginalizing over all correct choices at each generation step (by normalizing the probability distribution over allowed actions to sum up those that have the same effect).
In the subtree-copying setting, the lengths of action sequences representing different choices may differ.
In our implementation we handle this problem during training by simpling picking the generation sequence that greedily selecting T REE C P actions.
3. E DIT R EPRESENTATION
To compute a useful edit representation, a model needs to focus on the differences between x − and x + .
A risk in our framework is that f ∆ degenerates into an encoder for x + , turning α into a decoder.
To avoid this, we need to follow the standard autoencoder trick, i.e. it is important to limit the capacity of the result of f ∆ by generating the edit representation in a low-dimensional space R N . This acts as a bottleneck and encodes only the information that is needed to reconstruct x + from x − .
We again experimented with both sequence-based and graph-based representations of edits.
Sequence Encoding of Edits Given x − (resp.
x + ) as sequence of tokens t (0) − , . . .
t ( T − ) − (resp.
t (0)+ , . . .
t ( T + ) + ), we can use a standard (deterministic) difﬁng algorithm to compute an alignment of tokens in the two sequences.
We then use extra symbols ∅ for padding, + for additions, − for deletions, ↔ for replacements, and = for unchanged tokens to generate a single sequence representing both x − and x + .
This is illustrated in Figure 3(a).
By embedding the three entries in each element of the sequence separately and concatenating their representation, they can be fed into a standard sequence encoder whose ﬁnal state is our desired edit representation.
In this work, we use a biLSTM.
Graph Encoding of Edits As in the graph-to-tree neural editor, we represent x − and x + as trees
T ( x − ) and T ( x + ) .
We combine these trees into a graph representation G ( x − → x + ) by merging both trees into one graph, using “Removed”, “Added” and “Replaced” edges.
To connect the two trees, we compute the same alignment as in the sequence case, connecting leaves that are the same and each replaced leaf to its replacement.
We also propagate this information up in the trees, i.e. , two inner nodes are connected by “=” edges if all their descendants are connected by “=” edges.
This is illustrated in Figure 3(b).
Finally, we also use the same “+” / “-” / “ ↔ ” / “=” tags for the initial node representation, computing it as the concatenation of the string label ( i.e. token or nonterminal name) and the embedding of the tag.
To obtain an edit representation, we use a GGNN unrolled for a ﬁxed number of timesteps and again use the weighted averaging strategy of Gilmer et al.
E VALUATION
Evaluating an unsupervised representation learning method is challenging, especially for a newly deﬁned task.
Here, we aim to evaluate the quality of the learned edit representations with a series of qualitative and quantitative metrics on natural language and source code.
4. D ATASETS AND C ONFIGURATION
Natural Language Edits We use the WikiAtomicEdits (Faruqui et al., 2018) dataset of pairs of short edits on Wikipedia articles.
We sampled 1040 K edits from the English insertion portion of the dataset and split the samples into 1000 K / 20 K / 20 K train-valid-test sets.
Source Code Edits To obtain a dataset for source code, we clone a set of 54 C# projects on GitHub and collected a GitHubEdits dataset (see Appendix A for more information).
We selected all changes in the projects that are no more than 3 lines long and whose surrounding 3 lines of code before and after the edited lines have not been changed, ensuring that the edits are separate and short.
We then parsed the two versions of the source code and take as x − and x + the code that belongs to the top-most AST node that contains the edited lines.
Finally, we remove trivial changes such variable renaming, changes within comments or formatting changes.
Overall, this yields 111 724 edit samples.
For each edit we run a simple C# analysis to detect all variables and normalize variable names such that each unique variable within x − and x + has a unique normalized name V0 , V1 , etc.
This step is necessary to avoid the sparsity of data induced by the variety of different identiﬁer naming schemes.
We split the dataset into 91,372 / 10,176 / 10,176 samples as train/valid/test sets.
Additionally, we introduce a labeled dataset of source code edits by using C# “ﬁxers”.
Fixers are small tools built on top of the C# compiler, used to perform common refactoring and modernization tasks ( e.g. , using new syntactic sugar).
We selected 16 of these ﬁxers and ran them on 6 C# projects to generate a small C#Fixers dataset of 2,878 edit pairs with known semantics.
We present descriptions and examples of each ﬁxer in Appendix A.
Conﬁguration Throughout the evaluation we use a ﬁxed size of 512 for edit representations.
The size of word embeddings and hidden states of encoding LSTMs is 128. The dimensionality of the decoding LSTM is set to 256. Details of model conﬁguration can be found in Sect. A.
When generating the target x + , our neural editor model can optionally take as input the context of the original input x − (e.g., the preceding and succeeding code segments surrounding x − ), whose information could be useful for predicting x + .
For example, in source code edits the updated code snippet x
+ may reuse variables deﬁned in the preceding snippet.
In our code experiments, we use a standard bidirectional LSTM network to encode the tokenized 3 lines of code before and after x − as context.
The encoded context is used to initialize the decoder, and as an additional source for the pointer network to copy tokens from.
4. Q UALITY OF E DIT R EPRESENTATIONS
First, we study the ability of our models to encode edits in a semantically meaningful way.
RCS
IDE
RCS RCS
RCS RCS
RCS RCS
CA
Visualizing Edits on Fixers Data In a ﬁrst experiment, we train our sequential neural editor model on our GitHubEdits data and then compute representations for the edits generated by the
C# ﬁxers.
A t-SNE visualization (Maaten &
Hinton, 2008) of the encodings is shown in
Figure 4.
For this visualization, we randomly selected 100 examples from the edits of each ﬁxer (if that ﬁxer has more than 100 samples) and discarded ﬁxer categories with less thanexamples.
Readers are referred to Appendix A for detailed descriptions of each ﬁxer category.
We ﬁnd that our model produces dense clusters for simple or distinctive code edits, e.g. ﬁxer RCS1089 (using the ++ or -- unary operators instead of a binary operator ( e.g. , i = i +→ i++ ), and ﬁxer CA (adding .ConfigureAwait(false) for await statements).
We also analyzed cases where (1) the edit examples from the same ﬁxer are scattered, or (2) the clusters of different ﬁxers overlap with each other.
For example, the ﬁxer
RCS1077 covers 12 different aspects of optimizing LINQ method calls ( e.g. , type casting, counting, etc.), and hence its edits are scattered.
On the other hand, ﬁxers RCS1146 and RCS1206 yield overlapping clusters, as both ﬁxers change code to use the ?.
operator.
Fixers RCS1207 (change a lambda to a method group, e.g. foo(x=>bar(x))
→ foo(bar) ) and RCS1021 (simplify lambda expressions, e.g. foo(x=> { return 4; } ) → foo(x=>4) ) are similar, as both inline lambda expressions in two different ways.
Analysis yields that the representation is highly dependent on surface tokens.
For instance, IDE004 (removing redundant type casts, e.g. (int)2 → 2 ) and
RCS1207 (removing explicit argument lists) yield overlapping clusters, as both involve deleting identiﬁers wrapped by parentheses.
Human Evaluation on Encoding Natural Language WikiAtomicEdits
In a second experiment, we test how well neighborhoods in edit representation space correspond to semantic similarity.
We computed the ﬁve nearest neighbors of 200 randomly sampled seed edits from our training set, using both our trained sequence-to-sequence editing model with sequential edit encoder, as well as a simple bag-of-words baseline based on TF-IDF scores.
We then rated the quality of the retrieved neighbors on a scale of 0 (“unrelated edit”), 1 (“similar edit”) and 2 (“semantically or syntactically same edit”).
Details of the annotation schema is included in Sect.
E. We show the (normalized) discounted cumulative gain (DCG, Manning et al.
for the two models at the top of Tab.
1 (higher is better).
The relevance scores indicate that our neural model clearly outperforms the simplistic baseline.
Tab. 1 also presents two example edits with their nearest neighbors.
Example 1 shows that the neural edit models succeeded in representing syntactically and semantically similar edits, while the bag-of-words baseline relies purely on surface token overlap.
Interestingly, we also observed that the edit representations learned by the neural editing model on WikiAtomicEdits are somewhat sensitive to position, i.e. the position of the inserted tokens in both the seed edit and the nearest neighbors is similar.
This is illustrated in Example 2, where the second ( “senegalese striker” ) and the third ( “republican incumbent” ) nearest neighbors returned by the neural model have similar editing positions as the seed edit, while they are semantically diverse.
4. E DIT E NCODER P ERFORMANCE
To evaluate the performance of our two edit encoders discussed in Sect.
3.2 and disentangle it from the choice of neural editor, we train various combinations of our neural editor model and manually evaluate the quality of the edit representation.
More speciﬁcally, we trained our neural editor models on GitHubEdits and randomly sampled 200 seed edits and computed their 3 nearest neighbors using each end-to-end model.
We then rated the resulting groups using the same 0-2 scale as above.
The resulting relevance scores are shown in Tab. 2. [Table]
james nava ( born february 22 , 1983 ) is an american professional baseball outﬁelder
[Table]
nava is only the fourth player in mlb history to hit a grand slam in his ﬁrst major league at bat and the second to do it on the ﬁrst pitch . NN [Table] he batted .302 with 73 steals , and received a september call - up to the major leagues (cid:73) as an outﬁelder (cid:74) .
(cid:73) arthur ray briles ( born december 3 , 1955 ) is a former american football coach and (cid:74) his most recent head coaching position was at bay- lor university , a position he held from theseason through the 2015 season .
NNhe played (cid:73) as an outﬁelder (cid:74) for the hanshin tigers .
(cid:73) jonathan david disalvatore ( born march, 1981 ) is a professional ice hockey (cid:74) he was selected by the san jose sharks in the 4th round ( 104th overall ) of the 2000 nhl entry draft .
[Table]
in 2012 , his senior at oak mountain , dahl had a .412 batting average , 34 runs batted in ( rbis ) , and 18 stolen bases (cid:73) as an outﬁelder .
(cid:74) (cid:73) professor paul talalay ( born march 31 ,) is the john jacob abel (cid:74) distinguished service professor of pharmacology and director of the laboratory for molecular sciences at johns hopkins school of medicine in baltimore .
Example 2 she , along with her follow artist carolyn mase studied with (cid:73) impressionist landscape painter (cid:74) john henry twachtman at the art students league of new york .
NNhis brother was draughtsman william daniell and his uncle was (cid:73) landscape painter (cid:74) thomas daniell .
the ﬁrst painting was a portrait of a young girl , emerantia van beresteyn , the sister of (cid:73) the landscape painter (cid:74) nicolaes van beresteyn , the later founder of half of this hofje .
NNwilliam james linton ( december 7 , 1812 - de- cember 29 , 1897 ) was an english - born american wood engraver , (cid:73) landscape painter , (cid:74) political reformer and author of memoirs , novels , poetry and non-ﬁction .
he was the club ’s top scorer with 22 goals in all competitions , one more than (cid:73) senegalese striker (cid:74) lamine diarra , who left the club at the end of the season .
NNearly on , hopper modeled his style after chase and french (cid:73) impressionist (cid:74) masters douard manet and edgar degas .
caforio ” aggressively attacked ” his opponent , (cid:73) republican incumbent (cid:74) steve knight , for his delayed response to the leak .
Example 1 neural editing model returns syntactically and semantically similar edits.
Example
Neural edit representations are sensitive to position.
[Table]
Comparing the sequential edit encoders trained with Seq2Seq and Graph2Tree editors, we found that the edit encoder trained with the Graph2Tree objective performs better.
We hypothesize that this is because the Graph2Tree editor better captures structural-level information about an edit.
For instance,
Example 1 in Tab.
3 removes explicit type casting.
The Seq2Seq editor has difﬁculty distinguishing this type of edit, confusing it with changes of lambda expressions to method groups (1st and 2nd nearest neighbors) since both two types of edits involve removing paired parentheses.
Surprisingly, we found that the graph-based edit encoder does not outperform the sequence-based encoder.
However, we observe that the graph edit encoder in many cases tends to better capture high-level and abstract structural edit patterns.
Example 2 in Tab.
3 showcases a seed edit that swaps two consecutive declarations, which corresponds to swapping the intermediate Expression nodes representing each statement on the underlying AST.
In this case, the graph edit encoder is capable of grouping semantically similar edits, while it seems to be more difﬁcult for the sequential encoder [Table]
[Table]
encoder to capture the edit pattern.
On the other hand, we found that the graph edit encoder often fails to capture simpler, lexical level edits ( e.g. , Example 1).
This might suggest that terminal node information is not effectively propagated, an interesting issue worth future investigation.
4. P RECISION OF N EURAL E DITORS
Finally, we evaluate the performance of our end-to-end system by predicting the edited input x + given x − and the edit representation.
We are interested in answering two research questions: First , how well can our neural editors generate x + given the gold-standard edit representation f ∆ ( x − , x + ) ?
Second , and perhaps more interestingly, can we use the representation of a similar edit f ∆ ( x (cid:48)− , x (cid:48) + ) to generate x + by applying that edit to x − ( i.e. ˆ x + = α ( x − , f ∆ ( x (cid:48)− , x (cid:48) + )) )?
To answer the ﬁrst question, we trained our neural editor models on the WikiAtomicEdits and the
GitHubEdits dataset, and evaluate the performance of encoding and applying edits on test sets.
For completeness, we also evaluated the performance of our neural editor models with a simple “Bag-ofEdits” edit encoding scheme, where f ∆ ( x − , x + ) is modeled as the concatenation of two vectors, each representing the sum of the embeddings of added and deleted tokens in the edit, respectively.
This edit encoding method is reminiscent of the model used in Guu et al.
(2017) for solving a different task of language modeling by marginalizing over latent edits, which we will elaborate in Sect.
Tab. [Table] lists the evaluation results.
With our proposed sequence- and graph-based edit encoders, our neural editor models achieve reasonable end-to-end performance, surpassing systems using bag-of-edits representations.
This is because many edits are context-sensitive and position-sensitive , requiring edit representation models that go beyond the bag-of-edits scheme to capture those effects (more analysis is included in Appendix B).
Interestingly, on the GitHubEdits dataset, we ﬁnd that the Seq2Seq editor with sequential edit encoder registers the best performance.
However, it should be noted that in this set of experiments, we encode the gold-standard edit f ∆ ( x − , x + ) to predict x + .
As we will show later, better performance with the gold-standard edit does not necessarily imply better (more generalizable) edit representation.
Nevertheless, we hypothesize that the higher accuracy of the Seq2Seq edit is due to the fact that a signiﬁcant proportion of edits in this dataset is small and primarily syntactically simple.
Indeed we ﬁnd that 69% of test examples have a token-level edit distance of less than 5.
To answer the second question, we use the trained neural editors from the previous experiment, and test their performance in a “one-shot” transfer learning scenario.
Speciﬁcally, we use our high-quality C#Fixers dataset, and for each ﬁxer category F of semantically similar edits, we randomly select a seed edit { x (cid:48)− → x (cid:48) + } ∈ F , and use its edit representation f ∆ ( x (cid:48)− , x (cid:48)
+ ) to predict the updated code for all examples in F , i.e. , we have ˆ x + = α ( x − , f ∆ ( x (cid:48)− , x (cid:48) + ))
, ∀ { x − → x + } ∈ F .
This task is highly non-trivial, since a ﬁxer category could contain more than hundreds of edit examples collected from different C# projects.
Therefore, it requires the edit representations to generalize and transfer well, while being invariant of local lexical information like speciﬁc method names.
To make the experimental evaluation more robust to noise, for each ﬁxer category F , we randomly sampleseed edit pairs { x (cid:48)− → x (cid:48)
+ } , compute their edit representations and use them to predict the edited version of the examples in F and evaluate accuracy of predicting the exact ﬁnal version.
We then report the best score among the 10 seed representations as the performance metric on F .
Tab. 5 summarizes the results and also reports the upper bound performance when using the goldstandard edit representation f ∆ ( x − , x + ) to predict x + , and an approximation of the “lower bound” accuracies using pre-trained Seq2Seq and Graph2Tree models without edit encoders.
We found that our neural Graph2Tree editor with the sequential edit encoder signiﬁcantly outperforms the
Seq2Seq editor, even though Seq2Seq performs better when using gold-standard edit representations.
This suggest that the edit representations learned with the Graph2Tree model generalize better, especially for edits discussed in Sect.
4.2 that involve syntactic variations like RCS1021 (lambda expression simpliﬁcation, 7.8% vs. 30.7% for Seq2Seq and Graph2Tree), and RCS1207 (change lambdas to method groups, 7.1% vs. 26.2%).
Interestingly, we also observe that Seq2Seq outperforms the Graph2Tree model for edits with trivial surface edit sequences, where the Graph2Tree model does not have a clear advantage.
For example, on RCS1015 (use nameof operator, e.g.
Exception("x") → Exception(nameof(x)) )
, the accuracies for Seq2Seq and Graph2Tree are 40.0% (14/35) and 28.6% (10/35), resp.
We include more analysis of the results in Appendix C.
R ELATED W ORK
Edits have recently been considered in NLP, as they represent interesting linguistic phenomena in language modeling and discourse (Faruqui et al., 2018; Yang et al., 2017a).
Speciﬁcally, Guu et al.
(2017) present a generative model of natural language sentences via editing prototypes.
Our work shares with Guu et al.
(2017) in that (1) the posterior edit encoding model in Guu et al.
(2017) is similar to our baseline “bag-of-edits” encoder in Sec. 4.4, and (2) the sequence-to-sequence sentence generation model given the prototype and edit representation is reminiscent of our Seq2Seq editor.
In contrast, our work directly focuses on discriminative learning of representing edits and applying the learned edits for both sequential (NL) and structured (code) data.
Another similar line of research is “retrieve-and-edit” models for text generation (Hashimoto et al., 2018), where given an input data x , the target prediction y is generated by editing a similar target y (cid:48) that is retrieved based on the similarity between its source x (cid:48) and the input x .
While these models typically require an “editor” component to generate the output by exploiting the difference between similar inputs, they usually use the simpler bag-of-edits representations (Wu et al., 2019), or implicitly capture it via end-to-end neural networks (Contractor et al., 2018).
To our best knowledge, there is not any related work that classiﬁes or otherwise explicitly represents the differences over similar input, with the exception of differential recurrent neural networks used for action recognition in videos (Veeriah et al., 2015; Zhuang et al., 2018).
This is a substantially different task, as the data includes a temporal component as well.
Source code edits are a widely studied artifact.
Specialized software, such as git, is widely used to store source code revision histories.
Nguyen et al.
(2013) studied the repetitiveness of source code changes by identifying identical types of changes using a deterministic differencing tool.
In contrast, we employ on a neural network to cluster similar changes together.
Rolim et al.
(2017) use such clusters to synthesize small programs that perform the edit.
The approach is based on Rolim et al.
(2018) extract manually designed syntactic features from code and cluster over multiple changes to ﬁnd repeatable edit rules.
Similarly, Paletov et al.
(2018) extract syntactic features speciﬁcally targeting edits in cryptography API protocols.
In this work, we try to avoid hand-designed features and allow a neural network to learn the relevant aspects of a change by directly giving as input the original and ﬁnal version of a changed code snippet.
Modeling tree generation with machine learning is an old problem that has been widely studied in
NLP.
Starting with Maddison & Tarlow (2014), code generation has also been considered as a tree generation problem.
Close to our work is the decoder of Yin & Neubig (2017) which we use as the basis of our decoder.
The work of Chen et al.
(2018) is also related, since it provides a tree-to-tree model, but focuses on learning a single translation tasks and cannot be used directly to represent multiple types of edits.
Both Yin & Neubig (2017) and Chen et al.
(2018) have copying mechanism for single tokens, but our subtree copying mechanism is novel.
Autoencoders (see Goodfellow et al.
for an overview) have a long history in machine learning.
Variational autoencoders (Kingma & Welling, 2013) are similar to autoencoders but instead of focusing on the learned representation, they aim to create accurate generative probabilistic models.
Most (variational) autoencoders focus on encoding images but there have been works that autoencode sequences, such as text (Dai & Le, 2015; Bowman et al., 2015;
Yang et al., 2017b) and graphs (Simonovsky & Komodakis, 2018; Liu et al., 2018).
Conditional variational autoencoders (Sohn et al., 2015) have a related form to our model (with the exception of the KL term), but are studied as generative models, whereas we are primarily interested in the edit representation.
D ISCUSSION & C ONCLUSIONS
In this work, we presented the problem of learning distributed representation of edits.
We believe that the dataset of edits is highly relevant and should be studied in more detail.
While we have presented a set of initial models and metrics on the problem and obtained some ﬁrst promising results, further development in both of these areas is needed.
We hope that our work inspires others to work on this interesting problem in the future.
A CKNOWLEDGMENTS
We would like to thank Rachel Free for her insightful comments and suggestions.
A D ATASETS AND C ONFIGURATION
WikiAtomicEdits We randomly sampled 1040 K insertion examples from the English portion of
WikiAtomicEdits (Faruqui et al., 2018) dataset, with a train, development and test splits of 1000 K , 20 K and 20 K .
GitHubEdits
We cloned the top 54 C
# GitHub repositories based on their popularity (Tab. 8).
For each commit in the master branch, we collect the previous and updated versions of the source code, and extract all consecutive lines of edits that are smaller than three lines, and with at least three preceding and successive lines that have not been changed.
We then ﬁlter trivial changes such as variable and identiﬁer renaming, and changes happened within comments.
We also limit the number of tokens for each edit to be smaller than 100, and down-sample edits whose frequency is larger than 30.
Finally, we split the dataset by commit ids, ensuring that there are no edits in the training and testing (development) sets coming from the same commit.
Tab.
6 lists some statistics of the dataset.
[Table]
C#Fixers We selected 16 C# ﬁxers from Roslyn 4 and Roslynator 5 , and ran them on 6 C# projects to generate a small, high-quality C# ﬁxers dataset of 2 878 edit pairs with known semantics.
Table 7 lists the detailed descriptions for each ﬁxer category.
And more information can be found at https://github.com/JosefPihrt/Roslynator/blob/master/ src/Analyzers/README.md .
Network Conﬁguration Throughout the experiments, we use a ﬁxed edit representation size of 512.
The dimensionality of word embedding, the hidden states of the encoder LSTMs, as well as the gated graph neural network is 128, while the decoder LSTM uses a larger hidden size of 256.
For the graph-based edit encoder, we used a two-layer graph neural network, with 5 information propagation steps at each layer.
During training, we performed early stopping, and choose the best model based on perplexity scores on development set.
During testing, we decode a target element x + using a beam size of 5.
Fixer ID
Description
[Table]
statements
[Table]
[Table]
RCS
Use nameof
[Table]
[Table]
RCS
Simplify lambda expression
[Table]
[Table]
> { return f.ToString(); });
[Table]
items
[Table]
[Table]
> f.ToString());
RCS
Remove redundant parentheses x − : if ((x)) {} x − : if (x) {}
RCS
Use compound assignment x − : i
= i + 2; x + : i += 2;
RCS
Optimize LINQ method call x − : items.
Where(f => Foo(f)).Any(); x + : items.
Any(f => Foo(f));
RCS
Use -- / ++ operator instead of assignment x − : i
= i + 1; x + : i += 1;
RCS
Remove redundant ToString call x − : var x = s.ToString(); x
[Table]
: var x = s;
RCS
Mark local variable as const x − : string s = "a"; string s2 = s + "b"; x + : const string s
= "a"; string s2 = s + "b";
RCS
Add parentheses according to operator precedence x − : if (x || y && z) {} x + : if (x || (y && z) ) {}
RCS
Use conditional access x − : x != null && x.StartsWith("a"); x + : x?.StartsWith("a");
RCS
Optimize call of StringBuilder ’s Append / AppendLine x − : sb.
Append(s + "x"); x + : sb.
Append(s).Append("x");
RCS
Avoid NullReferenceException x − : items.
First().ToString(); x + : items?.First().ToString();
RCS
Use conditional access instead of conditional expression x − : int i =
(x != null) ?
x.Value.
GetHashCode() : 0; x + : int i = x?.GetHashCode() ?? 0;
RCS
Use method group instead of anonymous function x − : items.
Select(f => Foo(f)); x + : items.
Select(Foo);
[Table]
Microsoft/msbuild
Build Tool Mvc aspnet/
Mvc
MVC Framework Nancy
NancyFx/Nancy
HTTP based services Newtonsoft.
Json
JamesNK/Newtonsoft.
Json
JSON framework NLog
NLog/NLog
Loggin for .NET
OpenLiveWriter
OpenLiveWriter/ OpenLiveWriter
Text editor OpenRA
OpenRA/OpenRA
Strategy Game Engine Opserver opserver/Opserver
Monitoring System orleans dotnet/orleans
Distributed Virtual Actors PowerShell
PowerShell/PowerShell
Command Line Psychson brandonlw/Psychson
Firmware PushSharp
Redth/PushSharp
Push Notiﬁcations ravendb ravendb/ravendb
Database ReactiveUI reactiveui/ReactiveUI
Reactive MVC Framework RestSharp restsharp/RestSharp
HTTP/REST Client roslyn dotnet/roslyn .NET
Compiler Rx.
NET dotnet/reactive
Reactive extensions.
ServiceStack
ServiceStack/ServiceStack
Web Service Framework shadowsocks-windows shadowsocks/ shadowsocks-windows
Cryptography ShareX
ShareX/ShareX
Screen Recorder SignalR
SignalR/SignalR
Real-time web framework Sonarr
Sonarr/Sonarr
PVR SpaceEngineers
KeenSoftwareHouse/ SpaceEngineers
Game SparkleShare hbons/SparkleShare
File Sharing StackExchange.
Redis
StackExchange/ StackExchange.
Redis
Redis Client WaveFunctionCollapse mxgmn/ WaveFunctionCollapse
Bitmap/tilemap Generator Wox
Wox-launcher/Wox
Launcher
B C LUSTERING E XPERIMENTS
To qualitatively evaluate the quality of the learned edit representations.
We use the models trained on the WikiAtomicEdits and GitHubEdits datasets to cluster natural language and code edits.
We run K-Means clustering algorithm on 0 .
5 million sampled edits from WikiAtomicEdits, and all 90 K code edits from GitHubEdits, producing 50 000 and 20 000 clusters for each dataset.
Tab. 9 and Tab.
10 list some example clusters on WikiAtomicEdits and GitHub datasets, respectively.
Due to the size of clusters, we omit out-liners and present distinctive examples from each cluster.
On the WikiAtomicEdits dataset, we found clusters whose examples are semantically and syntactically similar.
More interestingly, on the source code data
, we ﬁnd representative clusters that relate to idiomatic patterns and best practices of programming.
The clustering results produced by our model would be useful for programming synthesis toolkits to generate interpretable code refractory rules, which we leave as interesting future work.
Finally, we remark that the clustering results indicate that the encoding of edits is context-sensitive and position-sensitive for both natural language and source code data.
For instance, the WikiAtomicEdits examples we present in Tab. 9 clearly indicate that semantically similar insertions also share similar editing positions.
This is even more visible in code edits (Tab. 10).
For instance, in the ﬁrst example in Tab. 10, Equal() can be changed to Empty() only in the Assert namespace (i.e., the context).
These examples demonstrate that it is important for an edit encoder to capture the contextual and positional information in edits, a property that cannot be captured by simple “bag-of-edits” edit representation methods.
[Table]
for stevenage borough
[Table]
athlete
[Table]
competed mainly in the 100 metres
[Table]
painter ,
[Table] [Table]
teacher
[Table]
professional
[Table]
league player
[Table]
player
[Table]
Add a parenthetical [Table] also .. [Table]
to modify the [Table] mid-state regional airport (cid:73) , also known as mid-state airport , (cid:74) is a small airport on in rush township , centre county in pennsylvania in the united states .
islamic culture (cid:73) , also known as saracenic culture , (cid:74) is a term primarily used in secular academia to describe the cultural practices common to historically islamic peoples .
birds of prey (cid:73) , also known as raptors , (cid:74) are birds that hunt for food primarily via ﬂight , using their keen senses , especially vision .
tetyana styazhkina (cid:73) , also written as tetyana stiajkina , (cid:74) ( ; born april [Table] 1977 ) is a ukrainian cycle racer who rides for the chirio forno d’asolo team .
acid jazz (cid:73) , also known as club jazz , (cid:74) is a musical genre that combines elements of jazz , soul , funk , disco and hip hop .
[Table]
the douro fully enters portuguese territory just after the conﬂuence with the gueda river ; once the douro enters portugal , major population centres are less frequent (cid:73) along the river (cid:74) .
mochou lake and mochou lake park are located at 35 hanzhongmen da jie in the jianye district of nanjing , china (cid:73) , west to qinhuai river (cid:74) .
reiner gamma is an albedo feature that is located on the oceanus procellarum , to the west of the reiner crater (cid:73) on the moon (cid:74) .
she made a brief return to the screen in ” parrish ” ( 1961 ) , playing the supporting role of mother which received little attention (cid:73) by the press (cid:74) .
he was involved in a few storylines , including one where he broke his toe and had a heart attack after he was pushed by a mugger (cid:73) in the market (cid:74) .
[Table]
(cid:73) at the time (cid:74) ajax and hercules were trapped behind a landslide at the gaillard cut , both were working to clear the landslide .
(cid:73) at the docks , (cid:74) hikaru attempts to befriend the tiger , but ﬁnds that it dislikes humans .
(cid:73) about the second , (cid:74) i do know they exist , but the question is whether they are considered a genre outside of japan .
(cid:73) in the battle , (cid:74) shirou uses his reality marble , unlimited blade works and defeats gilgamesh .
(cid:73) in the game , (cid:74) red is a curious 11 - year - old boy from pallet town .
[Table]
[Table]
Type V0 = V1?.GetType()
[Table]
V0 =
((V1!= null) ?
V1.Operand : null) as MemberExpression;
[Table]
V
[Table]
V1?.Operand as MemberExpression
[Table]
string V0 = V
==
null ?
null : V1.GetType().Name;
[Table]
string V0 = V1?.GetType().Name
[Table]
V0 = {+V
+}
==
null ?
null : V1(V2).ToArray()
[Table]
V0 = V1?.Invoke
[Table]
V2).ToArray()
[Table]
var V0 = V1.Customers.
Where(V2 => V2.CustomerID ==
LITERAL) .FirstOrDefault
[Table]
V0 = V1.Customers.
FirstOrDefault(V2 => V2.CustomerID == LITERAL)
[Table]
var V0 = V1.TypeConverters.
Where(V2 => V2.CanConvertTo(V3, V1)) .FirstOrDefault
(); x + var V0 = V1.TypeConverters.
FirstOrDefault(V2 => V2.CanConvertTo(V3, V1)); x − var V
= this.
V1.Where(V2 => V2.CanDeserialize(V3)) .FirstOrDefault
(); x + var V0 = this.
V1.FirstOrDefault(V2 => V2.CanDeserialize(V3)); x − var V0 = V1.Where(V2 => V2.Item1 =
= V3 && V2.Item
== V4) .FirstOrDefault
(); x + var V0 =
V1.FirstOrDefault(V2 => V2.Item1 == V3 && V2.Item
== V4); Description Change from Add function to indexer.
x − V0.Add(V1.key, V1.V2); x + V0[V1.key] =
V1.V2; x − V0.Add(V1.Id, V2); x + V0[V1.Id] = V2; x − V0.Add(V1.Etag, V1); x + V0[V1.Etag] = V1; x − V0.Add(V1.V2, V3.Merge(V1.V4));
x + V0[V1.V2] = V3.Merge(V1.V4);
[Table]
C B REAK - DOWN A NALYSIS OF T RANSFER L EARNING R ESULTS
Tab.
11 lists the detailed evaluation results for the transfer learning experiments discussed in Sect. 4.4.
We refer readers to Tab. 7 for detailed descriptions of each ﬁxer category.
The neural Graph2Tree editor outperforms the Seq2Seq editor (both with sequential edit encoders) onout of 16 ﬁxer categories.
However, we found that there are categories where both end-to-end system under-performs, even though the upper-bound accuracy is high ( e.g. RCS1077 , RCSRCS1197 ,
RCS1207 , RCS1032 ).
While improving the generalization ability of the neural editor models to achieve better transfer learning performance is an important future work, we remark that this task is indeed non-trivial.
First, some ﬁxer categories cover a broad range of similar edits, which could not be captured by a single seed edit.
xSecond, some categories contain syntactically or semantically complex refactoring rules.
For instance, RCS1207 converts method groups into anonymous functions, involving changing multiple positions of the source code, which might not be trivially captured by the sequential edit encoder from a single example edit.
Additionally, RCS1197 requires reasoning about a chain of expressions.
It turns sb.
Append(s1 + s2 + . . .
+ sN) into sb.
Append(s1).Append(s2).
[ . . . ]Append(sN)) , which our current models are unable to reason about.
More interestingly, we found that there are cases where the edits are syntactically simple, but could be semantically more difﬁcult to learn.
For instance, RCS1032 is about removing redundant parentheses from expressions.
Although the edit pattern might seem to be syntactically simple at the AST level (replacing a ParethesizedExpressionSyntax node by its child node), determining which pair of parentheses is actually redundant in an expression ( e.g. (a + b) * (c / d) ) is semantically non-trivial to learn from a single edit example.
We believe that further advances in (general) learning from source code are required to correctly handle these cases.
D I MPACT OF T RAINING S ET S IZE
To evaluate the data efﬁciency of our proposed approach, we tested the end-to-end performance of our neural editor model (Sect. 4.4, Tab. 4) with varying amount of training data.
Tab.
12 lists the results.
We found both Graph2Tree and Seq2Seq editors are relatively data efﬁcient.
They registered around 90% of the accuracies achieved using the full training set with only 60% of the training data.
E D ETAILS OF H UMAN E VALUATION
As discussed in Sect.
4.2, we performed human evaluation to rate the qualities of neighboring edits given a seed edit.
The annotation instructions on GithubEdits and WikiAtomicEdits datasets are listed below.
The annotation was carried out by three authors of this paper, and we anonymized the source of systems that generated the output.
The three-way Fleiss’ kappa inter-rater agreement is κ = 0 .
55 , which shows moderate agreement (Artstein & Poesio, 2008), an agreement level that is also used in other annotation tasks in NLP ( Das, 2018).
[Table]
Rating 2 Semantically and Syntactically Equivalent
The changed constituents in the seed edit and the neighboring edit are applied to the similar positions of the original sentence, serving the same syntactic and semantic role.
For example,
Examples
• Seed Edit x − var V0 = V1.Where(V2 =>
V2.Name =
= LITERAL).Single(); x + var V
=
V1.Single(V2=> V2.Name ==
LITERAL);
• Neighbor x − var V0 = V1.GetMembers().Where(V2 =>
V2.Kind =
= SymbolKind.
Property).Single(); x + var V0 = V1.GetMembers().Single(V
=
>
V2.Kind
= =
SymbolKind.
Property); • Seed Edit x − Type V0 = V
==
null ?
typeof(object) : V1.GetType(); x + Type V0 = V1?.GetType() ??
typeof(object); • Neighbor x − string V0 =
V
= =
null ? string.
Empty : VAR1.ToString(); x + string V
=
V1?.ToString() ??
string.
Empty; • Seed Edit x − Assert.
True(Directory.
Exists(V0)
== V1); x + Assert.
Equal(Directory.
Exists(V0), V1);
• Neighbor x − Assert.
True(V0.GetString(V0.GetBytes(LITERAL))
=
= V1.ContainingAssembly.
Identity.
CultureName); x + Assert.Equal(V0.GetString(VAR0.GetBytes(LITERAL)), V1.ContainingAssembly.
Identity.
CultureName);
Rating
1 Syntactically or Semantically Related
The seed and neighboring edits share functionally or syntactically similar patterns.
Examples
The following edit is a related edit of the ﬁrst example above, as it applies the same simpliﬁcation ( .Where(COND).Func() to .Func(COND) ), but for FirstOrDefault instead of Single :
• Seed Edit x − var V0 = V1.Where(V2 =>
V2.Name =
= LITERAL).Single(); x + var V
=
V1.Single(V2=> V2.Name ==
LITERAL); • Neighbor x − var V
= V1.Where(V2 =>
V3.ReportsTo =
=
V2.EmployeeID).FirstOrDefault(); x + var V
=
V1.FirstOrDefault(V2 =>
V3.ReportsTo =
=
V2.EmployeeID);
The following edit is a related edit of the second example above, as it also replaces a ternary expression for null checking with the ?.
and ??
operators:
• Seed Edit x − Type V0 = V
==
null ?
typeof(object) : V1.GetType(); x + Type V0 = V1?.GetType() ??
typeof(object); • Neighbor x − var V0 = V1 !
=
null ?
V1.ToList() : new List<TextSpan>(); x + var V
=
V1?.ToList() ??
new List<TextSpan>();
We also considered pairs such as the following related, since they share similar syntactic structure
• Seed Edit x − V0.State =
V1; x + V0.SetState(VAR1); • Neighbor x − V0.Quantity = V1; x + V0.SetQuantity(V1);
Rating
Not Related
The seed and neighboring edits are not related based on the above criteria.
[Table]
The changed constituents in the seed edit and the neighboring edit are applied to the similar positions of the original sentence, serving the same syntactic and semantic role.
For example,
[Table]
Neighbor
chaz guest
[Table]
born [Table] [Table] ) was born in niagra falls , . .
. , a decorated hero in wwii in europe , including the purple heart
[Table] [Table]
merlyn , is an american author , system administrator and programming consultant.
he was elected to donegal county council for sinn fin in 1979 , and held his seat until his death (cid:73) at age 56 (cid:74) .
davis graduated from high school in january 1947 , immediately enrolling at wittenberg college in rural ohio (cid:73) at age 17 (cid:74) . (cid:73) dror
(cid:74) feiler served as a paratrooper in the israel defense forces .
(cid:73) nagaur (cid:74) fort - sandy fort ; centrally located ; 2nd century old ; witnessed many battles ; lofty walls & spacious campus ; having many palaces & temples inside .
the original old bay house , home of the chief factor , still exists (cid:73) and is now part of the fort vermilion national historic site (cid:74) .
the population was 6,400 at the 2010 census (cid:73) and is part of the st. louis metropolitan area (cid:74) .
[Table]
The changed constituents in the seed and the neighboring edit are applied to the similar positions of the original sentence, and they play similar syntactic roles.
This includes examples like adding a disfunction, adding a complement, prepositional clause or other syntactic constructs with similar phrases or language structures.
For example,
[Table]
Neighbor
the douro fully enters portuguese territory just after the conﬂuence with the gueda river ; once the douro enters portugal , major population centres are less frequent (cid:73) along the river (cid:74) .
she made a brief return to the screen in ” parrish ” ( 1961 ) , playing the supporting role of mother which received little attention (cid:73) by the press (cid:74) .
when they found it , they discovered a group of pagumon living there instead who immediately proceeded to treat the digidestined as honored guests (cid:73) , saying that pagumon are the fresh form of koromon (cid:74) .
in 2012 slote and his baseball book ” jake ” were the subject of an espn ( 30 for 30 ) short documentary in which slote describes his writing process and reads from the book (cid:73) , saying it is his best writing (cid:74) .
the aircraft was intended to be (cid:73) certiﬁed and (cid:74) supplied as a complete ready - to - ﬂyaircraft for the ﬂight training and aerial work markets .
in june reinforcements ﬁnally did arrive when (cid:73) provincial and (cid:74) militia units from new york , new jersey , and new hampshire were sent up from fort edward by general daniel webb .
Rating 0 Not Related
The seed and neighboring edits are not related based on the above criteria.