Software defect prediction method based on generative adversarial network and ensemble learning
By using the AdaBoost method of data resampling and integrated learning in the software defect prediction method for classification training, the problem of software defect data class imbalance is solved, and the accuracy and recall rate of software defect prediction are improved.
Patent Information
- Application Number
- CN202111243350.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-25
- Publication Date
- 2025-02-21
- Estimated Expiration
- 2041-10-25
AI Technical Summary
When existing software defect prediction methods deal with class imbalanced software defect data, it is difficult to effectively alleviate the data imbalance problem, resulting in low accuracy and recall of classifiers.
The data resampling method based on the generative adversarial network is adopted to adaptively generate new few categories of defect-class data, and combine the AdaBoost method of integrated learning to perform software defect classification training to improve the accuracy and recall rate of prediction.
By generating adversarial networks, the class imbalance problem of software defect data is alleviated, and the accuracy, accuracy, recall and F-measure performance of software defect prediction methods are improved.
Smart Images

Figure CN114064459B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of software defect prediction, and in particular relates to a software defect prediction method based on generative adversarial networks and ensemble learning. Background Art
[0002] With the popularization of informatization, all kinds of software that are in use or in the process of development may have software defects. Being able to discover and locate software defects in a timely manner plays a significant role in the normal operation of the software system and the improvement of software functions. Software Defect Prediction (SDP) aims to find the module that is most likely to contain defects from the software project. The module here can be a function, a loop body, or a class, etc. Usually, the software defect prediction method is divided into the following three steps. First, according to the vulnerability reports of each module in the historical software, the software modules are marked as defects and non-defects. Secondly, in order to train various software defect prediction models, different software defect measurement indicator information is used as the feature of defect data. Finally, after a certain amount of data preprocessing, the training set and test set are divided to train and test the software defect prediction model.
[0003] At present, many software defect prediction methods have been proposed, such as support vector machine, decision tree, random forest, AdaBoost method, etc. However, these methods only perform brief preprocessing on the original defect data, make different modifications at the algorithm level, and do not consider the problems at the data level. Considering that software defects account for a small proportion of the entire software, the software defect dataset is a typical class imbalance data. For example, in PC1, PC2 and PC3 of NASA MDP software defect dataset, there is a serious class imbalance phenomenon. Due to the resampling of sample data, the originally unbalanced data can be balanced. Therefore, the resampling method is introduced into the software defect prediction method. Typical resampling methods include synthetic minority over-sampling technique (SMOTE), adaptive synthetic over-sampling method (ADASYN), random over-sampling method (ROS), random under-sampling method (RUS), etc. Among them, the SMOTE and ADASYN methods use artificial methods to synthesize small sample data to reduce the class imbalance problem of data. The rules for generating data are artificially specified, and it is difficult to find suitable rules in real software defect data sets. The ROS and RUS methods directly reduce the amount of data in the training set of the software defect prediction model, and the actual application effect is poor. Summary of the invention
[0004] The purpose of the present invention is to provide a software defect prediction method based on a generative adversarial network and ensemble learning. When dealing with different software defect data sets, the present invention can adaptively perform data resampling through a generative adversarial network to alleviate the problem of data imbalance. At the same time, the AdaBoost method of ensemble learning is used to perform software defect classification training to improve the accuracy, correctness, recall rate and F-measure performance of the software defect prediction method.
[0005] The technical solution adopted by the present invention is:
[0006] The software defect prediction method based on generative adversarial network and ensemble learning includes the following steps:
[0007] Step 1: preprocess the software defect data set, divide the preprocessed data set into a training set and a test set, and calculate the ratio of defective data to non-defective data in the training set data, which is recorded as the resampling rate;
[0008] Step 2: construct a generative adversarial network model, which consists of two parts: a generator and a discriminator;
[0009] Step 3, using a generative adversarial network to train the training set data until the generative adversarial network converges or reaches a set number of iterations, and then a trained generative adversarial network is obtained;
[0010] Step 4: Use the trained generative adversarial network to generate new minority category defect data according to the resampling rate calculated in step 1 to obtain a resampled training set.
[0011] Step 5: Use the ensemble learning AdaBoost method to train the software defect strong classifier. Input the training set resampled in step 4 into the software defect classifier. After the training is completed, test the classifier effect on the test set to obtain the software defect prediction evaluation index.
[0012] The present invention is also characterized in that:
[0013] The loss functions of the generator and discriminator are shown in formula (1) and formula (2):
[0014] DLoss=BCELoss(x true ,y true )+BCELoss(x fake ,y fake ) (1)
[0015] GLoss = BCELoss (x fake ,y true ) (2)
[0016] Where DLoss is the loss function of the discriminator, and GLoss is the loss function of the generator; both the generator and the discriminator use the Adam optimizer, using three hyperparameters: learning rate, Betas, and number of iterations; x fake 、x true ,y true ,y fake are respectively the fake data generated by the generator, the real minority defect data, the label of the real minority defect data and the label of the fake data. BCELoss is the binary cross entropy loss function, and the formula is:
[0017]
[0018] where x i ,y i are the i-th software defect sample and its corresponding label, and n is the total number of samples.
[0019] In step 2, the generator includes an initial random noise input layer, a final generated data output layer, and a block structure consisting of a linear layer, a batch normalization layer, and a LeakyReLU activation function. The batch normalization layer is not set in the first block structure after the noise input layer, and the batch normalization layer is not set in the previous block structure of the data output layer. The Sigmoid activation function is used in the data output layer, and the noise input dimension is noise_dim. After three block structures, it finally passes through the fully connected layer of the output layer, and the output dimension is out_dim.
[0020] In step 2, the discriminator includes an initial data input layer, a final identification result output layer, and a block structure consisting of a linear layer and a LeakyReLU activation function; the dimension of the input data is out_dim, which is the same as the data dimension output by the generator; the identification result output layer uses a linear layer and a Sigmoid activation function, and the final output result of the discriminator is a value between 0 and 1.
[0021] The steps for step 3 are:
[0022] Step 3.1, normalize the training set and constrain its value range to between 0 and 1;
[0023] Step 3.2: Input random noise with a value range of 0 to 1 into the generator G to generate fake data, and record the data label as 0; record the normalized training set label in step 3.1 as 1, and input it into the discriminator D together with the fake data to distinguish between real data and fake data;
[0024] Step 3.3, repeat step 3.2 until the generative adversarial network converges or reaches the set number of iterations, and obtain the trained generator G and discriminator D.
[0025] The specific operation of step 4 is as follows: step 4.1, using the generator G in the generative adversarial network trained in step 3, according to the resampling rate k in step 1, calculate the few sample software defect data to be generated, input the random noise from 0 to 1 into the generator G obtained in step 3, and obtain the generated minority category defect data after calculation by the generator;
[0026] In step 4.2, the minority class defect data generated in step 4.1 is subjected to a denormalization operation opposite to the normalization operation in step 3.2, and is merged with the original software defect minority class training data to obtain the resampled training set data.
[0027] The specific operations of step 5 are as follows:
[0028] Step 5.1: Perform a ten-fold cross-validation on the resampled training set in step 4. The entire training data set is divided into 10 parts, each of which will be used as a validation set to test the classifier during training. Repeat this process 10 times, and finally obtain the training performance index of the classifier by weighted average. The training process is completed, and the trained software defect strong classifier C is obtained.
[0029] Step 5.2, use the software defect strong classifier C in step 5.1 to test the model classification performance on the test set, obtain the classification result of the strong classifier C on the test set, and test the classification performance of the strong classifier on the test set.
[0030] In step 5.1, the training of the software defect strong classifier C adopts the AdaBoost learning method. The process of training the software defect strong classifier is shown in formulas (6), (7), and (8):
[0031]
[0032]
[0033]
[0034] Among them, α m is the weight of the mth decision tree weak classifier, C m (x) is the mth decision tree weak classifier, C(x) is the strong classifier obtained by ensemble learning, and the sign function is used to take the weighted positive and negative of the results of m weak classifiers, e m is the classification error rate of the mth decision tree weak classifier.
[0035] The beneficial effects of the present invention are:
[0036] 1) The method of the present invention proposes a software defect prediction method based on generative adversarial networks and ensemble learning. The use of generative adversarial networks to resample software defect data solves the drawbacks of artificial synthetic class resampling methods and alleviates the class imbalance problem of software defect data. When dealing with various software defect data sets, the correctness, accuracy, recall rate and F-measure performance of the classifier can be improved.
[0037] 2) The method of the present invention solves the problem of software defect prediction from two levels. At the data level, a generative adversarial network is used to perform data resampling, and at the algorithm level, an ensemble learning method is used to train the classifier. It combines two different angles to solve the software defect problem and improves the classification performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1This is an overall flow chart of a software defect prediction method based on generative adversarial networks and ensemble learning according to the present invention;
[0039] Figure 2 A diagram of a generative adversarial network structure in a software defect prediction method based on a generative adversarial network and ensemble learning according to the present invention;
[0040] Figure 3 A schematic diagram of the structure of the generator and discriminator in a software defect prediction method based on generative adversarial networks and ensemble learning;
[0041] Figure 4 This is a line graph showing the change in the accuracy of the model on the test set before and after the generative adversarial network is resampling in a software defect prediction method based on generative adversarial networks and ensemble learning. DETAILED DESCRIPTION
[0042] The present invention is described in detail below with reference to the accompanying drawings and specific embodiments.
[0043] The present invention is based on a software defect prediction method based on generative adversarial networks and ensemble learning, such as Figure 1 to Figure 3 , including the following steps:
[0044] Step 1: preprocess the software defect data set, divide the preprocessed data set into a training set and a test set, and calculate the ratio of defective data to non-defective data in the training set data, which is recorded as the resampling rate;
[0045] Step 2: construct a generative adversarial network model, which consists of two parts: a generator and a discriminator;
[0046] Step 3, using a generative adversarial network to train the training set data until the generative adversarial network converges or reaches a set number of iterations, and then a trained generative adversarial network is obtained;
[0047] Step 4: Use the trained generative adversarial network to generate new minority category defect data according to the resampling rate calculated in step 1 to obtain a resampled training set.
[0048] Step 5: Use the ensemble learning AdaBoost method to train the software defect strong classifier. Input the training set resampled in step 4 into the software defect classifier. After the training is completed, test the classifier effect on the test set to obtain the software defect prediction evaluation index.
[0049] In step 1:
[0050] The defect data set used in the method of the present invention comes from the NASA software defect data set, which includes 12 sub-data sets in total. The software defect measurement criteria used include the McCabe measurement method, the HalStead scientific measurement method, the code line number measurement method and the CK measurement method.
[0051] The preprocessing operation includes the removal of duplicate data, duplicate attributes and abnormal data. After random sampling of the preprocessed data, the training set and test set are divided.
[0052] In step 2, the structures of the generator and discriminator are:
[0053] The generator includes an initial random noise input layer, a final generated data output layer, and a block structure consisting of a linear layer, a batch normalization layer, and a LeakyReLU activation function. In the first block structure after the noise input layer and the previous block structure of the data output layer, no batch normalization layer is set. The Sigmoid activation function is used in the data output layer to map the numerical range of the generated data to the interval of 0 to 1. The noise input dimension is noise_dim. After three block structures, it finally passes through the fully connected layer of the output layer, and the output dimension is out_dim.
[0054] The discriminator includes an initial data input layer, a final identification result output layer, and a block structure consisting of a linear layer and a LeakyReLU activation function. The data input layer receives real minority class defect data and forged data generated by the generator. The dimension of the input data is out_dim, which is the same as the data dimension output by the generator. The identification result output layer uses a linear layer and a Sigmoid activation function, and the final output result of the discriminator is in the range of 0 to 1. The input data first passes through the data input layer, then through a block structure, and finally through the discriminator result output layer to obtain a discriminator output result of 0 to 1.
[0055] The loss functions of the generator and discriminator are shown in formula (1) and formula (2):
[0056] DLoss=BCELoss(x true ,y true )+BCELoss(x fake ,y fake ) (1)
[0057] GLoss = BCELoss (x fake ,y true ) (2)
[0058] Where DLoss is the loss function of the discriminator, and GLoss is the loss function of the generator. Both the generator and the discriminator use the Adam optimizer, using three hyperparameters: learning rate, Betas, and number of iterations. fake 、x true ,y true ,y fake are respectively the fake data generated by the generator, the real minority defect data, the label of the real minority defect data and the label of the fake data. BCELoss is the binary cross entropy loss function, and the formula is:
[0059]
[0060] where x i ,y i are the i-th software defect sample and its corresponding label, and n is the total number of samples.
[0061] The specific steps of step 3 are:
[0062] Step 3.1: Normalize the training set to constrain its value range to between 0 and 1. The formula used for normalization is shown in formula (4), where x ij is the jth eigenvalue of the i-th software defect sample, x j_min is the minimum value of the jth eigenvalue, x j_max is the maximum value of the jth eigenvalue, x is the normalized eigenvalue, which is a floating point number between 0 and 1;
[0063]
[0064] Step 3.2: Input random noise with a value range of 0 to 1 into the generator G to generate forged data, and record the data label as 0; record the normalized training set label in step 3.1 as 1, and input it into the discriminator D together with the forged data to distinguish between real data and forged data;
[0065] Step 3.3, repeat step 3.2 until the generative adversarial network converges or reaches the set number of iterations, and obtain the trained generator G and discriminator D.
[0066] The specific operation of step 4 is as follows: Step 4.1, using the generator G in the generative adversarial network trained in step 3, according to the resampling rate k calculated in step 1, calculate the minority category defect data to be generated, input the random noise from 0 to 1 into the generator G obtained in step 3, and obtain the generated minority category defect data after calculation by the generator:
[0067] N=(k-1)*T (5)
[0068] Where N is the amount of defect data that needs to be synthesized, k is the resampling rate, and T is the amount of minority defect data in the training set;
[0069] In step 4.2, the minority class defect data generated in step 4.1 is subjected to a denormalization operation opposite to the normalization operation in step 3.2, and is merged with the original software defect minority class training data to obtain a resampled training set.
[0070] The specific operations of step 5 are as follows:
[0071] Step 5.1: The software defect classifier uses the AdaBoost ensemble learning method, using M identical decision tree weak classifiers as weak learners, and obtains the final software defect strong classifier through weighted averaging; the training process is as follows:
[0072] Step 5.1: Perform a 10-fold cross-validation on the resampled training set in step 4. The entire training data set is divided into 10 parts. Each part will be used as a validation set to test the classifier during training. Repeat this process 10 times, and the weighted average is used as the final performance indicator of the classifier. At the end of the training process, the trained software defect classifier C is obtained. The process of using the AdaBoost learning method to train the final classifier is shown in formulas (6), (7), and (8):
[0073]
[0074]
[0075]
[0076] Among them, α m is the weight of the mth decision tree weak classifier, C m (x) is the mth decision tree weak classifier, C(x) is the strong classifier obtained by ensemble learning, and the sign function is used to take the weighted positive and negative of the results of M weak classifiers, e m is the classification error rate of the mth decision tree weak classifier;
[0077] Step 5.2: Use the strong classifier C trained in step 5.1 and the precision, correctness, recall, and F-measure performance indicators to perform a performance test on the trained software defect classifier on the test set.
[0078] The method proposed in the present invention adopts a generative adversarial network to generate software defect data at the data level. After alleviating the problem of data class imbalance, an ensemble learning method is used at the algorithm level. A ten-fold cross-validation method is adopted during training to ensure the randomness and rationality of the training process. Ultimately, the combination of the two improves the accuracy of software defect classification.
[0079] Example 1
[0080] Execute step 1. The software defect data set used in this embodiment comes from the NASA software defect data set, which includes 12 sub-data sets. The software defect measurement criteria used include the McCabe measurement method, the HalStead scientific measurement method, the code line measurement method, and the CK measurement method. The number of features contained in each sub-data set varies, as shown in the feature column of Table 1. After the original NASA software defect data set is preprocessed, duplicate data, duplicate attributes, and abnormal data are removed. The preprocessed NASA software defect data set is shown in Table 1.
[0081] The data set in Table 1 is randomly sampled and divided into training set and test set in a ratio of 8:2. The number of defective data and non-defective data in the training set data is counted respectively, and then the ratio between the two is calculated to obtain the resampling rate k, which is used to calculate the number of resampled data.
[0082] Table 1 NASA software defect dataset
[0083]
[0084] Execute step 2. The generative adversarial network constructed in this embodiment is divided into two parts: a generator and a discriminator. The specific structures of the generator and the discriminator are shown in Figure 3 , the noise input dimension noise_dim of the generator is set to 50, the output dimension of the generator is the same as the input dimension out_dim of the discriminator, and is set to the number of features of the 12 sub-datasets used in this embodiment, which varies depending on the characteristics of the factor dataset. In the Adam optimizer used by the generator and the discriminator, the learning rate hyperparameter is set to 0.05, the Betas hyperparameter is set to (0.5, 0.999), and the number of iterations hyperparameter is set to 5000.
[0085] Execute steps 3 and 4.
[0086] Execute step 5. In this embodiment, the number of weak classifiers M set by ensemble learning is 200. 200 decision tree weak classifiers are trained on the training set data. After the classifier weights are updated, a strong classifier for software defects is finally obtained by weighting. The strong classifier is used to evaluate the performance of the method on the test set.
[0087] For the evaluation of the software defect prediction model, since the final prediction result is a binary classification result, in this embodiment, the binary classification confusion matrix is calculated to obtain four evaluation indicators: precision, correctness, recall and F-measure. The confusion matrix is shown in Table 2.
[0088] Table 2 Binary Classification Confusion Matrix
[0089]
[0090] According to the real category of the sample and the result predicted by the model, it can be divided into four cases: True Positive (TP), False Negative (FN), False Positive (FP) and True Negative (TN). The calculation of the other four evaluation indicators can be seen in formulas (9), (10), (11) and (12).
[0091] The calculation formula of accuracy is shown in formula (9).
[0092]
[0093] The calculation formula of precision is shown in formula (10).
[0094]
[0095] The calculation formula of recall is shown in formula (11).
[0096]
[0097] The calculation formula of F-measure is shown in formula (12).
[0098]
[0099] For the classification results obtained in this embodiment, the performance of the model on the test set is tested according to the performance evaluation indicators described in formulas (9), (10), (11), and (12). The line graph of the change in the accuracy index of the software defect prediction model before and after resampling is shown in Fig. Figure 4 , taking the CM1 dataset as an example. The classification method evaluation of accuracy, precision, recall and F-measure on the test set data (taking the average result of 10 cross-validations) is shown in Table 3.
[0100] Table 3 Evaluation of classification methods on the test set
[0101] Evaluation indicators Accuracy Accuracy Recall F-measure CM1 78% 87% 86% 0.87 JM1 73% 83% 82% 0.83 KC1 66% 79% 73% 0.76 KC3 81% 85% 92% 0.89 MC1 85% 87% 88% 0.88 MC2 77% 76% 88% 0.81 MW1 82% 90% 89% 0.89 PC1 88% 94% 92% 0.93 PC2 84% 97% 87% 0.87 PC3 85% 91% 91% 0.91 PC4 86% 92% 91% 0.92 PC5 71% 80% 80% 0.80
Claims
1. A software defect prediction method based on generative adversarial networks and ensemble learning, characterized in that: The following steps are involved: Step 1: preprocess the software defect data set, divide the preprocessed data set into a training set and a test set, and calculate the ratio of defective data to non-defective data in the training set data, which is recorded as the resampling rate; Step 2: construct a generative adversarial network model, which consists of two parts: a generator and a discriminator; Step 3, using a generative adversarial network to train the training set data until the generative adversarial network converges or reaches a set number of iterations, and then a trained generative adversarial network is obtained; Step 4: Use the trained generative adversarial network to generate new minority category defect data according to the resampling rate calculated in step 1 to obtain a resampled training set. Step 5: Use the ensemble learning AdaBoost method to train the software defect strong classifier, input the training set resampled in step 4 into the software defect classifier, and after the training is completed; test the classifier effect on the test set to obtain the software defect prediction evaluation index; The loss functions of the generator and discriminator are shown in formula (1) and formula (2): (1) (2) DLoss is the loss function of the discriminator, and GLoss is the loss function of the generator. Both the generator and the discriminator use the Adam optimizer, with three hyperparameters: learning rate, Betas, and number of iterations. x fake , x true , y true , y fake are respectively the fake data generated by the generator, the real minority defect data, the label of the real minority defect data and the label of the fake data. BCELoss is the binary cross entropy loss function, and the formula is: in x i ,y i Respectively i software defect samples and their corresponding labels, n is the total number of samples; In step 2, the generator includes an initial random noise input layer, a final generated data output layer, and a block structure consisting of a linear layer, a batch normalization layer, and a LeakyReLU activation function. The batch normalization layer is not set in the first block structure after the noise input layer, and the batch normalization layer is not set in the previous block structure of the data output layer. The Sigmoid activation function is used in the data output layer, the noise input dimension is noise_dim, and after three block structures, it finally passes through the fully connected layer of the output layer, and the output dimension is out_dim; The discriminator in step 2 includes an initial data input layer, a final identification result output layer, and a block structure composed of a linear layer and a LeakyReLU activation function; the dimension of the input data is out_dim, which is the same as the data dimension output by the generator; the identification result output layer uses a linear layer and a Sigmoid activation function, and the final output result of the discriminator is a value between 0 and 1; The steps of step 3 are: Step 3.1, normalize the training set and constrain its value range to between 0 and 1; Step 3.2: Input random noise with a value range of 0 to 1 into the generator G Generate fake data and record the data label as 0; record the normalized training set label in step 3.1 as 1 and input it into the discriminator together with the fake data. D ,distinguish between real data and fake data; Step 3.3, repeat step 3.2 until the generative adversarial network converges or reaches the set number of iterations, and then the trained generator is obtained. G and the discriminator D ; The specific operation of step 4 is as follows: Step 4.1, use the generator in the generative adversarial network trained in step 3 G , according to the resampling rate in step 1 k , calculate the few sample software defect data to be generated, and input the random noise from 0 to 1 into the generator obtained in step 3 G In the above example, after calculation by the generator, the generated minority category defect data is obtained; Step 4.2, performing a denormalization operation opposite to the normalization operation in step 3.2 on the minority class defect data generated in step 4.1, and merging them with the original software defect minority class training data to obtain the resampled training set data; The specific operations of step 5 are as follows: Step 5.1: Perform a 10-fold cross-validation on the resampled training set in step 4. The entire training data set is divided into 10 parts. Each part will be used as a validation set to test the classifier during training. Repeat this process 10 times and finally obtain the training performance index of the classifier by weighted average. The training process is completed and a strong classifier for software defects is obtained. C ; Step 5.2: Use the strong software defect classifier in step 5.1 C , test the model classification performance on the test set and obtain a strong classifier C The classification results on the test set are used to test the classification performance of the strong classifier on the test set; The software defect strong classifier in step 5.1 C The training adopts AdaBoost learning method to train the strong classifier of software defects, see formula (6), (7), (8); (6) (7) (8) in, For the m The weights of the decision tree weak classifiers, For the m decision tree weak classifiers, is the strong classifier obtained by ensemble learning, Function used to get m The weighted positive and negative of the weak classifier results, For the m The error rate of classification by a decision tree weak classifier.
Citation Information
Patent Citations
Data generation method of generative adversarial network based on cloud ERP supply chain ecosphere
CN115098674A