Text adversarial sample generation method, system, computer device and storage medium
Through the sentence generation method guided by attack words, the problem of text adversarial sample generation technology in long texts being difficult to ensure the correctness of words and grammar is solved, achieving low modification rate and highly aggressive adversarial sample generation, and improving the robustness of the model. performance and attack efficiency.
Patent Information
- Application Number
- CN202111237436.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-22
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2041-10-22
AI Technical Summary
Existing text adversarial sample generation technology has difficulty in ensuring word correctness, grammatical correctness, and semantic integrity of the original text in long texts, and the attack effect is difficult to achieve in long texts, especially in automated paper retrieval and news classification systems. Security and robustness need to be improved urgently.
Through the sentence generation method based on attack word guidance, the attack word set is obtained using tf-idf score calculation, the word with the highest attack score is selected, and a grammatically correct sentence is constructed according to the sentence template and syntactic rules, through the neural network integration strategy and multiple rounds of iteration Computationally generate adversarial examples to ensure low modification rate and low destructiveness.
It successfully generates adversarial samples at a low modification rate, avoids spelling errors and grammatical errors, improves attack efficiency and model robustness, and is suitable for long text scenarios.
Smart Images

Figure CN114091448B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of adversarial sample generation, and in particular to a method, system, computer device and storage medium for generating text adversarial samples based on attack word guided sentence generation. Background Art
[0002] At present, deep learning models are widely used in many fields, such as computer vision, natural language processing, and speech recognition. At the same time, the security of deep learning models has also been greatly challenged. Academic research related to text adversarial attacks has developed rapidly in recent years, and there are many research results. The representative text generation technologies that are currently at the forefront of development and have good attack effects include using gradient information to screen out words that have a great impact on the model classification results, and generating text adversarial samples by creating spelling errors to destroy these words, and generating adversarial samples based on optimized approximation through synonym replacement.
[0003] However, both methods have certain limitations. Among them, the first method uses spelling errors to generate perturbations, successfully generates adversarial samples at a low modification rate, and does not affect human understanding of the text. However, this method is vulnerable to the spelling check mechanism, and the adversarial samples generated by this method are easily filtered. The second method uses the optimization approximation method to continuously optimize and approximate the randomly generated adversarial samples to generate a strong attack sample. It can achieve effective attacks in short texts, but in long texts, it is difficult to achieve ideal results with a high modification rate. In addition, this method replaces important words with synonyms, and grammatical errors are still difficult to avoid.
[0004] As far as the current adversarial sample generation technology is concerned, it is difficult to guarantee the correctness of words, grammatical correctness, semantic integrity of the original text, etc., and most of the existing research is aimed at short texts, and the effectiveness of the attack method cannot be guaranteed in long texts. However, long texts are currently widely used in scenarios such as automatic paper retrieval and news classification systems. There is an urgent need for research on adversarial attacks on long texts to better detect the security of related models and improve the robustness of related models. Summary of the invention
[0005] In order to solve the above technical problems, the purpose of the present invention is to provide a method, system, computer device and storage medium for generating text adversarial samples based on attack word guided sentence generation, which can reduce the modification rate of adversarial samples by generating strongly perturbed short sentences and has low destructive disturbance.
[0006] In a first aspect, an embodiment of the present invention provides a method for generating a text adversarial sample based on attack word guided sentence generation, the method comprising:
[0007] Obtain a data set, calculate the tf-idf score for the words in the data set, and obtain a part-of-speech dictionary of the data set and a set of attack words corresponding to different tags in the data set;
[0008] Selecting an attack word set corresponding to the label of the original sample from the data set, and selecting a word with the highest attack score from the attack word set as the attack word;
[0009] According to a preset sentence template, a syntactic rule corresponding to the part of speech of the attack word is selected, and a part of speech word corresponding to the syntactic rule is selected from the part of speech dictionary, and the part of speech word and the attack word are combined to form a sentence that conforms to the syntactic rule;
[0010] According to a preset adding condition, the sentence is added to the original sample to obtain a new sample;
[0011] The new sample is subjected to multiple rounds of iterative calculations according to preset iterative conditions to obtain an adversarial sample.
[0012] Further, the steps of obtaining a data set, calculating tf-idf scores for words in the data set, and obtaining a part-of-speech dictionary of the data set and an attack word set corresponding to different tags in the data set include:
[0013] Preprocess all texts in the data set and then segment them to obtain a corpus dictionary corresponding to the data set, wherein the corpus dictionary stores all words appearing in the data set;
[0014] Calculating the tf-idf scores of all words in the corpus dictionary, and taking the words whose tf-idf scores are greater than a preset first threshold as candidate attack words;
[0015] Select words whose tf-idf score is less than a preset second threshold, and mark their word parts of speech to form a part-of-speech dictionary corresponding to the data set;
[0016] Using a neural network integration strategy to score the offensiveness of the candidate attack words under different data set labels, and using the candidate attack words whose attack scores are greater than a preset third threshold as basic attack words corresponding to the data set label;
[0017] According to the synonym dictionary, all words that are synonyms of the basic attack word are obtained, and all synonyms corresponding to the basic attack word form an attack word set corresponding to the data set label.
[0018] Furthermore, the step of scoring the offensiveness of the candidate attack words under different data set labels by using a neural network integration strategy and using the candidate attack words with an attack score greater than a preset third threshold as basic attack words corresponding to the data set label includes:
[0019] Input the samples in the data set into the trained neural network model to obtain the confidence of the sample text under the corresponding label;
[0020] Adding the candidate attack word to the end of the sample text, and calculating the confidence of the sample text after adding the candidate attack word under the corresponding label;
[0021] Using the difference between the two confidences as the attack score of the candidate attack word in the sample text;
[0022] Acquire sample data with the same label in the data set as a subset, and generate an attack score of the candidate attack word in the subset according to the attack score of each data in the subset;
[0023] According to a plurality of pre-trained neural network models, respectively obtaining an attack score of the candidate attack word in the subset under each of the neural network models;
[0024] A weighted average is performed on the multiple attack scores to obtain a final attack score of the candidate attack word in the subset, and the candidate attack word whose final attack score is greater than a preset third threshold is used as a basic attack word corresponding to the label of the subset.
[0025] Furthermore, the tf-idf scores of all words in the dictionary are calculated using the following formula:
[0026]
[0027] Among them, N w is the total number of times word w appears in the text, N is the total number of words in the text, D is the total number of texts in the dataset, and D w is the number of texts containing word w.
[0028] The attack score of the candidate attack word in the sample text is calculated using the following formula:
[0029]
[0030] Among them, x is the text of the sample in the dataset, x w is the text after adding word w to the end of text x, f y (x) represents the confidence of the neural network model predicting text x as label y, f y (x w ) indicates that the neural network model converts this article x w The confidence of the prediction being label y;
[0031] The attack score of the candidate attack word in the subset is calculated using the following formula:
[0032]
[0033] Among them, x i represents the data in the subset subD, and m represents the total number of texts in the subset subD.
[0034] The final attack score of the candidate attack word in the subset is calculated using the following formula:
[0035]
[0036] Where n is the number of pre-trained neural network models, Indicates that through the model f k The calculated word w in the text x i Attack score in Represents the model f k The weight of
[0037] in
[0038] acc k For model f k The classification accuracy after training.
[0039] Furthermore, according to a preset sentence template, a syntactic rule corresponding to the part of speech of the attack word is selected, and a part of speech word corresponding to the syntactic rule is selected from the part of speech dictionary, and the step of combining the part of speech word and the attack word to form a sentence conforming to the syntactic rule includes:
[0040] Marking the part of speech of the attack word, and selecting the syntactic rule corresponding to the part of speech from a preset sentence template;
[0041] Selecting a part-of-speech word corresponding to the syntactic rule from the part-of-speech dictionary, and combining the part-of-speech word with the attack word to form a sentence that complies with the syntactic rule;
[0042] The sentence is checked for grammatical correctness to obtain a grammatically correct sentence.
[0043] Furthermore, the preset adding condition is that the adding position of the generated sentence is between two complete sentences in the text of the original sample, and cannot be inserted into the beginning and end of the text of the original sample.
[0044] Furthermore, the step of performing multiple rounds of iterative calculations on the new sample according to the preset iterative conditions to obtain the final adversarial sample includes:
[0045] Input the new sample into the trained neural network model to obtain the predicted label corresponding to the new sample;
[0046] Determine whether the predicted label is consistent with the label of the original sample corresponding to the new sample. If the two labels are consistent, regenerate a new sample. If the two labels are inconsistent, use the new sample as an adversarial sample.
[0047] In a second aspect, an embodiment of the present invention provides a text adversarial sample generation system based on attack word guided sentence generation, the system comprising:
[0048] A word set dictionary generation module is used to obtain a data set, perform tf-idf score calculation on the words in the data set, and obtain a part-of-speech dictionary of the data set and an attack word set corresponding to different tags in the data set;
[0049] An attack word selection module, used to select an attack word set corresponding to the label of the original sample from the data set, and select a word with the highest attack score from the attack word set as the attack word;
[0050] A sentence generation module, configured to select a syntactic rule corresponding to the part of speech of the attack word according to a preset sentence template, and select a part-of-speech word corresponding to the syntactic rule from the part-of-speech dictionary, and combine the part-of-speech word and the attack word to form a sentence that conforms to the syntactic rule;
[0051] A new sample generation module, used to add the sentence to the original sample according to a preset adding condition to obtain a new sample;
[0052] The adversarial sample generation module is used to perform multiple rounds of iterative calculations on the new sample according to preset iterative conditions to obtain an adversarial sample.
[0053] In a third aspect, an embodiment of the present invention further provides a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above method when executing the computer program.
[0054] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program implements the steps of the above method when executed by a processor.
[0055] The above-mentioned application provides a method, system, computer device and storage medium for generating text adversarial samples based on attack word-guided sentence generation. Through the method, the generated attack short sentence is added to the original sample to produce strong disturbance, which is not easy to be detected. The adversarial sample can be successfully generated at a low modification rate. At the same time, the attack word is expanded into an attack short sentence and inserted between two sentences of the original sample. No grammatical errors will be generated, thereby ensuring the low destructiveness of the disturbance. This is very meaningful for the existing field of text adversarial sample generation. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Figure 1 is a flow chart of a method for generating text adversarial samples based on attack word guided sentence generation in an embodiment of the present invention;
[0057] Figure 2 yes Figure 1 Schematic diagram of the process of step S10;
[0058] Figure 3 yes Figure 2 Schematic diagram of the process of step S104;
[0059] Figure 4 yes Figure 1 Schematic diagram of the process of step S30;
[0060] Figure 5 yes Figure 1 Schematic diagram of the process of step S50;
[0061] Figure 6 is the attack word set of the IMDB data set in the embodiment of the present invention;
[0062] Figure 7 is a sentence template corresponding to the IMDB data set in an embodiment of the present invention;
[0063] Figure 8 is a structural diagram of a text adversarial sample generation system based on attack word guided sentence generation in an embodiment of the present invention;
[0064] Fig. 9 It is a diagram of the internal structure of a computer device in an embodiment of the present invention. DETAILED DESCRIPTION
[0065] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0066] See also Figure 1 The first embodiment of the present invention proposes a method for generating text adversarial samples based on attack word guided sentence generation, which includes steps S10 to S50:
[0067] Step S10, obtaining a data set, performing tf-idf score calculation on the words in the data set, obtaining a part-of-speech dictionary of the data set and a set of attack words corresponding to different tags in the data set.
[0068] In this embodiment, a unified attack word set is constructed for the data set with the same label, so each type of label has a corresponding attack word set, and the attack word set contains the attack words of all original samples under the corresponding label. The specific construction steps are as follows: Figure 2 As shown:
[0069] Step S101, pre-processing and then segmenting all texts in a data set to obtain a corpus dictionary corresponding to the data set, wherein the corpus dictionary stores all words appearing in the data set.
[0070] After obtaining the corpus, we first preprocess the data text in the corpus, such as removing non-text parts of the data, spell checking, spelling correction, stem extraction and stem restoration, conversion to lowercase and introduction of stop words, etc., and then segment the processed text. The algorithms and tools used include but are not limited to the maximum matching algorithm, Vibit algorithm or nltk tool, etc. The specific processing method can be flexibly selected according to the actual situation.
[0071] All the words obtained by segmentation are recorded in the corpus dictionary so that we can carry out the next step of processing and calculation.
[0072] Step S102, calculating the tf-idf scores of all words in the corpus dictionary, and taking the words whose tf-idf scores are greater than a preset first threshold as candidate attack words.
[0073] After obtaining the corpus dictionary, we need to select appropriate words from all the words as candidate attack words. In this embodiment, we use the tf-idf score to select candidate attack words. tf-idf is a statistical method used to evaluate the importance of a word to a file set or one of the files in a corpus. The importance of a word increases in direct proportion to the number of times it appears in a file, but at the same time it decreases in inverse proportion to the frequency of its appearance in the corpus. tf-idf tends to filter out common words and retain important words. Generally speaking, the higher the tf-idf score, the higher the category distinction ability of the word in the text classification task and the greater the contribution to the model classification results. The attack words proposed in the present invention are also words that have a greater impact on the model classification results, so their tf-idf scores are often relatively high.
[0074] In this embodiment, the following formula is used to calculate the tf-idf scores of all words in the corpus dictionary:
[0075]
[0076] Among them, N w is the total number of times word w appears in the text, N is the total number of words in the text, D is the total number of texts in the dataset, and D w is the number of texts containing word w.
[0077] After calculating the tf-idf scores of all words, we selected the top 10% of the words in the tf-idf score as candidate attack words. In fact, the selection ratio can be adjusted according to the actual situation, but it should be noted that the ratio should not be too small to prevent important words from being missed. Since the subsequent calculation of the word offensiveness score depends on model access, the calculation time cost is much higher than the time cost of calculating the tf-idf value, so this step can effectively narrow the scope of candidate attack words and improve the efficiency of generating the attack word set.
[0078] Step S103, selecting words whose tf-idf scores are less than a preset second threshold, and marking their word parts of speech to form a part-of-speech dictionary corresponding to the data set.
[0079] After calculating the tf-idf scores of all words, it is necessary to construct a part-of-speech dictionary that is used in the subsequent sentence generation stage. In this embodiment, we select the words with the lowest 50% of tf-idf scores as common words, and use a part-of-speech tagging tool to tag the common words. The common words and their parts of speech together form a part-of-speech dictionary. The storage structure of the part-of-speech dictionary is as follows: {part-of-speech: common word set}.
[0080] Step S104, using a neural network integration strategy to score the offensiveness of the candidate attack words under different data set labels, and using the candidate attack words with attack scores greater than a preset third threshold as basic attack words corresponding to the data set label.
[0081] In the embodiment of the present invention, a neural network integration strategy is used, and basic attack words are selected according to the offensiveness of the words. The specific steps are as follows: Figure 3 As shown:
[0082] Step S1041, input the samples in the data set into the trained neural network model to obtain the confidence of the sample text under the corresponding label.
[0083] Step S1042: Add the candidate attack word to the end of the sample text, and calculate the confidence of the sample text after adding the candidate attack word under the corresponding label.
[0084] Step S1043: taking the difference between the two confidences as the attack score of the candidate attack word in the sample text.
[0085] Through the above steps, we can get a set of candidate attack words, add the candidate attack words in the set to the end of the text x in the original sample (x, y), and get a new sample text x 1 , the original sample text x and the new sample text x 1 The input is fed into the pre-trained neural network model to calculate the confidence of the two texts under the original sample label y. We take the degree of reduction in confidence under category y before and after the candidate attack word is added as the attack score of the word in text x.
[0086] The attack score reflects the offensiveness of the candidate attack word. The higher the attack score, the stronger the offensiveness. If the attack score is negative, it is considered that the attack word is not offensive to the text and is assigned a value of 0. w (x) represents the attack score of word w in text x, then S w (x) can be expressed as:
[0087]
[0088] Among them, x is the text of the sample in the dataset, x w is the text after adding the candidate attack word w to the end of the text x, f y (x) represents the confidence of the neural network model predicting text x as label y, f y (x w ) indicates that the neural network model converts this article x w The confidence of the prediction being label y;
[0089] Step S1044, obtaining sample data with the same label in the data set as a subset, and generating an attack score of the candidate attack word in the subset according to the attack score of each data in the subset.
[0090] Since different texts may have the same label in the dataset, we define the data with the same label in the dataset as a subset subD. For the candidate attack word w∈D, the following formula is used to calculate the attack score of the candidate attack word w in the subset subD:
[0091]
[0092] Among them, x i represents the data in the subset subD, and m represents the total number of texts in the subset subD.
[0093] Step S1045, according to the pre-trained multiple neural network models, respectively obtain the attack score of the candidate attack word in the subset under each of the neural network models.
[0094] Step S1046, performing weighted averaging on the multiple attack scores to obtain a final attack score of the candidate attack word in the subset, and taking the candidate attack word whose final attack score is greater than a preset third threshold as a basic attack word corresponding to the label of the subset.
[0095] In order to make the selection of basic attack words more reasonable and more aggressive, we adopted a neural network integration strategy to select basic attack words. Since neural network models are prone to overfitting of high variance data, it is difficult to reproduce the results, and the results of the model are also extremely sensitive to the initialization parameters, and each model has its own characteristics, and the error predicted by each model is different, so we trained multiple models and combined the prediction results together to reduce the variance, and the results will be better than the results of any single model.
[0096] In this embodiment, the bagging method in the integrated strategy is used for the selection of attack words, that is, by training 1 to n models and combining their decision information to jointly determine the attack score of the candidate attack words. Among them, the model structure includes but is not limited to CNN, RNN, LSTM, etc., and the appropriate number and type of models can be selected according to the actual situation, and no specific limitation is made here.
[0097] For models 1 to n, the candidate attack words are scored for offensiveness respectively, and the final attack score of the candidate attack words is obtained by weighted average. That is, formula (3) is updated as:
[0098]
[0099] Where n is the number of pre-trained neural network models, Represented by model f k The calculated candidate attack word w in the text x i Attack score in Represents the model f k The weight of
[0100] in
[0101] acc k For model f k The classification accuracy after training, the denominator is the sum of the accuracy of all models, The value of is the result after normalization of the model accuracy.
[0102] The final attack score of the candidate attack words corresponding to the label y is calculated by formula (4), and the words with the top 5% scores are selected as basic attack words. The selection ratio can be adjusted according to the actual situation, but in order to ensure the attack efficiency, it will generally not be higher than 5% of the total number of candidate attack words.
[0103] The integration strategy can combine multiple weak learners to obtain a better and more comprehensive strong classifier. The embodiment of the present invention uses the integration strategy to calculate the word attack score, avoiding the bias of the single classifier scoring, making the selected attack words more aggressive and universal.
[0104] Step S105, obtaining all words that are synonyms of the basic attack word according to a synonym dictionary, and forming an attack word set corresponding to the data set label with all synonyms corresponding to the basic attack word.
[0105] After obtaining the basic attack words corresponding to the labels in the data set, the basic attack words are expanded using a synonym dictionary based on Wordnet, and all words that are synonyms of the basic attack words are selected from the synonym dictionary, and these words are used as the final attack words to form the attack word set corresponding to the label. Of course, other synonym dictionaries can also be used for expansion, and no specific restrictions are made here.
[0106] Step S20, according to the label of the original sample, select the attack word set corresponding to the label, and select the word with the highest attack score from the attack word set as the attack word.
[0107] Through the above steps, we can get the attack word sets corresponding to different labels. For the target attack model we use, according to the text label of the original sample in the input data set, we use formula (2) to select the word with the highest attack score under the text from the attack dictionary corresponding to the label as the attack word.
[0108] Among them, the target attack model used can be one of the n trained models. In this case, the attack method provided in this embodiment is implemented very effectively. The target attack model is also a new deep learning model. In this case, the attack method proposed in this embodiment is still effective, because the attack words proposed in this embodiment are universal and transferable, that is, the attack words generated using 1 to n trained models are also offensive under the new model.
[0109] Step S30, according to a preset sentence template, select the syntactic rule corresponding to the part of speech of the attack word, and select the part of speech words corresponding to the syntactic rule from the part of speech dictionary, and combine the part of speech words and the attack word to form a sentence that conforms to the syntactic rule.
[0110] Through the above steps, a suitable attack word is selected from the attack word set as the attack word for a certain adversarial text generation task. The steps of generating a complete attack sentence based on the attack word are as follows: Figure 4 As shown:
[0111] Step S301, marking the part of speech of the attack word, and selecting a syntactic rule corresponding to the part of speech from a preset sentence template.
[0112] Step S302: Select a part-of-speech word corresponding to the syntactic rule from the part-of-speech dictionary, and combine the part-of-speech word and the attack word to form a sentence that complies with the syntactic rule.
[0113] Step S303, checking the sentence for grammatical correctness to obtain a grammatically correct sentence.
[0114] After selecting the most offensive words to the input original sample text according to formula (2), part-of-speech tools such as Stanford CoreNLP, NLTK, TextBlob and other third-party library toolkits in the field of natural language processing are used to perform part-of-speech tagging on the offensive words.
[0115] At the same time, we have also set up sentence templates in advance based on basic rules such as grammatical structure. The templates contain different numbers of syntactic rules corresponding to different parts of speech of words. The syntactic rules are the number of words contained in the sentence and the parts of speech of the words at each position. It should be understood that the sentence template is set according to the actual situation. The syntactic rules corresponding to the parts of speech only need to comply with the grammatical rules, and there are no other specific rules.
[0116] According to the part of speech of the attacking word, the syntactic rule corresponding to the part of speech is selected from the sentence template, where the part of speech may contain more than one syntactic rule. Here we randomly select one of the syntactic rules to generate an attacking short sentence.
[0117] In step S10, a part-of-speech dictionary corresponding to the data set is obtained. Here, we select words of other parts of speech except the offensive part of speech that conform to the syntactic rules from the part-of-speech dictionary. These words and the offensive words together form a sentence that conforms to the syntactic rules.
[0118] For the generated sentences, use grammar checking tools such as pyflakes, pylint, etc. to check their grammatical correctness. If there is no grammatical error, the sentence is output as an attack short sentence. If a grammatical error occurs, it is necessary to reselect the syntactic rule corresponding to the attack word or reselect words from the part-of-speech dictionary and construct the sentence again until no grammatical error is found, thereby obtaining a grammatically correct sentence that complies with the syntactic rules as an attack short sentence. The grammar checking tools used include but are not limited to tools such as pyflakes, pylint, etc., and any one or more can be flexibly selected for grammar checking according to actual conditions.
[0119] The reason why words are selected from the part-of-speech dictionary to form sentences is that the part-of-speech dictionary contains words with low df-idf scores. These words are often words of low importance and have low contribution to the classification results of the model, thus ensuring that the words that play a major role in the classification results in the generated sentences are attack words, ensuring the attack effect of expanding the attack words into sentences.
[0120] Step S40: adding the sentence to the original sample according to a preset adding condition to obtain a new sample.
[0121] When adding the generated attack phrase to the text of the original sample, two conditions need to be met. The first is that it cannot be inserted at the beginning or end of the text, which can ensure the concealment of the insertion; the second is that it needs to be inserted between two complete sentences to ensure that the integrity of the original sentence is not destroyed. When these two conditions are met, other candidate insertion positions are randomly selected. Inserting at different positions has a slight impact on the classification results of the model, but the change of the insertion position will not have a decisive effect on its attack performance. Considering the cost and benefits of selecting the position, a simple random insertion method is selected.
[0122] Step S50, performing multiple rounds of iterative calculations on the new sample according to preset iterative conditions to obtain an adversarial sample.
[0123] For new samples, it is necessary to determine whether they meet the conditions of adversarial samples. If not, the next round of iterative calculation is required. The specific steps are as follows: Figure 5 As shown:
[0124] Step S501, inputting the new sample into the trained neural network model to obtain a prediction label corresponding to the new sample;
[0125] Step S502, determining whether the predicted label is consistent with the label of the original sample corresponding to the new sample, if the two labels are consistent, regenerating a new sample, if the two labels are inconsistent, using the new sample as an adversarial sample.
[0126] The new sample is input into the target adversarial model for prediction to obtain its corresponding predicted label. The predicted label is compared with the label of the original sample before the attack sentence is added to the new sample. If the two labels are consistent, it means that the adversarial sample is not successfully generated. At this time, it is necessary to re-select another word from the attack dictionary as the attack word, regenerate the attack sentence according to the new attack word, add the newly generated sentence back to the original sample to generate a new sample, and then perform label prediction on the newly generated new sample until the predicted label of the new sample is inconsistent with the label of the original sample. At this time, the new sample is output as a text adversarial sample.
[0127] In this embodiment, the adversarial sample generation stage directly obtains the attack words to generate perturbation short sentences. Each perturbation only needs to access the target model once. Through experiments, it can be found that the perturbation added in one attack often does not exceed 5 times, that is, the total number of visits is ≤5. Compared with the existing black box attack technology, the number of model visits is often related to the length of the text. For medium and long texts, the number of visits may be as many as 100 times. Therefore, the method proposed in the present invention greatly shortens the number of times the attack model is visited and improves the attack efficiency.
[0128] The embodiment of the present invention is applicable to data sets of various types of text classification and text sentiment analysis, and also has a good implementation effect in long texts. Here, we take the classic data set IMDB in the field of natural language processing, namely movie review data, as an example to describe in detail the entire process of the text adversarial sample generation method based on attack word guided sentence generation in the embodiment of the present invention.
[0129] First, based on the IMDB dataset, multiple stable classifiers are trained. The model structure is not limited to CNN, RNN, LSTM, etc. The target attack model can be one of the trained models or a new deep learning model.
[0130] Before the attack, build an attack dictionary in advance to increase the attack speed. First, clean and segment IMDB, build a corpus dictionary to store all the words that appear in the data set, and calculate according to the above steps to generate the attack word set of IMDB. Since IMDB is a binary classification data set with labels "Positive" and "Negative", there are two attack word sets in the IMDB data set, namely pos_words and neg_words. Figure 6According to the word part of speech, we pre-set the sentence template according to the grammatical rules. The sentence template contains different numbers of grammatical rules corresponding to the part of speech, such as Figure 7 shown.
[0131] At the same time, we select the words with the lowest 50% of tf-idf scores as common words, annotate their parts of speech through tools, and form a part-of-speech dictionary corresponding to the IMDB dataset. The format of the part-of-speech dictionary is {part-of-speech: common word set}. Take part of the IMDB part-of-speech dictionary as an example:
[0132] 'PRON':['Its','You','Who',…,'Anything'],
[0133] 'VERB':['saw','got','gone',…,'said','acted','took'],
[0134] 'ADP':['in','with','for',…,'after','over'],
[0135] …
[0136] 'NOUN':['film','movie',…,'story','character','role']
[0137] Select the original sample (x, positive), where
[0138] For the original sample (x, y), where x = [x1, x2, ... x n ], x ix is a complete sentence after being separated into sentences, y is positive, and the attack word set pos_words corresponding to the label positive is obtained. The attack words in pos_words are added to the end of x in sequence, and the attack score is calculated according to formula (2). Assuming the attack score score = [0.434, 0.123, 0.402, ..., 0.327, 0.219], since there are 27 elements in pos_words, there will also be 27 corresponding elements in score. The highest score is 0.434, and the corresponding attack word is pointless. The tool uses part-of-speech tagging to obtain its part-of-speech ADJ. According to the sentence template, there are four syntactic rules corresponding to the adjective ADJ. Here, the corresponding syntactic rule structure RNBA (pronoun + noun + copula + adjective A) is randomly selected. The attack word corresponds to the fourth word of the generated sentence, and the remaining three words are obtained from the part-of-speech dictionary, that is, the pronoun, noun, and copula are obtained from the part-of-speech dictionary in sequence. According to the PNBA structure, the obtained words are combined into a short sentence s = That scene is pointless. Use the tool pyflakes to check the grammatical correctness of s. If s has grammatical errors, reselect the grammatical rules and the words that make up the sentence until the generated sentence s has no grammatical errors.
[0139] For the generated sentence s, add it to the original sample to generate a new sample x′=[x1,x2,…,s,…x n ], input x′ into the target adversarial model, and get the predicted label y′. If y′=y, reselect the attack word to generate the sentence, and then regenerate the new sample, and then perform label prediction until y′ is Negative, which is inconsistent with the original input label Positive, and satisfies the adversarial sample generation adjustment. x′ is the adversarial sample corresponding to x, that is, x′=“the most striking feature about this well acted film,is the almost surreal images of the eraand time it was shot it…That scene is pointless.It's all too often when colloquialisms of today's world are carelessly used in movies about another time and place.”
[0140] The embodiment of the present invention provides a method for generating text adversarial samples based on attack word-guided sentence generation. Compared with the previous attack method based on modifying words and characters, which will damage the original text to varying degrees and easily generate grammatical errors during the modification process, the present invention obtains attack words through a neural network integration strategy, and expands the attack words into attack short sentences and adds them to the original text. It can not only achieve effective attacks at low disturbance rates, but also has low destructiveness to the original text and does not generate spelling errors and grammatical errors. It also has a good implementation effect in long texts. Compared with traditional methods, the text adversarial sample generation method of the present invention is not only more aggressive, but also greatly shortens the number of times the attack model is accessed, thereby improving the attack efficiency.
[0141] See also Figure 8 Based on the same inventive concept, the text adversarial sample generation system based on attack word guided sentence generation provided by the second embodiment of the present invention includes:
[0142] The word set dictionary generation module 10 is used to obtain a data set, perform tf-idf score calculation on the words in the data set, and obtain a part-of-speech dictionary of the data set and an attack word set corresponding to different tags in the data set.
[0143] The attack word selection module 20 is used to select an attack word set corresponding to the label of the original sample from the data set, and select a word with the highest attack score from the attack word set as the attack word.
[0144] The sentence generation module 30 is used to select the syntactic rule corresponding to the part of speech of the attack word according to a preset sentence template, and select the part of speech words corresponding to the syntactic rule from the part of speech dictionary, and combine the part of speech words and the attack word to form a sentence that conforms to the syntactic rule.
[0145] The new sample generating module 40 is used to add the sentence to the original sample according to a preset adding condition to obtain a new sample.
[0146] The adversarial sample generation module 50 is used to perform multiple rounds of iterative calculations on the new sample according to preset iterative conditions to obtain an adversarial sample.
[0147] The technical features and technical effects of the text adversarial sample generation system based on attack word guided sentence generation proposed in the embodiment of the present invention are the same as the method proposed in the embodiment of the present invention, and will not be repeated here. Each module in the above-mentioned text adversarial sample generation system based on attack word guided sentence generation can be implemented in whole or in part through software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.
[0148] See also Fig. 9 , an internal structure diagram of a computer device in an embodiment, the computer device can specifically be a terminal or a server. The computer device includes a processor, a memory, a network interface, a display and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a text adversarial sample generation method based on attack word guided sentence generation is implemented. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covered on the display screen, or a key, trackball or touchpad set on the computer device housing, or an external keyboard, touchpad or mouse, etc.
[0149] It can be understood by those skilled in the art that Fig. 9 The structure shown in the figure is merely a block diagram of a portion of the structure related to the present application scheme, and does not constitute a limitation on the computer device to which the present application scheme is applied. The specific computing device may include more or fewer components than those shown in the figure, or combine certain components, or have the same component arrangement.
[0150] In addition, an embodiment of the present invention further provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above method when executing the computer program.
[0151] In addition, an embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the above method are implemented.
[0152] In summary, the embodiment of the present invention provides a text adversarial sample generation based on attack word guided sentence generation. The method obtains a data set, calculates the tf-idf score of the words in the data set, obtains the part-of-speech dictionary of the data set and the attack word set corresponding to different labels in the data set; selects the attack word set corresponding to the label of the original sample from the data set, and selects the word with the highest attack score from the attack word set as the attack word; selects the syntactic rule corresponding to the part-of-speech of the attack word according to the preset sentence template, and selects the part-of-speech word corresponding to the syntactic rule from the part-of-speech dictionary, and the part-of-speech word and the attack word together constitute a sentence that conforms to the syntactic rule; according to the preset adding condition, the sentence is added to the original sample to obtain a new sample; according to the preset iteration condition, the new sample is iterated for multiple rounds to obtain an adversarial sample. The method generates a new sample by expanding the attack word into a sentence and adding it to the original sample, which has the characteristics of low modification rate and high offensiveness, and will not cause spelling errors and grammatical errors, and will not destroy the content integrity of the original text. At the same time, it also shortens the number of times the attack model is accessed, and improves the attack efficiency.
[0153] Each embodiment in this specification is described in a progressive manner, and the same or similar parts of each embodiment can be directly referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. It should be noted that the technical features of the above-mentioned embodiments can be combined arbitrarily. In order to make the description concise, all possible combinations of the technical features in the above-mentioned embodiments are not described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0154] The above-mentioned embodiments only express several preferred implementation modes of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for ordinary technicians in the technical field, several improvements and substitutions can be made without departing from the technical principles of the present invention, and these improvements and substitutions should also be regarded as the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be based on the protection scope of the claims.
Claims
1. A method for generating text adversarial samples based on attack word guided sentence generation, characterized in that: include: Obtain a data set, calculate the tf-idf score for the words in the data set, and obtain a part-of-speech dictionary of the data set and a set of attack words corresponding to different tags in the data set; Selecting an attack word set corresponding to the label of the original sample from the data set, and selecting a word with the highest attack score from the attack word set as the attack word; According to a preset sentence template, a syntactic rule corresponding to the part of speech of the attack word is selected, and a part of speech word corresponding to the syntactic rule is selected from the part of speech dictionary, and the part of speech word and the attack word are combined to form a sentence that conforms to the syntactic rule; According to a preset adding condition, the sentence is added to the original sample to obtain a new sample; Perform multiple rounds of iterative calculations on the new sample according to preset iterative conditions to obtain an adversarial sample; The steps of obtaining a data set, calculating tf-idf scores for words in the data set, and obtaining a part-of-speech dictionary of the data set and an attack word set corresponding to different tags in the data set include: Preprocess all texts in the data set and then segment them to obtain a corpus dictionary corresponding to the data set, wherein the corpus dictionary stores all words appearing in the data set; Calculating the tf-idf scores of all words in the corpus dictionary, and taking the words whose tf-idf scores are greater than a preset first threshold as candidate attack words; Select words whose tf-idf score is less than a preset second threshold, and mark their word parts of speech to form a part-of-speech dictionary corresponding to the data set; Using a neural network integration strategy to score the offensiveness of the candidate attack words under different data set labels, and using the candidate attack words whose attack scores are greater than a preset third threshold as basic attack words corresponding to the data set label; According to the synonym dictionary, all words that are synonyms of the basic attack word are obtained, and all synonyms corresponding to the basic attack word form an attack word set corresponding to the data set label; The step of scoring the offensiveness of the candidate attack words under different data set labels by using a neural network integration strategy and using the candidate attack words whose attack scores are greater than a preset third threshold as the basic attack words corresponding to the data set labels includes: Input the samples in the data set into the trained neural network model to obtain the confidence of the sample text under the corresponding label; Adding the candidate attack word to the end of the sample text, and calculating the confidence of the sample text after adding the candidate attack word under the corresponding label; Using the difference between the two confidences as the attack score of the candidate attack word in the sample text; Acquire sample data with the same label in the data set as a subset, and generate an attack score of the candidate attack word in the subset according to the attack score of each data in the subset; According to a plurality of pre-trained neural network models, respectively obtaining an attack score of the candidate attack word in the subset under each of the neural network models; A weighted average is performed on the multiple attack scores to obtain a final attack score of the candidate attack word in the subset, and the candidate attack word whose final attack score is greater than a preset third threshold is used as a basic attack word corresponding to the label of the subset.
2. The method for generating text adversarial samples based on attack word guided sentence generation according to claim 1, characterized in that: The tf-idf scores of all words in the dictionary are calculated using the following formula: Among them, N w is the total number of times word w appears in the text, N is the total number of words in the text, D is the total number of texts in the dataset, and D w is the number of texts containing word w; The attack score of the candidate attack word in the sample text is calculated using the following formula: Among them, x is the text of the sample in the dataset, x w is the text after adding word w to the end of text x, f y (x) represents the confidence of the neural network model predicting text x as label y, f y (x w ) indicates that the neural network model converts this article x w The confidence of the prediction being label y; The attack score of the candidate attack word in the subset is calculated using the following formula: Among them, x i represents the data in the subset subD, and m represents the total number of texts in the subset subD; The final attack score of the candidate attack word in the subset is calculated using the following formula: Where n is the number of pre-trained neural network models, Represented by model f k The calculated word w in the text x i Attack score in Represents the model f k The weight of acc k For model f k The classification accuracy after training.
3. The method for generating text adversarial samples based on attack word guided sentence generation according to claim 1, characterized in that: The step of selecting the syntactic rule corresponding to the part of speech of the attack word according to the preset sentence template, selecting the part of speech word corresponding to the syntactic rule from the part of speech dictionary, and combining the part of speech word with the attack word to form a sentence conforming to the syntactic rule comprises: Marking the part of speech of the attack word, and selecting the syntactic rule corresponding to the part of speech from a preset sentence template; Selecting a part-of-speech word corresponding to the syntactic rule from the part-of-speech dictionary, and combining the part-of-speech word with the attack word to form a sentence that complies with the syntactic rule; The sentence is checked for grammatical correctness to obtain a grammatically correct sentence.
4. The method for generating text adversarial samples based on attack word guided sentence generation according to claim 1, characterized in that: The preset adding condition is that the adding position of the generated sentence is between two complete sentences in the text of the original sample, and cannot be inserted into the beginning and end of the text of the original sample.
5. The method for generating text adversarial samples based on attack word guided sentence generation according to claim 1, characterized in that: The step of performing multiple rounds of iterative calculations on the new sample according to the preset iterative conditions to obtain the final adversarial sample includes: Input the new sample into the trained neural network model to obtain the predicted label corresponding to the new sample; Determine whether the predicted label is consistent with the label of the original sample corresponding to the new sample. If the two labels are consistent, regenerate a new sample. If the two labels are inconsistent, use the new sample as an adversarial sample.
6. A text adversarial sample generation system based on attack word guided sentence generation, characterized in that: The system comprises: The word set dictionary generation module is used to obtain a data set, calculate the tf-idf score of the words in the data set, obtain the part-of-speech dictionary of the data set and the attack word set corresponding to different tags in the data set, and the specific steps include: Preprocess all texts in the data set and then segment them to obtain a corpus dictionary corresponding to the data set, wherein the corpus dictionary stores all words appearing in the data set; Calculating the tf-idf scores of all words in the corpus dictionary, and taking the words whose tf-idf scores are greater than a preset first threshold as candidate attack words; Select words whose tf-idf score is less than a preset second threshold, and mark their word parts of speech to form a part-of-speech dictionary corresponding to the data set; Using a neural network integration strategy to score the offensiveness of the candidate attack words under different data set labels, and using the candidate attack words whose attack scores are greater than a preset third threshold as basic attack words corresponding to the data set label; According to the synonym dictionary, all words that are synonyms of the basic attack word are obtained, and all synonyms corresponding to the basic attack word form an attack word set corresponding to the data set label; The step of scoring the offensiveness of the candidate attack words under different data set labels by using a neural network integration strategy and using the candidate attack words whose attack scores are greater than a preset third threshold as the basic attack words corresponding to the data set labels includes: Input the samples in the data set into the trained neural network model to obtain the confidence of the sample text under the corresponding label; Adding the candidate attack word to the end of the sample text, and calculating the confidence of the sample text after adding the candidate attack word under the corresponding label; Using the difference between the two confidences as the attack score of the candidate attack word in the sample text; Acquire sample data with the same label in the data set as a subset, and generate an attack score of the candidate attack word in the subset according to the attack score of each data in the subset; According to a plurality of pre-trained neural network models, respectively obtaining an attack score of the candidate attack word in the subset under each of the neural network models; Taking a weighted average of the plurality of attack scores to obtain a final attack score of the candidate attack word in the subset, and taking the candidate attack word whose final attack score is greater than a preset third threshold as a basic attack word corresponding to the label of the subset; An attack word selection module, used to select an attack word set corresponding to the label of the original sample from the data set, and select a word with the highest attack score from the attack word set as the attack word; A sentence generation module, configured to select a syntactic rule corresponding to the part of speech of the attack word according to a preset sentence template, and select a part-of-speech word corresponding to the syntactic rule from the part-of-speech dictionary, and combine the part-of-speech word and the attack word to form a sentence that conforms to the syntactic rule; A new sample generation module, used to add the sentence to the original sample according to a preset adding condition to obtain a new sample; The adversarial sample generation module is used to perform multiple rounds of iterative calculations on the new sample according to preset iterative conditions to obtain an adversarial sample.
7. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.
