A controllable text generation method based on natural language inference type under few samples
By combining paradigm learning and demonstration learning in the controllable text generation of natural language reasoning type, using dynamic demonstration algorithm and maximum difference template selection, the generation accuracy rate under few samples is improved, and the low generation quality and low generation quality in the existing technology are solved. The problem of unreliable template selection is solved and more efficient text generation is achieved.
Patent Information
- Application Number
- CN202210137932.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-15
- Publication Date
- 2024-09-06
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The controllable text generation of the natural language reasoning type has low generation quality with a small number of samples, the template selection method of the existing technology has unreliable results on the verification set, and the demonstration learning method fails to adjust the example vector according to the current field.
A controllable text generation model based on paradigm learning and demonstration learning is used to obtain the examples most similar to the premise through the retriever. The dynamic demonstration algorithm and the maximum difference template selection algorithm are used, combined with the pre-trained T5 and BART models, to build a text that adapts to the current task. Templates and generators to optimize the generator training process.
The generation accuracy rate under few samples has been improved by 12%, and the verification set data is effectively used to select better templates. The dynamic demonstration algorithm improves the adaptability of sample selection, and the generator performs better in current tasks and fields.
Smart Images

Figure CN114519342B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of natural language processing, and in particular to a controllable text generation method based on natural language reasoning type with few samples. Background Art
[0002] The text generation of natural language reasoning type is to generate a hypothesis h that conforms to the logical relationship c given a premise p and a logical relationship c. There are three types of logical relationships c:
[0003] Entailment: It follows from the premise that the hypothesis is correct.
[0004] Neutral: It is not possible to infer from the premises whether the hypothesis is true or false.
[0005] Contradiction: It is possible to deduce from the premise that the hypothesis is wrong.
[0006] A simple example is, premise p: Xiao Ming is playing basketball. If it is an implication, then the hypothesis can be: Xiao Ming is exercising. If it is neutral, then the hypothesis can be: Xiao Ming likes Xiao Hong. If it is a contradiction, then the hypothesis can be: Xiao Ming is playing football.
[0007] This technology has a wide range of uses. It can be used as a test task for model reasoning ability, for data enhancement of natural language reasoning type, and for controllable text generation in specific scenarios in the real world.
[0008] Research on text generation for natural language inference is mainly based on a public SNLI dataset, which is a manually annotated dataset with more than 500,000 annotated data that can be used to train deep learning models. The initial research focused only on the implication relationship and trained a sequence to sequence model based on a recurrent neural network (RNN) and an attention mechanism. Good results have been generated on simple implication relationships. Later, researchers expanded the relationship to all three types and explored two models based on latent variables, one of which is a single decoder type and the other is an autoencoder type. Compared with previous work, this work achieves controllable generation on three types of relationships, and the generated data can be used to build a new dataset. The new dataset can be used by other natural language processing (NLP) tasks. Recent new research has utilized visual knowledge. Since the SNLI dataset is based on the image caption dataset, each premise has a corresponding image. In this work, the researchers continued the content of the first work and only focused on the implication relationship, but they also added image information. Through a multimodal approach, they built two encoders, one encoding text information and the other encoding image information. Finally, the outputs of the two encoders were concatenated and input into a decoder to predict the hypothesis h. By adding image information, the generation effect was improved.
[0009] The above-mentioned related research technologies have a flaw: they all require a large amount of labeled data to train deep learning models. In a specific field (such as medicine) or when applied to the real world, there is a lack of a large amount of labeled data. That is, these models cannot generate high-quality hypotheses h under few-shot conditions.
[0010] On the other hand, there are many general technologies for solving various problems under few samples. With the development of pre-trained language models, prompt-based learning has become increasingly important under few sample problems. The idea of prompt-based learning is that although we lack training data under few samples, we can find ways to better utilize the knowledge learned by the pre-trained model during the pre-training process. Prompt-based learning transforms downstream tasks into a form similar to pre-training tasks by constructing templates, so that the pre-trained model can better apply the knowledge learned in pre-training to downstream tasks. For example, for sentence translation tasks, Chinese sentences need to be translated into English. The traditional method is to fine-tune the pre-trained model on a large amount of annotated translation data. However, this method does not work well under few sample conditions. The way of prompt-based learning is to construct a template "A is translated to", where A is replaced by the Chinese sentence to be translated, and then let the pre-trained model continue to generate the text behind this sentence. The model can generate good results with a small number of sample training or even without training. This technology can also be combined with demonstration learning. Demonstration learning is to add an example to the model input to show the model how to generate.
[0011] Still in the sentence translation task, the template is now "M is translated to N, A is translated to", and then the model is asked to generate the following sentences as before. Different from the previous one, "M is translated to N" is added here as an example to show to the model.
[0012] This technology has two related research directions. One is how to construct the template, that is, how to select the words in the template (here, the three words "translate to"). At first, people wrote templates manually based on their professional knowledge, and later began to build templates based on retrieval, rewriting, etc. Later, a gradient-based search algorithm was proposed, that is, some words are randomly initialized at the beginning, and then the training data is used to obtain the gradient to update the words, and it is repeated until convergence. In recent studies, a method of using the T5 pre-trained model to generate templates has been proposed. Compared with previous methods, it does not require the number of words in the template to be specified in advance, and it is more convenient to use. However, when this method is used for controllable text generation problems, the results on the verification set will be unreliable.
[0013] Another direction is how to find suitable examples. At first, a random search method was used, and later researchers proposed to use similarity search. That is, for each input, an encoder is used to obtain its sentence vector, and then an example is found so that the sentence vector of this example is closest to the input sentence vector. Experimental results also show that this similarity method is better than random search. However, the sentence vector generated by this method is fixed. It is completely based on the encoder and is not combined with the current application field.
[0014] In summary, the shortcomings of the prior art are:
[0015] (1) Research related to controllable text generation of natural language reasoning type has not considered solutions with few samples.
[0016] (2) The best performing method for automatically generating templates in paradigm learning currently has unreliable results on the validation set when used for problem (1).
[0017] (3) Existing demonstration learning methods do not make specific adjustments to vectors based on the current domain and problem when selecting examples. Summary of the invention
[0018] The purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a controllable text generation method based on natural language reasoning type with a small number of samples with high generation accuracy.
[0019] The purpose of the present invention can be achieved by the following technical solutions:
[0020] A controllable text generation method based on natural language reasoning type under few samples, the method converts premise p into hypothesis h corresponding to a specific logical relationship c through a controllable text generation model to generate the controllable text, wherein:
[0021] The controllable text generation model includes a retriever and a generator. The retriever retrieves the example most similar to the premise p from the training set. The premise p, logical relationship c and the example are all normalized by a pre-built template. The generator uses a dynamic demonstration algorithm based on the normalized premise p, logical relationship c and the example to generate the final hypothesis h.
[0022] Furthermore, the template is obtained by the following steps:
[0023] Constructing the meta template:
[0024] T g (s i )=[p i ][MASK]h i ]
[0025] where s i =(p i ,c i ,h i ) represents the i-th data in the training set, including the premise p i , logical relationship c i And the corresponding assumption h i , [MASK] is a special placeholder character;
[0026] Use the pre-trained T5 model to generate the words corresponding to [MASK] and obtain the final template for each logical condition m. The goal of generation is to find multiple alternative templates T for each logical condition m. m , so that the following objectives are maximized:
[0027]
[0028] Where D train represents the training set, P t5 is the output probability of the T5 model;
[0029] Using the validation set from the candidate template T m Get the best template T m ′m.
[0030] Furthermore, the optimal template T′ m The formula for obtaining is:
[0031]
[0032] Where D k ={s i |s i ∈D,c i =k}, the subscript train represents the training set, the subscript dev represents the validation set, S L is an evaluation function, C is a set of all logical conditions,
[0033] Furthermore, the logical condition includes implication, neutrality or contradiction.
[0034] Furthermore, the dynamic demonstration algorithm is specifically as follows:
[0035] The retriever retrieves a similarity distribution similar to the premise p from the training set;
[0036] The generator generates a corresponding hypothesis distribution based on the normalized premise p, the logical relationship c and the example;
[0037] The final hypothesis h is obtained based on the product of the similarity distribution obtained by the retriever and the hypothesis distribution generated by the generator.
[0038] Furthermore, the generator is constructed based on the BART model.
[0039] Furthermore, the controllable text generation model is trained using the Adam optimizer and the stochastic gradient descent method.
[0040] Furthermore, the loss function used in training the controllable text generation model is -logP, where P is the probability of the model output, expressed as:
[0041]
[0042] where N is the length of the generated hypothesis, is the probability distribution of the retriever, s k is the kth data in the training set, s i is the i-th input data, is the probability distribution of the generator, is the input data s i The corresponding output sequence is the word at position j, is the input data s i The corresponding output sequence is the word sequence from the 1st to the j-1th position, It is the premise i The results after normalization.
[0043] Furthermore, the probability distribution of the retriever The expression is:
[0044]
[0045] Where Sim represents similarity.
[0046] The present invention also provides a computer-readable storage medium, comprising one or more programs for execution by one or more processors of an electronic device, wherein the one or more programs include instructions for executing the controllable text generation method based on natural language inference type with a small number of samples as described above.
[0047] Compared with the prior art, the present invention has the following beneficial effects:
[0048] 1) The present invention constructs a controllable text generation model of natural language reasoning type based on paradigm learning and demonstration learning. Compared with the traditional model, the generation accuracy is improved by 12% with few samples.
[0049] 2) The present invention adopts the maximum-margin template selection algorithm when selecting the best template. Compared with the traditional algorithm, it can use all the data in the verification set and select a better template with three times the amount of data of the traditional algorithm, effectively improving the accuracy of text generation.
[0050] 3) The present invention proposes a dynamic demonstration algorithm, which simultaneously utilizes the similarity distribution obtained by the retriever and the hypothesis distribution generated by the generator. When the model is trained, the parameters of the retriever will be optimized in the direction of increasing the similarity of the examples that can increase the training results of the generator. Compared with the traditional static demonstration algorithm, it can better adapt to the current tasks and fields and select more suitable examples. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] Figure 1 It is a schematic diagram of the process of the present invention. DETAILED DESCRIPTION
[0052] The present invention is described in detail below in conjunction with the accompanying drawings and specific embodiments. This embodiment is implemented based on the technical solution of the present invention, and provides a detailed implementation method and specific operation process, but the protection scope of the present invention is not limited to the following embodiments.
[0053] like Figure 1 As shown, this embodiment provides a controllable text generation method based on natural language reasoning type under a small number of samples. The method converts premise p into hypothesis h corresponding to a specific logical relationship c through a controllable text generation model to generate the controllable text. The controllable text generation model includes a retriever and a generator. The retriever retrieves and obtains the example most similar to the premise p from a training set. The premise p, the logical relationship c and the example are all normalized by a pre-constructed template. The normalization is completed by a normalization function. The generator uses a dynamic demonstration algorithm based on the normalized premise p, the logical relationship c and the example to generate the final hypothesis h.
[0054] The key points involved in the above method include:
[0055] (1) Normalized Function
[0056] This method generates corresponding templates for the three logical relationships considered. With the templates, a normalization function can be constructed. The normalization function directly replaces the premise p part in the template with the input premise, leaving the hypothesis h part empty and leaving it to the generator to complete.
[0057] The template is constructed as follows:
[0058] First, construct the following meta template:
[0059] T g (s i)=[p i ][MASK]h i ]
[0060] where s i =(p i ,c i ,h i ) represents the i-th data in the training set, including the premise p i , logical relationship c i And the corresponding assumption h i , [MASK] is a special placeholder character.
[0061] Then use the pre-trained T5 model to generate the word corresponding to [MASK]. The goal of generation is to find T for each logical condition m. m , so that the following objectives are maximized:
[0062]
[0063] Where D train represents the training set, P t5 is the output probability of the T5 model. In this way, we can find the best template on the current training set for each of implication, neutrality and contradiction.
[0064] Because the paradigm is very sensitive to the model, the performance of the template found by the T5 model on the generator is unknown, and the data of the validation set is needed to make a selection. When using T5 to generate a template for each condition m, you can use Beam search and set the search width to 100 to find 100 candidate templates. You can further use the validation set to select the best template T′ according to the following formula m :
[0065]
[0066] Among them, D m ={s i |s i ∈D,c i =m},S L is an evaluation function, L is the generator of the model, and the evaluation function uses the training data and template T m To train the generator L, and then use the validation set data to test the generated results and give a score. Specifically, a natural language inference type classifier can be used to score the final generated results to determine whether the generated results conform to the given logical relationship with the given premise.
[0067] However, the above formula only considers 1 / 3 of the data in the validation set for evaluation, which may lead to unreliable results. Therefore, this embodiment proposes a maximum margin template selection algorithm, which selects the best template T′ m The formula for obtaining is improved to:
[0068]
[0069] Where D k ={s i |s i ∈D,c i =k}, the subscript train represents the training set, the subscript dev represents the validation set, S L is an evaluation function, C is a set of all logical conditions, i.e., the three relations in this embodiment, and
[0070] The above improved formula can make use of all the data in the validation set, giving positive scores to the same conditions and negative scores to the different conditions. The reason for this is that the three conditions conflict with each other, so a good template should have lower scores on other conditions.
[0071] (2) Retrieval
[0072] The retriever is responsible for finding the most similar example to the input premise p, and inputting it into the generator together. The retriever of this embodiment uses similarity to find the most similar example. In the traditional algorithm, the SBERT model is used to calculate the vector of the sentence, and then the cosine similarity is calculated. However, this algorithm is static, and the sentence vector is not fitted to the current task and field. This embodiment proposes a dynamic demonstration algorithm. On the basis of obtaining the similarity distribution, the similarity distribution obtained by the retriever and the hypothetical distribution generated by the generator are multiplied as the final distribution. In this way, when the model is trained, the parameters of the retriever will be optimized in the direction of increasing the similarity of the examples that can increase the training results of the generator.
[0073] Based on the above dynamic demonstration algorithm, the final output probability of the controllable text generation model in this embodiment is:
[0074]
[0075] where N is the length of the generated hypothesis, is the probability distribution of the retriever, s k is the kth data in the training set, s i is the i-th input data, is the probability distribution of the generator, is the input data s i The corresponding output sequence is the word at position j, is the input data i The corresponding output sequence is the word sequence from the 1st to the j-1th position, It is the premise i Normalized results, where the probability distribution of the retriever The expression is:
[0076]
[0077] Where Sim represents similarity.
[0078] (3) Generator
[0079] The generator in this embodiment uses a pre-trained BART model.
[0080] (4) Model training
[0081] The controllable text generation model constructed in this embodiment uses the Adam optimizer and the stochastic gradient descent method to train the entire model, and the loss function is -logP.
[0082] If the above method is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0083] The preferred specific embodiments of the present invention are described in detail above. It should be understood that a person skilled in the art can make many modifications and changes based on the concept of the present invention without creative work. Therefore, any technical solution that can be obtained by a person skilled in the art through logical analysis, reasoning or limited experiments based on the concept of the present invention on the basis of the prior art should be within the scope of protection determined by the claims.
Claims
1. A controllable text generation method based on natural language inference type under few samples, It is characterized in that The method uses a controllable text generation model to convert premise p into hypothesis h corresponding to a specific logical relationship c to generate the controllable text, wherein: The controllable text generation model includes a retriever and a generator. The retriever retrieves and obtains the example most similar to the premise p from the training set. The premise p, the logical relationship c and the example are all normalized by a pre-built template. The generator uses a dynamic demonstration algorithm based on the normalized premise p, the logical relationship c and the example to generate the final hypothesis h. The template is obtained by following the steps below: Constructing the meta template: T g (s i )=[p i ][MASK][h i ] where s i =(p i ,c i ,h i ) represents the i-th data in the training set, including the premise p i , logical relationship c i And the corresponding assumption h i , [MASK] is a special placeholder character; Use the pre-trained T5 model to generate the words corresponding to [MASK] and obtain the final template for each logical condition m. The goal of generation is to find multiple alternative templates T for each logical condition m. m , so that the following objectives are maximized: Where D train represents the training set, P t5 is the output probability of the T5 model; Using the validation set from the candidate template T m Get the best template T′ m ; The dynamic demonstration algorithm is specifically as follows: The retriever retrieves a similarity distribution similar to the premise p from the training set; The generator generates a corresponding hypothesis distribution based on the normalized premise p, the logical relationship c and the example; The final hypothesis h is obtained based on the product of the similarity distribution obtained by the retriever and the hypothesis distribution generated by the generator.
2. According to claim 1, the controllable text generation method based on natural language inference type under few samples, It is characterized in that The optimal template T′ m The formula for obtaining is: Where D k ={s i |s i ∈D,c i =k}, the subscript train represents the training set, the subscript dev represents the validation set, S L is an evaluation function, C is a set of all logical conditions, 3. The controllable text generation method based on natural language inference type with few samples according to claim 1 or 2, It is characterized in that The logical conditions include implication, neutrality or contradiction.
4. According to claim 1, the controllable text generation method based on natural language inference type under few samples, It is characterized in that The generator is built based on the BART model.
5. According to claim 1, the controllable text generation method based on natural language inference type under few samples, It is characterized in that The controllable text generation model is trained using the Adam optimizer and the stochastic gradient descent method.
6. The controllable text generation method based on natural language inference type under few samples according to claim 1, It is characterized in that The loss function used in training the controllable text generation model is -logP, where P is the probability of the model output, expressed as: where N is the length of the generated hypothesis, is the probability distribution of the retriever, s k is the kth data in the training set, s i is the i-th input data, is the probability distribution of the generator, is the input data s i The corresponding output sequence is the word at position j, is the input data s i The corresponding output sequence is the word sequence from the 1st to the j-1th position, It is the premise i The results after normalization.
7. The controllable text generation method based on natural language inference type with few samples according to claim 6, It is characterized in that The probability distribution P of the retriever θr (s k |s i ) is: Where Sim represents similarity.
8. A computer-readable storage medium, It is characterized in that It includes one or more programs for execution by one or more processors of an electronic device, and the one or more programs include instructions for executing a controllable text generation method based on natural language inference type under a small number of samples as described in any one of claims 1-7.
