# An Intelligent Code Search Approach Using Hybrid Encoders

Authors: Yao Meng
Journal: Wireless Communications and Mobile Computing
 (2021)

---

## Abstract

The intelligent code search with natural language queries has become an important researching area in software engineering. In this paper, we propose a novel deep learning framework At-CodeSM for source code search. The powerful code encoder in At-CodeSM, which is implemented with an abstract syntax tree parsing algorithm (Tree-LSTM) and token-level encoders, maintains both the lexical and structural features of source code in the process of code vectorizing. Both the representative and discriminative models are implemented with deep neural networks. Our experiments on the CodeSearchNet dataset show that At-CodeSM yields better performance in the task of intelligent code searching than previous approaches.


---

## Body

## 1. Introduction

In the modern world, the application of computer software has already penetrated every aspect of our lives. The necessities of life, such as medical care, resources, transportation, and public security, depend on the running of high-quality software. For code quality and efficiency, programmers repeatedly involve successful code snippets developed by other authors. It has been a key task in the field of software engineering to locate target code snippets, which are returned by natural language queries, in code repositories such as GitHub and SourceForge.The traditional code search algorithms are mostly implemented with information retrieval methods, which are widely used in the field of natural language processing. These approaches usually treat the source code as a plain text or a sequence of tokens, and then return the related code snippets by computing the similarity of queries and code documents. The token-based code search toolkits have been already used in industry, e.g., Sourcerer and Krugle. They treat both the query words and code snippets in codebase as plain text and return the target codes by key word-matching or template-matching technology. In the academic area, McMillan et al. propose a searching model Portfolio [1], which returns a chain of functions through keyword matching and the PageRank algorithm. Lv et al. developed a famous code-searching engine CodeHow [2], which combines text similarity and API matching through an extended Boolean model.Source code search algorithms based on text retrieval strategy often fail in practice. Different from plain text, code snippets usually contain both the structural and semantic information. The source code snippets made by programmers and queries written in natural languages are essentially heterogeneous. They may not share common lexical tokens, synonyms, or language structures. Sometimes programmers are extremely unsatisfied with searching results from traditional information retrieval algorithms. To improve code search according to natural language queries, researchers tend to use various code representations. They construct different code encoders with machine learning methods, which map source code and query words to the same vector space. The most related code vectors are returned as searching results according to the distance [3] between adjacent vectors.In recent years, with the rapid development of deep learning technology, researchers in the field of software engineering begin to analyze the structural and semantic features of source code with deep learning approaches. Based on the common neural networks such as the convolutional neural network, the recursive neural network, and the recurrent neural network, scholars exploit various deep encoders to represent source code in different vector spaces [4–6]. Compared to traditional approaches, deep neural networks can extract the structural and semantic information hidden in the source code snippets during code embedding, which might improve the representation ability of code vectors.In this paper, we propose a novel source code search model using deep learning algorithms. The model maps both the code snippets in the codebase and natural language queries to the same high-dimensional vector space and then return the most related code snippets by computing the cosine similarity between code vectors and query vectors. Different from previous research, At-CodeSM vectorizes the source code with three independent encoders, e.g., the method name encoder, the lexical encoder, and the structural encoder. The lexical encoder extracts the tokens from the source code and encodes them with an attention-based LSTM network. The core tokens are strengthened with heavy attention scores. The structural encoder firstly transforms the source code into an AST, and then encodes the whole tree using a tree-LSTM network in a recursive way. The combination of three encoders in At-CodeSM retains both the lexical and syntactics of code in the process of representation, which improves the searching performance.In practice, At-CodeSM firstly updates the parameters of the source code encoder and query encoder by offline learning on the training set. It then conducts an offline embedding on all the code snippets in the search base. Finally, At-CodeSM uses an online matching algorithm to compare the cosine similarity between the query vector and code vectors from the search base. The code snippets with higher similarity scores are returned as the searching results.The main contributions of this work are as follows:(i)
We propose a novel source code representation model based on the deep neural network. The model implements the encoding of method name, method body, and method grammar tree, respectively, with three independent encoders. Compared with other models, the representation process in At-CodeSM effectively retains both the lexical and semantic characteristics of the code snippets(ii)
To our knowledge, we are the first to introduce a self-attention mechanism, which was developed in NLP, into the area of intelligent code search. With the self-attention mechanism, key words in the source code are strengthened, which finally improve the searching performance(iii)
The big achievements are made when we apply our representation model in the intelligent code search area. We improve the model parameters with supervised learning on a large-scale dataset CodeSearchNet, which is made by Microsoft. The experiments show that our model is superior to the state-of-art approachesThe remainder of this paper is organized as follows: Section2 describes related work in the code search area. Section 3 presents background materials of various LSTM structures and the self-attention mechanism. Section 4 elaborates on the details of At-CodeSM. Section 5 gives the experiment and evaluation. In Section 6, we discuss the crucial factors which may affect the searching performance. Finally, we conclude the paper in Section 7.


## 2. Related Work

### 2.1. Traditional Approaches

Using common NLP methods, most traditional code search approaches treat source code as plain text or a set of tokens, ignoring its structure and semantics. Such models often extract code features manually or with NLP methods, and then return related code snippets via a text similarity algorithm [7, 8]. Mostly applied in the early stage, these models completed simple search tasks with keyword matching. Due to the low complexity and cross-platform features, these approaches have been widely used in industry. For instance, Lucene is a successful code search platform implemented with traditional approaches.In 1993, Chang and Eastman [9] proposed a famous code search tool SMART, which was based on keyword searching. SMART identified the code snippets as token sequences and then returned the target code according to a common keyword-matching algorithm. The early SMART model implemented an exact matching algorithm on keywords. Inspired by SMART, software companies such as Google and GitHub improved matching strategies in their code search tools with fuzzy search algorithms.People began to study different matching algorithms for code search performance. LV et al. [2] proposed CodeHow, which transformed the queries into corresponding APIs. The target code was returned by API matching in codebase. Reiss et al. [10] presented a semantic search engine, which improved the searching performance by filtering unrelated code snippets. The structural semantic indexing model proposed by Bajracharya et al. [11] could establish the connection between natural language queries and source code keywords.Marcus et al. [12] attempted to introduce Latent Semantic Analysis (LSA) into the field of code search. Their model made a good achievement by semantic code analysis. Inspired by a previous study, Jiang et al. [13] proposed ROSF, which combined IR and supervised learning technology during code embedding. It performed well in the task of code search when applying multiple structural extraction approaches.


### 2.2. Deep Learning Approaches

With the development of deep learning theory, people tend to solve problems in every field with deep learning approaches [14–18]. In recent years, researchers in the field of software engineering have introduced deep neural networks [19–22] such as multilayer perceptron network, recurrent neural network and convolutional neural network to solve the problems. The researchers carefully update their models with supervised or unsupervised learning on large-scale datasets. The neural network model through supervised or unsupervised training. Compared with previous coding encoding algorithms, deep neural encoders can automatically extract the structural and semantic features hidden in the source code.Iyer et al. [23] presented LSTM networks with attention to produce summaries that describe C# code snippets and SQL queries. Their model codeNN is built on a classical encoder-decoder framework, which takes the source code as plain text and models the conditional distribution of the summary. Allamanis et al. [24] applied a neural convolutional attentional model to vectorize the source code snippets. These learning-based approaches mainly learn the latent features from source code with neural networks.Gu et al. [25] proposed a code search model called codeNN with complicated neural networks. Inspired by joint embedding in the image processing field, codeNN treats the code snippet as a combination of function names, API calls, and a sequence of tokens. The code encoder is implemented with bi-LSTM and MLP networks. They build their own dataset by collecting Java source code snippets from GitHub. The first sentence of the code comment is identified as the data label. The typical questions posted on StackOverflow are accumulated as the queries in the test set, and corresponding code snippets for each query are also collected with other code search engines. The matching scores between snippets and queries are judged by experts. The construction of datasets and evaluation methods have been used by scholars for contrast experiments. codeNN makes full use of the structural characteristics of source code in the process of code encoding. It outperforms the classical code search engines Lucene and CodeHow. Although inaccurate results might be returned, or partial-related items are ahead of exact matching ones in the result list, it is still the most effective code search method right now. Cambronero et al. [19] reconfirmed this ranking in their latest review in 2019, but they declared the complex network structure also resulted in low embedding efficiency.The searching models with unsupervised learning has attracted the attention of researchers for lack of labeled data, although their performance might not match one with supervised learning models. Sachdev et al. [3] trained their search model on large-scale corpus with unsupervised learning. The model encodes all the snippets in a codebase with common document embedding and TD-IDF techniques, and then returns related code vectors according to a specific vector similarity algorithm.The famous pretrained model BERT [26], which was proposed in 2018, has played an important role in word-embedding techniques in the area of NLP. Researchers Kanade et al. [27] were the first to introduce BERT into the field of code retrieval. The contrast experiments show that their model with BERT embedding outperforms searching models based on LSTM networks with word2vec embedding. They declare that their pretrained model also outperforms the Transformer [28], which is trained from scratch.Recent scholars manage to improve the models’ searching ability by intensifying the expressiveness of queries [29–31]. Programmers often apply code search with few words for simplicity so that queries lack semantic information, which fails to meet the requirements of developers. In 2019, Liu et al. [32] proposed a NQE model which is capable of enlarging query words. The query enhancer in the model inputs a small number of query keywords and outputs an extended query statement with new words. The searching models are trained on a specific corpus with supervised learning. Experiments show that the searching performance is obviously improved by the query enhancer in NQE.


## 2.1. Traditional Approaches

Using common NLP methods, most traditional code search approaches treat source code as plain text or a set of tokens, ignoring its structure and semantics. Such models often extract code features manually or with NLP methods, and then return related code snippets via a text similarity algorithm [7, 8]. Mostly applied in the early stage, these models completed simple search tasks with keyword matching. Due to the low complexity and cross-platform features, these approaches have been widely used in industry. For instance, Lucene is a successful code search platform implemented with traditional approaches.In 1993, Chang and Eastman [9] proposed a famous code search tool SMART, which was based on keyword searching. SMART identified the code snippets as token sequences and then returned the target code according to a common keyword-matching algorithm. The early SMART model implemented an exact matching algorithm on keywords. Inspired by SMART, software companies such as Google and GitHub improved matching strategies in their code search tools with fuzzy search algorithms.People began to study different matching algorithms for code search performance. LV et al. [2] proposed CodeHow, which transformed the queries into corresponding APIs. The target code was returned by API matching in codebase. Reiss et al. [10] presented a semantic search engine, which improved the searching performance by filtering unrelated code snippets. The structural semantic indexing model proposed by Bajracharya et al. [11] could establish the connection between natural language queries and source code keywords.Marcus et al. [12] attempted to introduce Latent Semantic Analysis (LSA) into the field of code search. Their model made a good achievement by semantic code analysis. Inspired by a previous study, Jiang et al. [13] proposed ROSF, which combined IR and supervised learning technology during code embedding. It performed well in the task of code search when applying multiple structural extraction approaches.


## 2.2. Deep Learning Approaches

With the development of deep learning theory, people tend to solve problems in every field with deep learning approaches [14–18]. In recent years, researchers in the field of software engineering have introduced deep neural networks [19–22] such as multilayer perceptron network, recurrent neural network and convolutional neural network to solve the problems. The researchers carefully update their models with supervised or unsupervised learning on large-scale datasets. The neural network model through supervised or unsupervised training. Compared with previous coding encoding algorithms, deep neural encoders can automatically extract the structural and semantic features hidden in the source code.Iyer et al. [23] presented LSTM networks with attention to produce summaries that describe C# code snippets and SQL queries. Their model codeNN is built on a classical encoder-decoder framework, which takes the source code as plain text and models the conditional distribution of the summary. Allamanis et al. [24] applied a neural convolutional attentional model to vectorize the source code snippets. These learning-based approaches mainly learn the latent features from source code with neural networks.Gu et al. [25] proposed a code search model called codeNN with complicated neural networks. Inspired by joint embedding in the image processing field, codeNN treats the code snippet as a combination of function names, API calls, and a sequence of tokens. The code encoder is implemented with bi-LSTM and MLP networks. They build their own dataset by collecting Java source code snippets from GitHub. The first sentence of the code comment is identified as the data label. The typical questions posted on StackOverflow are accumulated as the queries in the test set, and corresponding code snippets for each query are also collected with other code search engines. The matching scores between snippets and queries are judged by experts. The construction of datasets and evaluation methods have been used by scholars for contrast experiments. codeNN makes full use of the structural characteristics of source code in the process of code encoding. It outperforms the classical code search engines Lucene and CodeHow. Although inaccurate results might be returned, or partial-related items are ahead of exact matching ones in the result list, it is still the most effective code search method right now. Cambronero et al. [19] reconfirmed this ranking in their latest review in 2019, but they declared the complex network structure also resulted in low embedding efficiency.The searching models with unsupervised learning has attracted the attention of researchers for lack of labeled data, although their performance might not match one with supervised learning models. Sachdev et al. [3] trained their search model on large-scale corpus with unsupervised learning. The model encodes all the snippets in a codebase with common document embedding and TD-IDF techniques, and then returns related code vectors according to a specific vector similarity algorithm.The famous pretrained model BERT [26], which was proposed in 2018, has played an important role in word-embedding techniques in the area of NLP. Researchers Kanade et al. [27] were the first to introduce BERT into the field of code retrieval. The contrast experiments show that their model with BERT embedding outperforms searching models based on LSTM networks with word2vec embedding. They declare that their pretrained model also outperforms the Transformer [28], which is trained from scratch.Recent scholars manage to improve the models’ searching ability by intensifying the expressiveness of queries [29–31]. Programmers often apply code search with few words for simplicity so that queries lack semantic information, which fails to meet the requirements of developers. In 2019, Liu et al. [32] proposed a NQE model which is capable of enlarging query words. The query enhancer in the model inputs a small number of query keywords and outputs an extended query statement with new words. The searching models are trained on a specific corpus with supervised learning. Experiments show that the searching performance is obviously improved by the query enhancer in NQE.


## 3. Preliminaries

In this section, we briefly give some well-accepted definitions in the area of source code search and deep learning.


### 3.1. Abstract Syntax Tree

Abstract syntax tree is a kind of syntax tree representing the abstract syntactic structure of the source code. It has been widely used by programming compliers and software engineering tools due to the powerful representation ability. Different from concrete syntax trees (CTS), abstract syntax trees do not contain all the details of source code such as the punctuation and delimiters. They only include the syntax structure of the source code at an abstract level. AST contains both the lexical and syntax information of the source code, which is often employed in the industry as an intermediate tool to extract the hidden informationFigure1 is the structure of an AST. Nodes of the AST correspond to the constructs or symbols of the source code. We conclude from the graph that the AST fully retains the structural information of the source code.Figure 1
Abstract syntax tree.


### 3.2. Attention

The attention mechanism is the internal process for machines to imitate the human observation of things in the world. When processing images, our vision quickly obtains the target area by a global scan, i.e., the focus of attention. Then, the brain pays more attention to the focus area for details, while suppressing irrelevant information.Bahdanau et al. [33] firstly applied the attention mechanism in the field of machine translation, i.e., sequence to sequence learning. He successfully solved the problem of long-term dependency in machine translation, i.e., the translation models based on fixed vector representations often lost history information of long sentences in the decoding process. After that, people began to study deep encoders with attention layers, which achieved amazing effects in all areas of NLP.Mathematically, the attention is essentially a mapping function composed of Query, Key, and Value. The calculation of attention values is divided into three steps:Step 1.
Query is combined with each Key to calculate the attention weight. The similarity functionfQ,Kcan be defined in multiple ways. The simplest calculation is shown as follows:
(1)fQ,K=KTQ.Step 2.
The softmax function is used to normalize the attention weight obtained in Step1, as shown in equation (2). Sometimes, we have to scale the attention scores later as the original ones are too large to calculate:
(2)ai=softmaxfQ,K.Step 3.
The final attention is the weighted sum of normalized weightsai and corresponding values, which is shown as follows:
(3)AttentionQ,K,V=∑aiV.The attention mechanism is widely used in RNN-based encoder-decoder models. The input of the decoder’s current state is determined by the weighted average of all hidden layers’ output values in the encoder. The attention algorithm is transformed to the self-attention algorithm whenQ=K=V in the encoder [28]. Our lexical encoder in At-CodeSM is implemented with a specific self-attention LSTM layer.


### 3.3. LSTM Networks

Recurrent neural networks (RNN) can process input sequences of arbitrary length via the recurrent structure with shared weights. Unfortunately, a common problem with the traditional RNN is that components of the gradient vector can grow or decay exponentially over long sequences during training. Therefore, the LSTM architecture [34], which was invented by Hochreiter and Schmidhuber in 1997, addresses this problem of learning long-term dependency by introducing a memory cell that is able to preserve states over long periods of time. The core part of the LSTM is a cell memory Ct, including an input gate it, an output gate ot, and a forget gate ft. Figure 2 describes the basic structure of LSTM. Different from the original RNN, the LSTM solves the problem of long-term dependency effectively with the special structure memory cell, discarding the trivial information in the history to avoid the gradient vanishing.Figure 2
Structure of standard LSTM.The cell memory of LSTM is composed of three gate controllers. The forget gateft controls the extent to which the previous memory cell is forgotten, which is essential a sigmoid function. The entry of ft is a weighted combination of the previous output value and the current input value. The input gate it controls how much each unit is updated, and the output gate ot controls the exposure of the internal memory state. The LSTM transition equations are the following:
(4)ft=σWf∙ht−1,xt+bf,it=σWi∙ht−1,xt+bi,Ct=ft×Ct−1+it×tanhWf∙ht−1,xt+bc,ot=σWo∙ht−1,xt+bo,ht=ot∙tanhCt,where Wi, Wf, Wo are the weighted matrices, and bi, bf, bo are the biases of LSTM to be learned during training, parameterizing the transformations of the input, forget, and output gates, respectively. σ and tanh are the activation functions, and ∙ denotes the element-wise multiplication. xt is the input of the LSTM cell unit, and ht is the output of the hidden layer at the current time step.In order to deal with structural data, Tai et al. [35] invented tree-based LSTM networks (Tree-LSTM) from the standard LSTM networks. Figure 3 shows the difference between standard LSTM and tree-LSTM networks. Figure 3(a) is the common LSTM network designed for sequential data, while Figure 3(b) is the tree-LSTM network used for structural data. Unlike traditional LSTM networks, tree-LSTM contains an output and n inputs at time step t, for which it is convenient to handle data stored in trees. Normally, it is used for binary trees when n=2. The updating equations of tree-LSTM are detailed in Section 4.2.3.Figure 3
Comparison between standard LSTM and tree-LSTM.
(a)(b)


## 3.1. Abstract Syntax Tree

Abstract syntax tree is a kind of syntax tree representing the abstract syntactic structure of the source code. It has been widely used by programming compliers and software engineering tools due to the powerful representation ability. Different from concrete syntax trees (CTS), abstract syntax trees do not contain all the details of source code such as the punctuation and delimiters. They only include the syntax structure of the source code at an abstract level. AST contains both the lexical and syntax information of the source code, which is often employed in the industry as an intermediate tool to extract the hidden informationFigure1 is the structure of an AST. Nodes of the AST correspond to the constructs or symbols of the source code. We conclude from the graph that the AST fully retains the structural information of the source code.Figure 1
Abstract syntax tree.


## 3.2. Attention

The attention mechanism is the internal process for machines to imitate the human observation of things in the world. When processing images, our vision quickly obtains the target area by a global scan, i.e., the focus of attention. Then, the brain pays more attention to the focus area for details, while suppressing irrelevant information.Bahdanau et al. [33] firstly applied the attention mechanism in the field of machine translation, i.e., sequence to sequence learning. He successfully solved the problem of long-term dependency in machine translation, i.e., the translation models based on fixed vector representations often lost history information of long sentences in the decoding process. After that, people began to study deep encoders with attention layers, which achieved amazing effects in all areas of NLP.Mathematically, the attention is essentially a mapping function composed of Query, Key, and Value. The calculation of attention values is divided into three steps:Step 1.
Query is combined with each Key to calculate the attention weight. The similarity functionfQ,Kcan be defined in multiple ways. The simplest calculation is shown as follows:
(1)fQ,K=KTQ.Step 2.
The softmax function is used to normalize the attention weight obtained in Step1, as shown in equation (2). Sometimes, we have to scale the attention scores later as the original ones are too large to calculate:
(2)ai=softmaxfQ,K.Step 3.
The final attention is the weighted sum of normalized weightsai and corresponding values, which is shown as follows:
(3)AttentionQ,K,V=∑aiV.The attention mechanism is widely used in RNN-based encoder-decoder models. The input of the decoder’s current state is determined by the weighted average of all hidden layers’ output values in the encoder. The attention algorithm is transformed to the self-attention algorithm whenQ=K=V in the encoder [28]. Our lexical encoder in At-CodeSM is implemented with a specific self-attention LSTM layer.


## 3.3. LSTM Networks

Recurrent neural networks (RNN) can process input sequences of arbitrary length via the recurrent structure with shared weights. Unfortunately, a common problem with the traditional RNN is that components of the gradient vector can grow or decay exponentially over long sequences during training. Therefore, the LSTM architecture [34], which was invented by Hochreiter and Schmidhuber in 1997, addresses this problem of learning long-term dependency by introducing a memory cell that is able to preserve states over long periods of time. The core part of the LSTM is a cell memory Ct, including an input gate it, an output gate ot, and a forget gate ft. Figure 2 describes the basic structure of LSTM. Different from the original RNN, the LSTM solves the problem of long-term dependency effectively with the special structure memory cell, discarding the trivial information in the history to avoid the gradient vanishing.Figure 2
Structure of standard LSTM.The cell memory of LSTM is composed of three gate controllers. The forget gateft controls the extent to which the previous memory cell is forgotten, which is essential a sigmoid function. The entry of ft is a weighted combination of the previous output value and the current input value. The input gate it controls how much each unit is updated, and the output gate ot controls the exposure of the internal memory state. The LSTM transition equations are the following:
(4)ft=σWf∙ht−1,xt+bf,it=σWi∙ht−1,xt+bi,Ct=ft×Ct−1+it×tanhWf∙ht−1,xt+bc,ot=σWo∙ht−1,xt+bo,ht=ot∙tanhCt,where Wi, Wf, Wo are the weighted matrices, and bi, bf, bo are the biases of LSTM to be learned during training, parameterizing the transformations of the input, forget, and output gates, respectively. σ and tanh are the activation functions, and ∙ denotes the element-wise multiplication. xt is the input of the LSTM cell unit, and ht is the output of the hidden layer at the current time step.In order to deal with structural data, Tai et al. [35] invented tree-based LSTM networks (Tree-LSTM) from the standard LSTM networks. Figure 3 shows the difference between standard LSTM and tree-LSTM networks. Figure 3(a) is the common LSTM network designed for sequential data, while Figure 3(b) is the tree-LSTM network used for structural data. Unlike traditional LSTM networks, tree-LSTM contains an output and n inputs at time step t, for which it is convenient to handle data stored in trees. Normally, it is used for binary trees when n=2. The updating equations of tree-LSTM are detailed in Section 4.2.3.Figure 3
Comparison between standard LSTM and tree-LSTM.
(a)(b)


## 4. Our Proposed Approach

### 4.1. General Framework

In this section, we detail the workflow of At-CodeSM, which is summarized in Figure4. It contains a source code encoder Ec and a query encoder Eq. Ec maps the source code into representation vectors with a neural network, while Eq maps the queries written in natural languages into vectors with another neural network. Both encoders are trained with supervised learning on large corpora.Figure 4
The workflow of At-CodeSM.The code search usually contains an offline data preprocessing and an online search. In the process of offline data preprocessing, At-CodeSM updatesEc and Eq on the training corpus, and then apply an offline embedding on the search corpus with Ec. All the snippets in the search base are transformed to code vectors. In the process of online search, Eq firstly encodes the users’ query, and then matches the query vector with each code vector on the search corpus. The top k snippets are returned as search lists according to the cosine similarity between vectors. All the encoders of At-CodeSM are detailed in the following subsections.


### 4.2. Source Code Encoder

As the core component of our model,Ec applies lexical embedding and structural embedding, respectively, during encoding process, so that the output vector maintains both the structural and semantic characteristics of the source code.Figure5 shows the structure of the source code encoder. It is a combined model, which includes a method name encoder, a lexical encoder, and a structural encoder. The method name encoder inputs the full name of the function and output a representation vector Vname. The lexical encoder inputs the body of the function and outputs a representation vector Vtoken. The structural encoder outputs a representation vector Vast by embedding the corresponding AST with a specific traversal algorithm. Finally, Vname, Vtoken, and Vast are fused into a unified vector vc through a fully connected layer in At-CodeSM.Figure 5
The structure of source code encoder and query encoder.We present all the encoders in the following sections.


#### 4.2.1. Lexical Embedding

The body of source code methods contains many core words which describes the functionality. During lexical embedding, At-CodeSM extracts core tokens from the method body, and then embeds them with common word-embedding technology in NLP by treating the source code snippets as plain texts. Finally, the output vector is generated with an attentional LSTM network. We use an independent attention layer at the back of lexical encoder, as the contribution of each token varies in implementing the functionality.The process of lexical embedding is divided into three steps:(1)
Data preprocessing: in order to collect tokens from a Java method, we tokenize the method body and split each token according to a camel case, e.g.,changeToPDF and parseXML. All the duplicated tokens, stop words, and Java keywords are removed as they frequently occur in source code and are not discriminative(2)
Token embedding: letT=token1,token2,token3,⋯ denote the sequence of tokens from source code after step (1). The lexical encoder includes a transformation f, which makes ftoken1,token2,token3,⋯=vtoken1,vtoken2,vtoken3,⋯. By contrast, we implement the transformation f with Word2vec [36].(3)
Source code vector generation: the sequence of token vectorsvtoken1, vtoken2, vtoken3, … are loaded into a standard bi-LSTM. Equation (5) shows the calculation of LSTM, where ht⟶ and ht⃐ denote the forward and backward hidden status at step t, and ht is the concatenation of ht⟶ and ht⃐(5)ht⟶=LSTM⟶vtokentt∈1,n,ht⃐=LSTM⃐vtokentt∈n,1,ht=ht⟶,ht⃐t∈1,n.However, the effect of each token is different in high-level programming languages, e.g., the tokenXML is more important than other tokens in a method dealing with the data storage in XML files. In this paper, we attempt to introduce the self-attention algorithm in the process of lexical embedding. To our knowledge, it is the first time to use attention mechanism in the field of code search. Since our model only includes the encoder, we decide to use the self-attention algorithm. The attention scores can be calculated in many ways as there are various definitions of similarity function. In our paper, equation (6) and equation (7) are used to calculate the attention scores, which are defined in [37]:
(6)αi=exphiTK∑iexphiTK,i∈1,n,(7)vtoken=∑iαihi,where hi denotes the hidden states of the bidirectional LSTM, K denotes a context vector which is initialized randomly. Hu et al. [38] indicates that the inner product of hi and K denotes the contribution of hi to the source code vector. The value of K is updated continuously with other parameters via supervised learning. αi denotes the ith attention score, corresponding with the hidden state hi. vtoken is the representation vector of the given code fragment, which is generated by the weighted sum of all the hidden states. Figure 6 shows the encoding process with the self-attention layer.Figure 6
Lexical embedding with self-attention.


#### 4.2.2. Method Name Embedding

The method name is a description of the method functionality, which is composed of a small amount of keywords. Early code search algorithms apply a simple keyword matching between queries and method names. At-CodeSM uses an independent encoder for the embedding of method names, which is simpler than lexical embedding. LetT=name1,name2,name3,⋯ denote the sequence of small names extracted from the method name; after that, the encoder applies a transformation f, which makes fname1,name2,name3,⋯=vname1,vname2,vname3,⋯ with Word2vec technology. Finally, At-CodeSM outputs the representation vector for the method name with a bi-LSTM network, which is shown as follows:
(8)vname=hmt=hmt⟶,hmt⃐,where hmt⟶ and hmt⃐ are the forward and backward hidden states of the bi-LSTM, vname is the final semantic vector of the method name, generated from the concatenation of hmt⟶ and hmt⃐. We tried to implement self-attention in the embedding layer, which had no performance improvement by experiments. Experimental results with various self-attention layers are discussed later.


#### 4.2.3. Structural Embedding

We employ javalang to generate an abstract syntax tree (AST) from the source code snippet. The structural encoder then transforms the AST into a high-dimensional vectorVast according to a specific AST traversal algorithm. Recent researchers have proposed several AST embedding algorithms, e.g., RvNN embedding algorithm [6], structure-based algorithm (SBT) [38], tree-LSTM embedding algorithm [35], and tree-CNN embedding algorithms [39]. We implement the structural encoder in At-CodeSM with a special tree-LSTM embedding algorithm, which is used by Hu et al. in their code clone detector CDLH.Tai et al. proposed the tree-based LSTM networks, which is used for the embedding of ASTs in CDLH. Different from standard LSTM networks, neural cells in tree-LSTM contain several forget gates, each of which corresponds with an input. The transition equations for tree-LSTM are the following:(9)i=σWix+∑i=1LUilrl+bi,fl=σWfx+Uflrl+bf,l=1,2,⋯,L,o=σWox+∑i=1LUolrl+bo,u=tanhWux+∑i=1LUulrl+bu,c=i⊙u+∑i=1Lfl⊙cl,r=o⊙tanhc,where x is the embedding of the corresponding token, L denotes the number of children, fl are L forget gates for the children of the AST node, l is the index number for its children, r is a control variable for the output, Wi, Wf, Wo, Wu, Uil, Ufl, Uol, and Uul are weight matrices, and bi, bf, bo, bu are bias vectors. ⊙ denotes the element-wise multiplication, and σ is the sigmoid function.The original ASTs generated from source code might have multiple branches. The classical tree-LSTM networks can be used for the embedding of ASTs with multiple branches. The number of childrenL varies for different AST nodes, which might cause problems in the parameter-sharing of weight matrices. We transform the original ASTs into binary trees, whose nodes contain 0 or 2 children.The transform algorithm for binary trees is divided into two steps:(1)
We split nodes with more than 2 children and generate a new right child together with the old left child as its children, and then put all children except the leftmost as the children of this new node. Repeat the operation in an up-down way until only nodes with 0, 1, and 2 children are left(2)
The nodes with 1 child are merged with its childAfter transformation, the AST is transformed into a full binary tree, nodes of which contain 0 or 2 children. Figure7 describes the transformation of a subtree. The binary tree on the right part of the figure is transformed from the left one, and node 6 and node 7 are generated after two splits.Figure 7
The transformation for binary trees.For implementation, we input the embeddings of the two children into the Tree-LSTM networks, and the hidden state for each internal node is computed recursively in a bottom-up way. The final output from the AST rootVast is the representation for the code snippet.


### 4.3. Query Embedding

In practice, programmers input queries in the repositories for code search. In the training process, the query encoder is updated with target descriptions. Both queries and descriptions are a sequence of natural language words. The lower part of Figure5 describes the structure of the query encoder equation. The process of query embedding is similar to the embedding of method names in source code. The query encoder transforms a sequence of tokens to a sequence of vectors vquery1, vquery2, and vquery3 with word-embedding technology. Finally, the embedding network embeds this sequence of vectors into a query vector using bi-LSTM, which is shown as follows:
(10)vquery=hqt=hqt⟶,hqt⃐,where hqt⟶ and hqt⃐ are the forward and backward hidden states produced by the final layer of LSTM in the query encoder, and vquery is the output vector of the query sentence. We tried to add a self-attention layer in the query encoder, which had no performance improvement by experiments.


### 4.4. Loss Function

In this section, we present the loss function used in At-CodeSM. With carefully trained encoders, and At-CodeSM maps both the source code and queries to different vectors in a unified vector space. The goal of training is as follows: if a code snippet and a description have similar semantics, their embedded vectors should be close to each other. In other words, given an arbitrary code snippet C and an arbitrary description D, we want it to predict a distance with high similarity ifD is an exact description of C, and a little similarity otherwise.We build each training instance as a tripleC,D+,D− for supervised training. For each code snippet C, there is a positive description D+ (correct description) and a negative description D− (false description) randomly chosen from the collection of other D+. In the training process, the search model predicts the cosine similarities of both C,D+ and C,D− pairs and minimizes the ranking loss [40], which is defined as follows:
(11)Lθ=∑C,D+,D−∈Pmax0,ϵ−cosc,d++cosc,d−,where θ indicates the model parameters, P indicates the training corpus, ϵ is a constant margin. c, d+, and d− are the embedded vectors of C, D+, and D‐, respectively. The lost function Lθ encourages the similarity between a code snippet and its correct description to increase, and the similarity between a code snippet and its false description to decrease.


### 4.5. Evaluation Metrics

The objective of code search models is to locate suitable code snippets in code repositories. They often return many results based on similarity algorithms due to the big capacity of codebase. In most cases, developers only focus on the snippets ahead in the result list. In our experiment, we select MRR and NDCG as evaluation metrics which are often used in the field of information retrieval [2, 25, 41, 42], to compare the performances of At-CodeSM and other baselines.


## 4.1. General Framework

In this section, we detail the workflow of At-CodeSM, which is summarized in Figure4. It contains a source code encoder Ec and a query encoder Eq. Ec maps the source code into representation vectors with a neural network, while Eq maps the queries written in natural languages into vectors with another neural network. Both encoders are trained with supervised learning on large corpora.Figure 4
The workflow of At-CodeSM.The code search usually contains an offline data preprocessing and an online search. In the process of offline data preprocessing, At-CodeSM updatesEc and Eq on the training corpus, and then apply an offline embedding on the search corpus with Ec. All the snippets in the search base are transformed to code vectors. In the process of online search, Eq firstly encodes the users’ query, and then matches the query vector with each code vector on the search corpus. The top k snippets are returned as search lists according to the cosine similarity between vectors. All the encoders of At-CodeSM are detailed in the following subsections.


## 4.2. Source Code Encoder

As the core component of our model,Ec applies lexical embedding and structural embedding, respectively, during encoding process, so that the output vector maintains both the structural and semantic characteristics of the source code.Figure5 shows the structure of the source code encoder. It is a combined model, which includes a method name encoder, a lexical encoder, and a structural encoder. The method name encoder inputs the full name of the function and output a representation vector Vname. The lexical encoder inputs the body of the function and outputs a representation vector Vtoken. The structural encoder outputs a representation vector Vast by embedding the corresponding AST with a specific traversal algorithm. Finally, Vname, Vtoken, and Vast are fused into a unified vector vc through a fully connected layer in At-CodeSM.Figure 5
The structure of source code encoder and query encoder.We present all the encoders in the following sections.


### 4.2.1. Lexical Embedding

The body of source code methods contains many core words which describes the functionality. During lexical embedding, At-CodeSM extracts core tokens from the method body, and then embeds them with common word-embedding technology in NLP by treating the source code snippets as plain texts. Finally, the output vector is generated with an attentional LSTM network. We use an independent attention layer at the back of lexical encoder, as the contribution of each token varies in implementing the functionality.The process of lexical embedding is divided into three steps:(1)
Data preprocessing: in order to collect tokens from a Java method, we tokenize the method body and split each token according to a camel case, e.g.,changeToPDF and parseXML. All the duplicated tokens, stop words, and Java keywords are removed as they frequently occur in source code and are not discriminative(2)
Token embedding: letT=token1,token2,token3,⋯ denote the sequence of tokens from source code after step (1). The lexical encoder includes a transformation f, which makes ftoken1,token2,token3,⋯=vtoken1,vtoken2,vtoken3,⋯. By contrast, we implement the transformation f with Word2vec [36].(3)
Source code vector generation: the sequence of token vectorsvtoken1, vtoken2, vtoken3, … are loaded into a standard bi-LSTM. Equation (5) shows the calculation of LSTM, where ht⟶ and ht⃐ denote the forward and backward hidden status at step t, and ht is the concatenation of ht⟶ and ht⃐(5)ht⟶=LSTM⟶vtokentt∈1,n,ht⃐=LSTM⃐vtokentt∈n,1,ht=ht⟶,ht⃐t∈1,n.However, the effect of each token is different in high-level programming languages, e.g., the tokenXML is more important than other tokens in a method dealing with the data storage in XML files. In this paper, we attempt to introduce the self-attention algorithm in the process of lexical embedding. To our knowledge, it is the first time to use attention mechanism in the field of code search. Since our model only includes the encoder, we decide to use the self-attention algorithm. The attention scores can be calculated in many ways as there are various definitions of similarity function. In our paper, equation (6) and equation (7) are used to calculate the attention scores, which are defined in [37]:
(6)αi=exphiTK∑iexphiTK,i∈1,n,(7)vtoken=∑iαihi,where hi denotes the hidden states of the bidirectional LSTM, K denotes a context vector which is initialized randomly. Hu et al. [38] indicates that the inner product of hi and K denotes the contribution of hi to the source code vector. The value of K is updated continuously with other parameters via supervised learning. αi denotes the ith attention score, corresponding with the hidden state hi. vtoken is the representation vector of the given code fragment, which is generated by the weighted sum of all the hidden states. Figure 6 shows the encoding process with the self-attention layer.Figure 6
Lexical embedding with self-attention.


### 4.2.2. Method Name Embedding

The method name is a description of the method functionality, which is composed of a small amount of keywords. Early code search algorithms apply a simple keyword matching between queries and method names. At-CodeSM uses an independent encoder for the embedding of method names, which is simpler than lexical embedding. LetT=name1,name2,name3,⋯ denote the sequence of small names extracted from the method name; after that, the encoder applies a transformation f, which makes fname1,name2,name3,⋯=vname1,vname2,vname3,⋯ with Word2vec technology. Finally, At-CodeSM outputs the representation vector for the method name with a bi-LSTM network, which is shown as follows:
(8)vname=hmt=hmt⟶,hmt⃐,where hmt⟶ and hmt⃐ are the forward and backward hidden states of the bi-LSTM, vname is the final semantic vector of the method name, generated from the concatenation of hmt⟶ and hmt⃐. We tried to implement self-attention in the embedding layer, which had no performance improvement by experiments. Experimental results with various self-attention layers are discussed later.


### 4.2.3. Structural Embedding

We employ javalang to generate an abstract syntax tree (AST) from the source code snippet. The structural encoder then transforms the AST into a high-dimensional vectorVast according to a specific AST traversal algorithm. Recent researchers have proposed several AST embedding algorithms, e.g., RvNN embedding algorithm [6], structure-based algorithm (SBT) [38], tree-LSTM embedding algorithm [35], and tree-CNN embedding algorithms [39]. We implement the structural encoder in At-CodeSM with a special tree-LSTM embedding algorithm, which is used by Hu et al. in their code clone detector CDLH.Tai et al. proposed the tree-based LSTM networks, which is used for the embedding of ASTs in CDLH. Different from standard LSTM networks, neural cells in tree-LSTM contain several forget gates, each of which corresponds with an input. The transition equations for tree-LSTM are the following:(9)i=σWix+∑i=1LUilrl+bi,fl=σWfx+Uflrl+bf,l=1,2,⋯,L,o=σWox+∑i=1LUolrl+bo,u=tanhWux+∑i=1LUulrl+bu,c=i⊙u+∑i=1Lfl⊙cl,r=o⊙tanhc,where x is the embedding of the corresponding token, L denotes the number of children, fl are L forget gates for the children of the AST node, l is the index number for its children, r is a control variable for the output, Wi, Wf, Wo, Wu, Uil, Ufl, Uol, and Uul are weight matrices, and bi, bf, bo, bu are bias vectors. ⊙ denotes the element-wise multiplication, and σ is the sigmoid function.The original ASTs generated from source code might have multiple branches. The classical tree-LSTM networks can be used for the embedding of ASTs with multiple branches. The number of childrenL varies for different AST nodes, which might cause problems in the parameter-sharing of weight matrices. We transform the original ASTs into binary trees, whose nodes contain 0 or 2 children.The transform algorithm for binary trees is divided into two steps:(1)
We split nodes with more than 2 children and generate a new right child together with the old left child as its children, and then put all children except the leftmost as the children of this new node. Repeat the operation in an up-down way until only nodes with 0, 1, and 2 children are left(2)
The nodes with 1 child are merged with its childAfter transformation, the AST is transformed into a full binary tree, nodes of which contain 0 or 2 children. Figure7 describes the transformation of a subtree. The binary tree on the right part of the figure is transformed from the left one, and node 6 and node 7 are generated after two splits.Figure 7
The transformation for binary trees.For implementation, we input the embeddings of the two children into the Tree-LSTM networks, and the hidden state for each internal node is computed recursively in a bottom-up way. The final output from the AST rootVast is the representation for the code snippet.


## 4.2.1. Lexical Embedding

The body of source code methods contains many core words which describes the functionality. During lexical embedding, At-CodeSM extracts core tokens from the method body, and then embeds them with common word-embedding technology in NLP by treating the source code snippets as plain texts. Finally, the output vector is generated with an attentional LSTM network. We use an independent attention layer at the back of lexical encoder, as the contribution of each token varies in implementing the functionality.The process of lexical embedding is divided into three steps:(1)
Data preprocessing: in order to collect tokens from a Java method, we tokenize the method body and split each token according to a camel case, e.g.,changeToPDF and parseXML. All the duplicated tokens, stop words, and Java keywords are removed as they frequently occur in source code and are not discriminative(2)
Token embedding: letT=token1,token2,token3,⋯ denote the sequence of tokens from source code after step (1). The lexical encoder includes a transformation f, which makes ftoken1,token2,token3,⋯=vtoken1,vtoken2,vtoken3,⋯. By contrast, we implement the transformation f with Word2vec [36].(3)
Source code vector generation: the sequence of token vectorsvtoken1, vtoken2, vtoken3, … are loaded into a standard bi-LSTM. Equation (5) shows the calculation of LSTM, where ht⟶ and ht⃐ denote the forward and backward hidden status at step t, and ht is the concatenation of ht⟶ and ht⃐(5)ht⟶=LSTM⟶vtokentt∈1,n,ht⃐=LSTM⃐vtokentt∈n,1,ht=ht⟶,ht⃐t∈1,n.However, the effect of each token is different in high-level programming languages, e.g., the tokenXML is more important than other tokens in a method dealing with the data storage in XML files. In this paper, we attempt to introduce the self-attention algorithm in the process of lexical embedding. To our knowledge, it is the first time to use attention mechanism in the field of code search. Since our model only includes the encoder, we decide to use the self-attention algorithm. The attention scores can be calculated in many ways as there are various definitions of similarity function. In our paper, equation (6) and equation (7) are used to calculate the attention scores, which are defined in [37]:
(6)αi=exphiTK∑iexphiTK,i∈1,n,(7)vtoken=∑iαihi,where hi denotes the hidden states of the bidirectional LSTM, K denotes a context vector which is initialized randomly. Hu et al. [38] indicates that the inner product of hi and K denotes the contribution of hi to the source code vector. The value of K is updated continuously with other parameters via supervised learning. αi denotes the ith attention score, corresponding with the hidden state hi. vtoken is the representation vector of the given code fragment, which is generated by the weighted sum of all the hidden states. Figure 6 shows the encoding process with the self-attention layer.Figure 6
Lexical embedding with self-attention.


## 4.2.2. Method Name Embedding

The method name is a description of the method functionality, which is composed of a small amount of keywords. Early code search algorithms apply a simple keyword matching between queries and method names. At-CodeSM uses an independent encoder for the embedding of method names, which is simpler than lexical embedding. LetT=name1,name2,name3,⋯ denote the sequence of small names extracted from the method name; after that, the encoder applies a transformation f, which makes fname1,name2,name3,⋯=vname1,vname2,vname3,⋯ with Word2vec technology. Finally, At-CodeSM outputs the representation vector for the method name with a bi-LSTM network, which is shown as follows:
(8)vname=hmt=hmt⟶,hmt⃐,where hmt⟶ and hmt⃐ are the forward and backward hidden states of the bi-LSTM, vname is the final semantic vector of the method name, generated from the concatenation of hmt⟶ and hmt⃐. We tried to implement self-attention in the embedding layer, which had no performance improvement by experiments. Experimental results with various self-attention layers are discussed later.


## 4.2.3. Structural Embedding

We employ javalang to generate an abstract syntax tree (AST) from the source code snippet. The structural encoder then transforms the AST into a high-dimensional vectorVast according to a specific AST traversal algorithm. Recent researchers have proposed several AST embedding algorithms, e.g., RvNN embedding algorithm [6], structure-based algorithm (SBT) [38], tree-LSTM embedding algorithm [35], and tree-CNN embedding algorithms [39]. We implement the structural encoder in At-CodeSM with a special tree-LSTM embedding algorithm, which is used by Hu et al. in their code clone detector CDLH.Tai et al. proposed the tree-based LSTM networks, which is used for the embedding of ASTs in CDLH. Different from standard LSTM networks, neural cells in tree-LSTM contain several forget gates, each of which corresponds with an input. The transition equations for tree-LSTM are the following:(9)i=σWix+∑i=1LUilrl+bi,fl=σWfx+Uflrl+bf,l=1,2,⋯,L,o=σWox+∑i=1LUolrl+bo,u=tanhWux+∑i=1LUulrl+bu,c=i⊙u+∑i=1Lfl⊙cl,r=o⊙tanhc,where x is the embedding of the corresponding token, L denotes the number of children, fl are L forget gates for the children of the AST node, l is the index number for its children, r is a control variable for the output, Wi, Wf, Wo, Wu, Uil, Ufl, Uol, and Uul are weight matrices, and bi, bf, bo, bu are bias vectors. ⊙ denotes the element-wise multiplication, and σ is the sigmoid function.The original ASTs generated from source code might have multiple branches. The classical tree-LSTM networks can be used for the embedding of ASTs with multiple branches. The number of childrenL varies for different AST nodes, which might cause problems in the parameter-sharing of weight matrices. We transform the original ASTs into binary trees, whose nodes contain 0 or 2 children.The transform algorithm for binary trees is divided into two steps:(1)
We split nodes with more than 2 children and generate a new right child together with the old left child as its children, and then put all children except the leftmost as the children of this new node. Repeat the operation in an up-down way until only nodes with 0, 1, and 2 children are left(2)
The nodes with 1 child are merged with its childAfter transformation, the AST is transformed into a full binary tree, nodes of which contain 0 or 2 children. Figure7 describes the transformation of a subtree. The binary tree on the right part of the figure is transformed from the left one, and node 6 and node 7 are generated after two splits.Figure 7
The transformation for binary trees.For implementation, we input the embeddings of the two children into the Tree-LSTM networks, and the hidden state for each internal node is computed recursively in a bottom-up way. The final output from the AST rootVast is the representation for the code snippet.


## 4.3. Query Embedding

In practice, programmers input queries in the repositories for code search. In the training process, the query encoder is updated with target descriptions. Both queries and descriptions are a sequence of natural language words. The lower part of Figure5 describes the structure of the query encoder equation. The process of query embedding is similar to the embedding of method names in source code. The query encoder transforms a sequence of tokens to a sequence of vectors vquery1, vquery2, and vquery3 with word-embedding technology. Finally, the embedding network embeds this sequence of vectors into a query vector using bi-LSTM, which is shown as follows:
(10)vquery=hqt=hqt⟶,hqt⃐,where hqt⟶ and hqt⃐ are the forward and backward hidden states produced by the final layer of LSTM in the query encoder, and vquery is the output vector of the query sentence. We tried to add a self-attention layer in the query encoder, which had no performance improvement by experiments.


## 4.4. Loss Function

In this section, we present the loss function used in At-CodeSM. With carefully trained encoders, and At-CodeSM maps both the source code and queries to different vectors in a unified vector space. The goal of training is as follows: if a code snippet and a description have similar semantics, their embedded vectors should be close to each other. In other words, given an arbitrary code snippet C and an arbitrary description D, we want it to predict a distance with high similarity ifD is an exact description of C, and a little similarity otherwise.We build each training instance as a tripleC,D+,D− for supervised training. For each code snippet C, there is a positive description D+ (correct description) and a negative description D− (false description) randomly chosen from the collection of other D+. In the training process, the search model predicts the cosine similarities of both C,D+ and C,D− pairs and minimizes the ranking loss [40], which is defined as follows:
(11)Lθ=∑C,D+,D−∈Pmax0,ϵ−cosc,d++cosc,d−,where θ indicates the model parameters, P indicates the training corpus, ϵ is a constant margin. c, d+, and d− are the embedded vectors of C, D+, and D‐, respectively. The lost function Lθ encourages the similarity between a code snippet and its correct description to increase, and the similarity between a code snippet and its false description to decrease.


## 4.5. Evaluation Metrics

The objective of code search models is to locate suitable code snippets in code repositories. They often return many results based on similarity algorithms due to the big capacity of codebase. In most cases, developers only focus on the snippets ahead in the result list. In our experiment, we select MRR and NDCG as evaluation metrics which are often used in the field of information retrieval [2, 25, 41, 42], to compare the performances of At-CodeSM and other baselines.


## 5. Experiments

All the experiments are conducted on a server, having a CPU of 16 cores with GPU acceleration. The framework is built on python 3.6 and CUDA 9.0. All the tokens are embedded using word2vec with the Skip-gram algorithm, and we set the embedding size to be 128.The standard LSTM and tree-LSTM networks are used to build the encoders, respectively, where the hidden dimensions are set to be 128. We use SGD to train the model and set Batch_size to 64. We employ the optimizer Adam with a learning rate of 0.005 to train the model.


### 5.1. Dataset Preparation

There are very few public datasets for source code search as it is very difficult to collect exact descriptions for code snippets in the code repositories with massive code data. Especially, the similarity scores between code snippets and queries should be judged by experts, which might cause human bias. Researchers usually construct three datasets for contrast experiments in code search area.(1)
Training corpus: it refers to a dataset of paired code and the natural language description. A common example for the natural language description could be the code snippet’s corresponding docstring. A training corpus is used to update the searching models with supervised labeling. Unsupervised learning models use only code snippets from the training corpus(2)
Search corpus: it refers to a dataset of unique code entries after data preprocessing. Entries are unique to avoid repetition in search results. Code search engines take effort to apply an offline embedding on each code snippet in the search corpus with their code encoders before an online search. Data in the search corpus are not necessarily like the ones in training corpus. Sometimes, code snippets in the search corpus might not be paired with descriptions(3)
Testing corpus (benchmark queries): it refers to a set of evaluation queries and corresponding code snippets used to gauge the performance of trained models. It is defined asqueryi,Ci∣i∈N,Ci≥1. Depending on the size of Ci, testing sets generally fall into two categories in code search:
(a)
Each test case includes a query and a corresponding code snippet, i.e.,∀queryi,Ci=1. These test cases can be automatically collected and evaluated. However, due to the complexity of the code structure, it is possible for search engines in practice to return heterogeneous code snippets with the same functionality, e.g., type 4 clone pairs. These correct cases are probably identified as “false.” The evaluation for code snippets with similar functionality, which are returned by search engines, will lose effectiveness(b)
Each test case includes a query and several corresponding code snippets, i.e.,∃queryi,Ci>1. Test cases of this kind successfully solve the problem of code snippets with similar functionality but different shapes. Based on these test cases, we can evaluate the search engine’s performance with traditional methods in the IR field. However, it is difficult to find all the target snippets related to the same query and judge the relatedness between them. Recent scholars manage to collect similar code snippets according to the same query by various search models, and the relatedness scores are often obtained manually. Hence, the testing corpus does not have a large capacity. We believe that the problem of collecting test cases will be solved with the development of deep learning methods in the field of source code clone detection


### 5.2. Dataset Description

Previous researchers [3, 43] build their own datasets by collecting source code from code repositories, due to the lack of public datasets for code search and summarization. In order to solve the problem, Microsoft Cambridge Research Center and GitHub company released a large-scale dataset CodeSearchNet [44] for machine learning in 2020. The dataset contains more than 6 million code snippets extracted from GitHub, an open-source code repository with massive code data, among which more than 2 million snippets are commented methods. The code extraction is applied according to “popularity” as indicated by the number of stars and forks in GitHub. The dataset authors generate the label for each code snippet by extracting the first sentence from its comment. Table 1 shows the data in CodeSearchNet, which are grouped by programming language. These labeled data on the second column of Table 1 can be used for code search and code summarization tasks. We conduct contrast experiments on the Java subset.Table 1
Data statistics in CodeSearchNet.
LanguageLabeled data(functions with descriptions)AllGo347789726768Java5429911569889Javascript1579881857835PHP717313977821Python5035021156085Ruby57393164048The dataset for java in CodeSearchNet is split in 80-10-10 train/valid/test proportions after data preprocessing. We use the labeled java dataset as the search corpus, the original training set as our training corpus. Figure8 shows the partition of Java dataset in our experiments.Figure 8
Java dataset.At-CodeSM and other baseline models are evaluated with two different testing sets, which are described in Section5.1:
(1)
Test A: we apply an automatic testing on the Java testing set from CodeSearchNet. Table1 shows that the Java testing set contains 26909 method-description pairs. The experimental results are unbiased due to the exclusive partition of training and testing sets [45]. We calculate the MRR values of all the searching models in Test A(2)
Test B: expert scoring are employed in the evaluation of ranking models in order to cope with heterogeneous code snippets with similar functionality. Experts score each item in the result list by the relatedness between the snippet and query. Traditional IR-based methods such as NDCG are then applied for evaluation depending on the matching scores. Test B is rarely used by researchers due to the luxury manual work. Hence, the authors of CodeSearchNet construct a new dataset called CodeSearchNet Challenge for code evaluation. It is defined asDi,Cin,Rin∣i=1,2,⋯,99;n=1,2,⋯,100, where Di denotes the description or query, Cin is a corresponding code snippet according to Di, Rin is a matching score between Cin and Di given by expertsCodeSearchNet Challenge contains 99 natural languages queries paired with likely results for each of six programming languages. All the queries are obtained from the famous search engine Bing with high click-through rates, each of which contains around 10 corresponding code snippets, returned by baseline code search models. Every query-result pair is labeled by a human expert, indicating the relevance of the result for the query. Table2 shows some examples in CodeSearchNet Challenge. Table 3 shows the criterion of relevance followed by experts.Table 2
Examples in CodeSearchNet challenge.
QueryThe path of the returned codeScoreConnect to sqlhttps://github.com/druid/.../DruidPool.java#L31-L510Connect to sqlhttps://github.com/Teddy-Zhu/...jdbc/JDBCPool.java#L97-L1051Convert html to pdfhttps://github.com/Coveros/.../Reporter.java#L741-L7522Convert json to csvhttps://github.com/GCRC/nunaliit/.../ExportRecordsCSV.java#L118-L1500Convert string to numberhttps://github.com/b3log/.../JSONObject.java#L2074-L21263Table 3
Criterion of relevance.
DescriptionScoreCriteriaTotally irrelevant0I would never want to see this for this query.Weak match1That is not exactly what I was looking for, but there are some useful elements I would use (e.g., APIs and code structure) and can form the basis of a new query or exploration towards solving my query.Strong match2This is more or less what I was looking for. I would use the code in here as a backbone for my purpose, but I will not necessarily copy-paste it or use this library.Exact match3It is exactly what I was looking for. I would copy-paste the code and make minor adaptions or will use this functionality of the library in my code.


### 5.3. Contrast Experiments

We compare At-CodeSM with three baseline models in the field of source code search by different experiments on CodeSearchNet dataset:(1)
NBoW model (Neural Bag of Words) [45]: as a classical code search model, NBoW has already been implemented in CodeSearchNet. It applies code embedding with common NLP methods by treating source code as the plain text. The tokens extracted from the code snippet are embedded with a transformation matrix, which is updated by training. The representation vector for the code snippet is generated by a max-pooling function over all the token vectors. NBoW implements the query embedding with the same embedding algorithm. Finally, NBoW returns the most related code snippets in the vector space as search results with a similarity algorithm between vectors(2)
NCS model (Neural Code Search) [3]: built in Facebook, NCS generates the representation vector of the code with a combination of token-level embedding with fastText [45], which is similar to Word2vec, and conventional IR techniques, such as TF-IDF. The encoders of NCS are not implemented with conventional deep neural networks nor supervised training(3)
CODEnn model [25]: all the encoders in CODEnn are implemented with complicated neutral networks, compared to former baselines. CODEnn includes three encoders for method names, APIs, and method body, respectively. The encoders for method names and APIs are implemented with a standard LSTM, while the lexical encoder uses a simple MLP for the embedding of method body. Finally, CODEnn outputs a unified vector by merging three subvectors through a MLP layer. For query embedding, a bi-LSTM network is employed to process the sequence of token vectors extracted from the query sentence. CODEnn returns the result list according to the cosine similarity algorithm. Unlike NCS, authors of CODEnn update their model with supervised learning. The labeled data are collected from code repositories by themselves. The authors provide the framework of CODEnn in GitHub, which we use for our contrast experiments on CodeSearchNetFigure9 shows the experimental results of At-CodeSM and baseline models on the test corpus of CodeSearchNet. The figures above the bars are the MRR values of search models, which are the rank of the first hit results. We see that the search performance of CODEnn and At-CodeSM, which is evaluated by MRR, is much higher than the former models.Figure 9
The MRRs for various models on CodeSearchNet.Figure10 shows the experimental results of At-CodeSM and baseline models on CodeSearchNet Challenge. We see that At-biLSTM and CODEnn once again take the lead in the search performance evaluated by NDCG.Figure 10
The NDCGs for various models on CodeSearchNet.


## 5.1. Dataset Preparation

There are very few public datasets for source code search as it is very difficult to collect exact descriptions for code snippets in the code repositories with massive code data. Especially, the similarity scores between code snippets and queries should be judged by experts, which might cause human bias. Researchers usually construct three datasets for contrast experiments in code search area.(1)
Training corpus: it refers to a dataset of paired code and the natural language description. A common example for the natural language description could be the code snippet’s corresponding docstring. A training corpus is used to update the searching models with supervised labeling. Unsupervised learning models use only code snippets from the training corpus(2)
Search corpus: it refers to a dataset of unique code entries after data preprocessing. Entries are unique to avoid repetition in search results. Code search engines take effort to apply an offline embedding on each code snippet in the search corpus with their code encoders before an online search. Data in the search corpus are not necessarily like the ones in training corpus. Sometimes, code snippets in the search corpus might not be paired with descriptions(3)
Testing corpus (benchmark queries): it refers to a set of evaluation queries and corresponding code snippets used to gauge the performance of trained models. It is defined asqueryi,Ci∣i∈N,Ci≥1. Depending on the size of Ci, testing sets generally fall into two categories in code search:
(a)
Each test case includes a query and a corresponding code snippet, i.e.,∀queryi,Ci=1. These test cases can be automatically collected and evaluated. However, due to the complexity of the code structure, it is possible for search engines in practice to return heterogeneous code snippets with the same functionality, e.g., type 4 clone pairs. These correct cases are probably identified as “false.” The evaluation for code snippets with similar functionality, which are returned by search engines, will lose effectiveness(b)
Each test case includes a query and several corresponding code snippets, i.e.,∃queryi,Ci>1. Test cases of this kind successfully solve the problem of code snippets with similar functionality but different shapes. Based on these test cases, we can evaluate the search engine’s performance with traditional methods in the IR field. However, it is difficult to find all the target snippets related to the same query and judge the relatedness between them. Recent scholars manage to collect similar code snippets according to the same query by various search models, and the relatedness scores are often obtained manually. Hence, the testing corpus does not have a large capacity. We believe that the problem of collecting test cases will be solved with the development of deep learning methods in the field of source code clone detection


## 5.2. Dataset Description

Previous researchers [3, 43] build their own datasets by collecting source code from code repositories, due to the lack of public datasets for code search and summarization. In order to solve the problem, Microsoft Cambridge Research Center and GitHub company released a large-scale dataset CodeSearchNet [44] for machine learning in 2020. The dataset contains more than 6 million code snippets extracted from GitHub, an open-source code repository with massive code data, among which more than 2 million snippets are commented methods. The code extraction is applied according to “popularity” as indicated by the number of stars and forks in GitHub. The dataset authors generate the label for each code snippet by extracting the first sentence from its comment. Table 1 shows the data in CodeSearchNet, which are grouped by programming language. These labeled data on the second column of Table 1 can be used for code search and code summarization tasks. We conduct contrast experiments on the Java subset.Table 1
Data statistics in CodeSearchNet.
LanguageLabeled data(functions with descriptions)AllGo347789726768Java5429911569889Javascript1579881857835PHP717313977821Python5035021156085Ruby57393164048The dataset for java in CodeSearchNet is split in 80-10-10 train/valid/test proportions after data preprocessing. We use the labeled java dataset as the search corpus, the original training set as our training corpus. Figure8 shows the partition of Java dataset in our experiments.Figure 8
Java dataset.At-CodeSM and other baseline models are evaluated with two different testing sets, which are described in Section5.1:
(1)
Test A: we apply an automatic testing on the Java testing set from CodeSearchNet. Table1 shows that the Java testing set contains 26909 method-description pairs. The experimental results are unbiased due to the exclusive partition of training and testing sets [45]. We calculate the MRR values of all the searching models in Test A(2)
Test B: expert scoring are employed in the evaluation of ranking models in order to cope with heterogeneous code snippets with similar functionality. Experts score each item in the result list by the relatedness between the snippet and query. Traditional IR-based methods such as NDCG are then applied for evaluation depending on the matching scores. Test B is rarely used by researchers due to the luxury manual work. Hence, the authors of CodeSearchNet construct a new dataset called CodeSearchNet Challenge for code evaluation. It is defined asDi,Cin,Rin∣i=1,2,⋯,99;n=1,2,⋯,100, where Di denotes the description or query, Cin is a corresponding code snippet according to Di, Rin is a matching score between Cin and Di given by expertsCodeSearchNet Challenge contains 99 natural languages queries paired with likely results for each of six programming languages. All the queries are obtained from the famous search engine Bing with high click-through rates, each of which contains around 10 corresponding code snippets, returned by baseline code search models. Every query-result pair is labeled by a human expert, indicating the relevance of the result for the query. Table2 shows some examples in CodeSearchNet Challenge. Table 3 shows the criterion of relevance followed by experts.Table 2
Examples in CodeSearchNet challenge.
QueryThe path of the returned codeScoreConnect to sqlhttps://github.com/druid/.../DruidPool.java#L31-L510Connect to sqlhttps://github.com/Teddy-Zhu/...jdbc/JDBCPool.java#L97-L1051Convert html to pdfhttps://github.com/Coveros/.../Reporter.java#L741-L7522Convert json to csvhttps://github.com/GCRC/nunaliit/.../ExportRecordsCSV.java#L118-L1500Convert string to numberhttps://github.com/b3log/.../JSONObject.java#L2074-L21263Table 3
Criterion of relevance.
DescriptionScoreCriteriaTotally irrelevant0I would never want to see this for this query.Weak match1That is not exactly what I was looking for, but there are some useful elements I would use (e.g., APIs and code structure) and can form the basis of a new query or exploration towards solving my query.Strong match2This is more or less what I was looking for. I would use the code in here as a backbone for my purpose, but I will not necessarily copy-paste it or use this library.Exact match3It is exactly what I was looking for. I would copy-paste the code and make minor adaptions or will use this functionality of the library in my code.


## 5.3. Contrast Experiments

We compare At-CodeSM with three baseline models in the field of source code search by different experiments on CodeSearchNet dataset:(1)
NBoW model (Neural Bag of Words) [45]: as a classical code search model, NBoW has already been implemented in CodeSearchNet. It applies code embedding with common NLP methods by treating source code as the plain text. The tokens extracted from the code snippet are embedded with a transformation matrix, which is updated by training. The representation vector for the code snippet is generated by a max-pooling function over all the token vectors. NBoW implements the query embedding with the same embedding algorithm. Finally, NBoW returns the most related code snippets in the vector space as search results with a similarity algorithm between vectors(2)
NCS model (Neural Code Search) [3]: built in Facebook, NCS generates the representation vector of the code with a combination of token-level embedding with fastText [45], which is similar to Word2vec, and conventional IR techniques, such as TF-IDF. The encoders of NCS are not implemented with conventional deep neural networks nor supervised training(3)
CODEnn model [25]: all the encoders in CODEnn are implemented with complicated neutral networks, compared to former baselines. CODEnn includes three encoders for method names, APIs, and method body, respectively. The encoders for method names and APIs are implemented with a standard LSTM, while the lexical encoder uses a simple MLP for the embedding of method body. Finally, CODEnn outputs a unified vector by merging three subvectors through a MLP layer. For query embedding, a bi-LSTM network is employed to process the sequence of token vectors extracted from the query sentence. CODEnn returns the result list according to the cosine similarity algorithm. Unlike NCS, authors of CODEnn update their model with supervised learning. The labeled data are collected from code repositories by themselves. The authors provide the framework of CODEnn in GitHub, which we use for our contrast experiments on CodeSearchNetFigure9 shows the experimental results of At-CodeSM and baseline models on the test corpus of CodeSearchNet. The figures above the bars are the MRR values of search models, which are the rank of the first hit results. We see that the search performance of CODEnn and At-CodeSM, which is evaluated by MRR, is much higher than the former models.Figure 9
The MRRs for various models on CodeSearchNet.Figure10 shows the experimental results of At-CodeSM and baseline models on CodeSearchNet Challenge. We see that At-biLSTM and CODEnn once again take the lead in the search performance evaluated by NDCG.Figure 10
The NDCGs for various models on CodeSearchNet.


## 6. Discussion

In this chapter, we take a detailed discussion about the performance of At-CodeSM and other code search models based on ablation tests. We try to find the key components which affect searching accuracy by further experiments.


### 6.1. Comparing with Baseline Models

Compared to other baseline models, we conclude from the contrast experiments that At-CodeSM model outperforms the other models in both MRR values, which are generated from automated testing, and NDCG values which are generated with manual evaluation. NCS model encodes the source code with common NLP methods. The unsupervised learning has a negative influence on the searching performance, although unlabeled data are easily collected. Figures9 and 10 show the poor performance of NCS, of which the searching accuracy and ranking in the result list are lower than codeNN and At-CodeSM. The performance of NBoW is not ideal, because its code encoder ignores both the sequential information of tokens and structural information in source code.CodeNN is a successful code search model trained with supervised learning. It introduces deep learning technology into the field of code search. It makes great achievement in source code search by using a combination of method names, tokens, and APIs during code embedding. Inspired by codeNN, we propose the At-CodeSM model. The performance of our model is slightly better than codeNN. We guess it is the introduction of self-attention and tree-LSTM networks that improve the searching performance. The core words are strengthened with a self-attention mechanism, which highlight the behavior and logic hidden in function bodies. The AST encoder in our model, which is implemented with tree-LSTM networks, further extracts the structural features hidden in source code, compared to API revocations in codeNN. Both models outperform NCS and NBoW, which only extract lexical features.


### 6.2. AST Traversal Algorithms

A traversal algorithm for ASTs is introduced in At-CodeSM. The corresponding AST maintains the structural feature hidden in the source code snippet. We use a tree-based LSTM network in the embedding of AST. In the field of software engineering, many AST-based traversal algorithms are proposed, with which corresponding ASTs are transformed to representation vectors.In order to investigate the effects of the embedding algorithms for ASTs, we conduct contrast experiments on the same dataset, and the results are shown in Table4. Models in the table are the same except that the structural encoders are implemented with three different traversal algorithms for AST embedding. Model 1 embeds the source code with a combination of the lexical encoder and method name encoder, which ignores the structural embedding completely. Model 2 implements the AST embedding with SBT, which convert the ASTs into special formatted sequences of nodes. A bi-LSTM network is used to generate the representation vector according to the sequence of nodes. The authors of SBT claim the transformation keeps the structural information. Model 3 is an At-CodeSM model, whose structural encoder is implemented with a Tree-based LSTM. We compare both the MRR and NDCG values in Table 4.Table 4
Search performance depending on various AST traversal algorithms.
DescriptionsMRRNDCGWithout AST (model 1)46.45%0.4149SBT (model 2)51.03%0.4683Tree-LSTM (model 3)53.37%0.4918Through the above experimental results, it can be observed that model 1 shows the poorest performance, as it fails to extract the structural information hidden in the AST. The search performance of model 2 is slightly inferior to that of model 3. We guess the one-dimensional sequence generated from SBT might lose the structural information of some tree nodes.


### 6.3. Self-Attention Algorithms

The use of a self-attention layer, which stresses the effect of key words in source clone embedding, is a great innovation in our paper. In this section, we investigate the effect of self-attention layers on the search performance by contrast experiments. Figure11 shows the experimental data we collect from five code search models, each of which is built on the basic At-CodeSM model. The detailed structure of At-CodeSM is presented in Section 4.2, including three encoders for method names, code tokens, and the code structure, respectively. The structures of these models are the same except the self-attention layers:
(1)
NoAtt model: it does not include the self-attention layer at all. The vectors generated from three independent encoders are merged via a fusion layer(2)
MNAtt model: the self-attention layer is placed at the back of the name encoder, which assigns weights to the tokens extracted from method names(3)
StrAtt model: the self-attention mechanism is used during structural embedding. Each node vector is assigned with an attentional weight(4)
At-CodeSM model: it is proposed in Section4. The self-attention layer is used during lexical encoding(5)
At-CodeSM2 model: it is a variant of the original At-CodeSM model, where the attention scores are computed according to equation (12). Many formulas are proposed to calculate the attention scores in the NLP field. Equation (12) is a simple implementation for the self-attention algorithm without other model parameters. We want to investigate the effects of various attention algorithms by contrast experimentsFigure 11
Search models with various self-attention layers.Figure11 shows that the self-attention layer greatly improves the search performance of the model. The contributions of different words are various in high-level programming languages. The core words implementing the logical functionality are more important than the normal ones. The encoder with self-attention layers can strengthen the effectiveness of core words and weaken the impact of common words such as variable declaration. Both the MRR and NDCG values of NoAtt substantially fall behind the other models. The performance improvement in MNAtt is not obvious. We guess that all the words extracted from the method name are important, the contributions of which are similar. The performance of StrAtt is close to that of At-CodeSM. However, it takes more time to train StrAtt due to the complicated structure encoder with attention.We find that the search performance of At-CodeSM and At-CodeSM2 are slightly different. The performance of At-CodeSM is higher when involving an independent parameterK in equation (7). We suspect that the search performance of the second model is slightly affected due to the lack of some adjustments to the input when calculating the attention scores.
(12)αi=exphiThi∑iexphiThi.


## 6.1. Comparing with Baseline Models

Compared to other baseline models, we conclude from the contrast experiments that At-CodeSM model outperforms the other models in both MRR values, which are generated from automated testing, and NDCG values which are generated with manual evaluation. NCS model encodes the source code with common NLP methods. The unsupervised learning has a negative influence on the searching performance, although unlabeled data are easily collected. Figures9 and 10 show the poor performance of NCS, of which the searching accuracy and ranking in the result list are lower than codeNN and At-CodeSM. The performance of NBoW is not ideal, because its code encoder ignores both the sequential information of tokens and structural information in source code.CodeNN is a successful code search model trained with supervised learning. It introduces deep learning technology into the field of code search. It makes great achievement in source code search by using a combination of method names, tokens, and APIs during code embedding. Inspired by codeNN, we propose the At-CodeSM model. The performance of our model is slightly better than codeNN. We guess it is the introduction of self-attention and tree-LSTM networks that improve the searching performance. The core words are strengthened with a self-attention mechanism, which highlight the behavior and logic hidden in function bodies. The AST encoder in our model, which is implemented with tree-LSTM networks, further extracts the structural features hidden in source code, compared to API revocations in codeNN. Both models outperform NCS and NBoW, which only extract lexical features.


## 6.2. AST Traversal Algorithms

A traversal algorithm for ASTs is introduced in At-CodeSM. The corresponding AST maintains the structural feature hidden in the source code snippet. We use a tree-based LSTM network in the embedding of AST. In the field of software engineering, many AST-based traversal algorithms are proposed, with which corresponding ASTs are transformed to representation vectors.In order to investigate the effects of the embedding algorithms for ASTs, we conduct contrast experiments on the same dataset, and the results are shown in Table4. Models in the table are the same except that the structural encoders are implemented with three different traversal algorithms for AST embedding. Model 1 embeds the source code with a combination of the lexical encoder and method name encoder, which ignores the structural embedding completely. Model 2 implements the AST embedding with SBT, which convert the ASTs into special formatted sequences of nodes. A bi-LSTM network is used to generate the representation vector according to the sequence of nodes. The authors of SBT claim the transformation keeps the structural information. Model 3 is an At-CodeSM model, whose structural encoder is implemented with a Tree-based LSTM. We compare both the MRR and NDCG values in Table 4.Table 4
Search performance depending on various AST traversal algorithms.
DescriptionsMRRNDCGWithout AST (model 1)46.45%0.4149SBT (model 2)51.03%0.4683Tree-LSTM (model 3)53.37%0.4918Through the above experimental results, it can be observed that model 1 shows the poorest performance, as it fails to extract the structural information hidden in the AST. The search performance of model 2 is slightly inferior to that of model 3. We guess the one-dimensional sequence generated from SBT might lose the structural information of some tree nodes.


## 6.3. Self-Attention Algorithms

The use of a self-attention layer, which stresses the effect of key words in source clone embedding, is a great innovation in our paper. In this section, we investigate the effect of self-attention layers on the search performance by contrast experiments. Figure11 shows the experimental data we collect from five code search models, each of which is built on the basic At-CodeSM model. The detailed structure of At-CodeSM is presented in Section 4.2, including three encoders for method names, code tokens, and the code structure, respectively. The structures of these models are the same except the self-attention layers:
(1)
NoAtt model: it does not include the self-attention layer at all. The vectors generated from three independent encoders are merged via a fusion layer(2)
MNAtt model: the self-attention layer is placed at the back of the name encoder, which assigns weights to the tokens extracted from method names(3)
StrAtt model: the self-attention mechanism is used during structural embedding. Each node vector is assigned with an attentional weight(4)
At-CodeSM model: it is proposed in Section4. The self-attention layer is used during lexical encoding(5)
At-CodeSM2 model: it is a variant of the original At-CodeSM model, where the attention scores are computed according to equation (12). Many formulas are proposed to calculate the attention scores in the NLP field. Equation (12) is a simple implementation for the self-attention algorithm without other model parameters. We want to investigate the effects of various attention algorithms by contrast experimentsFigure 11
Search models with various self-attention layers.Figure11 shows that the self-attention layer greatly improves the search performance of the model. The contributions of different words are various in high-level programming languages. The core words implementing the logical functionality are more important than the normal ones. The encoder with self-attention layers can strengthen the effectiveness of core words and weaken the impact of common words such as variable declaration. Both the MRR and NDCG values of NoAtt substantially fall behind the other models. The performance improvement in MNAtt is not obvious. We guess that all the words extracted from the method name are important, the contributions of which are similar. The performance of StrAtt is close to that of At-CodeSM. However, it takes more time to train StrAtt due to the complicated structure encoder with attention.We find that the search performance of At-CodeSM and At-CodeSM2 are slightly different. The performance of At-CodeSM is higher when involving an independent parameterK in equation (7). We suspect that the search performance of the second model is slightly affected due to the lack of some adjustments to the input when calculating the attention scores.
(12)αi=exphiThi∑iexphiThi.


## 7. Conclusion

In this paper, we have presented a novel source code-searching model At-biLSTM based on deep learning with a self-attention mechanism. It contains a query encoder and three independent source code encoders, which successfully capture both the lexical and syntactic information of the code during embedding. The searching results are returned by computing the cosine similarity between the query vector and code vectors in a unified high-dimensional vector space. Contrast experiments show that our model significantly outperforms most of the existing code search models.We will focus on three aspects in the future. First, the At-CodeSM should be used in large-scale codebase. At present, both the algorithms for clone search and data collected in the labs cannot be applied in the real world. We will study and improve the searching performance of our model in industry. Second, we will focus on the code-embedding technology. In recent years, many pretrained models for embedding have made great achievements in NLP applications. We plan to rebuild our code encoders with BERT, which is the most popular pretrained model in the world. Third, we will make a study of query expansion technology. We plan to add a special component for the expansion of keywords in queries, which might improve the searching performance.The threats to the validity stem from the datasets used in our experiments. The models are trained on a new dataset called CodeSearchNet which was created by Microsoft in 2020, as the labeled data for code search are difficult to accumulate. The labeled data in CodeSearchNet are collected in the same way as in other datasets. The first sentence of the method comment is identified as the query description, which is far away from the real query in practice. The matching scores for 99 queries are all given manually. The introduction of human evaluation might have a negative influence on searching results when we evaluate the models’ NDCG values.



---

*Source: 9990988-2021-07-01.xml*