A method for generating machine responses with specified emotions for requests and emotion labels
Through the deep learning framework and conditional variational autoencoder, combined with emotional labels and word vector representation, the problem of lack of emotional tendency in human-computer dialogue systems in the existing technology is solved, achieving more fluent and accurate emotional dialogue generation, and improving dialogue System performance and emotional expressiveness.
Patent Information
- Application Number
- CN202210034903.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-13
- Publication Date
- 2024-10-22
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The replies generated by existing human-computer dialogue systems based on deep learning lack emotional tendencies and fail to effectively utilize the fine-grained classification of emotional words, resulting in less fluent and accurate emotional expressions.
Using a deep learning framework, through pre-trained network models and conditional variational autoencoders, emotional labels are used to generate dialogue responses that express specified emotions, and the process of emotion classification and response generation is optimized by combining word vector representation and emotion scoring mechanisms.
It realizes human-machine dialogue that is more fluent in expression and more accurate in emotion, improves the performance and flexibility of emotional dialogue generation, and can better control the emotional expression and semantic association in reply generation.
Smart Images

Figure CN114547261B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a method for generating a machine reply with specified emotions for a request and an emotion tag, and belongs to the technical field of natural language processing. Background Art
[0002] With the continuous development of human-computer dialogue systems, we are increasingly exposed to such "robots" that can communicate with humans in our lives. They are gradually integrating into people's daily lives and constantly improving people's living standards by helping to complete many things.
[0003] At present, the existing human-computer dialogue systems based on deep learning often generate "cold" responses without emotional tendencies. Emotions are regarded as a kind of "human intelligence", and emotional expression and perception can be achieved through natural language, which reflects the importance of emotion-based dialogue generation.
[0004] Emotion-based dialogue generation has become a novel and meaningful research. However, existing studies mainly adopt the method of splicing emotion embedding at the decoder input or introducing external emotion dictionaries, ignoring the more detailed classification of emotion words. Summary of the invention
[0005] The purpose of the present invention is to address the defects of the prior art and creatively propose a method for generating a machine reply with specified emotions for requests and emotion tags.
[0006] The innovation of the present invention lies in: utilizing a deep learning framework and using different emotion tags to generate dialogue responses expressing specified emotions, thereby realizing a human-computer dialogue mechanism that is more fluent in expression and more accurate in emotion.
[0007] A method for generating a machine reply with a specified emotion for a request and an emotion label, comprising the following steps:
[0008] Step 1: Obtain training data and perform data processing.
[0009] Specifically, we use the conversation dataset with emotion tags to extract sentences with emotion and obtain the corpus for pre-training the model. We calculate the TF-IDF (term frequency–inverse document frequency) of the vocabulary to obtain the topic dictionary and the external sentiment dictionary. We use the entire corpus to train and obtain the vector representation of the word.
[0010] Step 2: Build a pre-trained network model for sentiment scoring.
[0011] Specifically, the pre-trained network model consists of LSTM (Long short-term memory) and a fully connected layer, and its output is the sentiment classification.
[0012] Among them, the sentiment score is calculated by cosine similarity, as shown in Formula 1:
[0013]
[0014] Among them, p classify represents the sentiment classification result, l one-hot represents the one-hot vector of the sentiment label; ‖·‖ represents the 2-norm of vector ·;
[0015] The pre-training loss function is as follows:
[0016] L′=L′1+L′2 (2)
[0017] L′1=(1-score) (3)
[0018] L′2=-∑ j y j log(p j ) (4)
[0019] Among them, L′ represents the pre-training loss function, L′1 represents the sentiment score loss, and L′2 represents the sentiment classification loss; p j represents the probability of data being classified into the jth category; y j Represents the actual classification result, which is 1 when the data belongs to class j and 0 otherwise.
[0020] Preferably, the pre-trained network model includes an encoder, a decoder, a conditional variational autoencoder, an output mapping module and a pre-trained sentiment scorer. The encoder and the decoder are composed of a gated recurrent unit GRU, which is used for input-output mapping; the conditional variational autoencoder is used to map the reference response to the latent variable space; the output mapping module is used to map the output of the decoder to the vocabulary in the dictionary; the pre-trained sentiment scorer is used to score and control the input of the sentiment factor in the decoder.
[0021] Step 3: Construct the loss function for formal training.
[0022] The formal training loss function consists of three parts: the KL divergence between the conditional variational autoencoder distributions It is expressed as follows:
[0023]
[0024] Among them, KL represents KL divergence calculation, P r() represents the distribution of the recognition network output in the conditional variational autoencoder, z represents the hidden variable, X represents the input request, Indicates reference reply, P p () represents the distribution of the prior network output in the conditional variational autoencoder.
[0025] Second, the sentiment classification loss of latent variables It is expressed as follows:
[0026]
[0027] in, Indicates that the distribution The cross entropy under ; P(label|z) represents the probability that the classification result of the latent variable z is label, and label represents the target emotion category.
[0028] The third is the cross entropy loss between the output response sequence and the reference response sequence It is expressed as follows:
[0029]
[0030] Among them, Y represents the target output, z represents the hidden variable, t represents the time step, and y t represents the output of the tth time step, and y represents the output of the time step.
[0031] The loss function used for formal training is The overall representation is as follows:
[0032]
[0033] Among them, α is a hyperparameter used to control the weight of KL loss, which increases from 0 to 1 during the training process.
[0034] Step 4: Use the training data obtained in step 1 and the pre-trained network model constructed in step 2 to train the pre-trained sentiment scoring network parameters Parameter pre-train .
[0035] Step 5: Use the training data obtained in step 1, the loss function in step 3, and the pre-trained sentiment scoring network parameters obtained in step 4 to train the pre-trained network model constructed in step 2.
[0036] Specifically, the method is as follows:
[0037] Step 1: Use the word vector from step 1 and the network parameters from step 4 pre-train , initialize the pre-trained network model.
[0038] Step 2: Each batch contains the input request, reference response, and sentiment label of the reference response in the training data obtained in step 1. The sentence sequence is mapped to the vector space and sent to the encoder and variational autoencoder respectively to obtain the intermediate representation h and latent variable z.
[0039] Step 3: The input intermediate representation obtained in step 2, as well as the sentiment embedding and sentiment score, are integrated and sent to the decoder, as shown below:
[0040]
[0041]
[0042] Among them, s t represents the state output at the tth time step, s t-1 Represents the state output of the t-1th time step; represents the weight of the attention mechanism; h i represents the output of the encoder at the i-th time step; C t-1 represents the input at the t-1th time step, represents the word in the reference reply at the t-1th time step, emb(y) represents the word vector corresponding to word y, Extra info represents the vector concatenated by the latent variable z and the sentiment embedding, score t-1 Represents the output score of the sentiment scorer at step t-1.
[0043] Step 4: Perform output mapping on the output of step 3 to obtain the output word.
[0044] Step 5: Send the output vocabulary to the pre-trained sentiment scorer to get the current sentiment score, and repeat steps 3-5 until the output is the sentence end sign eos.
[0045] Step 6: Calculate the loss function and use back propagation to calculate the gradient, use the momentum optimization algorithm MomentumOptimizer to optimize, and update the model parameter values.
[0046] Step 7: Repeat steps 2-6 until the loss function becomes stable.
[0047] The emotional dialogue generation application is actually run. In the absence of reference responses, the discriminant network in the conditional variational autoencoder is used to generate the distribution of latent variables.
[0048] Beneficial Effects
[0049] Compared with the prior art, the method of the present invention has the following beneficial effects:
[0050] 1. The present invention introduces a pre-trained sentiment scoring mechanism that can control the sentiment expression in the response generation process, taking into account vocabulary and the interaction between words.
[0051] 2. The present invention introduces a fine-grained sentiment dictionary, which comprehensively considers the probability of sentiment words appearing in different sentiment categories, and fine-tunes them during the training process, which can enrich the sentiment expression of the output response.
[0052] 3. The present invention utilizes a conditional variational autoencoder to model the reference response, so that the decoder can learn the corresponding relationship between input and output and optimize the semantic association between the response and the input request.
[0053] 4. The present invention has achieved good performance improvement on multiple emotional dialogue generation data sets. At the same time, the pre-trained emotion scorer proposed in the present invention has strong flexibility and adaptability. BRIEF DESCRIPTION OF THE DRAWINGS
[0054] Figure 1 It is the work flow chart of the method of the present invention;
[0055] Figure 2 It is a training data preprocessing diagram of the method of the present invention;
[0056] Figure 3 The overall network architecture diagram of the method of the present invention;
[0057] Figure 4 This is an output mapping structure diagram of the method of the present invention. DETAILED DESCRIPTION
[0058] The present invention is described in detail below with reference to the accompanying drawings and specific embodiments.
[0059] Example
[0060] like Figure 1 The method for generating a machine reply with a specified emotion for a request and an emotion tag comprises the following steps:
[0061] Step 1: Obtain training data and perform data processing.
[0062] Using the existing emotional conversation generation (ECG) dataset with emotion labels, we extract sentences with emotion labels of happiness, sadness, disgust, anger, and like as the pre-training corpus, and collect all sentences as the formal training corpus. We use the formal training corpus to calculate the TF-IDF of the vocabulary to obtain the topic dictionary, and also obtain the external emotion dictionary. We use the entire corpus to obtain the word vector representation using the GloVE method, and the word vector dimension is selected as 100 dimensions. Figure 2 shown.
[0063] Step 2: Build a pre-trained network model.
[0064] The pre-trained network model will be used for sentiment scoring. The model consists of LSTM and fully connected layers. The output is the sentiment classification result. The sentiment score is calculated by the one-hot encoding cosine similarity between the classification result and the sentiment label. The formula is as follows:
[0065]
[0066] The loss function is as follows:
[0067] L′=L′1+L′2
[0068] L′1=(1-score)
[0069]
[0070] Specifically, the network model consists of five parts, such as Figure 3 As shown, it includes an encoder (one encoder is used in this embodiment), a decoder, a conditional variational autoencoder, an output mapping module and a pre-trained sentiment scorer. Among them, the encoder and decoder are used for input-output mapping, mapped to a vocabulary of size 40,000, and are both composed of a universal gated recurrent unit (GRU). The size of the gated recurrent unit is set to 256 dimensions, and a bidirectional recurrent neural network is used for implementation. The conditional variational autoencoder is used to map the reference response to the latent variable space, and the size of the latent variable is 100 dimensions. The output mapping module is used to map the output of the decoder to the vocabulary in the dictionary, and the pre-trained sentiment scorer is used to score the input of the sentiment factor in the control decoder, wherein the sentiment factor includes a 100-dimensional sentiment embedding and a 100-dimensional latent variable.
[0071] Step 3: Design the loss function.
[0072] The loss function consists of three parts. One of them is the KL divergence between the distributions generated by the conditional variational autoencoder module discriminant network and the prior network, which is expressed as follows:
[0073]
[0074] The second is the sentiment classification loss of latent variables, which is expressed as follows:
[0075]
[0076] The third is the cross entropy loss between the output response sequence and the reference response sequence, expressed as follows:
[0077]
[0078] The overall loss function is expressed as follows:
[0079]
[0080] α increases from 0 to 1 during training and is fixed at 1 after 800,000 pieces of training data.
[0081] Step 4: Using the training data obtained in step 1 and the pre-trained network model obtained in step 2, train until the loss function becomes stable, thereby obtaining the network parameters of the pre-trained sentiment scoring. pre-train .
[0082] Step 5: Figure 3 As shown, the network model of step 3 is trained using the training data obtained in step 1, the network loss function of step 4, and the pre-trained sentiment scoring parameters obtained in step 4.
[0083] Step 1: Use the 100-dimensional word vector from step 1 and the pre-trained parameters from step 5 pre-train Initialize the model.
[0084] Step 2: Each batch contains the input request, reference response, and sentiment label of the reference response in the training data obtained in step 1. The sentence sequence is mapped to the vector space and sent to the encoder and variational autoencoder respectively to obtain the intermediate representation h and latent variable z.
[0085] Step 3: Use the input intermediate representation obtained in Step 2, the 100-dimensional sentiment embedding, and the sentiment score calculated by the pre-trained sentiment scoring module based on the output words of the previous time step, and integrate them into the decoder. The formula is as follows:
[0086]
[0087]
[0088] Step 4: Send the output of step 3 to the output mapping module, and get the word with the largest probability value in the vocabulary as the output word of the current time step. Figure 4 As shown in the figure, the probabilities of the three dictionaries are calculated using GRU output and latent variables, and then the probability of each word in the sentiment dictionary is obtained based on the GRU output, sentiment embedding, latent variables and the fine-grained prior probability value of the sentiment dictionary; the GRU output is used to pass through a fully connected layer to obtain the probability of words in the topic dictionary and the probability of words in the general dictionary.
[0089] Step 5: Send the output vocabulary to the pre-trained sentiment scorer to get the current sentiment score, and repeat steps 3-5 until the output is the end-of-sentence sign (eos).
[0090] Step 6: Calculate the loss function and use back propagation to calculate the gradient, use the momentum optimization algorithm MomentumOptimizer for optimization, with a learning rate of 0.0001, and update the parameter values of the main framework of the model.
[0091] Step 7: Repeat steps 2-6 and train for 30 rounds until the loss function becomes stable.
[0092] The emotional dialogue generation application is actually run. In the absence of reference responses, the discriminant network in the conditional variational autoencoder is used to generate the distribution of latent variables.
Claims
1. A method for generating a machine reply with a specified emotion for a request and an emotion label, characterized in that: The following steps are involved: Step 1: Use the conversation dataset with emotion labels to extract sentences with emotion, obtain the corpus for pre-training the model, and collect all sentences as the formal training corpus; calculate the word frequency-inverse text frequency of the vocabulary to obtain the topic dictionary, and at the same time, obtain the external emotion dictionary; use the entire corpus to train and obtain the vector representation of the word; Step 2: Build a pre-trained network model for sentiment scoring; The pre-trained network model consists of a long short-term memory (LSTM) and a fully connected layer, and its output is the sentiment classification. The sentiment score is calculated by cosine similarity, as shown in Formula 1: Among them, p classify represents the sentiment classification result, l one-hot represents the one-hot vector of the sentiment label; ||·|| represents the 2-norm of the vector ·; The pre-training loss function is as follows: L'=L'1+L'2 (2) L'1=(1-score) (3) L'2=-∑ j y j log(p j ) (4) Among them, L' represents the pre-training loss function, L'1 represents the sentiment score loss, and L'2 represents the sentiment classification loss; p j represents the probability of data being classified into the jth category; y j Indicates the actual classification result, which is 1 when the data belongs to class j and 0 in other cases; The pre-trained network model includes a first encoder, a second encoder, a third encoder, a decoder, a conditional variational autoencoder, an output mapping module and a pre-trained sentiment scorer; wherein the three encoders and the decoder are all composed of a gated recurrent unit GRU, which is used for input-output mapping; the conditional variational autoencoder is used to map the reference response to the latent variable space, and the conditional variational autoencoder includes the first encoder; the output mapping module is used to map the output of the decoder to the vocabulary in the dictionary; the pre-trained sentiment scorer is used to score the input of the sentiment factor in the decoder, and the pre-trained sentiment scorer includes the third encoder; Step 3: Construct the loss function for formal training; The formal training loss function consists of the following three parts: One is the KL divergence between the conditional variational autoencoder distributions It is expressed as follows: Among them, KL represents KL divergence calculation, P r () represents the distribution of the recognition network output in the conditional variational autoencoder, z represents the hidden variable, X represents the input request, Indicates reference reply, P p () represents the distribution of the prior network output in the conditional variational autoencoder; Second, the sentiment classification loss of latent variables It is expressed as follows: in, Indicates that the distribution The cross entropy under P(label|z) represents the probability that the classification result of the latent variable z is label, and label represents the target emotion category. The third is the cross entropy loss between the output response sequence and the reference response sequence It is expressed as follows: Among them, P(y_t|y_{<t}, z, X, label) represents the probability of generating a word at each time step given the latent variable z, the input request X, and the target sentiment category label. Y represents the target output, z represents the latent variable, t represents the time step, and y t represents the output at the t-th time step, and y represents the output of the time step; The loss function used for formal training is It is expressed as follows: Among them, α is a hyperparameter used to control the weight of KL loss, which increases from 0 to 1 during the training process; Step 4: Use the training data obtained in step 1 and the pre-trained network model constructed in step 2 to train the pre-trained sentiment scoring network parameters Parameter pre-train ; Step 5: Use the training data obtained in step 1, the loss function in step 3, and the pre-trained sentiment scoring network parameters obtained in step 4 to train the pre-trained network model constructed in step 2; Step 1: Use the word vector obtained in step 1 and the network parameters obtained in step 4 pre-train , initialize the pre-trained network model; Step 2: Each batch contains the input request, reference response, and sentiment label of the reference response in the training data obtained in step 1. The sentence sequence is mapped to the vector space and sent to the encoder and variational autoencoder respectively to obtain the intermediate representation h and latent variable z; Step 3: The input intermediate representation obtained in step 2, as well as the sentiment embedding and sentiment score, are integrated and sent to the decoder, as shown below: Among them, s t represents the state output at the tth time step, s t-1 Represents the state output of the t-1th time step; represents the weight of the attention mechanism; h i represents the output of the encoder at the i-th time step; C t-1 represents the input at the t-1th time step, represents the word in the reference reply at the t-1th time step, emb(y) represents the word vector corresponding to word y, Extra info represents the vector concatenated by the latent variable z and the sentiment embedding, socre t-1 represents the output score of the sentiment scorer at step t-1; Step 4: Map the output of step 3 to get the output word; Step 5: Send the output vocabulary to the pre-trained sentiment scorer to get the current sentiment score, and repeat steps 3-5 until the output is the sentence end mark eos; Step 6: Calculate the loss function and use back propagation to calculate the gradient, use the momentum optimization algorithm MomentumOptimizer to optimize, and update the model parameter values; Step 7: Repeat steps 2-6 until the loss function becomes stable; The emotional dialogue generation application is actually run. In the absence of reference responses, the discriminant network in the conditional variational autoencoder is used to generate the distribution of latent variables.
2. The method for generating a machine reply with a specified emotion for a request and an emotion tag as claimed in claim 1, characterized in that: In step 1, the GloVE method is used to obtain the vector representation of words using the entire corpus, and the word vector dimension is selected as 100 dimensions.
3. The method for generating a machine reply with a specified emotion for a request and an emotion tag as claimed in claim 1, characterized in that: In step 1, the emotion labels of the extracted sentences include happiness, sadness, disgust, anger, and like.
