A cross-project software defect prediction method based on hybrid project adversarial learning
Through the hybrid project adversarial learning method, using adversarial networks and supervised comparison learning, the feature distribution differences in cross-project software defect prediction are solved, and the problem of insufficient feature information retention and relationship utilization in the existing technology is solved, achieving more accurate Software defect prediction.
Patent Information
- Application Number
- CN202210231504.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-10
- Publication Date
- 2024-11-08
- Estimated Expiration
- 2042-03-10
AI Technical Summary
When the existing cross-project software defect prediction model deals with the difference in feature distribution between different projects, it is difficult to effectively retain the original feature information, and the relationship between the source and the target project is not fully utilized, resulting in poor prediction results.
A hybrid project adversarial learning method is adopted, through adversarial network model and supervised comparison learning, combining proportional factors and domain discriminative networks, feature extraction and reconstruction are performed, distribution differences are reduced and discriminative features are retained, and cross-project software defect prediction is achieved.
It effectively reduces the difference in feature distribution between different projects, improves the extraction ability of feature mapping network, enhances the generalization ability of the model, and improves the accuracy and effectiveness of software defect prediction.
Smart Images

Figure CN114816983B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software engineering, and in particular to a cross-project software defect prediction method based on hybrid project adversarial learning. Background Art
[0002] In today's society, the scale of software systems is getting larger and larger, and the calls between different systems are complicated. If a commercial software system causes a production accident due to a software bug after it goes online, the loss will be immeasurable. Traditional software defect troubleshooting requires a lot of time and manpower through manual methods, and the troubleshooting effect is not very satisfactory. At this time, an automated method is needed to discover bugs in the software during the software development process. The traditional software defect prediction method is to perform it within the same project. This method will be better, but often a newly developed project does not have sufficient historical data, or historical data is lost, etc., which will bring difficulties to software defect prediction. At this time, it is necessary to train a model on other projects and then use it for defect prediction on new projects. This method is called cross-project software defect prediction. There are also some problems with cross-project software defect prediction. For example, due to the experience of developers or the different programming languages used in projects from different companies, there is a large difference in feature distribution between the source project and the target project after feature representation. How to reduce the distribution difference while better retaining the original features is a key topic studied in cross-project software defect prediction. In addition, some existing cross-project software defect prediction models cannot fully utilize the information of source and target projects and cannot better mine the relationships between different projects. Summary of the invention
[0003] Purpose of the invention: In order to solve the problem of how to better retain the identification feature information when there are too large distribution differences between different projects, the present invention proposes a cross-project software defect prediction method based on hybrid project adversarial learning, and uses this method to predict cross-project software defects.
[0004] Technical solution: To achieve the purpose of the present invention, the technical solution adopted by the present invention is: a cross-project software defect prediction method based on hybrid project adversarial learning, comprising the following steps:
[0005] S1: Obtain a cross-project software defect dataset and divide it into a training set and a test set. The training set includes the source project and the target project, and the test set includes the target project. The source project has labeled instances, and the target project has unlabeled instances.
[0006] S2: Initialize a random scale factor and mix the source items and target items in the training dataset according to the scale factor;
[0007] S3: Extract features from the source items and target items in the training dataset respectively;
[0008] S4: Mix the features extracted from different projects according to the proportional factor;
[0009] S5: The extracted features are input into the generator of the adversarial network model respectively, and the generator reconstructs the features according to the input features;
[0010] S6: The discriminator of the adversarial network model makes a true or false judgment on the reconstructed features and the original input features;
[0011] S7: The adversarial network model is trained according to the discriminator loss function, the generator loss function and the supervised contrastive learning loss function. The output of the model is 0 or 1. The model output 1 indicates that it is a defective instance.
[0012] S8: After the network converges, software defect prediction is performed on the instances in the test set.
[0013] Furthermore, the cross-project software defect dataset of S1:
[0014] Source item characteristics are represented as The target item characteristics are expressed as
[0015] where x s ,y s Represents the source project and its corresponding label, Represents the i-th instance in the source project and its corresponding label, n s Indicates the number of source project instances; x t Indicates the target project, represents the i-th instance in the target project, n t Indicates the number of target project instances.
[0016] Further, the S2 includes:
[0017] Initialize the scale factor λ, which follows the β distribution and has a value between 0 and 1;
[0018] The source items and target items in the training data set are mixed according to the proportional factor to obtain a mixed item feature representation: The subscript i represents the i-th instance.
[0019] Further, the S3 includes:
[0020] S3.1: Construct a three-layer fully connected neural network as the feature mapping network of the source project and the target project. After the feature mapping network, the representations of the source project and the target project features are obtained as follows: The similarity information within the project is mined through the feature mapping network, and then The input classifier is used for category judgment, that is, defect prediction. The classifier outputs 0 or 1, where 1 represents a defect.
[0021] Further, Input the classifier to make category judgment, that is, to make defect prediction, including:
[0022] S3.1.1: The classifier consists of a two-layer fully connected neural network. Let the network mapping function be C(·). The cross entropy loss L is defined on the source items. ce Conduct learning and predict defects for different software instances by training classifiers;
[0023] Cross entropy loss function L ce Defined as
[0024]
[0025] Where E represents expectation, P s Represents the data distribution of the source project;
[0026] S3.1.2: Using supervised contrastive learning loss function L sc Using the label information in the source project, we can mine the potential discriminant information between instances, increase the correlation between instances of the same category, and reduce the correlation between instances of different categories in the process of learning discriminant features;
[0027] Supervised Contrastive Learning Loss Function L sc Defined as
[0028]
[0029] in All indexes p in set P(i) belong to set A(i), and the label of the instance with index p in P(i) is the same as the label of the instance with index i; |P(i)| is the cardinality of P(i), which indicates the number of indexes p with the same label as index i; A(i)≡{1,...,n s}\{i} represents the set of source item indices that do not include the currently calculated index i; the symbol “·” represents the inner product, and τ∈R + is a scalar temperature parameter.
[0030] Furthermore, in S4, the features extracted from different projects are mixed according to the proportional factor; a feature representation of a source project and a target project is obtained by feature mixing.
[0031] Further, the S5 includes:
[0032] Before the feature reconstruction stage, we first define a one-hot class label vector l for both the source and target items and the mixed features. cla and a one-dimensional uncertainty compensation value l comp , specifically expressed as follows:
[0033]
[0034]
[0035]
[0036] In the formula, and Represent the class label vector of the source project and the one-dimensional uncertainty compensation value respectively. For the one-hot encoding corresponding to the source project label, 1 is in the vector The index in is location; and Represent the class label vector and one-dimensional uncertainty compensation value of the target item respectively; the one-hot encoding λ corresponding to the mixed item is located in the vector The index in is location; and Represent the class label vector and one-dimensional uncertainty compensation value of the mixed item respectively; l comp is a compensation value used to normalize the vector, l cla With l comp The sum is 1;
[0037] Generator N d According to the auxiliary feature l cla and l comp The feature representation for reconstruction is Where [·] represents the concatenation of vectors, Refers to the feature representation of any instance in the source project, target project, or mixed project after feature mapping or
[0038] Further, the S6 includes:
[0039] S6.1: Enhance the discriminative ability of the discriminator through adversarial training. The discriminator D consists of three branch sub-networks, namely the domain discrimination sub-network D under the discriminator dom , feature mapping subnetwork f D and classification subnetwork D cla ; The input of the three branch sub-networks is the reconstructed features; D domUsed to determine whether a feature is a reconstructed feature;
[0040] For data from different projects, the discriminator uses the following loss functions to guide learning:
[0041]
[0042]
[0043]
[0044] In the formula and denote the discrimination losses of source items, target items, and mixed items, respectively. and It is the source project and target project data generated by the generator;
[0045] S6.2: During training, the mixed features are mixed somewhere on the source and target item feature levels, and the domain classifier assigns a prediction score between 0 and 1; the domain classification loss Used to guide the domain classifier to output a soft score,
[0046]
[0047] Where P s and P t Represents the data distribution of the source and target items respectively, Labels representing mixed items;
[0048] S6.3: Introducing triplet loss Constrain the distance between the mixed items and the source items and the target items to help the domain classifier to distinguish the domain.
[0049]
[0050] where f D As a feature extractor of the discriminator D; [·] + =max(0,·) represents the hinge loss function; when λ>0.5, (a,p,n)=(x m ,x s ,x t ), otherwise (a,p,n)=(x m ,x t ,x s ), boundary threshold f tri (λ)=|2λ-1|.
[0051] Furthermore, when the generator restores the features of the source and target items, it ensures that the discriminant features of the data are not lost, and the classification loss of the source and target items is and They are defined as
[0052]
[0053] Where D cla is the classification subnetwork of the discriminator D, is the pseudo label output by the classifier for the instance in the target item, and It is the source and target project data generated by the generator.
[0054] Furthermore, the generation loss function, discriminant loss function and supervised contrastive learning loss function of the joint model use the minimax game strategy to optimize the generation model and the discriminant model; fix the generator and feature mapping network parameters, and update the discriminator parameters; fix the discriminator parameters, and update the classifier, generator and feature mapping network parameters.
[0055] Beneficial effects: Compared with the prior art, the technical solution of the present invention has the following beneficial technical effects:
[0056] (1) The present invention is based on the framework of generative adversarial network autoencoder, and combines the adversarial network to improve the reconstruction process within the autoencoder project. The strategy of minimax game makes it difficult to distinguish the original features and reconstructed features within the project. Combined with the supervised contrastive learning method, the discriminative features are better retained while reducing the distribution differences between different projects.
[0057] (2) The present invention introduces an intermediate project, which is a mixture of the source project and the target project according to a proportional factor between 0 and 1. In combination with a domain discriminant network, the generator's ability to reconstruct features is improved. While generating a domain-invariant representation, it makes it difficult to distinguish data between projects, effectively reducing the distribution difference of data across projects, and at the same time improving the feature extraction capability of the feature mapping network, so that the model has a stronger generalization ability. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] Figure 1 It is the overall framework diagram of the present invention. DETAILED DESCRIPTION
[0059] The technical solution of the present invention is further described below in conjunction with the accompanying drawings and embodiments.
[0060] See also Figure 1 The cross-project software defect prediction method based on hybrid project adversarial learning of the present invention comprises:
[0061] S1: Obtain cross-project software defect prediction datasets, including common datasets such as AEEEM and NASA; divide them into training sets and test sets. The source project is the training set, the target project serves as the training set and test set, the source project is a labeled dataset, and the target project is an unlabeled dataset.
[0062] For source items, the characteristics are represented as The target item characteristics are expressed as where x s ,y s Represents the source project and its corresponding label, Represents the i-th instance in the source project and its corresponding label, n s Indicates the number of source project instances; x t Indicates the target project, represents the i-th instance in the target project, n t Indicates the number of target project instances.
[0063] S2: Randomly initialize the scaling factor λ, which follows the β distribution and has a value between 0 and 1. Each time the iteration is performed, λ is randomly changed to obtain a mixed project feature representation: Where i represents the i-th instance.
[0064] S3: Extract features from the source items and target items in the training dataset respectively;
[0065] A three-layer fully connected neural network is constructed as the feature mapping network of the source and target items. The number of neurons in each layer is 50, 20, and 10 respectively. The activation function of each layer of the neural network adopts ReLU. After the feature mapping network, the feature representations of the source and target items are obtained as follows: The feature mapping network is used to further mine the similarity information within the project, and then The input classifier is used for category prediction. The classifier outputs 0 or 1, where 1 represents a defect. The classifier consists of a two-layer fully connected neural network. Let its network mapping function be C(·), the number of neurons in the first layer be 20, the activation function be ReLU, the number of neurons in the second layer be 2, and the activation function be sigmoid. It is implemented by defining the cross entropy loss L on the source items. ce To learn, we train the classifier to predict defects for different software instances. The loss function L ce Defined as
[0066]
[0067] Where E represents expectation, P sRepresents the data distribution of the source project. In order to effectively mine the potential discriminative information between instances, supervised contrastive learning loss is used to make full use of the label information in the source project. The supervised contrastive learning loss function L sc Defined as
[0068]
[0069] where A(i)≡{1,...,n s}\{i} represents the set of source item indexes, but does not include the currently calculated index i; All indices p in set P(i) belong to set A(i), and the label of the instance with index p in P(i) is the same as the label of the instance with index i; |P(i)| is the cardinality of P(i), which indicates the number of indices p that have the same label as index i; the symbol “·” indicates the inner (dot) product, and τ∈R + is a scalar temperature parameter, τ = 0.05 in this invention. sc This increases the correlation between instances of the same category and reduces the correlation between instances of different categories, which will help the model to mine discriminative feature information and improve prediction accuracy.
[0070] S4: Based on the scale factor λ initialized at each iteration, feature level mixing is performed to obtain a feature representation of a mixture of source and target items.
[0071] S5: Before the feature reconstruction stage, we first define a one-hot category label vector l for the two items and the mixed features. cla and a one-dimensional uncertainty compensation value l comp , which is specifically expressed as follows:
[0072]
[0073]
[0074]
[0075] In the formula, and Represent the class label vector of the source project and the one-dimensional uncertainty compensation value respectively. For the one-hot encoding corresponding to the source project label, 1 is in the vector The index in is location; and Represent the class label vector and one-dimensional uncertainty compensation value of the target item respectively; the one-hot encoding λ corresponding to the mixed item is located in the vector The index in is location, and Represent the class label vector and one-dimensional uncertainty compensation value of the mixed item respectively; l comp is a compensation value used to normalize the vector, l cla With l comp The sum is 1; then, generator N d According to the auxiliary feature l cla and l comp The feature representation for reconstruction is Where [·] represents the concatenation of vectors, It can be the feature representation of any instance in the source project after feature mapping Or the feature representation of any instance in the target project after feature mapping Or the feature representation of any instance in the mixed project after feature mapping
[0076] S6: Enhance the discriminative ability of the discriminator through adversarial training. The discriminator D is composed of three branch sub-networks, namely domain discrimination sub-network D dom , feature mapping subnetwork f D and classification subnetwork D cla The input of the three branch sub-networks is the reconstructed features. The front part of the discriminator D is a common sub-network consisting of two fully connected layers. The number of neurons in the first layer is 64, and the number of neurons in the second layer is 32. LeakyReLu is used as the activation function in each layer. dom It consists of a fully connected layer with 1 neuron, sigmoid activation function, and branch D dom Used to determine whether the feature has been reconstructed. For data from different projects, the discriminator uses the following loss functions to guide learning:
[0077]
[0078]
[0079]
[0080] In the formula and denote the discrimination losses of source items, target items, and mixed items, respectively. and It is the source and target project data generated by the generator.
[0081] During training, the hybrid features are mixed somewhere on the source and target item feature levels, and the domain classifier assigns a prediction score between 0 and 1.
[0082] Domain Classification Loss Used to guide the domain classifier to output a soft score, where P s and P t Represents the data distribution of source and target items respectively, Indicates the label of mixed items. A hard score means that a traditional domain classifier can only output 0 or 1 and cannot accurately reflect other situations. A soft score can output any value between 0 and 1. The use of soft scores expands the discrimination range of the domain classifier, making it easier to capture the current domain situation and enhancing the discrimination ability of the domain classifier.
[0083] In order to further constrain the distance between the mixed items and the source items or target items to help the domain classifier perform domain discrimination, the triplet loss is introduced where f D As the feature extractor of the discriminator D, it is composed of a one-layer fully connected network with 20 neurons. [·] + =max(0,·) represents the hinge loss function; when λ>0.5, (a,p,n)=(x m ,x s ,x t ), otherwise (a,p,n)=(x m ,x t ,x s ), a flexible way to calculate the boundary threshold f tri (λ)=|2λ-1|.
[0084] S7: Classification loss and To ensure the category consistency between the reconstructed features and the input features, the classification loss Defined as Defined as Where D cla It is the classification subnetwork of the discriminator D, which consists of a layer of fully connected neural network with 2 neurons. It is the pseudo label output by the classifier for the instance in the target project. When the generator restores the features of the source project and the target project, and The loss is used to ensure that the discriminative features of the data are not lost.
[0085] S8, the generator loss function, the discriminator loss function and the supervised contrastive learning loss function of the joint network model, considering that the optimization goals of the generator and the discriminator are opposite, the maximum and minimum game strategy is used for optimization. The game strategy is implemented using the stochastic gradient descent optimization algorithm, and the Adam optimizer of pytorch is used in the present invention.
[0086] For the parameters θ of the discriminator, generator, classifier, and feature mapping network D , θ C and θ e The optimization process is as follows:
[0087] Fix the generator and feature map network parameters and update the discriminator parameters:
[0088]
[0089] Fix the discriminator parameters and update the classifier, generator, and feature map network parameters:
[0090]
[0091]
[0092]
[0093] in and Represent the parameters θ D ,θ C , and The gradient of ω and It is a hyperparameter, which indicates the weight value given to the loss to balance the importance of the loss, and r is the learning rate.
[0094] In one embodiment, the minimum training batch size is 256, the learning rate r is 0.005, and the two weight hyperparameters ω and It is set to 0.01 through experiments.
[0095] S9: After the network converges, software defects are predicted for the instance of the target project. The output of the model is 0 or 1. The model output of 1 indicates that it is a defective instance.
Claims
1. A cross-project software defect prediction method based on hybrid project adversarial learning, characterized in that: The steps include: S1: Obtain a cross-project software defect dataset and divide it into a training set and a test set. The training set includes the source project and the target project, and the test set includes the target project. The source project has labeled instances, and the target project has unlabeled instances. Source item characteristics are represented as The target item characteristics are expressed as x s ,y s Represents the source project and its corresponding label, Represents the i-th instance in the source project and its corresponding label, n s Indicates the number of source project instances, x t Indicates the target project, represents the i-th instance in the target project, n t Indicates the number of target project instances; S2: Initialize the scaling factor λ, which follows the β distribution and has a value between 0 and 1; The source items and target items in the training data set are mixed according to the proportional factor to obtain a mixed item feature representation: Where the subscript i represents the i-th instance; S3: Extract features from the source items and target items in the training dataset respectively; S3.1: Construct a three-layer fully connected neural network as the feature mapping network of the source project and the target project. After the feature mapping network, the representations of the source project and the target project features are obtained as follows: The similarity information within the project is mined through the feature mapping network, and then Input the classifier to make category judgment, that is, to predict defects. The classifier outputs 0 or 1, where 1 represents a defect. S3.1.1: The classifier consists of a two-layer fully connected neural network. Let the network mapping function be C(·). The cross entropy loss L is defined on the source items. ce Conduct learning and predict defects for different software instances by training classifiers; Cross entropy loss function L ce Defined as Where E represents expectation, P s Represents the data distribution of the source project; S3.1.2: Using supervised contrastive learning loss function L sc Using the label information in the source project, we can mine the potential discriminant information between instances, increase the correlation between instances of the same category, and reduce the correlation between instances of different categories in the process of learning discriminant features; Supervised Contrastive Learning Loss Function L sc Defined as in All indexes p in set P(i) belong to set A(i), and the label of the instance with index p in P(i) is the same as the label of the instance with index i; |P(i)| is the cardinality of P(i), which indicates the number of indexes p with the same label as index i; A(i)≡{1,...,n s }\{i} represents the set of source item indices that do not include the currently calculated index i; the symbol "·" represents the inner product, and τ∈R + is a scalar temperature parameter; S4: Mix the features extracted from different projects according to the proportional factor; S5: The extracted features are input into the generator of the adversarial network model respectively, and the generator reconstructs the features according to the input features; Before the feature reconstruction stage, we first define a one-hot class label vector l for both the source and target items and the mixed features. cla and a one-dimensional uncertainty compensation value l comp , specifically expressed as follows: In the formula, and Represent the class label vector of the source project and the one-dimensional uncertainty compensation value respectively. For the one-hot encoding corresponding to the source project label, 1 is in the vector The index in is location; and Represent the class label vector and one-dimensional uncertainty compensation value of the target item respectively; the one-hot encoding λ corresponding to the mixed item is located in the vector The index in is location; and Represent the class label vector and one-dimensional uncertainty compensation value of the mixed item respectively; l comp is a compensation value used to normalize the vector, l cla With l comp The sum is 1; Generator N d According to the auxiliary feature l cla and l comp The feature representation for reconstruction is Where [·] represents the concatenation of vectors, Refers to the feature representation of any instance in the source project, target project, or mixed project after feature mapping or S6: The discriminator of the adversarial network model makes a true or false judgment on the reconstructed features and the original input features; S6.1: Enhance the discriminative ability of the discriminator through adversarial training. The discriminator D consists of three branch sub-networks, namely the domain discrimination sub-network D under the discriminator dom , feature mapping subnetwork f D and classification subnetwork D cla ; The input of the three branch sub-networks is the reconstructed features; D dom Used to determine whether a feature is a reconstructed feature; For data from different projects, the discriminator uses the following loss functions to guide learning: In the formula and denote the discrimination losses of source items, target items, and mixed items, respectively. and It is the source project and target project data generated by the generator; S6.2: During training, the mixed features are mixed somewhere on the source and target item feature levels, and the domain classifier assigns a prediction score between 0 and 1; the domain classification loss Used to guide the domain classifier to output a soft score, Where P s and P t Represents the data distribution of source and target items respectively, Labels representing mixed items; S6.3: Introducing triplet loss Constrain the distance of the mixed items to the source items and target items to help the domain classifier to distinguish the domain. where f D As a feature extractor of the discriminator D; [·] + =max(0,·) represents the hinge loss function; when λ>0.5, (a,p,n)=(x m ,x s ,x t ), otherwise (a,p,n)=(x m ,x t ,x s ), boundary threshold f tri (λ) = |2λ-1|; S7: The adversarial network model is trained according to the discriminator loss function, the generator loss function and the supervised contrastive learning loss function. The output of the model is 0 or 1. The model output 1 indicates that it is a defective instance. S8: After the network converges, software defect prediction is performed on the instances in the test set.
2. The cross-project software defect prediction method based on hybrid project adversarial learning according to claim 1 is characterized in that: S4, mixing the features extracted from different projects according to the proportional factors; By mixing features, we can get a mixed feature representation of the source item and the target item.
3. The cross-project software defect prediction method based on hybrid project adversarial learning according to claim 1 is characterized in that: When the generator restores the features of the source and target items, it ensures that the discriminant features of the data are not lost, and the classification loss of the source and target items and They are defined as Where D cla is the classification subnetwork of the discriminator D, is the pseudo label output by the classifier for the instance in the target item, and It is the source and target project data generated by the generator.
4. The cross-project software defect prediction method based on hybrid project adversarial learning according to any one of claims 1 to 3, characterized in that: The generation loss function, discriminant loss function and supervised contrastive learning loss function of the joint model use the minimax game strategy to optimize the generation model and the discriminant model; fix the generator and feature mapping network parameters, and update the discriminator parameters; fix the discriminator parameters, and update the classifier, generator and feature mapping network parameters.
