An Emotional Text Generation Method Fine-Tuned Based on Emotional Cues
By constructing emotional prompts and fine-tuning, the emotional text generation problem using pre-trained language generation models under limited hardware resources is solved, and a more coherent and emotional long text is generated under the Transformer architecture.
Patent Information
- Application Number
- CN202211710150.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-29
- Publication Date
- 2025-08-05
- Estimated Expiration
- 2042-12-29
AI Technical Summary
The prior art is difficult to effectively use pre-trained language generation models for emotional text generation under limited hardware resources, especially the model generation capabilities based on RNN architecture are insufficient, while the model based on Transformer architecture requires a large amount of hardware resources and modified model structure.
The NRC emotional intensity dictionary is used to construct emotional prompts, freeze the autoregressive pre-trained language model parameters, and emotional decoding is performed in each step of the model decoding process through emotional prompts to enhance the emotional expression of the generated text.
Make full use of pre-trained language models under limited hardware resources to generate more coherent, authentic and emotional long text.
Smart Images

Figure CN115858736B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of natural language processing, and in particular to an emotion text generation method based on emotion prompt fine-tuning. Background Art
[0002] Natural language generation is an important task in the field of natural language processing. Since emotional intelligence is an important part of artificial intelligence, in order to make the artificial intelligence system closer to humans, emotion text generation has received extensive attention and research. Traditional emotion text generation methods are mainly based on artificial rules and language templates, and often cannot handle complex situations. Thanks to the rapid development of deep learning, the emotion text generation task has made great progress, but there are still some problems. Existing technologies for emotion text generation methods are mainly based on the RNN architecture, but the RNN architecture model has weak text generation ability and cannot be compared with the most advanced pre-trained language generation models based on the Transformer architecture in recent years. Since the Transformer architecture model has a large number of network parameters, generally only medium and large enterprises can afford the corresponding hardware resources. And it is difficult to control this type of model for emotion text generation without modifying the model architecture to allow additional input attributes. Therefore, existing methods cannot effectively use pre-trained language generation models for emotion text generation under limited hardware resource conditions. Specific examples are as follows:
[0003] (1) For the classic end-to-end generation model LSTM based on the RNN architecture, an improved end-to-end dialogue model based on dialogue constraints is proposed, that is, combining the recognition of the topic, emotion and intention of the dialogue to generate a response with reasonable emotion (Guan Mengyu, Wang Zhongqing, Li Shoushan, Zhou Guodong. Research on Reply Generation Based on Dialogue Constraints [J]. Journal of Chinese Information Processing, 2022, 36(08): 144-153.). Even though this method can adaptively generate emotion response texts, it still tends to generate responses with shorter text lengths and has weak text generation ability.
[0004] (2) For the pre-trained language model GPT2-Chinese based on the Transformer architecture, an encoding-decoding framework is proposed. The static personalized information of the user is modeled and encoded at the Encoder end, and a bidirectional independent attention module is added at the Decoder end to achieve personalized text generation (Gao Yongbing, Gao Juntian, Ma Rong, Yang Lidong. User Granularity-Level Personalized Social Text Generation Model [J / OL]. Computer Applications, 2022: 1-8). The disadvantage of this method is that it needs to modify the original structure of the model, integrate personalized features and fine-tune the training of the entire model parameters, which requires a large amount of hardware resource support. Summary of the Invention
[0005] The purpose of the present invention is to provide an emotion text generation method based on emotion prompt fine-tuning, mainly to solve the problem of making full use of the most advanced pre-trained language generation model for emotion text generation under the condition of limited hardware resources. The present invention constructs emotion prompts using the NRC emotion dictionary, performs prompt fine-tuning on the model, and uses an emotion decoding method in each step of the model decoding process, improving the emotion expression ability of the generated text.
[0006] The present invention is achieved by at least one of the following technical solutions.
[0007] An emotion text generation method based on emotion prompt fine-tuning, comprising the following steps:
[0008] S1. Construct continuous emotion prompt words for different emotion categories by using the NRC emotion intensity dictionary and the embedding layer of the autoregressive pre-trained language model;
[0009] S2. Freeze the parameters of the original autoregressive pre-trained language model, use the emotion prompt words to train the autoregressive pre-trained language model, and integrate the emotion factors into the autoregressive pre-trained language model, that is, only perform gradient updates on the emotion parameters and integrate the emotion factors into the language model;
[0010] S3. After training with the emotion prompt words, use the emotion decoding method in each decoding stage of the model generation to further enhance the emotion expression of the generated text.
[0011] Preferably, the NRC emotion intensity dictionary is a mapping relationship database of emotion category intensities and words; the NRC emotion intensity dictionary contains 14,000 words;
[0012] Preferably, the emotion categories include anger, anticipation, disgust, fear, happiness, sadness, surprise, and trust.
[0013] Preferably, the constructing of continuous emotion prompt words for different emotion categories by using the NRC emotion intensity dictionary and the embedding layer of the autoregressive pre-trained language model specifically includes the following steps:
[0014] Perform common word screening on the NRC emotion intensity dictionary D, remove uncommon words and sort them in descending order according to the emotion intensity to obtain a new emotion intensity dictionary D new ;
[0015] Assume the length of the emotion prompt is H, and select the first n words from the emotion intensity dictionary D new according to different emotion categories to form prompt words, obtaining discrete emotion prompts;
[0016] Input the discrete emotion prompts of different emotion categories into the embedding layer of the preset autoregressive pre-trained language model to obtain continuous emotion prompts Pr for corresponding emotion categories.
[0017] Preferably, freeze the original autoregressive pre-trained language model parameters, train the autoregressive pre-trained language model using sentiment prompts, and incorporate sentiment factors into the autoregressive pre-trained language model, which specifically includes the following steps:
[0018] Take the continuous sentiment prompt as the adjustable parameter θ Pr Incorporate it into the generation probability function of the autoregressive pre-trained language model to obtain the sentiment text generation probability function of the autoregressive pre-trained language model;
[0019] Different from the traditional fine-tuning method where all model parameters are updated by gradients, use the affective prompt tuning method to train on the sentiment corpus, freeze the original model parameters, and only update the gradients of the additional adjustable parameter θ Pr for gradient update.
[0020] Preferably, the sentiment text generation probability function is:
[0021]
[0022] where X is the text sentence, which consists of n words {x1, x2, …, x n}; is the conditional probability, that is, the word x i to be generated is determined by the combination of the sentiment prompt and the previous i words [Pr; x <i , Pr is the continuous sentiment prompt, and x i is the i-th word x to be generated in the text sentence sequence.
[0023] Preferably, after training with sentiment prompts, perform sentiment decoding on each decoding stage of the autoregressive pre-trained language model to further enhance the sentiment expression of the generated text, which specifically includes the following steps:
[0024] Obtain the sentiment soft prompt Pr tuned obtained through steps S1 and S2;
[0025] Obtain the word candidate set obtained through the sentiment text generation probability function;
[0026] Calculate the maximum cosine distance between each word in the word candidate set and the sentiment soft prompt to obtain the sentiment similarity;
[0027] Input the sentiment similarity and the sentiment text generation probability function into the preset sentiment word generation probability formula to obtain the generation probability of each word, and take the word with the maximum probability as the next generated word.
[0028] Preferably, the calculation formula for emotional similarity is as follows:
[0029] sim = max[cos(h w , h pr )]
[0030] Among them, h w is the hidden layer state representation of a word in the word candidate set, h pr is the hidden layer state representation of the emotional soft prompt, and cos(h w , h pr ) is the cosine distance between the word and the emotional soft prompt.
[0031] Preferably, inputting the emotional similarity and the emotional text generation probability function into a preset emotional word generation probability formula to obtain the generation probability of each word, and taking the word with the maximum probability as the next generated word. The specific formula is as follows:
[0032]
[0033] x i = argmax C(k)
[0034] Among them, W (k) is the word candidate set composed of k words, C(k) is the generation probability of the emotional word, is the probability of the candidate word, λ is the emotional intensity coefficient, sim is the emotional similarity, and x i is the i-th word x to be generated in the text sentence sequence, and the one with the maximum generation probability is taken.
[0035] Preferably, the autoregressive pre-trained language model is GPT-2, CTRL or Reformer.
[0036] Compared with the existing technologies, the beneficial effects of the present invention are as follows:
[0037] 1. Most of the existing emotional text generation models are based on the RNN architecture, and the text generation ability of the models based on the RNN architecture is weaker than that of the pre-trained language models based on the Transformer architecture. However, the huge network parameters of the Transformer architecture models hinder developers, especially individual developers, from utilizing their superior text generation ability. The present invention constructs an emotional prompt and uses the emotional prompt fine-tuning technology to freeze the original model parameters. Only a small number of additional custom parameters need to be trained to fully utilize the pre-trained language model for emotional text generation.
[0038] 2. After the emotional prompt fine-tuning, by adding emotional decoding during the process of the model generating text, the emotional expression ability of the model to generate text is further enhanced. Compared with other models, the method proposed in this invention can generate more coherent, realistic and emotional long texts. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] The accompanying drawings here are incorporated into the specification and constitute a part of this specification, showing embodiments in line with the present invention, and are used together with the specification to explain the principles of the present invention.
[0040] Figure 1 It is a flowchart of an emotional text generation method based on emotional prompt fine-tuning shown in the embodiment;
[0041] Figure 2 It is a flowchart of constructing a continuous emotional prompt shown in the embodiment;
[0042] Figure 3 It is a flowchart of emotional prompt fine-tuning shown in the embodiment;
[0043] Figure 4 It is a flowchart of emotional decoding shown in the embodiment. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0044] The present invention will be further described in detail below in conjunction with the embodiments and the accompanying drawings, but the embodiments of the present invention are not limited thereto.
[0045] Here, the exemplary embodiments will be described in detail, and the examples are shown in the accompanying drawings. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. On the contrary, they are merely examples of devices and methods consistent with some aspects of the present invention as detailed in the appended claims.
[0046] As Figure 1 shown, an emotional text generation method based on emotional prompt fine-tuning includes the following steps:
[0047] Step S1, constructing continuous emotional prompts for different emotional categories by using the NRC emotional intensity dictionary and the embedding layer of the autoregressive pre-trained language model;
[0048] The NRC emotional intensity dictionary is a publicly available database containing the mapping relationship between 14,000 words and the intensity of 8 emotional categories; the emotional categories include anger (anger), anticipation (anticipation), disgust (disgust), fear (fear), joy (joy), sadness (sadness), surprise (surprise) and trust (trust). In this embodiment, only the emotional category joy (joy) is selected as an example for illustration, and examples of other emotional categories will not be elaborated.
[0049] The autoregressive pre-trained language model includes, but is not limited to, GPT-2, CTRL, Reformer, etc.;
[0050] In one embodiment, the GPT2-Medium model can be selected as an example for illustration. The GPT2-Medium model has 354M network parameters, and the size of its corpus tokens is 50257;
[0051] Please refer to Figure 2 , in one embodiment of a method for constructing continuous sentiment prompts of the present invention, the following steps are included:
[0052] Step S11: Screen the common words in the NRC sentiment intensity dictionary D, eliminate the uncommon words, and sort them in descending order of sentiment intensity to obtain a new sentiment intensity dictionary D new ;
[0053] The screening method is to use the first 5000 words in the pre-trained corpus of the T5 (Transfer Text-to-Text Transformer) model for screening. Because the pre-trained corpus of the T5 model is sorted in descending order of the likelihood of word occurrence. For example, before screening and sorting, the words with the sentiment category of joy in the NRC sentiment intensity dictionary D are {(joy, 0.924), (happiness, 0.984), (enjoy, 0.812), (bestdayever, 0.938)...}, and after screening and sorting, the words with the sentiment category of joy in the new sentiment intensity dictionary D new are {(happiness, 0.984), (joy, 0.924), (enjoy, 0.812)...}.
[0054] Step S12: Assume that the length of the sentiment prompt is H, and select the first H words from the sentiment intensity dictionary D new according to different sentiment categories to form a prompt, and obtain a discrete sentiment prompt;
[0055] The length of the sentiment prompt is set to 3. From the above example, it can be seen that the discrete sentiment prompt is composed of happiness, joy and enjoy.
[0056] Step S13: Input the discrete sentiment prompts of different sentiment categories into the embedding layer of a preset autoregressive pre-trained language model to obtain the corresponding sentiment categories;
[0057] The dimension of the embedding layer of the GPT2-Medium model is (50257, 1024); then the discrete emotion prompt of the emotion category joy is input into the embedding layer, and the vector representation of the continuous emotion prompt Pr can be obtained as output, and its dimension is (3, 50257, 1024).
[0058] Step S2: Freeze the parameters of the original autoregressive pre-trained language model, and use emotion prompt fine-tuning for model training to integrate emotion factors into the language model;
[0059] Freezing the parameters of the original autoregressive pre-trained language model means that during the emotion prompt fine-tuning process, no gradient update is performed on the original network parameters, and only gradient update is performed on the continuous emotion prompt Pr;
[0060] Please refer to Figure 3 , in one embodiment of the emotion prompt fine-tuning method of the present invention, it includes the following steps:
[0061] Step S21: Incorporate the continuous emotion prompt Pr as the adjustable parameter θ Pr into the generation probability function of the model to obtain the emotion text generation probability function of the model;
[0062] The generation probability function of the model is:
[0063]
[0064] where θ represents the original trainable network parameters of the model.
[0065] After freezing the original model parameters, the emotion text generation probability function is:
[0066]
[0067] where X is the text sentence, which consists of a sequence of words {x1, x2, …, x n}, is the conditional probability, that is, the word x i to be generated is determined by the combination of the emotion prompt and the previous i words [Pr; x <i , Pr is the continuous emotion prompt, and x i is the i-th word x to be generated in the text sentence sequence.
[0068] Step S22: Use the fine-tuning method to train on the emotion corpus, and only perform gradient update on the adjustable parameter θ Pr ;
[0069] The emotional corpus includes, but is not limited to, an emotional database obtained through web crawlers, a self-built emotional dataset classified by humans into emotional categories, and publicly available emotional datasets such as GoEmotions, ISEAR, and SemEval. In this embodiment, only the GoEmotions emotional dataset is selected as an example for illustration. In GoEmotions, each line of data consists of a statement and a label, for example, (I am happy, joy). The data storage methods of the other emotional datasets are similar; the fine-tuning method is similar to the traditional fine-tuning method, but only performs gradient updates on the adjustable parameter θ Pr and the fine-tuning parameter configuration is also similar to the traditional fine-tuning configuration.
[0070] Step S3: After using emotional prompt fine-tuning, use an emotional decoding method at each decoding stage of the model generation to further enhance the emotional expression of the generated text;
[0071] The emotional decoding method first takes the emotional prompt after emotional prompt fine-tuning and the maximum probability candidate set predicted by the language model as inputs, then inputs them into the decoder of the model for calculation, and finally outputs the emotional word generation probability;
[0072] The decoder performs autoregressive decoding with the input text encoding vector sequence as the prefix information. The autoregressive decoding means that the current hidden layer state h i is calculated from the current input text encoding vector h i and the prefix hidden layer state h <i through the formula h i =LM(x i ,h <i ), where LM is the language model, and finally the hidden layer state of the last layer is calculated step by step in time.
[0073] Please refer to Figure 4 , in one embodiment of an emotional decoding method of the present invention, it includes the following steps:
[0074] Step S31: Obtain the emotional soft prompt Pr tuned ;
[0075] The emotional soft prompt Pr tuned is obtained by fine-tuning the continuous emotional prompt Pr; it is easy to understand that in this embodiment, the emotional soft prompt Pr tuned is a three-dimensional vector with an emotional prompt length of 3: (3, 50257, 1024);
[0076] Step S32: Obtain the set of word candidates obtained through the emotional text generation probability function;
[0077] Step S33: Calculate the maximum cosine distance between each word in the word candidate set and the sentiment soft prompt to obtain the sentiment similarity;
[0078] Step S34: Input the sentiment similarity and the sentiment text generation probability function into the preset sentiment word generation probability formula to obtain the generation probability of each word, and select the word with the maximum probability as the next generated word
[0079] The calculation formula for the sentiment similarity is:
[0080] sim = max[cos(h w , h pr )]
[0081] where h w is the hidden layer state representation of a word in the word candidate set, h pr is the hidden layer state representation of the sentiment soft prompt, and cos(h w , h pr ) is the cosine distance between the word and the sentiment soft prompt;
[0082] The preset sentiment word generation probability formula is:
[0083]
[0084] x i = argmaxC(k)
[0085] where W (k) is the word candidate set composed of k words, C(k) is the generation probability of the sentiment word, is the probability of the candidate word, λ is the sentiment intensity coefficient, and x i is the next generated word, and the one with the maximum generation probability is selected.
[0086] The value range of the sentiment intensity coefficient λ is [0, 1]. When the sentiment intensity coefficient is 0, sentiment decoding is not performed; the value range of the number of elements k in the word candidate set is [5, 10].
[0087] In this embodiment, it is assumed that the prefix input text is "Love is", the emotional intensity coefficient λ is 0.6, and the number of elements k in the word candidate set is 5. First, the sequence encoding information of the prefix input text is calculated through the emotional text generation probability function, and the predicted generation probability corresponding to each word in the word candidate set can be obtained: {(a, 0.7), (the, 0.6), (beautiful, 0.5), (painful, 0.4), (gift, 0.3)}. Then, by calculating the emotional similarity, the emotional similarity corresponding to each word can be obtained: {(beautiful, 0.8), (gift, 0.6), (painful, 0.3), (a, 0.2), (the, 0.1)}. Finally, through the calculation of the emotional word generation probability formula, the predicted generation probability of the emotional word can be obtained:
[0088] {(beautiful, 0.98), (a, 0.82), (gift, 0.66), (the, 0.66), (painful, 0.52)}. Therefore, the continued word of the prefix input text is "beautiful", and the output text "Love is beautiful" is obtained. If the length of the output text has not reached the set maximum text generation length, then "Love is beautiful" is used as the new prefix input text, and the emotional text generation is performed autoregressively.
[0089] The embodiments of the present invention are shown and described above. It can be understood that the above embodiments are exemplary and should not be construed as limitations on the present invention. Those skilled in the art of this technology can easily think of various equivalent modifications or substitutions within the technical scope disclosed by the present invention, and these modifications or substitutions should all be covered within the protection scope of the present invention.
Claims
1. A method for generating emotional text based on emotional prompt fine-tuning, characterized in that: The following steps are involved: S1. Use the NRC sentiment intensity dictionary and the embedding layer of the autoregressive pre-trained language model to construct continuous emotional prompts of different emotional categories. Specifically, the following steps are included: The NRC sentiment intensity dictionary D is filtered for common words, non-common words are removed and sorted in descending order of sentiment intensity to obtain a new sentiment intensity dictionary D. new ; Assuming that the length of the emotional prompt is H, according to different emotional categories, from the emotional intensity dictionary D new Select the first n words to form the prompt and get the discrete emotional prompt; The discrete emotional cues of different emotional categories are input into the embedding layer of the preset autoregressive pre-trained language model to obtain the continuous emotional cues Pr of the corresponding emotional categories; S2. Freeze the original autoregressive pre-trained language model parameters, use the emotional prompts to train the autoregressive pre-trained language model, and integrate the emotional factors into the autoregressive pre-trained language model. That is, only perform gradient updates on the emotional parameters and integrate the emotional factors into the language model. Specifically, the following steps are included: The continuous emotional prompt Pr is used as an adjustable parameter θ Pr Integrate it into the generation probability function of the autoregressive pre-trained language model to obtain the emotional text generation probability function of the autoregressive pre-trained language model; train it on the emotional corpus, freeze the original model parameters, and only adjust the additional adjustable parameter θ Pr Perform gradient updates; S3. After training with the emotional cues, perform emotional decoding on each decoding stage generated by the autoregressive pre-trained language model to further enhance the emotional expression of the generated text. Specifically, the following steps are included: Get the emotional soft prompt Pr obtained through step S1 and step S2 tuned ; Obtain a word candidate set obtained by the sentiment text generation probability function; Calculate the maximum cosine distance between each word in the word candidate set and the sentiment soft prompt to obtain the sentiment similarity; The sentiment similarity and sentiment text generation probability function are input into the preset sentiment word generation probability formula to obtain the generation probability of each word, and the word with the maximum probability is taken as the next generated word.
2. The method for generating emotional text based on emotional prompt fine-tuning according to claim 1, characterized in that: The NRC sentiment intensity dictionary is a mapping relationship database between sentiment category intensity and words; the NRC sentiment intensity dictionary contains 14,000 words.
3. The method for generating emotional text based on emotional prompt fine-tuning according to claim 1, characterized in that: The emotion categories include anger, anticipation, disgust, fear, happiness, sadness, surprise, and trust.
4. The method for generating emotional text based on emotional prompt fine-tuning according to claim 1, characterized in that: The emotional text generation probability function is: Where X is a text sentence, which consists of n words {x1,x2,…,x n }composition, is the conditional probability, that is, the word x that needs to be generated i The combination of the emotional prompt and the first i words [Pr; x <i ] decision, Pr is a continuous emotional prompt, x i is the i-th word x to be generated in the text sentence sequence.
5. The method for generating emotional text based on emotional prompt fine-tuning according to claim 1, characterized in that: The calculation formula for sentiment similarity is: sim=max[cos(h w ,h pr )] Among them, h w is the hidden layer state representation of a word in the word candidate set, h pr is the hidden layer state representation of emotional soft prompt, cos(h w ,h pr ) is the cosine distance between words and sentiment soft prompts.
6. The method for generating emotional text based on emotional prompt fine-tuning according to claim 1, characterized in that: The sentiment similarity and sentiment text generation probability function are input into the preset sentiment word generation probability formula to obtain the generation probability of each word, and the word with the maximum probability is taken as the next generated word. The specific formula is: x i =argmaxC(k) Among them, W (k) is a word candidate set consisting of k words, C(k) is the generation probability of sentiment words, is the probability of the candidate word, λ is the sentiment intensity coefficient, sim is the sentiment similarity, x i For the i-th word x to be generated in the text sentence sequence, take the one with the highest generation probability.
7. The method for generating emotional text based on emotional prompt fine-tuning according to any one of claims 1 to 6, characterized in that: The autoregressive pre-trained language model is GPT-2, CTRL or Reformer.
Citation Information
Patent Citations
Automatic dialogue generation method and system based on combination of emotion and cue word
CN112765333A
Audio analytics for natural language processing
US20190341026A1