A Chinese adversarial sample generation method and device for text review
By generating and using Chinese adversarial samples, the problem of the text review system's insufficient detection ability of deformed words is solved, the recognition ability and robustness of the model are improved, the training process is simplified, and the need for manual review is reduced.
Patent Information
- Application Number
- CN202011259475.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-12
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2040-11-12
AI Technical Summary
Existing text review systems cannot effectively deal with harmful content bypassing detection through deformed words, resulting in the need for additional manual review, and adversarial samples threaten the security of deep learning models.
By generating Chinese adversarial samples for text review, using word replacement methods to obtain perturbation words, replacing sentences based on importance information, generating adversarial samples that meet preset conditions, and improving the model's recognition capabilities through adversarial training.
It improves the model's ability to identify harmful samples and its robustness, increases the diversity of training data, improves model training efficiency, and reduces the need for manual review.
Smart Images

Figure CN112364641B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence, and in particular to a method and device for generating Chinese adversarial samples for text review. Background Art
[0002] In recent years, with the development of big data technology and the continuous improvement of hardware computing power, deep learning technology has been widely used in many fields, such as computer vision, speech recognition and natural language processing. However, with the rapid development of deep learning technology, the security issues of deep learning models have gradually attracted the attention of researchers. Szegedy et al. first discovered the existence of adversarial examples: input samples formed by deliberately adding small interferences to the data set, causing the model to give an erroneous output with high confidence.
[0003] Adversarial examples reveal the vulnerability of deep learning models and have attracted great attention from researchers. In the field of natural language processing (NLP), adversarial examples against deep learning models have threatened real-world applications including text moderation. Text moderation (i.e. filtering harmful content in text, such as insults, discrimination, personal attacks, racist remarks, etc.) is an important part of NLP applications, among which keyword matching-based and machine learning-based text classification methods are currently the most commonly used moderation methods. However, publishers of harmful content often deform sensitive words that express emotions so that the processed harmful information can bypass the detection of the text moderation system. For example, using "waste" instead of "waste" can achieve the above purpose. In related technologies, keyword-based text moderation systems cannot respond to these deformed words quickly and require additional manual review.
[0004] There is currently no effective solution to many technical problems existing in related technologies. Summary of the invention
[0005] In order to solve the above technical problems or at least partially solve the above technical problems, the present application provides a Chinese adversarial sample generation method and device for text review.
[0006] In a first aspect, the present application embodiment provides a Chinese adversarial sample generation method for text review, including:
[0007] Get the statement information to be processed;
[0008] Segmenting the sentence information to be processed to obtain multiple words;
[0009] determining first importance information of the word;
[0010] Obtaining disturbance words corresponding to each of the words;
[0011] According to the first importance information, the disturbance sentence information is obtained after each of the words in the sentence information to be processed is replaced with a corresponding disturbance word;
[0012] When it is determined that the disturbance statement information satisfies a preset condition, an adversarial sample after a successful attack on the statement information to be processed is obtained according to the disturbance statement information.
[0013] Optionally, as in the aforementioned method, the step of obtaining the disturbance words corresponding to each of the words includes:
[0014] Determine the pinyin and the character form of the word;
[0015] According to the pinyin, at least one character in the word is replaced with a character obtained by pinyin as the disturbance word; or,
[0016] According to the character shape, at least one character in the word is replaced with a character with a similar shape that meets a preset similarity requirement, and the resulting character is used as the disturbance word; or,
[0017] According to the pinyin and the glyph, at least one character in the word is replaced with a homophone and / or a homophone or glyph that meets a preset similarity requirement, and the resulting character is used as the disturbance word.
[0018] Optionally, the method as described above further includes:
[0019] Arrange the words from high to low according to the first importance information to obtain arrangement order information corresponding to each word;
[0020] According to the arrangement order information, replacement order information corresponding to each of the words is determined; the replacement order information is used to determine the order of replacing the words with corresponding disturbance words.
[0021] Optionally, as in the aforementioned method, the step of sequentially obtaining, according to the first importance information, disturbance sentence information after replacing each of the words in the sentence information to be processed with a corresponding disturbance word includes:
[0022] Determine the lowest importance disturbance word with the lowest importance among all the disturbance words corresponding to the word, and obtain the corresponding relationship between the word and the lowest importance disturbance word;
[0023] According to the replacement order information, each of the words is replaced with the corresponding disturbance word of the lowest importance in sequence according to the corresponding relationship, and the disturbance sentence information is obtained.
[0024] Optionally, as in the aforementioned method, determining the lowest importance disturbance word with the lowest importance among all the disturbance words corresponding to the word includes:
[0025] After replacing the words in the sentence information to be processed with the disturbance words, replaced sentence information corresponding to the disturbance words is obtained;
[0026] Deleting the disturbance word in the replaced sentence information to obtain second missing-word sentence information corresponding to the disturbance word;
[0027] Determine, according to a preset text audit model, a third weight value corresponding to the replaced sentence information and a fourth weight value corresponding to the second missing-word sentence information;
[0028] Obtaining second importance information corresponding to the disturbance word according to differences between the third weight value and the fourth weight value;
[0029] The lowest importance disturbance word corresponding to the word is obtained according to the second importance information corresponding to the disturbance word corresponding to the word.
[0030] Optionally, as in the aforementioned method, determining the importance information corresponding to each of the words includes:
[0031] Deleting the words in the sentence information to be processed respectively to obtain first word-missing sentence information corresponding to the words;
[0032] Determine a first weight value corresponding to the to-be-processed sentence information and a second weight value corresponding to the first missing-word sentence information according to a preset text audit model;
[0033] The first importance information corresponding to the word is obtained according to the difference between the first weight value and the second weight value.
[0034] Optionally, as in the aforementioned method, the disturbance statement information satisfies a preset condition, including:
[0035] In the disturbance sentence information, the number of the disturbance words is less than or equal to a preset upper limit threshold of the number of disturbance words; and
[0036] The ratio of the number of the disturbance words to the total number of words in the disturbance sentence information is less than a preset disturbance ratio threshold; and
[0037] The first prediction label corresponding to the disturbance statement information is inconsistent with the second prediction label corresponding to the statement information to be processed; wherein the first prediction label is obtained by predicting the disturbance statement information through a preset text audit model, and the second prediction label is obtained by predicting the statement information to be processed through the text audit model.
[0038] In a second aspect, an embodiment of the present application provides an adversarial training method, comprising:
[0039] Obtain training data and verification data according to the adversarial sample generated by the method described in any of the previous items;
[0040] Training a preset text review model using the training data to obtain a trained text review model;
[0041] When the trained text verification model is verified by the verification data and meets the preset requirements, a target text verification model is obtained according to the trained text verification model.
[0042] In a third aspect, the embodiment of the present application provides a Chinese adversarial sample generation device for text review, including:
[0043] A statement acquisition module is used to obtain statement information to be processed;
[0044] A word segmentation module, used for segmenting the sentence information to be processed to obtain multiple words;
[0045] An importance determination module, used to determine the first importance information of the word;
[0046] A disturbance word module, used to obtain disturbance words corresponding to each of the words;
[0047] A replacement module, configured to sequentially obtain disturbance sentence information after replacing each of the words in the sentence information to be processed with a corresponding disturbance word according to the first importance information;
[0048] The sample generation module is used to obtain an adversarial sample after successfully attacking the statement information to be processed according to the perturbation statement information when it is determined that the perturbation statement information meets a preset condition.
[0049] In a fourth aspect, an embodiment of the present application provides an adversarial training device, comprising:
[0050] A data acquisition module, used to obtain training data and verification data according to the adversarial samples generated by the method described in any of the previous items;
[0051] A training module, used to train a preset text review model using the training data to obtain a trained text review model;
[0052] The verification module is used to obtain a target text verification model according to the trained text verification model when the trained text verification model is verified by the verification data and meets the preset requirements.
[0053] In a fifth aspect, an embodiment of the present application provides an electronic device, comprising: a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;
[0054] The memory is used to store computer programs;
[0055] The processor is used to implement any of the processing methods described above when executing the computer program.
[0056] In a sixth aspect, an embodiment of the present application provides a storage medium, wherein the storage medium includes a stored program, wherein when the program is run, the method steps described in any of the preceding items are executed.
[0057] The above technical solution provided by the embodiment of the present application has the following advantages compared with the prior art:
[0058] The method provided in the embodiment of the present application can obtain adversarial samples by replacing words in the sentences to be processed, thereby increasing the diversity of samples used to train the prediction model. At the same time, it can improve the convenience of obtaining training data and the efficiency of model training by automatically generating adversarial samples. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0060] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0061] Figure 1 A flowchart of a method for generating Chinese adversarial samples for text review provided in an embodiment of the present application;
[0062] Figure 2 A flowchart of a method for generating Chinese adversarial samples for text review provided in another embodiment of the present application;
[0063] Figure 3A flowchart of a method for generating Chinese adversarial samples for text review provided for the application example of this application;
[0064] Figure 4 A schematic diagram of a process of adversarial training provided for an application example of this application;
[0065] Figure 5 A block diagram of a Chinese adversarial sample generation device for text review provided in an embodiment of the present application;
[0066] Figure 6 A block diagram of an adversarial training device provided in an embodiment of the present application;
[0067] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0068] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0069] At present, most of the research in the field of text adversarial is based on English data. The perturbation methods for English data, such as adding, deleting, and exchanging adjacent letters, can easily cause changes in Chinese semantics and affect understanding. This paper combines the characteristics of the diverse word forms of harmful texts in text auditing and proposes an efficient method for generating Chinese adversarial samples. The generated adversarial samples can well preserve the semantics of the original samples. By injecting adversarial samples into the training process of the text auditing model based on deep learning, the recognition ability and robustness of the model for harmful samples can be effectively improved.
[0070] Figure 1 A Chinese adversarial sample generation method for text review provided in an embodiment of the present application includes steps S1 to S6 as described below:
[0071] Step S1. Obtaining statement information to be processed;
[0072] Specifically, the sentence information to be processed can be obtained by collecting social media data on the Internet. The social media data may include abusive speech and normal speech.
[0073] Furthermore, after obtaining the statement information to be processed, the statement information to be processed can be preprocessed. When the source of the social media data is social media such as Weibo, the preprocessing method may include: deleting hashtags (tweet topics, i.e., words preceded by a # sign on Weibo to represent the topic of the tweet), @ symbols, forwarded content, web links, etc.
[0074] Step S2: Segment the sentence information to be processed to obtain multiple words.
[0075] Specifically, word segmentation is the process of recombining continuous sentence information to be processed into word sequences according to certain specifications. For example, when the sentence information to be processed includes the word w i When , we can perform word segmentation to obtain the word set X, where X = [w1,w2,…,w n ],w i is a word in the word set X.
[0076] Optionally, a custom word segmentation dictionary can be used to assist in the word segmentation of the sentence information to be processed.
[0077] Furthermore, after word segmentation, stop words in the word list are deleted, and then the above words are obtained.
[0078] Step S3: Determine the first importance information of the word.
[0079] The first importance information may be information for representing the importance of the word in affecting the semantics of the sentence information to be processed.
[0080] Step S4: Obtain the disturbance words corresponding to each word.
[0081] Specifically, the disturbance word can be a disguised word corresponding to the original word. For example, for the word "bad person", "huai person" is a disturbance word obtained by replacing the pinyin of "bad". In addition, the disturbance word can also be obtained by processing the word in other ways.
[0082] Step S5. According to the first importance information, the disturbed sentence information is obtained in sequence after each word in the sentence information to be processed is replaced with the corresponding disturbed word.
[0083] Specifically, the disturbance sentence information is sentence information including disturbance words in the text. When replacing, the words with the highest importance in the sentence to be processed may be replaced with disturbance words according to the first importance information, and then the words with the second highest importance may be replaced with disturbance words. After the corresponding disturbance words are replaced one by one in this way, the disturbance sentence information is obtained.
[0084] Step S6. When it is determined that the disturbance statement information satisfies the preset condition, an adversarial sample after a successful attack on the statement information to be processed is obtained according to the disturbance statement information.
[0085] Specifically, the preset condition may be a condition for determining that the adversarial sample is successfully generated; a successful attack may be: when the sentence information to be processed is identified as an abusive sentence, after replacing the words in the sentence information to be processed with the corresponding disturbance words, the disturbance sentence information is identified as a non-abusive sentence; that is, the predicted result is opposite to or inconsistent with the original result.
[0086] Through the method in this embodiment, adversarial samples can be obtained by replacing words in the sentences to be processed, thereby increasing the diversity of samples used to train the prediction model. At the same time, adversarial samples can be automatically generated to improve the convenience of obtaining training data and the efficiency of model training.
[0087] In real scenarios, publishers of harmful texts usually deform harmful information (e.g., insulting texts) in order to bypass review, including using pinyin replacement, homophone replacement, pictographic replacement, etc. Figure 2 As shown, in some embodiments, in order to solve the technical problem, as in the aforementioned method, the step S4 obtains the disturbance words corresponding to each word, including the following steps S21 to S24:
[0088] Step S21: Determine the pinyin and character shape of the word.
[0089] Specifically, the pinyin and glyphs of each word can be obtained by performing text recognition on the words; wherein, optionally, the glyphs may include information such as radicals corresponding to each character in the word, so that the disturbed words can be obtained by replacing the radicals in the later stage.
[0090] Step S22: According to the pinyin, at least one character in the word is replaced with characters obtained after the pinyin is used as the disturbance word.
[0091] That is to say, one or more characters in a word are replaced by pinyin to obtain a disturbed word; for example, for the word "bad person", "huai person" is the disturbed word obtained by replacing the pinyin of "bad".
[0092] Step S23: Based on the glyphs, at least one character in the word is replaced with a similar character whose glyphs meet the preset similarity requirements, and the resulting character is used as the disturbance word.
[0093] Specifically, the preset similarity requirement can be determined by: determining the size of the first region corresponding to the different part between the similar-looking character and the character to be replaced, and the size of the second region corresponding to the whole character to be replaced; calculating the difference between the size of the first region and the size of the second region; when the difference is less than the preset upper limit ratio threshold, it is determined that the similar-looking character meets the preset similarity requirement with the character to be replaced. Furthermore, after replacing at least one character with a similar-looking character, a perturbed word can be obtained. Since the difference between the perturbed word and the original word is small enough, it will not affect people's understanding of the text. However, for the model, since there are differences in the characters between the perturbed word and the original word, it cannot accurately predict the meaning of the perturbed word. For example, by replacing the character "村 (village)" in "村花 (village flower)" with the similar-looking character "忖 (ponder)", the perturbed word "忖花 (ponder flower)" is obtained.
[0094] Step S24. According to the pinyin and the shape of the character, use the character obtained by replacing at least one character in the word with a homophone and / or a similar-looking homophone that meets the preset similarity requirement as the perturbed word.
[0095] Specifically, the similar-looking homophone can be a character that has the same pronunciation as the character to be replaced and is similar in shape. The method for obtaining the similar-looking homophone can be: first determine the pinyin of the character to be replaced, and then query the homophones with the same pinyin (it can be the same pinyin and the same tone, or just the same pinyin) in the character library through the pinyin of the character to be replaced; then among all the homophones, query and obtain the corresponding similar-looking character according to the method in step S23, and use it as the similar-looking homophone. For example, by replacing the character "笨 (stupid)" in "笨蛋 (idiot)" with the similar-looking homophone "苯 (benzene)", the perturbed word "苯蛋 (benzene egg)" is obtained.
[0096] Through the method in this embodiment, perturbed words in multiple cases can be obtained. The method for obtaining the perturbed words in this embodiment adopts the character-level replacement attack method, that is, replacing at least one character in the original word. Moreover, the perturbed words obtained by the replacement method in this embodiment basically do not affect people's understanding of the text and can well retain the semantics of the sentence. After the adversarial attack, the text review classifier for semantic prediction will be misled and misjudge the abusive text as normal text.
[0097] For example, the results of predicting the text including the perturbed words obtained by the above method by a text review classifier that has not been trained with the adversarial text in this embodiment are shown in the following table:
[0098]
[0099] In some embodiments, as described in the foregoing method, the following steps P1 to P3 are further included:
[0100] Step P1. Determine the first importance information corresponding to each word.
[0101] Specifically, the first importance information is information corresponding to the importance of the word, and the importance may be a prediction score corresponding to a specific prediction type (when the prediction type is insult, the importance information may be a score for judging the degree of insult, for example: the first importance information corresponding to "idiot" may be 100, the first importance information corresponding to "stupid" may be 80, and so on).
[0102] Step P2. Arrange the words from high to low according to the first importance information to obtain the arrangement order information corresponding to each word.
[0103] Specifically, since each word has corresponding first importance information, the order information corresponding to each word can be obtained by sorting the words according to the importance information.
[0104] Step P3. Determine the replacement order information corresponding to each word according to the arrangement order information; the replacement order information is used to determine the order in which the words are replaced with the corresponding disturbance words.
[0105] That is to say, the replacement order information corresponding to each word is obtained according to the arrangement order information. Therefore, the replacement order information is obtained by arranging the first importance information from high to low.
[0106] In some embodiments, as in the aforementioned method, the step S5 sequentially obtains the disturbed sentence information after replacing each word in the sentence information to be processed with the corresponding disturbed word according to the first importance information, including the following steps S51 and S52:
[0107] Step S51: among all disturbance words corresponding to the word, determine the lowest importance disturbance word with the lowest importance, and obtain the corresponding relationship between the word and the lowest importance disturbance word.
[0108] Specifically, in the process of generating adversarial samples, since each word corresponds to multiple perturbation words, there will be multiple replacement options. Taking into account the generation efficiency of adversarial samples, the present invention adopts the idea of a greedy algorithm, that is, when replacing each word, the lowest importance perturbation word with the lowest current importance (for example, the lowest insulting nature) is selected, and after determining the lowest importance perturbation word with the lowest importance, and generating adversarial samples based on it, and training the model to be trained, the trained model can also recognize perturbation words with higher importance.
[0109] Step S52: According to the replacement order information, each word is replaced with a corresponding disturbance word of the lowest importance in sequence according to the corresponding relationship, and disturbance sentence information is obtained.
[0110] Specifically, after obtaining the replacement order information, the words are disturbed in descending order according to their importance. i The corresponding lowest importance perturbation word, when replacing the word w i After that, the disturbance sentence information is obtained. When the disturbance sentence information does not meet the preset conditions, the next word w is continued based on the disturbance sentence information. i+1 Replacements are made until a preset condition is triggered.
[0111] The method in this embodiment can achieve the purpose of replacing each word in order according to importance by replacing each word with a disturbance word in order according to the replacement order information. Further, in the case of classifying abusive texts, abusive words can be targeted more accurately, so that the disturbance words in the obtained adversarial text can correspond to the words with the highest abusive nature, thereby improving the accuracy and training value of the adversarial text.
[0112] In some embodiments, as in the aforementioned method, the step P1 of determining the importance information corresponding to each word includes the following steps P11 to P13:
[0113] Step P11. Delete the words in the sentence information to be processed respectively to obtain the first word-missing sentence information corresponding to the words.
[0114] Specifically, the first missing-word sentence information refers to the sentence information corresponding to the words deleted from the sentence information to be processed. Moreover, each word corresponds to a piece of first missing-word sentence information.
[0115] Step P12. Determine the first weight value corresponding to the sentence information to be processed and the second weight value corresponding to the first missing-word sentence information according to the preset text review model.
[0116] Specifically, the weight value may be a prediction score corresponding to a specific prediction type, and the prediction score is obtained by predicting the sentence information to be processed through the text review model.
[0117] Step P13. Obtain first importance information corresponding to the word based on the difference between the first weight value and the second weight value.
[0118] That is to say, the first importance information corresponding to the word is obtained by the difference between the first weight value corresponding to the sentence information to be processed and the second weight value corresponding to the first missing-word sentence information.
[0119] One of the optional implementations could be:
[0120] Referring to the TextFooler algorithm, the change in the predicted score before and after removing the i-th word, where i = 1, 2, ..., n. The formula is as follows:
[0121]
[0122] in, For word w i The importance score of Y and are different category labels, namely: F Y (X) is the predicted score for sentence X being of category Y; is the predicted score of sentence X for category Y; The predicted score of sentence X for category Y after removing word wi, After removing the word wi, sentence X is the category The predicted score of .
[0123] In some embodiments, as in the aforementioned method, the step S51 determines the lowest importance disturbance word with the lowest importance among all disturbance words corresponding to the word, including the following steps S511 to S515:
[0124] Step S511: After replacing the words in the sentence information to be processed with the disturbance words, the replaced sentence information corresponding to the disturbance words is obtained.
[0125] That is to say, the words in the sentence information to be processed are replaced with the disturbance words corresponding to the words, and the sentence information after replacement is obtained; for example: when the sentence information to be processed is "The forest is big, and there are all kinds of garbage", the word to be replaced is "garbage", and the corresponding disturbance word is "garbage", the sentence information after replacement is "The forest is big, and there are all kinds of garbage".
[0126] Step S512: Delete the disturbing words in the replaced sentence information to obtain second missing-word sentence information corresponding to the disturbing words.
[0127] Specifically, the second missing-word sentence information refers to the sentence information corresponding to the sentence information to be processed after the disturbance words are deleted. Moreover, each disturbance word corresponds to a piece of second missing-word sentence information.
[0128] Step S513: Determine the third weight value corresponding to the replaced sentence information and the fourth weight value corresponding to the second missing word sentence information according to the preset text review model.
[0129] Specifically, the third weight value and the fourth weight value may be prediction scores corresponding to the same specific prediction type, and the prediction scores are obtained by predicting the sentence information to be processed by the text audit model.
[0130] Step S514: Obtain second importance information corresponding to the disturbance word according to the difference between the third weight value and the fourth weight value.
[0131] That is, the second importance information corresponding to the disturbance word is obtained by the difference between the third weight value corresponding to the replaced sentence information and the fourth weight value corresponding to the second missing word sentence information.
[0132] Step S515: Obtain the lowest importance disturbance word corresponding to the word according to the second importance information corresponding to the disturbance word corresponding to the word.
[0133] Specifically, after obtaining the second importance information of each disturbance word corresponding to the word, the lowest importance disturbance word with the lowest second importance can be selected from each disturbance word corresponding to the word.
[0134] In some embodiments, as in the aforementioned method, the step S6, the disturbance statement information meets the preset condition, includes the following steps S61 to S63:
[0135] Step S61: In the disturbance sentence information, the number of disturbance words is less than or equal to a preset upper limit threshold of the number of disturbance words; and
[0136] Step S62: the ratio of the number of disturbance words to the total number of words in the disturbance sentence information is less than a preset disturbance ratio threshold; and
[0137] Step S63. The first prediction label corresponding to the disturbance sentence information is inconsistent with the second prediction label corresponding to the sentence information to be processed; wherein the first prediction label is obtained by predicting the disturbance sentence information through a preset text review model, and the second prediction label is obtained by predicting the sentence information to be processed through a text review model.
[0138] Specifically, when the number of perturbation words is too large or the number of perturbation words accounts for too high a proportion of the total number of words in the perturbation sentence information, the adversarial sample may have a significant change in semantics compared to the original sample due to excessive changes in the sentence information to be processed. Therefore, it is necessary to set an upper limit threshold of the number of perturbation words and a perturbation ratio threshold. Among them, the upper limit threshold of the number of perturbation words is a threshold used to limit the number of perturbation words in the sentence information to be processed, and the perturbation ratio threshold is a threshold used to account for the number of perturbation words in the total number of words in the perturbation sentence information.
[0139] For example: when the upper threshold of the number of disturbance words is g (for example: 4), and the disturbance ratio threshold is p (for example: 50%), when the number of disturbance words is less than or equal to g, and the ratio is less than or equal to p, but the predicted label is not reversed, the preset conditions are met and the sentence information to be processed continues to be disturbed.
[0140] When the number of disturbed words is less than or equal to g, and the ratio is less than or equal to p, and the predicted label has been reversed, the perturbation of the sentence information to be processed is successful; when the sentence information to be processed is identified as abusive text by the text review model, successful perturbation means that the text review model cannot correctly identify the perturbed sentence information to be processed as abusive text.
[0141] If the number of disturbed words is greater than g or the proportion is greater than p, but the predicted label is not reversed, the preset condition is not met, and the disturbance of the sentence to be processed is stopped, and the disturbance fails.
[0142] In general, the text audit model is a two-classification model, and there are generally only two cases for the predicted label. Therefore, when the first predicted label is inconsistent with the second predicted label, it means that the first predicted label is opposite to the second predicted label. For example, when the label corresponding to the sentence information to be processed is abusive text, and the label corresponding to the perturbed sentence information needs to be normal text (ie, non-abusive text), it means that the text cannot be correctly predicted by the text audit model, so it can be used for training the text audit model to improve its prediction accuracy for adversarial samples.
[0143] Using the method in this embodiment, the attack results obtained by attacking the statement information to be processed are shown in the following table:
[0144] Harmful text Data indicators Test set data volume (items) 377 Attack success rate (%) 14.85 Average number of disturbing words (words) 3.83 Average text length (words) 46.37
[0145] Therefore, the method in this embodiment can effectively generate adversarial samples in Chinese.
[0146] like Figure 3 In the application example shown:
[0147] 1. Combined with this custom word segmentation dictionary, preprocess the input sentence X and segment it to obtain X = [w1, w2, ..., w n ]; where w i is a word in the sentence X obtained after word segmentation;
[0148] 2. Input the entire sentence and the sentence after removing the i-th word into the trained text review model (i.e., the abusive text classification model), and calculate the importance of each word according to the importance calculation formula of the TextFooler algorithm (an English adversarial sample generation algorithm).
[0149] 3. Remove stop words from the word list and sort the word sequence X from high to low according to word importance;
[0150] 4. Perform word perturbations in descending order of importance; for word wi , traverse all replacement words for the current word, use the TextFooler word importance score formula to select the word with the lowest insulting nature among the replacement words to replace the original word, and then continue to replace the next word on this basis until the stop condition is met and the adversarial sample is output.
[0151] 5. Stop condition: If the following three conditions are met at the same time, the adversarial sample is successfully generated: 1) the number of perturbed words is less than or equal to n; 2) the ratio of the number of perturbed words to the total number of text words is less than p; 3) the predicted label is reversed; otherwise, no adversarial sample is generated.
[0152] According to an embodiment of another aspect of the present application, an adversarial training method is also provided, comprising steps S7 to S9 as described below:
[0153] Step S7. Obtain training data and verification data based on the adversarial samples generated by any of the methods in the previous embodiments.
[0154] Specifically, the training data and the verification data may include both adversarial samples and original test sets, wherein the samples in the original test set may be original sentence information that has not been replaced by perturbation words.
[0155] Generally, in the process of constructing training data, the label corresponding to the sentence information to be processed corresponding to the adversarial sample is retained (that is, when the sentence information to be processed corresponds to an abusive text, the label corresponding to the adversarial sample is also an abusive text).
[0156] Step S8. Train the preset text review model using the training data to obtain a trained text review model;
[0157] Step S9. When the trained text review model is verified by verification data and meets preset requirements, a target text review model is obtained according to the trained text review model.
[0158] Specifically, the preset requirement may be a lower limit threshold of the accuracy of the prediction corresponding to the text review model. In addition, other requirements may also be provided, which are not limited here one by one.
[0159] That is to say, the text review model is first trained with training data to obtain a trained text review model, and then the trained text review model is verified with verification data and when it meets the preset requirements, the target text review model can be obtained.
[0160] When the text type corresponding to the adversarial sample is insulting text, and the BERT model is trained using the training data and verification data obtained in this embodiment, the model obtained after training has the effect shown in the following table:
[0161]
[0162] As can be seen from the above table, after adversarial training, the recognition accuracy of the BERT model for adversarial samples increased from 86.47% to 98.94%, indicating that adversarial training can effectively improve the model's ability to recognize harmful samples.
[0163] In general, adversarial samples can effectively increase the diversity of training samples. The model learns the correlation between the replacement words (i.e., disrupted words) and the original words, and improves the areas where the model's text vector representation space performs poorly.
[0164] like Figure 4 In the application example shown:
[0165] 1. Get training data;
[0166] 2. Perform data preprocessing on the input training data;
[0167] 3. Obtain preprocessed training data;
[0168] 4. Input the preprocessed training data into the adversarial sample generation module;
[0169] 5. Obtain adversarial samples of training data, with the original sample labels consistent with the adversarial sample labels;
[0170] 6. Mix the adversarial samples and the original training data as new training data to input the text audit model;
[0171] 7. Fine-tune the text review model again based on the new training data to obtain an adversarially trained text review model.
[0172] like Figure 5 As shown, according to an embodiment of another aspect of the present application, a Chinese adversarial sample generation device for text review is also provided, including:
[0173] A statement acquisition module 11 is used to acquire statement information to be processed;
[0174] A word segmentation module 12, used for segmenting the sentence information to be processed to obtain a plurality of words;
[0175] Importance determination module 13, used to determine the first importance information of the word;
[0176] A disturbance word module 14 is used to obtain disturbance words corresponding to each word;
[0177] A replacement module 15 is used to sequentially obtain disturbance sentence information after replacing each word in the sentence information to be processed with a corresponding disturbance word according to the first importance information;
[0178] The sample generation module 16 is used to obtain an adversarial sample after successfully attacking the statement information to be processed according to the disturbance statement information when it is determined that the disturbance statement information meets the preset conditions.
[0179] Specifically, the specific process of each module in the device of the embodiment of the present invention realizing its function can be referred to the relevant description in the method embodiment, which will not be repeated here.
[0180] like Figure 6 As shown, according to an embodiment of another aspect of the present application, the present application also provides a confrontation training device, including:
[0181] A data acquisition module 21, configured to obtain training data and verification data according to the adversarial samples generated by the method of any of the previous embodiments;
[0182] A training module 22, used to train a preset text review model using training data to obtain a trained text review model;
[0183] The verification module 23 is used to obtain a target text verification model according to the trained text verification model when the trained text verification model is verified by verification data and meets preset requirements.
[0184] Specifically, the specific process of each module in the device of the embodiment of the present invention realizing its function can be referred to the relevant description in the method embodiment, which will not be repeated here.
[0185] According to another embodiment of the present application, there is also provided an electronic device, including: Figure 7 As shown, the electronic device may include: a processor 1501 , a communication interface 1502 , a memory 1503 and a communication bus 1504 , wherein the processor 1501 , the communication interface 1502 , and the memory 1503 communicate with each other via the communication bus 1504 .
[0186] Memory 1503, used for storing computer programs;
[0187] The processor 1501 is used to implement the steps of the above method embodiment when executing the program stored in the memory 1503.
[0188] The bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0189] The communication interface is used for communication between the above electronic device and other devices.
[0190] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0191] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0192] An embodiment of the present application further provides a storage medium, the storage medium including a stored program, wherein the method steps of the above method embodiment are executed when the program is run.
[0193] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0194] The foregoing is merely a specific embodiment of the present invention, which enables those skilled in the art to understand or implement the present invention. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features claimed herein.
Claims
1. A Chinese adversarial sample generation method for text review, It is characterized in that include: Get the statement information to be processed; Segmenting the sentence information to be processed to obtain multiple words; Determining the first importance information of the word includes: deleting the words in the sentence information to be processed respectively to obtain first missing-word sentence information corresponding to the word; determining a first weight value corresponding to the sentence information to be processed and a second weight value corresponding to the first missing-word sentence information according to a preset text review model, wherein the first weight value and the second weight value are score values corresponding to a specific prediction type; obtaining the first importance information corresponding to the word according to the difference between the first weight value and the second weight value, wherein the first importance information corresponding to the word according to the difference between the first weight value and the second weight value is summarized as removing the i-th word w i Formula I for the change of the predicted scores before and after wi , the change formula I wi for: Where i = 1, 2, ..., n, For word w i The importance score of Y and are different category labels, namely: F Y (X) is the predicted score for sentence X to be of category Y; Is the statement X a category The predicted score of The predicted score of sentence X for category Y after removing word wi, After removing the word wi, sentence X is the category The predicted score of Obtaining disturbance words corresponding to each of the words, including: determining the pinyin and glyph of the words; according to the pinyin, replacing at least one word in the word with a pinyin to obtain a character as the disturbance word; or, according to the glyph, replacing at least one word in the word with a similar glyph that meets a preset similarity requirement to obtain a character as the disturbance word; or, according to the pinyin and glyph, replacing at least one word in the word with a homophone and / or a homophone that meets a preset similarity requirement to obtain a character as the disturbance word; according to the first importance information, sequentially obtaining disturbance sentence information after replacing each of the words in the sentence information to be processed with the corresponding disturbance word; When it is determined that the disturbance statement information satisfies a preset condition, obtaining an adversarial sample after successfully attacking the statement information to be processed according to the disturbance statement information; Before obtaining disturbance sentence information after replacing each of the words in the to-be-processed sentence information with a corresponding disturbance word in sequence according to the first importance information, the method further includes: arranging the words from high to low according to the first importance information to obtain arrangement order information corresponding to each word; determining replacement order information corresponding to each word according to the arrangement order information; the replacement order information is used to determine the order of replacing the words with the corresponding disturbance words.
2. The method according to claim 1, It is characterized in that The step of sequentially obtaining disturbance sentence information after replacing each of the words in the sentence information to be processed with a corresponding disturbance word according to the first importance information includes: Determine the lowest importance disturbance word with the lowest importance among all the disturbance words corresponding to the word, and obtain the corresponding relationship between the word and the lowest importance disturbance word; According to the replacement order information, each of the words is replaced with the corresponding disturbance word of the lowest importance in sequence according to the corresponding relationship, and the disturbance sentence information is obtained.
3. The method according to claim 2, It is characterized in that Among all the disturbance words corresponding to the word, determining the lowest importance disturbance word with the lowest importance, including: After replacing the words in the sentence information to be processed with the disturbance words, replaced sentence information corresponding to the disturbance words is obtained; Deleting the disturbance word in the replaced sentence information to obtain second missing-word sentence information corresponding to the disturbance word; Determine, according to a preset text audit model, a third weight value corresponding to the replaced sentence information and a fourth weight value corresponding to the second missing-word sentence information; Obtaining second importance information corresponding to the disturbance word according to differences between the third weight value and the fourth weight value; The lowest importance disturbance word corresponding to the word is obtained according to the second importance information corresponding to the disturbance word corresponding to the word.
4. The method according to claim 1, It is characterized in that The disturbance statement information meets the preset conditions, including: In the disturbance sentence information, the number of the disturbance words is less than or equal to a preset upper limit threshold of the number of disturbance words; and The ratio of the number of the disturbance words to the total number of words in the disturbance sentence information is less than a preset disturbance ratio threshold; and The first prediction label corresponding to the disturbance statement information is inconsistent with the second prediction label corresponding to the statement information to be processed; wherein the first prediction label is obtained by predicting the disturbance statement information through a preset text audit model, and the second prediction label is obtained by predicting the statement information to be processed through the text audit model.
5. An adversarial training method, It is characterized in that include: Obtaining training data and verification data according to the adversarial sample generated by the method according to any one of claims 1 to 4; Training a preset text review model using the training data to obtain a trained text review model; When the trained text verification model is verified by the verification data and meets the preset requirements, a target text verification model is obtained according to the trained text verification model.
6. A Chinese adversarial sample generation device for text review, It is characterized in that include: A statement acquisition module is used to obtain statement information to be processed; A word segmentation module, used for segmenting the sentence information to be processed to obtain multiple words; The importance determination module is used to determine the first importance information of the word, including: deleting the words in the sentence information to be processed respectively to obtain the first missing word sentence information corresponding to the word; determining the first weight value corresponding to the sentence information to be processed and the second weight value corresponding to the first missing word sentence information according to a preset text review model, wherein the first weight value and the second weight value are score values corresponding to a specific prediction type; obtaining the first importance information corresponding to the word according to the difference between the first weight value and the second weight value, wherein the first importance information corresponding to the word according to the difference between the first weight value and the second weight value is summarized as removing the i-th word w i Formula I for the change of the predicted scores before and after wi , the change formula I wi for: Where i = 1, 2, ..., n, For word w i The importance score of Y and are different category labels, namely: F Y (X) is the predicted score for sentence X to be of category Y; Is the statement X a category The predicted score of The predicted score of sentence X for category Y after removing word wi, After removing the word wi, sentence X is the category The predicted score of The disturbance word module is used to obtain the disturbance word corresponding to each of the words, including: determining the pinyin and the glyph of the word; according to the pinyin, replacing at least one character in the word with the pinyin to obtain the character as the disturbance word; or, according to the glyph, replacing at least one character in the word with a similar character whose glyph meets the preset similarity requirement to obtain the character as the disturbance word; or, according to the pinyin and the glyph, replacing at least one character in the word with a homophone and / or homophonic and similar character whose glyph meets the preset similarity requirement to obtain the character as the disturbance word; A replacement module, configured to sequentially obtain disturbance sentence information after replacing each of the words in the sentence information to be processed with a corresponding disturbance word according to the first importance information; A sample generation module, configured to obtain, when it is determined that the disturbance statement information satisfies a preset condition, an adversarial sample after a successful attack on the statement information to be processed according to the disturbance statement information; The method further includes a module for arranging the words from high to low according to the first importance information before obtaining the disturbance sentence information after replacing each of the words in the sentence information to be processed with the corresponding disturbance word in sequence according to the first importance information, so as to obtain the arrangement order information corresponding to each word; determining the replacement order information corresponding to each of the words according to the arrangement order information; and the replacement order information is used to determine the order of replacing the words with the corresponding disturbance words.
7. An adversarial training device, It is characterized in that include: A data acquisition module, configured to obtain training data and verification data according to the adversarial sample generated by the method according to any one of claims 1 to 4; A training module, used to train a preset text review model using the training data to obtain a trained text review model; The verification module is used to obtain a target text verification model according to the trained text verification model when the trained text verification model is verified by the verification data and meets the preset requirements.
8. An electronic device, It is characterized in that include: A processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus; The memory is used to store computer programs; The processor is used to implement the method steps described in any one of claims 1 to 5 when executing the computer program.
9. A storage medium, It is characterized in that The storage medium includes a stored program, wherein the program executes the method steps of any one of claims 1 to 5 when executed.
