Statement generation method and apparatus, and storage medium and electronic device

Generate sentence granularity adversarial attack samples through encoder-decoder structure, which solves the problem that the robustness of the task processing model cannot be evaluated at the sentence level in the prior art, and improves the robustness of the model and the diversity of adversarial attack samples. Validity.

WO2024235214A1PCT designated stage expired Publication Date: 2024-11-21TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
PCT/CN2024/093001
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-05-17
Filing Date
2024-05-14
Publication Date
2024-11-21

AI Technical Summary

Technical Problem

The lack of effective sentence-grained text-adversarial attack sample generation method in the prior art, and it is impossible to effectively evaluate the robustness of the task processing model at the sentence level.

Method used

The characterization vector of the statement is perturbed through the encoder-decoder structure, and a sentence granularity adversarial attack sample with the same or similar semantics as the original statement is generated. The perturbation weight vector is used to control the degree of perturbation to achieve the diversity and effectiveness of the adversarial attack sample.

Benefits of technology

The gap in the sentence-grained text attack method was filled, the robustness of the task processing model and the diversity and effectiveness of the anti-attack samples were improved, and the robustness of the model was effectively evaluated at the sentence level.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024093001_21112024_PF_FP_ABST
    Figure CN2024093001_21112024_PF_FP_ABST
Patent Text Reader

Abstract

Disclosed in the present application are a statement generation method and apparatus, and a storage medium and an electronic device. The method comprises: encoding a first statement representation vector of a first statement, so as to obtain a first semantic representation vector and a disturbance weight vector, wherein the first statement representation vector is a vector which is determined according to a word vector of each word in the first statement, the first semantic representation vector is used for representing the semantics of the first statement, and the disturbance weight vector is used for controlling disturbance of a predetermined disturbance vector to the first semantic representation vector; according to the disturbance weight vector and the disturbance vector, disturbing the first semantic representation vector, so as to obtain a second semantic representation vector; and decoding the second semantic representation vector, so as to obtain a second statement, wherein the first statement and the second statement have the same or similar semantics.
Need to check novelty before this filing date? Find Prior Art

Description

Sentence generation method and device, storage medium and electronic device

[0001] This application claims priority to Chinese patent application number 2023105602181 filed on May 17, 2023, with invention name “Sentence generation method and device, storage medium and electronic device”. Technical Field

[0002] The present application relates to the field of computer technology, and in particular to a sentence generation method and device, a storage medium, and an electronic device. Background Art

[0003] In natural language processing tasks, text adversarial attacks are often used to evaluate the robustness of task processing models. The basic implementation of text adversarial attacks involves adding human-imperceptible perturbations to text data samples, causing the model to make incorrect predictions, thereby testing the model's robustness and / or improving it.

[0004] There are three types of text adversarial attacks: character-level, word-level, and sentence-level. Generating adversarial attack samples plays a crucial role in implementing these three types of text adversarial attacks.

[0005] In the related art, methods for generating adversarial attack samples at character granularity and methods for generating adversarial attack samples at word granularity are proposed. Among them, the adversarial text generation method at character granularity calculates the importance of each character in the sentence, and uses the descending order of the importance of the characters as the attack order. The disturbance is added by replacing characters with characters that are similar in pronunciation or shape, or adding or deleting characters, etc. The sample attack method at word granularity calculates the importance of each word in the sentence, and uses the descending order of the importance of the words as the attack order. The adversarial attack samples are generated by replacing synonyms or near synonyms. However, most of the current text adversarial attack tasks are performed at character granularity or word granularity. There is no effective solution for how to generate adversarial attack samples (adversarial attack text) at sentence granularity and how to implement sentence granularity text attack methods.

[0006] Summary of the Invention

[0007] Embodiments of the present application provide a sentence generation method and apparatus, a storage medium, and an electronic device.

[0008] According to an embodiment of the present application, a sentence generation method is provided, including: encoding a first sentence representation vector of a first sentence to obtain a first semantic representation vector and a disturbance weight vector, wherein the first sentence representation vector is a vector determined based on the word vectors of each word in the first sentence, the first semantic representation vector is used to represent the semantics of the first sentence, and the disturbance weight vector is used to control the disturbance of the first semantic representation vector by a predetermined disturbance vector; according to the disturbance weight vector and the disturbance vector, the first semantic representation vector is disturbed to obtain a second semantic representation vector; and the second semantic representation vector is decoded to obtain a second sentence, wherein the first sentence and the second sentence have the same or similar semantics.

[0009] According to an embodiment of the present application, a sentence generation device is also provided, including: an encoding unit for encoding a first sentence representation vector of a first sentence to obtain a first semantic representation vector and a disturbance weight vector, wherein the first sentence representation vector is a vector determined according to the word vectors of each word in the first sentence, the first semantic representation vector is used to represent the semantics of the first sentence, and the disturbance weight vector is used to control the disturbance of the first semantic representation vector by a predetermined disturbance vector; a first processing unit for perturbating the first semantic representation vector according to the disturbance weight vector and the disturbance vector to obtain a second semantic representation vector; a decoding unit for decoding the second semantic representation vector to obtain a second sentence, wherein the first sentence and the second sentence have the same or similar semantics.

[0010] According to an embodiment of the present application, a computer-readable storage medium is further provided, in which a computer program is stored, wherein the computer program is configured to execute the above statement generation method when run.

[0011] According to an embodiment of the present application, a computer program product is also provided, including a computer program / instruction, which implements the steps of the above method when executed by a processor.

[0012] According to an embodiment of the present application, an electronic device is also provided, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the sentence generation method through the computer program.

[0013] Through the above-mentioned embodiment provided by the present application, the first semantic representation vector of the first sentence is perturbed according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector. Then, by decoding the second semantic representation vector, a second sentence with the same or similar semantics as the first sentence is obtained, and the second sentence is used as a sentence-granular adversarial attack sample. By using the perturbation weight vector, the degree of perturbation can be set, thereby controlling the semantic distance between the second semantic representation vector and the first semantic representation vector. The semantic distance between the generated sentence-granular adversarial attack sample and the original input sample is controllable. This is in line with the fundamental purpose of generating adversarial attack samples, that is, to perform perturbations at the sentence level while being semantically similar to the original sample to obtain corresponding samples with similar semantics / intentions. In this way, the effectiveness of the generated adversarial attack samples can be improved. In addition, by adjusting the perturbation weight vector, perturbation information of different perturbation degrees can be added to the original input sample, thereby increasing the diversity of the hidden layer representation of the original input sample and improving the diversity of the generated adversarial attack samples. In addition, by utilizing the sentence-granular text attack method to evaluate the robustness of the task processing model, the robustness of the task processing model is improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0014] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0015] FIG1 is a schematic diagram of an application scenario of a sentence generation method according to an embodiment of the present application;

[0016] FIG2 is a flow chart of a sentence generation method according to an embodiment of the present application;

[0017] FIG3 is a schematic diagram of another application scenario of the sentence generation method according to an embodiment of the present application;

[0018] FIG4 is a schematic diagram of another application scenario of the sentence generation method according to an embodiment of the present application;

[0019] FIG5 is an overall schematic diagram of a sentence generation method according to an embodiment of the present application;

[0020] FIG6 is another overall schematic diagram of the sentence generation method according to an embodiment of the present application;

[0021] FIG7 is a specific example of a sentence generation method according to an embodiment of the present application;

[0022] FIG8 is a schematic structural diagram of an encoder according to an embodiment of the present application;

[0023] FIG9 is a schematic structural diagram of a decoder according to an embodiment of the present application;

[0024] FIG10 is a result of normalizing the output of the last layer decoder according to an embodiment of the present application;

[0025] FIG11 is a schematic diagram of an implementation process of a beam search algorithm according to an embodiment of the present application;

[0026] FIG12 is a schematic structural diagram of a sentence generating apparatus according to an embodiment of the present application;

[0027] FIG13 is a schematic structural diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0028] In order to enable those skilled in the art to better understand the solution of the present application, 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.

[0029] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0030] The technical solutions in the embodiments of this application will comply with legal provisions during implementation. When performing operations in accordance with the technical solutions in the embodiments, the data used will not involve user privacy. While ensuring that the operation process is compliant and legal, the security of the data is guaranteed.

[0031] Glossary:

[0032] Adversarial attack samples are generated by perturbing the original sample while preserving its semantics to the greatest extent possible. These samples are used to attack the model and identify weaknesses. Adversarial attack samples can be used to identify model weaknesses and subsequently train the model to enhance its robustness. Adversarial attack samples are classified into three granularities: character, word, and sentence. Adversarial attack samples typically have the same labels as the original sample.

[0033] Character-level attack: Corresponding to English letters or Chinese characters, at the character level, by replacing characters in the original sample with similar characters or homophones, and adding, deleting, or modifying the original sample at the character level, etc., to generate attack samples and attack the model to find its weaknesses.

[0034] Word-level attack: For English or Chinese words, at the word level, the attack is performed by replacing words in the original sample with synonyms and adding, deleting, or modifying the original sample at the word level to generate attack samples and attack the model to find its weaknesses.

[0035] Sentence-level attack: For English or Chinese sentences, perturbations are performed at the sentence level to generate attack samples, which are used to attack the model to find its weaknesses.

[0036] Text adversarial attacks: Imperceptible perturbations are added to text data samples, causing the model to make incorrect predictions, thereby testing the model's robustness and flaws. Text adversarial attacks are categorized into three types: character-level, word-level, and sentence-level.

[0037] Model robustness: Robustness can be understood as a model's tolerance to data variations. A model is considered robust if even small deviations in the data or minor perturbations within the model have only a minor impact on the model output while still producing correct results.

[0038] Automatic corpus annotation system: The unlabeled corpus data is preprocessed through an algorithm, and high-confidence label results are automatically assigned to the corpus, thereby producing training data for model training.

[0039] End-to-end: An automated process from input to output. The input is the original input, and the output is the desired result. For example, the original input is input into the model, and the model outputs the result after processing. The entire process is an end-to-end method.

[0040] According to one aspect of an embodiment of the present application, a statement generation method is provided. As an example implementation, the above statement generation method can be, but is not limited to, applied to the application scenario shown in Figure 1. In the application scenario shown in Figure 1, the terminal device 102 can be, but is not limited to, communicating with the server 106 via the network 104, and the server 106 can be, but is not limited to, performing operations on the database 108, such as write data operations or read data operations. The above terminal device 102 can include, but is not limited to, a human-computer interaction screen, a processor, and a memory. The above human-computer interaction screen can be, but is not limited to, used to display the first statement, the second statement, etc. on the terminal device 102. The above processor can be, but is not limited to, used to respond to the above human-computer interaction operation, perform corresponding operations, or generate corresponding instructions, and send the generated instructions to the server 106. The above memory is used to store relevant processing data, such as the first semantic representation vector, the second semantic representation vector, the second statement, etc.

[0041] As an example, the following steps in the sentence generation method can be performed on the server 106. Step S102, encode the first sentence representation vector of the first sentence to obtain a first semantic representation vector and a disturbance weight vector. The first sentence representation vector is related to the word vectors of each word in the first sentence. For example, the first sentence representation vector is determined based on the word vectors of each word in the first sentence. The first semantic representation vector is used to represent the semantics of the first sentence. The disturbance weight vector is used to control the disturbance of the first semantic representation vector by a predetermined disturbance vector. Step S104, disturb the first semantic representation vector according to the disturbance weight vector and the disturbance vector to obtain a second semantic representation vector. Step S106, decode the second semantic representation vector to obtain a second sentence, wherein the first sentence and the second sentence have the same or similar semantics. The sameness or similarity of the semantics of the first sentence and the second sentence can be determined by the distance between the first semantic representation vector and the second semantic representation vector.

[0042] The second sentence obtained in the above manner can be used as a sentence-granular adversarial attack sample to evaluate the robustness of the task processing model, thereby improving the robustness of the task processing model.

[0043] FIG2 is a flow chart of a sentence generation method according to an embodiment of the present application, which includes the following steps:

[0044] Step S202: Encode the first sentence representation vector of the first sentence to obtain a first semantic representation vector and a perturbation weight vector. The first sentence representation vector is associated with the word vectors of each word in the first sentence. For example, the first sentence representation vector is determined based on the word vectors of each word in the first sentence. The first semantic representation vector is used to represent the semantics of the first sentence. The perturbation weight vector is used to control the perturbation of the first semantic representation vector by a predetermined perturbation vector.

[0045] Step S204 : perturb the first semantic representation vector according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector.

[0046] Step S206: Decode the second semantic representation vector to obtain a second sentence, wherein the first sentence and the second sentence have the same or similar semantics.

[0047] Fig. 3 is a schematic diagram of another application scenario of the sentence generation method according to an embodiment of the present application. As shown in Fig. 3, the original data sample is "Sincerity can move even metal and stone".

[0048] Figure 4 is a schematic diagram of another application scenario of the sentence generation method according to an embodiment of the present application. As shown in Figure 4, when using the text adversarial attack method to conduct adversarial training on the task processing model, the training results are manually reviewed and the weaknesses of the task processing model are identified, so that the task processing model can be fine-tuned, thereby improving the robustness of the task processing model when encountering adversarial attack text.

[0049] Related technologies have proposed methods for generating character-level adversarial attack samples and word-level adversarial attack samples, but no effective solution has been proposed for generating sentence-level adversarial attack text.

[0050] To this end, an embodiment of the present application proposes a method for generating sentence-granular adversarial attack text based on an encoder-decoder structure.

[0051] Specifically, Figure 5 is an overall schematic diagram of the sentence generation method according to an embodiment of the present application. The text generated by this method is an adversarial attack text. As shown in Figure 5, the first sentence representation vector of the first sentence is used as input and input into the encoder to obtain a first semantic representation vector m with a dimension of 512×512 and a perturbation weight vector σ with a dimension of 512×512. Among them, the perturbation weight vector σ is, for example, a vector obtained by random sampling according to a Gaussian distribution, and σ is used to assign weights to the perturbation information, specifically exp(σ)×e, and e can be, but is not limited to, a perturbation vector that conforms to a normal distribution. The first semantic representation vector m includes {m1, m2, m3…m 512}, the perturbation weight vector σ includes {σ1,σ2,σ3…σ 512}, the perturbation vector e includes {e1,e2,e3…e 512 The second semantic representation vector c corresponds to c in Figure 5 k, including {c1,c2,c3…c 512}.

[0052] In order to facilitate understanding of the technical solutions in various embodiments of the present application, the explanation is given assuming that the dimension of the first semantic representation vector is 512×512.

[0053] For example, the first sentence is "Sincerity can move even metal and stone." By encoding the first sentence representation vector of the first sentence, we obtain a first semantic representation vector of 512 (the length of the sentence characters) × 512 (the vector dimension of each character). Each character in "Sincerity can move even metal and stone" is converted to a vector of dimension 1 × 512, and the character length in the sentence is defined as 512 bytes. If the number of characters (character length) in the sentence is less than 512 bytes, the number of characters is padded with zeros to convert it to 512 bytes.

[0054] The first semantic representation vector is perturbed according to the perturbation weight vector σ and the perturbation vector e to obtain a second semantic representation vector c of dimension 512 × 512. The second semantic representation vector c is decoded using a decoder to obtain a decoded text, i.e., the second sentence.

[0055] The following is a further description of the method for generating adversarial attack text based on the encoder-decoder structure in conjunction with Figure 6. Figure 6 is another overall schematic diagram of the sentence generation method according to an embodiment of the present application. The text generated by this method is adversarial attack text. As shown in Figure 6, in step S61, the original sample (first sentence) is used as input and input into the encoder, and the first hidden layer semantic representation of the original sample is obtained in the hidden layer.

[0056] In step S62, disturbance information that conforms to the data distribution (eg, normal distribution) is sampled according to the data distribution and added to the first latent layer semantic representation to obtain a second latent layer semantic representation.

[0057] In step S63, the second latent semantic representation is input into the decoder to obtain a decoded text (the second sentence).

[0058] In step S64, the decoded text is determined as a generated adversarial attack sample.

[0059] Through the above steps S61 to S64, multiple adversarial attack samples (adversarial attack texts) in text form can be generated. The model is trained using the multiple adversarial attack samples and fine-tuned to enhance the robustness of the model.

[0060] Figure 7 is a specific example of a sentence generation method according to an embodiment of the present application. As shown in Figure 7, assume the first sentence is "Sincerity can move even metal and stone." Through steps S61 to S64, the perturbation information is applied to the first sentence according to the perturbation weight parameter, and the result of the perturbation is decoded to obtain the second sentence "Sincerity can move even metal and stone." The following describes the encoding and decoding process in conjunction with a specific embodiment.

[0061] Through the above-mentioned embodiments provided in the present application, the first semantic representation vector of the first sentence is perturbed according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector, and then the second semantic representation vector is decoded to obtain a second sentence having the same or similar semantics as the first sentence, and the second sentence is used as a sentence-granularity adversarial attack sample, thereby filling the gap in the sentence-granularity-based text adversarial attack method in the related art, solving the technical problem in the related art that the sentence-granularity text attack method cannot be used to evaluate the robustness of the task processing model, and achieving the technical effect of improving the robustness of the task processing model.

[0062] As an example, the encoding of the first sentence representation vector of the first sentence to obtain the first semantic representation vector and the perturbation weight vector includes:

[0063] Pass the first sentence representation vector through N encoders with the same structure in sequence to obtain the Nth sentence encoding vector, where N is a positive integer greater than or equal to 1;

[0064] Perform a first linear transformation on the Nth sentence encoding vector to obtain a first semantic representation vector;

[0065] Perform a second linear transformation on the Nth sentence encoding vector to obtain a perturbation weight vector.

[0066] Figure 8 is a schematic diagram of the structure of an encoder according to an embodiment of the present application. As shown in Figure 8, assuming that the number of encoder layers N = 6, the first sentence representation vector is sequentially passed through N encoders with the same structure. For example, the same structure means that the N encoders all include: a self-attention module, a feed forward network module, a first summation and normalization module, and a second summation and normalization module. The parameters of the N encoders are different.

[0067] During the processing of N encoders, the output of each encoder is input into the next encoder, and the output of each encoder has the same dimension, for example, 512×512.

[0068] As an example implementation, the first sentence representation vector is sequentially passed through N encoders of the same structure to obtain the Nth sentence encoding vector, including:

[0069] Input the first sentence representation vector into the first encoder of N encoders to obtain the first sentence encoding vector;

[0070] When N is greater than or equal to 2, the i-th sentence encoding vector is obtained by the following steps, where i is a positive integer greater than or equal to 2 and less than or equal to N:

[0071] The i-1th sentence encoding vector is input into the i-th encoder among N encoders to obtain the i-th sentence encoding vector, where the i-1th sentence encoding vector is the sentence encoding vector output by the i-1th encoder among N encoders.

[0072] When the number of encoders N is 6, the sentence encoding vector with a dimension of 512×512 output by the 6th encoder is input into the Linear1 (linearly changing network structure 1) layer and subjected to the first linear transformation to obtain the first semantic representation vector m; the sentence encoding vector with a dimension of 512×512 output by the 6th encoder is input into the Linear2 (linearly changing network structure 2) layer and subjected to the second linear transformation to obtain the perturbation weight vector σ.

[0073] The network structure of Linear1 layer is the same as that of Linear2 layer, but the structural parameters are different.

[0074] As an example implementation, the above-mentioned inputting the i-1th sentence encoding vector into the i-th encoder among N encoders to obtain the i-th sentence encoding vector includes:

[0075] Input the i-1th sentence encoding vector into the self-attention module in the i-th encoder to obtain the i-th self-attention vector, where the i-1th sentence encoding vector has the same dimension as the i-th self-attention vector;

[0076] Input the i-th self-attention vector and the i-1-th sentence encoding vector into the first summation and normalization module in the i-th encoder, so that the first summation and normalization module sums the i-th self-attention vector and the i-1-th sentence encoding vector to obtain a first sum vector, and normalizes the first sum vector to obtain an i-th normalized vector, wherein the i-1-th sentence encoding vector and the i-th normalized vector have the same dimension;

[0077] Input the i-th normalized vector into the feedforward network module in the i-th encoder to obtain the i-th feedforward vector, where the i-1-th sentence encoding vector has the same dimension as the i-th feedforward vector;

[0078] The i-th normalized vector and the i-th feedforward vector are input into the second summing and normalization module in the i-th encoder, so that the second summing and normalization module sums the i-th normalized vector and the i-th feedforward vector to obtain a second summed vector, and normalizes the second summed vector to obtain the i-th sentence encoding vector, wherein the i-1-th sentence encoding vector has the same dimension as the i-th sentence encoding vector.

[0079] The following takes i=2 as an example and explains the processing of each layer encoder in conjunction with FIG8 .

[0080] In step S81, the first sentence encoding vector is input into the self-attention module in the second encoder to obtain the second self-attention vector.

[0081] The encoder in the embodiment of the present application applies a self-attention mechanism in the self-attention module. The self-attention mechanism is used to find the semantic association between each word element (token) in a sequence (sentence) and other words in the sequence. The self-attention mechanism is to calculate the representation vector of each position in the input sequence (such as a sentence), and then calculate a weight distribution based on these representation vectors, and finally apply this weight distribution to each position in the input sequence to obtain a weighted representation vector. In this way, the self-attention mechanism allows the model where the encoder is located to pay attention to any position in the input sequence when processing the current input, without relying on the previous calculation results. This enables the model where the encoder is located to better capture the long-range dependencies in the input sequence.

[0082] The calculation method of the self-attention mechanism is implemented by the following formula (1):

[0083] Where Q is the input vector representation of the original sample. For example, for the first sentence, "Sincerity can move even metal and stone," Q is the corresponding vector representation (the first sentence representation vector). In the self-attention mechanism, K = Q and V = Q. Q, K, and V represent the query vector (Query), key vector (Key), and value vector (Value), respectively.

[0084] The dimension of the first sentence representation vector is max_seq_length * hidden_size, where max_seq_length can be, but is not limited to, the maximum character length of the original text (the first sentence), which can be understood here as the hidden layer size. Assuming the maximum character length of the first sentence is 512, the dimension of the second self-attention vector is 512×512.

[0085] In step S82, the second self-attention vector and the first sentence encoding vector in step S81 are input into the first summing and normalization module in the second encoder to obtain a second normalized vector. The first summing and normalization module can first sum the second self-attention vector and the first sentence encoding vector to obtain a summed vector. By summing the second self-attention vector output by the self-attention mechanism with the first sentence encoding vector, the original information can be enhanced. The summing process can be performed element by element (each element is added to the element at the same position as it).

[0086] The summed vector is then normalized. This can be done using Norm(Q+Attention(Q,K,V)), where Norm is the normalization function. The dimension of the second normalized vector after normalization remains 512×512. Assume that the normalized result is x1.

[0087] The purpose of normalization is to ensure that the information before and after the self-attention mechanism is retained.

[0088] In step S83, the second normalized vector is input into the feedforward network module in the second encoder to obtain a second feedforward vector.

[0089] Among them, the feedforward network module includes a feedforward network. The result output by the feedforward network is Feed_forward(x1), wherein Feed_forward is the calculation unit of the feedforward network. Using the feedforward network, the first normalized vector is first subjected to dimensionality increase processing, and then to dimensionality reduction processing. Its purpose is to further perform information fusion on the first normalized vector output in the above step S82. That is, the information of each input unit (vector representation) is subjected to matrix operations and normalization of the feedforward network, and finally output as a vector representation (that is, the information of all text inputs is fused). The dimension of the second feedforward vector is still 512×512.

[0090] In step S84, the second feedforward vector and the second normalized vector are input into the second addition and normalization module in the second encoder to obtain the second sentence encoding vector.

[0091] Specifically, normalization is performed through Norm(x1+Feed_forward(x1)) to obtain the second sentence encoding vector.

[0092] Repeat steps S81 to S84 six times, and after the last encoder outputs the sixth sentence encoding vector, use the Linear1 layer and Linear2 layer shown in FIG8 to obtain the first semantic representation vector m and the perturbation weight vector σ output by the encoder.

[0093] As an exemplary implementation, performing a first linear transformation on the Nth sentence encoding vector to obtain a first semantic representation vector includes: multiplying the Nth sentence encoding vector by a first weight matrix to obtain the first semantic representation vector, wherein the dimension of the Nth sentence encoding vector and the dimension of the first weight matrix are both M×M, where M is a positive integer greater than or equal to 2;

[0094] Performing a second linear transformation on the Nth sentence encoding vector to obtain a perturbation weight vector includes: multiplying the Nth sentence encoding vector by a second weight matrix to obtain a perturbation weight vector, wherein the dimension of the Nth sentence encoding vector and the dimension of the second weight matrix are both M×M, and the first weight matrix and the second weight matrix are different weight matrices. The perturbation weight vector output by the encoder reflects the weight of each position in the sentence.

[0095] For example, assuming N = 6, the sixth sentence encoding vector output by the second summation and normalization module in the sixth encoder is multiplied by the first weight matrix to obtain the first semantic representation vector m. The sixth sentence encoding vector is multiplied by the second weight matrix to obtain the perturbation weight vector σ. The first semantic representation vector m and the perturbation weight vector σ have the same dimension, both 512×512.

[0096] As an example, the first semantic representation vector is perturbed according to the perturbation weight vector and the perturbation vector to obtain the second semantic representation vector, including:

[0097] Determining a perturbation encoding vector according to the perturbation weight vector and the perturbation vector, wherein the perturbation encoding vector and the first semantic representation vector have the same dimension;

[0098] The disturbance coding vector and the first semantic representation vector are added together to obtain a second semantic representation vector.

[0099] According to the perturbation weight vector and the perturbation vector, the perturbation coding vector is determined, including:

[0100] Multiply the perturbation weight vector after the exponential operation by each row vector in the perturbation vector to obtain each row vector in the perturbation code vector;

[0101] The disturbance coding vector and the first semantic representation vector are added together to obtain a second semantic representation vector, including:

[0102] Each row vector in the perturbation coding vector is summed with the vector of the corresponding row in the first semantic representation vector to obtain a second semantic representation vector.

[0103] Referring to the method mentioned in the above embodiment of generating adversarial attack samples by adding human-imperceptible disturbances to text data samples, the disturbance vector is applied to the first semantic representation vector to obtain a second semantic representation vector.

[0104] In one embodiment, the perturbation of the first semantic representation vector according to the perturbation weight vector and the perturbation vector to obtain the second semantic representation vector includes:

[0105] The second semantic representation vector is obtained by the following formula (2): k =exp(σ k )×e k +m k (2)

[0106] Among them, c k is the k-th row vector of dimension 1×M in the second semantic representation vector, σ k represents the vector with dimension 1×M in the kth row of the perturbation weight vector, e k Represents the vector of dimension 1×M in the kth row of the perturbation vector, exp(σ k )×e k Represents the vector of dimension 1×M in the kth row of the perturbation code vector, m k Represents the vector with dimension 1×M of the k-th row in the first semantic representation vector, where k is a positive integer greater than or equal to 1 and less than or equal to M, the dimension of the perturbation weight vector, the dimension of the perturbation vector, the dimension of the perturbation encoding vector, the dimension of the first semantic representation vector, and the dimension of the second semantic representation vector are all M×M, and M is a positive integer greater than or equal to 2.

[0107] Among them, σ is used to control the degree of disturbance of the original input information by the disturbance information. The perturbation weight vector exp(σ k ) is multiplied by the k-th row vector in the perturbation vector e to obtain the k-th row in the perturbation code vector. Similarly, a perturbation code vector of dimension M×M (512×512) is obtained. By adding the perturbation code vector and the first semantic representation vector, the perturbation information and the information contained in the first sentence are fused to obtain the second semantic representation vector.

[0108] By adopting the above method, the magnitude of the disturbance degree can be set by the perturbation weight vector, thereby controlling the semantic distance between the second semantic representation vector and the first semantic representation vector. The semantic distance between the sentence-granularity adversarial attack sample generated in this way and the original input sample is controllable. This is in line with the fundamental purpose of generating adversarial attack samples, that is, to perform perturbations at the sentence level under the condition of semantic similarity with the original sample to obtain corresponding samples with similar semantics / intentions. In this way, the effectiveness of the generated adversarial attack samples can be improved. In addition, by adjusting the perturbation weight vector, perturbation information of different disturbance degrees can be added to the original input sample, thereby increasing the diversity of the hidden layer representation of the original input sample and improving the diversity of the generated adversarial attack samples.

[0109] As an example implementation, the above decoding of the second semantic representation vector to obtain the second sentence includes:

[0110] Pass the second semantic representation vector through P decoders of the same structure in sequence to obtain the P-th sentence decoding vector, where P is a positive integer greater than or equal to 1;

[0111] Perform a third linear transformation on the P-th sentence decoding vector to obtain a second sentence representation vector;

[0112] The second sentence is determined according to the second sentence representation vector.

[0113] FIG9 is a schematic diagram of the structure of a decoder according to an embodiment of the present application. As shown in FIG9 , the decoder model includes P decoders connected in sequence. The P decoders have the same structure, for example, each including a first self-attention module, a third summation and normalization module, a second self-attention module, a fourth summation and normalization module, and a feedforward network module. Although the structures of the P decoders are the same, their parameters are different.

[0114] Figure 9 uses the detailed structure of one of the decoders as an example to illustrate an embodiment of the present application. By sequentially passing the second semantic representation vector through P decoders of the same structure, the P-th sentence decoding vector is obtained, that is, the sentence decoding vector output by the feedforward network in the P-th decoder.

[0115] Using the linear transformation structure in the Linear3 layer shown in FIG9 , a third linear transformation is performed on the P-th sentence decoding vector to obtain a second sentence representation vector, and the second sentence is further determined.

[0116] As an example, the second semantic representation vector is sequentially passed through P decoders of the same structure to obtain the P-th sentence decoding vector, including:

[0117] Inputting the second semantic representation vector and the preset start symbol into the first decoder of the P decoders to obtain the first sentence decoding vector;

[0118] When P is greater than or equal to 2, the j-th sentence decoding vector is obtained by the following steps, where j is a positive integer greater than or equal to 2 and less than or equal to P:

[0119] The second semantic representation vector and the j-1th sentence decoding vector are input into the j-th decoder among the P decoders to obtain the j-th sentence decoding vector, where the j-1th sentence decoding vector is the sentence decoding vector output by the j-1th decoder among the P decoders.

[0120] Assuming P = 6, for the first decoder, the preset start symbol is input into the first decoder, and it is processed by the first self-attention module, the third summation and normalization module, the second self-attention module, the fourth summation and normalization module and the feedforward network module in the first decoder to obtain the first sentence decoding vector.

[0121] Among them, the second semantic representation vector serves as the input of the middle layer of the first decoder, that is, as the input of the second self-attention module.

[0122] The second semantic representation vector and the first sentence decoding vector are input into the second decoder to obtain the second sentence decoding vector; the second semantic representation vector and the second sentence decoding vector are input into the third decoder to obtain the third sentence decoding vector; and so on, the sixth sentence decoding vector output by the sixth decoder is obtained.

[0123] As an example implementation, the second semantic representation vector and the j-1th sentence decoding vector are input into the jth decoder of the P decoders to obtain the jth sentence decoding vector, including:

[0124] Input the j-1th sentence decoding vector into the first self-attention module in the j-th decoder to obtain the j-th first self-attention vector, where the j-1th sentence decoding vector has the same dimension as the j-th self-attention vector;

[0125] Input the j-th self-attention vector and the j-1-th sentence decoding vector into the third summation and normalization module in the j-th decoder to obtain the j-th third normalized vector, wherein the j-1-th sentence decoding vector and the j-th third normalized vector have the same dimension. The third summation and normalization module is used to sum the j-th self-attention vector and the j-1-th sentence decoding vector to obtain a third sum vector, and normalize the third sum vector to obtain the j-th third normalized vector;

[0126] Input the jth third normalized vector and the second semantic representation vector into the second self-attention module in the jth decoder to obtain the jth second self-attention vector, where the jth third normalized vector has the same dimension as the second semantic representation vector and the jth second self-attention vector;

[0127] Inputting the j-th second self-attention vector and the j-th third normalized vector into the fourth summation and normalization module in the j-th decoder to obtain the j-th fourth normalized vector, wherein the j-th second self-attention vector and the j-th fourth normalized vector have the same dimension, and the fourth summation and normalization module is used to sum the j-th second self-attention vector and the j-th third normalized vector to obtain a fourth sum vector, and normalize the fourth sum vector to obtain the j-th fourth normalized vector;

[0128] The jth fourth normalized vector is input into the feedforward network module in the jth decoder to obtain the jth sentence decoding vector, where the jth fourth normalized vector has the same dimension as the jth sentence decoding vector.

[0129] Assuming j=2, the implementation process of obtaining the j-th sentence decoding vector is described in conjunction with the structure of the decoder shown in FIG9 .

[0130] In step S91, the first sentence decoding vector is input into the first self-attention module in the second decoder to obtain the second first self-attention vector.

[0131] The method for calculating self-attention can refer to the above formula (1), which will not be repeated here. The function of the first self-attention module is to understand the information input into the second decoder.

[0132] In step S92, the second first self-attention vector and the first sentence decoding vector in step S91 are input into the third addition and normalization module in the second encoder to obtain the second third normalized vector.

[0133] The normalization process may refer to the description of step S82 in the encoder part above, wherein the dimension of the second third normalized vector after normalization is still 512×512.

[0134] The purpose of normalization is to ensure that the information before and after the self-attention module is preserved.

[0135] In step S93, the second third normalized vector and the second semantic representation vector are input into the second self-attention module in the second decoder to obtain a second self-attention vector with a dimension of 512×512.

[0136] During the self-attention processing, the second semantic representation vector c output by the encoder is used as the Q and K values ​​in the second self-attention calculation, and the second third normalized vector output by step S92 is used as V. The self-attention calculation is performed using the above formula (1), and the second self-attention vector with a dimension of 512×512 is obtained.

[0137] Among them, the second self-attention module in the decoder is used to fuse the second semantic representation vector after the perturbation information with the second third normalized vector.

[0138] In step S94, the second self-attention vector and the second third normalized vector are input into the fourth addition and normalization module in the second decoder to obtain the second fourth normalized vector with a dimension of 512×512.

[0139] The normalization process can refer to the description in step S92 and will not be repeated here.

[0140] In step S95, the second fourth normalized vector is input into the feedforward network module in the second decoder to obtain a second sentence decoding vector with a dimension of 512×512.

[0141] The feedforward network in the second decoder is used to perform depth calculation on the final fusion information. The calculation formula can refer to the description of step S83 above.

[0142] Repeat steps S91 to S95 for P=6 times, and after the last decoder outputs the sixth sentence decoding vector, the second sentence output by the decoder is obtained by using the Linear3 layer and softmax shown in Figure 9.

[0143] As an example, performing the third linear transformation on the P-th sentence decoding vector to obtain the second sentence representation vector includes: multiplying the P-th sentence decoding vector having a dimension of M×M by a third weight matrix having a dimension of M×T to obtain a second sentence representation vector having a dimension of M×T, where T represents the number of words included in a preset dictionary and T is a positive integer greater than or equal to 2;

[0144] Determine the second sentence according to the second sentence representation vector, including: normalizing the second sentence representation vector with a dimension of M×T to obtain a normalized vector with a dimension of M×T; when the value of the vector with a dimension of 1×T in the k-th row of the normalized vector with a dimension of M×T meets the condition of determining the corresponding word from a preset dictionary, and the value at the q-th position in the vector with a dimension of 1×T in the k-th row is the largest, determine the word at the q-th position in the preset dictionary as the word in the second sentence, wherein k is a positive integer greater than or equal to 1 and less than or equal to M, and q is a positive integer greater than or equal to 1 and less than or equal to T.

[0145] Assuming P = 6 and M = 512, after passing the second semantic representation vector sequentially through six decoders of the same structure to obtain the sixth sentence decoding vector output by the feedforward network in the sixth decoder, the second sentence representation vector is obtained by linear transformation processing in the Linear3 layer. Specifically, the sixth sentence decoding vector of dimension 512×512 is multiplied by the third weight matrix of dimension 512×1000 to obtain a second sentence representation vector of dimension 512×1000, where 1000 represents the number of words included in the preset dictionary.

[0146] Since all 1000 words in the dictionary are involved in the calculation, after performing softmax (normalization) on the second sentence representation vector with a dimension of 512×1000, a normalized vector with a dimension of 512×1000 is obtained as shown in Figure 10. Specifically, the softmax classifier is used to predict the characters with probability and predict the next generated character. The calculation process of the softmax classifier can be implemented by the following formula (3):

[0147] Where W is the model parameter, T is the dictionary size (for example, 1000 words), t is the ordinal number of each word in the dictionary, and p(y|x) represents the probability that the softmax classifier predicts the output as y under the condition that the output of the feedforward network is x.

[0148] In other words, by traversing the 1000 words in the dictionary and combining the above formula (3), the prediction probability of each word in the 1000 words is calculated, and the top N words with the largest prediction probability are taken as candidate results.

[0149] The values ​​of the vector with dimension 1×T in the kth row of the above-mentioned normalized vector with dimension M×T meet the conditions for determining the corresponding word from the preset dictionary, including but not limited to: at least some of the T values ​​on the row are greater than 0 and less than 1; or greater than a preset value, for example, greater than 0.6, 0.7 or 0.8, etc.

[0150] For each vector with a dimension of 1×1000 per row, the softmax outputs the predicted probability of each word, and the sum of the probabilities of each word is 1. The word with the largest value among the 1000 probability values in each row is determined as the word in the second sentence.

[0151] For example, for the first row in the normalized vector, the value at the second position is the largest, which means that the second word "心" in the dictionary is the word in the second sentence. For the second row in the normalized vector, the value at the third position is the largest, which means that the third word "诚" in the dictionary is the word in the second sentence. For the third row in the normalized vector, the value at the fifth position is the largest, which means that the fifth word "则" in the dictionary is also the word in the second sentence. And so on, multiple characters included in the second sentence can be determined.

[0152] Taking the first sentence of the original input of the encoder as "精诚所至金石为开" and the candidate generation result taking the parameters of beam search: beam_number = 1, P = 6 as an example, the implementation process of the decoder is described below.

[0153] In step S1002, after inputting the first sentence representation vector of the first sentence into the encoder shown in Figure 8 to obtain the second semantic representation vector after adding perturbation information, it is input into the self-attention mechanism of the decoder (in subsequent steps, the second semantic representation vector is input).

[0154] In step S1004, the preset start symbol [CLS] is input into the decoder. The decoder generates different candidate words for each position of the second sentence to be generated, and different candidate words correspond to different search paths. The decoder outputs the result with the highest probability, and outputs the character "心" (combined with Figure 10). The highest probability refers to the search path with the highest cumulative probability. <​​​​​​​​​​In step S1012, the input and output characters of step S1010 are concatenated to obtain "[CLS] The heart is sincere and it will work", which is used as the input of the decoder. The decoder outputs the result with the highest probability, which is the output character terminator "[EOS]".

[0159] When the decoder outputs the terminator, the generation process of the second statement ends.

[0160] In step S1014, post-processing is performed.

[0161] Remove the start symbol [CLS] and the terminator [EOS]. The finally generated adversarial attack text is "The heart is sincere and it will work".

[0162] The above is an example process of generating the candidate attack sample "The heart is sincere and it will work" of the sentence granule after the encoder-decoder from the first statement "Sincerity can move mountains".

[0163] Next, the sorting and screening process of the adversarial attack candidate text will be described.

[0164] (1) Generating candidate adversarial attack texts based on the beam search algorithm

[0165] Taking Figure 11 as an example, the second semantic representation vector of "Sincerity can move mountains" is input into the decoder, and the decoder may output the following results: "With all sincerity", "The heart is sincere and it will work", "The mind should be sincere", etc. The corresponding cumulative probabilities of different generation results are different.

[0166] In the process of the decoder generating candidate results word by word, different candidate words are generated for each position of the second statement to be generated, and then the top k candidate words are selected. Different candidate words correspond to different search paths (abbreviated as paths). The top k candidate words are the top k candidate words with the largest cumulative probability of the paths among the candidate words at each position. Different generation results exist under different paths. In this way, multiple candidate results can be obtained. According to the greedy algorithm (i.e., beam_number = 1), taking the path with the maximum probability each time may not obtain the generation result with the largest cumulative probability. Therefore, the beam search algorithm is adopted in the embodiments of the present application. In the beam search algorithm, during the process of generating word by word by traversing the paths, the top beam_number (the maximum number of candidates with the maximum probability at each node) candidates with the maximum probability are taken for the next decoder generation process.

[0167] As shown in FIG. 11, at the position of the first word of the second sentence to be generated, after the input [CLS] start symbol, two candidate paths of "诚" (probability 0.4) and "心" (probability 0.2) are taken for the next generation operation of the decoder. Then, for the path of "诚", two paths with the highest probabilities of "诚心" and "诚实" are taken to continue the generation operation. For the path of "心", the generation operation continues along the two paths with the highest probabilities of "心诚" and "心意". And so on, multiple candidate adversarial attack texts such as "心诚则灵", "诚心诚意", and "心意要真诚" can be generated.

[0168] (2) Screening candidate generation results based on strategies and rules

[0169] For different tasks, the candidate adversarial attack texts generated based on the beam search algorithm above need to be further screened. In the embodiments of this application, a rule-based screening method is adopted, which is specifically as follows:

[0170] Taking the text adversarial attack in the context of the named entity recognition task in the field of natural language processing as an example. Regular expressions are used to filter out candidate adversarial attack samples that meet the conditions and eliminate samples that attack the target entity (noisy samples), as shown below:

[0171] For candidate adversarial attack sample x in all adversarial attack samples:

[0172] Among them, n is the number of correct named entities, and ENTITYi is the corresponding i-th correctly recognized named entity. A named entity is an entity with a specific meaning in the text, for example, proper nouns, personal names, place names, etc. The goal of the named entity recognition task is to identify the entities in the text and classify them. The candidate adversarial attack sample does not match the regular expression ^(?!.*\bENTITYi\b).+$, which can be understood as ENTITYi is a substring of the candidate adversarial attack sample.

[0173] For example, for the original text "The Yellow River Chorus in the evening party was magnificent", the correct entity recognition result of named entity recognition is (song, "The Yellow River Chorus"), and the number of correct named entities n = 1. There are 4 candidate attack samples generated by the methods of various embodiments of this application:

[0174] Candidate 1: "The performance of Mother River in the evening party was magnificent"

[0175] Candidate 2: "The Yellow River Chorus in the evening party was magnificent"

[0176] Candidate 3: "During the evening performance, the Yellow River Chorus was really impressive"

[0177] Candidate 4: "The Yellow River Chorus performed tonight was really impressive"

[0178] According to the filtering algorithm, candidates 1 and 4, because they matched the correct entity result "Yellow River Cantata," attacked the entity that needed to be retained (the "Yellow River Cantata" entity was modified due to the attack), and thus are generated noise samples and need to be eliminated. The entities that need to be retained are substrings of the original text that cannot be modified. Candidates 2 and 3 meet the task requirements (attacking the named entity recognition task while not attacking the target named entity) and are retained as the final selected sentence-granular text attack generation samples. Through this screening process, samples that do not meet the requirements are filtered out from the candidate adversarial attack samples.

[0179] The beam search algorithm determines candidate adversarial attack samples. At each step, multiple words are selected to form candidate adversarial attack samples. The generated results with the highest cumulative probability across different paths are selected as candidate adversarial attack samples. This improves the stability of the quality of the adversarial attack samples and enhances the robustness of the model when using them to adjust the task processing model. Screening candidate adversarial attack samples improves the effectiveness and quality of the resulting adversarial attack samples.

[0180] In addition, in order to ensure the quality of the adversarial attack text generated by the encoder-decoder model, it is usually necessary to train the encoder-decoder model. The training process (testing process) includes but is not limited to the following three stages:

[0181] (1) Pre-trained encoder-decoder model

[0182] The parameters of the encoder-decoder are pre-trained using massive unlabeled corpus. Since the language model of the encoder-decoder needs to be a universal language model, conventional pre-training methods can be applied to complete the pre-training process.

[0183] (2) Collect data

[0184] i) Data source collection 1: annotating some manually annotated raw data and adversarial attack sample pairs (e.g., "With sincerity, even metal and stone can be opened" and "With sincerity, miracles can be achieved" are a pair of samples);

[0185] ii) Data Source 2: Use the results of existing character / word granularity attacks as labeled sample pairs. For example, in the current character granularity adversarial attack algorithm, the attack on "Reading the History of the Ming Dynasty" generates "Reading the History of the Ming Dynasty." The preceding and following sentences are considered as sample pairs and used as the training data source.

[0186] (3) Fine-tuning the model

[0187] Based on the pre-trained model in step 1), the model is fine-tuned using the data in step 2) to obtain the final model parameters.

[0188] As an example, the above method further includes:

[0189] When the first sentence includes F words, obtain word vectors of the F words in the first sentence to obtain F word vectors of dimension 1×M, where F is a positive integer greater than or equal to 2, and M is a positive integer greater than or equal to 2;

[0190] When F is less than M, concatenate F word vectors with a dimension of 1×M and MF padded vectors with a dimension of 1×M to obtain the first sentence representation vector with a dimension of M×M, where the values ​​in the MF padded vectors with a dimension of 1×M are all 0.

[0191] For example, if M = 512, where M represents the maximum input length of each character, then each character in the first sentence is converted into a 1×512-dimensional word vector. If the first sentence is "Sincerity can move even metal and stone," F = 8, which is less than M. Then, convert each of these eight characters into a 1×512-dimensional word vector, resulting in eight 1×512-dimensional word vectors, which is less than 512 1×512-dimensional word vectors. Then, the remaining MF(512-8) 1×512-dimensional vectors can be padded with zeros, resulting in 504 1×512-dimensional padded vectors.

[0192] When F is equal to M, concatenate F word vectors of dimension 1×M to obtain the first sentence representation vector of dimension M×M.

[0193] As an example, the method in each of the above embodiments further includes:

[0194] The target sentence recognition model to be tested is tested using the first sentence and the second sentence to obtain a target test result, wherein the target test result is used to indicate whether the same recognition result is obtained for the first sentence and the second sentence.

[0195] In a specific application scenario, assuming that the task processing model is a model for sentence recognition, the implementation process of testing the target sentence recognition model using the first sentence and the second sentence includes:

[0196] Inputting the first sentence into the target sentence recognition model to obtain a first emotion label, wherein the first emotion label is used to represent the emotion type expressed by the semantics of the first sentence;

[0197] Inputting the second sentence into the target sentence recognition model to obtain a second emotion label, wherein the second emotion label is used to represent the emotion type expressed by the semantics of the second sentence;

[0198] Compare whether the first emotion label and the second emotion label are the same;

[0199] When the first emotion label and the second emotion label are the same, the target test result indicates that the first sentence and the second sentence are recognized to have the same recognition result;

[0200] When the first emotion label and the second emotion label are different, the target test result indicates that different recognition results are obtained for the first sentence and the second sentence.

[0201] For example, assuming that the pre-set emotion labels are happy, sad, and angry, the first sentence is input into the target sentence recognition model to obtain the first emotion label; the second sentence (adversarial attack text) is input into the target sentence recognition model to obtain the second emotion label.

[0202] If the first emotion label and the second emotion label are the same, for example, both are happy, it means that when there is a small deviation in the input sample or a small disturbance occurs inside the target sentence recognition model, the output of the target sentence recognition model is still the correct result, which means that the impact of the adversarial attack text on the target sentence recognition model is small, that is, the target sentence recognition model is more robust.

[0203] On the contrary, when the first emotion label and the second emotion label are different, it includes:

[0204] When the target test result indicates that different recognition results are obtained for the first sentence and the second sentence, prompt information is generated, wherein the prompt information is used to prompt adjustment of the target sentence recognition model.

[0205] By adjusting the target sentence recognition model (which can also be understood as adversarial training), the vulnerabilities of the target sentence recognition model can be fixed, thereby quickly increasing the robustness of the target sentence recognition model.

[0206] In addition, in the process of using the above sentence generation method to generate adversarial attack samples and perform adversarial training on the task processing model, the beneficial effects achieved include at least one of the following:

[0207] 1. The technical solution in this application provides a sentence-granular adversarial sample generation scheme for the adversarial attack method. That is, in addition to the character / word granularity, a sentence-granular adversarial attack sample scheme is provided, filling the gap in the sentence-granularity text attack method.

[0208] 2. This application proposes an effective end-to-end solution for generating effective adversarial samples. Compared with the previous text adversarial sample generation method based on rule and strategy addition / deletion / modification at the word granularity, this application is based on the encoder-decoder model and generates adversarial attack samples through an end-to-end automated method, providing new ideas and implementation methods for adversarial attacks. By perturbation weight vectors, the degree of perturbation can be set, thereby controlling the semantic distance between the second semantic representation vector and the first semantic representation vector. The semantic distance between the sentence-granularity adversarial attack samples generated in this way and the original input samples is controllable. This is in line with the fundamental purpose of generating adversarial attack samples, that is, to perform perturbations at the sentence level while being semantically similar to the original samples to obtain corresponding samples with similar semantics / intentions. In this way, the effectiveness of the generated adversarial attack samples can be improved. In addition, by adjusting the perturbation weight vectors, perturbation information of different perturbation degrees can be added to the original input samples, increasing the diversity of the hidden layer representation of the original input samples and improving the diversity of the generated adversarial attack samples.

[0209] 3. Application to text classification tasks: This method was applied to text classification tasks, effectively identifying model weaknesses and corresponding samples, rapidly increasing the robustness of the model and correcting model vulnerabilities.

[0210] 4. Application to named entity recognition tasks: This method is applied to named entity recognition tasks. By using strategies and rules to filter the candidate generation results, it can effectively filter out samples that the model cannot correctly predict and effectively repair the model.

[0211] For the sake of simplicity, the aforementioned method embodiments are described as a series of action combinations. However, those skilled in the art should be aware that this application is not limited by the order of the actions described, because according to this application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in this specification are all preferred embodiments, and the actions and modules involved are not necessarily required by this application.

[0212] According to another aspect of the embodiment of the present application, there is also provided a sentence generation device as shown in FIG12 . The device includes an encoding unit 1202 , a first processing unit 1204 , and a decoding unit 1206 .

[0213] Encoding unit 1202 is configured to encode a first sentence representation vector of a first sentence to obtain a first semantic representation vector and a perturbation weight vector. The first sentence representation vector is determined based on the word vectors of each word in the first sentence. The first semantic representation vector is used to represent the semantics of the first sentence. The perturbation weight vector is used to control the perturbation of the first semantic representation vector by a predetermined perturbation vector.

[0214] The first processing unit 1204 is configured to perturb the first semantic representation vector according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector.

[0215] The decoding unit 1206 is configured to decode the second semantic representation vector to obtain a second sentence. The first sentence and the second sentence have the same or similar semantics.

[0216] The encoding unit 1202 includes:

[0217] A first processing module is configured to sequentially pass the first sentence representation vector through N encoders of the same structure to obtain an Nth sentence encoding vector, where N is a positive integer greater than or equal to 1;

[0218] A second processing module is used to perform a first linear transformation on the Nth sentence encoding vector to obtain a first semantic representation vector;

[0219] The third processing module is used to perform a second linear transformation on the Nth sentence encoding vector to obtain a disturbance weight vector.

[0220] The first processing module includes:

[0221] The first processing submodule is used to perform the following processing:

[0222] Input the first sentence representation vector into the first encoder of N encoders to obtain the first sentence encoding vector;

[0223] When N is greater than or equal to 2, the i-th sentence encoding vector is obtained by the following steps, where i is a positive integer greater than or equal to 2 and less than or equal to N:

[0224] The i-1th sentence encoding vector is input into the i-th encoder among N encoders to obtain the i-th sentence encoding vector, where the i-1th sentence encoding vector is the sentence encoding vector output by the i-1th encoder among N encoders.

[0225] The first processing module includes:

[0226] The second processing submodule is used to input the i-1th sentence encoding vector into the self-attention module in the i-th encoder to obtain the i-th self-attention vector, wherein the i-1th sentence encoding vector and the i-th self-attention vector have the same dimension;

[0227] a third processing submodule, configured to input the i-th self-attention vector and the i-1-th sentence encoding vector into a first summing and normalization module in the i-th encoder, wherein the first summing and normalization module sums the i-th self-attention vector and the i-1-th sentence encoding vector to obtain a first summed vector, and normalizes the first summed vector to obtain an i-th normalized vector, wherein the i-1-th sentence encoding vector and the i-th normalized vector have the same dimension;

[0228] a fourth processing submodule, configured to input the i-th normalized vector into the feedforward network module in the i-th encoder to obtain the i-th feedforward vector, wherein the i-1-th sentence encoding vector has the same dimension as the i-th feedforward vector;

[0229] The fifth processing submodule is used to input the i-th normalized vector and the i-th feedforward vector into the second summing and normalization module in the i-th encoder, so that the second summing and normalization module sums the i-th normalized vector and the i-th feedforward vector to obtain a second summed vector, and normalizes the second summed vector to obtain the i-th sentence encoding vector, wherein the i-1-th sentence encoding vector has the same dimension as the i-th sentence encoding vector.

[0230] The second processing module includes:

[0231] a first multiplication submodule, configured to multiply the Nth sentence encoding vector by the first weight matrix to obtain a first semantic representation vector, wherein the dimension of the Nth sentence encoding vector and the dimension of the first weight matrix are both M×M, where M is a positive integer greater than or equal to 2;

[0232] The third processing module includes:

[0233] The second multiplication submodule is used to multiply the Nth sentence encoding vector by the second weight matrix to obtain a perturbation weight vector, wherein the dimension of the Nth sentence encoding vector and the dimension of the second weight matrix are both M×M, and the first weight matrix and the second weight matrix are different weight matrices.

[0234] The first processing unit 1204 includes:

[0235] a fourth processing module, configured to determine a perturbation coding vector based on the perturbation weight vector and the perturbation vector, wherein the perturbation coding vector and the first semantic representation vector have the same dimension;

[0236] The fifth processing module is used to add the disturbance coding vector and the first semantic representation vector to obtain a second semantic representation vector.

[0237] The fifth processing module includes:

[0238] The sixth processing submodule is used to multiply the perturbation weight vector after performing the exponential operation with each row vector in the perturbation vector to obtain each row vector in the perturbation coding vector; and add each row vector in the perturbation coding vector and the vector of the corresponding row in the first semantic representation vector to obtain the second semantic representation vector.

[0239] The decoding unit 1206 includes:

[0240] a sixth processing module, configured to sequentially pass the second semantic representation vector through P decoders of the same structure to obtain a P-th sentence decoding vector, where P is a positive integer greater than or equal to 1;

[0241] a seventh processing module, configured to perform a third linear transformation on the P-th sentence decoding vector to obtain a second sentence representation vector;

[0242] An eighth processing module is configured to determine the second sentence according to the second sentence representation vector.

[0243] The sixth processing module includes:

[0244] The seventh processing submodule is used to perform the following processing:

[0245] Inputting the second semantic representation vector and the preset start symbol into the first decoder of the P decoders to obtain the first sentence decoding vector;

[0246] When P is greater than or equal to 2, the j-th sentence decoding vector is obtained by the following steps, where j is a positive integer greater than or equal to 2 and less than or equal to P:

[0247] The second semantic representation vector and the j-1th sentence decoding vector are input into the j-th decoder among the P decoders to obtain the j-th sentence decoding vector, where the j-1th sentence decoding vector is the sentence decoding vector output by the j-1th decoder among the P decoders.

[0248] The sixth processing module further includes:

[0249] an eighth processing submodule, configured to input the j-1th sentence decoding vector into the first self-attention module in the j-th decoder to obtain the j-th first self-attention vector, wherein the j-1th sentence decoding vector and the j-th self-attention vector have the same dimension;

[0250] a ninth processing submodule, configured to input the j-th self-attention vector and the j-1-th sentence decoding vector into a third summing and normalization module in the j-th decoder to obtain a j-th third normalized vector, wherein the j-1-th sentence decoding vector and the j-th third normalized vector have the same dimension, the third summing and normalization module being configured to sum the j-th self-attention vector and the j-1-th sentence decoding vector to obtain a third summed vector, and to normalize the third summed vector to obtain a j-th third normalized vector;

[0251] a tenth processing submodule, configured to input the j-th third normalized vector and the second semantic representation vector into the second self-attention module in the j-th decoder to obtain a j-th second self-attention vector, wherein the j-th third normalized vector has the same dimension as the second semantic representation vector and the j-th second self-attention vector;

[0252] an eleventh processing submodule, configured to input the j-th second self-attention vector and the j-th third normalized vector into a fourth summing and normalizing module in the j-th decoder to obtain a j-th fourth normalized vector, wherein the j-th second self-attention vector and the j-th fourth normalized vector have the same dimension, the fourth summing and normalizing module being configured to sum the j-th second self-attention vector and the j-th third normalized vector to obtain a fourth summed vector, and to normalize the fourth summed vector to obtain the j-th fourth normalized vector;

[0253] The twelfth processing submodule is used to input the jth fourth normalized vector into the feedforward network module in the jth decoder to obtain the jth sentence decoding vector, wherein the jth fourth normalized vector and the jth sentence decoding vector have the same dimension.

[0254] The seventh processing module includes:

[0255] a thirteenth processing submodule, configured to multiply the P-th sentence decoding vector having a dimension of M×M by a third weight matrix having a dimension of M×T to obtain a second sentence representation vector having a dimension of M×T, where T represents the number of words included in a preset dictionary and T is a positive integer greater than or equal to 2;

[0256] The eighth processing module includes:

[0257] The fourteenth processing submodule is used to normalize the second sentence representation vector with a dimension of M×T to obtain a normalized vector with a dimension of M×T; when the value of the vector with a dimension of 1×T in the k-th row of the normalized vector with a dimension of M×T meets the condition of determining the corresponding word from the preset dictionary, and the value at the q-th position in the vector with a dimension of 1×T in the k-th row is the largest, the word at the q-th position in the preset dictionary is determined as the word in the second sentence, wherein k is a positive integer greater than or equal to 1 and less than or equal to M, and q is a positive integer greater than or equal to 1 and less than or equal to T.

[0258] The above device also includes:

[0259] an acquiring unit, configured to acquire word vectors of the F words in the first sentence when the first sentence includes F words, to obtain F word vectors of dimension 1×M, where F is a positive integer greater than or equal to 2, and M is a positive integer greater than or equal to 2;

[0260] A concatenation unit is used to concatenate F word vectors of 1×M dimension with MF padded vectors of 1×M dimension to obtain a first sentence representation vector of M×M dimension when F is less than M, wherein the values ​​in the MF padded vectors of 1×M dimension are all 0.

[0261] The above device also includes:

[0262] The testing unit is used to test the target sentence recognition model to be tested using the first sentence and the second sentence to obtain a target test result, wherein the target test result is used to indicate whether the same recognition result is recognized for the first sentence and the second sentence.

[0263] The above test unit includes:

[0264] A first input module, configured to input the first sentence into a target sentence recognition model to obtain a first emotion label, wherein the first emotion label is used to represent the emotion type expressed by the semantics of the first sentence;

[0265] a second input module, configured to input the second sentence into the target sentence recognition model to obtain a second emotion label, wherein the second emotion label is used to represent the emotion type expressed by the semantics of the second sentence;

[0266] A comparison module is configured to compare whether the first emotion label and the second emotion label are identical. If the first emotion label and the second emotion label are identical, the target test result indicates that the first sentence and the second sentence have the same recognition result. If the first emotion label and the second emotion label are different, the target test result indicates that the first sentence and the second sentence have different recognition results.

[0267] The above device also includes:

[0268] The second processing unit is configured to generate prompt information when the target test result indicates that different recognition results are obtained for the first sentence and the second sentence, wherein the prompt information is used to prompt adjustment of the target sentence recognition model.

[0269] By applying the above-mentioned device to perturb the first semantic representation vector of the first sentence according to the perturbation weight vector and the perturbation vector, a second semantic representation vector is obtained, and then by decoding the second semantic representation vector, a second sentence with the same or similar semantics as the first sentence is obtained, and the second sentence is used as a sentence-granularity adversarial attack sample, the gap in the sentence-granularity-based text adversarial attack method in the related technology is filled, and the technical problem in the related technology that the sentence-granularity text attack method cannot be used to evaluate the robustness of the task processing model is solved, thereby achieving the technical effect of improving the robustness of the task processing model.

[0270] The embodiment of the sentence generation device here can refer to the embodiment of the sentence generation method mentioned above, which will not be repeated here.

[0271] According to another aspect of the embodiments of the present application, an electronic device for implementing the above-mentioned sentence generation method is also provided, and the electronic device may be the terminal device shown in Figure 13. Various embodiments of the present application are described using the electronic device as a background device as an example. As shown in Figure 13, the electronic device includes a memory 1302 and a processor 1304, wherein the memory 1302 stores a computer program, and the processor 1304 is configured to execute the steps in any of the above-mentioned method embodiments through the computer program.

[0272] In various embodiments of the present application, the electronic device may be located in at least one network device among a plurality of network devices of a computer network.

[0273] In various embodiments of the present application, the processor may be configured to execute the following steps through a computer program:

[0274] S1, encoding a first sentence representation vector of a first sentence to obtain a first semantic representation vector and a disturbance weight vector, wherein the first sentence representation vector is a vector determined based on word vectors of each word in the first sentence, the first semantic representation vector is used to represent the semantics of the first sentence, and the disturbance weight vector is used to control the disturbance of the first semantic representation vector by a predetermined disturbance vector;

[0275] S2, perturbing the first semantic representation vector according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector;

[0276] S3: Decode the second semantic representation vector to obtain a second sentence, wherein the first sentence and the second sentence have the same or similar semantics.

[0277] Those skilled in the art will appreciate that the structure shown in FIG13 is for illustration only, and the electronic device may also be a target terminal such as a smartphone (such as an Android phone, an iOS phone, etc.), a tablet computer, a PDA, a mobile internet device (MID), or a PAD. FIG13 does not limit the structure of the electronic device. For example, the electronic device may include more or fewer components (such as a network interface, etc.) than those shown in FIG13, or may have a configuration different from that shown in FIG13.

[0278] Memory 1302 can be used to store software programs and modules, such as program instructions / modules corresponding to the sentence generation method and apparatus in the embodiments of the present application. Processor 1304 executes the software programs and modules stored in memory 1302 to perform various functional applications and data processing, thereby implementing the above-mentioned sentence generation method. Memory 1302 may include high-speed random access memory (RAM) and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, memory 1302 may further include memory remotely located relative to processor 1304, and these remote memories may be connected to the terminal via a network. Examples of such networks include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof. Memory 1302 may specifically, but is not limited to, be used to store the first sentence, the first semantic representation vector, and the perturbation weight vector. As an example, as shown in FIG13 , memory 1302 may, but is not limited to, include the encoding unit 1202, the first processing unit 1204, and the decoding unit 1206 of the above-mentioned sentence generation apparatus. In addition, it may also include but is not limited to other module units in the above-mentioned sentence generation device, which will not be repeated in this example.

[0279] The transmission device 1306 is used to receive or send data via a network. Specific examples of the network include wired networks and wireless networks. In one embodiment, the transmission device 1306 includes a network interface controller (NIC), which can be connected to other network devices and a router via a network cable to communicate with the Internet or a local area network. In one embodiment, the transmission device 1306 is a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0280] In addition, the electronic device further includes: a display 1308 for displaying the direction prompt information of the target sound; and a connection bus 1310 for connecting various module components in the electronic device.

[0281] In other embodiments, the target terminal or server may be a node in a distributed system, wherein the distributed system may be a blockchain system, and the blockchain system may be a distributed system formed by connecting multiple nodes via network communication. The nodes may form a peer-to-peer (P2P) network, and any computing device, such as a server, terminal, or other electronic device, may become a node in the blockchain system by joining the peer-to-peer network.

[0282] According to another aspect of the present application, a computer program product or computer program is provided, comprising computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the statement generation method provided in the various example implementations of the aforementioned server verification processing and other aspects, wherein the computer program is configured to perform the steps of any of the aforementioned method embodiments when executed.

[0283] In various embodiments of the present application, the computer-readable storage medium may be configured to store a computer program for performing the following steps:

[0284] S1, encoding a first sentence representation vector of a first sentence to obtain a first semantic representation vector and a disturbance weight vector, wherein the first sentence representation vector is a vector determined based on word vectors of each word in the first sentence, the first semantic representation vector is used to represent the semantics of the first sentence, and the disturbance weight vector is used to control the disturbance of the first semantic representation vector by a predetermined disturbance vector;

[0285] S2, perturbing the first semantic representation vector according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector;

[0286] S3: Decode the second semantic representation vector to obtain a second sentence, wherein the first sentence and the second sentence have the same or similar semantics.

[0287] In various embodiments of the present application, a person of ordinary skill in the art may understand that all or part of the steps in the various methods of the above embodiments may be completed by instructing the hardware related to the target terminal through a program, and the program may be stored in a computer-readable storage medium, which may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0288] The serial numbers of the above embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.

[0289] If the integrated units in the above embodiments are implemented in the form of software functional units and sold or used as independent products, they can be stored in the above-mentioned computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling one or more computer devices (which can be personal computers, servers, or network devices, etc.) to execute all or part of the steps of the methods of each embodiment of the present application.

[0290] In the above embodiments of the present application, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0291] In the several embodiments provided in this application, it should be understood that the disclosed client can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of units is merely a logical functional division. In actual implementation, there may be other division methods, such as combining or integrating multiple units or components into another system, or ignoring or not implementing some features. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interface, indirect coupling or communication connection of units or modules, and may be electrical or other forms.

[0292] Units described as separate components may or may not be physically separate, and components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the units may be selected according to actual needs to achieve the objectives of the various embodiments of the present application.

[0293] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0294] The above is only a preferred embodiment of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.

Claims

1. A sentence generation method, characterized in that: include: Encoding a first sentence representation vector of a first sentence to obtain a first semantic representation vector and a disturbance weight vector, wherein the first sentence representation vector is a vector determined according to word vectors of each word in the first sentence, the first semantic representation vector is used to represent the semantics of the first sentence, and the disturbance weight vector is used to control the disturbance of a predetermined disturbance vector to the first semantic representation vector; Perturbing the first semantic representation vector according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector; The second semantic representation vector is decoded to obtain a second sentence, wherein the first sentence and the second sentence have the same or similar semantics.

2. The method according to claim 1, characterized in that The encoding of the first sentence representation vector of the first sentence to obtain a first semantic representation vector and a disturbance weight vector includes: Passing the first sentence representation vector through N encoders with the same structure in sequence to obtain an Nth sentence encoding vector, where N is a positive integer greater than or equal to 1; Performing a first linear transformation on the Nth sentence encoding vector to obtain the first semantic representation vector; Perform a second linear transformation on the Nth sentence encoding vector to obtain the disturbance weight vector.

3. The method according to claim 2, characterized in that The step of sequentially passing the first sentence representation vector through N encoders having the same structure to obtain an Nth sentence encoding vector includes: Inputting the first sentence representation vector into a first encoder among the N encoders to obtain a first sentence encoding vector; When N is greater than or equal to 2, the i-th sentence encoding vector is obtained by the following steps, where i is a positive integer greater than or equal to 2 and less than or equal to N: Input the i-1th sentence encoding vector into the i-th encoder among the N encoders to obtain the i-th sentence encoding vector, wherein the i-1th sentence encoding vector is the sentence encoding vector output by the i-1th encoder among the N encoders.

4. The method according to claim 3, characterized in that The step of inputting the i-1th sentence encoding vector into the i-th encoder among the N encoders to obtain the i-th sentence encoding vector comprises: Inputting the i-1th sentence encoding vector into the self-attention module in the i-th encoder to obtain an i-th self-attention vector, wherein the i-1th sentence encoding vector has the same dimension as the i-th self-attention vector; Inputting the i-th self-attention vector and the i-1-th sentence encoding vector into a first summing and normalizing module in the i-th encoder, so that the first summing and normalizing module sums the i-th self-attention vector and the i-1-th sentence encoding vector to obtain a first summed vector, and normalizes the first summed vector to obtain an i-th normalized vector, wherein the i-1-th sentence encoding vector and the i-th normalized vector have the same dimension; Inputting the i-th normalized vector into a feedforward network module in the i-th encoder to obtain an i-th feedforward vector, wherein the i-1-th sentence encoding vector has the same dimension as the i-th feedforward vector; The i-th normalized vector and the i-th feedforward vector are input into the second sum and normalization of the i-th encoder. Module, enables the second addition and normalization module to add the i-th normalized vector and the i-th feedforward vector to obtain a second sum vector, and normalizes the second sum vector to obtain the i-th sentence encoding vector, wherein the i-1-th sentence encoding vector has the same dimension as the i-th sentence encoding vector.

5. The method according to any one of claims 2 to 4, characterized in that: The performing a first linear transformation on the Nth sentence encoding vector to obtain the first semantic representation vector includes: multiplying the Nth sentence encoding vector by a first weight matrix to obtain the first semantic representation vector, wherein the dimension of the Nth sentence encoding vector and the dimension of the first weight matrix are both M×M, and M is a positive integer greater than or equal to 2; The performing a second linear transformation on the Nth sentence encoding vector to obtain the perturbation weight vector includes: multiplying the Nth sentence encoding vector by a second weight matrix to obtain the perturbation weight vector, wherein the dimension of the Nth sentence encoding vector and the dimension of the second weight matrix are both M×M, and the first weight matrix and the second weight matrix are different weight matrices.

6. The method according to any one of claims 1 to 5, characterized in that: The perturbing the first semantic representation vector according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector includes: Determining a disturbance encoding vector according to the disturbance weight vector and the disturbance vector, wherein the disturbance encoding vector and the first semantic representation vector have the same dimension; The disturbance coding vector and the first semantic representation vector are added to obtain the second semantic representation vector.

7. The method according to claim 6, characterized in that The step of determining a disturbance coding vector according to the disturbance weight vector and the disturbance vector comprises: Multiplying the perturbation weight vector after performing the exponential operation with each row vector in the perturbation vector to obtain each row vector in the perturbation code vector; The adding the disturbance coding vector and the first semantic representation vector to obtain the second semantic representation vector includes: Each row vector in the disturbance coding vector is summed with the vector of the corresponding row in the first semantic representation vector to obtain the second semantic representation vector.

8. The method according to any one of claims 1 to 7, characterized in that: The decoding of the second semantic representation vector to obtain a second sentence includes: Passing the second semantic representation vector through P decoders of the same structure in sequence to obtain a P-th sentence decoding vector, where P is a positive integer greater than or equal to 1; Performing a third linear transformation on the P-th sentence decoding vector to obtain a second sentence representation vector; The second sentence is determined according to the second sentence characterization vector.

9. The method according to claim 8, characterized in that The step of sequentially passing the second semantic representation vector through P decoders having the same structure to obtain a P-th sentence decoding vector includes: Inputting the second semantic representation vector and a preset start symbol into the first decoder of the P decoders to obtain a first sentence decoding vector; When P is greater than or equal to 2, the j-th sentence decoding vector is obtained by the following steps, where j is a positive integer greater than or equal to 2 and less than or equal to P: The second semantic representation vector and the j-1th sentence decoding vector are input into the j-th decoder among the P decoders to obtain the j-th sentence decoding vector, wherein the j-1th sentence decoding vector is the sentence decoding vector output by the j-1th decoder among the P decoders.

10. The method according to claim 9, characterized in that The step of inputting the second semantic representation vector and the j-1th sentence decoding vector into the jth decoder of the P decoders to obtain the jth sentence decoding vector comprises: Inputting the j-1 th sentence decoding vector into the first self-attention module in the j th decoder to obtain a j th first self-attention vector, wherein the j-1 th sentence decoding vector has the same dimension as the j th self-attention vector; Inputting the j-th self-attention vector and the j-1-th sentence decoding vector into the third summing and normalization module in the j-th decoder to obtain a j-th third normalized vector, wherein the j-1-th sentence decoding vector and the j-th third normalized vector have the same dimension, and the third summing and normalization module is used to sum the j-th self-attention vector and the j-1-th sentence decoding vector to obtain a third summed vector, and normalize the third summed vector to obtain the j-th third normalized vector; Inputting the j-th third normalized vector and the second semantic representation vector into the second self-attention module in the j-th decoder to obtain a j-th second self-attention vector, wherein the j-th third normalized vector has the same dimension as the second semantic representation vector and the j-th second self-attention vector; Inputting the j-th second self-attention vector and the j-th third normalized vector into the fourth summing and normalizing module in the j-th decoder to obtain the j-th fourth normalized vector, wherein the j-th second self-attention vector and the j-th fourth normalized vector have the same dimension, and the fourth summing and normalizing module is used to sum the j-th second self-attention vector and the j-th third normalized vector to obtain a fourth summed vector, and normalizing the fourth summed vector to obtain the j-th fourth normalized vector; Input the jth fourth normalized vector into the feedforward network module in the jth decoder to obtain the jth sentence decoding vector, wherein the jth fourth normalized vector and the jth sentence decoding vector have the same dimension.

11. The method according to any one of claims 8 to 10, characterized in that: The performing a third linear transformation on the P-th sentence decoding vector to obtain a second sentence representation vector includes: multiplying the P-th sentence decoding vector having a dimension of M×M by a third weight matrix having a dimension of M×T to obtain the second sentence representation vector having a dimension of M×T, wherein T represents the number of words included in a preset dictionary, and T is a positive integer greater than or equal to 2; The determining of the second sentence according to the second sentence representation vector includes: normalizing the second sentence representation vector of dimension M×T to obtain a normalized vector of dimension M×T; when the value of the vector of dimension 1×T in the k-th row of the normalized vector of dimension M×T satisfies the condition of determining the corresponding word from the preset dictionary, and the value at the q-th position in the vector of dimension 1×T in the k-th row is the largest, determining the word at the q-th position in the preset dictionary as the word in the second sentence, wherein k is a positive integer greater than or equal to 1 and less than or equal to M, and q is a positive integer greater than or equal to 1 and less than or equal to T.

12. The method according to any one of claims 1 to 11, characterized in that The method further comprises: When the first sentence includes F words, word vectors of the F words in the first sentence are obtained to obtain F word vectors with a dimension of 1×M, where F is a positive integer greater than or equal to 2, and M is a positive integer greater than or equal to 2; When F is less than M, the F word vectors with dimensions of 1×M are concatenated with MF padded vectors with dimensions of 1×M to obtain a first sentence representation vector with dimensions of M×M, wherein the values ​​in the MF padded vectors with dimensions of 1×M are all 0.

13. The method according to any one of claims 1 to 12, characterized in that The method further comprises: The target sentence recognition model to be tested is tested using the first sentence and the second sentence to obtain a target test result, wherein the target test result is used to indicate whether the same recognition result is recognized for the first sentence and the second sentence.

14. The method according to claim 13, characterized in that The using the first sentence and the second sentence to test the target sentence recognition model to be tested to obtain a target test result includes: Inputting the first sentence into the target sentence recognition model to obtain a first emotion label, wherein the first emotion label is used to represent the emotion type expressed by the semantics of the first sentence; Inputting the second sentence into the target sentence recognition model to obtain a second emotion label, wherein the second emotion label is used to represent the emotion type expressed by the semantics of the second sentence; Comparing whether the first emotion label and the second emotion label are the same; In the case where the first emotion label and the second emotion label are the same, the target test result indicates that the first sentence and the second sentence are recognized to have the same recognition result; When the first emotion label and the second emotion label are different, the target test result indicates that different recognition results are obtained for the first sentence and the second sentence.

15. The method according to claim 13 or 14, characterized in that The method further comprises: When the target test result indicates that different recognition results are obtained for the first sentence and the second sentence, prompt information is generated, wherein the prompt information is used to prompt adjustment of the target sentence recognition model.

16. A sentence generating device, characterized in that: include: an encoding unit, configured to encode a first sentence representation vector of a first sentence to obtain a first semantic representation vector and a disturbance weight vector, wherein the first sentence representation vector is a vector determined according to word vectors of each word in the first sentence, the first semantic representation vector is used to represent the semantics of the first sentence, and the disturbance weight vector is used to control the disturbance of the first semantic representation vector by a predetermined disturbance vector; A first processing unit, configured to perturb the first semantic representation vector according to the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector; A decoding unit is used to decode the second semantic representation vector to obtain a second sentence, wherein the first sentence and the second sentence have the same or similar semantics.

17. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein the program can be executed by a terminal device or a computer to execute the method described in any one of claims 1 to 15.

18. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps of the method described in any one of claims 1 to 15 are implemented.

19. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to execute the method according to any one of claims 1 to 15 through the computer program.

Citation Information

Patent Citations

  • Document abstract generation method and device, storage medium and electronic equipment

    CN113204958A

  • Adversarial sample generation method, device and equipment and readable storage medium

    CN114331829A

  • Comparative sentence sentiment analysis method and system based on BERT adversarial training

    CN114911931A

  • Statement generation method and device, storage medium and electronic equipment

    CN116955550A

  • Sentence generation model generator, sentence generation model, and sentence generator

    WO2023079911A1