A neural network-based functional code evaluation method and device

By extracting the seven-dimensional feature vector of functional code and using a neural network model to evaluate code similarity, the problem of insufficient flexibility and intelligence in functional code detection in existing technologies is solved. This enables early detection and assessment of code risks during the development phase, reducing the cost of handling code defects.

CN117272328BActive Publication Date: 2026-11-20中国邮政储蓄银行股份有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311463509.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-11-06
Publication Date
2026-11-20
Estimated Expiration
2043-11-06

AI Technical Summary

Technical Problem

Existing technologies lack flexibility and universality for specific functions within an enterprise in functional code detection, have low levels of intelligence, make it difficult to detect security flaws during the development phase, and have insufficient application of neural network technology in the evaluation of specific functional codes.

Method used

By extracting seven-dimensional feature vectors from standard code and R&D personnel's code, and using a neural network model to train and evaluate the similarity of the code, the digitization and characterization of code with specific functions can be achieved. The trained neural network model can then be used to evaluate the probability that the code under test can achieve the corresponding function.

Benefits of technology

It improves the intelligence level of functional code detection, detects code risks in advance, reduces defect repair costs, promotes the collaboration and integration of code evaluation and R&D personnel, and ensures the high-quality development of software R&D products.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117272328B_ABST
    Figure CN117272328B_ABST
Patent Text Reader

Abstract

The application discloses a neural network-based function code evaluation method and device. The method comprises the following steps: obtaining a target code segment from a standard code for implementing a specific function, and obtaining a sample code segment from an implementation code of the corresponding function written by a developer; through the same feature extraction algorithm, feature data of program capacity and key program elements in the code segments are extracted to form seven-dimensional feature vectors, and after normalization processing, a label feature vector and a sample feature vector are obtained; a neural network model is constructed, the label feature vector is used as a label value, and a large number of sample feature vectors are used as input data for training; and then the trained neural network model is used to evaluate the probability that the to-be-tested code can implement the corresponding function. The application not only realizes digitization and characterization of the specific function code, but also applies the neural network technology to detection of the function code, so that the function code evaluation can be advanced to the development stage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and specifically to a method and apparatus for evaluating functional codes based on neural networks. Background Technology

[0002] Currently, functional code testing primarily focuses on evaluating code writing standards and potential security vulnerabilities, failing to assess the implementation of specific functions within an enterprise. This is mainly reflected in the following three aspects:

[0003] (1) Currently, the detection of code written by R&D personnel to implement specific functions is mostly concentrated after functional testing. During the development stage, there is only manual code review, which is not conducive to the early detection of security defects and the level of intelligence of detection is low.

[0004] (2) The extraction and detection of specific function code feature information currently often uses program capacity method, machine code recognition method, etc., which lacks flexibility and universality and is not suitable for the detection of specific functions.

[0005] (3) In current artificial intelligence, neural network technology is often used in code similarity and vulnerability detection, but its application in code evaluation for specific functions such as enterprise internal security needs is relatively limited. Summary of the Invention

[0006] The purpose of this application is to propose a method and apparatus for evaluating functional codes based on neural networks to solve one or more of the problems mentioned above.

[0007] According to the first aspect of this application, a method for evaluating functional codes based on neural networks is proposed, comprising the following steps:

[0008] Obtain target code snippets of positive cases from standard code that implements a specific function, and obtain sample code snippets of positive cases from the implementation code of the corresponding function written by R&D personnel;

[0009] For the target code segment of the positive case and the sample code segment, the same feature extraction algorithm is used to extract the feature data of program capacity and key program elements in the code segment to form a seven-dimensional feature vector. After normalizing the seven-dimensional feature vector, the label feature vector and sample feature vector of the positive case that can be used for neural network model operation are obtained.

[0010] For the constructed neural network model, the label feature vector of the positive case is used as the label value, and a large number of sample feature vectors of the positive case are used as input data to train the neural network model until a trained neural network model is obtained.

[0011] The test code for the corresponding function written by the R&D personnel is used to extract its feature vector through the same feature extraction algorithm and then normalized before being input into the trained neural network model. The cosine similarity between the output vector of the model and the label feature vector of the positive case is calculated, and the probability that the test code can achieve the corresponding function is evaluated based on the cosine similarity.

[0012] According to a second aspect of this application, a function code evaluation device based on a neural network is proposed, comprising:

[0013] The code snippet acquisition unit is used to obtain target code snippets of positive cases from standard code that implements a specific function, and to obtain sample code snippets of positive cases from the implementation code of the corresponding function written by the R&D personnel.

[0014] The feature vector extraction unit is used to extract feature data of program capacity and key program elements from the target code segment of the positive case and the sample code segment respectively through the same feature extraction algorithm to form a seven-dimensional feature vector. After normalizing the seven-dimensional feature vector, the corresponding label feature vector and sample feature vector of the positive case can be obtained for the neural network model to operate.

[0015] The network model training unit is used to train the constructed neural network model using the label feature vectors of the positive cases as label values ​​and a large number of sample feature vectors of the positive cases as input data until a trained neural network model is obtained.

[0016] The functional code evaluation unit is used to extract the feature vector of the code to be tested for the corresponding function written by the R&D personnel through the same feature extraction algorithm, normalize it, and then input it into the trained neural network model. The unit calculates the cosine similarity between the output vector of the model and the label feature vector of the positive case, and evaluates the probability that the code to be tested can achieve the corresponding function based on the cosine similarity.

[0017] According to a third aspect of this application, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to perform the aforementioned method.

[0018] The above-described technical solutions adopted in the embodiments of this application can achieve the following beneficial effects:

[0019] This application embodiment obtains target code fragments and sample code fragments of positive cases. Using the same feature extraction algorithm, it extracts feature data of program capacity and key program elements from the obtained code fragments to form seven-dimensional feature vectors. After normalizing the seven-dimensional feature vectors, it obtains the label feature vectors of positive cases and the sample feature vectors for neural network model computation. This achieves the digitization and characterization of specific functional code, improves the flexibility and universality of feature vector extraction, and ensures the accuracy of feature vector extraction, facilitating feature analysis in practical applications. By applying neural network technology to the detection of functional code, the label feature vectors of positive cases and the sample feature vectors are used to train a neural network model. The trained neural network model is then used to detect the code to be tested written by developers for the corresponding function, evaluating the probability that the code to be tested can achieve the corresponding function. This helps developers to evaluate functional code in advance during the development stage, identify potential code risks early, reduce the cost of fixing code defects, and promote collaboration between code evaluators and developers. It also helps enterprises evaluate the effectiveness of functional code implementation during software development in advance, reduce the cost of handling code defects, improve the intelligence level of functional code detection, strengthen code risk management during the development stage, and ensure the high-quality development of enterprise software products. Attached Figure Description

[0020] The accompanying drawings, which form part of this application, are used to provide a further understanding of this application. The illustrative embodiments and descriptions of this application are used to explain this application and do not constitute an undue limitation of this application. In the drawings:

[0021] Figure 1 A flowchart illustrating the functional code evaluation method based on neural networks according to an embodiment of this application is shown;

[0022] Figure 2 shows a schematic diagram of a security requirement function code snippet as an example of an embodiment of this application;

[0023] Figure 3 A schematic diagram of the feature vector extraction process in an embodiment of this application is shown;

[0024] Figure 4 A schematic diagram illustrating the neural network model training and detection process in an embodiment of this application is shown;

[0025] Figure 5 The diagram illustrates the data flow processing logic of sample and label feature vectors during neural network model training in an embodiment of this application.

[0026] Figure 6 A schematic diagram of the BP neural network model constructed in an embodiment of this application is shown;

[0027] Figure 7A schematic diagram of matrix operations for the input, output, and model parameters of the BP neural network model in an embodiment of this application is shown.

[0028] Figure 8 The initial BP neural network model parameter table in the embodiments of this application is shown;

[0029] Figure 9 A table showing the characteristics of activation functions selected for training the BP neural network model in the embodiments of this application is provided.

[0030] Figure 10 The first set of training experiment results data of the BP neural network model in the embodiments of this application are shown;

[0031] Figure 11 The loss curve corresponding to the first group of training experiment results of the BP neural network model in the embodiments of this application is shown;

[0032] Figure 12 The second set of training experiment results data of the BP neural network model in the embodiments of this application is shown;

[0033] Figure 13 The third set of training experiment results data of the BP neural network model in the embodiments of this application is shown;

[0034] Figure 14 The loss curve corresponding to the third group of training experiment results of the BP neural network model in the embodiments of this application is shown;

[0035] Figure 15 The diagram shows the verification results of the BP neural network model detecting positive cases in the embodiments of this application;

[0036] Figure 16 The diagram shows the verification results of the BP neural network model detecting counterexamples in the embodiments of this application;

[0037] Figure 17 This invention illustrates a functional structure diagram of a neural network-based functional code evaluation device according to an embodiment of this application.

[0038] Figure 18 A schematic diagram of the structure of an electronic device according to an embodiment of this application is shown. Detailed Implementation

[0039] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.

[0040] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate for the embodiments of this application described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0041] Example 1

[0042] First, it should be understood that evaluating the code under test means assessing whether the code can perform a specific function, and this is usually achieved by directly evaluating the probability that the code under test can perform a specific function. The following section uses the implementation of internal enterprise security requirements as an example to illustrate the method of this application's embodiments in detail.

[0043] According to the first aspect of this application, embodiments of this application propose a functional code evaluation method based on neural networks, see [link to relevant documentation]. Figure 1 As shown, steps S1 to S4 are included:

[0044] S1: Obtain the target code snippet of the positive case from the standard code that implements the specific function, and obtain the sample code snippet of the positive case from the implementation code of the corresponding function written by the R&D personnel.

[0045] This step involves two aspects: firstly, exploring the standard code for implementing security requirements within the enterprise to obtain target code snippets for positive cases; and secondly, collecting a large number of sample code snippets for positive cases by gathering a large amount of code written by R&D personnel to implement the corresponding security requirements.

[0046] While developers may encounter some differences in code implementation due to the flexibility of syntax and programming conventions when writing code for specific functions, the differences in specific language code snippets for achieving those functions are minimal from a program characteristics perspective. Internal software development processes within enterprises typically have well-defined security requirements, and the development process must strictly adhere to the enterprise's internal security management system.

[0047] As shown in Figure 2, taking the security requirements of a company's 3DES encryption algorithm as an example, the textual description of the security requirements shown in Figure 2(a) is first obtained. Then, the textual security requirements are transformed into a standard Java code snippet for program implementation as shown in Figure 2(b). This Java code snippet is used as benchmark data to obtain a target code snippet for a positive case. By collecting a large number of Java code snippets written by R&D personnel to implement the functional requirements of this specific security requirement, a sample library of positive case program snippets is formed, resulting in a large number of sample code snippets for positive cases.

[0048] It should be noted that the embodiments in this application are only examples using the Java language, and the code snippets that can achieve the corresponding functions are not limited to a certain programming language.

[0049] S2, for the target code segment of the positive case and the sample code segment, the same feature extraction algorithm is used to extract the feature data of program capacity and key program elements in the code segment to form a seven-dimensional feature vector. After normalizing the seven-dimensional feature vector, the label feature vector and sample feature vector of the positive case that can be used for neural network model operation are obtained.

[0050] By studying the feature information of Java programs with specific functions, this application embodiment integrates program capacity and key program elements to form a seven-dimensional feature vector, which facilitates rapid detection of whether the program meets the security requirements during the development stage.

[0051] The program capacity is measured using the Halstead metric, a software engineering method for evaluating program complexity. Its principle is to count the number of operators and operands appearing in the program, using their frequency as the target to measure program capacity. Taking a Java program fragment P as an example, which consists of a finite number of combinations of operators and operands, let's assume the number of operand types in P is denoted as n1, the number of operator types as n2, the total number of operands in P as N1, and the total number of operators as N2. Then, the vocabulary size n, program length N, and program capacity V of P are calculated.

[0052] Furthermore, through research, the inventors discovered that four program elements in a Java program fragment—the number of assignment statements (a), loop structures (b), conditional structures (c), and functions (d)—are crucial for identifying specific functions implemented in a particular language. Therefore, this application innovatively combines information on Halstead vocabulary, program length, and program capacity with the aforementioned four program elements to construct a seven-dimensional feature vector that measures the characteristics of Java implementation code for security requirements in R&D.

[0053] That is, the seven-dimensional feature vector in this embodiment is: [program length N, vocabulary n, program capacity V, number of assignment statements a, number of loop structures b, number of conditional structures c, number of functions d], where it is assumed that: the number of operand types in the code segment is denoted as n1, the number of operator types is denoted as n2, the total number of operands is denoted as N1, and the total number of operators is denoted as N2, then:

[0054] Vocabulary size n = n1 + n2; Program length N = N1 + N2; Program capacity V = N * log2(n).

[0055] See Figure 3 For the target code fragment and sample code fragment of the positive case obtained in step S1, the same feature extraction algorithm is used, such as extracting the seven-dimensional feature elements from the Java code using a Python program, to form seven-dimensional feature vector data. Given that the values ​​of each element in the feature vector may differ significantly, to eliminate the influence of different dimensions among the feature elements, the extracted feature vector values ​​can be normalized by summing the summation ratio. That is, the feature vector values ​​of each element are summed, and the proportion of each feature vector value in the summation is calculated, so that the values ​​of each element of the vector fall within the range [0,1]. This completes the normalization process of the seven-dimensional feature vector, ultimately yielding the label feature vector and sample feature vector of the positive case that can be used for neural network model computation.

[0056] S3, for the constructed neural network model, use the label feature vector of the positive case as the label value, and use a large number of sample feature vectors of the positive case as input data to train the neural network model until a trained neural network model is obtained.

[0057] See Figure 4For the constructed neural network model, each training iteration inputs a sample feature vector of a positive case, and the labeled value is the label feature vector of the positive case. After the network model performs the operation, it outputs a result vector. The cosine similarity between the result vector and the label feature vector of the positive case is calculated. If the similarity evaluation error is greater than the set error threshold, the model parameters are adjusted and optimized to continue training, making the evaluation error of the neural network model smaller and smaller, until the evaluation error is less than the set error threshold and tends to converge. At this point, the neural network model is considered to have completed training, the training ends, the model parameters are saved, and a trained neural network model that can identify positive cases is obtained.

[0058] S4. The test code for the corresponding function written by the R&D personnel is used to extract its feature vector through the same feature extraction algorithm and normalized before being input into the trained neural network model. The cosine similarity between the output vector of the model and the label feature vector of the positive case is calculated, and the probability that the test code can achieve the corresponding function is evaluated based on the cosine similarity.

[0059] See also Figure 4 When verifying the code to be tested written by the R&D personnel for the corresponding function, the feature vector of the code to be tested is first extracted by the same feature extraction algorithm and normalized to obtain the feature vector to be tested. The feature vector to be tested is then input into the trained neural network model. After the model operates, a result vector is output. The cosine similarity between the result vector and the label feature vector of the positive case is calculated to determine the similarity between the two vectors. Then, the probability that the code to be tested can achieve the corresponding function is evaluated based on the cosine similarity.

[0060] In summary, the neural network-based functional code evaluation method of this application obtains target code segments and sample code segments of positive cases. Using the same feature extraction algorithm, it extracts feature data of program capacity and key program elements from the obtained code segments to form seven-dimensional feature vectors. After normalizing the seven-dimensional feature vectors, it obtains the label feature vectors and sample feature vectors of positive cases that can be used for neural network model computation. This achieves the digitization and characterization of specific functional codes, improves the flexibility and universality of feature vector extraction, and ensures the accuracy of feature vector extraction, facilitating feature analysis in practical applications. By applying neural network technology to functional codes... The detection method uses the label feature vectors and sample feature vectors of positive cases to train a neural network model. The trained neural network model is then used to detect the code to be tested written by the developers for the corresponding functions, and to evaluate the probability that the code to be tested can achieve the corresponding functions. This helps developers to evaluate functional code in advance during the development stage, discover potential code risks in advance, reduce the cost of fixing code defects, and promote the collaboration between code evaluators and developers. It also helps enterprises to evaluate the effectiveness of functional code implementation in the software development process in advance, reduce the cost of handling code defects, improve the intelligence level of functional code detection, strengthen code risk management during the development stage, and ensure the high-quality development of enterprise software R&D products.

[0061] It is understandable that evaluating the code for the function under test can also be achieved by assessing the probability that the code cannot achieve a specific function. Therefore, according to some embodiments of this application, the method of this application further includes:

[0062] Sample code snippets of counterexamples are obtained from the code written by the R&D personnel that cannot implement the corresponding function; the sample code snippets of the counterexamples are processed by the same feature extraction algorithm to extract their feature vectors and then normalized to obtain the sample feature vectors of the counterexamples;

[0063] The vector whose dot product with the label feature vector of the positive case is 0 is used as the label feature vector of the negative case.

[0064] For the constructed neural network model, the label feature vector of the countercase is used as the label value, and a large number of sample feature vectors of the countercase are used as input data to train the neural network model until a trained neural network model is obtained.

[0065] The test code for the corresponding function written by the R&D personnel is used to extract its feature vector through the same feature extraction algorithm and then normalized before being input into the trained neural network model. The cosine similarity between the output vector of the model and the label feature vector of the counterexample is calculated, and the probability that the test code cannot achieve the corresponding function is evaluated based on the cosine similarity.

[0066] In the above implementation, the same feature extraction algorithm can be used to extract the sample feature vectors of the negative cases that do not meet the specific functional requirements from the obtained sample code fragments of the negative cases. At this time, the sample feature vectors of the positive and negative cases and the label feature vector of the positive cases are available for subsequent training of the neural network model, but the standard code and label feature data of the negative case samples are not yet available. For a certain specific functional requirement, the developers can easily obtain its standard implementation code and the implementation code that meets its requirements. Code that does not meet the specific functional requirements can also be easily obtained, but it is difficult to define a certain code that does not meet the specific function as the standard non-compliant code. To overcome this problem, this application embodiment uses vector knowledge in mathematics to solve it. The dot product of two vectors is -1, which means that the two vectors are completely opposite. However, combined with the characteristics of actual software code, there is no case where the actual code feature element is negative. Therefore, the dot product value of two opposite vectors should be in the range of [0,1]. Accordingly, this application embodiment uses the vector whose dot product with the label feature vector corresponding to the standard code of the positive case is 0 as the label feature vector of the negative case for subsequent training of the neural network model to identify the negative cases.

[0067] At this point, during the feature extraction phase of the functional code, we have obtained the sample feature vector data of the positive and negative cases, as well as the label feature vector data of the positive and negative cases, required for training the neural network model. These vector data are saved in corresponding CSV files. Subsequently, a Python program will read the relevant data and input it according to the format requirements of the network model to iteratively train the neural network model.

[0068] See also Figure 4 Using the obtained label feature vectors of countercases and a large number of sample feature vectors of countercases, the constructed neural network model is then trained. Each time, a sample feature vector of a countercase is input, and the given label value is the label feature vector of the countercase. The output vector of the network model is obtained, and the cosine similarity between the output vector and the label feature vector of the countercase is calculated. If the similarity evaluation error is greater than the set error threshold, the model parameters are adjusted and optimized to continue training, so that the evaluation error of the neural network model becomes smaller and smaller, until the evaluation error is less than the set error threshold and tends to converge. At this point, the neural network model is considered to be trained and training ends. The model parameters are saved, and a trained neural network model that can also identify countercases is obtained.

[0069] When verifying the test code written by the R&D personnel for the corresponding function, the test feature vector obtained from the test code is also input into the trained neural network model. However, unlike identifying positive cases, identifying negative cases involves calculating the cosine similarity between the result vector and the label feature vector of the negative case. The probability that the test code cannot achieve the corresponding function is evaluated based on this cosine similarity.

[0070] To enable the neural network model to fully learn and adapt to real-world situations, and to train a more accurate and intelligent model, according to some embodiments of this application, the method of this application further includes:

[0071] First, the sample feature vector of each positive case is paired with the label feature vector of the positive case, and the sample feature vector of each negative case is paired with the label feature vector of the negative case, forming a large number of feature vector pairs of positive and negative cases. Then, the feature vector pairs of positive and negative cases are randomly arranged by the data randomization combination method.

[0072] During the training process of the neural network model, the unordered feature vectors of positive and negative cases are used to train the neural network model until a well-trained neural network model is obtained.

[0073] The data stream processing logic for the sample and label feature vectors in the above embodiments is as follows: Figure 5 As shown, the training data is first paired to form key-value pairs (sample feature vector, label feature vector). Then, using a data shuffling method, the positive and negative case feature vector pairs are rearranged in random order, forming an unordered data stream of positive and negative case feature vector pairs. A large number of unordered positive and negative case feature vector pairs are then input into the neural network model, continuously adjusting and optimizing the model parameters to reduce the network model's error. When the error converges, the neural network model can be considered successfully trained, and the resulting trained neural network model can identify both positive and negative cases.

[0074] When validating the test code written by R&D personnel for the corresponding function, the first step is to extract the feature vector of the test code using the same feature extraction algorithm and normalize it to obtain the test feature vector. This test feature vector is then input into a trained neural network model. After the model's computation, it outputs a result vector. Next, the cosine similarity between this result vector and the label feature vectors of the positive and negative cases is calculated. The probability of the test code achieving the corresponding function is assessed based on the cosine similarity with the label feature vectors of the positive cases, and the probability of it not achieving the corresponding function is assessed based on the cosine similarity with the label feature vectors of the negative cases. The result with the higher probability value is output as the final evaluation result. For example, if the cosine similarity between the result vector output by the neural network model and the label feature vectors of the positive and negative cases is 86% and 38%, respectively, then the evaluation result output by the model is: the probability that the test code can achieve the corresponding function is 86%.

[0075] According to some embodiments of this application, the constructed neural network model is a BP neural network model, which is a three-layer network model including an input layer, a single hidden layer and an output layer. The input layer takes in a seven-dimensional input vector, and the output layer outputs a seven-dimensional output vector. The initial weights and biases from the input layer to the hidden layer and from the hidden layer to the output layer are initialized using a random value method or a fixed value method with (0,1).

[0076] This application utilizes a back propagation neural network (BP neural network), a common technique in deep learning, which automatically adjusts and optimizes the model during training to accurately evaluate the code under test. The principle is that after the signal propagates forward between network nodes, the neural network model automatically performs error analysis with the target, then propagates backward to adjust the weights and thresholds of the network nodes, ensuring that the next signal propagation more closely approximates the target value, thus obtaining a model consistent with the expected result. For example, to evaluate code for security requirements, the sample feature vectors of positive and negative cases of security requirements can be used as input data to the BP neural network model, and the label feature vectors of the positive and negative cases can be used as the label values ​​of the network model. By setting the required parameters for the BP neural network model and conducting multiple rounds of iterative training, adjusting the weights, thresholds, and other parameters of the entire BP neural network model, a BP neural network model suitable for the target requirements is finally obtained.

[0077] See Figure 6 and Figure 7 By studying techniques such as code similarity detection and backpropagation neural networks, in constructing Figure 6 In the BP neural network model shown, this embodiment of the application uses a three-layer network model, namely an input layer, a single hidden layer, and an output layer. The matrix operation formulas of its input, output, and model parameters are as follows: Figure 7As shown, since the sample feature vector is seven-dimensional, the BP neural network in this application has seven inputs, corresponding to the seven dimensions of the sample feature vector. At the same time, since the label feature vector is seven-dimensional, the final network model output is also a seven-dimensional vector. The cosine similarity between the output seven-dimensional vector and the label feature vector is calculated to evaluate whether the code under test meets the target requirements.

[0078] It should be noted that when constructing a BP neural network model, the hyperparameters of the BP neural network need to be defined first to pre-initialize the network model. Then, based on the analysis of experimental results, the hyperparameters of the BP neural network are gradually adjusted to achieve the expected effect of accurately evaluating the code under test. Since the weights and biases of the BP neural network can be continuously adjusted and optimized through the backpropagation process, it is necessary to initialize the initial weights and biases from the input layer to the hidden layer, and the initial weights and biases from the hidden layer to the output layer. Generally, the (0,1) random value method or the fixed value method is used. This application prefers to use the fixed value method to simplify the calculation.

[0079] The weight optimization process of a BP neural network ultimately transforms into a function of the learning rate, network output value, target value, input value, and threshold. The values ​​from the forward propagation can be substituted to obtain the corresponding results. During the backpropagation process of multiple neurons, the weight changes are gradually adjusted according to this principle, thus adjusting the weight parameters of the entire network to adapt to the target value of the network model, ultimately forming a model with learning and adaptive capabilities. The initial BP neural network model parameter table in this application is as follows: Figure 8 As shown in the table below, the characteristics of the various activation functions selected for training the BP neural network model are compared. Figure 9 As shown.

[0080] During the training of a BP neural network model, the learning rate, activation function, and number of hidden layer nodes are three important parameters. Appropriately adjusting these three parameters based on the training results can lead to a smoother convergence of the network model's error loss, indicating that the network model has been successfully trained and is ready for result verification and analysis. According to some implementation methods of this application, during the training of the BP neural network model, the three important parameters—activation function, learning rate, and number of hidden layer nodes—are determined sequentially through three sets of experiments.

[0081] Through extensive training experiments, result analysis, and parameter optimization, the inventors summarized three sets of experiments to find the most suitable parameter values. Notably, after each set of experiments was completed, the inventors input a pair of positive and negative test data into the model network and used a cosine similarity algorithm to calculate the similarity between the model's output vector and the label values, thereby verifying the optimization effect of the BP neural network model parameters.

[0082] The first set of training experiments aims to find the most suitable activation function. Four commonly used activation functions in BP neural networks—Sigmoid, Tanh, Softsign, and ReLU—are selected as options, and their function characteristics are as follows: Figure 9 As shown. The remaining parameters are as follows: Figure 8 The initial hyperparameters are defined as follows: learning rate 0.00005, number of hidden layer nodes 50. The training record table and the similarity test results for positive and negative cases are shown below. Figure 10 As shown in the figure, the Softsign function performs the worst, while the Sigmoid and Tanh functions perform similarly. The ReLU function not only exhibits a smooth and convergent loss curve, but also significantly improves the effectiveness of both positive and negative case tests, achieving over 60% effectiveness for positive cases and over 87% effectiveness for negative cases. Therefore, the ReLU activation function was ultimately determined to be the most suitable activation function. The loss curve corresponding to the first set of training experiment results is shown in the figure. Figure 11 As shown,

[0083] The second set of training experiments aims to find the most suitable learning rate parameters. The learning rate parameter significantly influences the parameter optimization performance of the BP neural network model; both excessively large and excessively small learning rates negatively impact the overall learning performance of the network model. In this experiment, the learning rate is set as a variable, the activation function uses the already determined ReLU function, and the remaining parameters use the same data as in the first set of training experiments. The training record table and the similarity test results for positive and negative cases are shown below. Figure 12 As shown in the figure, the effectiveness of the positive and negative case tests decreases as the learning rate decreases and increases as the learning rate increases. When the learning rate reaches 0.00005, the loss curve exhibits the smallest fluctuation and the smoothest convergence. Therefore, 0.00005 is ultimately determined as the most suitable learning rate parameter. Since the learning rate parameter determined in the second set of training experiments is the same as that in the first set of training experiments, the loss curve corresponding to the results of the second set of training experiments is also shown in the figure. Figure 11 As shown.

[0084] The third training experiment aimed to find the most suitable hidden layer node parameters. The hidden layer node parameters significantly influence the learning granularity of the BP neural network model. Increasing the number of nodes leads to more granular learning and better test results, but the effect plateaus after a certain number of nodes, and further increases in nodes will increase the model's computational load. The experiment started with an initial number of hidden layer nodes of 50, and then decreased and increased the number of nodes, observing the results. The learning rate was set to 0.00005, the activation function to ReLU, and all other parameters remained the same as in the previous experiments. The training record table and the similarity test results for positive and negative cases are shown below. Figure 13As shown in the figure, the effectiveness of the positive and negative case tests significantly increases with the increase of the number of hidden layer nodes, and the loss function curve converges significantly and smoothly. When the number of nodes reaches 190 or more, its effect and convergence are stable, with the similarity test results for positive cases reaching over 75% and the similarity test results for negative cases reaching over 88%. Therefore, 190 was finally determined as the most suitable parameter for the number of hidden layer nodes. The loss curve corresponding to the results of the third set of training experiments is shown in the figure. Figure 14 As shown.

[0085] Through the three sets of training experiments described above, the key parameter values ​​for the BP neural network model suitable for this application were determined: a learning rate of 0.00005, a ReLU activation function, and 190 hidden layer nodes. At this point, the network model tends to stabilize. Using the save function provided by PyTorch, the entire network model data is saved for later use to test whether the features of the functional code written by developers are similar to those of standard code, thereby determining whether the written code can achieve the corresponding function.

[0086] For the obtained stable BP neural network model, the inventors also used the corresponding functional implementation code written by the R&D personnel, as well as randomly obtained non-compliant code, as verification data. After extracting their feature vectors, these were input into the network model calculation to verify the effectiveness of the network model. The verification results of the positive cases are as follows: Figure 15 As shown, the verification results of the counterexamples are as follows: Figure 16 As shown, the average validity of positive case verification is above 76%, and the average validity of negative case verification is above 89%. This demonstrates that the data can accurately determine whether the functional code written by the R&D personnel can achieve the corresponding function, thus achieving the expected experimental objectives well.

[0087] At this point, the detailed implementation and optimization process of the BP neural network model of this application has been completed, forming an intelligent model suitable for feature detection of specific functional implementation code in software development, and the detection effect has achieved the expected goal. In the research and optimization of the network model, this application proposed applying BP neural network technology from deep learning to the detection of functional code, identified three important parameters that determine the BP neural network model, summarized a systematic method for BP neural network model training and optimization, explored a method suitable for the implementation of specific functional code within enterprises, and for performance detection and optimization, and finally obtained BP neural network model parameters suitable for feature similarity detection of specific functional code, such as activation function, number of hidden layer nodes, learning rate, number of network layers, number of input layer nodes, and number of output layer nodes. These parameters can be used in BP neural network models for specific function detection and have a certain degree of universality.

[0088] Example 2

[0089] According to a second aspect of this application, embodiments of this application propose a functional code evaluation device based on a neural network, see [link to relevant documentation]. Figure 17 As shown, it includes:

[0090] The code snippet acquisition unit 171 is used to acquire target code snippets of positive cases from standard code that implements a specific function, and to acquire sample code snippets of positive cases from the implementation code of the corresponding function written by the R&D personnel.

[0091] The feature vector extraction unit 172 is used to extract feature data of program capacity and key program elements in the target code segment of the positive case and the sample code segment respectively through the same feature extraction algorithm to form a seven-dimensional feature vector. After normalizing the seven-dimensional feature vector, the label feature vector and sample feature vector of the positive case that can be used for neural network model operation are obtained.

[0092] The network model training unit 173 is used to train the constructed neural network model using the label feature vectors of the positive cases as label values ​​and a large number of sample feature vectors of the positive cases as input data until a trained neural network model is obtained.

[0093] The functional code evaluation unit 174 is used to extract the feature vector of the code to be tested for the corresponding function written by the R&D personnel through the same feature extraction algorithm, and after normalization, input it into the trained neural network model, calculate the cosine similarity between the output vector of the model and the label feature vector of the positive case, and evaluate the probability that the code to be tested can achieve the corresponding function based on the cosine similarity.

[0094] According to some embodiments of this application, the seven-dimensional feature vector is: [program length N, vocabulary n, program capacity V, number of assignment statements a, number of loop structures b, number of conditional structures c, number of functions d], wherein it is assumed that: the number of operand types in the code segment is denoted as n1, the number of operator types is denoted as n2, the total number of operands is denoted as N1, and the total number of operators is denoted as N2, then: vocabulary n = n1 + n2; program length N = N1 + N2; program capacity V = N * log2(n).

[0095] According to some embodiments of this application, the code snippet acquisition unit 171 is also used to acquire sample code snippets of counterexamples from the code written by the R&D personnel that cannot implement the corresponding function.

[0096] The feature vector extraction unit 172 is also used to extract the feature vector of the sample code segment of the countercase using the same feature extraction algorithm and normalize it to obtain the sample feature vector of the countercase; and to take the vector whose dot product with the label feature vector of the positive case is 0 as the label feature vector of the countercase.

[0097] The network model training unit 173 is also used to train the constructed neural network model using the label feature vectors of the countercases as label values ​​and a large number of sample feature vectors of the countercases as input data until a trained neural network model is obtained.

[0098] The functional code evaluation unit 174 is also used to extract the feature vector of the code to be tested for the corresponding function written by the R&D personnel through the same feature extraction algorithm, and after normalization, input it into the trained neural network model, calculate the cosine similarity between the result vector output by the model and the label feature vector of the countercase, and evaluate the probability that the code to be tested cannot achieve the corresponding function based on the cosine similarity.

[0099] According to some embodiments of this application, the network model training unit 173 is specifically used to first pair the sample feature vector of each positive case with the label feature vector of the positive case, and pair the sample feature vector of each negative case with the label feature vector of the negative case, forming a large number of feature vector pairs of positive and negative cases respectively. Then, the feature vector pairs of positive and negative cases are randomly arranged by a data randomization method. The unordered feature vector pairs of positive and negative cases are used to train the neural network model until a trained neural network model is obtained.

[0100] According to some embodiments of this application, the constructed neural network model is a BP neural network model, which is a three-layer network model including an input layer, a single hidden layer and an output layer. The input layer takes in a seven-dimensional input vector, and the output layer outputs a seven-dimensional output vector. The initial weights and biases from the input layer to the hidden layer and from the hidden layer to the output layer are initialized using a random value method or a fixed value method with (0,1).

[0101] During the training process of the BP neural network model, three important parameters of the BP neural network model—activation function, learning rate, and number of hidden layer nodes—were determined sequentially through three sets of experiments.

[0102] I can understand. Figure 17 Each unit in the device can implement each step in the method of Embodiment 1 described above, and the relevant explanations of the method of Embodiment 1 are applicable to all units in the device. Figure 17The functional code evaluation device for the neural network shown will not be described in detail here. It should also be noted that the unit names described in the embodiments of this application do not necessarily constitute a limitation on the unit itself.

[0103] Example 3

[0104] According to a third aspect of this application, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to execute the neural network-based functional code evaluation method of this application.

[0105] The following is for reference. Figure 18 The diagram illustrates a structural schematic of an electronic device suitable for implementing the embodiments of this application. The electronic devices in the embodiments of this application may include, but are not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 18 The electronic device shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of this application.

[0106] like Figure 18 As shown, an electronic device may include a processing unit (such as a central processing unit, graphics processing unit, etc.) that can perform various appropriate actions and processes based on a program stored in read-only memory (ROM) or a program loaded from a storage device into random access memory (RAM). The RAM also stores various programs and data required for the operation of the electronic device. The processing unit, ROM, and RAM are interconnected via a bus. Input / output (I / O) interfaces are also connected to the bus.

[0107] Typically, the following systems can be connected to the I / O interface: input devices including, for example, touchscreens, touchpads, keyboards, mice, image sensors, microphones, accelerometers, gyroscopes, etc.; output devices including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices including, for example, magnetic tapes, hard disks, etc.; and communication devices. Communication devices allow electronic devices to communicate wirelessly or wiredly with other devices to exchange data. Although electronic devices with various systems are shown in the figures, it should be understood that it is not required to implement or possess all the systems shown. More or fewer systems may be implemented alternatively.

[0108] Specifically, according to embodiments of this application, the processes described in the flowcharts above can be implemented as computer software programs. For example, embodiments of this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from a storage device, or installed from a ROM. When the computer program is executed by a processing device, it performs the functions defined in the methods of embodiments of this application.

[0109] Finally, it should be noted that:

[0110] The sequence numbers of the embodiments in this application are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments. In the above embodiments of this application, the descriptions of each embodiment have their own emphasis; parts not described in detail in a certain embodiment can be referred to in the relevant descriptions of other embodiments. The embodiments of this application can be implemented using hardware, software, firmware, or a combination thereof.

[0111] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units can be a logical functional division, and in actual implementation, there may be other division methods. For instance, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces, units, or modules, and may be electrical or other forms.

[0112] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0113] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of methods, systems, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0114] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit. Moreover, if the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application.

Claims

1. A functional code evaluation method based on neural networks, characterized in that, Includes the following steps: Obtain target code snippets of positive cases from standard code that implements a specific function, and obtain sample code snippets of positive cases from the implementation code of the corresponding function written by R&D personnel; For the target code segment of the positive case and the sample code segment, the same feature extraction algorithm is used to extract the feature data of program capacity and key program elements in the code segment to form a seven-dimensional feature vector. After normalizing the seven-dimensional feature vector, the label feature vector and sample feature vector of the positive case that can be used for neural network model operation are obtained. For the constructed neural network model, the label feature vector of the positive case is used as the label value, and a large number of sample feature vectors of the positive case are used as input data to train the neural network model until a trained neural network model is obtained. The code to be tested, written by the R&D personnel, is used to extract its feature vector through the same feature extraction algorithm. After normalization, it is input into the trained neural network model. The cosine similarity between the output vector of the model and the label feature vector of the positive case is calculated. The probability that the code to be tested can achieve the corresponding function is evaluated based on the cosine similarity. The seven-dimensional feature vector is: [program length N, vocabulary n, program capacity V, number of assignment statements a, number of loop structures b, number of conditional structures c, number of functions d]. It is assumed that: the number of operand types in the code segment is denoted as n1, the number of operator types is denoted as n2, the total number of operands is denoted as N1, and the total number of operators is denoted as N2. Then: vocabulary n = n1 + n2. Program length N = N1 + N2; Program capacity V = N * log2 (n); The method further includes: Sample code fragments of counterexamples are obtained from the code written by the R&D personnel that cannot implement the corresponding function. The sample code fragments of the counterexamples are then processed by the same feature extraction algorithm to extract their feature vectors and normalize them to obtain the sample feature vectors of the counterexamples. The vector whose dot product with the label feature vector of the positive case is 0 is used as the label feature vector of the negative case. For the constructed neural network model, the label feature vector of the countercase is used as the label value, and a large number of sample feature vectors of the countercase are used as input data to train the neural network model until a trained neural network model is obtained. The test code for the corresponding function written by the R&D personnel is used to extract its feature vector through the same feature extraction algorithm and then normalized before being input into the trained neural network model. The cosine similarity between the output vector of the model and the label feature vector of the counterexample is calculated, and the probability that the test code cannot achieve the corresponding function is evaluated based on the cosine similarity.

2. The method according to claim 1, characterized in that, The method further includes: First, the sample feature vector of each positive case is paired with the label feature vector of the positive case, and the sample feature vector of each negative case is paired with the label feature vector of the negative case, forming a large number of feature vector pairs of positive and negative cases. Then, the feature vector pairs of positive and negative cases are randomly arranged by the data randomization combination method. During the training process of the neural network model, the unordered feature vectors of positive and negative cases are used to train the neural network model until a well-trained neural network model is obtained.

3. The method according to claim 1, characterized in that, The constructed neural network model is a BP neural network model, which is a three-layer network model including an input layer, a single hidden layer, and an output layer. The input layer takes a seven-dimensional input vector as input, and the output layer outputs a seven-dimensional output vector. The initial weights and biases from the input layer to the hidden layer, and the initial weights and biases from the hidden layer to the output layer are initialized using a random value method (0, 1) or a fixed value method.

4. The method according to claim 3, characterized in that, During the training process of the BP neural network model, three important parameters of the BP neural network model—activation function, learning rate, and number of hidden layer nodes—were determined sequentially through three sets of experiments.

5. A functional code evaluation device based on a neural network, characterized in that, include: The code snippet acquisition unit is used to obtain target code snippets of positive cases from standard code that implements a specific function, and to obtain sample code snippets of positive cases from the implementation code of the corresponding function written by the R&D personnel. The feature vector extraction unit is used to extract feature data of program capacity and key program elements from the target code segment of the positive case and the sample code segment respectively through the same feature extraction algorithm to form a seven-dimensional feature vector. After normalizing the seven-dimensional feature vector, the corresponding label feature vector and sample feature vector of the positive case can be obtained for the neural network model to operate. The network model training unit is used to train the constructed neural network model using the label feature vectors of the positive cases as label values ​​and a large number of sample feature vectors of the positive cases as input data until a trained neural network model is obtained. The functional code evaluation unit is used to extract the feature vector of the code to be tested for the corresponding function written by the R&D personnel through the same feature extraction algorithm, normalize it, and then input it into the trained neural network model. The unit calculates the cosine similarity between the output vector of the model and the label feature vector of the positive case, and evaluates the probability that the code to be tested can achieve the corresponding function based on the cosine similarity. The seven-dimensional feature vector is: [program length N, vocabulary n, program capacity V, number of assignment statements a, number of loop structures b, number of conditional structures c, number of functions d]. It is assumed that: the number of operand types in the code segment is denoted as n1, the number of operator types is denoted as n2, the total number of operands is denoted as N1, and the total number of operators is denoted as N2. Then: vocabulary n = n1 + n2. Program length N = N1 + N2; Program capacity V = N * log2 (n); Also includes: Sample code fragments of counterexamples are obtained from the code written by the R&D personnel that cannot implement the corresponding function. The sample code fragments of the counterexamples are then processed by the same feature extraction algorithm to extract their feature vectors and normalize them to obtain the sample feature vectors of the counterexamples. The vector whose dot product with the label feature vector of the positive case is 0 is used as the label feature vector of the negative case. For the constructed neural network model, the label feature vector of the countercase is used as the label value, and a large number of sample feature vectors of the countercase are used as input data to train the neural network model until a trained neural network model is obtained. The test code for the corresponding function written by the R&D personnel is used to extract its feature vector through the same feature extraction algorithm and then normalized before being input into the trained neural network model. The cosine similarity between the output vector of the model and the label feature vector of the counterexample is calculated, and the probability that the test code cannot achieve the corresponding function is evaluated based on the cosine similarity.

6. The apparatus according to claim 5, characterized in that, The code snippet acquisition unit is also used to acquire sample code snippets of counterexamples from the code written by the R&D personnel that cannot implement the corresponding function; The feature vector extraction unit is also used to extract the feature vector of the sample code segment of the countercase using the same feature extraction algorithm and normalize it to obtain the sample feature vector of the countercase; and to take the vector whose dot product with the label feature vector of the positive case is 0 as the label feature vector of the countercase. The network model training unit is also used to train the constructed neural network model using the label feature vectors of the countercases as label values ​​and a large number of sample feature vectors of the countercases as input data until a trained neural network model is obtained. The functional code evaluation unit is also used to extract the feature vector of the code to be tested for the corresponding function written by the R&D personnel through the same feature extraction algorithm, normalize it, and then input it into the trained neural network model. The unit calculates the cosine similarity between the result vector output by the model and the label feature vector of the counterexample, and evaluates the probability that the code to be tested cannot achieve the corresponding function based on the cosine similarity.

7. The apparatus according to claim 6, characterized in that, The network model training unit is specifically used to first pair the sample feature vector of each positive case with the label feature vector of the positive case, and pair the sample feature vector of each negative case with the label feature vector of the negative case, forming a large number of feature vector pairs of positive and negative cases. Then, the feature vector pairs of positive and negative cases are randomly arranged using a data randomization method. The unordered feature vector pairs of positive and negative cases are used to train the neural network model until a trained neural network model is obtained.

8. The apparatus according to any one of claims 5-7, characterized in that, The constructed neural network model is a BP neural network model, which is a three-layer network model, including an input layer, a single hidden layer, and an output layer. The input layer takes a seven-dimensional input vector as input, and the output layer outputs a seven-dimensional output vector. The initial weights and biases from the input layer to the hidden layer, and the initial weights and biases from the hidden layer to the output layer are initialized using a random value method (0, 1) or a fixed value method. During the training process of the BP neural network model, three important parameters of the BP neural network model—activation function, learning rate, and number of hidden layer nodes—were determined sequentially through three sets of experiments.

9. An electronic device, characterized in that, include: At least one processor; as well as, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • Code quality evaluation method, device and system and storage medium

    CN110858176A

  • Weighted code coverage

    US20210149793A1