Text adversarial attack method based on improved quantum-behaved particle swarm optimization algorithm
The improved quantum behavior particle swarm optimization algorithm optimizes the search for adversarial samples in word-level natural language processing, solving the problem of insufficient search capabilities in the existing technology, significantly improving the attack success rate and the correctness of syntax and semantics, while reducing the The change rate of adversarial examples.
Patent Information
- Application Number
- CN202110106877.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-27
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2041-01-27
AI Technical Summary
Existing adversarial attack methods in the field of word-level natural language processing have weak global search capabilities and are prone to 'premature' and falling into local optima, resulting in poor attack effects.
The improved quantum behavioral particle swarm optimization algorithm (ID-QPSO) is used to obtain word alternative word sets through a sememe-based method, and search in the alternative word set space to optimize the generation of adversarial examples.
It improves the grammatical and semantic correctness of adversarial samples, improves the attack success rate, and reduces the sample modification rate, achieving more effective text adversarial attacks.
Smart Images

Figure CN112765355B
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of natural language processing text confrontation attack, and provides a text confrontation attack method based on an improved quantum behavior particle swarm optimization algorithm. Background Art
[0002] Deep neural networks are widely used in various fields such as images, speech, and natural language. Despite this, studies have shown that deep neural networks are very vulnerable to adversarial attacks, that is, adding small perturbations to the input data can change the results given by deep neural networks. For example, after adding perturbations to a panda picture, the semantics of the image does not change to the human eye, but the Convolutional Neural Network (CNN) recognizes the panda as a gibbon. The fragility of deep neural networks has raised concerns about safety. For example, putting very small graffiti on a road sign can prevent the autonomous driving system from correctly identifying the road sign, which may have serious consequences. Research on adversarial attacks can help build more robust deep neural networks and inspire research on how to defend against adversarial attacks. However, research on adversarial attacks in the field of natural language processing is far less than research on adversarial attacks in the field of images. This is partly because the pixel values of an image are continuous, making it easy to implement gradient-based adversarial attacks, and small perturbations of pixel values are almost imperceptible to the human eye and will not change the semantics of the image. Natural language is discrete, and small changes in words may also lead to changes in semantics. Deep neural networks are also widely used in natural language processing, including spam filtering, sentiment analysis, and fake news detection. Therefore, adversarial attacks in the field of natural language processing are also very valuable.
[0003] At present, adversarial attack methods in the field of natural language processing can be divided into three categories according to the elements changed during the adversarial attack: character-level adversarial attack, word-level adversarial attack, and sentence-level adversarial attack. Changes at the character level may often cause grammatical errors, and spelling checks or grammar checks can prevent such attacks. Sentence-level adversarial attack methods achieve the effect of adversarial attack by adding sentences or repeating sentences. Word-level adversarial attack methods generally construct adversarial samples by replacing words. Compared with adversarial samples generated by character-level adversarial attack methods, samples generated by word-level attack methods have better semantic coherence and fewer grammatical errors. Compared with adversarial samples obtained by sentence-level attack methods, adversarial samples generated by word-level attack methods have a relatively small change rate and are less likely to be detected. Word-level adversarial methods can achieve relatively good results in both semantic and grammatical coherence and change rate.
[0004] However, the existing word-level adversarial methods have weak global search capabilities for adversarial samples, and are prone to "premature maturity" and falling into local optimality, so there is a large room for improvement. The present invention proposes an improved word-level text adversarial method, optimizes the search optimization algorithm, and uses an improved quantum-behaved particle swarm optimization algorithm (ID-QPSO) to more effectively search for adversarial samples. Experiments have shown that the method of the present invention has achieved better results. Summary of the invention
[0005] The present invention proposes a text adversarial attack method based on semantic primitives and an improved quantum-behavior particle swarm optimization algorithm. Specifically, a semantic primitive-based method is first used to obtain a word's alternative word set, and then an improved discrete quantum-behavior particle swarm optimization algorithm is applied to search in the alternative word set space, and finally an adversarial sample is obtained.
[0006] The technical solution of the present invention:
[0007] The text adversarial attack method based on the improved quantum behavior particle swarm optimization algorithm has the following steps:
[0008] Step 1: Traverse the words at each position of the input sentence and use the semantic-based method to obtain the replacement word for each word;
[0009] Step 2: Copy the sentence M times to establish a particle swarm with a number of M;
[0010] Step 3: Traverse each particle, find the word that causes the most damage to the model at each position of the particle, and perform mutation operation based on it;
[0011] Step 4: Input the mutated particles into the model and observe the model prediction value. If the attack is successful, output the result and the step ends;
[0012] Step 5: If the attack in step 4 is unsuccessful, update the optimal position of the individual particles and the global optimal position of the particle swarm;
[0013] Step 6: Input the updated particle swarm into the model and observe the model prediction value. If the attack is successful, output the result and the step ends;
[0014] Step 7: If the attack in step 6 is unsuccessful, update the optimal position of individual particles and the global optimal position of the particle swarm;
[0015] Step 8: Repeat steps 3 to 7 until the attack is successful or the maximum number of iterations is reached.
[0016] Furthermore, we first define the search space of the improved quantum-behavior particle swarm optimization algorithm ID-QPSO. Let the input sentence be x orig , the sentence length is D, and its jth word is represented by x orig,j , xorig,j The set composed of its alternative words is S(x orig,j ), then the search space of the algorithm is S = {S(x orig,j )|j=1,2,...,D}, that is, the entire search space is D-dimensional, and each dimension corresponds to a corresponding set of alternative words. An adversarial sample is used as a particle in the ID-QPSO algorithm, and the particle swarm contains a total of M particles.
[0017] Furthermore, in step 2, the input sentence is copied M times as the positions of M initial particles, and a particle swarm of M is established. The initial position vector of the i-th particle is recorded as
[0018] Further, the step 3 described above is: traverse each particle, and at the tth iteration, traverse At each position of the model, find the word that is most harmful to the model at each position and form a vector composed of replacement words. Then, use the vector composed of the replacement words to replace Perform mutation operation:
[0019]
[0020] in, Indicates the word after mutation, represents the word on the jth dimension of the ith particle at the tth iteration, is the word that is most harmful to the model in the jth dimension of the i-th particle. yes Variant The probability of q is an independent random variable uniformly distributed on [0,1]. represents the value of the variable at the tth iteration of the jth dimension of the ith particle; and Positively correlated with the damage to the model.
[0021] Furthermore, the step 4: the mutated particles Input the attacked model F and observe the model prediction value. If The confrontation is successful. This is the adversarial sample that was searched.
[0022] Further, step five: if the attack is unsuccessful, update the optimal position of the individual particles of the particle swarm and the global optimal position according to equations (2) to (8):
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030] Wherein, formula (2) represents the discretization process, Represents the discretization operation, Discrete to 0 or 1; is the word on the jth dimension of the mutated particle i, x org,j is the word of the jth dimension of the original input sentence, The physical meaning in this discrete case is the average probability of the word in the jth dimension being replaced; The best position of the particle in the jth dimension at the tth iteration and all previous moments is recorded, that is, the optimal position of the individual particle; The best position of the entire particle swarm in the jth dimension at the tth iteration and all previous moments is recorded, that is, the global optimal position; is located in and A position in between attracts particles to come closer; It is an intermediate variable whose value may be outside the interval [0,1], so it is converted into a probability value through the sigmoid function. It means the probability that the word of the jth dimension of the ith particle should be replaced at the tth iteration, r is a random variable uniformly distributed on [0,1]; α is the contraction and expansion coefficient, T is the maximum number of iterations, t is the current number of iterations, and μ are two independent random variables uniformly distributed on [0,1], They represent independent random variables r, and μ is the value of μ at the tth iteration of the jth dimension of the ith particle.
[0031] Further, step seven: if the attack is unsuccessful, update the optimal position of the individual particles in the particle swarm and the global optimal position
[0032] Furthermore, the search space is composed of alternative words for each word in the sentence, and is therefore a discrete space.
[0033] Beneficial effects of the present invention:
[0034] 1. The adversarial samples obtained by the method of the present invention can better maintain the correctness of the original grammatical semantics;
[0035] 2. The attack success rate of the method of the present invention is high;
[0036] 3. The method of the present invention has a low rate of change to the sample. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 It is a text adversarial algorithm based on ID-QPSO.
[0038] Figure 2 right The mutation operation.
[0039] Figure 3 The sememe tree of notebook in HowNet and words that share the same sememe with notebook. DETAILED DESCRIPTION
[0040] The technical solution of the present invention is further described below based on embodiments and drawings.
[0041] In linguistics, a semantic primitive refers to the smallest indivisible semantic unit. A word can be represented by a set of a finite number of semantic primitives. Dong Zhendong and others used 2,089 semantic primitives to annotate about 100,000 Chinese words and their corresponding English words, forming a semantic primitive knowledge base, namely HowNet. Each word in HowNet is represented as a tree structure. Since a word may have multiple meanings, each meaning is a semantic primitive tree. For example, Figure 3 As shown in the figure, the word "notebook" has two meanings: "notebook" and "laptop". In HowNet, both meanings are represented by semantic primitives, forming a tree structure. Another similar knowledge base is WordNet, a semantic knowledge base built by Princeton University. WordNet is a knowledge base based on words, while HowNet is a knowledge base based on concepts. Compared with WordNet, HowNet can find more semantically related words.
[0042] The data set used in this example
[0043] In order to verify the effectiveness of our attack method, we conducted experiments on three datasets, namely the IMDB dataset, the SST-2 dataset, and the SNLI dataset. All three datasets are text classification tasks. The IMDB dataset and the SST-2 dataset are sentiment classification tasks, and the SNLI dataset is a natural language inference task.
[0044] The IMDB dataset is a dataset containing 50,000 movie reviews, 25,000 of which are used as training sets and 25,000 as test sets. Each review is labeled as positive or negative.
[0045] The SST-2 dataset is also a dataset in the field of movie reviews. It contains about 11,000 reviews and is divided into training set, validation set and test set.
[0046] The SNLI dataset is a natural language inference dataset published by Stanford University. The dataset contains 570,000 pairs of sentences. The relationship between each pair of sentences is labeled as contradiction, implication, or neutral. 550,000 pairs are divided into training sets, 10,000 pairs as validation sets, and 10,000 pairs as test sets.
[0047] Attack model and comparison method in this embodiment
[0048] We choose the Bi-LSTM model and the BERT model as the attacked models. The Bi-LSTM followed by the maximum pooling model was proposed by Conneau et al. in 2017 [CONNEAU A, KIELA D, SCHWENK H, et al. Supervised learning of universal sentence representations from natural language inference data: Proceedings of the 2017 Conference on Empirical Methods in Natural Language Processing [C], 2017.]. The BERT model is a pre-trained language model proposed by Devlin et al. in 2019, which achieved the best results on 11 language tasks [DEVLIN J, CHANG M, LEE K, et al. Bert: Pre-training of deep bidirectional transformers for language understanding: Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Lin-guistics: Human Language Technologies, Volume 1 (Long and Short Papers) [C], 2019.]. The Bi-LSTM of the present invention uses 300-dimensional GloVe word vectors [
[0049] PENNINGTON J, SOCHER R, MANNING C D. Glove: Global vectors for word representation: Proceedings of the 2014 conference on empirical methods in natural language processing (EMNLP) [C], 2014.], the hidden layer dimension is set to 128 dimensions; the base version used by the BERT model.
[0050] We selected three methods as experimental comparison methods. The first comparison method is the method proposed by Alzantot et al. [ALZANTOT M, SHARMA Y, ELGOHARY A, et al. Generating natural language adversarial examples: Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing [C], 2018.], which searches for replacement words in word vectors, controls the number of replacement words by limiting the distance in the word vector space, and then uses a genetic algorithm to search for adversarial samples. The second comparison method is the method proposed by Ren et al. [REN S, DENG Y, HE K, et al. Generating natural language adversarial examples through probability weighted word saliency: Proceedings of the 57th annual meeting of the association for computational linguistics [C], 2019.], which uses synonyms as replacement words and then uses a greedy algorithm to search for adversarial samples. The third method is the method proposed by Zang et al. [ZANG Y,QI F,YANG C,et al.Word-level textual adver-sarial attacking as combinatorial optimization:Proceedings of the 58th AnnualMeeting of the Association for Computational Linguistics[C],2020.], which uses a semantic-based method to obtain replacement words, and then uses a particle swarm optimization algorithm to search for adversarial samples. This method is currently the best method.
[0051] Experimental details and evaluation criteria of this example
[0052] In order to conduct the experiment more effectively, we randomly selected 500 data from the IMDB dataset, and 1,000 data from the SST-2 dataset and the SNLI dataset for the experiment. Similar to the experimental settings of Alzantot et al. and Zang et al., when selecting the sentences to be attacked, the selected sentences must be between 10 and 100 in length and the model can give correct predictions. If the model cannot correctly predict the original sentence, there is no need to conduct an adversarial attack. At the same time, in order to maintain the consistency of the comparative experiment, we set the maximum number of iterations T of the quantum behavior particle swarm optimization algorithm to 20 and the number of particles M in the particle swarm to 60, which is consistent with the experimental settings of Alzantot et al. and Zang et al. The same evaluation criteria are used for the three datasets in the experiment, namely the attack success rate and the average modification rate of the adversarial samples, and the calculation formula is as follows:
[0053]
[0054]
[0055] where |sent success | indicates the number of sentences that were successfully attacked, |sent attack | represents the total number of sentences attacked, c is the number of modified words in the sample, and l is the total number of words in the original input of the sample. We only count successful adversarial samples with a modification rate of no more than 25%, and consider other cases as failed adversarial attacks.
[0056] The experimental results of this example
[0057] Table 1. Success rate of counterattack (%)
[0058]
[0059] The results of the success rate of the adversarial attack are shown in Table 1. The method proposed in this paper achieved the highest success rate in 5 out of 6 experiments. The experimental results also show that for the same classification task, the attack success rate of SST-2 is lower than that of IMDB. Because the average sentence length of the IMDB dataset exceeds 200, while the average sentence length of SST-2 is only about 17, it is easy to exceed the 25% modification rate limit when attacking sentences in SST, causing the attack to fail.
[0060] The results of the modification rate of adversarial samples for successful attacks are shown in Table 2. The method of the present invention achieved the smallest average modification rate of samples in 4 out of 6 experiments. The results of Tables 1 and 2 prove that the attack method of the present invention can achieve a higher attack success rate with a lower modification rate.
[0061] Table 2 Average modification rate of successful adversarial samples (%)
[0062]
[0063] Manual evaluation and case analysis of this embodiment
[0064] In order to evaluate the grammatical and semantic correctness of the generated adversarial samples, we selected 500 sentences from SST-2. The comparative methods and the method of the present invention all successfully attacked the Bi-LSTM model in these 500 sentences, that is, they all generated successful adversarial samples. We asked three professionals to score the 500 original sentences and the adversarial samples generated by each adversarial attack method. The scoring principle is:
[0065] 1. If there are grammatical errors and the meaning is not clear, 1 point will be awarded;
[0066] 2. Only grammatical errors or semantic errors will be scored 2 points;
[0067] 3. 3 points if there are no errors in both grammar and semantics.
[0068] The scoring results are shown in Table 3. The original sentences in the SST-2 dataset are user reviews of movies, so the original sentences also have some grammatical errors or semantic incoherence. The scores of the adversarial samples generated by the adversarial method proposed by Zang et al. and the method of the present invention are similar, because the method of finding replacement words is the same, the replacement word space is consistent, and the grammatical and semantic errors are mainly caused by the inappropriate replacement words, so the scores of the two methods are similar.
[0069] Table 3 Syntax and semantics manual scoring
[0070]
[0071]
[0072] Table 4 lists some adversarial examples generated for the Bi-LSTM model on the SST-2 dataset. We list two sentences and the adversarial examples generated by the Zang et al. method and the present invention method for these two sentences. The changed words in the adversarial examples and the corresponding words in the original text are marked in italics and bold. As can be seen from the table, our method and the Zang et al. method differ in the selection of replacement positions and replacement words due to different search algorithms. In the two cases shown, our algorithm can successfully implement adversarial attacks by changing relatively few words.
[0073] Table 4 Examples of adversarial samples
[0074]
[0075] The present invention proposes an improved word-level text adversarial attack method. By improving the search optimization algorithm, the method of the present invention can search for adversarial samples more effectively. Experiments have shown that the adversarial samples obtained by the method of the present invention have a higher success rate and a lower modification rate while maintaining the original grammatical and semantic correctness, and are a more effective text adversarial method.
Claims
1. A text adversarial attack method based on an improved quantum-behavior particle swarm optimization algorithm, characterized in that: Here are the steps: Step 1: Traverse the words at each position of the input sentence and use the semantic-based method to obtain the replacement word for each word; Step 2: Copy the sentence M times to establish a particle swarm with a number of M; Step 3: Traverse each particle, find the word that causes the most damage to the model at each position of the particle, and perform mutation operation based on it; Step 4: Input the mutated particles into the model and observe the model prediction value; if the attack is successful, output the result and the step ends; Step 5: If the attack in step 4 is unsuccessful, update the optimal position of the individual particles and the global optimal position of the particle swarm; Step 6: Input the updated particle swarm into the model and observe the model prediction value; if the attack is successful, output the result and the step ends; Step 7: If the attack in step 6 is unsuccessful, update the optimal position of the individual particles and the global optimal position of the particle swarm; Step 8: Repeat steps 3 to 7 until the attack is successful or the maximum number of iterations is reached.
2. The text adversarial attack method based on the improved quantum-behavior particle swarm optimization algorithm according to claim 1 is characterized in that: First, define the search space of the improved quantum behavior particle swarm optimization algorithm ID-QPSO; let the input sentence be x orig , the sentence length is D, and its jth word is represented by x orig,j , x orig,j The set composed of its alternative words is S(x orig,j ), then the search space of the algorithm is S = {S(x orig,j )|j=1,2,...,D}, that is, the entire search space is D-dimensional, and each dimension corresponds to a corresponding set of alternative words; an adversarial sample is used as a particle in the ID-QPSO algorithm, and the particle swarm contains a total of M particles.
3. The text counterattack method based on the improved quantum behavior particle swarm optimization algorithm according to claim 1 or 2, characterized in that: Step 2: Input a sentence and copy M copies as the positions of M initial particles to establish a particle swarm with a number of M. Initially, the position vector of the i-th particle is recorded as D is the sentence length.
4. The text counterattack method based on the improved quantum behavior particle swarm optimization algorithm according to claim 1 or 2, characterized in that: Step 3: traverse each particle, and at the tth iteration, traverse For each position of the model, find the word that is most harmful to the model at each position and form a vector composed of replacement words; then, use the vector composed of the replacement words to replace Perform mutation operation: in, Indicates the mutated word, represents the word on the jth dimension of the ith particle at the tth iteration, is the word that is most harmful to the model in the jth dimension of the i-th particle. yes Variant The probability of q is an independent random variable uniformly distributed on [0,1]. represents the value of the variable at the tth iteration of the jth dimension of the ith particle; and Positively correlated with the damage to the model.
5. The text adversarial attack method based on the improved quantum behavior particle swarm optimization algorithm according to claim 3 is characterized in that: Step 3: traverse each particle, and at the tth iteration, traverse For each position of the model, find the word that is most harmful to the model at each position and form a vector composed of replacement words; then, use the vector composed of the replacement words to replace Perform mutation operation: in, Indicates the mutated word, represents the word on the jth dimension of the ith particle at the tth iteration, is the word that is most harmful to the model in the jth dimension of the i-th particle. yes Variant The probability of q is an independent random variable uniformly distributed on [0,1]. represents the value of the variable at the tth iteration of the jth dimension of the ith particle; and Positively correlated with the damage to the model.
6. The text counterattack method based on the improved quantum behavior particle swarm optimization algorithm according to claim 1, 2 or 5, characterized in that: Step 4: The mutated particles Input the attacked model F and observe the model prediction value. If The confrontation is successful. This is the adversarial sample that was searched.
7. The text counterattack method based on the improved quantum behavior particle swarm optimization algorithm according to claim 1, 2 or 5, characterized in that: Step 5: If the attack is unsuccessful, update the optimal position of the individual particles of the particle swarm and the global optimal position. Update the position of the particles according to equations (3) to (9): Wherein, formula (3) represents the discretization process, Represents the discretization operation, Discrete to 0 or 1; is the word on the jth dimension of the mutated particle i, x org,j is the word of the jth dimension of the original input sentence, The physical meaning in this discrete case is the average probability of the word in the jth dimension being replaced; The best position of the particle in the jth dimension at the tth iteration and all previous moments is recorded, that is, the optimal position of the individual particle; The best position of the entire particle swarm in the jth dimension at the tth iteration and all previous moments is recorded, that is, the global optimal position; is located in and A position in between attracts particles to come closer; As an intermediate variable, it is converted into a probability value through the sigmoid function It means the probability that the word of the jth dimension of the ith particle should be replaced at the tth iteration, r is a random variable uniformly distributed on [0,1]; α is the contraction and expansion coefficient, T is the maximum number of iterations, t is the current number of iterations, and μ are two independent random variables uniformly distributed on [0,1], They represent independent random variables r, and μ is the value of μ at the tth iteration of the jth dimension of the ith particle.
8. The text adversarial attack method based on the improved quantum behavior particle swarm optimization algorithm according to claim 6 is characterized in that: Step 5: If the attack is unsuccessful, update the optimal position of the individual particles of the particle swarm and the global optimal position. Update the position of the particles according to equations (10) to (16): Wherein, formula (10) represents the discretization process, Represents the discretization operation, Discrete to 0 or 1; is the word on the jth dimension of the mutated particle i, x org,j is the word of the jth dimension of the original input sentence, The physical meaning in this discrete case is the average probability of the word in the jth dimension being replaced; The best position of the particle in the jth dimension at the tth iteration and all previous moments is recorded, that is, the optimal position of the individual particle; The best position of the entire particle swarm in the jth dimension at the tth iteration and all previous moments is recorded, that is, the global optimal position; is located in and A position in between attracts particles to come closer; As an intermediate variable, it is converted into a probability value through the sigmoid function It means the probability that the word of the jth dimension of the ith particle should be replaced at the tth iteration, r is a random variable uniformly distributed on [0,1]; α is the contraction and expansion coefficient, T is the maximum number of iterations, t is the current number of iterations, and μ are two independent random variables uniformly distributed on [0,1], They represent independent random variables r, and μ is the value of μ at the tth iteration of the jth dimension of the ith particle.
9. The text counterattack method based on the improved quantum behavior particle swarm optimization algorithm according to claim 1, 2, 5 or 8, characterized in that: Step 7: If the attack is unsuccessful, update the optimal position of the individual particles in the particle swarm and the global optimal position 10. The text counterattack method based on the improved quantum behavior particle swarm optimization algorithm according to claim 1, 2, 5 or 8, characterized in that: The search space consists of alternatives to each word in the sentence, and is therefore a discrete space.
