Diversity controllable text generation method and device based on diffusion model
By introducing a control information clustering model in natural language processing, encoding natural language prompts into potential instruction control categories, and through distributed sampling of the diffusion model, the problem of difficult-to-control diverse text generation in the existing technology is solved, and achieved The controllability and diversity of text improve the user experience.
Patent Information
- Application Number
- CN202411008772.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-26
- Publication Date
- 2024-09-27
- Estimated Expiration
- 2044-07-26
AI Technical Summary
Existing technology is difficult to achieve fine-grained control when generating diverse text, resulting in randomness in the text generation process that makes it difficult to ensure the controllability of the text.
The control information clustering model is introduced to cluster the natural language prompt codes in the latent space and convert them into potential instruction control information categories. By sampling the distribution of control information categories, the diffusion model is guided to generate text to ensure that the generated text is controllable. sex and diversity.
It achieves flexible control of complex control requirements, such as the realization of abstract concepts, opinions, and intentions, while ensuring the diversity and fluency of generated text, improving user experience.
Smart Images

Figure CN118551735B
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of natural language processing, and more specifically, relates to a method and device for generating diverse and controllable text based on a diffusion model. Background Art
[0002] The task of controllable text generation is to generate text that meets specific requirements or constraints based on given control conditions. These control conditions can be described in many forms, including keywords, key phrases, attribute labels, tables, etc. However, these control forms are difficult to express complex control requirements, such as abstract concepts, emotions, opinions, intentions, etc.
[0003] Taking the intelligent service field as an example, chatbots automatically generate appropriate answers based on user needs; personalized news generation needs to combine readers' interests, background knowledge, and reading preferences to generate news that users are interested in; creative writing services need to combine the context given by users to generate novel and diverse texts, while also meeting the writing style required by the author; automatic generation of software test cases needs to generate test cases with similar structures but diverse contents based on given user test cases for application in software testing. Control conditions in the form of natural language can express the complex control requirements in the above application scenarios. Therefore, the controllable text generation task controlled by natural language has important application value.
[0004] Diverse text refers to a set of texts that are semantically similar but have different sentence structures and vocabulary expressions under the same control conditions. Diverse text can make the generated content more attractive and innovative, and at the same time realize personalized and customized content generation in applications such as personalized recommendations and dialogue systems, thereby improving user experience. In order to solve the problem of diverse controllable text generation, some patent documents have been published in the prior art field. These methods can be mainly divided into diversity methods based on sampling in the decoding stage and diversity methods based on hidden state random variables.
[0005] The diversity method based on sampling in the decoding stage mainly introduces different sampling strategies in the decoding stage to ensure the vocabulary diversity of the generated text.
[0006] For example, the Chinese patent document (CN116050401A) uses spectral clustering and beam search decoding methods at the output of the decoder to automatically generate diversity. The Chinese patent document (CN115630640A) solves the problem of repeated generated text and improves text diversity by adding a penalty factor based on the diversity evaluation index to the probability corresponding to the target word in the dictionary during the decoding stage. However, the above-mentioned methods change the probability distribution of the output words during the decoding stage, which will reduce the fluency of the generated text and affect the quality of the generated text.
[0007] Diversity methods based on hidden state random variables use random methods such as variational autoencoders or diffusion models to achieve diversity in generated text using hidden state random variables.
[0008] For example, Chinese patent document (CN116629272A) introduces a text semantic hashing method and uses hash codes to represent natural language descriptive control variables. The invention introduces an additional conditional encoder to realize the control of the text generation process by the descriptive control variable, and uses the hidden state random variables of the variational autoencoder to realize the diversity of the generated text. Chinese patent document (CN116432663A) proposes a controllable diversity professional text generation method based on element diagrams. By constructing a conditional encoder to establish the association between viewpoints and semantic features, the control of text semantics by viewpoints is realized, and the diversity of text expression is realized by randomly sampling different expression features. Chinese patent document (CN115374271A) proposes a diversity text generation method based on deep LSTM by sampling different model parameters according to a specific distribution. Each parameter of the LSTM model is changed from a certain value to a set of distributions, so that the model has more uncertainty and diversity, and the diversity of generated text is realized by changing the model.
[0009] The paper "Diffusion-lm improves controllable text generation" proposes a non-autoregressive language model combined with a diffusion model. This method iteratively reduces Gaussian noise to word vectors and uses the randomness of the diffusion model noise reduction process to achieve diverse text generation.
[0010] However, the hidden states in the generation process of the above-mentioned methods are random, making it difficult to achieve fine-grained control and unable to ensure the controllability of the text while generating diverse texts. Summary of the invention
[0011] The present invention aims to overcome at least one defect of the above-mentioned prior art and provide a method for generating diverse and controllable text based on a diffusion model.
[0012] The present invention also provides a device for realizing the method for generating diverse controllable texts based on the diffusion model. Summary of the invention:
[0014] Aiming at the problem of diverse text generation under natural language control, the present invention combines cognitive science, introduces a control information clustering model, encodes natural language prompts in latent space and clusters them, transforms natural language control into potential instruction control information categories, and realizes diverse text generation that meets the semantic constraints of natural language prompts. The diversity of generated text is achieved by sampling the distribution of control information categories; the control variables are used to guide the generation process of the diffusion model, which ensures the controllability of the generated text.
[0015] Technical term explanation:
[0016] Task requirement: In this invention, it refers to the description of the task that needs to be completed by the generated model.
[0017] Natural language prompt: In the present invention, it refers to the use of natural language to describe the task requirements. For example, in the question answering task, the natural language prompt is "Answer the following question: How many points does a backgammon board have?".
[0018] Control variable: in the present invention, refers to the result of encoding the natural language prompt using an encoder.
[0019] Control information category: In the present invention, a Gaussian mixture model is used to cluster the control variables, and each category corresponds to control information with different tendencies, which is referred to as a control information category for short.
[0020] Diffusion Model: It is a generative network structure based on Markov chain inference, including diffusion process and reverse diffusion process. The present invention applies the diffusion model to the field of text generation, and gradually applies noise to the text encoding in the diffusion process until the text encoding is completely destroyed and becomes standard Gaussian noise. In the reverse diffusion process, a denoising network is trained to learn the process of restoring the original text encoding from Gaussian noise.
[0021] The detailed technical scheme of the present invention is as follows:
[0022] A method for generating diverse and controllable text based on a diffusion model, the method comprising:
[0023] S1. Constructing a diverse text generation model based on natural language control, wherein the diverse text generation model includes a natural language prompt encoder, a text reconstruction autoencoder, a control information clustering model and a latent space diffusion model;
[0024] S2. In the training phase, given a reference text And include task requirements and control requirements Natural language prompts , and the reference text and natural language prompts Inputting a diversity text generation model to train the diversity text generation model;
[0025] S3. In the application phase, given a natural language prompt , and the natural language prompt Inputting the trained diverse text generation model to iteratively generate diverse texts that meet corresponding controls;
[0026] Wherein, in the step S2, training the diversity text generation model specifically includes:
[0027] S21. Using the reference text Train the text reconstruction autoencoder to use the trained text reconstruction autoencoder to reconstruct the reference text Encode and encode the original text Mapped into a continuous latent space;
[0028] S22. According to the task requirements and natural language prompts The semantic information of , using contrastive learning to train the natural language prompt encoder, so as to use the trained natural language prompt encoder to Encode and obtain the control variable ; Wherein, the natural language prompt In the latent space, it is divided into categories of control information with similar controls;
[0029] S23, the control variable Input the control information clustering model and use the EM algorithm to obtain the control variables Corresponding natural language prompts Control information category , to control information from the category The corresponding control information is sampled from the distribution of ;
[0030] S24, reconstructing the text into the original text encoding output by the encoder As the hidden state at the initial moment of the latent space diffusion model, a forward diffusion process and a backward reverse diffusion process are constructed, and the original text is encoded step by step in the forward diffusion process. Add standard Gaussian noise and we get Text encoding of the time ; Using the control variable in the backward diffusion process , control information as well as Text encoding of the time The denoising network is trained to iteratively generate diverse texts that meet the corresponding controls using the trained denoising network.
[0031] Preferably, according to the present invention, in step S21, the text reconstruction autoencoder after training is used to reconstruct the reference text. Encode and encode the original text Mapped into a continuous latent space, specifically:
[0032] (1);
[0033] In formula (1): Indicates the original text encoding; Indicates hidden space; Indicates the length of the text sentence; represents the dimension of the latent space; Reconstructing autoencoders for text; Indicates reference text.
[0034] Preferably, according to the present invention, in step S22, the natural language prompt encoder is used to encode the natural language prompt. Encode and obtain the control variable , specifically:
[0035] (3);
[0036] In formula (3): represents the control variable; Prompt encoder for natural language; Represents a natural language prompt.
[0037] Preferably, according to the present invention, in step S22, the loss function used to train the natural language prompt encoder includes a reconstruction loss function and a contrast loss function, namely:
[0038] (4);
[0039] In formula (4): represents the loss of the natural language cue encoder; represents the reconstruction loss; represents contrast loss; represents the real prompt encoding, that is, the control variable corresponding to the real natural language prompt; represents the reconstructed prompt encoding, that is, the control variable corresponding to the reconstructed natural language prompt; represents the currently selected anchor sample, represents the positive sample selected based on the anchor sample, Indicates the number of samples selected based on the anchor sample. negative samples;
[0040] Among them, the reconstruction loss function uses the cross entropy loss function, that is:
[0041] (5);
[0042] In formula (5), The first dimensions, The first Dimensions;
[0043] Use n-pair loss as the contrast loss function, that is:
[0044] (6);
[0045] In formula (6): represents the exponential function; represents the transpose of the anchor sample encoding; Indicates Negative sample encoding; Represents the positive sample encoding.
[0046] According to a preferred embodiment of the present invention, in step S23, the control variable is obtained using the EM algorithm. Corresponding natural language prompts Control information category , to control information from the category The corresponding control information is sampled from the distribution of , specifically including:
[0047] E-step: According to the parameters of the current control information clustering model, calculate the control variable corresponding to each natural language prompt belongs to the The degree of membership of the control information category is:
[0048] (7);
[0049] In formula (7): Indicates Natural language prompts The corresponding control variable Belong to The degree of membership of a control information category; Indicates Natural language prompts The corresponding control variable Belong to The probability of controlling the information category, That means The mean and variance of the distribution of the control information categories; Indicates the total number of control information categories, Indicates Control information categories; represents the total number of natural language prompts;
[0050] Select the control information category with the highest probability as the natural language prompt Control information category ,Right now:
[0051] (8);
[0052] M-step: Use the membership calculated by E-step to update the parameters of the control information clustering model, namely:
[0053] (9);
[0054] (10);
[0055] In formulas (9) and (10): Indicates The mean of the control information categories; Indicates The variance of the control information category; Indicates that the natural language prompt belongs to The number of control information categories, Indicates the number of natural language prompts. represents the total number of natural language prompts; Indicates that each natural language prompt belongs to The sum of the probabilities of the control information categories; For the matrix The transpose of
[0056] Repeat the above E-step and M-step until the convergence condition is reached until, Indicates The parameters of the iterations, Is a positive number.
[0057] According to a preferred embodiment of the present invention, in step S24, during the forward diffusion process, the original text encoding is gradually transferred to the original text encoding. Add standard Gaussian noise and we get Text encoding of the time , specifically:
[0058] (14);
[0059] In formula (14): represents the distribution of the diffusion process, Represents the original text encoding, that is, the hidden state at the initial moment; express The text encoding of the moment is The hidden state of the moment; represents Gaussian distribution; represents a matrix of all 1s; is a hyperparameter, and , Indicates The noise ratio at each moment, the value range is between 0-1;
[0060] In the backward diffusion process, the regression loss is used to denoise the network. For training, we have:
[0061] (18);
[0062] In formula (18): represents the regression loss; express expectations; Indicates A natural language prompt for the control information category.
[0063] According to the preferred embodiment of the present invention, in step S3, in the application stage, the trained denoising network is used. Iteratively generate diverse texts that meet control requirements, namely:
[0064] (20);
[0065] Among them, different variances are used at different times , to control the randomness of the sampling process, that is:
[0066] (twenty one);
[0067] In formula (21): is a hyperparameter, Represents a real number greater than or equal to 0.
[0068] In another aspect of the present invention, a device for implementing a method for generating diverse controllable text based on a diffusion model is provided, the device comprising:
[0069] A construction module is used to construct a diverse text generation model based on natural language control, wherein the diverse text generation model includes a natural language prompt encoder, a text reconstruction autoencoder, a control information clustering model, and a latent space diffusion model;
[0070] Training module, used to train And include task requirements and control requirements Natural language prompts , and the reference text and natural language prompts Inputting a diversity text generation model to train the diversity text generation model;
[0071] Execution module, used to execute actions based on given natural language prompts , and the natural language prompt Inputting the trained diverse text generation model to iteratively generate diverse texts that meet corresponding controls;
[0072] The training of the diversity text generation model specifically includes:
[0073] S21. Using the reference text Train the text reconstruction autoencoder to use the trained text reconstruction autoencoder to reconstruct the reference text Encode and encode the original text Mapped into a continuous latent space;
[0074] S22. According to the task requirements and natural language prompts The semantic information of , using contrastive learning to train the natural language prompt encoder, so as to use the trained natural language prompt encoder to Encode and obtain the control variable ; Wherein, the natural language prompt In the latent space, it is divided into categories of control information with similar controls;
[0075] S23, the control variable Input the control information clustering model and use the EM algorithm to obtain the control variables Corresponding natural language prompts Control information category , to control information from the category The corresponding control information is sampled from the distribution of ;
[0076] S24, reconstructing the text into the original text encoding output by the encoder As the hidden state at the initial moment of the latent space diffusion model, a forward diffusion process and a backward reverse diffusion process are constructed, and the original text is encoded step by step in the forward diffusion process. Add standard Gaussian noise and we get Text encoding of the time ; Using the control variable in the backward diffusion process , control information as well as Text encoding of the time The denoising network is trained to iteratively generate diverse texts that meet the corresponding controls using the trained denoising network.
[0077] In another aspect of the present invention, there is also provided an electronic device, comprising:
[0078] at least one processor; and
[0079] A memory storing instructions, which, when executed by the at least one processor, enables the at least one processor to execute the diversity controllable text generation method based on the diffusion model as described above.
[0080] In another aspect of the present invention, a machine-readable storage medium is provided, which stores executable instructions, and when the instructions are executed, the machine executes the diversity controllable text generation method based on the diffusion model as described above.
[0081] Compared with the prior art, the present invention has the following beneficial effects:
[0082] (1) Compared with the existing methods, the method for generating diverse and controllable text based on the diffusion model provided by the present invention uses natural language instructions as control information to achieve complex control such as abstract concepts, viewpoints, and intentions. At the same time, natural language prompts optimize the user experience, and users can use natural language to flexibly change control conditions and achieve flexible control of the text generation model.
[0083] (2) The present invention introduces a control information clustering model to ensure the controllability of the generated text. Inspired by cognitive science, hundreds of individuals have similar ideas, although they express them differently. The present invention encodes natural language prompts and clusters them in latent space, converts natural language control into potential instruction control categories, samples control information according to the prompt control category distribution, guides the text generation process, and realizes the control of the generated text.
[0084] (3) The present invention introduces diverse control information to ensure the diversity of generated text. By sampling the distribution of control information categories, diverse control information for the same natural language prompt is sampled, guiding the generation process of the diffusion model, and achieving diverse text generation. BRIEF DESCRIPTION OF THE DRAWINGS
[0085] Figure 1 It is an architectural diagram of the diversity controllable text generation method based on the diffusion model described in the present invention.
[0086] Figure 2 It is a schematic diagram of the process of fine-tuning the natural language prompt encoder in the present invention.
[0087] Figure 3 It is a schematic flow chart of the application phase of the method of the present invention. DETAILED DESCRIPTION
[0088] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.
[0089] It should be noted that the following detailed descriptions are exemplary and are intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meanings as those commonly understood by those skilled in the art to which the present invention belongs.
[0090] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is also intended to include the plural form. In addition, it should be understood that when the terms "comprising" and / or "including" are used in this specification, it indicates the presence of features, steps, operations, devices, components and / or combinations thereof.
[0091] In the absence of conflict, the embodiments of the present invention and the features of the embodiments may be combined with each other.
[0092] In view of the defects of the prior art, the present invention proposes a controllable diversity text generation method based on a diffusion model, which includes a model training stage and an application stage.
[0093] In the training phase, given the task requirements and control requirements Natural language prompts , encoded using the natural language prompt encoder, and obtained the control variable ; The control information clustering model can capture the different natural language prompts constructed The commonality of potential control in the ,distinguishes different prompt control categories from the perspective of distribution.,The control information is obtained by sampling the Gaussian distribution of potential control categories. , as the control information of the latent space diffusion model.
[0094] At the same time, the text reconstruction autoencoder is used to reconstruct the given reference text. Encode and encode the original text As the hidden state of the latent space diffusion model at the initial moment. The latent space diffusion model constructs a diffusion process in the text encoding space and uses the data generated in the diffusion process as the supervision data for training the denoising network in the inverse diffusion process; using the control variable and control information Together they serve as control information in the latent space, guiding the inverse diffusion process and training the denoising network.
[0095] In the application phase, given a natural language prompt , use the natural language prompt encoder to encode it and get the control variable ; Use the control information clustering model to obtain control information Then, we sample from the text prior distribution as Finally, the control information and control variables As the input of the latent space diffusion model, the trained denoising network is used to gradually generate diverse texts that meet the corresponding controls.
[0096] The following is a detailed description of the controllable diversity text generation method and device based on the diffusion model of the invention in conjunction with specific embodiments.
[0097] Embodiment 1,
[0098] This embodiment provides a method for generating controllable diversity text based on a diffusion model, the method comprising:
[0099] S1. Construct a diverse text generation model based on natural language control, wherein the diverse text generation model includes a natural language prompt encoder, a text reconstruction autoencoder, a control information clustering model and a latent space diffusion model.
[0100] Specific reference Figure 1The diverse text generation model based on natural language control designed in this embodiment includes four parts: natural language prompt encoder, text reconstruction autoencoder, control information clustering model and latent space diffusion model. The natural language prompt encoder is used to encode natural language prompts to better reflect the commonalities and differences between control information; the text reconstruction autoencoder can accurately map text encodings into corresponding texts; the control information clustering model is used to model the different distributions of control variables of different control types and convert natural language prompts into control information; the latent space diffusion model is used to take control information and control variables as input, guide the diffusion process, and generate diverse texts that meet control requirements.
[0101] Preferably, during the implementation, the text reconstruction autoencoder, the natural language prompt encoder and the latent space diffusion model all use a neural network structure, and the control information clustering model uses a Gaussian mixture model and uses the EM algorithm to update and iterate.
[0102] S2. In the training phase, given a reference text And include task requirements and control requirements Natural language prompts , and the reference text and natural language prompts A diversity text generation model is input to train the diversity text generation model.
[0103] In the step S2, training the diversity text generation model specifically includes:
[0104] S21. Using the reference text Train the text reconstruction autoencoder to use the trained text reconstruction autoencoder to reconstruct the reference text Encode and encode the original text Mapped into a continuous latent space.
[0105] In this embodiment, an unsupervised method is used to fine-tune the training text reconstruction autoencoder. The training goal of the text reconstruction autoencoder is to accurately encode and decode the text, and it is preferably trained using an unsupervised reconstruction method.
[0106] The training input of the text reconstruction autoencoder is the reference text , the output is the reconstructed text for the input text The training target is the output reconstructed text Reconstruct the input reference text as much as possible .
[0107] This process is completed by the text reconstruction autoencoder. The BART model is preferably used as the basic model of the text reconstruction autoencoder, and the cross entropy reconstruction loss function is used for fine-tuning training. The cross entropy reconstruction loss function is the same as the reconstruction loss function used in the natural language prompt encoder training process below, that is, formula (5). Then, the trained text reconstruction autoencoder is used Reference text Encode and encode the original text Mapped into a continuous latent space, we have:
[0108] (1);
[0109] In formula (1): Indicates the original text encoding; Indicates hidden space; Indicates the length of the text sentence; represents the dimension of the latent space; Reconstructing autoencoders for text; Indicates reference text.
[0110] S22. According to the task requirements and natural language prompts The semantic information of , using contrastive learning to train the natural language prompt encoder, so as to use the trained natural language prompt encoder to Encode and obtain the control variable ; Wherein, the natural language prompt In the latent space, it is divided into categories of control information with similar controls.
[0111] Specifically, for a given task requirement and control requirements , design natural language prompt templates, construct natural language prompts The natural language prompt It organizes task requirements and original sentence information in the form of natural language that can be understood by humans, including the task requirements of the generated task. and control requirements Two parts, namely:
[0112] (2);
[0113] In formula (2): natural language cues that represent constructs; Indicates task requirements; Indicates control needs; Is the interval symbol.
[0114] For different generation tasks, task requirements There are different ways of construction.
[0115] For example, taking the comment generation task as an example, the task requirements of this task are =[Generate comment with the information:]. For the synonym rewriting task, the task requirements =[Generate paraphrase of the following sentence:].
[0116] Control requirements Provide the corresponding information expected to be contained in the generated text. For example, in the comment generation task, the control requirement It contains the name of the restaurant being reviewed, the restaurant rating, the consumption level, the restaurant location, and other information. In the synonymous rewriting task, the control demand This is the text that needs to be rewritten.
[0117] Specific reference Figure 2 In this embodiment, according to the given task requirements and natural language prompts The semantic information of the constructed natural language prompts is used to select positive and negative samples, namely anchor samples. , where each natural language prompt is preferred Select a positive sample and Negative samples , a contrastive learning approach is used to fine-tune the natural language prompt encoder.
[0118] The goal of fine-tuning the natural language prompt encoder is to capture different natural language prompts The potential commonalities in the data can bring the control variables with similar controls closer together and distance the control variables with different controls further apart.
[0119] The natural language prompt encoder It is preferred to use the BART model as the basic model, and then use the trained natural language prompt encoder to input natural language prompts Encoding to obtain control variables , that is:
[0120] (3);
[0121] In formula (3): represents the control variable; Prompt encoder for natural language; Represents a natural language prompt.
[0122] Furthermore, the loss function for fine-tuning the natural language prompt encoder consists of two parts, namely the reconstruction loss function and the contrast loss function, namely:
[0123] (4);
[0124] In formula (4): represents the loss of the natural language cue encoder; represents the reconstruction loss; represents contrast loss; represents the real prompt encoding, that is, the control variable corresponding to the real natural language prompt; represents the reconstructed prompt encoding, that is, the control variable corresponding to the reconstructed natural language prompt; represents the currently selected anchor sample, represents the positive sample selected based on the anchor sample, Indicates the number of samples selected based on the anchor sample. Negative samples.
[0125] The reconstruction loss function can make natural language prompts After being encoded, the semantic knowledge is retained as much as possible. The reconstruction loss function uses the cross entropy loss function, that is:
[0126] (5);
[0127] In formula (5), The first dimensions, The first Dimensions;
[0128] The contrast loss function can differentiate the control variables of different categories of control. Here, n-pair loss is preferably used as the contrast loss function, that is:
[0129] (6);
[0130] In formula (6): represents the exponential function; represents the transpose of the anchor sample encoding; Indicates Negative sample encoding; Represents the positive sample encoding.
[0131] Among them, negative samples are divided into intra-class negative samples and inter-class negative samples. The task requirements of intra-class negative samples are Same but control requirements Semantic differences are large; task requirements for negative samples between classes different.
[0132] S23, the control variable Input the control information clustering model and use the EM algorithm to obtain the control variables Corresponding natural language prompts Control information category , to control information from the category The corresponding control information is sampled from the distribution of .
[0133] The control information clustering model is used to model natural language prompts, capture the potential control commonalities in natural language prompts, and make natural language prompts with similar controls belong to the same control category.
[0134] In this embodiment, the control information clustering model adopts a Gaussian mixture model.
[0135] Based on cognitive science, it is assumed that natural language cues can be divided into The control information categories with similar controls are distributed in Gaussian distributions that are independent of each other, and each Gaussian distribution represents a control information category. A small number of natural language prompts are randomly selected, and a control information clustering model is established using these natural language prompts.
[0136] Specifically, the control information clustering model is first initialized. It is known that natural language prompts can be divided into control information categories with similar controls, then the randomly generated Group mean and variance , which can be regarded as The initial distribution of potential natural language control instructions. Represents natural language prompts The corresponding control variable Belong to The probability of controlling the information category, That is The mean and variance of the distribution of the control information categories.
[0137] For the Gaussian mixture model, since each control information category has unknown parameters , , it is not possible to use the maximum likelihood method to derive the calculation, and it is necessary to use the EM algorithm to iteratively solve the mean and variance of each control information category. The specific steps are as follows:
[0138] E-step: According to the parameters of the current control information clustering model, calculate the control variable corresponding to each natural language prompt belongs to the The degree of membership of the control information category is:
[0139] (7);
[0140] In formula (7): Indicates Natural language prompts The corresponding control variable Belong to The degree of membership of a control information category; Indicates Natural language prompts The corresponding control variable Belong to The probability of controlling the information category, That means The mean and variance of the distribution of the control information categories; Indicates the total number of control information categories, Indicates Control information categories; Represents the total number of natural language prompts.
[0141] Select the control information category with the highest probability as the natural language prompt Control information category ,Right now:
[0142] (8).
[0143] M-step: Use the membership calculated by E-step to update the parameters of the control information clustering model, namely:
[0144] (9);
[0145] (10);
[0146] In formulas (9) and (10): Indicates The mean of the control information categories; Indicates The variance of the control information category; Indicates that the natural language prompt belongs to The number of control information categories, Indicates the number of natural language prompts. represents the total number of natural language prompts; Indicates that each natural language prompt belongs to The sum of the probabilities of the control information categories; For the matrix The transpose of
[0147] Repeat the above E-step and M-step until the convergence condition is reached until, Indicates The parameters of the iterations, is a very small positive number.
[0148] It should be understood that in the above process, the new control variables corresponding to the new natural language prompts other than the randomly selected small number of natural language prompts are , calculate the likelihood probability that the natural language prompt corresponding to the new control variable belongs to each control information category, and select the control information category with the largest likelihood probability as the control information category of the prompt , that is:
[0149] (11);
[0150] In formula (11): Represents the new control variable The corresponding natural language prompt belongs to The probability of controlling the information category.
[0151] Finally, in the control information category of obtaining natural language prompts Then, the control information of the natural language prompt is obtained by sampling based on the mean and variance of the category distribution. , guiding the latent space diffusion model to generate text that meets the corresponding control. For the same natural language prompt, different control information can be generated through multiple samplings to ensure the diversity of generated text.
[0152] S24, reconstructing the text into the original text encoding output by the encoder As the hidden state at the initial moment of the latent space diffusion model, a forward diffusion process and a backward reverse diffusion process are constructed, and the original text is encoded step by step in the forward diffusion process. Add standard Gaussian noise and we get Text encoding of the time ; Using the control variable in the backward diffusion process , control information as well as Text encoding of the time The denoising network is trained to iteratively generate diverse texts that meet the corresponding controls using the trained denoising network.
[0153] The implementation of this step is to construct a diffusion model in the latent space and introduce control variables to guide text generation.
[0154] The latent space diffusion model encodes the original text As the hidden state at the initial moment, the forward diffusion process and the backward reverse diffusion process are constructed on this basis.
[0155] The forward diffusion process is a parameter-free noise adding process. By gradually adding standard Gaussian noise to the initial hidden state, the intermediate hidden state is constructed. Until completion Step diffusion The noise of the hidden state at each moment is close to standard Gaussian noise. The dimension of the hidden state at each moment is the same as the dimension of the text encoding. is a Markov chain.
[0156] from The hidden state at time The process of the hidden state at the moment can be parameterized as follows:
[0157] (12);
[0158] (13);
[0159] In formulas (12) and (13): Represents the original text encoding, that is, the hidden state at the initial moment; express The text encoding of the moment is The hidden state of the moment; for The text encoding of the moment is The hidden state of the moment; represents the intermediate hidden state; represents a Gaussian distribution; hyperparameter Indicates that it is spreading The amount of noise that is increasing at any moment; Indicates continuous multiplication, represents a matrix of all ones.
[0160] The hidden state at any time can be encoded based on the original text , hyperparameters and time It can be directly sampled from the distribution of , without iterating step by step according to formula (11), that is:
[0161] (14);
[0162] In formula (14): represents the distribution of the diffusion process, Represents the original text encoding, that is, the hidden state at the initial moment; express The text encoding of the moment is The hidden state of the moment; represents Gaussian distribution; represents a matrix of all 1s; is a hyperparameter, and , Indicates The noise ratio at each moment, the value range is between 0 and 1.
[0163] The purpose of the backward inverse diffusion process is to gradually remove the noise in the hidden state at each moment of the diffusion process and gradually restore the original text encoding. The backward inverse diffusion process is to recover the original distribution information from Gaussian noise. Assuming that the backward inverse diffusion process is also a Gaussian distribution, but it is impossible to fit the distribution step by step, then it is necessary to construct a parameter distribution for estimation, that is,
[0164] (15);
[0165] (16);
[0166] (17);
[0167] In formulas (15) to (17): and Gaussian distribution representing the inverse diffusion process The mean and variance of the moment; is a hyperparameter, and .
[0168] Furthermore, in the backward inverse diffusion process, in this embodiment, a denoising network is constructed and trained. , predict the initial state of the text from the hidden state containing noise Given Text encoding of the time and denoising network Output , we can sample Text encoding of the time .
[0169] In order to realize controllable text generation, in this embodiment, the control variable and the control information sampled from the control variable As a latent space hint, integrated into the denoising network The training process. Given time ,available Text encoding of the time , combined with the control variables and control information , then , which can be used to predict the original text encoding .
[0170] In this embodiment, regression loss is used to denoise the network. For training, we have:
[0171] (18);
[0172] In formula (18): represents the regression loss; express expectations; Indicates A natural language prompt for the control information category.
[0173] Finally, use the trained denoising network , the distribution of the reverse diffusion process can be Defined as the diffusion process distribution in the forward process The inverse process uses the original text encoding predicted by the inverse diffusion model and Text encoding of the time ,from Sampling Text encoding of the time , that is:
[0174] (19).
[0175] Thus, using the control variable and control information Together they serve as control information of the latent space, guide the inverse diffusion process, and train the denoising network. In the application stage, the trained denoising network can be used to gradually and iteratively generate diverse texts that meet the corresponding control.
[0176] S3. In the application phase, given a natural language prompt , and the natural language prompt The trained diversity text generation model is input to gradually and iteratively generate diversity text that complies with the corresponding control.
[0177] After the training of the above diverse text generation model based on natural language control is completed, given a natural language prompt ,Using the trained natural language cue encoder, control information clustering model and latent space diffusion model, we generate control-compliant diverse text.
[0178] Specific reference Figure 3 , given a natural language prompt , use the trained natural language prompt encoder to encode it and get the control variable , and then the obtained control variable Input the trained control information clustering model to obtain the natural language prompt The control information category , and then according to the control information category The distribution sampling obtains the control information .
[0179] For the latent space diffusion model, we sample from a standard Gaussian distribution as The hidden state at the moment. According to the control variable and the sampled control information The latent space prompts composed of The following steps are used to generate diverse texts that meet the control requirements, namely:
[0180] (20);
[0181] Among them, different variances are used at different times , to control the randomness of the sampling process, that is:
[0182] (twenty one);
[0183] In formula (21): is a hyperparameter, Represents a real number greater than or equal to 0.
[0184] The following is an example of applying the method of this embodiment to a specific scenario:
[0185] In the three common network text generation problems of comment generation, question answering and synonym rewriting, due to the diversity of intelligent service users, it is necessary to generate corresponding diverse texts to meet the needs of different users and improve the user experience. In this scenario, this embodiment generates a set of diverse texts that meet the control for the same natural language prompt through a diverse text generation method based on natural language prompts.
[0186] In the above example, the natural language prompt contains task requirements and control requirements. For example, for the review generation task, the task requirement =[Generate comment with the information], control requirements To review the restaurant's name, rating, consumption level, location and other information.
[0187] The training process of the above application example includes two stages: pre-training and training diffusion model. In the pre-training stage, the text reconstruction autoencoder, natural language prompt encoder and control information clustering model are mainly pre-trained.
[0188] The text reconstruction autoencoder uses BART as the base model, and the input during pre-training is the reference text in the training data. , the output is the reconstructed text , the cross entropy reconstruction loss function used has been given in the above description.
[0189] The natural language prompt encoder also uses BART as the basic model. In the pre-training stage, the input is the natural language prompt with constructed positive and negative sample pairs, and the output is the control variable. The loss functions used are the reconstruction loss function and the contrast loss function, which have been given in the above description.
[0190] Among them, positive and negative samples are based on task requirements and control requirements In this embodiment, natural language prompts with the same task requirements and similar control requirements as the anchor sample are selected as positive samples. Negative samples are divided into intra-class negative samples and inter-class negative samples. The task requirements of intra-class negative samples are the same, but the control requirements have large semantic differences, while inter-class negative samples require different task requirements. For one anchor sample, 1 positive sample, 10 intra-class negative samples, and 20 inter-class negative samples are selected in this application example.
[0191] After the above pre-training process is completed, the latent space diffusion model training is performed: the reference text is encoded using the pre-trained text reconstruction autoencoder, and the original text encoding is used as the initial hidden state to construct the diffusion process. The natural language prompt is encoded using the pre-trained natural language prompt encoder, and the control information category to which the prompt belongs is obtained through the control information clustering model, and the control information is sampled; the control variables and the sampled control information are provided as input to the denoising network in the inverse diffusion process, and the loss function used is given in the above description.
[0192] After the entire training process is completed, given a natural language prompt, the control information category to which the prompt belongs is determined based on the control variable and the control information is sampled. Random sampling from standard Gaussian noise is used as The hidden state at each moment uses the trained denoising network to iteratively generate diverse text that meets the control conditions.
[0193] Embodiment 2,
[0194] This embodiment provides a device for implementing a method for generating diverse controllable text based on a diffusion model, the device comprising:
[0195] A construction module is used to construct a diverse text generation model based on natural language control, wherein the diverse text generation model includes a natural language prompt encoder, a text reconstruction autoencoder, a control information clustering model, and a latent space diffusion model;
[0196] Training module, used to train And include task requirements and control requirements Natural language prompts , and the reference text and natural language prompts Inputting a diversity text generation model to train the diversity text generation model;
[0197] Execution module, used to execute actions based on given natural language prompts , and the natural language prompt Inputting the trained diverse text generation model to iteratively generate diverse texts that meet corresponding controls;
[0198] The training of the diversity text generation model specifically includes:
[0199] S21. Using the reference text Train the text reconstruction autoencoder to use the trained text reconstruction autoencoder to reconstruct the reference text Encode and encode the original text Mapped into a continuous latent space;
[0200] S22. According to the task requirements and natural language prompts The semantic information of , using contrastive learning to train the natural language prompt encoder, so as to use the trained natural language prompt encoder to Encode and obtain the control variable ; Wherein, the natural language prompt In the latent space, it is divided into categories of control information with similar controls;
[0201] S23, the control variable Input the control information clustering model and use the EM algorithm to obtain the control variables Corresponding natural language prompts Control information category , to control information from the category The corresponding control information is sampled from the distribution of ;
[0202] S24, reconstructing the text into the original text encoding output by the encoder As the hidden state at the initial moment of the latent space diffusion model, a forward diffusion process and a backward reverse diffusion process are constructed, and the original text is encoded step by step in the forward diffusion process. Add standard Gaussian noise and we get Text encoding of the time ; Using the control variable in the backward diffusion process , control information as well as Text encoding of the time The denoising network is trained to iteratively generate diverse texts that meet the corresponding controls using the trained denoising network.
[0203] Embodiment 3,
[0204] This embodiment also provides an electronic device, including:
[0205] at least one processor; and
[0206] A memory storing instructions, which, when executed by the at least one processor, enables the at least one processor to execute the diversity controllable text generation method based on the diffusion model as described above.
[0207] In this embodiment, the electronic device may include, but is not limited to: personal computers, server computers, workstations, desktop computers, laptop computers, notebook computers, mobile computing devices, smart phones, tablet computers, cellular phones, personal digital assistants (PDAs), handheld devices, messaging devices, wearable computing devices, consumer electronic devices, and the like.
[0208] Embodiment 4,
[0209] This embodiment also provides a machine-readable storage medium storing executable instructions, which, when executed, enable the machine to execute the above-mentioned method for generating diverse controllable text based on the diffusion model.
[0210] Specifically, a system or device equipped with a readable storage medium can be provided, on which software program codes that implement the functions of any of the above-mentioned embodiments are stored, and a computer or processor of the system or device can read and execute instructions stored in the readable storage medium.
[0211] In this case, the program code itself read from the machine-readable medium can realize the function of any one of the above embodiments, and thus the machine-readable code and the machine-readable storage medium storing the machine-readable code constitute part of this specification.
[0212] Examples of readable storage media include floppy disks, hard disks, magneto-optical disks, optical disks (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD-RW), magnetic tapes, non-volatile memory cards, and ROMs. Optionally, the program code may be downloaded from a server computer or a cloud via a communication network.
[0213] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0214] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0215] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0216] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0217] Obviously, the above embodiments of the present invention are merely examples for clearly illustrating the technical solution of the present invention, and are not intended to limit the specific implementation methods of the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the claims of the present invention shall be included in the protection scope of the claims of the present invention.
Claims
1. A method for generating diverse and controllable text based on a diffusion model, characterized in that: The method comprises: S1. Constructing a diverse text generation model based on natural language control, wherein the diverse text generation model includes a natural language prompt encoder, a text reconstruction autoencoder, a control information clustering model and a latent space diffusion model; S2. In the training phase, given a reference text And include task requirements and control requirements Natural language prompts , and the reference text and natural language prompts Inputting a diversity text generation model to train the diversity text generation model; S3. In the application phase, given a natural language prompt , and the natural language prompt Inputting the trained diverse text generation model to iteratively generate diverse texts that meet corresponding controls; Wherein, in the step S2, training the diversity text generation model specifically includes: S21. Using the reference text Train the text reconstruction autoencoder to use the trained text reconstruction autoencoder to reconstruct the reference text Encode and encode the original text Mapped into a continuous latent space; S22. According to the task requirements and natural language prompts The semantic information of , using contrastive learning to train the natural language prompt encoder, so as to use the trained natural language prompt encoder to Encode and obtain the control variable ; Wherein, the natural language prompt In the latent space, it is divided into categories of control information with similar controls; S23, the control variable Input the control information clustering model and use the EM algorithm to obtain the control variables Corresponding natural language prompts Control information category , to control information from the category The corresponding control information is sampled from the distribution of ; S24, reconstructing the text into the original text encoding output by the encoder As the hidden state at the initial moment of the latent space diffusion model, a forward diffusion process and a backward reverse diffusion process are constructed, and the original text is encoded step by step in the forward diffusion process. Add standard Gaussian noise and we get Text encoding of the time ; Using the control variable in the backward diffusion process , control information as well as Text encoding of the time The denoising network is trained to iteratively generate diverse texts that meet the corresponding controls using the trained denoising network.
2. The method for generating diverse controllable text based on a diffusion model according to claim 1, characterized in that: In step S21, the trained text reconstruction autoencoder is used to reconstruct the reference text. Encode and encode the original text Mapped into a continuous latent space, specifically: (1); In formula (1): Indicates the original text encoding; Indicates hidden space; Indicates the length of the text sentence; represents the dimension of the latent space; Reconstructing autoencoders for text; Indicates reference text.
3. The method for generating diverse controllable text based on a diffusion model according to claim 1, characterized in that: In step S22, the natural language prompt encoder is used to perform the natural language prompt Encode and obtain the control variable , specifically: (3); In formula (3): represents the control variable; Prompt encoder for natural language; Represents a natural language prompt.
4. The method for generating diverse controllable text based on a diffusion model according to claim 1, characterized in that: In step S22, the loss function used to train the natural language prompt encoder includes a reconstruction loss function and a contrast loss function, namely: (4); In formula (4): represents the loss of the natural language cue encoder; represents the reconstruction loss; represents contrast loss; represents the real prompt encoding, that is, the control variable corresponding to the real natural language prompt; represents the reconstructed prompt encoding, that is, the control variable corresponding to the reconstructed natural language prompt; represents the currently selected anchor sample, represents the positive sample selected based on the anchor sample, Indicates the number of samples selected based on the anchor sample. negative samples; Among them, the reconstruction loss function uses the cross entropy loss function, that is: (5); In formula (5), The first dimensions, The first Dimensions; Use n-pair loss as the contrast loss function, that is: (6); In formula (6): represents the exponential function; represents the transpose of the anchor sample encoding; Indicates Negative sample encoding; Represents the positive sample encoding.
5. The method for generating diverse controllable text based on a diffusion model according to claim 1, characterized in that: In step S23, the control variable is obtained using the EM algorithm. Corresponding natural language prompts Control information category , to control information from the category The corresponding control information is sampled from the distribution of , specifically including: E-step: According to the parameters of the current control information clustering model, calculate the control variable corresponding to each natural language prompt belongs to the The degree of membership of the control information category is: (7); In formula (7): Indicates Natural language prompts The corresponding control variable Belong to The degree of membership of a control information category; Indicates Natural language prompts The corresponding control variable Belong to The probability of controlling the information category, That means The mean and variance of the distribution of the control information categories; Indicates the total number of control information categories, Indicates Control information categories; represents the total number of natural language prompts; Select the control information category with the highest probability as the natural language prompt Control information category ,Right now: (8); M-step: Use the membership calculated by E-step to update the parameters of the control information clustering model, namely: (9); (10); In formulas (9) and (10): Indicates The mean of the control information categories; Indicates The variance of the control information category; Indicates that the natural language prompt belongs to The number of control information categories, Indicates the number of natural language prompts. represents the total number of natural language prompts; Indicates that each natural language prompt belongs to The sum of the probabilities of the control information categories; For the matrix The transpose of Repeat the above E-step and M-step until the convergence condition is reached until, Indicates The parameters of the iterations, Is a positive number.
6. The method for generating diverse controllable text based on a diffusion model according to claim 1, characterized in that: In step S24, during the forward diffusion process, the original text is encoded step by step. Add standard Gaussian noise and we get Text encoding of the time , specifically: (14); In formula (14): represents the distribution of the diffusion process, Represents the original text encoding, that is, the hidden state at the initial moment; express The text encoding of the moment is The hidden state of the moment; represents Gaussian distribution; represents a matrix of all 1s; is a hyperparameter, and , Indicates The noise ratio at each moment, the value range is between 0-1; In the backward diffusion process, the regression loss is used to denoise the network. For training, we have: (18); In formula (18): represents the regression loss; express expectations; Indicates A natural language prompt for the control information category.
7. The method for generating diverse controllable text based on a diffusion model according to claim 1, characterized in that: In step S3, in the application phase, the trained denoising network is used Iteratively generate diverse texts that meet control requirements, namely: (20); Among them, different variances are used at different times , to control the randomness of the sampling process, that is: (21); In formula (21): is a hyperparameter, Represents a real number greater than or equal to 0.
8. A device for implementing a method for generating diverse and controllable text based on a diffusion model, characterized in that: The device comprises: A construction module is used to construct a diverse text generation model based on natural language control, wherein the diverse text generation model includes a natural language prompt encoder, a text reconstruction autoencoder, a control information clustering model, and a latent space diffusion model; Training module, used to train And include task requirements and control requirements Natural language prompts , and the reference text and natural language prompts Inputting a diversity text generation model to train the diversity text generation model; Execution module, used to execute actions based on given natural language prompts , and the natural language prompt Inputting the trained diverse text generation model to iteratively generate diverse texts that meet corresponding controls; The training of the diversity text generation model specifically includes: S21. Using the reference text Train the text reconstruction autoencoder to use the trained text reconstruction autoencoder to reconstruct the reference text Encode and encode the original text Mapped into a continuous latent space; S22. According to the task requirements and natural language prompts The semantic information of , using contrastive learning to train the natural language prompt encoder, so as to use the trained natural language prompt encoder to Encode and obtain the control variable ; Wherein, the natural language prompt In the latent space, it is divided into categories of control information with similar controls; S23, the control variable Input the control information clustering model and use the EM algorithm to obtain the control variables Corresponding natural language prompts Control information category , to control information from the category The corresponding control information is sampled from the distribution of ; S24, reconstructing the text into the original text encoding output by the encoder As the hidden state at the initial moment of the latent space diffusion model, a forward diffusion process and a backward reverse diffusion process are constructed, and the original text is encoded step by step in the forward diffusion process. Add standard Gaussian noise and we get Text encoding of the time ; Using the control variable in the backward diffusion process , control information as well as Text encoding of the time The denoising network is trained to iteratively generate diverse texts that meet the corresponding controls using the trained denoising network.
9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and A memory storing instructions, which, when executed by the at least one processor, causes the at least one processor to execute the method for generating diverse controllable text based on a diffusion model as described in any one of claims 1 to 7.
10. A machine-readable storage medium, characterized in that: The machine-readable storage medium stores executable instructions, which, when executed, enable the machine to execute the method for generating diverse controllable texts based on a diffusion model as described in any one of claims 1 to 7.
