A cross-modal software retrieval method based on hypothesis testing in heterogeneous semantic space
By using a deep learning model based on hypothesis testing in a heterogeneous semantic space, projecting text and code into the corresponding distribution and sample space, and calculating matching scores, the problem of deep semantic overlap and correlation distinction between text and code in the existing technology is solved. , improving the precision and accuracy of cross-modal retrieval of the software.
Patent Information
- Application Number
- CN202210693666.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-18
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2042-06-18
AI Technical Summary
Existing software cross-modal retrieval technology is difficult to accurately measure the deep semantic overlap of text and code in a heterogeneous semantic space, and fails to effectively distinguish the correlation between different code executions and text, resulting in insufficient information retrieval accuracy.
A deep learning model based on hypothesis testing is adopted to project the text to the CFP-related distribution in the distribution space, project the code to the set of CFP sample vectors in the sample space, and calculate the matching score in the heterogeneous semantic space through triplet loss Functional and stochastic gradient descent algorithms optimize distribution generation networks and sample generation networks.
It achieves accurate representation of the unique semantics of text and code in a heterogeneous semantic space, improves the accuracy of cross-modal retrieval, can distinguish the correlation between different code executions and text, and is suitable for a variety of software cross-modal retrieval tasks.
Smart Images

Figure CN115048491B
Abstract
Description
Technical Field
[0001] The present invention relates to a software cross-modal retrieval method, and in particular to a software cross-modal retrieval method based on hypothesis testing in a heterogeneous semantic space and applicable to multiple tasks. Background Art
[0002] With the development of Internet technology and big data systems, software data in the software development process is gradually accumulated and stored in open source software databases. Representative software databases include the code version management database GitHub, the software vulnerability database NVD, and the software defect tracking database Bugzilla. Using the knowledge accumulated in the software warehouse to guide software development helps to reduce the software development and maintenance cycle and improve the quality and security of software products. Therefore, in order to make full use of the knowledge in the software database to guide the production practice of software engineering, the field of software information retrieval has received widespread attention in recent years. Among them, cross-modal software retrieval between natural language text and code (such as defect location, code search and other text retrieval code tasks, as well as vulnerability knowledge retrieval, patch log retrieval and other code retrieval text tasks) is one of the hot research directions in this field.
[0003] Early research on cross-modal software retrieval mainly used artificial features to represent text and code respectively, and realized information retrieval by calculating the similarity between the two. However, this type of method focuses more on the similarity of cross-modal data in low-level semantic features such as lexical features, and it is difficult to extract the deep semantic overlap of text and code. To solve this problem, some studies use feature extraction models based on deep learning, that is, projecting text and code into two different feature vectors in the same semantic space through natural language encoding networks and program language encoding networks, and measuring the similarity between text and code through similarity measurement functions (such as cosine similarity or Euclidean distance) or binary classification layers (category 1 is match, category 0 is mismatch). However, due to the context-independent grammar type of code and special grammatical structures such as data flow and control flow, it is often considered to be data of different modalities from text, so it is difficult to accurately measure the semantic overlap between the two in the same semantic space.
[0004] In addition, existing studies have not focused on the difference in the relevance of different code executions to text. In software cross-modal retrieval tasks, text often has different relevance to the specific execution of code under different input conditions. For example, in a bug localization task, a bug will only be triggered in code execution under certain specific input conditions. Since bug reports are often descriptions of bugs in the code, it can be considered that they are more relevant to the code execution where the bug exists.
[0005] At present, no literature has been found that projects multimodal data into a heterogeneous semantic space in software cross-modal retrieval tasks, nor has any literature been found that uses a deep learning model based on hypothesis testing to calculate the matching scores of heterogeneous data in a heterogeneous space. Summary of the invention
[0006] The purpose of the present invention is to provide a software cross-modal retrieval method (DeepHT) based on hypothesis testing in heterogeneous semantic space. DeepHT projects code as CFP sample vectors in sample space and text as CFP related distribution in distribution space, and calculates the matching scores of the two through hypothesis testing. The present invention maps text and code into heterogeneous semantic space, which is conducive to extracting the unique semantics of text and code and improving the accuracy of information retrieval.
[0007] The objective of the present invention is achieved through the following technical solutions:
[0008] A software cross-modal retrieval method based on hypothesis testing in a heterogeneous semantic space comprises the following steps:
[0009] Step 1: Preprocess text and code data;
[0010] Step 2: Use a distribution generation network to represent the text as a CFP-related distribution;
[0011] Step 3: Use the sample generation network to represent the code as a set of CFP sample vectors;
[0012] Step 4: Use a hypothesis testing method to calculate the matching score between the CFP sample vector set and the CFP related distribution in the heterogeneous semantic space;
[0013] Step 5: Optimize the distribution generation network and sample generation network through the triple loss function and stochastic gradient descent algorithm;
[0014] Step 6: Use the distribution generation network and sample generation network trained in step 5 to implement the cross-modal retrieval task in the form of text retrieval code;
[0015] Step 7: Use the distribution generation network and sample generation network trained in step 5 to implement the cross-modal retrieval task in the form of code retrieval text.
[0016] The present invention provides a method for projecting multimodal data into a heterogeneous semantic space and constructing a statistical distribution model in the heterogeneous space to model the correlation between different code executions and texts. The method is called Deep Hypothesis Testing (DeepHT), and "deep" refers to the transformation of hypothesis testing theory into deep learning implementation. DeepHT is not only conducive to capturing the specific execution of code under different input conditions, but also conducive to distinguishing the correlation between different specific executions and texts. Specifically, for the feature extraction stage of the code, each control flow path (Control Flow Path, CFP) on its control flow graph (Control FlowGraph, CFG) is extracted, where each path can represent a specific code execution, and each CFP is mapped to a sample vector in the sample space. For the feature extraction stage of the text, the present invention maps the text to a multidimensional normal distribution (also called CFP correlation distribution) in the distribution space for modeling the correlation degree of each CFP to the text. Compared with the prior art, the present invention has the following advantages:
[0017] 1. The present invention proposes for the first time to project code and text into heterogeneous semantic spaces for representation learning, that is, to project text into the CFP related distribution space and project code into the CFP sample space. Compared with the existing method of calculating the representation of text and code in the same semantic space, the advantage of this method is that it can accurately represent the unique semantics of text and code, thereby improving the accuracy of cross-modal retrieval.
[0018] 2. The present invention proposes a deep learning model based on hypothesis testing theory, which can accurately measure the degree of compliance of the CFP sample set with the CFP correlation distribution, thereby realizing accurate calculation of matching scores across semantic spaces.
[0019] 3. The CFP correlation distribution proposed in the present invention can be used to distinguish the correlation between different code executions and texts.
[0020] 4. The present invention can realize a variety of software cross-modal retrieval tasks, including text retrieval code (such as defect location and code search, etc.) and code retrieval text form cross-modal retrieval tasks (such as vulnerability knowledge retrieval and patch log retrieval, etc.).
[0021] 5. Different from the existing methods that project text and code into the same semantic space, the present invention projects code and text into heterogeneous semantic spaces for the first time. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1 It is an overall flow chart of the software cross-modal retrieval method of the present invention.
[0023] Figure 2This is an example diagram that introduces the text preprocessing process using CVE-2020-5331 as an example.
[0024] Figure 3 This is an example diagram that introduces the code preprocessing process using the vulnerable function radeon_atom_tv_timings in the Linux kernel as an example.
[0025] Figure 4 Taking the vulnerability knowledge retrieval task as an example, the vulnerability function radeon_atom_tv_timings is used as a query to retrieve the instance graph of CVE related to it in the database composed of CVE-2010-5331 and CVE-2020-15211. DETAILED DESCRIPTION
[0026] The technical solution of the present invention is further described below in conjunction with the accompanying drawings, but is not limited thereto. Any modification or equivalent replacement of the technical solution of the present invention without departing from the spirit and scope of the technical solution of the present invention should be included in the protection scope of the present invention.
[0027] The present invention provides a software cross-modal retrieval method based on hypothesis testing in a heterogeneous semantic space. First, a distribution generation network is used to project text into a CFP-related distribution in a distribution space. Second, the code is represented as a control flow graph and all paths are extracted. Then, a sample generation network is used to map CFP into a CFP sample vector in a sample space. At this time, the code is represented as a set of CFP sample vectors. Finally, a hypothesis test is used to calculate the degree of compliance of the CFP sample vector set with the CFP-related distribution as the matching score of the two, and the method is used to implement cross-modal retrieval tasks in the form of code retrieval text or text retrieval code. Figure 1 As shown, the method specifically comprises the following steps:
[0028] Step 1: Preprocess the code and text.
[0029] In the task of text retrieval code, it is necessary to preprocess the text query and all the codes to be retrieved in the database; in the task of code retrieval text, it is necessary to preprocess the code query and all the text to be retrieved in the database. The specific steps are as follows:
[0030] Step 11: Text preprocessing: Remove stop words, numbers, and punctuation marks from the text, split identifiers that appear in camel case or underscore case, and restore the part of speech and lowercase each word.
[0031] Step 12: Extract CFPs from the code. For file-level search tasks, first extract all functions in the code file based on the push-down automaton function grammar analyzer (this step is skipped for function-level retrieval tasks). Then, normalize the line breaks in the functions. Finally, convert each function into a CFG, and extract each CFP in it through depth-first search. The above process can convert the code into a CFP set.
[0032] Step 13: CFP preprocessing: Split the identifiers named in camel case or underscore in CFP, and restore the parts of speech and lowercase the split tokens.
[0033] Step 2: Use a deep learning model to represent the text as a CFP-related distribution. This deep learning model is also called a distribution generation network.
[0034] In order to simplify the problem, the present invention regards the covariance matrix ∑ of the CFP correlation distribution as a diagonal matrix whose diagonal elements are greater than 0 and other elements are 0, and defines the vector composed of its diagonal elements as σ 2 , by σ 2 The vector consisting of the reciprocal of the square root of each element in is defined as σ -1 The specific steps are as follows:
[0035] Step 21: Represent the words in the text as one-hot encodings and use a trainable word embedding layer to represent each word in the text as a word embedding vector;
[0036] Step 22: Use the Text-CNN network (Convolutional Neural Networks for Sentence Classification, 2014) to extract the semantic vector representation of the text;
[0037] Step 23: Send the semantic vector representation of the text into a fully connected layer with an activation function of tanh to generate the expected vector μ and output it;
[0038] Step 24: Send the semantic vector representation of the text into the fully connected layer with sigmoid activation function to generate the inverse standard deviation vector σ -1 And output, the generated μ and σ 2 It can uniquely represent a CFP related distribution N(μ,σ 2 ).
[0039] Step 3: Use a deep learning model to represent the code as a set of CFP sample vectors. This deep learning model is also called a sample generation network. The specific steps are as follows:
[0040] Step 31: Represent the basic lexical units (tokens) in each sentence in the given CFP as one-hot encoding, and use the same trainable word embedding layer as in step 21 to represent each token as a word embedding vector.
[0041] Step 32: For each sentence in a given CFP, use multiple convolutional neural networks (CNNs) with different convolution kernel sizes to extract the vector representations of the sentence under different receptive fields, and concatenate the vector representations under different receptive fields to obtain the vector representation of the sentence.
[0042] Step 33: Represent all CFPs as CFP sample vectors. Send the representation sequence of the sentences in the CFP to the Long Short-Term Memory (LSTM) network, and use the average output at each moment as the CFP sample vector;
[0043] Step 34: Execute steps 31 to 33 for all CFPs in the code, thereby representing the code as a set of CFP sample vectors.
[0044] Step 4: Calculate the degree of compliance of the CFP sample vector set (ie, code) with the CFP related distribution (ie, text) through hypothesis testing as the matching score between the two.
[0045] In order to measure the correlation of heterogeneous data in a heterogeneous semantic space, the present invention proposes a deep learning model based on hypothesis testing (i.e., deep hypothesis testing). The model can calculate the matching score of the two by measuring the degree of compliance of the CFP sample vector set (i.e., code) to the CFP related distribution (i.e., text). Hypothesis testing is widely used in the field of mathematical statistics to verify whether there is a certain relationship between the sample set and the distribution, such as judging whether the sample set obeys the distribution. The general process is:
[0046] 1) Make an assumption about the facts, denoted as H0.
[0047] 2) Select a statistic S that follows distribution D when hypothesis H0 holds.
[0048] 3) Calculate the observed value v of the statistic S based on the distribution or sample to be tested.
[0049] 4) Make a decision on whether to accept hypothesis H0. Since the statistic S obeys the distribution D when hypothesis H0 is true, we can calculate the probability p that S takes the value v based on D. If the value of p is small, it means that when H0 is true, a low-probability event will occur, which contradicts the hypothesis H0. In this case, H0 should be rejected. On the contrary, if the value of p is large, H0 is accepted.
[0050] The present invention is based on T 2 The calculation method of matching score is introduced by taking the hypothesis test as an example. Other hypothesis testing methods can also be used. The specific steps are as follows:
[0051] Step 41: Make the null hypothesis H0: E(X) = μ and an alternative hypothesis H1: E(X) = μ, where X = [x1, x2, …, x n ] is the CFP sample vector set, x i is the i-th CFP sample vector, E(X) is the mean of X, and μ is the expected vector of the CFP related distribution.
[0052] Step 42: Select the statistic T 2 ,have:
[0053]
[0054]
[0055] Here, ⊙ represents the Hadamard product.
[0056] Step 43: According to T 2 The basic theory of the test shows that when the hypothesis H0 is established, T 2 It follows the chi-square distribution, denoted by T 2 ~x 2 , using the left test, when the significance level is α, we have:
[0057]
[0058] Since the value of α is often small, is a low-probability event. If the hypothesis H0 is established and leads to the occurrence of this low-probability event, it can be considered that the result of the sampling test contradicts the hypothesis H0, so the hypothesis H0 should be rejected and the hypothesis H1 should be accepted. When , the hypothesis H0 is accepted and the hypothesis H1 is rejected. Therefore, the present invention uses T 2 As the matching score e of text and code, when the sample set follows the distribution, the value of e is relatively small, otherwise it is relatively large.
[0059] Step 5: Optimize the distribution generation network and sample generation network through the triple loss function and stochastic gradient descent algorithm, so that the distribution generation network and sample generation network learn how to generate CFP related distribution and CFP samples. The specific steps are as follows:
[0060] Step 51: Take a mini-batch of positive sample text-code pairs (referred to as positive sample pairs) from the training set without duplication.
[0061] Step 52: For each positive sample pair in the mini-batch, a negative sample pair is constructed by negative sampling. Specifically, the text or code in the positive sample pair is randomly replaced with another text or code in the dataset so that the two no longer match each other, thereby obtaining a negative sample pair.
[0062] Step 53: Calculate the loss using the following triplet loss function and update the network parameters in reverse through the Adam optimizer:
[0063]
[0064] Where n is the number of samples in a mini-batch, is the matching score of the i-th positive sample pair, is the matching score of the i-th negative sample pair, and c is the threshold hyperparameter.
[0065] Step 54: Repeat steps 51 to 53 until all sample pairs in the training set are traversed, and the training process of one epoch is completed.
[0066] Step 55: Repeat step 54 until the predetermined maximum number of iterations is reached. The parameters that achieve the best performance on the validation set are taken as the trained model parameters.
[0067] Step 6: Use the trained distribution generation network and sample generation network to implement software cross-modal retrieval of text retrieval code. The specific steps are as follows:
[0068] Step 61: Use a sample generation network to convert each code in the database to be searched into a set of CFP sample vectors;
[0069] Step 62: Use a distribution generation network to transform the text query into a CFP related distribution;
[0070] Step 63: Use hypothesis testing to calculate the matching score between the CFP correlation distribution and each CFP sample vector set;
[0071] Step 64: Sort all codes in the database in ascending order according to the matching scores, and return the first N results in the sorted list to the user.
[0072] Step 7: Use the trained distribution generation network and sample generation network to implement software cross-modal retrieval of code retrieval text. The specific steps are as follows:
[0073] Step 71: Use a distribution generation network to convert each text in the database to be retrieved into a CFP-related distribution;
[0074] Step 72: Use a sample generation network to convert the code query into a set of CFP sample vectors;
[0075] Step 73: Calculate the matching score between the CFP sample vector set and each CFP related distribution using hypothesis testing;
[0076] Step 74: Sort all texts in the database in ascending order according to the matching scores, and return the first N results in the sorted list to the user.
[0077] Embodiment 1:
[0078] Taking CVE-2010-5331 as an example, the text preprocessing process is introduced. The text preprocessing process includes removing stop words, numbers and punctuation marks, splitting identifiers, restoring parts of speech and lowering them. Figure 2 shown.
[0079] Embodiment 2:
[0080] Taking the vulnerability code snippet as an example, the extraction process of the CFP set is introduced. First, the CFG of the code is constructed, and all CFPs are extracted from the CFG. The specific process is as follows: Figure 3 shown.
[0081] Embodiment 3:
[0082] Taking the vulnerability knowledge search task as an example (using the vulnerability function as a query to retrieve the related vulnerability description in the CVE database), we introduce the method of cross-modal retrieval using hypothesis testing. Specifically, we use the vulnerability function radeon_atom_tv_timings in the Linux kernel as a query to retrieve the related CVE in the CVE database. For the convenience of calculation, it is assumed that the CVE database only contains two vulnerabilities, CVE-2020-15211 and CVE-2010-5331. First, the text description of the jth CVE in the vulnerability database is sent to the distribution generation network to generate μ j and Represents a CFP-related distribution Secondly, for a given code query, it is represented as a CFP set, and each CFP in it is fed into the sample generation network to generate a CFP sample vector (the i-th CFP sample vector is denoted as x i ), thereby converting the code into a CFP sample vector set X = [x1, x2, …, x n ]. Finally, the hypothesis test is used to calculate the distribution of X for each CFP According to the matching score, all CVEs in the database are sorted in ascending order and output. The calculation process is as follows: Figure 4 shown.
Claims
1. A software cross-modal retrieval method based on hypothesis testing in heterogeneous semantic space, characterized by The method comprises the following steps: Step 1: Preprocess the text and code data. The specific steps are as follows: Step 11: Text Preprocessing Remove stop words, numbers, and punctuation marks from the text, split identifiers that appear in camel case or underscore case, and restore the part of speech and lowercase each word; Step 12: Extract CFP from code For the file-level search task, we first extract all functions in the code file using the push-down automaton-based function grammar analyzer, then normalize the line breaks in the functions, and finally convert each function into a CFG, and extract each CFP in it through depth-first search. Step 13: CFP Preprocessing Split identifiers named in camel case or underscore in CFP, and restore the parts of speech and lowercase the split tokens; Step 2: Use the distribution generation network to represent the text as a CFP-related distribution. The covariance matrix ∑ of the CFP-related distribution is regarded as a diagonal matrix whose diagonal elements are greater than 0 and other elements are 0, and the vector composed of its diagonal elements is defined as σ 2 , by σ 2 The vector consisting of the reciprocal of the square root of each element in is defined as σ -1 , the specific steps are as follows: Step 21: Represent the words in the text as one-hot encoding and use the word embedding layer to represent each word in the text as a word embedding vector; Step 22: Use the Text-CNN network to extract the semantic vector representation of the text; Step 23: Send the semantic vector representation of the text into a fully connected layer with an activation function of tanh to generate the expected vector μ and output it; Step 24: Send the semantic vector representation of the text into the fully connected layer with the activation function sigmoid to generate the inverse standard deviation vector σ -1 And output, the generated μ and σ 2 It can uniquely represent a CFP related distribution N(μ,σ 2 ); Step 3: Use the sample generation network to represent the code as a set of CFP sample vectors; Step 4: Use a hypothesis testing method to calculate the matching score between the CFP sample vector set and the CFP related distribution in the heterogeneous semantic space. The specific steps are as follows: Step 41: Make the null hypothesis H0: E(X) = μ and an alternative hypothesis H1: E(X) ≠ μ, where X = [x1, x2, …, x m ] is the CFP sample vector set, x i is the i-th CFP sample vector, E(X) is the mean of X, and μ is the expected vector of the CFP related distribution; Step 42: Select the statistic T 2 ,have: Among them, ⊙ represents the Hadamard product; Step 43: When hypothesis H0 is established, T 2 It follows the chi-square distribution, denoted by T 2 ~x 2 , using the left test, when the significance level is α, we have: is a low-probability event. If the hypothesis H0 is established and leads to the occurrence of this low-probability event, it can be considered that the result of the sampling test contradicts the hypothesis H0. Therefore, the hypothesis H0 should be rejected and the hypothesis H1 should be accepted. On the contrary, when When , we accept hypothesis H0 and reject hypothesis H1; Step 5: Optimize the distribution generation network and sample generation network through the triple loss function and stochastic gradient descent algorithm; Step 6: Use the distribution generation network and sample generation network trained in step 5 to implement the software cross-modal retrieval task in the form of text retrieval code; Step 7: Use the distribution generation network and sample generation network trained in step 5 to implement the software cross-modal retrieval task in the form of code retrieval text.
2. The software cross-modal retrieval method based on hypothesis testing in heterogeneous semantic space according to claim 1 is characterized in that The specific steps of step 3 are as follows: Step 31: Represent the tokens in each sentence in the given CFP as one-hot encoding, and use the word embedding layer to represent each token as a word embedding vector; Step 32: For each sentence in a given CFP, use multiple convolutional neural networks with different convolution kernel sizes to extract the vector representation of the sentence under different receptive fields, and concatenate the vector representations under different receptive fields to obtain the vector representation of the sentence; Step 33: Send the representation sequence of the sentence in the CFP to the long short-term memory network, and use the average of the output at each moment as the CFP sample vector; Step 34: Execute steps 31 to 33 for all CFPs in the code, thereby representing the code as a set of CFP sample vectors.
3. The software cross-modal retrieval method based on hypothesis testing in heterogeneous semantic space according to claim 1 is characterized in that The specific steps of step 5 are as follows: Step 51: Take a mini-batch of positive sample pairs from the training set without duplication; Step 52: construct a negative sample pair for each positive sample pair in the mini-batch through negative sampling; Step 53: Calculate the loss using the triplet loss function and reversely update the network parameters through the Adam optimizer; Step 54: Repeat steps 51 to 53 until all sample pairs in the training set are traversed, and the training process of one epoch is completed; Step 55: Repeat step 54 until the predetermined maximum number of iterations is reached, and take the parameters that achieve the best performance on the validation set as the trained model parameters.
4. The software cross-modal retrieval method based on hypothesis testing in heterogeneous semantic space according to claim 3 is characterized in that The triplet loss function is: Where n is the number of samples in a mini-batch, is the matching score of the i-th positive sample pair, is the matching score of the i-th negative sample pair, and c is the threshold hyperparameter.
5. The software cross-modal retrieval method based on hypothesis testing in heterogeneous semantic space according to claim 1 is characterized in that The specific steps of step 6 are as follows: Step 61: Use a sample generation network to convert each code in the database to be searched into a set of CFP sample vectors; Step 62: Use a distribution generation network to transform the text query into a CFP related distribution; Step 63: Use hypothesis testing to calculate the matching score between the CFP correlation distribution and each CFP sample vector set; Step 64: Sort all codes in the database in ascending order according to the matching scores, and return the first N results in the sorted list to the user.
6. The software cross-modal retrieval method based on hypothesis testing in heterogeneous semantic space according to claim 1 is characterized in that The specific steps of step 7 are as follows: Step 71: Use a distribution generation network to convert each text in the database to be retrieved into a CFP-related distribution; Step 72: Use a sample generation network to convert the code query into a set of CFP sample vectors; Step 73: Calculate the matching score between the CFP sample vector set and each CFP related distribution using hypothesis testing; Step 74: Sort all texts in the database in ascending order according to the matching scores, and return the first N results in the sorted list to the user.
