Sentence generation method, sentence generation device, computer program, and electronic device
By generating sentence-level malicious attack samples through encoder and decoder structures, this approach solves the problem of existing technologies being unable to generate sentence-level malicious attack samples, thereby improving the robustness and diversity of the model.
Patent Information
- Authority / Receiving Office
- JP · JP
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2024-05-14
- Publication Date
- 2026-03-10
AI Technical Summary
Existing technologies lack effective methods for generating sentence-level malicious attack samples, making it impossible to effectively assess the robustness of task processing models.
By using a generation device and method, and leveraging encoder and decoder structures, sentence-level malicious attack samples are generated. By adjusting the perturbation weight vector and perturbation vector to perturb the sentence representation, sentence-level malicious attack samples with the same or similar meanings are generated.
We have achieved sentence-level malicious attacks on the task processing model, which improves the model's robustness and diversity, and enhances its anti-interference ability.
Smart Images

Figure 2026508276000001_ABST
Abstract
Description
[Technical Field]
[0001] This application claims priority from a Chinese patent application filed on May 17, 2023, bearing application number 2023105602181 and entitled "Sentence generation method and device, storage medium and electronic device."
[0002] The present application relates to the technical field of computers, and more particularly to a sentence generation method and device, a storage medium, and an electronic device. [Background technology]
[0003] In natural language processing tasks, text adversarial attack methods are typically used to evaluate the robustness of task processing models. The basic implementation of text adversarial attack methods involves testing the robustness of a model and / or improving it by adding perturbations imperceptible to humans to text data samples, causing the model to make incorrect predictions.
[0004] Text adversarial attacks can be divided into three types: character-level, word-level, and sentence-level. In the process of realizing the three types of text attack methods, how to generate adversarial attack samples plays a crucial role.
[0005] Related art has proposed methods for generating character-level adversarial attack samples and word-level adversarial attack samples. The character-level adversarial text generation method calculates the importance of each character in a sentence, and then uses the descending order of character importance as the attack order. Perturbations are applied by replacing characters with similar phonetic or morphological characters, or by correcting characters. The word-level sample attack method calculates the importance of each word in a sentence, and then uses the descending order of word importance as the attack order. The adversarial attack sample is generated by replacing a word with a synonym or similar word. However, currently, text adversarial attack tasks are often performed character-level or word-level, and there are no effective solutions yet for how to generate sentence-level adversarial attack samples (adversarial attack text) or how to implement a sentence-level text attack method. Summary of the Invention [Problem to be solved by the invention]
[0006] The embodiments of the present application provide a sentence generation method and apparatus, a storage medium, and an electronic device. [Means for solving the problem]
[0007] According to an embodiment of the present application, there is provided a sentence generation method, the method including the steps of: obtaining a first meaning representation vector and a perturbation weight vector by encoding a first sentence representation vector of a first sentence, the first sentence representation vector being a vector determined based on character vectors of each character in the first sentence, the first meaning representation vector being for indicating a meaning of the first sentence, and the perturbation weight vector being for controlling perturbation of the first meaning representation vector by a predetermined perturbation vector; obtaining a second meaning representation vector by perturbing the first meaning representation vector based on the perturbation weight vector and the perturbation vector; and obtaining a second sentence by decoding the second meaning representation vector, wherein the first sentence and the second sentence have the same or similar meaning.
[0008] According to an embodiment of the present application, there is further provided a sentence generation device, including: an encoding unit that obtains a first meaning representation vector and a perturbation weight vector by encoding a first sentence representation vector of a first sentence, the first sentence representation vector being determined based on character vectors of each character in the first sentence, the first meaning representation vector being for indicating a meaning of the first sentence, and the perturbation weight vector being for controlling perturbation of the first meaning representation vector by a predetermined perturbation vector; a first processing unit that obtains a second meaning representation vector by perturbing the first meaning representation vector based on the perturbation weight vector and the perturbation vector; and a decoding unit that obtains a second sentence by decoding the second meaning representation vector, wherein the first sentence and the second sentence have the same or similar meaning.
[0009] According to an embodiment of the present application, there is further provided a computer-readable storage medium having a computer program stored therein, the computer program being configured to, when executed, cause execution of the above-described sentence generation method.
[0010] According to an embodiment of the present application, there is further provided a computer program product including computer programs / instructions, which, when executed by a processor, perform the steps of the above method.
[0011] According to an embodiment of the present application, there is further provided an electronic device including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to cause the computer program to execute the sentence generation method. [Effects of the Invention]
[0012] According to the above-described embodiment provided herein, a first semantic representation vector of a first sentence is perturbed based on a perturbation weight vector and a perturbation vector to obtain a second semantic representation vector. The second semantic representation vector is then decoded to obtain a second sentence having the same or similar meaning as the first sentence, and the second sentence is used as a sentence-level adversarial attack sample. The perturbation weight vector can be used to set the magnitude of the perturbation and further to control the semantic distance between the second semantic representation vector and the first semantic representation vector. The semantic distance between the generated sentence-level adversarial attack sample and the original input sample is controllable. This satisfies the fundamental purpose of generating adversarial attack samples, i.e., to obtain samples with similar corresponding meanings / intent by perturbing at the sentence level while approximating the meaning of the original sample. In this way, the effectiveness of the generated adversarial attack sample can be improved. Furthermore, by adjusting the perturbation weight vector, perturbation information with different perturbation degrees can be added to the original input sample, 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, the robustness of the task processing model is improved by evaluating it using a sentence-by-sentence text attack method.
[0013] The drawings described herein are intended to provide a further understanding of the present application and constitute a part of the present application. The schematic examples and the description thereof are intended to aid in the interpretation of the present application and are not to be construed as an undue limitation of the present application. [Brief explanation of the drawings]
[0014] [Figure 1] FIG. 1 is a schematic diagram of an application scenario of a sentence generation method according to an embodiment of the present application. [Figure 2] 1 is a flowchart of a sentence generation method according to an embodiment of the present application. [Figure 3] FIG. 10 is a schematic diagram of another application scenario of the sentence generation method according to an embodiment of the present application. [Figure 4]FIG. 10 is a schematic diagram of another application scenario of the sentence generation method according to an embodiment of the present application. [Figure 5] 1 is an overall schematic diagram of a sentence generation method according to an embodiment of the present application; [Figure 6] FIG. 2 is another overall schematic diagram of a sentence generation method according to an embodiment of the present application. [Figure 7] 1 is a specific example of a sentence generation method according to an embodiment of the present application. [Figure 8] FIG. 1 is a schematic diagram of an encoder configuration according to an embodiment of the present application; [Figure 9] FIG. 2 is a schematic diagram of a decoder configuration according to an embodiment of the present application; [Figure 10] 10 shows the result of normalizing the output of the decoder of the final layer according to an embodiment of the present application. [Figure 11] FIG. 2 is a schematic diagram of the process of implementing a beam search algorithm according to an embodiment of the present application. [Figure 12] 1 is a schematic diagram of a configuration of a sentence generation device according to an embodiment of the present application; [Figure 13] 1 is a schematic diagram illustrating a configuration of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION OF THE INVENTION
[0015] In order to allow those skilled in the art to understand the configuration of the present application more clearly and completely, the configuration of the embodiment of the present application will be described below with reference to the drawings of the embodiment of the present application.
[0016] Terms such as "first," "second," and the like in the present specification, claims, and drawings are used to distinguish between similar objects and are not necessarily used to describe a particular order or priority. It should be understood that terms so used are interchangeable where appropriate, such that the embodiments of the present invention described herein can be practiced in orders other than those illustrated or described herein. Furthermore, the terms "comprise," "have," and any variations thereof are intended to cover a non-exclusive inclusion. For example, a process, method, system, product, or apparatus comprising a series of steps or units is not necessarily limited to the explicitly listed steps or units, but may include other steps or units not explicitly listed or that are inherent to the process, method, product, or apparatus.
[0017] The implementation of the present embodiment complies with regulations, and the data used in the implementation of the present embodiment does not affect user privacy. This ensures that the operation complies with regulations and laws, and also guarantees data security.
[0018] Terminology Adversarial attack samples: Adversarial attack samples are generated by adding perturbations to the original samples, with the assumption that the semantics of the original samples is preserved as much as possible, and are used to attack the model and find its weaknesses. Adversarial attack samples can be used to find model weaknesses and further adjust the model to enhance its robustness. Adversarial attack samples are divided into three types of attack samples: character-based, word-based, and sentence-based. Adversarial attack samples usually have the same annotation labels as the original samples.
[0019] Character-level attack: Corresponding to the English alphabet or Chinese characters, at the character level, perturbations are added by replacing the characters of the original sample with homophones / homographs or performing character-level addition / deletion / modification on the original sample to generate attack samples and attack the model to find the weaknesses of the model.
[0020] Word-level attack: Corresponding to English words or Chinese phrases, at the word level, perturbations are added by replacing the words of the original sample with synonyms or performing word-level addition / deletion / modification on the original sample to generate attack samples and attack the model to find the weaknesses of the model.
[0021] Sentence-level attack: Corresponding to English sentences or Chinese sentences, at the sentence level, perturbations are performed to generate attack samples and attack the model to find the weaknesses of the model.
[0022] Text adversarial attack: By adding imperceptible perturbations to text data samples to cause mispredictions of the model, the robustness and defects of the model are explored. Text adversarial attacks can be divided into three types of attack methods: character-level, word-level, and sentence-level.
[0023] Model robustness: Robustness can be understood as the tolerance of the model to data changes. Assuming that a small deviation in the data or a small perturbation in the model only has a small impact on the output of the model and can still produce correct results, the model can be said to be robust.
[0024] Language material automatic annotation system: By preprocessing unlabeled language material data by an algorithm and automatically attaching highly reliable label results to the language material to generate training data for model training.
[0025] End-to-end: An automated flow from input to output, where the input is the original input and the output is the desired result. For example, if an original input is input to a model and the model processes it and then outputs the result, the overall process is an end-to-end method.
[0026] According to one aspect of the embodiment of the present application, a sentence generation method is provided. As an exemplary embodiment, the sentence generation method may be applied to, but is not limited to, an application scenario as shown in FIG. 1. In the application scenario as shown in FIG. 1, a terminal device 102 may communicate with a server 106 via a network 104. The server 106 may perform operations (e.g., data write operations and data read operations) on a database 108. The terminal device 102 may include, but is not limited to, a human-computer interaction screen, a processor, and a memory. The human-computer interaction screen may be used to display, for example, a first sentence and a second sentence on the terminal device 102. The processor may be used, but is not limited to, to perform a corresponding operation in response to the human-computer interaction operation, or to generate a corresponding instruction and send the generated instruction to the server 106. The memory may be used to store related processing data, for example, a first semantic expression vector, a second semantic expression vector, and a second sentence.
[0027] In one exemplary embodiment, the following steps of the sentence generation method may be executed on the server 106. In step S102, a first sentence representation vector of a first sentence is encoded to obtain a first semantic representation vector and a perturbation weight vector. The first sentence representation vector is correlated with the character vector of each character in the first sentence. For example, the first sentence representation vector is determined based on the character vector of each character in the first sentence. The first semantic representation vector is for indicating the meaning of the first sentence. The perturbation weight vector is for controlling perturbation of the first semantic representation vector by a predetermined perturbation vector. In step S104, a second semantic representation vector is obtained by perturbing the first semantic representation vector based on the perturbation weight vector and the perturbation vector. In step S106, the second semantic representation vector is decoded to obtain a second sentence, and the first sentence and the second sentence have the same or similar meaning. Whether the meanings of the first sentence and the second sentence are the same or similar may be determined based on the distance between the first semantic representation vector and the second semantic representation vector.
[0028] The second sentence obtained as above can be used as a sentence-level adversarial attack sample to evaluate the robustness of the task processing model, thereby improving the robustness of the task processing model.
[0029] 2 is a flowchart of a sentence generation method according to an embodiment of the present invention. The flow includes the following steps:
[0030] In step S202, a first meaning representation vector and a perturbation weight vector are obtained by encoding a first sentence representation vector of the first sentence. Here, the first sentence representation vector correlates with the character vector of each character in the first sentence. For example, the first sentence representation vector is determined based on the character vector of each character in the first sentence. The first meaning representation vector is for indicating the meaning of the first sentence. The perturbation weight vector is for controlling perturbation of the first meaning representation vector by a predetermined perturbation vector.
[0031] In step S204, the first meaning expression vector is perturbed based on the perturbation weight vector and the perturbation vector to obtain a second meaning expression vector.
[0032] In step S206, the second sentence is obtained by decoding the second semantic representation vector, and the first sentence and the second sentence have the same or similar meaning.
[0033] 3 is a schematic diagram of another application scenario of the sentence generation method according to the embodiment of the present application. As shown in FIG. 3, the original data sample is the Chinese phrase "Jeongchengsozijinshiweikai."
[0034] 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 FIG. 4, when performing adversarial training on a task processing model using a text adversarial attack method, the training results are artificially checked to determine the weaknesses of the task processing model, and the task processing model is fine-tuned to improve the robustness of the task processing model when encountering adversarial attack text.
[0035] In the related art, a method for generating character-based adversarial attack samples and a method for generating word-based adversarial attack samples have been proposed, but no effective solution has been proposed for generating sentence-based adversarial attack texts.
[0036] For this reason, in the embodiment of the present application, a method for generating sentence-based adversarial attack text based on an encoder-decoder structure is proposed.
[0037] Specifically, FIG. 5 is an overall schematic diagram of a 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 FIG. 5, a first sentence representation vector of a first sentence is input to an encoder to obtain a first semantic representation vector m having a dimension of 512×512 and a perturbation weight vector σ having a dimension of 512×512. Here, the perturbation weight vector σ is, for example, a vector randomly sampled based on a Gaussian distribution. σ is used to assign weights to perturbation information. Specifically,
number
[0038] In order to facilitate understanding of the configurations of various embodiments of the present application, all of the embodiments will be interpreted and explained assuming that the dimensions of the first semantic expression vector are 512×512.
[0039] For example, if the first sentence is "Jeongseongso zi jingshi wei kai" in Chinese, a first semantic representation vector of 512 (length of characters in the sentence) x 512 (dimension of the vector of each character) is obtained by encoding the first sentence representation vector of the first sentence. Here, each character in the Chinese "Jeongseongso zi jingshi wei kai" is converted into a vector with a dimension of 1 x 512, and the length of the characters in the sentence is defined as 512 bytes. If the number of characters in the sentence (character length) is less than 512 bytes, the number of characters is converted to 512 bytes by padding with zeros.
[0040] 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 with dimensions of 512 × 512. The second semantic representation vector c is decoded using a decoder to obtain a decoded text, that is, a second sentence.
[0041] Next, the method for generating adversarial attack text based on an encoder-decoder structure will be further described with reference to FIG. 6. FIG. 6 is another overall schematic diagram of a sentence generation method according to an embodiment of the present application. The text generated by this method is adversarial attack text. As shown in FIG. 6, in step S61, an original sample (first sentence) is input to the encoder, and a first hidden layer semantic representation of the original sample is obtained from the hidden layer.
[0042] In step S62, perturbation information that fits a data distribution (for example, a normal distribution) is sampled according to this data distribution and added to the first hidden layer semantic representation to obtain a second hidden layer semantic representation.
[0043] In step S63, the second hidden layer semantic representation is input to the decoder to obtain the decoded text (second sentence) after decoding.
[0044] In step S64, the decrypted text is determined as the generated adversarial attack sample.
[0045] According to the above steps S61 to S64, a plurality of adversarial attack samples (adversarial attack texts) in text format can be generated. The plurality of adversarial attack samples are used to train the model and fine-tune the model, thereby enhancing the robustness of the model.
[0046] FIG. 7 is a specific example of a sentence generation method according to an embodiment of the present application. As shown in FIG. 7, assume that the first sentence is "Jeongcheng sozu zi jingshi wei kai" in Chinese. According to steps S61 to S64 above, perturbation information is applied to the first sentence according to the perturbation weight parameters, and the result of the application is decoded to obtain the second sentence, "Xincheng zhe ling" in Chinese. The process of implementing encoding and decoding will be described below with reference to a specific embodiment.
[0047] According to the above embodiment provided in the present application, the first semantic expression vector of the first sentence is perturbed based on the perturbation weight vector and the perturbation vector to obtain a second semantic expression vector, and then the second semantic expression vector is decoded to obtain a second sentence having the same or similar meaning as the first sentence, and the second sentence is used as a sentence-level adversarial attack sample. This fills the gap in the sentence-level text adversarial attack method in the related art, solves the technical problem that the sentence-level text attack method in the related art cannot be used to evaluate the robustness of the task processing model, and achieves the technical effect of improving the robustness of the task processing model.
[0048] For example, the step of obtaining the first semantic representation vector and the perturbation weight vector by encoding the first sentence representation vector of the first sentence may include the following steps: Sequentially passing the first sentence representation vector through N encoders (N is a positive integer equal to or greater than 1) with the same structure to obtain an Nth sentence encoding vector; Obtaining a first semantic representation vector by performing a first linear transformation of the N-th sentence encoding vector; Obtaining a perturbation weight vector by performing a second linear transformation of the Nth sentence coding vector.
[0049] 8 is a schematic diagram of an encoder configuration according to an embodiment of the present application. As shown in FIG. 8, assuming that the number of layers of the encoder is N=6, a first sentence representation vector is sequentially passed through N encoders with the same structure. The same structure means, for example, that each of the N encoders includes a self-attention module, a feedforward module, a first additive normalization module, and a second additive normalization module. The parameters of the N encoders are different.
[0050] In the process of processing N encoders, the output of each encoder is input to the next encoder, and the dimensions of the output of each encoder are the same, for example, 512x512.
[0051] In one exemplary implementation, the step of obtaining the Nth sentence encoding vector by sequentially passing the first sentence representation vector through N encoders having the same structure includes: inputting the first sentence representation vector into a first encoder of the N encoders to obtain a first sentence encoding vector; and if N is 2 or greater, obtaining the i-th sentence coding vector by inputting the i-1th (i is a positive integer greater than or equal to 2 and less than or equal to N) sentence coding vector into the i-th encoder of the N encoders, where the i-1th sentence coding vector is the sentence coding vector output from the i-1th encoder of the N encoders.
[0052] When the number of encoders N is 6, the sentence coding vector with dimensions 512x512 output from the sixth encoder is input to the Linear1 (linearly changing network structure 1) layer, and a first linear transformation is performed on it to obtain the first semantic representation vector m. The sentence coding vector with dimensions 512x512 output from the sixth encoder is input to the Linear2 (linearly changing network structure 2) layer, and a second linear transformation is performed on it to obtain the perturbation weight vector σ.
[0053] The network structures of the Linear1 layer and the Linear2 layer are the same, but the structural parameters are different.
[0054] In one exemplary implementation, the step of obtaining the i-th sentence coding vector by inputting the i-1-th sentence coding vector to the i-th encoder of the N encoders includes the steps of: obtaining an i-th self-attention vector by inputting the i-1-th sentence coding vector into a self-attention module in the i-th encoder, where the i-1-th sentence coding vector and the i-th self-attention vector have the same dimension; inputting the i-th self-attention vector and the i-1-th sentence coding vector into a first summation normalization module in the i-th encoder, causing the first summation normalization module to perform summation processing on the i-th self-attention vector and the i-1-th sentence coding vector to obtain a first summation vector, and normalizing the first summation vector to obtain an i-th normalized vector, wherein the i-1-th sentence coding vector and the i-th normalized vector have the same dimension; obtaining an i-th feedforward vector by inputting the i-th normalized vector into a feedforward network module in the i-th encoder, where the i-1-th sentence encoding vector and the i-th feedforward vector have the same dimension; inputting the i-th normalized vector and the i-th feedforward vector into a second summation normalization module in the i-th encoder, causing the second summation normalization module to add the i-th normalized vector and the i-th feedforward vector to obtain a second summation vector, and normalizing the second summation vector to obtain the i-th sentence coding vector, wherein the (i-1)-th sentence coding vector and the i-th sentence coding vector have the same dimension.
[0055] Next, the processing steps of the encoder for each layer will be explained with reference to FIG. 8, taking the case of i=2 as an example.
[0056] In step S81, the first sentence encoding vector is input to the self-attention module in the second encoder to obtain the second self-attention vector.
[0057] In the embodiment of the present application, the encoder has a self-attention module that applies a self-attention mechanism to find the semantic relevance between each token in a sequence (sentence) and other tokens in the sequence. The self-attention mechanism calculates a representation vector for each position in an input sequence (e.g., a sentence), then calculates a weight distribution based on these representation vectors, and finally applies the weight distribution to each position in the input sequence to obtain a weighted representation vector. In this manner, the self-attention mechanism allows the model in the encoder to focus on any position in the input sequence without relying on previous calculation results when processing the current input. This allows the model in the encoder to better capture long-range dependencies in the input sequence.
[0058] The computational method of the self-attention mechanism is realized by the following Equation 1.
[0059]
number
[0060] Here, Q is the input vector representation of the original sample. For example, if the first sentence is "Jeongseongsozizi jingshi weikai" in Chinese, Q is its corresponding vector representation (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 numerical vector (Value), respectively.
[0061] The dimension of the first sentence representation vector is max_seq_length*hidden_size, where max_seq_length can be understood as the size of the hidden layer and may be, but is not limited to, the maximum character length of the original text (first sentence). Assuming the maximum character length of the first sentence is 512, the dimension of the second self-attention vector is 512 x 512.
[0062] In step S82, the second self-attention vector in step S81 and the first sentence encoding vector are input to a first summation normalization module in the second encoder to obtain a second normalized vector. The first summation normalization module may first add the second self-attention vector and the first sentence encoding vector to obtain a summation vector. By adding the second self-attention vector output from the self-attention mechanism to the first sentence encoding vector, the original information can be strengthened. The addition may be performed element by element (adding each element to the element at the same position).
[0063] Next, the sum vector is normalized. It can be normalized by Norm(Q+Attention(Q,K,V)), where Norm is the normalization function. After normalization, the dimension of the second normalized vector is still 512x512. Assume the normalized result is x1.
[0064] Here, the normalization process aims to ensure that all information before and after the self-attention mechanism processing is retained.
[0065] In step S83, the second normalized vector is input to a feedforward network module in the second encoder to obtain a second feedforward vector.
[0066] Here, the feedforward network module includes a feedforward network. The result output from the feedforward network is Feed_forward(x1), where Feed_forward is the calculation unit of the feedforward network. Using the feedforward network, a dimension expansion process is first performed on the first normalized vector, and then a dimension deletion process is performed. This is for the purpose of further performing information fusion on the first normalized vector output in step S82. That is, the information (vector representation) of each input unit is finally output as a vector representation through matrix operation and normalization by the feedforward network (i.e., all information input from the text is fused). The dimension of the second feedforward vector is still 512 x 512.
[0067] In step S84, the second feedforward vector and the second normalized vector are input into a second summation normalization module in the second encoder to obtain a second sentence encoding vector.
[0068] Specifically, the second sentence coding vector is obtained by performing a normalization process using Norm(x1+Feed_forward(x1)).
[0069] The above steps S81 to S84 are repeated six times, and after the sixth sentence encoding vector is output from the final layer encoder, the Linear1 layer and Linear2 layer shown in FIG. 8 are used to obtain the first semantic expression vector m and perturbation weight vector σ that are finally output from the encoder.
[0070] In one exemplary implementation, the step of obtaining a first semantic representation vector by performing a first linear transformation of the Nth sentence coding vector includes a step of obtaining the first semantic representation vector by multiplying the Nth sentence coding vector by a first weight matrix, where the dimensions of the Nth sentence coding vector and the first weight matrix are both M×M (M is a positive integer greater than or equal to 2).
[0071] The step of obtaining a perturbation weight vector by performing a second linear transformation of the Nth sentence coding vector includes the step of obtaining the perturbation weight vector by multiplying the Nth sentence coding vector by a second weight matrix, where the dimensions of the Nth sentence coding vector and 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 from the encoder reflects the weight of each position in the sentence.
[0072] For example, assuming N=6, the sixth sentence encoding vector output from the second additive normalization module in the sixth encoder is multiplied by the first weight matrix to obtain the first semantic representation vector m, and the sixth sentence encoding vector is multiplied by the second weight matrix to obtain the perturbation weight vector σ, where the dimensions of the first semantic representation vector m and the perturbation weight vector σ are the same, 512×512.
[0073] For example, the step of obtaining a second semantic expression vector by perturbing the first semantic expression vector based on the perturbation weight vector and the perturbation vector may include: determining a perturbed coding vector based on the perturbation weight vector and the perturbation vector, wherein the perturbed coding vector and the first semantic representation vector have the same dimension; and obtaining a second semantic representation vector by adding the perturbation encoding vector and the first semantic representation vector.
[0074] The step of determining a perturbation coding vector based on the perturbation weight vector and the perturbation vector includes: The method includes a step of multiplying each row vector of the perturbation vector by the exponentially operated perturbation weight vector to obtain each row vector of the perturbation encoding vector.
[0075] The step of obtaining a second semantic expression vector by adding the perturbation encoding vector and the first semantic expression vector includes: The method includes a step of obtaining a second meaning expression vector by adding each row vector of the perturbation encoding vector to a corresponding row vector of the first meaning expression vector.
[0076] Referring to the method of generating adversarial attack samples by adding perturbations imperceptible to humans to text data samples mentioned in the above embodiment, a second semantic representation vector is obtained by applying the perturbation vector to the first semantic representation vector.
[0077] In one embodiment, the step of obtaining a second semantic representation vector by perturbing the first semantic representation vector according to the perturbation weight vector and the perturbation vector includes: The step of obtaining the second semantic expression vector is performed according to the following formula 2:
[0078]
number
[0079] where:
number
number
number
number
number
[0080] where σ is for controlling the degree of perturbation of the original input information by the perturbation information.
number
[0081] According to the above method, the magnitude of the perturbation can be set by the perturbation weight vector, and the semantic distance between the second semantic representation vector and the first semantic representation vector can be controlled. The semantic distance between the generated sentence-level adversarial attack sample and the original input sample can be controlled. This achieves the fundamental purpose of generating adversarial attack samples: obtaining samples with similar meanings / intent by perturbing at the sentence level while approximating the meaning of the original sample. This improves the effectiveness of the generated adversarial attack samples. Furthermore, by adjusting the perturbation weight vector, perturbation information with different perturbation degrees can be added to the original input sample, increasing the diversity of the hidden layer representation of the original input sample and improving the diversity of the generated adversarial attack samples.
[0082] In one exemplary implementation, the step of obtaining the second sentence by decoding the second semantic representation vector includes: A step of sequentially passing the second semantic representation vector through P decoders (P is a positive integer equal to or greater than 1) having the same structure to obtain a P-th sentence decoded vector; obtaining a second sentence representation vector by performing a third linear transformation of the P-th sentence decoded vector; determining the second sentence based on the second sentence representation vector.
[0083] 9 is a schematic diagram of a decoder configuration according to an embodiment of the present application. As shown in FIG. 9, the decoder model includes P decoders connected in series. The P decoders have the same structure, for example, each of them includes a first self-attention module, a third summation normalization module, a second self-attention module, a fourth summation normalization module, and a feedforward network module. The P decoders have the same structure, but their parameters are different.
[0084] 9, the detailed structure of one of the decoders is taken as an example to explain an embodiment of the present application. The second meaning representation vector is passed through P decoders with the same structure in sequence to obtain the Pth sentence decoded vector, i.e., the sentence decoded vector output from the feedforward network of the Pth decoder.
[0085] Using the linear transformation structure in the Linear3 layer shown in FIG. 9, a third linear transformation is performed on the P-th sentence decoded vector to obtain a second sentence representation vector and further determine the second sentence.
[0086] For example, the step of obtaining the P-th sentence decoded vector by passing the second semantic representation vector through P decoders having the same structure in sequence may include: obtaining a first sentence-decoded vector by inputting the second semantic representation vector and a predetermined start symbol into a first decoder of the P decoders; and if P is 2 or greater, inputting the second semantic expression vector and the j-1th (j is a positive integer greater than or equal to 2 and less than or equal to P) sentence decoding vector into the jth decoder of the P decoders to obtain the j-1th sentence decoding vector, wherein the j-1th sentence decoding vector is the sentence decoding vector output from the j-1th decoder of the P decoders.
[0087] Assuming P=6, for the first decoder, a predetermined starting symbol is input to the first decoder and processed using the first self-attention module, the third summation normalization module, the second self-attention module, the fourth summation normalization module, and the feedforward network module in the first decoder to obtain the first sentence decoded vector.
[0088] Here, the second semantic representation vector is used as the input to the hidden layer of the first decoder, i.e., the input to the second self-attention module.
[0089] By inputting the second semantic representation vector and the first sentence decoded vector into the second decoder, the second sentence decoded vector is obtained, and by inputting the second semantic representation vector and the second sentence decoded vector into the third decoder, the third sentence decoded vector is obtained, and by analogy in this way, the sixth sentence decoded vector output from the sixth decoder is obtained.
[0090] In one exemplary implementation, the step of obtaining the j-th sentence-decoded vector by inputting the second semantic representation vector and the j−1-th sentence-decoded vector into the j-th decoder of the P decoders includes: obtaining a j-th first self-attention vector by inputting the j-1-th sentence decoded vector into a first self-attention module in the j-th decoder, wherein the j-1-th sentence decoded vector and the j-th self-attention vector have the same dimension; a step of inputting the jth self-attention vector and the j-1th sentence decoded vector into a third summation normalization module in the jth decoder to obtain a jth third normalized vector, wherein the j-1th sentence decoded vector and the jth third normalized vector have the same dimension, and the third summation normalization module adds the jth self-attention vector and the j-1th sentence decoded vector to obtain a third summation vector, and normalizes the third summation vector to obtain the jth third normalized vector; inputting the jth third normalized vector and the second semantic representation vector into a second self-attention module in the jth decoder to obtain a jth second self-attention vector, wherein the jth third normalized vector has the same dimension as the second semantic representation vector and the jth second self-attention vector; inputting the jth second self-attention vector and the jth third normalized vector into a fourth summing and normalizing module in the jth decoder to obtain a jth fourth normalized vector, wherein the jth second self-attention vector and the jth fourth normalized vector have the same dimension, and the fourth summing and normalizing module adds the jth second self-attention vector and the jth third normalized vector to obtain a fourth summing vector, and normalizes the fourth summing vector to obtain the jth fourth normalized vector; and obtaining a jth sentence-decoded vector by inputting the jth fourth normalized vector into a feedforward network module in the jth decoder, wherein the jth fourth normalized vector and the jth sentence-decoded vector have the same dimension.
[0091] Assuming that j=2, the implementation process of the above steps of obtaining the j-th sentence decoded vector will be described with reference to the decoder structure shown in FIG.
[0092] In step S91, the first sentence decoded vector is input to the first self-attention module in the second decoder to obtain the second first self-attention vector.
[0093] Here, the self-attention calculation method can be referred to the above Equation 1, but the explanation will be omitted here. The role of the first self-attention module is to understand the information input to the second decoder.
[0094] In step S92, the second first self-attention vector in step S91 and the first sentence decoded vector are input into the third summation normalization module in the second encoder to obtain the second third normalized vector.
[0095] For the normalization process, please refer to the description of step S82 in the encoder section above. Here, the dimension of the second third normalized vector after normalization is still 512×512.
[0096] Here, the normalization process aims to ensure that all information before and after the processing of the self-attention module is retained.
[0097] In step S93, the second third normalized vector and the second semantic representation vector are input into a second self-attention module in the second decoder to obtain a second self-attention vector with a dimension of 512×512.
[0098] In the self-attention processing process, the second semantic representation vector c output from the encoder is used as the values of Q and K in the second self-attention calculation, and the second third normalized vector output in step S92 is used as V, and the self-attention calculation is performed using the above formula 1 to obtain a second self-attention vector with dimensions of 512 x 512.
[0099] Here, the second self-attention module in the decoder is for performing information fusion between the second semantic representation vector after information perturbation and the second third normalized vector.
[0100] In step S94, the second self-attention vector and the second third normalized vector are input into a fourth summation normalization module in the second decoder to obtain a second fourth normalized vector with a dimension of 512x512.
[0101] Here, the normalization process can be performed by referring to the explanation of step S92, but the explanation will be omitted here.
[0102] In step S95, the second fourth normalized vector is input into the feedforward network module in the second decoder to obtain a second decoded sentence vector with a dimension of 512×512.
[0103] Here, the feedforward network in the second decoder is for performing deep calculation of the final fusion information, and the calculation formula can be seen from the explanation of step S83 above.
[0104] The above steps S91 to S95 are repeated P=6 times, and after the sixth sentence decoded vector is output from the decoder of the final layer, the second sentence finally output from the decoder is obtained by using the Linear3 layer and softmax shown in Figure 9.
[0105] For example, the step of obtaining a second sentence representation vector by performing a third linear transformation on the P-th sentence decoded vector includes the step of obtaining a second sentence representation vector with a dimension of M×T by multiplying the P-th sentence decoded vector with a dimension of M×M by a third weight matrix with a dimension of M×T, where T represents the number of characters included in the predetermined dictionary and is a positive integer equal to or greater than 2.
[0106] The step of determining the second sentence based on the second sentence representation vector includes the steps of: normalizing the second sentence representation vector having dimensions M×T to obtain a normalized vector having dimensions M×T; and determining the character at the qth position in the specified lexicon as the character in the second sentence if the value of the vector having dimensions 1×T in the kth row (k is a positive integer greater than or equal to 1 and less than or equal to M) in the normalized vector having dimensions M×T satisfies the condition for determining the corresponding character from the specified lexicon, and the value at the qth position (q is a positive integer greater than or equal to 1 and less than or equal to T) in the kth row of the vector having dimensions 1×T is maximum.
[0107] Assuming P=6 and M=512, when the second semantic representation vector is passed through six decoders with the same structure to obtain the sixth sentence decoded vector output from the feedforward network of the sixth decoder, the second sentence representation vector is obtained by using a linear transformation process with three linear layers. Specifically, the sixth sentence decoded vector, which has dimensions of 512x512, is multiplied by a third weight matrix, which has dimensions of 512x1000, to obtain the second sentence representation vector, which has dimensions of 512x1000. Here, 1000 represents the number of characters included in the specified dictionary.
[0108] Since all 1000 characters in the dictionary participate in the calculation, the second sentence representation vector with dimensions of 512x1000 is softmaxed (normalized) to obtain a normalized vector with dimensions of 512x1000, as shown in Figure 10. Specifically, a softmax classifier is used to predict the character with a probability, and then predict the next character to be generated. Here, the calculation process of the softmax classifier may be realized by the following Equation 3.
[0109]
number
[0110] where W is the model parameter, T is the size of the lexicon (e.g., 1000 characters), and t is the order of each character in the lexicon.
number
[0111] In other words, by scanning the 1000 characters in the dictionary and using Equation 3 above, the predicted probability of each of the 1000 characters is calculated, and the top N characters with the highest predicted probabilities are selected as candidate results.
[0112] For the value of the vector with dimension 1×T in the k-th row of the normalized vector with dimension M×T to satisfy the condition of determining the corresponding character from a predetermined dictionary, it includes that at least some of the T values in this row are greater than 0 and less than 1, or, for example, greater than a predetermined value such as 0.6, 0.7, or 0.8, but is not limited thereto.
[0113] For each row vector with dimension 1×1000, what is output by softmax is the predicted probability of each character, and the sum of the probabilities of each character is 1. Among the 1000 probability values of each row, the character with the maximum value is determined as the character in the second sentence.
[0114] For example, for the first row in the normalized vector, if the value at the second position is the maximum, it means that the second character in the dictionary, the Chinese character "心", is the character in the second sentence. For the second row in the normalized vector, if the value at the third position is the maximum, it means that the third character in the dictionary, the Chinese character "誠", is the character in the second sentence. For the third row in the normalized vector, if the value at the fifth position is the maximum, it means that the fifth character in the dictionary, the Chinese character "則", is also the character in the second sentence. By analogy in this way, multiple characters included in the second sentence can be determined.
[0115] Next, taking the case where the first sentence, which is the original input of the encoder, is the Chinese sentence "精誠所至金石為開" and the parameters of the beam search in the candidate generation result are beam_number = 1 and P = 6 as an example, the realization process of the decoder will be described.
[0116] In step S1002, after inputting the first sentence representation vector of the first sentence into the encoder shown in FIG. 8 to obtain the second semantic representation vector with perturbation information added, the second semantic representation vector is input into the self-attention mechanism of the decoder (in all subsequent steps, the second semantic representation vector is input).
[0117] In step S1004, a predetermined start symbol [CLS] is input to the decoder. The decoder generates different candidate characters for each position of the characters in the generated second sentence, and the different candidate characters correspond to different search paths. The decoder outputs the Chinese character '心' (heart) which is the result with the maximum probability (see Figure 10). The maximum probability refers to the search path with the maximum cumulative probability.
[0118] In step S1006, the input of step S1004 (predetermined start character [CLS]) and the Chinese output character '心' (heart) are combined and input to the decoder, and the decoder outputs the Chinese character '誠' (sincerity) which is the result with the maximum probability.
[0119] In step S1008, by combining the input character and the output character of step S1006, '[CLS]心誠' is obtained as the input to the decoder, and the decoder outputs the Chinese character '則' (rule) which is the result with the maximum probability.
[0120] In step S1010, by combining the input character and the output character of step S1008, '[CLS]心誠則' is obtained as the input to the decoder, and the decoder outputs the Chinese character '霊' (spiritual) which is the result with the maximum probability.
[0121] In step S1012, by combining the input character and the output character of step S1010, '[CLS]心誠則霊' is obtained as the input to the decoder, and the decoder outputs the character end symbol '[EOS]' which is the result with the maximum probability.
[0122] When the end symbol is output from the decoder, the generation process of the second sentence ends.
[0123] In step S1014, post-processing is performed.
[0124] After removing the start symbol [CLS] and the end symbol [EOS], the finally generated adversarial attack text is the Chinese '心誠則霊' (sincerity leads to spirituality).
[0125] The above is an exemplary process of generating the Chinese sentence "Sincerity can move even the hardest hearts" which is a candidate attack sample at the sentence level through an encoder-decoder from the Chinese sentence "Genuine efforts can overcome any difficulty" in the first sentence.
[0126] Next, the process of sorting and selecting adversarial attack candidate texts will be described.
[0127] (1) Generating candidate adversarial attack texts based on the beam search algorithm Taking Figure 11 as an example, when the second semantic representation vector of the Chinese sentence "Genuine efforts can overcome any difficulty" is input into the decoder, the decoder may output results such as the Chinese sentences "With all sincerity", "Sincerity can move the spirits", and "The mind should be sincere". The cumulative probabilities corresponding to different generation results are different.
[0128] In the process of the decoder generating candidate results character by character, for each character position in the generated second sentence, different candidate characters are generated, and then the first k candidate characters among them are taken. Different candidate characters correspond to different search paths (abbreviated as paths). The first k candidate characters are the first k candidate characters with the maximum cumulative probability among the candidate characters at each position. Different paths have different generation results. In this way, multiple candidate results can be obtained. According to the greedy method (i.e., beam_number = 1), every time the path with the maximum probability is obtained, it is not necessarily possible to obtain the generation result with the maximum cumulative probability. Therefore, in the embodiments of the present application, the beam search algorithm is used. In the beam search algorithm, in the process of scanning paths and generating characters one by one, the first beam_number (the number of candidates with the maximum probability at each node) candidate characters with the maximum probability are taken to execute the next generation flow of the decoder.
[0129] As shown in FIG. 11, at the position of the first word of the second sentence to be generated, after inputting the start symbol [CLS], two candidate paths of Chinese "cheng" (probability 0.4) and Chinese "xin" (probability 0.2) are taken, and the next generation operation of the decoder is performed. Subsequently, in the path of Chinese "cheng", two paths of Chinese "chengxin" and "chengshi" with the highest probability are taken to continue the generation operation. In the path of Chinese "xin", the generation operation is continued along two paths of Chinese "xincheng" and "xinyi" with the highest probability. By analogy in this way, a plurality of candidate adversarial attack texts such as Chinese "xin cheng ze ling", "cheng xin cheng yi", and "xin yi yao zhen cheng" can be generated.
[0130] (2) Selecting candidate generation results based on strategies and rules For different tasks, it is necessary to further select the candidate adversarial attack texts generated based on the above-mentioned beam search algorithm. In the embodiments of the present application, a selection method based on rules is used. Specifically, it is as follows.
[0131] Taking the text adversarial attack based on the named entity recognition task in the field of natural language processing as an example. As follows, using regular expressions, candidate adversarial attack samples that meet the conditions are filtered, and samples that attack the target entity (belonging to noise samples) are removed.
[0132] For candidate adversarial attack sample x in all adversarial attack samples For i in range(n): If the candidate adversarial attack sample does not match the regular expression ^(?!.*\bENTITYi\b).+$ Continue (continue repeating) Else Remove candidate adversarial attack sample x (remove this candidate adversarial attack sample) Here, n is the number of correctly named entities, and ENTITYi is the corresponding i-th correctly recognized named entity. A named entity is an entity with a specific meaning in a text, such as a proper noun, a person's name, or a place name. The named entity recognition task aims to recognize and classify entities in a text. If a candidate adversarial attack sample does not match the regular expression ^(?!.*\bENTITYi\b).+$, it can be understood that ENTITYi is a substring of the candidate adversarial attack sample.
[0133] For example, if the original text is the Chinese "Wanhuili Huanghe Da He Chorus Qi Ze Lei Hong", the correct entity recognition result of the named entity recognition is (song, "Yellow River Da He Chorus"), and the number of correctly named entities is n = 1. There are four candidate attack samples generated based on the methods of various embodiments of the present application.
[0134] Candidate 1: Chinese "Wanhui Li Hao Yan Sang Qi Zheng Hui Hong" Candidate 2: Chinese: "The evening meeting in the Yellow River leads to a great chorus of enthusiasm and enthusiasm" Candidate 3: Chinese: "Late evening performances in the Yellow River chorus truly good and sensible" Candidate 4: Chinese: "Tonight's performance of the Yellow River chorus is truly good and has a sense of humor." According to the filtering algorithm, candidates 1 and 4 match the exact entity result "Yellow River Chorus," but the entity to be retained is attacked (the entity "Yellow River Chorus" is changed by the attack), and they belong to the generated noise samples, so they need to be removed. The entity to be retained is a substring in the original text that should not be changed. Candidates 2 and 3 meet the task requirements (they attack the named entity recognition task, but do not attack the named entity detected by the target), so they are retained as the final selected sentence-level text attack generated samples. Through the above filtering process, samples that do not meet the requirements are filtered out from the candidate adversarial attack samples.
[0135] Using the candidate adversarial attack samples determined based on the above-mentioned beam search algorithm, multiple words are selected at each step to generate multiple candidate adversarial attack samples, and the generation result with the maximum cumulative probability over different paths is selected as the candidate adversarial attack sample. This improves the quality stability of the adversarial attack samples and improves the robustness of the model when adjusting the task processing model using the adversarial attack samples. By selecting the candidate adversarial attack samples, the effectiveness and quality of the finally obtained adversarial attack samples can be improved.
[0136] In addition, to ensure the quality of the adversarial attack text generated by the encoder-decoder based model, it is usually necessary to train the encoder-decoder based model. The training process (testing process) includes, but is not limited to, the following three stages:
[0137] (1) Pre-training the Encoder-Decoder Model The parameters of the encoder-decoder are pre-trained using a large amount of unlabeled language data. Because the language model of the encoder-decoder needs to be a general-purpose language model, the pre-training process can be completed using conventional pre-training methods.
[0138] (2) Data collection i) Collecting Data Source 1: We artificially annotate pairs of original data and adversarial attack samples (e.g., the Chinese phrase "Jessing sincerely reaches gold stones" and the Chinese phrase "Xincheng Zhe Ling" are a pair of samples).
[0139] ii) Data Source 2 Collection: The results of existing character-level / word-level attacks are applied as annotated sample pairs. For example, with the current character-level adversarial attack algorithm, attacking the Chinese text "Read the History of the Great Ming Dynasty" will generate the Chinese text "Read the History of the Ming Dynasty." That is, the sample pairs before and after the attack are considered as training data sources.
[0140] (3) Fine-tuning the model Based on the pre-training of the model in step (1), the final model parameters are obtained by fine-tuning the model using the data in step (2).
[0141] In one example, the method comprises: If the first sentence contains F characters (F is a positive integer of 2 or more), obtain character vectors of the F characters in the first sentence, and obtain F character vectors with dimensions 1×M (M is a positive integer of 2 or more); if F is smaller than M, combining F character vectors of dimension 1×M with MF filler vectors of dimension 1×M to obtain a first sentence representation vector of dimension M×M, wherein all of the values in the MF filler vectors of dimension 1×M are 0; If F is equal to M, the method further includes: combining the F character vectors with dimensions 1×M to obtain a first sentence representation vector with dimensions M×M.
[0142] For example, assuming M = 512 (M represents the maximum input length of each character), each character in the first sentence is converted into a 1x512-dimensional character vector. If the first sentence is the Chinese phrase "Jeongcheng sojourns to jade stones and opens the way," then F = 8, and F is smaller than M. By converting each of these eight characters into a 1x512-dimensional character vector, eight 1x512-dimensional character vectors are obtained. Because there are fewer than 512 1x512-dimensional character vectors, the remaining MF(512 - 8) 1x512-dimensional vectors may be padded with zeros to obtain 504 padded vectors each having a dimension of 1x512.
[0143] As an example, the method in each of the above embodiments includes: The method further includes a step of obtaining a target test result by testing the target sentence recognition model to be tested using the first sentence and the second sentence, the target test result being for indicating whether the same recognition result is recognized for the first sentence and the second sentence.
[0144] In a specific application scenario, assuming that the task processing model is a model for sentence recognition, the realization process of the step of testing the target sentence recognition model using the above first sentence and second sentence is as follows: obtaining a first emotion label by inputting the first sentence into a target sentence recognition model, the first emotion label being for indicating an emotion type expressed by the meaning of the first sentence; obtaining a second emotion label by inputting the second sentence into a target sentence recognition model, the second emotion label being for indicating an emotion type expressed in the meaning of the second sentence; comparing whether the first emotion label and the second emotion label are the same; If the first emotion label and the second emotion label are the same, indicating in the target test result that the same recognition result has been recognized for the first sentence and the second sentence; If the first emotion label and the second emotion label are different, indicating in the target test result that different recognition results have been recognized for the first sentence and the second sentence.
[0145] For example, assuming that the pre-set emotion labels are joy, sadness, and anger, the first emotion label is obtained by inputting the first sentence into the target sentence recognition model, and the second emotion label is obtained by inputting the second sentence (the adversarial attack text) into the target sentence recognition model.
[0146] If the first emotion label and the second emotion label are the same (for example, both are joy), it indicates that even if there is a small deviation in the input sample or a small perturbation inside the target sentence recognition model, the output of the target sentence recognition model is still accurate, which means that the impact of the adversarial attack text on the target sentence recognition model is small, i.e., the target sentence recognition model is robust.
[0147] Conversely, if the first and second emotion labels are different, The method includes a step of generating presentation information when the target test result indicates that different recognition results have been recognized for the first sentence and the second sentence, the presentation information being for presentation to adjust the target sentence recognition model.
[0148] By tuning the target sentence recognition model (which can also be understood as adversarial training), we can fix bugs in the target sentence recognition model and quickly increase the robustness of the target sentence recognition model.
[0149] In addition, in the process of using the above sentence generation method to generate adversarial attack samples and perform adversarial training of task processing models, the beneficial effects achieved include at least one of the following:
[0150] 1. In the present application, a sentence-based adversarial sample generation configuration is provided for the adversarial attack method, i.e., in addition to character-based / word-based, a sentence-based adversarial attack sample configuration is provided, filling the gap in the sentence-based text attack method.
[0151] 2. This application proposes an effective end-to-end configuration for generating effective adversarial samples. Compared with conventional text adversarial sample generation methods that add, delete, or modify text at the character or word level based on rules and strategies, this application provides a new concept and implementation method for adversarial attacks by generating adversarial attack samples using an end-to-end automated method based on an encoder-decoder model. The perturbation weight vector sets the magnitude of the perturbation and further controls the semantic distance between the second semantic representation vector and the first semantic representation vector. The semantic distance between the generated sentence-level adversarial attack sample and the original input sample is controllable. This satisfies the fundamental purpose of generating adversarial attack samples, i.e., to obtain samples with similar corresponding meanings / intent by perturbing at the sentence level while approximating the meaning of the original sample. In this way, the effectiveness of the generated adversarial attack samples can be improved. Furthermore, by adjusting the perturbation weight vector, perturbation information with different perturbation degrees can be added to the original input sample, increasing the diversity of the hidden layer representation of the original input sample and improving the diversity of the generated adversarial attack samples.
[0152] 3. Applied to text classification task: The method is applied to text classification task, and the weaknesses of the model and the corresponding samples can be effectively found, the robustness of the model can be quickly increased, and the bugs of the model can be fixed.
[0153] 4. Applied to the named entity recognition task: The method is applied to the named entity recognition task, and by using strategies and rules to select candidate generation results, it is possible to effectively select samples that the model cannot predict accurately and effectively repair the model.
[0154] For ease of explanation, each of the above-described method embodiments is expressed as a combination of a series of operations, but those skilled in the art should know that the present application is not limited by the order of the operations described, since some steps can be performed in other orders or simultaneously. Next, those skilled in the art should also know that all of the embodiments described in the specification belong to preferred embodiments, and that such operations and modules are not necessarily required for the present application.
[0155] According to another aspect of the embodiment of the present application, there is further provided a sentence generation apparatus as shown in Fig. 12. The apparatus includes an encoding unit 1202, a first processing unit 1204, and a decoding unit 1206.
[0156] The encoding unit 1202 encodes 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 determined based on the character vector of each character in the first sentence. The first semantic representation vector is for indicating the meaning of the first sentence. The perturbation weight vector is for controlling perturbation of the first semantic representation vector by a predetermined perturbation vector.
[0157] The first processing unit 1204 obtains a second semantic expression vector by perturbing the first semantic expression vector according to the perturbation weight vector and the perturbation vector.
[0158] The decoding unit 1206 obtains the second sentence by decoding the second semantic representation vector, where the first sentence and the second sentence have the same or similar meaning.
[0159] The encoding unit 1202 includes: a first processing module that sequentially passes the first sentence representation vector through N encoders (N is a positive integer equal to or greater than 1) that have the same structure, thereby obtaining an N-th sentence encoding vector; a second processing module for performing a first linear transformation of the N-th sentence encoding vector to obtain a first semantic representation vector; and a third processing module for obtaining a perturbation weight vector by performing a second linear transformation of the Nth sentence coding vector.
[0160] The first processing module is inputting the first sentence representation vector into a first encoder of the N encoders to obtain a first sentence encoding vector; and a first processing sub-module that executes the step of: when N is 2 or more, inputting the i-1th (i is a positive integer greater than or equal to 2 and less than or equal to N) sentence coding vector into the i-th encoder of the N encoders to obtain the i-1th sentence coding vector, where the i-1th sentence coding vector is the sentence coding vector output from the i-1th encoder of the N encoders.
[0161] The first processing module is a second processing sub-module for obtaining an i-th self-attention vector by inputting the (i-1)-th sentence encoding vector into a self-attention module in the i-th encoder, where the (i-1)-th sentence encoding vector and the i-th self-attention vector have the same dimension; a third processing sub-module that inputs the i-th self-attention vector and the i-1-th sentence coding vector into a first summation normalization module in the i-th encoder, causes the first summation normalization module to perform summation processing on the i-th self-attention vector and the i-1-th sentence coding vector to obtain a first summation vector, and normalizes the first summation vector to obtain an i-th normalized vector, wherein the i-1-th sentence coding vector and the i-th normalized vector have the same dimension; a fourth processing sub-module for inputting the i-th normalized vector into a feedforward network module in the i-th encoder to obtain the i-th feedforward vector, wherein the i-1-th sentence encoding vector and the i-th feedforward vector have the same dimension; and a fifth processing sub-module that inputs the i-th normalized vector and the i-th feedforward vector to a second addition normalization module in the i-th encoder, causes the second addition normalization module to add the i-th normalized vector and the i-th feedforward vector to obtain a second addition vector, and normalizes the second addition vector to obtain the i-th sentence encoding vector, wherein the (i-1)-th sentence encoding vector and the i-th sentence encoding vector have the same dimension.
[0162] The second processing module is The present invention includes a first multiplication submodule that obtains a first semantic expression vector by multiplying an Nth sentence coding vector by a first weight matrix, where the dimensions of the Nth sentence coding vector and the first weight matrix are both M×M (M is a positive integer greater than or equal to 2).
[0163] The third processing module is a second multiplication sub-module for obtaining a perturbation weight vector by multiplying the Nth sentence coding vector by a second weight matrix, where the dimensions of the Nth sentence coding vector and the second weight matrix are both M×M, and the first weight matrix and the second weight matrix are different weight matrices;
[0164] The first processing unit 1204 a fourth processing module that determines a perturbed coding vector based on the perturbation weight vector and the perturbation vector, where the perturbed coding vector and the first semantic representation vector have the same dimension; and a fifth processing module that performs an addition process on the perturbation encoding vector and the first meaning expression vector to obtain a second meaning expression vector.
[0165] The fifth processing module is The sixth processing submodule obtains each row vector of the perturbation encoding vector by multiplying the perturbation weight vector on which the exponential operation has been performed by each row vector of the perturbation vector, and obtains a second semantic expression vector by adding each row vector of the perturbation encoding vector to a corresponding row vector in the first semantic expression vector.
[0166] The decoding unit 1206 a sixth processing module for sequentially passing the second semantic representation vector through P decoders (P is a positive integer equal to or greater than 1) having the same structure to obtain a P-th sentence decoded vector; a seventh processing module for obtaining a second sentence representation vector by performing a third linear transformation on the P-th sentence decoded vector; and an eighth processing module that determines the second sentence based on the second sentence representation vector.
[0167] The sixth processing module is obtaining a first sentence-decoded vector by inputting the second semantic representation vector and a predetermined start symbol into a first decoder of the P decoders; and a seventh processing sub-module that executes the step of: when P is 2 or greater, inputting the second semantic representation vector and the j-1th (j is a positive integer greater than or equal to 2 and less than or equal to P) sentence decoded vector into the jth decoder of the P decoders to obtain the j-1th sentence decoded vector, wherein the j-1th sentence decoded vector is the sentence decoded vector output from the j-1th decoder of the P decoders.
[0168] The sixth processing module is an eighth processing sub-module for obtaining a jth first self-attention vector by inputting the j-1th sentence decoded vector into a first self-attention module in the jth decoder, wherein the j-1th sentence decoded vector and the jth self-attention vector have the same dimension; a ninth processing sub-module that inputs the jth self-attention vector and the j-1th sentence decoded vector into a third summation normalization module in the jth decoder to obtain the jth third normalized vector, where the j-1th sentence decoded vector and the jth third normalized vector have the same dimension, and the third summation normalization module adds the jth self-attention vector and the j-1th sentence decoded vector to obtain the third summation vector, and normalizes the third summation vector to obtain the jth third normalized vector; a tenth processing sub-module for inputting the jth third normalized vector and the second semantic representation vector into a second self-attention module in a jth decoder to obtain a jth second self-attention vector, wherein the jth third normalized vector has the same dimension as the second semantic representation vector and the jth second self-attention vector; an eleventh processing sub-module for inputting the jth second self-attention vector and the jth third normalized vector into a fourth summing and normalizing module in the jth decoder to obtain the jth fourth normalized vector, where the jth second self-attention vector and the jth fourth normalized vector have the same dimension, and the fourth summing and normalizing module is for summing the jth second self-attention vector and the jth third normalized vector to obtain the fourth summed vector, and normalizing the fourth summed vector to obtain the jth fourth normalized vector; The method further includes a twelfth processing sub-module that obtains a jth sentence-decoded vector by inputting the jth fourth normalized vector into a feedforward network module in the jth decoder, where the jth fourth normalized vector and the jth sentence-decoded vector have the same dimension.
[0169] The seventh processing module is The method includes a 13th processing sub-module that obtains a second sentence representation vector having a dimension of M×T by multiplying a Pth sentence decoding vector having a dimension of M×M by a third weight matrix having a dimension of M×T, where T represents the number of characters included in a predetermined dictionary and is a positive integer greater than or equal to 2.
[0170] The eighth processing module is The system includes a fourteenth processing submodule that normalizes the second sentence representation vector having dimensions M×T to obtain a normalized vector having dimensions M×T, and determines the character at the q-th position in the specified lexicon as the character in the second sentence if the value of the vector having dimensions 1×T in the k-th row (k is a positive integer greater than or equal to 1 and less than or equal to M) in the normalized vector having dimensions M×T satisfies a condition for determining a corresponding character from a specified lexicon and the value at the q-th position (q is a positive integer greater than or equal to 1 and less than or equal to T) in the vector having dimensions 1×T in the k-th row is maximum.
[0171] The device comprises: an acquisition unit for acquiring character vectors of the F characters in the first sentence when the first sentence contains F characters (F is a positive integer of 2 or more); and acquiring F character vectors with a dimension of 1×M (M is a positive integer of 2 or more); and a combining unit that, when F is smaller than M, combines F character vectors of dimension 1×M with MF filling vectors of dimension 1×M to obtain a first sentence expression vector of dimension M×M, wherein the values of the MF filling vectors of dimension 1×M are all 0.
[0172] The device comprises: The test unit further includes a test unit that obtains a target test result by testing the target sentence recognition model to be tested using the first sentence and the second sentence, the target test result being for indicating whether the same recognition result is recognized for the first sentence and the second sentence.
[0173] The above test unit is a first input module for inputting a first sentence into a target sentence recognition model to obtain a first emotion label, the first emotion label being for indicating an emotion type expressed in the meaning of the first sentence; a second input module for inputting the second sentence into a target sentence recognition model to obtain a second emotion label, the second emotion label being for indicating an emotion type expressed in the meaning of the second sentence; and and a comparison module for comparing whether the first emotion label and the second emotion label are the same.
[0174] If the first emotion label and the second emotion label are the same, the target test result indicates that the same recognition result was recognized for the first sentence and the second sentence. If the first emotion label and the second emotion label are different, the target test result indicates that different recognition results were recognized for the first sentence and the second sentence.
[0175] The device comprises: The system further includes a second processing unit that generates presentation information when the target test result indicates that different recognition results have been recognized for the first sentence and the second sentence, the presentation information being for presentation to adjust the target sentence recognition model.
[0176] The above device is applied to perturbing the first semantic representation vector of the first sentence based on the perturbation weight vector and the perturbation vector to obtain a second semantic representation vector, and then decoding the second semantic representation vector to obtain a second sentence having the same or similar meaning as the first sentence, and using the second sentence as a sentence-level adversarial attack sample, thereby filling the gap in the sentence-level text adversarial attack method in the related art, solving the technical problem that the sentence-level text attack method in the related art 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.
[0177] For an embodiment of the sentence generation device here, reference may be made to the embodiment of the sentence generation method described above, but a description thereof will be omitted here.
[0178] According to another aspect of the present application, there is further provided an electronic device for implementing the above-described sentence generation method. The electronic device may be the terminal device shown in FIG. 13. In various embodiments of the present application, a case where the electronic device is a back-end device will be described as an example. As shown in FIG. 13, the electronic device includes a memory 1302 and a processor 1304. A computer program is stored in the memory 1302, and the processor 1304 is configured to execute the steps of any one of the above-described method embodiments in accordance with the computer program.
[0179] In various embodiments of the present application, the electronic device may be located on at least one network device of a plurality of network devices of a computer network.
[0180] In various embodiments of the present application, the processor is configured to: Step S1 of obtaining a first semantic expression vector and a perturbation weight vector by encoding a first sentence expression vector of a first sentence, the first sentence expression vector being a vector determined based on the character vector of each character in the first sentence, the first semantic expression vector being for indicating the meaning of the first sentence, and the perturbation weight vector being for controlling perturbation to the first semantic expression vector by a predetermined perturbation vector; Step S2 of obtaining a second semantic representation vector by perturbing the first semantic representation vector according to the perturbation weight vector and the perturbation vector; The method may be configured to perform a step S3 of obtaining a second sentence by decoding the second semantic representation vector, where the first sentence and the second sentence have the same or similar meaning.
[0181] As will be understood by those skilled in the art, the configuration shown in FIG. 13 is merely schematic, and the electronic device may be a target terminal such as a smartphone (e.g., an Android smartphone, an iOS smartphone, etc.), a tablet computer, a palmtop computer, a mobile internet device (MID), a PAD, etc. FIG. 13 does not limit the configuration of the electronic device. For example, the electronic device may include more or fewer components (e.g., a network interface, etc.) than those shown in FIG. 13, or may be arranged differently from those shown in FIG. 13.
[0182] The memory 1302 can be used to store software programs and modules, such as program instructions / modules corresponding to the sentence generation method and apparatus of the present invention. The processor 1304 executes the software programs and modules stored in the memory 1302 to perform various functional applications and data processing, i.e., to implement the sentence generation method. The memory 1302 may include high-speed random access memory or non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 1302 may further include memory located remotely from the processor 1304, which may be connected to the terminal via a network. Examples of such networks include, but are not limited to, the Internet, a corporate intranet, a local area network, a mobile communication network, and combinations thereof. The memory 1302 may be used to store, for example, a first sentence, a first semantic expression vector, and a perturbation weight vector. 13, the memory 1302 may include, but is not limited to, the encoding unit 1202, the first processing unit 1204, and the decoding unit 1206 in the sentence generation device. Also, the memory 1302 may further include, but is not limited to, other module units in the sentence generation device, and the description thereof will be omitted in this example.
[0183] The transmission device 1306 transmits and receives data over a network. Specific examples of the network may include a wired network and a wireless network. In one example, the transmission device 1306 includes a network interface controller (NIC) that can be connected to other network devices and routers via a network cable, thereby communicating with the Internet or a local area network. In one example, the transmission device 1306 is a radio frequency (RF) module that communicates with the Internet wirelessly.
[0184] The electronic device further includes a display 1308 that displays direction presentation information of the target sound, and a connection bus 1310 that connects the respective module components in the electronic device.
[0185] In another embodiment, the target terminal or server may be a node of a distributed system. Here, the distributed system may be a blockchain system. The blockchain system may be a distributed system in which the plurality of nodes are connected in the form of network communication. Here, the nodes may form a peer-to-peer (P2P) network, and any type of computing device, such as a server, terminal, or other electronic device, may become a node of the blockchain system by joining the peer-to-peer network.
[0186] According to another aspect of the present application, there is further provided a computer program product or computer program including computer instructions stored in a computer-readable storage medium. A processor of a computing device reads the computer instructions from the computer-readable storage medium, and when the processor executes the computer instructions, causes the computing device to perform the sentence generation method provided in various exemplary implementations of the above-described server check process and other aspects. Here, when the computer program is executed, it is configured to cause the computing device to perform the steps of any one of the above-described method embodiments.
[0187] In various embodiments of the present application, the computer-readable storage medium includes: Step S1 of obtaining a first semantic expression vector and a perturbation weight vector by encoding a first sentence expression vector of a first sentence, the first sentence expression vector being a vector determined based on the character vector of each character in the first sentence, the first semantic expression vector being for indicating the meaning of the first sentence, and the perturbation weight vector being for controlling perturbation to the first semantic expression vector by a predetermined perturbation vector; Step S2 of obtaining a second semantic representation vector by perturbing the first semantic representation vector according to the perturbation weight vector and the perturbation vector; and step S3 of obtaining a second sentence by decoding the second semantic representation vector, wherein the first sentence and the second sentence have the same or similar meaning.
[0188] In various embodiments of the present application, as can be understood by those skilled in the art, all or part of the steps of each method in the above embodiments may be executed by instructing relevant hardware of the target terminal through a program. The program may be stored in a computer-readable storage medium. The storage medium may include a flash disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, an optical disk, etc.
[0189] The numbers of the above-mentioned embodiments of the present application are for illustrative purposes only and do not indicate the superiority or inferiority of the embodiments.
[0190] The integrated units in the above embodiments may be realized in the form of a software functional unit and stored in the above computer-readable storage medium when sold or used as an independent product. Based on this understanding, the configurations of the present application may essentially, in other words, the portion contributing to the prior art, or all or part of the configurations, be embodied in the form of a software product. The computer software product is stored in a storage medium and includes some instructions for causing one or more computer devices (which may be personal computers, servers, network devices, etc.) to execute all or part of the steps of the methods of each embodiment of the present application.
[0191] In the above embodiments of the present application, the description of each embodiment has its own focus, and for the parts not described in detail in one embodiment, reference can be made to the relevant descriptions of other embodiments.
[0192] In some embodiments provided herein, it should be understood that the displayed client may be implemented in other ways. The above-described device embodiments are merely schematic. For example, the division of units is merely a logical division of functions, and other division methods may be used in actual implementation. For example, multiple units or components may be combined or incorporated into other systems, or some features may be ignored or not implemented. Furthermore, the shown or discussed mutual couplings, direct couplings, or communication connections may be via some interfaces, and the indirect couplings or communication connections of units or modules may be electrical or other types.
[0193] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units, i.e., they may be located in one place or distributed across multiple units on a network. Depending on actual needs, some or all of the units may be selected to achieve the objectives of the invention according to various embodiments of the present application.
[0194] Furthermore, the functional units in each embodiment of the present application may be integrated into one processing unit, each unit may exist physically as an individual unit, or two or more units may be integrated into one unit. The above-mentioned integrated units may be realized in the form of hardware or in the form of a software functional unit.
[0195] The above is only a preferred embodiment of the present application. It should be noted that those skilled in the art can make some improvements and modifications without departing from the principle of the present application. These improvements and modifications should also be considered as part of the protection scope of the present application. [Explanation of symbols]
[0196] 102 Terminal Equipment 1202 coding units 1204 First Processing Unit 1206 Decoding Unit 1302 memory 1304 processor 1306 Transmission Equipment 1308 Display 1310 Connection Bus
Claims
1. A sentence generation method, comprising: a step of obtaining a first semantic representation vector and a perturbation weight vector by encoding a first sentence representation vector of a first sentence, wherein the first sentence representation vector is a vector determined based on a character vector of each character in the first sentence, the first semantic representation vector is for indicating the meaning of the first sentence, and the perturbation weight vector is for controlling perturbation to the first semantic representation vector by a predetermined perturbation vector; obtaining a second meaning-representing vector by perturbing the first meaning-representing vector based on the perturbation weight vector and the perturbation vector; and obtaining a second sentence by decoding the second semantic representation vector, wherein the first sentence and the second sentence have the same or similar meaning. A sentence generation method characterized by:
2. The step of obtaining a first semantic representation vector and a perturbation weight vector by encoding a first sentence representation vector of a first sentence includes: Sequentially passing the first sentence representation vector through N encoders (N is a positive integer equal to or greater than 1) having the same structure to obtain an Nth sentence encoding vector; performing a first linear transformation of the N-th sentence coding vector to obtain the first semantic representation vector; and obtaining the perturbation weight vector by performing a second linear transformation of the N-th sentence coding vector.
2. The sentence generation method according to claim 1,
3. The step of obtaining an N-th sentence encoding vector by sequentially passing the first sentence representation vector through N encoders having the same structure includes: inputting the first sentence representation vector into a first encoder of the N encoders to obtain a first sentence encoding vector; and if N is 2 or more, inputting the i-1th (i is a positive integer greater than or equal to 2 and less than or equal to N) sentence coding vector into the i-th encoder of the N encoders to obtain the i-1th sentence coding vector, wherein the i-1th sentence coding vector is the sentence coding vector output from the i-1th encoder of the N encoders.
3. The sentence generation method according to claim 2, wherein:
4. The step of obtaining the i-th sentence coding vector by inputting the i-1-th sentence coding vector to the i-th encoder of the N encoders includes: obtaining an i-th self-attention vector by inputting the (i-1)-th sentence coding vector into a self-attention module in the i-th encoder, wherein the (i-1)-th sentence coding vector and the i-th self-attention vector have the same dimension; inputting the i-th self-attention vector and the i-1-th sentence coding vector into a first summation normalization module in the i-th encoder, causing the first summation normalization module to perform summation processing on the i-th self-attention vector and the i-1-th sentence coding vector to obtain a first summation vector, and normalizing the first summation vector to obtain an i-th normalized vector, wherein the i-1-th sentence coding vector and the i-th normalized vector have the same dimension; obtaining an i-th feedforward vector by inputting the i-th normalized vector into a feedforward network module in the i-th encoder, wherein the (i-1)-th sentence encoding vector and the i-th feedforward vector have the same dimension; inputting the i-th normalized vector and the i-th feedforward vector to a second summation normalization module in the i-th encoder, causing the second summation normalization module to perform summation processing of the i-th normalized vector and the i-th feedforward vector to obtain a second summation vector, and normalizing the second summation vector to obtain the i-th sentence coding vector, wherein the (i-1)-th sentence coding vector and the i-th sentence coding vector have the same dimension.
4. The sentence generation method according to claim 3.
5. the step of obtaining the first semantic representation vector by performing a first linear transformation of the N-th sentence coding vector includes a step of obtaining the first semantic representation vector by multiplying the N-th sentence coding vector by a first weight matrix, wherein both the dimension of the N-th sentence coding vector and the dimension of the first weight matrix are M×M (M is a positive integer equal to or greater than 2); the step of obtaining the perturbation weight vector by performing a second linear transformation of the Nth sentence coding vector includes a step of obtaining the perturbation weight vector by multiplying the Nth sentence coding vector by a second weight matrix, wherein both the dimension of the Nth sentence coding vector and the dimension of the second weight matrix are M×M, and the first weight matrix and the second weight matrix are different weight matrices.
5. The sentence generation method according to claim 2, wherein:
6. The step of obtaining a second meaning-representing vector by perturbing the first meaning-representing vector based on the perturbation weight vector and the perturbation vector includes: determining 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; and obtaining the second semantic vector by adding the perturbed encoded vector and the first semantic vector.
6. A sentence generation method according to claim 1, wherein:
7. determining a perturbation coding vector based on the perturbation weight vector and the perturbation vector, multiplying each row vector of the perturbation vector by the perturbation weight vector on which the exponential operation has been performed, to obtain each row vector of the perturbation coding vector; The step of obtaining the second semantic vector by adding the perturbation encoding vector and the first semantic vector includes: and obtaining the second meaning expression vector by adding each row vector of the perturbed encoding vector to a corresponding row vector of the first meaning expression vector.
7. The sentence generation method according to claim 6.
8. The step of obtaining a second sentence by decoding the second semantic representation vector comprises: Sequentially passing the second meaning representation vector through P decoders (P is a positive integer equal to or greater than 1) having the same structure, to obtain a P-th sentence decoded vector; performing a third linear transformation of the P-th sentence decoded vector to obtain a second sentence representation vector; determining the second sentence based on the second sentence representation vector; 8. A sentence generation method according to claim 1, wherein:
9. The step of obtaining a P-th sentence decoded vector by passing the second semantic representation vector through P decoders having the same structure in sequence, obtaining a first sentence-decoded vector by inputting the second semantic representation vector and a predetermined start symbol into a first decoder of the P decoders; and if P is 2 or more, inputting the second meaning representation vector and a j-1th (j is a positive integer greater than or equal to 2 and less than or equal to P) sentence decoded vector into a j-th decoder among the P decoders to obtain a j-th sentence decoded vector, wherein the j-1th sentence decoded vector is the sentence decoded vector output from the j-1th decoder among the P decoders.
9. The sentence generation method according to claim 8.
10. the step of inputting the second semantic representation vector and the j-1-th sentence decoded vector into a j-th decoder among the P decoders to obtain a j-th sentence decoded vector, obtaining a j-th first self-attention vector by inputting the j-1-th sentence decoded vector into a first self-attention module in the j-th decoder, wherein the j-1-th sentence decoded vector and the j-th self-attention vector have the same dimension; a step of inputting the jth self-attention vector and the j-1th sentence-decoded vector into a third summation normalization module in the jth decoder to obtain a jth third normalized vector, wherein the j-1th sentence-decoded vector and the jth third normalized vector have the same dimension, and the third summation normalization module adds the jth self-attention vector and the j-1th sentence-decoded vector to obtain a third summation vector, and normalizes the third summation vector to obtain the jth third normalized vector; obtaining a jth second self-attention vector by inputting the jth third normalized vector and the second semantic representation vector into a second self-attention module in the jth decoder, wherein the jth third normalized vector has the same dimension as the second semantic representation vector and the jth second self-attention vector; inputting the jth second self-attention vector and the jth third normalized vector into a fourth summing normalization module in the jth decoder to obtain a jth fourth normalized vector, wherein the jth second self-attention vector and the jth fourth normalized vector have the same dimension, and the fourth summing normalization module adds the jth second self-attention vector and the jth third normalized vector to obtain a fourth summing vector, and normalizes the fourth summing vector to obtain the jth fourth normalized vector; and obtaining the j-th sentence-decoded vector by inputting the j-th fourth normalized vector into a feedforward network module in the j-th decoder, wherein the j-th fourth normalized vector and the j-th sentence-decoded vector have the same dimension.
10. The sentence generation method according to claim 9.
11. the step of obtaining a second sentence representation vector by performing a third linear transformation of the P-th sentence decoded vector includes a step of obtaining the second sentence representation vector having a dimension of M×T by multiplying the P-th sentence decoded vector having a dimension of M×M by a third weight matrix having a dimension of M×T, where T represents the number of characters included in a predetermined dictionary and is a positive integer of 2 or more; The step of determining the second sentence based on the second sentence representation vector includes the steps of: normalizing the second sentence representation vector, which has a dimension of M×T, to obtain a normalized vector, which has a dimension of M×T; and, if a value of a vector, which has a dimension of 1×T and is in a k-th row (k is a positive integer of 1 or more and M or less), in the normalized vector, which has a dimension of M×T, satisfies a condition for determining a corresponding character from the predetermined dictionary, and the value at the q-th position (q is a positive integer of 1 or more and T or less) in the vector, which has a dimension of 1×T and is in the k-th row, is maximum, determining the character at the q-th position in the predetermined dictionary as the character in the second sentence.
11. The sentence generation method according to claim 8, wherein:
12. If the first sentence contains F characters (F is a positive integer of 2 or more), obtain character vectors of the F characters in the first sentence, thereby obtaining F character vectors with dimensions 1×M (M is a positive integer of 2 or more); if F is smaller than M, acquiring a first sentence expression vector having a dimension of M×M by combining the F character vectors having a dimension of 1×M with M−F filler vectors having a dimension of 1×M, wherein all of the M−F filler vectors having a dimension of 1×M have a value of 0.
12. A sentence generation method according to claim 1.
13. and further comprising: a step of obtaining a target test result by testing a target sentence recognition model to be tested using the first sentence and the second sentence, the target test result being for indicating whether the same recognition result is recognized for the first sentence and the second sentence.
13. A sentence generation method according to claim 1.
14. the step of obtaining a target test result by testing a target sentence recognition model to be tested using the first sentence and the second sentence, obtaining a first emotion label by inputting the first sentence into the target sentence recognition model, the first emotion label being for indicating an emotion type expressed by the meaning of the first sentence; obtaining a second emotion label by inputting the second sentence into the target sentence recognition model, the second emotion label being for indicating an emotion type expressed by the meaning of the second sentence; comparing whether the first emotion label and the second emotion label are the same; If the first emotion label and the second emotion label are the same, indicating to the target test result that the same recognition result has been recognized for the first sentence and the second sentence; and if the first emotion label and the second emotion label are different, indicating in the target test result that different recognition results have been recognized for the first sentence and the second sentence.
14. The sentence generation method according to claim 13.
15. and if the target test result indicates that different recognition results are recognized for the first sentence and the second sentence, generating presentation information, the presentation information being for presentation to adjust the target sentence recognition model.
15. The sentence generation method according to claim 13 or 14.
16. A sentence generation device, an encoding unit for obtaining a first semantic representation vector and a perturbation weight vector by encoding a first sentence representation vector of a first sentence, the first sentence representation vector being a vector determined based on a character vector of each character in the first sentence, the first semantic representation vector being for indicating a meaning of the first sentence, and the perturbation weight vector being for controlling perturbation of the first semantic representation vector by a predetermined perturbation vector; a first processing unit for perturbing the first meaning representation vector based on the perturbation weight vector and the perturbation vector to obtain a second meaning 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 meaning; A sentence generation device characterized by:
17. 16. A computer-readable storage medium containing a program stored therein, the program being capable of causing a terminal device or a computer to execute the sentence generation method of any one of claims 1 to 15 when executed by the terminal device or a computer.
18. A computer program product comprising computer programs / instructions which, when executed by a processor, cause the computer program / instructions to implement the steps of the sentence generation method according to any one of claims 1 to 15.
19. 16. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the sentence generation method according to any one of claims 1 to 15 by the computer program.