Domain confrontation cross-project software defect prediction based on context semantics
By adopting the domain adversarial method of contextual semantics in cross-project software defect prediction, using abstract syntax trees and bidirectional gated recurrent units to extract features, and reducing feature distribution differences through domain adversarial learning, the limitations of complex feature processing in traditional methods are solved. , improving the accuracy and generalization ability of software defect prediction.
Patent Information
- Application Number
- CN202410648437.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-23
- Publication Date
- 2024-08-30
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Traditional cross-project software defect prediction methods are difficult to accurately identify complex context and semantic details, resulting in low prediction accuracy. Moreover, transfer learning methods based on statistical criteria have greater limitations when processing complex features. The loss function design is complex and difficult to adapt to different projects. Code characteristics.
A domain adversarial cross-project software defect prediction method based on context semantics is adopted, tag vectors are extracted through abstract syntax trees, continuous word bag models and bidirectional gated recurrent units are used for feature extraction, and domain adversarial learning is used to reduce the gap between source projects and target projects. feature distribution differences to achieve defect feature transfer.
It improves the accuracy and generalization ability of cross-project defect prediction, can better characterize the contextual semantic information of the source code, and effectively transfers defect features between the source project and the target project, improving the prediction accuracy.
Smart Images

Figure CN118567651A
Abstract
Description
[Technical field]
[0001] The present invention relates to the technical field of cross-project software defect prediction, in particular to the technical field of cross-project software defect prediction based on context semantics domain adversarial. [Background technology]
[0002] In today's era of rapid development of information technology, software has become an indispensable part of daily life. However, with the continuous expansion of software scale, software quality issues have become more prominent, and software defects have become a major hidden danger. Software defects are problems, errors, or hidden functional defects in computer software or programs that destroy the ability to operate normally. Early detection of errors in software can save costs and improve software quality for life and production. To this end, researchers have proposed software defect prediction (SDP) technology. However, in the actual development process, the software project that needs to be predicted may be a brand new project or a project with less historical data. Therefore, cross-project defect prediction (CPDP) is proposed to make up for the lack of historical data by borrowing data from other mature projects.
[0003] Traditional CPDP methods usually include two stages: extracting features from source files and building machine learning classifiers. Some previous studies have focused on manually designing new or different features and combining certain features, such as Halstead features. [8], MaCabe features, and CK features. Using these features, researchers have established a feasible model to effectively distinguish defective files from non-defective files. Although these indicators have solved the prediction problem to a certain extent, the manually extracted features actually only achieve the fitting of the indicators and the number of defects, and can only represent the correlation between the indicators and the defects. It is difficult to capture complex information from the source code, especially the context and semantic details in the method. Therefore, when the defect appears inside the method, it is almost impossible for manual features to accurately identify the defect. This limitation is an important factor affecting the prediction accuracy of CPDP. To address this problem, researchers proposed to introduce the technology in the field of Natural Language Processing (NLP) into CPDP research to learn meaningful context and semantic features from the source code. In order not to lose the semantic and contextual information of the code during training and prediction, the semantic features in the code can usually be represented by technologies such as Abstract Syntax Tree (AST), the nodes on the tree are regarded as words in the text, and the Continuous Bag of Words (CBOW) model in the word embedding technology is used to preprocess the words and extract contextual semantic features. In addition, many studies [14-17]It is proposed to extract abstract features hidden in semantics through deep learning (DL) to improve prediction performance. Generally speaking, DL-based methods extract features from the AST of the program by using a deep learning network. Inspired by this, this paper uses a bidirectional gated recurrent unit and three fully connected layers as feature extractors. However, due to different code characteristics, different projects have significantly different distributions. Due to the different distributions, the classifier trained by the source project cannot adapt to the target project. To solve this problem, some studies have tried to use transfer learning methods. Methods based on statistical criteria transform data from sample space to feature space under the constraint of minimizing statistical metrics so that the distribution difference between the two domains is minimized in the feature space. Such methods can be successful in most cases, but there are some defects. For example: if the samples of the source domain and the target domain are mapped to a specific point in the feature space, there is no difference between the distribution of the source domain and the target domain. This mapping is meaningless. Although the distribution difference between the source domain and the target domain in the feature space is successfully reduced, the excessive pursuit of similarity may lead to the loss of key information of the original data. All samples are mapped to a specific point, and the network will not be able to distinguish them, so the classification task cannot be performed. However, the statistical criterion-based method uses the distribution distance between the source domain and the target domain as the loss function and uses a deep neural network to extract domain-invariant features. Although this type of method has achieved many research results, it has great limitations when dealing with complex features in experiments, and the loss function design is too complicated. From the perspective of extracting domain-invariant features, adversarial learning is a direction with greater development potential. On the one hand, compared with designing a complex distance between domains, adversarial networks are easier to implement. On the other hand, adversarial learning avoids artificially designed distances. The network adaptively learns what should be aligned between domains and to what extent during the training process, which usually achieves better results. [Summary of the invention]
[0004] The purpose of the present invention is to solve the problems in the prior art and propose a domain-adversarial cross-project software defect prediction based on contextual semantics. It can extract tag vectors from the abstract syntax trees of the source code and the target code, build a word vector table through a continuous bag-of-words model, use a bidirectional gated recurrent unit and a fully connected layer for feature extraction, and finally use domain adversarial learning to achieve defect feature transfer between the source project and the target project, thereby improving the cross-project defect prediction capability.
[0005] To achieve the above object, the present invention proposes a domain-adversarial cross-project software defect prediction based on context semantics, comprising the following steps:
[0006] S1. Program code parsing: Use AST Javalang to parse the grammatical structure from the source code to get an abstract syntax tree, from which nodes prone to defects are selected;
[0007] S2. Word vector generation: Convert the node sequence traversed from the AST into a numerical vector, regard the node of the abstract syntax tree as a word in the natural language, generate the corresponding word vector to transform the node sequence, and use the hierarchical softmax-based CBOW model to capture the semantic relationship between context words;
[0008] S3. Migration model establishment and defect prediction: After the CBOW model encoding is converted into a numerical vector, a BiGRU layer is constructed to capture long sequence dependencies. The CBOW model learns the semantic and structural information of past tokens and future tokens from both the forward and backward directions. The BiGRU layer contains two GRU layers. A GRU layer contains an update gate Z and a reset gate R. The former is used to update the hidden state, and the latter is used to determine which states should be forgotten. Two hidden state sequences are obtained through two GRU layers, including forward and reverse hidden states. Finally, the two are concatenated to obtain the final hidden sequence. The hidden state of the BiGRU layer is then linearly transformed using The three fully connected layers learn higher-level and abstract feature representations, fuse the generated semantic features with the hand-crafted features, and the fused features are input into the classifier for defect classification, completing the training of the source feature extractor and classifier. The parameters of the source feature extractor are shared with the target feature extractor, and feature extraction is performed. The feature vectors generated by the source feature extractor are regarded as real samples, and the feature vectors generated by the target feature extractor are regarded as false samples. A new discriminator is constructed to distinguish between real and false samples, align the feature distributions between the source project and the target project, and use the model to predict whether there are defects in the new project instance, thereby realizing the cross-project defect prediction task.
[0009] Preferably, in step S1, the AST is simplified to consist of four types of nodes: method call nodes and instance creation nodes, declaration nodes, control flow nodes and other necessary nodes. The method call nodes and instance creation nodes capture the function call relationship of the program, understand the modular structure in the code, identify functional dependencies, understand the interaction between different parts, and understand the overall structure of the program; the declaration nodes capture the structure of the program and the scope of the variables, and by analyzing the declaration nodes, identify the variables, functions and classes defined in the program, and understand the namespace and structure of the program. The control flow nodes provide information on the program execution path, understand the logical structure and loop structure of the program, and the other necessary nodes provide access to additional information in the program, including constant values and comment content, to provide a more comprehensive understanding of the program.
[0010] Preferably, in step S2, the context information of a specific word is used as input, and its distributed representation is output. The CBOW model consists of an input layer, a projection layer, and an output layer. First, a word with a fixed size window around a specific word is selected as the context, and it is converted into a uniquely hot encoded word vector as the input of the input layer. Secondly, the word vector of the input layer is multiplied by the input weight matrix, and the word vector of the projection layer is entered into the projection layer after cumulative averaging. Then, the word vector of the projection layer is multiplied by the output weight matrix, and normalized to the output layer with hierarchical Softmax to obtain the distributed representation of the specific word. The input layer consists of the context of the known word w, i.e., k words before and after Content(w). Where k is the size of the context window, and the word embedding vectors in the context window are cumulatively summed to obtain the projection layer:
[0011]
[0012] in It is the vocabulary w i The word embedding vector of
[0013] The output layer corresponds to a Huffman tree, which is constructed with words that appear in the corpus as leaf nodes and the number of times each word appears in the corpus as the weight. There is a separate path from the root node to the leaf node, and each non-leaf node corresponds to a vector θ, θ∈R h , h is the length of the vector, which has the same dimension as the word vector, gives the initial vector randomly to the words in the corpus, and provides the context projection X of the center word w w As input, formulas (2) and (3) represent the conditional probability formula for predicting word w as follows:
[0014]
[0015]
[0016] Among them, p w is the Huffman path from the root node to the leaf node corresponding to w, then l w is the path p w The number of nodes contained in It means p w Each node in is the Huffman code of word w, Represents the path p w The j-th node code on Represents the path p w The parameter vector of the non-leaf node above, m is the vector dimension, and formula (4) shows how to capture the context of word t and calculate the projection value, (p is the parent word of t, C is all the subwords of t).
[0017]
[0018] Preferably, in step S3, after the embedding layer, the source code is represented by a matrix EM=[em1, em2, ..., em n ]∈R n×d Represents, where d represents the word embedding dimension, within a given time t, the current embedding state em t and the previous hidden state H t-1 It is the input of update gate Z and reset gate R, and the output H of GRU layer t It can be calculated according to formula (5-8):
[0019]
[0020]
[0021]
[0022]
[0023] Among them, σ(·) and tanh(·) are activation functions, representing element-wise product, W em ∈R d×h and W H ∈R h×h is the weight parameter to be learned, b (z) 、b (R) and b are bias units,
[0024] BiGRU contains two GRU layers, which will get two hidden state sequences, including the forward and reverse hidden states. Finally, the two are concatenated to get the final hidden sequence, as shown in formulas (9) to (11).
[0025]
[0026]
[0027]
[0028] Then, the hidden state of BiGRU is linearly transformed, and a three-layer fully connected layer is used to learn higher-level and abstract feature representations to improve the expressiveness of the model. At the same time, the structure of the three-layer fully connected layer provides more parameters that can be adjusted, making the model more flexible. Through layer-by-layer learning, more robust features are learned, and the model has better generalization ability for unseen data, as shown in formulas (12) to (14).
[0029] z1=ReLU(W1·ht +b1) (12)
[0030] z2=ReLU(W2·z1+b2) (13)
[0031]
[0032] Among them, ReLU is the rectified linear unit activation function, W1, W2, W3 are the weight matrices of the three fully connected layers, b1, b2, b3 are the corresponding bias vectors, and the forward propagation of the entire model can be expressed as:
[0033]
[0034] Subsequently, the generated semantic features are fused with the handcrafted features, and the automatically extracted semantic information and the handcrafted features are comprehensively utilized to represent the input data more comprehensively and effectively, which can be understood as the following formula:
[0035] F concat =[F fc , F static ] (16)
[0036] Among them, F fc is the semantic feature obtained after BiGRU and three-layer full connection, F static It is a transferable feature obtained by TCA from static manual features.
[0037] Beneficial effects of the present invention: The present invention can fully characterize the contextual semantic information of the source code and perform better defect transfer between the source project and the target project. The main process of the model is as follows: First, a simplified AST is used to represent the code of each extracted program module, and then the token vector is traversed through the AST, and the word embedding is performed using the CBOW algorithm based on hierarchical softmax, which helps to fully capture the semantic relationship between nodes when the token vector is converted into a digital vector. Then, the numerical vector is input into the neural network model to extract the semantic and contextual features of the source project and the target project, and the data distribution differences between cross-projects are reduced by means of adversarial learning. Finally, the existence of defects is determined by the RF classifier. The present invention can extract tag vectors from the abstract syntax trees of the source code and the target code, construct a word vector table through a continuous bag of words model, use a bidirectional gated recurrent unit and a fully connected layer for feature extraction, and finally use the means of domain adversarial learning to achieve defect feature transfer between the source project and the target project, thereby improving the cross-project defect prediction capability.
[0038] The features and advantages of the present invention will be described in detail through embodiments in conjunction with the accompanying drawings.
Brief Description of the Drawings
[0039] Figure 1 It is a framework diagram of the ADA-GRU model for domain-adversarial cross-project software defect prediction based on contextual semantics of the present invention;
[0040] Figure 2 The present invention converts the source code of the domain-based cross-project software defect prediction based on context semantics into a simplified AST graph;
[0041] Figure 3 It is a hierarchical softmax-based CBOW model diagram of the present invention for domain-adversarial cross-project software defect prediction based on contextual semantics;
[0042] Figure 4 It is a diagram of the ADA migration model for domain-adversarial cross-project software defect prediction based on contextual semantics of the present invention;
[0043] Figure 5 It is a ranking diagram of the Scott-Knott effect size difference (ESD) of the domain-adversarial cross-project software defect prediction based on contextual semantics of the present invention for 6 different methods in 30 pairs of CPDP tasks;
[0044] Figure 6 It is a comparison chart of F-measure between the traditional embedding layer and the CBOW word embedding model in the ADA-GRU model for domain-adversarial cross-project software defect prediction based on contextual semantics of the present invention;
[0045] Figure 7 It is a comparison chart of F-measure of four classifiers in the ADA-GRU model for domain-adversarial cross-project software defect prediction based on contextual semantics of the present invention;
[0046] Figure 8 This is an accuracy comparison chart of four classifiers in the ADA-GRU model for cross-project software defect prediction based on contextual semantics of the present invention. [Specific implementation method]
[0047] The ADA-GRU model framework proposed by the present invention for domain-based cross-project software defect prediction based on contextual semantics is as follows: Figure 1 As shown in the figure, the source code is first parsed into an abstract syntax tree, from which some nodes prone to defects are selected. Then, all these nodes are encoded into sequence vectors through CBOW, and BiGRU and fully connected layers are used to generate semantic features. Adversarial learning is used to train the model to alleviate the distribution difference between the source project and the target project. Finally, the static program features are fused with the semantic features through Transfer Component Analysis (TCA) and input into the classifier for prediction.
[0048] 1.1. Program code analysis
[0049] AST can represent the grammatical structure parsed from the source code, which contains rich semantic information of the software project and can help predict potential defects or errors in the SDP field. This paper uses Javalang to parse the source and target project codes. There are many types of nodes in AST, some of which are highly related to code defects. Table 1 shows the four node categories selected in this paper. Figure 2 A code snippet and a simplified AST example are given. The simplified AST consists of four types of nodes: method call nodes and instance creation nodes, declaration nodes, control flow nodes, and other necessary nodes. The method call node captures the function call relationship of the program (such as Figure 2 func() function in the code), understanding the modular structure in the code, identifying functional dependencies, and understanding the interactions between different parts help to understand the overall structure of the program, declare nodes (such as Figure 2 f1 in the figure captures the structure of the program and the scope of the variables. By analyzing the declaration nodes, we can identify the variables, functions, and classes defined in the program, thereby understanding the namespace and structure of the program. Figure 2 ForStatement, ForControl) in the program provides information about the program execution path, which can help you understand the program's logical structure and loop structure. Other necessary nodes (such as Figure 2 BasicType in provides access to additional information in the program, such as constant values, comment content, etc., providing a more comprehensive understanding of the program.
[0050] Table 1 AST node types used in this article
[0051]
[0052] 1.2. Word vector generation
[0053] The node sequence traversed from the AST is a string sequence, which cannot be directly input into the neural network to extract features. Therefore, the node sequence must be converted into a numerical vector first. In NLP, word vectors are usually used to represent words. When predicting software defects, the nodes of the abstract syntax tree can be regarded as a word in natural language, and the corresponding word vectors are generated to transform the node sequence. In order to fully consider the contextual semantic information, this paper uses the CBOW model based on hierarchical softmax to capture the semantic relationship between contextual words. Its main idea is to take the contextual information of a specific word as input and output its distributed representation. The CBOW model consists of three layers: input layer, projection layer and output layer. First, the words with a fixed size window around a specific word are selected as context, and converted into a one-hot encoded word vector as the input of the input layer. Secondly, the word vector of the input layer is multiplied by the input weight matrix, and the cumulative average is entered into the projection layer. After that, the word vector of the projection layer is multiplied by the output weight matrix and normalized to the output layer with hierarchical Softmax. Finally, the distributed representation of the specific word is obtained. The structure of the CBOW model is as follows: Figure 3 shown.
[0054] The input layer consists of the context of the known word w, that is, the k words before and after Content(w), where k is the size of the context window. The word embedding vectors in the context window are accumulated and summed to obtain the projection layer:
[0055]
[0056] in It is the vocabulary w i The word embedding vector of .
[0057] The output layer corresponds to a Huffman tree, which is constructed by taking the words that appear in the corpus as leaf nodes and the number of times each word appears in the corpus as the weight. There is a separate path from the root node to the leaf node, and each non-leaf node corresponds to a vector θ (θ∈R h , h is the length of the vector, which has the same dimension as the word vector). The initial vector is randomly given to the words in the corpus and the context projection X of the center word w is provided w As input, formulas (2) and (3) represent the conditional probability formula for predicting word w as follows:
[0058]
[0059]
[0060] Among them, p w is the Huffman path from the root node to the leaf node corresponding to w, then l w is the path p wThe number of nodes contained in It means p w Each node in is the Huffman code of word w, Represents the path p w The j-th node code on Represents the path p w The parameter vector of the non-leaf node above, m is the vector dimension, and formula (4) shows how to capture the context of word t and calculate the projection value, (p is the parent word of t, C is all the subwords of t).
[0061]
[0062] 1.3. Migration model establishment and defect prediction
[0063] The main framework of the ADA migration model is as follows Figure 4 As shown in the figure, the model is based on the adversarial learning method and is mainly divided into three key steps: feature generation, adversarial training and feature fusion. After the CBOW model encoding is converted into a numerical vector, the BiGRU layer is constructed. Since the code is usually organized according to a certain sequence structure, BiGRU can effectively capture long sequence dependencies, and the bidirectional nature enables the model to learn the semantic and structural information of past tokens and future tokens from both the forward and backward directions. Compared with LSTM, GRU has fewer gate layers and fewer parameters that need to be optimized. A GRU layer contains an update gate Z and a reset gate R. The former is used to update the hidden state and the latter is used to decide which states should be forgotten. Assume that after the embedding layer, the source code can be represented by the matrix EM = [em1, em2,…, em n ]∈R n×d Indicates that d represents the word embedding dimension. At a given time t, the current embedding state em t and the previous hidden state H t-1 It is the input of update gate Z and reset gate R, and the output H of GRU layer t It can be calculated according to formula (5-8):
[0064]
[0065]
[0066]
[0067]
[0068] Among them, σ(·) and tanh(·) are activation functions and represent element-wise multiplication. W em ∈R d×h and W H ∈R h×h is the weight parameter to be learned, b (z) 、b (R) and b are bias units.
[0069] BiGRU contains two GRU layers. We will get two hidden state sequences, including the forward and reverse hidden states. Finally, we concatenate the two to get the final hidden sequence, as shown in formulas (9) to (11).
[0070]
[0071]
[0072]
[0073] Then, the hidden state of BiGRU is linearly transformed. This paper adopts three fully connected layers to learn higher-level and abstract feature representations to improve the expressiveness of the model. At the same time, the structure of the three fully connected layers provides more parameters that can be adjusted, making the model more flexible. Through layer-by-layer learning, more robust features are learned, and the model has better generalization ability for unseen data, as shown in formulas (12) to (14):
[0074] z1=ReLU(W1·h t +b1) (12)
[0075] z2=ReLU(W2·z1+b2) (13)
[0076]
[0077] Among them, ReLU is the rectified linear unit activation function, W1, W2, W3 are the weight matrices of the three fully connected layers, b1, b2, b3 are the corresponding bias vectors, and the forward propagation of the entire model can be expressed as:
[0078]
[0079] Subsequently, the generated semantic features are fused with the handcrafted features. This feature fusion method can comprehensively utilize the automatically extracted semantic information and the manually extracted features to more comprehensively and effectively represent the input data. It can be understood as the following formula:
[0080] F concat =[F fc , F static ] (16)
[0081] Among them, F fc is the semantic feature obtained after BiGRU and three-layer full connection, Fstatic It is the transferable feature obtained by TCA from the static manual features.
[0082] Finally, the fused features are input into the classifier for defect classification. At this stage, we obtain a well-trained source feature extractor and classifier. In order to adapt to the feature distribution of the target project, the parameters of the source feature extractor are shared with the target feature extractor, and feature extraction is performed. The feature vector generated by the source feature extractor is regarded as a real sample, and the feature vector generated by the target feature extractor is regarded as a false sample. A new discriminator is constructed, the purpose of which is to correctly classify real samples and false samples. The discriminator distinguishes between real and false samples, and the feature distribution between the source project and the target project is aligned. This adversarial learning mechanism effectively makes up for the feature difference between the source project and the target project, and improves the generalization performance of the model on the target project. After training, the ADA-GRU model can effectively predict whether there are defects in new project instances, and realize the defect prediction task across projects.
[0083] The algorithm process of ADA network is shown in Algorithm 1.
[0084] 2. Experiments and Discussion
[0085] Dataset
[0086] In order to verify the effectiveness of the ADA-GRU method, six open source projects are selected as evaluation datasets. The source code and corresponding PROMISE data of all projects are public and widely used in SDP research. In the experiment, DL-generated functions are extracted from Java source code, and static code metrics and data labels are extracted from the PROMISE repository. Table 2 shows the basic information of the selected projects, including project name, project version, number of instances, and defect rate. To ensure the generality of the evaluation results, the experimental dataset consists of projects of different sizes and defect rates (the maximum number of files is 1077, the minimum number of files is 205; the minimum defect rate is 19.5%, and the maximum defect rate is 98.8%). Table 3 shows the static code metrics contained in the PROMISE repository.
[0087] Table 2Thedatasetusedinthisarticle
[0088]
[0089] Table 3 List of 20 static indicators of PROMISE
[0090]
[0091] 2.2.Baseline Models
[0092] In order to verify the effectiveness of the proposed ADA-GRU model for the CPDP task, 5 baseline models were selected and all baseline methods were reproduced using PyTorch. All baseline methods were retrained under the same experimental settings.
[0093] To evaluate the performance of the ADA-GRU model, the following three research questions are explored:
[0094] RQ1: How does ADA-GRU compare to the existing CPDP model?
[0095] RQ2: Does the hierarchical softmax-based CBOW model perform better than traditional embedding layers?
[0096] RQ3: Which classifier performs better in ADA-GRU?
[0097] Three types of comparison methods were selected, including models with only manual features, features extracted by deep learning, and transfer learning based on statistical criteria methods. ADA-GRU was compared with TCA+, DBN, DPCNN, DPTCNN, and MANN. A brief description of the above comparison methods is as follows:
[0098] TCA+: This method uses traditional transfer learning methods to process manual features, reduce the differences in data distribution between projects, and expands customized normalization rules;
[0099] DBN: This method uses a deep belief network model to process AST vectors and generate deep features without having to deal with data differences between projects;
[0100] DPCNN: This method uses a convolutional neural network model to process AST vectors, generate deep features, and then fuse them with manual features without having to deal with data differences between projects;
[0101] DPTCNN: Based on the DPCNN model, it reduces the data distribution differences between projects by adding a matching layer to the convolutional neural network.
[0102] MANN: This method adds three adaptive layers to the convolutional neural network model to embed sample features into the multi-core Hilbert space for distribution, and uses the multi-core maximum mean difference method to reduce the differences between items. By constructing a nuclear norm module to maximize the nuclear norm of the target sample, the discrimination and diversity of the target sample are improved.
[0103] Evaluation indicators
[0104] In this paper, the F-measure and Accuracy indicators are used to evaluate the performance of the proposed model. These indicators are widely used to evaluate previous works on CPDP. In the process of binary classification, the results can be divided into four categories according to the classification results, as shown in Table 4.
[0105] Table 4 Confusion matrix
[0106]
[0107] True Positive (TP): indicates that the model correctly predicts positive samples as positive, that is, the number of samples that are actually positive and the model also predicts as positive. True Negative (TN): indicates that the model correctly predicts negative samples as negative, that is, the number of samples that are actually negative and the model also predicts as negative. False Positive (FP): indicates that the model incorrectly predicts negative samples as positive, that is, the number of samples that are actually negative but the model predicts as positive. False Negative (FN): indicates that the model incorrectly predicts positive samples as negative, that is, the number of samples that are actually positive but the model predicts as negative. Based on the confusion matrix, the true precision and recall can be calculated, as shown in formulas (17) and (18):
[0108]
[0109]
[0110] The F-measure indicator comprehensively considers the precision and recall of the model and is the harmonic mean of the two. The F-measure indicator formula is shown in (19):
[0111]
[0112] The most commonly used metric for balanced classification problems is accuracy, which evaluates the overall effectiveness of the model. Accuracy is equal to the number of correctly classified instances divided by the total number of all instances, as shown in formula (20):
[0113]
[0114] Statistical analysis methods
[0115] Performance Gain: To further compare the new method with the baseline model, the percentage difference calculation is used to show their performance difference in defect prediction, as follows:
[0116]
[0117] Among them, Res ADA-GRU Represents the various indicators in the experimental results of the model proposed in this paper, Res baseline The various indicators representing the comparative experimental results can be used to intuitively see how well the new model performs compared to the baseline model.
[0118] Statistical tests can be used to analyze whether there are significant statistical differences between the results of different methods. Cliff delta (δ) is used in this paper to measure the effect size between two different methods, thereby quantifying the difference between them. The following difference amounts are set in the experiment: negligible (|δ|<0.147), small (0.147<|δ|<0.33), medium (0.33<|δ|<0.474), and large (|δ|>0.474).
[0119] 3. Experimental results
[0120] 3.1. Experimental results and discussion on RQ1
[0121] Regarding the composition of the CPDP task, first one project is selected as the target project, and then the remaining 5 projects are used as source projects respectively (for example, Camel-1.6 data is used as the training set and Log4j-1.2 data is used as the test set). Therefore, 30 pairs are collected to perform CPDP. In fact, the class imbalance problem of software defect data is a typical feature in the CPDP task. No matter which project is used as the source project, the training data has a class imbalance problem. If the classifier is trained on a highly imbalanced dataset, the prediction model tends to support the majority class and has a weak ability to detect the minority class. Therefore, the class imbalance problem must be solved. Since class imbalance learning is only used as an application in this article, the standard SMOTE oversampling method is adopted.
[0122] Tables 5 and 6 show the comparison results of the two evaluation indicators of the Promise dataset. It can be observed from the table that ADA-GRU achieves an average of 0.712 and 0.637 in F1 and Accuracy respectively. Compared with other baseline methods, ADA-GRU is 9.3%, 9.8%, 7.9%, 8.6% and 5.8% higher in F1 than TCA+, DBN, DPCNN, DPTCNN, and MANN respectively. The average Accuracy of ADA-GRU is 0.637, which is 7.8%, 8.5%, 7.9%, 7.9% and 5.1% better than TCA+, DBN, DPCNN, DPTCNN, and MANN respectively. Figure 5 The corresponding Scott-KnottESD test is shown
[45] Results, including 30 pairs of CPDP tasks consisting of 6 datasets. Through comprehensive comparison, ADA-GRU outperforms other methods.
[0123] Table 5 Comparison of F1 measurement on PROMISE dataset using five methods
[0124]
[0125] Table 6 Comparison of the accuracy of 5 methods on the PROMISE dataset
[0126]
[0127] Scott-Knott Effect Size Difference (ESD) ranking 6 different methods on 30 pairs of CPDP tasks. The blue diamond indicates the average F1 of ADA-GRU.
[0128] Figure 5 Scott-Knott Effect Size Difference (ESD) ranking 6 different methods on 30 pairs of CPDP tasks. The blue diamond indicates the average F1 of ADA-GRU.
[0129] Table 7 Performance comparison between ADA-GRU and baseline models
[0130]
[0131] Cliffdelta:I(ignore)(|δ|<0.147), S(0.147<|δ|<0.33), M(0.33<|δ|<0.474), L(|δ|>0.474)
[0132] The results of the statistical analysis in Table 7 show that the percentage difference between the ADA-GRU method and the baseline model is: 8.7-15.4% improvement in Accuracy, 8.7-15.9% improvement in F-measure; the Cliff test shows that the new model has significant performance differences from the baseline model.
[0133] 3.2. Experimental results and discussion on RQ2
[0134] like Figure 6As shown in the figure, in the software projects used, for each project (Camel, Log4j, Lucene, Poi, Xalan, Xerces), the CBOW model generally performs better in performance indicators. Compared with the traditional embedding layer, the CBOW model improves by 0.4%, 3%, 0.7%, 3.8%, 6.9%, and 5.1% in Camel, Log4j, Lucene, Poi, Xalan, and Xerces projects, respectively. This shows that in this experiment, the CBOW model performs better for node embedding tasks and can more effectively capture the semantic relationship between nodes, thereby improving the overall performance, which has important practical application significance for software defect prediction.
[0135] 3.3. Experimental results and discussion on RQ3
[0136] The defect prediction model constructed in this paper is used to compare classifiers. By comparing the impact of multiple classifiers on model performance, it is verified which classifier is more effective for the experiment.
[0137] from Figure 7-8 It can be observed that RF improves F-measure by an average of 17.9%, 7.4%, and 10.7% in ADA-GRU compared with the currently popular MLP, LightGBM, and the most commonly used LR classifier; and improves Accuracy by 14.9%, 8.9%, and 9.3%, respectively.
[0138] 4. Conclusion
[0139] The present invention can fully characterize the contextual semantic information of the source code and perform better defect transfer between the source project and the target project. The main process of the model is as follows: first, a simplified AST is used to represent the code of each extracted program module, then the token vector is traversed through the AST, and the word embedding is performed using the hierarchical softmax-based CBOW algorithm, which helps to fully capture the semantic relationship between nodes when the token vector is converted into a digital vector. Then, the numerical vector is input into the neural network model to extract the semantic and contextual features of the source project and the target project, and the data distribution differences between cross-projects are reduced by means of adversarial learning. Finally, the existence of defects is determined by the RF classifier. In order to verify the effectiveness of the model, we use F-measure and Accuracy as evaluation indicators for two sets of comparative experiments. The experimental results show that the proposed ADA-GRU model outperforms the selected traditional and state-of-the-art CPDP methods. In the future, we hope to verify the effectiveness of our method on more open source projects and commercial projects. In addition, we would like to try other transfer learning techniques to reduce the differences in data distribution and improve prediction performance. Since the ADA-GRU model is only applicable to one-to-one prediction, we can also try to study the many-to-one situation in the future.
[0140] The above embodiments are intended to illustrate the present invention, not to limit the present invention. Any solution that is a simple transformation of the present invention belongs to the protection scope of the present invention.
Claims
1. Domain-adversarial cross-project software defect prediction based on contextual semantics, characterized by: The following steps are involved: S1. Program code parsing: Use AST Javalang to parse the grammatical structure from the source code to get an abstract syntax tree, from which nodes prone to defects are selected; S2, word vector generation: convert the node sequence traversed from the AST into a numerical vector, regard the node of the abstract syntax tree as a word in the natural language, generate the corresponding word vector to transform the node sequence, and use the hierarchical softmax-based CBOW model to capture the semantic relationship between context words; S3. Migration model establishment and defect prediction: After the CBOW model encoding is converted into a numerical vector, a BiGRU layer is constructed to capture long sequence dependencies. The CBOW model learns the semantic and structural information of past tokens and future tokens from both the forward and backward directions. The BiGRU layer contains two GRU layers. A GRU layer contains an update gate Z and a reset gate R. The former is used to update the hidden state, and the latter is used to determine which states should be forgotten. Two hidden state sequences are obtained through two GRU layers, including forward and reverse hidden states. Finally, the two are concatenated to obtain the final hidden sequence. The hidden state of the BiGRU layer is then linearly transformed using The three fully connected layers learn higher-level and abstract feature representations, fuse the generated semantic features with the hand-crafted features, and the fused features are input into the classifier for defect classification, completing the training of the source feature extractor and classifier. The parameters of the source feature extractor are shared with the target feature extractor, and feature extraction is performed. The feature vectors generated by the source feature extractor are regarded as real samples, and the feature vectors generated by the target feature extractor are regarded as false samples. A new discriminator is constructed to distinguish between real and false samples, align the feature distributions between the source project and the target project, and use the model to predict whether there are defects in the new project instance, thereby realizing the cross-project defect prediction task.
2. The domain-adversarial cross-project software defect prediction based on contextual semantics according to claim 1, characterized in that: In step S1, the AST is simplified to consist of four types of nodes: method call nodes and instance creation nodes, declaration nodes, control flow nodes and other necessary nodes. The method call nodes and instance creation nodes capture the function call relationship of the program, understand the modular structure in the code, identify functional dependencies, understand the interaction between different parts, and understand the overall structure of the program; the declaration nodes capture the structure of the program and the scope of the variables, and by analyzing the declaration nodes, identify the variables, functions and classes defined in the program, and understand the namespace and structure of the program. The control flow nodes provide information on the program execution path, understand the logical structure and loop structure of the program, and the other necessary nodes provide access to additional information in the program, including constant values and comment content, to provide a more comprehensive understanding of the program.
3. The domain-adversarial cross-project software defect prediction based on contextual semantics according to claim 1, characterized in that: In step S2, the context information of a specific word is used as input and its distributed representation is output. The CBOW model consists of an input layer, a projection layer and an output layer. First, words with a fixed-size window around a specific word are selected as context and converted into a uniquely hot encoded word vector as input to the input layer. Second, the word vector of the input layer is multiplied by the input weight matrix, and the word vectors of the projection layer are entered into the projection layer after cumulative averaging. Then, the word vectors of the projection layer are multiplied by the output weight matrix and normalized to the output layer using hierarchical Softmax to obtain the distributed representation of the specific word. The input layer consists of the context of the known word w, i.e., k words before and after Content(w). Where k is the size of the context window, and the word embedding vectors in the context window are cumulatively summed to obtain the projection layer: in It is the vocabulary w i The word embedding vector of The output layer corresponds to a Huffman tree, which is constructed with words that appear in the corpus as leaf nodes and the number of times each word appears in the corpus as the weight. There is a separate path from the root node to the leaf node, and each non-leaf node corresponds to a vector θ, θ∈R h , h is the length of the vector, which has the same dimension as the word vector, gives the initial vector randomly to the words in the corpus, and provides the context projection X of the center word w w As input, formulas (2) and (3) represent the conditional probability formula for predicting word w as follows: Among them, p w is the Huffman path from the root node to the leaf node corresponding to w, then l w is the path p w The number of nodes contained in It means p w Each node in is the Huffman code of word w, Represents the path p w The j-th node code on Represents the path p w The parameter vector of the non-leaf node above, m is the vector dimension, and formula (4) shows how to capture the context of word t and calculate the projection value, (p is the parent word of t, C is all the subwords of t).
4. The domain-adversarial cross-project software defect prediction based on contextual semantics according to claim 1, characterized in that: In step S3, after the embedding layer, the source code is represented by the matrix EM=[em1, em2, ..., em n ]∈R n×d Represents, where d represents the word embedding dimension, within a given time t, the current embedding state em t and the previous hidden state H t-1 It is the input of update gate Z and reset gate R, and the output H of GRU layer t It can be calculated according to formula (5-8): Among them, σ(·) and tanh(·) are activation functions, representing element-wise product, W em ∈R d×h and W H ∈R h×h is the weight parameter to be learned, b (Z) , b (R) and b are bias units, BiGRU contains two GRU layers, which will get two hidden state sequences, including the forward and reverse hidden states. Finally, the two are concatenated to get the final hidden sequence, as shown in formulas (9) to (11). Then, the hidden state of BiGRU is linearly transformed, and a three-layer fully connected layer is used to learn higher-level and abstract feature representations to improve the expressiveness of the model. At the same time, the structure of the three-layer fully connected layer provides more parameters that can be adjusted, making the model more flexible. Through layer-by-layer learning, more robust features are learned, and the model has better generalization ability for unseen data, as shown in formulas (12) to (14). z1=ReLU(W1 h t +b1) (12) z2=ReLU(W2·z1+b2) (13) Among them, ReLU is the rectified linear unit activation function, W1, W2, W3 are the weight matrices of the three fully connected layers, b1, b2, b3 are the corresponding bias vectors, and the forward propagation of the entire model can be expressed as: Subsequently, the generated semantic features are fused with the handcrafted features, and the automatically extracted semantic information and the handcrafted features are comprehensively utilized to represent the input data more comprehensively and effectively, which can be understood as the following formula: F concat =[F fc ,F static ] (16) Among them, F fc is the semantic feature obtained after BiGRU and three-layer full connection, F static It is a transferable feature obtained by TCA from static manual features.
Citation Information
Cited By
Traffic state probability prediction method for multi-source spatio-temporal data fusion
CN122347871A
A traffic state probability prediction method for multi-source spatio-temporal data fusion
CN122347871B
