A software defect prediction method based on local characteristics and long-range dependencies of code, and a computer-readable storage medium.
By employing CNN and BiLSTM to analyze local features and long-range dependencies, the method addresses the inconsistency of conventional methods, achieving superior predictive performance in software defect detection.
Patent Information
- Authority / Receiving Office
- JP · JP
- Patent Type
- Applications
- Current Assignee / Owner
- HANGZHOU DIANZI UNIV
- Filing Date
- 2025-11-25
- Publication Date
- 2026-06-24
AI Technical Summary
Conventional software defect prediction methods struggle with inconsistent performance across projects due to reliance on static indicators and insufficient capture of local features and long-range dependencies, leading to decreased model performance, especially in large-scale data analysis.
A method utilizing a CNN to extract local features and a two-layer BiLSTM to capture long-range dependencies, combined with traditional and semantic features, to enhance predictive accuracy through improved understanding of code structure and logic.
The method significantly improves defect prediction accuracy by effectively extracting local features and capturing complex long-range dependencies, enhancing model adaptability and reliability in diverse software projects.
Smart Images

Figure 2026103835000001_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical fields of software security and machine learning, and specifically relates to a software defect prediction method based on local features and long-distance dependency relationships of code.
Background Art
[0002] Software defect prediction is an important process in the software development and maintenance processes. By predicting and identifying potential software defects, the development team can correct these defects before releasing the software, thereby improving the quality of the final product, helping project managers identify parts or modules with a high likelihood of problems, thereby allocating limited testing and development resources more effectively, ensuring sufficient attention is paid to important parts, identifying and resolving problems in advance, reducing backtracking in the development process, thereby shortening the development cycle, contributing to identifying high-risk software components, and enabling the project team to take preventive measures to avoid potential risks from turning into actual problems. This is not only related to the quality and reliability of software but also the key to improving development efficiency and reducing costs.
[0003] In some studies, predictors are constructed using static indicators designed manually. These traditional features mainly focus on the statistical features of programs, such as Halstead features based on operators and operands, McCabe features based on dependencies, and CK features of object-oriented programs. However, traditional features that perform well in one project may not perform well in other projects. Also, conventional defect prediction methods are complex and have insufficient performance when processing large-scale data.
[0004] In addition to features represented by a series of code metrics, source code, as a unique text, contains rich syntactic and semantic information, and ignoring contextual information can significantly impact program comprehension. An Abstract Syntax Tree (AST) is an abstract representation of the syntactic structure of source code, allowing program structure to be described in tree form, with each node representing a single structure or symbol in the source code. While some researchers have verified that a combination of semantic and traditional features outperforms using traditional features alone in defect prediction, the chosen prediction model, while capable of capturing deep features, is insufficient to capture all semantic information in the AST. Local features provide detailed information, and long-range dependencies ensure the model understands the overall structure and logic, especially with respect to long-range dependencies and complex control flows. The lack of local features and long-range dependencies can render the model ineffective when analyzing complex sequences, resulting in a significant decrease in the model's overall performance. [Overview of the Initiative] [Problems that the invention aims to solve]
[0005] In response to the shortcomings of conventional technologies, the present invention proposes a software defect prediction method based on local features and long-range dependencies of code. It deeply analyzes the code structure using a CNN, extracts local features, and captures long-range dependencies using a two-layer BiLSTM. This allows for a better understanding and extraction of higher-level contextual information, enhancing the model's expressive power and thereby improving the accuracy and reliability of predictions. [Means for solving the problem]
[0006] A method for predicting software defects based on local characteristics and long-range dependencies of code, specifically comprising the following steps:
[0007] Step 1: Extract traditional and semantic features from the source code programs and defect reports of the original software defect dataset, label the defects, and perform imbalance processing and standardization.
[0008] Step 2: Using a time-convolutional neural network (TCNN), the extracted semantic features are rudimentarily trained and selected to preserve potential, defect-related features, thereby improving the accuracy and robustness of subsequent software defect predictions.
[0009] In step 3, the semantic features retained in step 2 are merged with traditional features to form a feature matrix of the target dimension.
[0010] Step 4 uses one CNN layer to extract local features from the target dimension feature matrix obtained in Step 3, and then two consecutive BiLSTM layers capture long-range dependencies in the time sequence, helping the model better understand and extract higher-level contextual information and enhancing the model's representational power. Finally, a dense layer is used to integrate the features from the second BiLSTM layer, a weighted sum is performed on this information, and the binary classification result is ultimately output using a sigmoid activation function.
[0011] Step 5: Code defect prediction can be viewed as a binary classification problem; therefore, a weighted binary cross-entropy loss function is used to adjust the weights of positive class samples, i.e., defective samples, thereby improving the model's ability to distinguish between this class of samples. Simultaneously, the Adam optimizer is selected and the model is trained.
[0012] Step 6 extracts semantic and traditional features from the source code program to be detected and its defect report. A time-convolutional neural network selects the semantic features, merges them with the traditional features, and then inputs this information into the model trained in Step 5 to output the defect prediction results. [Effects of the Invention]
[0013] Compared to conventional technology, the present invention has the following beneficial effects.
[0014] 1. CNNs effectively extract local features from input sequences through local sensing and weight sharing mechanisms. In code analysis, local features may include specific code modes, structures, syntax errors, etc., and the features extracted by the convolutional layer can reflect information such as the frequency of code use and structural complexity, helping the model identify potential defects.
[0015] 2. In code, function calls, variable scopes, etc., often span long distances, and this information is extremely important for defect prediction. The special structure of BiLSTM can capture long-range dependencies in time-sequence data. The first BiLSTM layer can learn local contextual information in the input sequence and simultaneously capture forward and backward dependencies. The second BiLSTM layer can help the model better understand and extract higher-level contextual information, enhancing the model's expressive power. Therefore, two consecutive BiLSTM layers can simultaneously consider past and future contextual information, and especially as the network depth increases, it can learn more complex and abstract feature representations and better model complex long-range dependencies, thereby improving the structure and logic of the code. To gain a comprehensive understanding and improve the model's adaptability to complex code logic.
[0016] 3. The representation learned by the first BiLSTM layer is based on the original input features, and the second BiLSTM layer is based on the output of the first layer. This layer-by-layer information fusion contributes to capturing richer semantic information. Particularly in sequencing tasks, the second BiLSTM layer further enhances the transmission of information flow, thereby improving the model's understanding of the sequence. By combining the local features extracted by the CNN with the long-range dependencies captured by the BiLSTM, the model can understand the input data more comprehensively. This combination allows the model not only to focus on single features but also to consider the dynamic relationships between features and their temporal changes. [Brief explanation of the drawing]
[0017] [Figure 1] This is a flowchart of a software defect prediction method based on local code features and long-range dependencies. [Figure 2] This is a schematic diagram of the structure of the model constructed in the example to capture local features and long-range dependencies of the code. [Modes for carrying out the invention]
[0018] The present invention will be further described below with reference to the drawings.
[0019] As shown in Figure 1, a software defect prediction method based on local code features and long-range dependencies enhances its ability to predict software defects by capturing local code features and long-range dependencies. Specifically, it includes the following steps:
[0020] Step 1: In this embodiment, the camel, Jedit4, log4j1, lucene, poi, synapse, xalan, and xerces datasets are used as the original software defect dataset, and the module instance names are extracted from the traditional feature files. The source code is analyzed to extract the AST (Abstract Syntax Tree) sequence, and then the AST sequence is converted into an integer-valued vector Token, so that the subsequent model can use the semantic features in numerical form.
[0021] Extract the semantic token sequence and traditional features from the traditional feature files and encoded Tokens, and attach defect labels.
[0022] The traditional features include statistical data such as code metrics, defect history, development process, and version management, and the semantic features include information such as program structure, data flow, design mode, and code similarity.
[0023] Finally, perform imbalance processing and normalization processing. Use the RandomOverSampler method for oversampling on the xalan dataset, and use the SMOTE method for oversampling on the remaining datasets.
[0024] Step 2: Utilize the Temporal Convolutional Neural Network (TCNN) to initially train and select the extracted semantic features. The TCN can process time-dependent data, enhance the stability of prediction, and maintain efficient performance even when there are noises or deficiencies in the data. In each training batch, perform forward propagation on the input semantic token sequence, output and retain the potential features related to defects, and select the binary cross-entropy loss Loss BCE to calculate the loss value.
[0025]
Number
[0026]
number
[0027]
number
[0028] The model parameters are optimized using the Adam optimizer, adjusting the learning rate and other hyperparameters. After training, the retained semantic features are stored as a NumPy array.
[0029] Step 3: Using hstack, horizontally merge the semantic and traditional features to form a feature matrix of the target dimension containing 180-dimensional semantic features, 20-dimensional traditional features, and 1-dimensional labels.
[0030] Step 4, as shown in Figure 2, construct a single model including a CNN layer and a double BiLSTM layer, which will be used to capture local features and long-range dependencies of the code. The specific steps are as follows:
[0031] s4.1, a convolutional layer (Conv1D) performs a one-dimensional convolution operation on the input feature matrix of the target dimension to extract local features. In code, for example, certain code fragments such as syntactic structures and common function calls may be identifiable features, and the convolutional kernel can capture specific modes or features by sliding across the input data, and these local features can be learned to improve the accuracy of classification.
[0032] In section s4.2, batch normalization is used to standardize the output of the convolutional layer, generate a standardized feature map, and effectively reduce the problem of internal covariate shift by adjusting and scaling the output of the activation function. Therefore, batch normalization can not only accelerate the model training process but also improve the model's adaptability to different data distributions and the overall stability of the network.
[0033] In s4.3, the Max Pooling 1D layer further processes the feature map output by the batch normalization layer, reducing its dimensionality while retaining the most prominent features to generate a pooled feature map. By retaining the maximum value, the Max Pooling layer allows the model to focus its attention on the strongest local features, thereby reducing unwanted noise.
[0034] In step 4.4, the pooled feature map is input to the first BiLSTM layer, which can transmit information over relatively long time steps, remember important information from the previous sequence, and influence the subsequent output, thereby capturing the time dependencies in the sequence. The second BiLSTM layer is used to better understand and extract higher-level contextual information based on the output of the first BiLSTM layer, thereby enhancing the model's expressive power.
[0035] In sequencing tasks, the second BiLSTM layer further enhances the transfer of information flow, thereby improving the model's understanding of the sequence. Thus, by processing different levels of features with two consecutive BiLSTM layers, the model can better model complex long-range dependencies and enhance its generalization ability. Furthermore, double BiLSTM layers allow for better gradient transfer.
[0036] In step 4.5, one dense layer is used to integrate the features output by the second BiLSTM layer, a weighted sum is performed on the information output by the second BiLSTM layer, and finally the binary classification result is output using a sigmoid activation function.
[0037] Step 5: To further address the data imbalance problem, a weighted binary cross-entropy loss function (Loss) is used during the model training phase.
[0038]
number
[0039] Here, N represents the sample size, and y true,i represents the true label of the i-th sample, and y true,i =1 indicates that the sample is in the positive class, i.e., a defective sample, and y true,i =0 indicates that the sample belongs to the negative class, i.e., it is a defect-free sample. pred,i `pos_weight` represents the predicted probability of the i-th sample output by the model, and its value ranges from 0 to 1. `pos_weight` represents the weight value corresponding to the positive class sample, and by adjusting the weight `pos_weight` of the positive class sample, the model's ability to distinguish less frequent classes can be improved.
[0040] The Adam optimizer is selected to train the model, and 5-fold cross-validation is used to ensure the model has good generalization ability and stability. After each split, the model is trained using the currently split training data and evaluated on the validation data. ReduceLROnPlateau is used to fine-tune the model parameters by reducing the learning rate when the validation loss stops improving. ModelCheckpoint is used to save the model that performs best in terms of validation loss, and if the validation loss does not improve within a certain number of epochs, training is terminated early.
[0041] To demonstrate the effectiveness of this method, comparative experiments were conducted with conventional methods commonly used in related technologies. The evaluation metric selected was the F1 score, with a higher F1 score indicating better predictive performance of the model. The experimental results are shown in Table 1.
[0042] [Table 1]
[0043] Compared to conventional techniques, the F1 score of this method has been significantly improved. In particular, in the Jedit4 project, the F1 score of this method reached 0.9137, which is much higher than DP-GCN's 0.64 and ECNN's 0.711, demonstrating its efficient adaptability to defect prediction tasks. Furthermore, in the log4j1 project, this method achieved a near-perfect score of 0.9974, realizing nearly perfect and accurate defect prediction, demonstrating its superior predictive ability.
[0044] Furthermore, experimental results revealed that while ECNN models perform well in some projects, they focus on using long-range dependencies for defect prediction, lacking sufficient attention to the hierarchical structure features of the code. On the other hand, DP-GCN shows a certain advantage when processing graph-structured data, and PSFM excels at extracting information from the semantic level of the code. However, this method demonstrates high robustness and efficiency in software projects of various sizes and types, indicating that it can accurately identify potential defect modes and achieve high prediction accuracy.
Claims
1. A software defect prediction method based on local characteristics and long-range dependencies of code, specifically, Step 1 involves extracting traditional and semantic features from the source code program and defect report, and assigning defect labels. Step 2 involves selecting extracted semantic features using a pre-trained time-convolutional neural network, Step 3 involves merging the semantic features selected in Step 2 with traditional features to form a feature matrix of the target dimension, Step 4 involves using one CNN layer to extract local features from the feature matrix of the target dimension obtained in step 3, then capturing long-range dependencies in the time sequence using two consecutive BiLSTM layers, then integrating the features output by the second BiLSTM layer using a dense layer, and finally outputting the predicted defect prediction result using a sigmoid activation function, the specific process being: s4.1, A convolutional layer performs a one-dimensional convolution operation on the input feature matrix of the target dimension to extract local features. s4.2, The output of the convolutional layer is standardized by the batch normalization layer, and a standardized feature map is generated. s4.3, The maximum pooling layer further processes the feature map output by the batch normalization layer, reducing the dimensionality of the feature map while retaining the most prominent features, and generating a pooled feature map. s4.4, the pooled feature map is input to the first BiLSTM layer to capture the time dependencies in the sequence, and the second BiLSTM layer further enhances the transmission of information flow based on the output of the first BiLSTM layer. Step 4 involves using one dense layer to integrate the features output by the second BiLSTM layer, performing a weighted sum on the information output by the second BiLSTM layer, and finally outputting the binary classification result using the sigmoid activation function. Step 5 involves comparing the defect prediction results predicted in Step 4 with the defect labels and training the model parameters in Step 4, using the following weighted binary cross-entropy loss function Loss: [Math 1] Here, N represents the number of samples to input into the model, and y true,i represents the true label of the i-th sample, and y true,i = 1 indicates that the sample is in the positive class, i.e., a defective sample, and y true,i = 0 indicates that the sample is in the negative class, i.e., a defect-free sample, and y pred,i represents the predicted probability of the i-th sample output by the model, and pos_weight is the weight value corresponding to the positive class sample, in step 5, A software defect prediction method based on local features and long-range dependencies of code, comprising: step 1 extracting semantic features and traditional features from a source code program to be detected and its defect report; step 2 selecting semantic features using a time-convolutional neural network pre-trained in step 2, further merging them with traditional features, then inputting the results into a model trained in step 5, and outputting defect prediction results.
2. The aforementioned traditional features include code metrics, defect history, development process, and version control details, while the aforementioned semantic features include program structure, data flow, design mode, and code A method for predicting software defects based on local features and long-range dependencies of code, as described in claim 1, characterized in that it includes code similarity information.
3. A software defect prediction method based on local features and long-range dependencies of a code according to claim 1, characterized in that it uses the RandomOverSampler method or the SMOTE method to perform oversampling on extracted features and performs data imbalance processing and standardization processing.
4. A software defect prediction method based on local features and long-range dependencies of code according to claim 1, characterized in that it uses a time-convolutional neural network to predict the probability of influence of different semantic features on defect prediction results.
5. Binary cross-entropy loss (Loss) BCE Use this to calculate the loss value of the time-convolutional neural network. [Math 2] A method for predicting software defects based on local features and long-range dependencies of code, as described in claim 4, characterized by optimizing the parameters of a time-convolutional neural network using the Adam optimizer.
6. A computer-readable storage medium that stores a computer program, and when the computer program is executed on a computer, causes the computer to perform the method according to any one of claims 1 to 5.