TAG : Type Auxiliary Guiding for Code Comment Generation (2020)

Abstract: Existing leading code comment generation approaches with the structure-to-sequence framework ignores the type information of the interpretation of the code, e.g., operator, string, etc. However, introducing the type information into the existing framework is non-trivial due to the hierarchical dependence among the type information. In order to address the issues above, we propose a Type Auxiliary Guiding encoder-decoder framework for the code comment generation task which considers the source code as an N-ary tree with type information associated with each node. Specifically, our framework is featured with a Type-associated Encoder and a Type-restricted Decoder which enables adaptive summarization of the source code. We further propose a hierarchical reinforcement learning method to resolve the training difficulties of our proposed framework. Extensive evaluations demonstrate the state-of-the-art performance of our framework with both the auto-evaluated metrics and case studies.

## introduction the comment for the programming code is critical for software development, which is crucial to the further maintenance of the project codebase with significant improvement of the readability @xcite . code comment generation aims to automatically transform program code into natural language with the help of deep learning technologies to boost the efficiency of the code development. existing leading approaches address the code comment generation task under the structure-tosequence (struct2seq) framework with an encoderdecoder manner by taking advantage of the inherent structural properties of the code. for instance, existing solutions leverage the syntactic structure of abstract syntax trees (ast) or parse trees from source code have shown significant improvement to the quality of the generated comments @xcite @xcite ; solutions representing source code as graphs have also shown high-quality comment generation abilities by taking advantage of extracting the structural information of the codes @xcite . although promising results were reported, we observe that the information of the node type in the code is not considered in these aforementioned struct2seq based solutions. the lack of such essential information lead to the following common limitations: 1) losing the accuracy for encoding the source code with the same structure but has different types. as shown in fig. 1(a ), a tree-lstm @xcite encoder is illustrated to extract the structural information, the two subtrees of the code 'select' and 'compare' in the dashed box have the same structure but different types, with the ignorance of the type information, the traditional encoders illustrate the same set of neural network parameters to encode the tree, which leads to an inaccurate generation of the comment. 2) losing both the efficiency and accuracy for searching the large vocabulary in the decoding procedure, especially for the out-of-vocabulary (oov) words that exist in the source code but not in the target dictionary. as shown in the fig. 1 (a), missing the type of 'acl' node usually results in an unknown word 'unk' in the generated comments. thus, the key to tackle these limitations is efficiently utilizing the node type information in the encoder-decoder framework. to well utilize the type information, we propose a type auxiliary guiding (tag) encoder-decoder framework. as shown in fig. 1(b) , in the encoding phase, we devise a type-associated encoder to encode the type information in the encoding of the n-ary tree. in the decoding phase, we facilitate the generation of the comments with the help of type information in a two-stage process naming operation selection and word selection to reduce the searching space for the comment output and avoid the out-of-vocabulary situation. considering that there is no ground-truth labels for the operation selection results in the two-stage generation process, we further devised a hierarchical reinforcement learning (hrl) method to resolve the training of our framework. our proposed framework makes the following contributions: ## related work code comment generation frameworks generate natural language from source code snippets, e.g. sql, lambda-calculus expression and other programming languages. as a specified natural language generation task, the mainstream approaches could be categorized into textual based method and structure-based method. the textual-based method is the most straightforward solution which only considers the sequential text information of the source code. for instance, @xcite uses topic models and n-grams to predict comments with given source code snippets; @xcite presents a language model code-nn using lstm networks with attention to generate descriptions about c# and sql; @xcite predicts summarization of code snippets using a convolutional attention network; @xcite presents a learning system to generate sentences from lambda-calculus expressions by inverting semantic parser into statistical machine translation methods. the structure-based methods take the structure information into consideration and outperform the textual-based methods. @xcite processes a code snippet into the set of compositional paths in its ast and uses attention mechanism to select the relevant paths during the decoding. @xcite presents a neural machine translation based model which takes ast node sequences as input and captures the structure and semantic of java codes. @xcite combines the syntactic level representation with lexical level representation by adopting a tree-to-sequence @xcite based model. @xcite considers a sql query as a directed graph and adopts a graph-to-sequence model to encode the global structure information. copying mechanism is utilized to address the oov issues in the natural language generation tasks by reusing parts of the inputs instead of selecting words from the target vocabulary. @xcite presents a hybrid pointer-generator network by introducing pointer network @xcite into a standard sequence-to-sequence (seq2seq) model for abstractive text summarization. copynet from @xcite incorporates the conventional copying mechanism into seq2seq model and selectively copy input segments to the output sequence. in addition, @xcite uses the copying mechanism to copy strings from the code. our targeted task is considered as the opposite process of natural language to programming code (nl-to-code) task. so some of the nl-to-code solutions are also taken as our references. dong and lapata (2016) distinguishes types of nodes in the logical form by whether nodes have child nodes. @xcite neubig (2017); rabinovich et al. (2017); xu et al. (2018a) take the types of ast nodes into account and generate the corresponding programming codes. cai et al. (2018) borrows the idea of automata theory and considers the specific types of sql grammar in backus-naur form (bnf) and generates accurate sql queries with the help of it. inspired by the methods considering the type � � s lstm lstm lstm … … gen copy operation selection stage what of select acl word distribution (generation) ## model overview we first make the necessary definition and formulation for the input data and the code comment generation problem for our type auxiliary guiding (tag) encoder-decoder framework. definition 1 token-type-tree. token-type-tree t x,τ represents the source code with the node set v , which is a rooted n-ary tree. and let internal node v @xmath0, τ j }, where x j denotes the token sequence and τ j denotes a type from grammar type set t . token-type-tree can be easily constructed from token information of the original source code and type information of its ast or parse tree. according to definition 1, we formulate the code comment generation task as follows. formulation 1 code comment generation with token-type-tree as the input. let s denote training dataset and labeled sample (t x,τ , y) ∈ s, where t x,τ is the input token-type-tree, @xmath1, y 2 , • • • , y m ) is the ground truth comment with m words. the task of code comment generation is to design a model which takes the unlabeled sample t x,τ as input and predicts the output as its comment, denoted as y. our framework follows the encoder-decoder manner, and consists of the revised two major components, namely the type-associated encoder and type-restricted decoder. as shown in fig. 2 . the type-associated encoder, as shown in fig. 2 , recursively takes the token-type-tree t x,τ as input, and maintains the semantic information of the source code in the hidden states. instead of using the same parameter sets to learn the whole tokentype-tree, type-associated encoder utilizes multiple sets of parameters to learn the different type of nodes. the parameters of the cells are adaptively invoked according to the type of the current node during the processing of the input token-type-tree. such a procedure enables the structured semantic representation to contain the type information of the source code. the type-restricted decoder, as shown in the right part of figure 2 , takes the original toke-typetree t x,τ and its semantic representation from encoder as input and generates the corresponding comment. different from conventional decoders which generate output only based on the target dictionary, our type-restricted decoder considers both input code to the encoder and target dictionary as the source of output. attention mechanism is employed to compute an attention vector which is used to generate the output words through a two-stage process: (1) determine either to copy from the original token-type-tree or to generate from the current hidden state according to the distribution of the operation. (2) if the copying operation is selected, the words are copied from the selected node from the token-type-tree t x,τ with restricted types; otherwise, the candidate word will be selected from the target dictionary. the above two-stage process is guided by the type which is extracted from the hidden state of encoder with the help of attention mechanism. such a process enables adaptive switching between copying and generation processes, and not only reduces the search space of the generation process but also addresses the oov problem with the copying mechanism. although the proposed framework provides an efficient solution with the utilization of the type information in the code, training obstacles are raised accordingly: (1) no training labels are provided for the operation selection stage. (2) there is a mismatch between the evaluation metric and the objective function. thus, we further devised an hrl method to train our tag model. in the hrl training, the tag model feeds back the evaluation metric as the learning reward to train the two-stage sampling process without relying on the groundtruth label of operation selection stage. ## type-associated encoder the encoder network aims to learn a semantic representation of the input source code. the key challenge is to provide distinct summarization for the sub-trees with the same structure but different semantics. as shown in the type-associated encoder in fig. 1 , the blue and red dashed blocks have the same 3-ary substructure. the sub-tree in the blue box shares the same sub-structure with the tree in the red box, which is usually falsely processed by the same cell in a vanilla tree-lstm. by introducing the type information, the semantics of the two subtrees are distinguished from each other. our proposed type-associated encoder is designed as a variant n -ary tree-lstm. instead of directly inputting type information as features into the encoder for learning, we integrate the type information as the index of the learning parameter sets of the encoder network. more specifically, differ-ent sets of parameters are defined through different types, which provides a more detailed summarization of the input. as is shown in fig. 1(b ), the two sub-trees in our proposed type-associated encoder are distinguished by the type information. the tree contains n ordered child nodes, which are indexed from 1 to n . for the j-th node, the hidden state and memory cell of its k-th child node is denoted as h jk and c jk , respectively. in order to effectively capture the type information, we set w τ j and b τ j to be the weight and bias of the j-th node, and u τ jk be the weight of the k-th child of the j-th node. the transition equation of the variant n -ary tree-lstm is shown as follow: we employ the forget gate @xcite for the tree-lstm, the parameters for the k-th child of the j-th node's is denoted as f jk . u τ jl,k is used to represent the weight of the type for the l-th child of the j-th node in the k-th forget gate. the major difference between our variants and the traditional tree-lstm is that the parameter set (w τ , u τ , b τ ) are specified for each type τ . for the following word selection stage. if the generation operation is selected, the predicted word will be generated from the targeted dictionary. if the copying operation is selected, then a type-restricted copying mechanism is enabled to restrict the search space by masking down the illegal grammar types. furthermore, a copying decay strategy is illustrated to solve the issue of repetitively focusing on specific nodes caused by the attention mechanism. the details of each part are given below. attention mechanism: the encoder extracts the semantic representation as the hidden state of the rooted nodes, denoted as h r , which are used to initialize the hidden state of the decoder, z 0 ← h r . at time step m, given output y m-1 and the hidden state of the decoder z m-1 at last time step m -1, the hidden state z m is recursively calculated by the lstm cells in the decoder, the attention vector q is calculate with: operation selection stage: operation selection stage determines either using the copying operation or the generation operation to select the words based on the attention vector and hidden states from the encoder. specifically, given the attention vector q m at time step m, operation selection stage estimates the conditional probabilities as the distribution of the operation p(â m |ŷ <m ; t x,τ ), where âm ∈ {0, 1} and 0 and 1 represents the copy and the generation operations, respectively. a fully connected layer followed by a softmax is implemented to compute the distribution of the operations. the w s in the eq. 9 is the trainable parameters. since there is no ground-truth label for operation selection, we employ an hrl method to jointly train the operation selection stage and the following stage, the details are provided in section 6. ## conclusion in this paper, we present a type auxiliary guiding encoder-decoder framework for the code comment generation task. our proposed framework takes full advantage of the type information associated with the code through the well designed type-associated encoder and type-restricted decoder. in addition, a hierarchical reinforcement learning method is provided for the training of our framework. the ex-perimental results demonstrate significant improvements over state-of-the-art approaches and strong applicable potential in software development. our proposed framework also verifies the necessity of the type information in the code translation related tasks with a practical framework and good results. as future work, we will extend our framework to more complex contexts by devising efficient learning algorithms.