Text generation method and device, equipment and storage medium

By setting optimization strategies in the training and inference phases respectively, and using penalty terms and dynamic generation probability adjustments, the problem of repetitive content generation in large language models is solved, thereby improving the diversity and quality of generated content.

CN117252271BActive Publication Date: 2026-01-27SOUNDAI TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311213432.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-09-19
Publication Date
2026-01-27
Estimated Expiration
2043-09-19

AI Technical Summary

Technical Problem

Large language models are prone to repetitive content during text generation, leading to information redundancy and reducing the quality of the generated content.

Method used

During the training phase, a loss function with a penalty term related to content redundancy is introduced to adjust the generation probability to avoid duplicate content; during the inference phase, the generation probability is dynamically adjusted to prevent the generation of duplicate content.

Benefits of technology

This effectively avoids generating unnecessary repetitive content from large models, thus improving the diversity and quality of the generated content.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117252271B_ABST
    Figure CN117252271B_ABST
Patent Text Reader

Abstract

The application discloses a text generation method and device, equipment and storage medium, and belongs to the technical field of artificial intelligence. The method can avoid the generation of unnecessary repetitive content by setting optimization strategies in the training stage and the inference stage. In detail, in the training stage, a penalty term is added to the loss function, wherein the penalty term is related to the first penalty factor and the content repetition degree of the predicted text output by the large model. That is, if the large model attempts to generate unnecessary repetitive content, the large model will be greatly punished due to the existence of the penalty term, thereby encouraging the large model to generate different content. In the inference stage, the large model remembers the generated content during the output generation word process, and uses the content repetition degree as the judgment standard to prevent the large model from generating unnecessary repetitive content by adjusting the generation probability.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of artificial intelligence technology, and in particular to a text generation method, apparatus, device, and storage medium. Background Technology

[0002] In recent years, with the rapid development of artificial intelligence technology, natural language processing, as an important direction of artificial intelligence, has also made significant progress. In particular, the emergence of large language models (also known as large language models or simply large models) has greatly promoted the performance improvement of natural language processing tasks.

[0003] However, large models often generate unnecessary repetitive content during text generation, exhibiting a "repeater" problem. This undoubtedly leads to information redundancy and reduces the quality of the generated content. Therefore, how to avoid large models generating unnecessary repetitive content to ensure model performance and improve generalization ability has become a focus of attention for those skilled in the art. Summary of the Invention

[0004] This application provides a text generation method, apparatus, device, and storage medium that can avoid generating unnecessary repetitive content from large models, thereby improving the quality of the generated content. The technical solution is as follows:

[0005] On the one hand, a text generation method is provided, the method comprising:

[0006] During the training phase, training data is input into an initial language model, and the encoder and decoder of the initial language model encode and decode the training data in sequence to generate predicted text.

[0007] Based on the reference text and the predicted text of the training data, a loss function is constructed; wherein, the loss function includes a penalty term, which is related to a first penalty factor and the content repetition of the predicted text; the weight values ​​of the initial language model are updated through the loss function until the model convergence condition is met, and the target language model is obtained;

[0008] During the inference phase, for the i-th position, in response to the fact that the content repetition of the word predicted by the decoder of the target language model is greater than the first threshold, the generation probability of the predicted word is adjusted based on the second penalty factor.

[0009] The generated word at the i-th position is determined based on the adjusted generation probability, and the determined generated word is output as the result; where i is a positive integer.

[0010] In one possible implementation, during the training phase, the method further includes:

[0011] For the j-th generated word in the predicted text, obtain the relative position index of each generated word generated before the j-th generated word;

[0012] The content repetition degree of the j-th generated word is determined based on the relative position index and suppression factor of each generated word.

[0013] Where j is a positive integer;

[0014] For any generated word generated before the j-th generated word, if the generated word is a repetition of the j-th generated word, the suppression factor is set to a first value; if the generated word is not a repetition of the j-th generated word, the suppression factor is set to a second value.

[0015] In one possible implementation, during the training phase, the method further includes:

[0016] The penalty term is constructed based on the first penalty factor and the content repetition of each generated word in the predicted text;

[0017] The first penalty factor has a value range of 0 to 1.

[0018] In one possible implementation, the step of adjusting the generation probability of the predicted word based on a second penalty factor when the content repetition of the word predicted by the decoder of the target language model is greater than a first threshold includes:

[0019] Obtain the relative position index of each generated word generated before the predicted word;

[0020] The content repetition degree of the predicted words is determined based on the relative position index and suppression factor of each generated word;

[0021] The content repetition of the predicted words is normalized.

[0022] If the normalized content repetition rate is greater than the first threshold, the generation probability of the predicted word is adjusted based on the second penalty factor.

[0023] Wherein, for any generated word generated before the predicted word, in response to the generated word being a repetition of the predicted word, the suppression factor is set to a first value; in response to the generated word not being a repetition of the predicted word, the suppression factor is set to a second value.

[0024] In one possible implementation, adjusting the predicted word generation probability based on the second penalty factor includes:

[0025] The product of the second penalty factor and the original generation probability of the predicted word is used as the adjusted generation probability of the predicted word.

[0026] The second penalty factor ranges from 0 to 1.

[0027] In one possible implementation, during the training phase, the method further includes:

[0028] In response to the content repetition of the generated word being greater than the second threshold and lasting for at least the first round, the first penalty factor is increased; after at most the second round, in response to the reduction in the content repetition of the generated word not reaching the magnitude threshold, the first penalty factor is further increased and the increase this time is greater than the increase in the previous time.

[0029] In response to the content repetition of the generated word being less than a third threshold and lasting for at least the first round, the first penalty factor is reduced; after at most the second round, in response to the increase in the content repetition of the generated word not reaching the magnitude threshold, the first penalty factor is further reduced and the reduction magnitude this time is greater than the previous reduction magnitude.

[0030] In one possible implementation, during the inference phase, the method further includes:

[0031] In response to the fact that the content repetition of the generated word is greater than the second threshold and continues for at least the first round, the second penalty factor is reduced; after at most the second round, in response to the fact that the reduction in the content repetition of the generated word does not reach the magnitude threshold, the second penalty factor is further reduced and the reduction magnitude this time is greater than the reduction magnitude of the previous time;

[0032] In response to the content repetition of the generated word being less than the third threshold and lasting for at least the first round, the second penalty factor is increased; after at most the second round, in response to the increase in the content repetition of the generated word not reaching the magnitude threshold, the second penalty factor is further increased and the increase this time is greater than the increase in the previous time.

[0033] In one possible implementation, the method further includes:

[0034] The first round and the second round are determined based on the total number of generated words and the total number of punctuation marks output by the decoder.

[0035] On the other hand, a text generation apparatus is provided, the apparatus comprising:

[0036] The training module is configured to input training data into an initial language model during the training phase, and then encode and decode the training data sequentially based on the encoder and decoder of the initial language model to generate predicted text.

[0037] The training module is further configured to construct a loss function based on the reference text and the predicted text of the training data; wherein, the loss function includes a penalty term, which is related to a first penalty factor and the content repetition of the predicted text; the weight values ​​of the initial language model are updated through the loss function until the model convergence condition is met, thereby obtaining the target language model;

[0038] The generation module is configured to, during the inference phase, adjust the generation probability of the predicted word based on a second penalty factor for the i-th position, in response to the content repetition of the word predicted by the decoder of the target language model being greater than a first threshold.

[0039] The generation module is further configured to determine the generated word at the i-th position based on the adjusted generation probability, and output the determined generated word as the result; where i is a positive integer.

[0040] In one possible implementation, the training module is configured as follows:

[0041] For the j-th generated word in the predicted text, obtain the relative position index of each generated word generated before the j-th generated word;

[0042] The content repetition degree of the j-th generated word is determined based on the relative position index and suppression factor of each generated word; where j is a positive integer.

[0043] For any generated word generated before the j-th generated word, in response to the fact that the generated word is repeated with the j-th generated word, the value of the inhibition factor is a first value;

[0044] In response to the fact that any generated word is not a repetition of the j-th generated word, the value of the inhibition factor is a second value.

[0045] In one possible implementation, the training module is configured as follows:

[0046] The penalty term is constructed based on the first penalty factor and the content repetition of each generated word in the predicted text; wherein the value of the first penalty factor ranges from 0 to 1.

[0047] In one possible implementation, the generation module is configured as follows:

[0048] Obtain the relative position index of each generated word generated before the predicted word;

[0049] The content repetition degree of the predicted words is determined based on the relative position index and suppression factor of each generated word;

[0050] The content repetition of the predicted words is normalized.

[0051] If the normalized content repetition rate is greater than the first threshold, the generation probability of the predicted word is adjusted based on the second penalty factor.

[0052] Wherein, for any generated word generated before the predicted word, in response to the fact that the generated word is repeated with the predicted word, the value of the suppression factor is a first value;

[0053] In response to the fact that any generated word does not repeat the predicted word, the suppression factor is set to a second value.

[0054] In one possible implementation, the generation module is configured as follows:

[0055] The product of the second penalty factor and the original generation probability of the predicted word is used as the adjusted generation probability of the predicted word; wherein the value of the second penalty factor ranges from 0 to 1.

[0056] In one possible implementation, the training module is configured as follows:

[0057] In response to the content repetition of the generated word being greater than the second threshold and lasting for at least the first round, the first penalty factor is increased; after at most the second round, in response to the reduction in the content repetition of the generated word not reaching the magnitude threshold, the first penalty factor is further increased and the increase this time is greater than the increase in the previous time.

[0058] In response to the content repetition of the generated word being less than a third threshold and lasting for at least the first round, the first penalty factor is reduced; after at most the second round, in response to the increase in the content repetition of the generated word not reaching the magnitude threshold, the first penalty factor is further reduced and the reduction magnitude this time is greater than the previous reduction magnitude.

[0059] In one possible implementation, the generation module is configured as follows:

[0060] In response to the fact that the content repetition of the generated word is greater than the second threshold and continues for at least the first round, the second penalty factor is reduced; after at most the second round, in response to the fact that the reduction in the content repetition of the generated word does not reach the magnitude threshold, the second penalty factor is further reduced and the reduction magnitude this time is greater than the reduction magnitude of the previous time;

[0061] In response to the content repetition of the generated word being less than the third threshold and lasting for at least the first round, the second penalty factor is increased; after at most the second round, in response to the increase in the content repetition of the generated word not reaching the magnitude threshold, the second penalty factor is further increased and the increase this time is greater than the increase in the previous time.

[0062] In one possible implementation, the device further includes:

[0063] The determining module is configured to determine the first round and the second round based on the total number of generated words and the total number of punctuation marks output by the decoder.

[0064] On the other hand, a computer device is provided, the device including a processor and a memory, the memory storing at least one piece of program code, the at least one piece of program code being loaded and executed by the processor to implement the above-described text generation method.

[0065] On the other hand, a computer-readable storage medium is provided, wherein at least one piece of program code is stored therein, the at least one piece of program code being loaded and executed by a processor to implement the above-described text generation method.

[0066] On the other hand, a computer program product or computer program is provided, which includes computer program code stored in a computer-readable storage medium. A processor of a computer device reads the computer program code from the computer-readable storage medium and executes the computer program code, causing the computer device to perform the above-described text generation method.

[0067] The text generation scheme provided in this application avoids the generation of unnecessary repetitive content by setting optimization strategies during the training and inference phases. Specifically, during the training phase, this application adds a penalty term to the loss function. This penalty term is related to a first penalty factor and the content repetition rate of the predicted text output by the large model. That is, if the large model attempts to generate unnecessary repetitive content, it will be significantly penalized due to this penalty term, thus incentivizing it to generate different content. During the inference phase, when outputting generated words, the large model remembers the already generated content and uses content repetition rate as a criterion to adjust the generation probability to prevent the large model from generating unnecessary repetitive content.

[0068] In summary, the embodiments of this application effectively solve the problem of generating repetitive content from large models, increase the diversity of generated content, and thus improve the quality of generated content. Attached Figure Description

[0069] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0070] Figure 1 This is a schematic diagram of the implementation environment involved in a text generation method provided in an embodiment of this application;

[0071] Figure 2 This is a schematic diagram of the architecture of a text generation method provided in an embodiment of this application;

[0072] Figure 3 This is a flowchart of a text generation method provided in an embodiment of this application;

[0073] Figure 4 This is a flowchart of another text generation method provided in the embodiments of this application;

[0074] Figure 5 This is a schematic diagram of the structure of a text generation device provided in an embodiment of this application;

[0075] Figure 6 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. Detailed Implementation

[0076] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.

[0077] In this application, the terms "first," "second," etc., are used to distinguish identical or similar items that have essentially the same function. It should be understood that there is no logical or temporal dependency between "first," "second," and "nth," nor does it limit the quantity or execution order. It should also be understood that although the following description uses the terms "first," "second," etc., to describe various elements, these elements should not be limited by the terms.

[0078] These terms are simply used to distinguish one element from another. For example, without departing from the various examples, the first element can be referred to as the second element, and similarly, the second element can be referred to as the first element. Both the first and second elements can be elements, and in some cases, they can be separate and distinct elements.

[0079] "At least one" refers to one or more elements. For example, at least one element can be one element, two elements, three elements, or any integer number of elements greater than or equal to one. "Multiple" refers to two or more elements. For example, multiple elements can be two elements, three elements, or any integer number of elements greater than or equal to two.

[0080] In this article, "and / or" indicates that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, or B alone. The character " / " generally indicates that the preceding and following objects are in an "or" relationship.

[0081] It should be noted that the information (including but not limited to user device information, user personal information, etc.), data (including but not limited to data used for analysis, data stored, data displayed, etc.) and signals involved in this application are all authorized by the user or fully authorized by all parties, and the collection, use and processing of related data must comply with the relevant laws, regulations and standards of the relevant regions.

[0082] Figure 1 This is a schematic diagram of the implementation environment of a text generation method provided in this application embodiment.

[0083] In this embodiment of the application, the implementation environment includes a computer device. For example, see [link to relevant documentation]. Figure 1 The aforementioned computer equipment includes a terminal 101 and a server 102. In other words, the text generation method is jointly executed by the terminal 101 and the server 102, which is not limited in this application.

[0084] In one possible implementation, server 102 is used for training a large model, and upon receiving a text generation request from terminal 101, automatically generates text based on the trained large model and returns the generated text to terminal 101. Alternatively, a dedicated server can train the large model and send the trained large model to server 102. In this case, upon receiving a text generation request from terminal 101, server 102 automatically generates text based on the trained large model and returns the generated text to terminal 101.

[0085] For example, terminal 101 is a computer device with a display screen, such as a smartphone or tablet computer; while server 102 can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, and this application does not limit it.

[0086] Furthermore, the servers involved in the embodiments of this application may also include other servers to provide more comprehensive and diversified services. Additionally, those skilled in the art will understand that the number of terminals may be more or fewer than illustrated. For example, the number of terminals may be only a few, or dozens or hundreds, or even more; this application does not limit this number.

[0087] For example, terminal 101 is equipped with an application that provides text generation functionality. For instance, a user can download the application via a mobile phone or tablet. Server 102 is used to provide background services for the application. For instance, server 102 is equipped with a pre-trained large model that can automatically generate text.

[0088] As mentioned earlier, large models suffer from a "repeater" problem in the automatic text generation process. This problem arises primarily because, during the training process, due to the influence of data distribution, large models may tend to generate content that appears frequently in the training data. Furthermore, in practical applications, the decoding strategy of large models may also lead to the generation of repetitive content.

[0089] To address the "repeater" problem, embodiments of this application provide, as follows: Figure 2 The large model training and inference optimization system 20 shown is used to avoid unnecessary repetitive content generation, thereby preventing overfitting and improving the model's generalization ability.

[0090] In this embodiment, the "repeating machine" problem is addressed simultaneously in both the training and inference phases, meaning that the model's training and inference strategies are changed concurrently. During training, a penalty term is introduced by modifying the loss function to prevent the model from generating unnecessary repetitive content. During inference, a dynamic generation probability adjustment mechanism is introduced by changing the decoding strategy to avoid generating unnecessary repetitive content, thereby improving the diversity of generated content and ultimately ensuring a better user experience.

[0091] In one possible implementation, see Figure 2 The optimization system 20 includes: a model training unit 21, a feedback mechanism unit 22, a model inference unit 23, and a dynamic deduplication decoding unit 24.

[0092] The feedback mechanism unit 22 and the dynamic deduplication decoding unit 24 are responsible for solving the "repeater" problem in the training and inference phases, respectively. In other words, the optimization system 20 solves the "repeater" problem of large models by introducing optimization strategies in the training and inference phases.

[0093] The model training unit 21 is responsible for receiving and processing training data, and then passing the processed training data to the large model for training. For example, the data processing here refers to preprocessing the training data, such as data cleaning, but this application does not limit this to specific methods.

[0094] Regarding the feedback mechanism unit 22, this unit is responsible for preventing the large model from generating unnecessary repetitive content by adding a penalty term to the loss function during the training phase.

[0095] For model inference unit 23, this unit is responsible for receiving user input during the inference phase and passing the user input to the large model to generate output corresponding to the user input.

[0096] The dynamic deduplication decoding unit 24 is responsible for preventing the large model from generating unnecessary repetitive content by dynamically adjusting the generation probability during the inference phase.

[0097] The following describes in detail how the optimization system 20 avoids generating duplicate content through the following implementation method.

[0098] Figure 3 This is a flowchart illustrating a text generation method provided in an embodiment of this application. The method is executed by a computer device, such as... Figure 2 The optimized system 20 is shown. See also... Figure 3 The method flow provided in this application embodiment includes:

[0099] 301. During the training phase, the computer device inputs the training data into the initial language model. The encoder and decoder based on the initial language model encode and decode the training data in sequence to generate the predicted text.

[0100] This step is by Figure 2 The model training unit 21 shown is executed.

[0101] It should be noted that, in order to distinguish between the large model before and after training, this application refers to the untrained initial large model as the initial language model, and the trained large model as the target language model.

[0102] For example, the large model used in this application embodiment is a pre-trained language model, such as a Transformer-based pre-trained language model, like the GPT model. The pre-trained language model includes an encoder and a decoder. The encoder encodes the input sequence of the model to obtain an encoded result; while the decoder performs decoding on the encoded result and outputs the decoded result as the final result.

[0103] Taking the GPT model as an example, the core of the GPT model is the Transformer model, which uses self-attention mechanism and residual connections to achieve efficient parallel computing and information flow. For instance, the GPT model is trained using unsupervised learning, mainly in two stages: the pre-training stage and the fine-tuning stage.

[0104] During the pre-training phase, the GPT model is trained using a large amount of training data to allow it to learn the statistical patterns of language. For example, the GPT model employs two pre-training tasks: MLM (Masked Language Modeling) and NSP (Next Sentence Prediction). The MLM task involves randomly replacing a portion of the words in the input sequence with [MASK] tags, and the model needs to predict these replaced words based on the context. The NSP task involves determining whether two consecutive sentences are adjacent, given the given sentences. These two tasks allow the model to learn the relationships between words and sentences, thereby improving its performance.

[0105] In the fine-tuning phase, the pre-trained model is fine-tuned using supervised learning to adapt to different natural language processing tasks. During fine-tuning, the model's weights are updated using the backpropagation algorithm, ultimately resulting in a larger model suitable for the specific task.

[0106] Taking the initial language model as a pre-trained model and the specific task as a text generation task for question-answering scenarios, the training data is the text corpus collected in question-answering scenarios, and this application does not limit it.

[0107] 302. During the training phase, the computer device constructs a loss function based on the reference text and the predicted text of the training data; wherein the loss function includes a penalty term, which is related to the first penalty factor and the content repetition of the predicted text.

[0108] This step is by Figure 2 The feedback mechanism unit 22 shown is executed.

[0109] It should be noted that the first penalty factor is referred to as parameter λ in this paper, and is also called the first decay factor or the first adjustment factor. Additionally, this penalty term is referred to as parameter Pe in this paper.

[0110] Regarding optimization strategies during the training phase, this application proposes a feedback mechanism: during model training, the loss function is modified by introducing a penalty term Pe. If the large model generates content that is repeated from previous content, the large model will be subject to a significant penalty due to the existence of this penalty term.

[0111] In one possible implementation, the value of the penalty term Pe is proportional to the repetition rate R of the generated content, i.e., Pe = λR.

[0112] For a detailed description of the optimization strategies during the training phase, please refer to the following text.

[0113] 303. During the training phase, the computer equipment updates the weight values ​​of the initial language model through the loss function until the model convergence condition is met, thus obtaining the target language model.

[0114] For example, the model convergence condition can be either reaching a specified number of iterations or the calculated loss value being less than a preset value; this application does not limit this.

[0115] 304. During the inference phase, for the i-th position, in response to the fact that the content repetition of the word predicted by the decoder of the target language model is greater than the first threshold, the computer device adjusts the generation probability of the predicted word based on the second penalty factor; and determines the generated word for the i-th position based on the adjusted generation probability.

[0116] Here, i takes the value of a positive integer. It should be noted that the second penalty factor is referred to as parameter δ in this paper, and is also called the second attenuation factor or the second adjustment factor.

[0117] Regarding optimization strategies for the inference phase, this application introduces a novel decoding strategy during decoding. With this decoding strategy, the model remembers already generated content and adjusts the generation probability of candidate words given by the decoder in the future based on this content, thereby avoiding the generation of unnecessary repetitive content.

[0118] For a detailed description of the optimization strategy for the inference phase, please refer to the following text.

[0119] 305. During the reasoning phase, the computer device outputs the determined generated words as the result.

[0120] The selected generated words can be arranged in order to form one or more complete sentences for output.

[0121] The text generation scheme provided in this application avoids the generation of unnecessary repetitive content by setting optimization strategies during the training and inference phases. Specifically, during the training phase, this application adds a penalty term to the loss function. This penalty term is related to a first penalty factor and the content repetition rate of the predicted text output by the large model. That is, if the large model attempts to generate unnecessary repetitive content, it will be significantly penalized due to this penalty term, thus incentivizing it to generate different content. During the inference phase, when outputting generated words, the large model remembers the already generated content and uses content repetition rate as a criterion to adjust the generation probability to prevent the large model from generating unnecessary repetitive content.

[0122] In summary, the embodiments of this application effectively solve the problem of generating repetitive content from large models, increase the diversity of generated content, and thus improve the quality of generated content.

[0123] The above is only a brief introduction to some technical details of the recommended solution provided in the embodiments of this application. The following is based on... Figure 4 The illustrated embodiments provide a detailed description of the recommended solution.

[0124] Figure 4 This is a flowchart of another text generation method provided in an embodiment of this application. The execution subject of this method is a computer device, such as... Figure 2 The optimized system 20 is shown. See also... Figure 4 The method flow provided in this application embodiment includes:

[0125] 401. During the training phase, the computer device inputs the training data into the initial language model. The encoder and decoder based on the initial language model encode and decode the training data in sequence to generate the predicted text.

[0126] This step can be referred to in step 301 above, and will not be repeated here.

[0127] 402. During the training phase, the computer device constructs a loss function based on the reference text and the predicted text of the training data; wherein the loss function includes a penalty term, which is related to the first penalty factor and the content repetition of the predicted text.

[0128] In one possible implementation, the original loss function is the cross-entropy loss function, which has the following form:

[0129] L(y, y′) = -∑ylog(y′)

[0130] Where y refers to the actual token, i.e., the reference text of the training data, and y′ refers to the token predicted by the model during training, i.e., the predicted text output by the decoder.

[0131] In another possible implementation, during the training phase, embodiments of this application construct the penalty term in the following manner:

[0132] A penalty term is constructed based on the first penalty factor and the content repetition of each generated word in the predicted text; wherein the value of the first penalty factor ranges from 0 to 1.

[0133] Accordingly, the loss function after adding the penalty term is as follows:

[0134] L(y,y′)=-∑ylog(y′)+λR

[0135] It should be noted that, regarding the calculation of content repetition rate R, this application embodiment uses a sliding window calculation method. It is understood that, for the generated word c... i Distance c i The further away the word is from c i The role of repetition in the judgment should be relatively weak. For example, in the text "I want to eat, I ate fish / meat today, eating is my favorite thing," although "eating" is repeated in the text, the meaning in the whole text is correct. Text like "I want to eat / eat / eat" is what should be focused on in the repetition judgment.

[0136] Based on the above analysis, the embodiments of this application introduce position information into the sliding window calculation method, so that the distance to the generated word c is... i The further apart the words are, the less significant their role in the repetition calculation. The formula for calculating content repetition R is as follows:

[0137]

[0138] Here, p is the relative position index of each word. ε is also referred to as the inhibition factor in this paper.

[0139] Based on the above description, the method provided in this application embodiment further includes the following during the training phase:

[0140] For the j-th generated word in the predicted text, obtain the relative position indices of all generated words generated before the j-th generated word; determine the content repetition degree of the j-th generated word based on the relative position indices of each generated word and the suppression factor. Here, j is a positive integer.

[0141] It should be noted that for any generated word generated before the j-th generated word, the suppression factor is set to the first value, such as 1, in response to any generated word being the same as the j-th generated word; and the suppression factor is set to the second value, such as 0, in response to any generated word not being the same as the j-th generated word.

[0142] 403. During the training phase, the computer equipment updates the weight values ​​of the initial language model through the loss function until the model convergence condition is met, thus obtaining the target language model.

[0143] This step can be referred to in step 303 above, and will not be repeated here.

[0144] 404. During the inference phase, for the i-th position, if the content repetition of the word predicted by the decoder of the target language model is greater than the first threshold, the computer device uses the product of the second penalty factor and the original generation probability of the predicted word as the adjusted generation probability of the predicted word; and determines the generated word for the i-th position based on the adjusted generation probability.

[0145] Regarding the optimization strategy for the inference stage, this application uses content duplication as the criterion for reducing the generation probability. For example, a sliding window calculation method is also used when determining content duplication.

[0146] In addition, after obtaining the content duplication rate, this application embodiment will further perform a normalization operation and set a threshold (referred to as the first threshold in this document). If the normalized content duplication rate exceeds the set threshold, this application embodiment will penalize it by adjusting its generation probability.

[0147] For example, in order to reduce the generation probability, the embodiments of this application use a second penalty factor δ multiplied by the original generation probability P(A), that is, the adjusted generation probability P(B) = δ*P(A), where 0 < δ < 1.

[0148] In one possible implementation, in response to the decoder of the target language model predicting that the content repetition of a word is greater than a first threshold, the product of a second penalty factor and the original generation probability of the predicted word is used as the adjusted generation probability of the predicted word, including but not limited to the following methods:

[0149] Obtain the relative position index of each generated word that precedes the predicted word above;

[0150] Based on the relative position index and suppression factor of each generated word, the content repetition degree of the predicted words is determined.

[0151] The content repetition of the predicted words above was normalized.

[0152] If the normalized content repetition rate is greater than the first threshold, the product of the second penalty factor and the original generation probability of the predicted word is used as the adjusted generation probability of the predicted word.

[0153] Specifically, for any generated word that precedes the predicted word, the suppression factor is set to a first value if the generated word is a repetition of the predicted word, and to a second value if the generated word is not a repetition of the predicted word.

[0154] 405. During the reasoning phase, the computer device outputs the determined generated words as the result.

[0155] It's important to note that the choice of the first penalty factor, λ, is crucial. If λ is too large, it may over-penalize repetitive content, causing the large model to become overly conservative and fail to generate necessary repetitions (such as certain fixed expressions). If λ is too small, the penalty term's impact will be too small to effectively prevent the model from generating unnecessary repetitive content. Therefore, λ needs to be adjusted experimentally to determine a value that effectively prevents the generation of unnecessary repetitive content without over-penalizing it. For example, λ can be set to 0.2 or 0.3.

[0156] Secondly, it's important to note that the choice of the second penalty factor δ is also crucial. If δ is too small, the large model will excessively avoid generating duplicate content during text generation, potentially leading to a decrease in the quality of the generated content. If δ is too large, the dynamic deduplication effect will deteriorate. Therefore, δ also needs to be adjusted experimentally to determine a value that effectively prevents the generation of unnecessary duplicate content without excessively affecting the quality of the generated content. For example, δ can be set to 0.9 or 0.7.

[0157] The third point to note is that in practical applications, the parameters λ and δ also need to be dynamically adjusted. For example, if the optimization system generates too much repetitive content within a certain time period, λ is increased and δ is decreased. Conversely, if the system is found to be overly conservative and hesitant to generate necessary repetitive content, λ is decreased and δ is increased. This dynamic adjustment mechanism allows the optimization system to self-optimize based on the actual situation, achieving the optimal deduplication effect.

[0158] In one possible implementation, embodiments of this application dynamically adjust λ and δ by controlling the magnitude of their increase or decrease.

[0159] For example, regarding the dynamic adjustment of λ, during the training process, the number of generated tokens is used as a benchmark and round information is added. The content repetition R is calculated in each round. If the content repetition R calculated in each round is large, it means that the large model has generated too much repetitive content, so the value of λ needs to be increased. If the content repetition calculated after several rounds does not decrease significantly, the magnitude of λ needs to be increased.

[0160] In addition, if the content repetition R calculated in each round is very small, for example, the content repetition calculated each time is 0 or close to 0, it means that the optimization system is too conservative, so the value of λ needs to be reduced; if the content repetition calculated after several rounds does not increase significantly, then the reduction of λ needs to be increased.

[0161] Based on the above description, during the training phase, the value of parameter λ can be dynamically adjusted in the following manner:

[0162] 1. If the content repetition of the generated word is greater than the second threshold and continues for at least the first round, the first penalty factor is increased; after at most the second round, if the decrease in the content repetition of the generated word does not reach the magnitude threshold, the first penalty factor is increased again and the increase this time is greater than the increase in the previous time.

[0163] The second threshold can be the same as or different from the first threshold; this application does not limit this.

[0164] 2. If the content repetition of the generated words is less than the third threshold and continues for at least the first round, the first penalty factor is reduced; after at most the second round, if the increase in the content repetition of the generated words does not reach the magnitude threshold, the first penalty factor is further reduced and the reduction this time is greater than the previous reduction.

[0165] In another possible implementation, during the inference phase, the value of parameter δ can be dynamically adjusted in the following manner:

[0166] 1. If the content repetition of the generated word is greater than the second threshold and continues for at least the first round, the second penalty factor is reduced; after at most the second round, if the reduction in the content repetition of the generated word does not reach the magnitude threshold, the second penalty factor is further reduced and the reduction this time is greater than the previous reduction.

[0167] 2. If the content repetition of the generated word is less than the third threshold and continues for at least the first round, the second penalty factor is increased; after at most the second round, if the increase in the content repetition of the generated word does not reach the magnitude threshold, the second penalty factor is increased again and the increase this time is greater than the increase in the previous time.

[0168] It should be noted that the embodiments of this application support determining the specific values ​​of the first round and the second round based on the total number of generated words and the total number of punctuation marks output by the decoder. That is, the values ​​of the first round and the second round vary with the length of the output text, and this application does not limit this.

[0169] The text generation scheme provided in this application avoids the generation of unnecessary repetitive content by setting optimization strategies during the training and inference phases. Specifically, during the training phase, this application adds a penalty term to the loss function. This penalty term is related to a first penalty factor and the content repetition rate of the predicted text output by the large model. That is, if the large model attempts to generate unnecessary repetitive content, it will be significantly penalized due to this penalty term, thus incentivizing it to generate different content. During the inference phase, when outputting generated words, the large model remembers the already generated content and uses content repetition rate as a criterion to adjust the generation probability to prevent the large model from generating unnecessary repetitive content.

[0170] In summary, the embodiments of this application effectively solve the problem of generating repetitive content from large models, increase the diversity of generated content, and thus improve the quality of generated content.

[0171] Taking the text generation method provided in this application as an example in a question-and-answer scenario, assuming the trained large model is applied to an intelligent question-and-answer system that provides users with tax advice and answers to tax questions, the trained large model can understand user input and generate corresponding answers based on the user input, while avoiding the generation of unnecessary repetitive content. In contrast, related intelligent question-and-answer systems often repeatedly generate certain answers, such as: "I am not a tax expert, but I can provide some general advice."

[0172] In detail, during the training phase, this embodiment first collects a large amount of tax-related training data, such as tax laws, tax answers, and tax cases. Then, during training, a feedback mechanism unit is used to modify the loss function. Specifically, a penalty term Pe is introduced, which is proportional to the repetition rate R of the generated content. Thus, if the large model attempts to generate the same answer as previously generated content, the existence of this penalty term will result in a significant penalty, thereby incentivizing the large model to generate different answers.

[0173] Furthermore, during the inference phase, embodiments of this application generate answers using a dynamic deduplication decoding unit. For example, when a user inputs a question, "How should I claim reimbursement for my transportation expenses?", the large model will begin generating an answer. During the answer generation process, the large model remembers the content already generated and uses content repetition as a criterion, dynamically adjusting the generation probability of candidate words to prevent the large model from generating unnecessary repetitive content.

[0174] Figure 5 This is a schematic diagram of the structure of a text generation device provided in an embodiment of this application. See also... Figure 5 The device includes:

[0175] Training module 501 is configured to input training data into an initial language model during the training phase, and to encode and decode the training data sequentially based on the encoder and decoder of the initial language model to generate predicted text.

[0176] The training module 501 is further configured to construct a loss function based on the reference text and the predicted text of the training data; wherein the loss function includes a penalty term, the penalty term being related to a first penalty factor and the content repetition of the predicted text; the weight values ​​of the initial language model are updated through the loss function until the model convergence condition is met, thereby obtaining the target language model;

[0177] The generation module 502 is configured to, during the inference phase, adjust the generation probability of the predicted word based on a second penalty factor for the i-th position, in response to the content repetition of the word predicted by the decoder of the target language model being greater than a first threshold.

[0178] The generation module 502 is further configured to determine the generated word at the i-th position based on the adjusted generation probability, and output the determined generated word as the result; where i is a positive integer.

[0179] The text generation scheme provided in this application avoids the generation of unnecessary repetitive content by setting optimization strategies during the training and inference phases. Specifically, during the training phase, this application adds a penalty term to the loss function. This penalty term is related to a first penalty factor and the content repetition rate of the predicted text output by the large model. That is, if the large model attempts to generate unnecessary repetitive content, it will be significantly penalized due to this penalty term, thus incentivizing it to generate different content. During the inference phase, when outputting generated words, the large model remembers the already generated content and uses content repetition rate as a criterion to adjust the generation probability to prevent the large model from generating unnecessary repetitive content.

[0180] In summary, the embodiments of this application effectively solve the problem of generating repetitive content from large models, increase the diversity of generated content, and thus improve the quality of generated content.

[0181] In one possible implementation, the training module is configured as follows:

[0182] For the j-th generated word in the predicted text, obtain the relative position index of each generated word generated before the j-th generated word;

[0183] The content repetition degree of the j-th generated word is determined based on the relative position index and suppression factor of each generated word; where j is a positive integer.

[0184] For any generated word generated before the j-th generated word, in response to the fact that the generated word is repeated with the j-th generated word, the value of the inhibition factor is a first value;

[0185] In response to the fact that any generated word is not a repetition of the j-th generated word, the value of the inhibition factor is a second value.

[0186] In one possible implementation, the training module is configured as follows:

[0187] The penalty term is constructed based on the first penalty factor and the content repetition of each generated word in the predicted text; wherein the value of the first penalty factor ranges from 0 to 1.

[0188] In one possible implementation, the generation module is configured as follows:

[0189] Obtain the relative position index of each generated word generated before the predicted word;

[0190] The content repetition degree of the predicted words is determined based on the relative position index and suppression factor of each generated word;

[0191] The content repetition of the predicted words is normalized.

[0192] If the normalized content repetition rate is greater than the first threshold, the generation probability of the predicted word is adjusted based on the second penalty factor.

[0193] Wherein, for any generated word generated before the predicted word, in response to the fact that the generated word is repeated with the predicted word, the value of the suppression factor is a first value;

[0194] In response to the fact that any generated word does not repeat the predicted word, the suppression factor is set to a second value.

[0195] In one possible implementation, the generation module is configured as follows:

[0196] The product of the second penalty factor and the original generation probability of the predicted word is used as the adjusted generation probability of the predicted word; wherein the value of the second penalty factor ranges from 0 to 1.

[0197] In one possible implementation, the training module is configured as follows:

[0198] In response to the content repetition of the generated word being greater than the second threshold and lasting for at least the first round, the first penalty factor is increased; after at most the second round, in response to the reduction in the content repetition of the generated word not reaching the magnitude threshold, the first penalty factor is further increased and the increase this time is greater than the increase in the previous time.

[0199] In response to the content repetition of the generated word being less than a third threshold and lasting for at least the first round, the first penalty factor is reduced; after at most the second round, in response to the increase in the content repetition of the generated word not reaching the magnitude threshold, the first penalty factor is further reduced and the reduction magnitude this time is greater than the previous reduction magnitude.

[0200] In one possible implementation, the generation module is configured as follows:

[0201] In response to the fact that the content repetition of the generated word is greater than the second threshold and continues for at least the first round, the second penalty factor is reduced; after at most the second round, in response to the fact that the reduction in the content repetition of the generated word does not reach the magnitude threshold, the second penalty factor is further reduced and the reduction magnitude this time is greater than the reduction magnitude of the previous time;

[0202] In response to the content repetition of the generated word being less than the third threshold and lasting for at least the first round, the second penalty factor is increased; after at most the second round, in response to the increase in the content repetition of the generated word not reaching the magnitude threshold, the second penalty factor is further increased and the increase this time is greater than the increase in the previous time.

[0203] In one possible implementation, the device further includes:

[0204] The determining module is configured to determine the first round and the second round based on the total number of generated words and the total number of punctuation marks output by the decoder.

[0205] All of the above-mentioned optional technical solutions can be combined in any way to form the optional embodiments of this application, and will not be described in detail here.

[0206] It should be noted that the text generation device provided in the above embodiments is only illustrated by the division of the above functional modules. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the text generation device and the text generation method embodiments provided in the above embodiments belong to the same concept, and the specific implementation process can be found in the method embodiments, which will not be repeated here.

[0207] Figure 6 This is a schematic diagram of the structure of a computer device 600 provided in an embodiment of this application. The computer 600 can be a server. The computer device 600 can vary significantly due to differences in configuration or performance, and may include one or more Central Processing Units (CPUs) 601 and one or more memories 602. The memories 602 store at least one line of program code, which is loaded and executed by the processor 601 to implement the text generation methods provided in the various method embodiments described above. Of course, the computer device 600 may also have wired or wireless network interfaces, a keyboard, and input / output interfaces for input and output. The computer device 600 may also include other components for implementing device functions, which will not be elaborated upon here.

[0208] In an exemplary embodiment, a computer-readable storage medium is also provided, such as a memory including program code that can be executed by a processor in a computer device to perform the text generation method in the above embodiments. For example, the computer-readable storage medium may be a read-only memory (ROM), a random access memory (RAM), a compact disc read-only memory (CD-ROM), magnetic tape, floppy disk, and optical data storage device, etc.

[0209] In an exemplary embodiment, a computer program product or computer program is also provided, which includes computer program code stored in a computer-readable storage medium. A processor of a computer device reads the computer program code from the computer-readable storage medium and executes the computer program code, causing the computer device to perform the text generation method described above.

[0210] Those skilled in the art will understand that all or part of the steps of the above embodiments can be implemented by hardware or by a program instructing related hardware. The program can be stored in a computer-readable storage medium, such as a read-only memory, a disk, or an optical disk.

[0211] The above description is merely an optional embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.

Claims

1. A text generation method, characterized in that, The method includes: During the training phase, training data is input into an initial language model, and the encoder and decoder of the initial language model encode and decode the training data in sequence to generate predicted text. Based on the reference text and the predicted text of the training data, a loss function is constructed; wherein, the loss function includes a penalty term, which is related to a first penalty factor and the content repetition of the predicted text; the weight values ​​of the initial language model are updated through the loss function until the model convergence condition is met, and the target language model is obtained; During the inference phase, for the i-th position, the relative position indices of each generated word preceding the word predicted by the decoder of the target language model are obtained; based on the relative position indices of each generated word and a suppression factor, the content repetition degree of the predicted word is determined; wherein, for any generated word preceding the predicted word, in response to the generated word repeating the predicted word, the suppression factor is set to a first value; in response to the generated word not repeating the predicted word, the suppression factor is set to a second value. In response to the content repetition degree being greater than a first threshold, the generation probability of the predicted word is adjusted based on a second penalty factor; The generated word at the i-th position is determined based on the adjusted generation probability, and the determined generated word is output as the result; where i is a positive integer.

2. The method according to claim 1, characterized in that, During the training phase, the method further includes: For the j-th generated word in the predicted text, obtain the relative position index of each generated word generated before the j-th generated word; The content repetition degree of the j-th generated word is determined based on the relative position index and suppression factor of each generated word; where j is a positive integer. For any generated word generated before the j-th generated word, if the generated word is a repetition of the j-th generated word, the suppression factor is set to a first value; if the generated word is not a repetition of the j-th generated word, the suppression factor is set to a second value.

3. The method according to claim 2, characterized in that, During the training phase, the method further includes: The penalty term is constructed based on the first penalty factor and the content repetition of each generated word in the predicted text; The first penalty factor has a value range of 0 to 1.

4. The method according to claim 1, characterized in that, The step of adjusting the predicted word generation probability based on a second penalty factor in response to the content repetition degree exceeding a first threshold includes: The content repetition rate of the predicted words is normalized. If the normalized content repetition rate is greater than the first threshold, the generation probability of the predicted word is adjusted based on the second penalty factor.

5. The method according to claim 1, characterized in that, The adjustment of the predicted word generation probability based on the second penalty factor includes: The product of the second penalty factor and the original generation probability of the predicted word is used as the adjusted generation probability of the predicted word. The second penalty factor ranges from 0 to 1.

6. The method according to claim 1, characterized in that, During the training phase, the method further includes: In response to the content repetition of the generated word being greater than the second threshold and lasting for at least the first round, the first penalty factor is increased; after at most the second round, in response to the reduction in the content repetition of the generated word not reaching the magnitude threshold, the first penalty factor is further increased and the increase this time is greater than the increase in the previous time. In response to the content repetition of the generated word being less than a third threshold and lasting for at least the first round, the first penalty factor is reduced; after at most the second round, in response to the increase in the content repetition of the generated word not reaching the magnitude threshold, the first penalty factor is further reduced and the reduction magnitude this time is greater than the previous reduction magnitude.

7. The method according to claim 1, characterized in that, During the reasoning phase, the method further includes: In response to the fact that the content repetition of the generated word is greater than the second threshold and continues for at least the first round, the second penalty factor is reduced; after at most the second round, in response to the fact that the reduction in the content repetition of the generated word does not reach the magnitude threshold, the second penalty factor is further reduced and the reduction magnitude this time is greater than the reduction magnitude of the previous time; In response to the content repetition of the generated word being less than the third threshold and lasting for at least the first round, the second penalty factor is increased; after at most the second round, in response to the increase in the content repetition of the generated word not reaching the magnitude threshold, the second penalty factor is further increased and the increase this time is greater than the increase in the previous time.

8. The method according to claim 6 or 7, characterized in that, The method further includes: The first round and the second round are determined based on the total number of generated words and the total number of punctuation marks output by the decoder.

9. A text generation device, characterized in that, The device includes: The training module is configured to input training data into an initial language model during the training phase, and then encode and decode the training data sequentially based on the encoder and decoder of the initial language model to generate predicted text. The training module is further configured to construct a loss function based on the reference text and the predicted text of the training data; wherein, the loss function includes a penalty term, which is related to a first penalty factor and the content repetition of the predicted text; the weight values ​​of the initial language model are updated through the loss function until the model convergence condition is met, thereby obtaining the target language model; The generation module is configured to, during the inference phase, for the i-th position, obtain the relative position indices of each generated word preceding the word predicted by the decoder of the target language model; determine the content repetition degree of the predicted word based on the relative position indices of each generated word and a suppression factor; wherein, for any generated word preceding the predicted word, in response to the generated word repeating the predicted word, the suppression factor is set to a first value; in response to the generated word not repeating the predicted word, the suppression factor is set to a second value; and in response to the content repetition degree being greater than a first threshold, adjust the generation probability of the predicted word based on a second penalty factor. The generation module is further configured to determine the generated word at the i-th position based on the adjusted generation probability, and output the determined generated word as the result; where i is a positive integer.

10. A computer device, characterized in that, The device includes a processor and a memory, the memory storing at least one line of program code, the at least one line of program code being loaded and executed by the processor to implement the text generation method as claimed in any one of claims 1 to 8.

11. A computer-readable storage medium, characterized in that, The storage medium stores at least one piece of program code, which is loaded and executed by a processor to implement the text generation method as described in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Automatic text abstracting method and device based on global semantics, medium and equipment

    CN111563160A

  • Method for avoiding duplication of translation segments in machine translation

    CN113191165A