Method for training dialogue model, dialogue implementation method and related device
The global and speaker text of the dialogue samples were extracted layer by layer for generative network training, which solved the problem that the intelligent dialogue system could not understand the dialogue logic and improved the accuracy and coherence of the response.
Patent Information
- Application Number
- CN202410942681.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-15
- Publication Date
- 2024-11-15
- Estimated Expiration
- 2044-07-15
AI Technical Summary
When existing intelligent dialogue systems generate replies based on deep learning, it is difficult to deeply understand the internal logical information of the dialogue text, resulting in the limitation of the accuracy, coherence and diversity of generated replies.
By obtaining global text at the global level and speaker text at the speaker level from the dialogue sample, input the generated network for encoding and decoding, calculate the loss between the predicted label and the real label, and iteratively train the generated network, it can be better A dialogue model that understands the contextual relationship of dialogue samples.
Improves the accuracy, coherence and diversity of the dialogue model in generating replies, ensuring the quality of generated replies.
Smart Images

Figure CN118536572B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of intelligent dialogue technology, and in particular, to a method for training a dialogue model, a dialogue implementation method, and related devices. Background Art
[0002] With the rapid development of artificial intelligence technology and the popularization of Internet applications, intelligent dialogue systems have been widely used and studied. Today, intelligent dialogue systems have become an important tool for many companies and organizations, applied in various scenarios, and have played a very important role in improving user experience.
[0003] At present, most common intelligent dialogue systems generate responses based on rules or templates, based on retrieval, or based on deep learning. Among them, generating responses based on deep learning is to use the entire dialogue sample directly as training data to train the dialogue model, but due to the complexity of the dialogue sample itself, the dialogue model cannot fully capture the inherent logical information in the dialogue text. Therefore, it is difficult for the dialogue model to deeply understand the contextual relationship of the dialogue sample, resulting in the dialogue model The responses are limited in accuracy, coherence, and diversity. Summary of the invention
[0004] In view of this, some embodiments of the present application provide a method for training a dialogue model, a dialogue implementation method and related devices, so that the trained dialogue model has accuracy, coherence and diversity in generating responses.
[0005] In a first aspect, an embodiment of the present application provides a method for training a dialogue model, the method comprising:
[0006] Obtaining several groups of dialogue samples, where the dialogue samples include at least one question sentence and at least one reply sentence, wherein the last reply sentence is marked as a true label;
[0007] Obtaining global text at the global level and speaker text at the speaker level from the conversation sample in a layered manner;
[0008] The global text and speaker text are input into the generative network for encoding and then decoding to obtain the predicted label;
[0009] Calculate the loss between the predicted label and the true label, and iteratively train the generative network according to the sum of the losses corresponding to several groups of dialogue samples until convergence to obtain a dialogue model;
[0010] In some embodiments, the generative network includes an encoder and a decoder, the encoder including a representation unit and a comprehension unit;
[0011] The representation unit is used to encode the input global text and speaker text respectively to obtain a global vector representation and a speaker vector representation;
[0012] The understanding unit includes multiple understanding branches, corresponding to the global level and each speaker level in the dialogue sample respectively. The understanding branch includes multiple cascaded reasoning modules, which are used to extract context features from the vector representation of the corresponding level to obtain the context feature vector of the corresponding level.
[0013] The decoder is used to decode the context feature vector corresponding to the global level and the context feature vector corresponding to the speaker level according to the generated word vector to obtain the predicted label;
[0014] In some embodiments, the representation unit includes a word segmentation encoding module, a semantic parsing module, and a position embedding module, wherein the word segmentation encoding module is used to perform a word segmentation operation on each sentence in the input text, encode each word, and obtain a sentence encoding;
[0015] The semantic parsing module is used to perform semantic parsing on the sentence encoding to obtain a semantic vector; the semantic vectors corresponding to each sentence in the input text constitute the semantic vector set of the input text;
[0016] The position embedding module is used to introduce the position information of each semantic vector into the input semantic vector set to obtain the vector representation of the input text;
[0017] In some embodiments, the representation unit further includes a first attention module for performing attention processing on the input vector representation; the vector representation output by the first attention module serves as the final vector representation of the input text.
[0018] In some embodiments, the reasoning module includes a feature extraction module, a second attention module, and a fusion module, wherein the feature extraction module is used to extract features from an input feature vector;
[0019] The second attention module is used to perform attention processing on the feature vector input to the feature extraction module and the feature vector output by the feature extraction module;
[0020] The fusion module is used to fuse the feature vector output by the feature extraction module and the feature vector output by the second attention module to obtain the context feature vector of the corresponding level.
[0021] In some embodiments, the feature extraction module includes a bidirectional long short-term memory network.
[0022] In some embodiments, the decoder includes a third attention module, a feedforward neural network, and a recurrent neural network;
[0023] The third attention module is used to perform attention processing on the generated word vector, the context feature vector corresponding to the global level, and the context feature vector corresponding to the speaker level;
[0024] The feedforward neural network is used to map the output of the third attention module;
[0025] The recurrent neural network decodes the output of the feedforward neural network to obtain the predicted label.
[0026] In a second aspect, one embodiment of the present invention provides a method for intelligent reply, comprising:
[0027] Get the conversations to be replied;
[0028] Input the dialogue to be replied into the dialogue model and output the reply sentence;
[0029] The dialogue model is trained using the method for training the dialogue reasoning model in the first aspect mentioned above.
[0030] In a third aspect, some embodiments of the present application provide an electronic device, including:
[0031] at least one processor, and
[0032] A memory communicatively connected to at least one processor, wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method of the first aspect.
[0033] In a fourth aspect, the present application further provides a non-transitory computer-readable storage medium, which stores computer-executable instructions, and the computer-executable instructions are used to enable an electronic device to execute the method of the first aspect.
[0034] Beneficial effects of the embodiments of the present application: Different from the prior art, the embodiments of the present application provide a method for training a dialogue model, the method comprising: obtaining several groups of dialogue samples, the dialogue samples including at least one question sentence and at least one reply sentence, wherein the last reply sentence is marked as a true label; obtaining global text at the global level and speaker text at the speaker level from the dialogue samples in layers; inputting the global text and speaker text into the generative network for encoding and decoding to obtain a predicted label; calculating the loss between the predicted label and the true label, and iteratively training the generative network according to the sum of the losses corresponding to several groups of dialogue samples until convergence to obtain a dialogue model. The method extracts text from dialogue samples at two different levels, namely the global level and the speaker level, and then iteratively trains the generative network by using the global text and speaker text at different levels after extraction as training sets. Compared with training the generative network directly with the complete dialogue text as the training set, the method helps the generative network understand the dialogue samples from multiple levels, which is conducive to the generative network better understanding the inherent logical relationship between the contexts in the dialogue samples, so that the trained dialogue model has accuracy, coherence and diversity in generating responses. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] One or more embodiments are exemplarily described by pictures in the corresponding drawings, and these exemplified descriptions do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and unless otherwise stated, the figures in the drawings do not constitute proportional limitations.
[0036] Figure 1 A schematic diagram of an operating environment for a method for training a dialogue model provided in one embodiment of the present application;
[0037] Figure 2 A schematic diagram of the structure of an electronic device provided in one embodiment of the present application;
[0038] Figure 3 A flowchart of a training method applied to a dialogue model in some embodiments of the present application;
[0039] Figure 4 A schematic diagram of a network structure generated in some embodiments of the present application;
[0040] Figure 5 This is a schematic diagram of the structure of the reasoning module in some embodiments of the present application;
[0041] Figure 6 This is a flowchart of a method for implementing a dialogue in some embodiments of the present application. DETAILED DESCRIPTION
[0042] The present application is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present application, but do not limit the present application in any form. It should be noted that, for those of ordinary skill in the art, several variations and improvements can also be made without departing from the concept of the present application. These all belong to the protection scope of the present application.
[0043] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0044] It should be noted that, if there is no conflict, the various features in the embodiments of the present application can be combined with each other, all within the scope of protection of the present application. In addition, although the functional module division is performed in the device schematic diagram and the logical order is shown in the flow chart, in some cases, the steps shown or described can be performed in a sequence different from the module division in the device or the flow chart. In addition, the words "first", "second", "third", etc. used herein do not limit the data and execution order, but only distinguish the same items or similar items with basically the same functions and effects.
[0045] Unless otherwise defined, all technical and scientific terms used in this specification have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used in this specification and in the specification of this application are only for the purpose of describing specific embodiments and are not intended to limit this application. The term "and / or" used in this specification includes any and all combinations of one or more of the related listed items.
[0046] In addition, the technical features involved in each embodiment of the present application described below can be combined with each other as long as they do not conflict with each other.
[0047] To facilitate understanding of the method provided in the embodiments of the present application, the nouns involved in the embodiments of the present application are first introduced:
[0048] (1) Neural Network
[0049] A neural network can be composed of neural units. Specifically, it can be understood as a neural network with an input layer, a hidden layer, and an output layer. Generally speaking, the first layer is the input layer, the last layer is the output layer, and the layers in between are all hidden layers. Among them, a neural network with many hidden layers is called a deep neural network (DNN). The work of each layer in a neural network can be described by the mathematical expression y=a(W·x+b). From a physical level, the work of each layer in a neural network can be understood as completing the transformation from input space to output space (i.e., from the row space to the column space of a matrix) through five operations on the input space (a set of input vectors). These five operations include: 1. Dimensionality increase / reduction; 2. Zoom in / out; 3. Rotation; 4. Translation; 5. "Bending". Among them, operations 2 and 3 are completed by "W·x", operation 4 is completed by "+b", and operation 5 is implemented by "a()". The word "space" is used here because the classified object is not a single thing, but a class of things. Space refers to the collection of all individuals of this type of thing. Among them, W is the weight matrix of each layer of the neural network. Each value in the matrix represents the weight value of a neuron in this layer. The matrix W determines the spatial transformation from the input space to the output space mentioned above, that is, the W of each layer of the neural network controls how to transform the space. The purpose of training a neural network is to finally obtain the weight matrix of all layers of the trained neural network. Therefore, the training process of a neural network is essentially to learn how to control spatial transformation, and more specifically to learn the weight matrix.
[0050] It should be noted that in the embodiments of the present application, the models used in the machine learning tasks are essentially neural networks. Common components in neural networks include convolutional layers, pooling layers, normalization layers, and inverse convolutional layers. By assembling these common components in neural networks, a model is designed. When the model parameters (weight matrices of each layer) are determined so that the model error meets the preset conditions or the number of model parameters is adjusted to reach a preset threshold, the model converges.
[0051] The convolution layer is configured with multiple convolution kernels, each of which is set with a corresponding step size to perform convolution operations on the image. The purpose of the convolution operation is to extract different features of the input image. The first convolution layer may only extract some low-level features such as edges, lines, and corners. Deeper convolution layers can iteratively extract more complex features from low-level features. The downsampling convolution layer is used to map a high-dimensional space to a low-dimensional space while maintaining the connection relationship / pattern between them (the connection relationship here refers to the connection relationship during convolution).
[0052] The reverse convolution layer (also called the upsampling convolution layer) is used to map a low-dimensional space to a high-dimensional space while maintaining the connection relationship / pattern between them (the connection relationship here refers to the connection relationship during convolution). Similarly, the reverse convolution layer is configured with multiple convolution kernels, each of which is set with a corresponding step size to perform deconvolution operations on the image. Generally, the framework library used to design neural networks (such as the PyTorch library) has a built-in upsumple() function, which can be called to achieve low-dimensional to high-dimensional spatial mapping.
[0053] Pooling is a method that imitates the human visual system to reduce the dimension of data or represent images with higher-level features. Common operations of pooling layers include maximum pooling, mean pooling, random pooling, median pooling, and combined pooling. Generally speaking, pooling layers are periodically inserted between convolutional layers of neural networks to achieve dimensionality reduction.
[0054] The normalization layer is used to normalize all neurons in the intermediate layer to prevent gradient explosion and gradient disappearance.
[0055] (2) Loss Function
[0056] In the process of training a neural network, because we want the output of the neural network to be as close as possible to the value we really want to predict, we can compare the current network's predicted value with the target value we really want, and then update the weight matrix of each layer of the neural network according to the difference between the two (of course, there is usually an initialization process before the first update, that is, pre-configuring parameters for each layer in the neural network). For example, if the network's predicted value is high, adjust the weight matrix to make it predict lower, and keep adjusting until the neural network can predict the target value we really want. Therefore, it is necessary to pre-define "how to compare the difference between the predicted value and the target value", which is the loss function or objective function, which are important equations used to measure the difference between the predicted value and the target value. Among them, taking the loss function as an example, the higher the output value (loss) of the loss function, the greater the difference, so the training of the neural network becomes a process of minimizing this loss as much as possible.
[0057] (3) Attention Mechanism
[0058] The principle of the Attention Mechanism is to imitate the attention mechanism of the human visual and cognitive system, by paying more attention to important parts when processing information, thereby improving processing efficiency and accuracy.
[0059] In traditional neural networks, the output of each neuron depends only on the output of all neurons in the previous layer, while in the attention mechanism, the output of each neuron not only depends on the output of all neurons in the previous layer, but can also be weighted according to different parts of the input data, that is, different weights are given to different parts. This allows the model to pay more attention to the key information in the input sequence, thereby improving the accuracy and efficiency of the model. It should be noted that the attention mechanism is not a pending neural network structure, but a general mechanism that can be applied to different neural network structures. For example, the attention mechanism can be used in a convolutional neural network to focus on important areas in the input image, and the attention mechanism can also be used in a recurrent neural network to focus on important parts of the input sequence.
[0060] The core idea of the attention mechanism is to enable the model to automatically determine which parts of the information are important and pay more attention to them according to the needs of the current task when processing sequence data. This mechanism calculates the correlation between each element in the input sequence and the current task goal, assigns a weight to each element, so that the model can focus more on the information most relevant to the current task.
[0061] In deep learning, the implementation of the attention mechanism usually includes the following steps: feature extraction, extracting features from each element in the input sequence so that the model can understand the meaning of these elements; calculating the attention score, calculating the similarity or correlation between each element and the current task target (such as the current word or current state), and obtaining an attention score. This score indicates the importance of the element to the current task; weighted summation, weighted summing the feature vectors of all elements with their attention scores to obtain a new vector. This vector contains a summary of the important information in the input sequence and can be used by the model for subsequent prediction or classification tasks.
[0062] (4) Multi-head attention mechanism
[0063] The multi-head in the multi-head attention mechanism is different from the convolution kernel in multiple convolution layers in a convolutional neural network. Multiple convolution layers in a convolutional neural network are equivalent to copying a single convolution network num_layers times, and each convolution layer can be operated independently. Multi-head attention can be understood as splitting the input feature values into smaller pieces, assigning a separate trainable weight parameter to each piece, and then sharing the same hidden layer output result. Each head cannot be regarded as a complete and independent codec architecture and operated separately.
[0064] Figure 1 1 is a schematic diagram of an operating environment of a method for training a gender recognition model provided in an embodiment of the present invention, including an electronic device 10.
[0065] The electronic device 10 is a device that can run according to a program and automatically and quickly process massive amounts of data. It is usually composed of a hardware system and a software system, such as a computer, a server, etc. The electronic device 10 can be a local device or a cloud device, such as a cloud server, a cloud host, a cloud service platform, a cloud computing platform, etc.
[0066] In the above Figure 1 Based on this, another embodiment of the present invention provides an electronic device 10, see Figure 1 , is a hardware structure diagram of an electronic device 10 provided in an embodiment of the present invention, specifically, as Figure 2 As shown, the electronic device 10 includes at least one processor 11 and a memory 12 ( Figure 2 (a bus connection and a processor are used as an example).
[0067] Among them, the processor 11 is used to provide computing and control capabilities to control the electronic device 10 to perform corresponding tasks, for example, to control the electronic device 10 to execute any one of the methods for training a dialogue model provided in the following embodiments of the invention or any one of the methods for intelligent dialogue provided in the following embodiments of the invention.
[0068] It can be understood that the processor 11 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0069] The memory 12 is a non-transient computer-readable storage medium, which can be used to store non-transient software programs, non-transient computer executable programs and modules, such as the program instructions / modules corresponding to the method for training the gender recognition model in the embodiment of the present invention, or the program instructions / modules corresponding to the method for identifying gender in the embodiment of the present invention. The processor 11 can implement the method for training the dialogue model in any of the following method embodiments by running the non-transient software programs, instructions and modules stored in the memory 12, and can implement the method for intelligent dialogue in any of the following method embodiments. Specifically, the memory 12 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage devices. In some embodiments, the memory 12 may also include a memory remotely arranged relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network and a combination thereof.
[0070] The following is a detailed description of the method for training a dialogue model provided by an embodiment of the present invention. Figure 3 , the method S100 includes but is not limited to the following steps:
[0071] S101: Obtain several groups of dialogue samples, where the dialogue samples include at least one question sentence and at least one reply sentence, wherein the last reply sentence is marked as a true label.
[0072] Among the common intelligent dialogue methods, the deep learning-based method is the main research method. The responses generated by this method are more personalized and natural. The response content generated by the deep learning method is obtained through dialogue sample training, so the obtained dialogue sample contains at least one question sentence; during the training process of the dialogue model, the learnable parameters need to be adjusted according to the real label and the predicted label, so the above dialogue sample includes at least one response sentence, and the response sentence is marked as the real label. For example, dialogue sample 1 {"How is the weather today?" "The weather is very good today."}, where "The weather is very good today." is the real label.
[0073] The above-mentioned groups of dialogue samples are text collections composed of multiple dialogue segments, which contain multiple dialogue segments, each of which contains at least one question sentence and at least one reply sentence, for training the required dialogue model. For example:
[0074] Dialogue sample 2 {[“Have you eaten?” “I have.” (True label)] Dialogue sample 3 [“How was your day?” “I had a great day.” (True label)] Dialogue sample 4 [“Are you in a good mood today?” “I am in a good mood today.” (True label)]}
[0075] The above sentence ends with a question mark, has the characteristics of asking questions, and the sentence that requires a reply is a question sentence, and the sentence that responds to the corresponding question sentence is a reply sentence. For example, dialogue sample 5 {"Is the TV series good? (question sentence)" "The TV series is very good. (reply sentence)"}
[0076] The reply sentence corresponding to the target question sentence is the true label, which indicates the correct answer or true state of each sample in the dialogue sample set, and indicates the expected value of the dialogue model learning prediction result. In deep learning, the true label is the reference point for calculating the accuracy of model prediction and adjusting model parameters. In order to make the model prediction as close to the true label as possible, the model parameters are continuously adjusted during the model training process.
[0077] S102: Obtaining global text at a global level and speaker text at a speaker level from the conversation sample in a layered manner.
[0078] In daily life, there are not only two-person conversations, but also multi-person conversations, and the conversation samples include more than one speaker. Therefore, the text content of the above conversation samples can be extracted from the global level and the speaker level, and the global text at the global level and the speaker text at the speaker level can be obtained. The global text and speaker text obtained are input into the generative network for training.
[0079] The global level refers to inputting the text content of the conversation sample as a whole into the generative network for training; the speaker level refers to extracting the corresponding text from the text content of the conversation sample according to the speaker, and then inputting the extracted text content set of each speaker into the conversation model for training. For example:
[0080] Dialogue sample 6 (global level) {A: "Have you eaten yet?" B: "I have eaten, how about you?" A: "I haven't eaten yet, how is your meal?"}, and the dialogue samples at the speaker level are respectively speaker A {"Have you eaten yet?" "I haven't eaten yet, how is your meal?"}, speaker B {"I have eaten, how about you?"}. The above is only an example of two interlocutors. It is understandable that a dialogue sample can also be a dialogue between three or four interlocutors.
[0081] The text content of the conversation samples is divided into the global level and the speaker level for text extraction. The obtained global text and speaker text are then input into the generative network for training. The generative network can better understand the inherent logical relationship of the input text, thereby improving the accuracy and coherence of the generated responses.
[0082] S103: The global text and the speaker text are input into the generation network for encoding and then decoding to obtain a predicted label.
[0083] In the generative network, the encoder completes the encoding process, that is, the encoder is responsible for processing the input sequence and converting it into an internal representation of a fixed length, which captures the key information of the input sequence. During the processing, the encoder gradually compresses the input information with the aim of encoding all necessary information into an abstract representation; the decoder completes the decoding process, that is, the decoder's task is to convert the internal representation output by the encoder into the target sequence. The decoder gradually generates the output sequence, and each step may depend on the output of the previous step and the internal representation passed from the encoder. During the generation process, the decoder gradually unpacks the information compressed by the encoder and converts it into meaningful output.
[0084] It can be understood that the predicted label is the predicted response of the generative network to the input text content, which is used to compare with the true label. The adjustable parameters of the generative network are adjusted according to the difference between the predicted label and the true label to improve the accuracy of the response generated by the generative network.
[0085] In some embodiments, see Figure 4 , the generation network includes an encoder and a decoder, and the encoder includes a representation unit and a comprehension unit.
[0086] The representation unit encodes the input global text and speaker text respectively to obtain a global vector representation and a speaker vector representation.
[0087] The understanding unit includes multiple understanding branches, which correspond to the global level and the speaker level in the dialogue sample respectively. The understanding branch includes multiple cascaded reasoning modules, which extract context features from the vector representation of the corresponding level to obtain the context feature vector of the corresponding level.
[0088] The decoder decodes the context feature vector corresponding to the global level and the context feature vector corresponding to the speaker level according to the generated word vector to obtain the predicted label.
[0089] After extracting text from the dialogue sample layer by layer, the encoder and decoder encode and decode the extracted text content to obtain the predicted label. The representation unit of the encoder converts the input text content into a vector representation of the text content. The vector representation of the global text obtained by the representation unit is the global vector representation; the vector representation of the speaker text obtained by the representation unit is the speaker vector representation. In some embodiments, the representation unit includes multiple downsampling convolutional layers.
[0090] The understanding unit is a part of the encoder, which contains multiple understanding branches, and the understanding branches are composed of multiple cascaded reasoning modules. The above understanding unit gradually mines and integrates the context information in the dialogue sample through the reasoning module, helping the generation network to better understand the context of the dialogue sample.
[0091] In some embodiments, the above-mentioned understanding branches each correspond to a vector representation at a different level (e.g. Figure 4 As shown in the figure, vector representations at different levels are input into the understanding unit, and the reasoning process and retrieval process are iteratively executed through the reasoning modules of the corresponding understanding branches, so as to help the generation network better understand the internal logic of the vector representation of the input understanding unit, thereby improving the accuracy and coherence of the generated response.
[0092] In some embodiments, the representation unit includes a word segmentation encoding module, a semantic parsing module and a position embedding module, wherein the word segmentation encoding module is used to perform word segmentation operations on each sentence in the input text, encode each word, and obtain sentence encoding.
[0093] The semantic analysis module is used to perform semantic analysis on the sentence encoding to obtain a semantic vector; the semantic vector corresponding to each sentence in the input text constitutes the semantic vector set of the input text.
[0094] The position embedding module is used to introduce the position information of each semantic vector into the input semantic vector set to obtain the vector representation of the input text.
[0095] After the acquired global text and speaker text are input into the representation unit, the word segmentation encoding module first obtains the sentence encoding of the corresponding input based on methods well known to those skilled in the art, such as a word segmentation method based on string matching, a word segmentation method based on understanding, and a word segmentation method based on statistics. In some embodiments, the word segmentation encoding module cuts the above text content into the smallest semantic unit token, and then converts the token into a numerical id, i.e., a position code, and provides it to the model for learning.
[0096] The semantic parsing module vectorizes the above sentence encoding to obtain the vector representation of the corresponding sentence. In some embodiments, the above semantic parsing module includes a bidirectional gated recurrent unit (Bi-GRU). The gated recurrent unit (GRU) is a variant of the traditional recurrent neural network (RNN). Compared with the RNN, the GRU includes a reset gate and an update gate. The amount of information passed to the next time step is controlled through the gating mechanism, which can effectively capture the semantic association between long sequences.
[0097] The sentence encoding output by the word segmentation encoding module is input into Bi-GRU, and Bi-GRU outputs the vector representation of the sentence encoding, that is, the semantic vector. The semantic vectors corresponding to each sentence in the above input text constitute the semantic vector set of the input text. For example, Bi-GRU vectorizes the input sentence encoding and uses the last hidden state as the representation of the sentence. , that is, the semantic vector of the current sentence is , the semantic vector representations of the N sentences obtained are represented by sets, that is, the semantic vector set of the input text is .
[0098] It is understandable that Bi-GRU utilizes both forward and backward information flows, allowing the model to better understand and process complex time series data. Compared with RNN, Bi-GRU can better handle tasks with contextual relevance, such as speech recognition, machine translation, etc. In addition, since Bi-GRU has a relatively simple structure and fewer parameters, it also has advantages in training and deployment.
[0099] In natural language processing tasks, when processing sequence data, position embedding can ensure that the model captures the position information corresponding to each element in the sequence to understand the semantics and structure of the sequence. Position embedding is a technology that encodes the position information of each element in the sequence into a vector form. In some embodiments, position embedding includes position encoding. By using sine and cosine functions of different frequencies, position encoding can capture the relative distance and order between different positions. For example, the position encoding of the above semantic vector set is vectorized to obtain the position encoding vector set , combining the above position encoding vector set with the semantic vector set to obtain the set of corresponding sentence vector representations, that is, .
[0100] The above position encoding vector can serve as an additional feature of the model input, helping the model to better understand the structure and meaning of the sequence, thereby better processing the order and correlation of sequence data.
[0101] In some embodiments, the representation unit further includes a first attention module for performing attention processing on the input vector representation; the vector representation output by the first attention module serves as the final vector representation of the input text.
[0102] In some embodiments, the first attention module includes a self-attention mechanism. The self-attention mechanism is specifically used to process the relationship within a single sequence, such as the relationship between words in a sentence and / or the relationship between sentences in a text paragraph. The self-attention mechanism allows the model to learn the dependencies within the sequence, such as the relationship between words, the relationship between parts of a sentence, etc., by calculating the attention of each element in the sequence to all other elements. For example, the set of corresponding sentence vectors is represented by The vector representation of the corresponding level is obtained through the attention mechanism, that is, It can be understood that the vector representation of speaker A and speaker B are respectively: , .
[0103] The self-attention mechanism is a special form of the attention mechanism that reduces the reliance on external information and is better at capturing the internal correlation of data or features. In this embodiment, the self-attention mechanism provides a powerful tool for processing complex sequence data problems through its efficient internal sequence modeling capabilities, which not only improves the performance of the model, but also greatly enhances the model's ability to handle long-distance dependencies.
[0104] In some embodiments, the reasoning module includes a feature extraction module, a second attention module, and a fusion module, wherein the feature extraction module is used to extract features from an input feature vector;
[0105] The second attention module is used to perform attention processing on the feature vector input to the feature extraction module and the feature vector output by the feature extraction module.
[0106] The fusion module is used to fuse the feature vector output by the feature extraction module and the feature vector output by the second attention module to obtain the context feature vector of the corresponding level.
[0107] In some embodiments, the feature extraction module includes a bidirectional long short-term memory network (Bi-LSTM).
[0108] Bidirectional LSTM is a type of LSTM network. Bi-LSTM does not have the internal structure of LSTM itself. It just applies LSTM twice in different directions and then concatenates the results of the two times as the final output.
[0109] In this embodiment, a bidirectional long short-term memory network is used to simulate the human cognitive reasoning process, for example:
[0110]
[0111] in, is the output vector. is represented by the global context Initialization is obtained, that is: ,in and is a learnable parameter.
[0112] The structure of Bi-LSTM can capture some specific pre- or post-position features in language grammar and enhance semantic association. By learning the intrinsic logical order between the vector representations of the corresponding levels through bidirectional Bi-LSTM, it can not only further extract features in the sequence and capture more complex sequence structures and dependencies, but also process sequences of variable lengths and / or batch process sequences of different lengths.
[0113] The second attention module includes the attention mechanism, such as Figure 5As shown in the figure, the above attention mechanism helps the model pay more attention to important features through weight assignment. It assigns a weight to each input item of the generated network, which represents the degree of attention paid by the generated network to this part. The size of the weight can be calculated by the softmax function, for example:
[0114]
[0115] Among them, Q, V, K represent the Queries, Keys, and Values matrices of the input sentence respectively. Each row of the matrix is the Query, Key, and Value vector corresponding to each word. Represents the length of a vector.
[0116] The second attention module uses the attention mechanism to match the vector representation at the global level, that is, to simulate the retrieval process. Through the above process, it helps the dialogue model to focus on important features more accurately and strengthens the judgment and decision-making capabilities of the dialogue model in key parts.
[0117] For example, in the training of the dialogue model, the detailed calculation of the tth word in the output prediction label is as follows:
[0118] f() is the vector multiplication function, which is and Calculate the matrix multiplication function. Multiply Q by the transposed matrix of K, and then perform softmax on it (normalize the scores of all words so that all words are positive and the sum is 1). For the obtained softmax score, we multiply it by each corresponding Value vector, that is,
[0119]
[0120] The fusion module includes residual links and normalization, such as Figure 5 As shown in the figure, the residual link adds the input and output of the network to increase the network depth without losing the initial features; normalization performs linear or nonlinear scaling on the input or output of the network layer and maps it to a specific range or distribution to improve the training stability and performance of the generated network. Since the residual link adds the original features, the generated network has the problem of gradient disappearance. Therefore, normalization is used to reduce calculations and reduce the risk of network gradient disappearance.
[0121] In some embodiments, the fusion module combines the output of the reasoning process The output of the retrieval process Fusion to get vector , which is used to input into the next reasoning module to continue the reasoning and retrieval steps, for example:
[0122]
[0123] in is the weight system, is the matrix multiplication. In summary, given the global context representation and the number of reasoning rounds , the entire understanding unit can be expressed as:
[0124]
[0125] It can be understood that, combining the context representation at the speaker A level and the context representation at the speaker B level, the entire understanding unit can be expressed as:
[0126]
[0127]
[0128] Discourse representation after comprehension unit The concatenation of the above output vectors is as follows:
[0129]
[0130] Residual connections introduce an identity mapping, allowing the original input information to be directly passed to deeper layers, thereby alleviating the gradient vanishing problem to a certain extent, making it easier for the network to learn the identity mapping or transformations close to the identity mapping during training, which helps the model converge to the optimal solution faster. Through residual connections, the model can more easily learn nonlinear transformations of input data, thereby improving the model's representation ability.
[0131] Normalization standardizes the activation values of each layer so that the input distribution of each layer remains relatively stable, which accelerates the training process of the model. Similarly, standardization reduces the model's dependence on parameter initialization and makes it more resilient to changes in the distribution of input data, thereby improving the stability of the model and making it easier to find the optimal solution.
[0132] In some embodiments, the decoder includes a third attention module, a feedforward neural network, and a recurrent neural network;
[0133] The third attention module is used to perform attention processing on the generated word vector, the context feature vector corresponding to the global level, and the context feature vector corresponding to the speaker level;
[0134] The feedforward neural network is used to map the output of the third attention module;
[0135] The recurrent neural network decodes the output of the feedforward neural network to obtain the predicted label.
[0136] The third attention module includes self-attention mechanism and multi-head self-attention mechanism. Multi-Head Attention mechanism is an extension of the attention mechanism. It uses multiple sets of different query (q), key (k) and value (v) matrices for each position of the input sequence, calculates the attention weight between each position and other positions, and then performs weighted summation on the sequence.
[0137] The generated word vector refers to the vector representation of the reply word that the network has generated before the current moment. For example, if the current generated word is the tth word, the generated word vector is represented as the vector representation of the first word to the t-1th word. The vector representation of the generated word is used as input through the third attention module to obtain a representation with multi-head self-attention, for example:
[0138] To generate the tth word , firstly, the t-1 words generated previously As input, get the vector representation of the generated word:
[0139]
[0140] in Targeted response The embedding vector of the generated word is .
[0141] The multi-head self-attention mechanism adds multiple attention heads to allocate attention to different dimensions of the input information in parallel on the basis of the self-attention mechanism, and processes multiple attention distributions in parallel, further enhancing the model's attention capture ability, thereby capturing richer features and contextual information, and improving the model's expressiveness and learning efficiency.
[0142] A feedforward neural network (FNN) is a computational model consisting of multiple neurons connected to each other, in which each neuron receives input signals from other neurons and passes output signals to the next neuron. Unlike RNN, the information processing method of FNN is unidirectional, passing from the input layer to the output layer in sequence.
[0143] The basic structure of FNN includes input layer, hidden layer and output layer, as well as corresponding activation functions, weights and biases. These components together constitute the overall picture of the network. The input layer is responsible for receiving raw data, usually corresponding to the dimension of the feature; the hidden layer contains one or more layers, each layer consists of multiple neurons, which are used to extract abstract features of the input data; the output layer produces the final prediction or classification result of the network; the activation function introduces nonlinear characteristics to the above feedforward neural network, enabling the network to learn complex functions; the weight connects the linear factors of neurons in each layer and controls the flow of information between neurons; the bias allows neurons to be activated without input, increasing the flexibility of the model. Weights and biases are learnable parameters of neural networks, which can be continuously adjusted during training to minimize prediction errors.
[0144] The input of the feedforward layer first undergoes a linear transformation to map the input to a high-dimensional space. This linear transformation is usually implemented by a weight matrix and a bias vector. After the linear transformation, the input passes through an activation function, such as ReLU, Sigmoid, Tanh, etc., to increase the nonlinear expression ability of the model. After the activation function, another linear transformation is performed to map the features of the high-dimensional space back to the original space to obtain the output of the feedforward layer.
[0145] It is understood that in some embodiments, the third attention module uses another multi-head attention in response to the historical representation As a query, as keys and values, and then output representation through a feedforward neural network :
[0146]
[0147] The feedforward layer can extract complex features of the input data through linear transformation and activation function, which helps the generative network to better understand the data. By increasing the number of hidden units in the feedforward layer, the complexity of the generative network can be increased and the expressiveness of the generative network can be improved. In deep neural networks, gradient disappearance is a common problem. The activation function in the feedforward layer can alleviate this problem, allowing the generative network to better generate reply responses.
[0148] A recurrent neural network (RNN) is an artificial neural network that uses sequence data or time series data. It has a memory mechanism and can store previous information for use when processing sequences. The above recurrent neural network includes a gated recurrent unit (GRU). GRU can solve problems such as the inability to have long-term memory in RNN and gradients in back propagation. It combines the forget gate and the input gate into a single "update gate", merges the cell state and the hidden state, and makes some other changes to form a more simplified model. The core process of GRU is reset gate->update gate->candidate hidden state->hidden state; among them, the reset gate helps capture short-term dependencies in the sequence; the update gate helps capture long-term dependencies in the sequence.
[0149] In some embodiments, a GRU model is used as a decoder to generate a reply, and the decoder decoding process is represented by the following formula:
[0150]
[0151] in is the hidden state of GRU at time t.
[0152] Compared with ordinary RNN, GRU has a simpler structure and better performance. It requires less training time than other types of recurrent neural networks, can effectively capture long-distance dependencies in sequences, and has better adaptability to variable-length sequences.
[0153] S104: Calculate the loss between the predicted label and the true label, and iteratively train the generated network according to the sum of the losses corresponding to several groups of dialogue samples until convergence to obtain a dialogue model.
[0154] Loss is an overall indicator to measure the prediction inaccuracy of the machine learning model on the entire data set. By minimizing the loss, the model parameters can be optimized and the prediction performance can be improved. The specific calculation of the loss is completed through the loss function, which receives the model's predicted label and true label as input and outputs a scalar value, namely the loss value. The loss values corresponding to the elements of the data set are summed up to obtain the loss sum, which represents the overall prediction error of the model on the entire data set.
[0155] The loss function is a function used in machine learning and deep learning to measure the difference between the model's predicted label and the true label. Different tasks and models may require different loss functions. The loss function is located between the forward propagation and backward propagation of the machine learning model. In the forward propagation stage, the model generates predicted values based on the input features; the loss function receives these predicted values and calculates the difference between them and the true value; the above difference is then used in the backward propagation stage to update the model's parameters and reduce the next prediction error.
[0156] In some embodiments, the output of the FNN is passed through a softmax layer to obtain a prediction value. The softmax layer is usually used as an activation function of the output layer of the neural network, especially in multi-class classification problems, and its role is to convert the original class scores into a probability distribution so that the sum of the probabilities of all classes is 1. Therefore, the output of the neural network can be interpreted as the probability of each class, and the wrong classification is penalized by the calculation formula of the logarithmic loss function, thereby achieving an accurate measurement of the classifier, for example:
[0157] The hidden layer representation at time t by the aforementioned gated recurrent unit (GRU) To obtain the word probability, the formula is as follows:
[0158]
[0159] in is a trainable parameter. Corresponding reply response sequence The log-likelihood of is:
[0160]
[0161] In some embodiments, the loss function of the generated network is a logarithmic loss function. Logarithmic loss is a commonly used loss function that is widely used when training classification models. When the predicted probability of the model is closer to the actual label, the logarithmic loss is smaller, so it can help the model better fit the data and improve the classification accuracy.
[0162] In summary, the training method of the dialogue model in the embodiment of the present application extracts dialogue samples in layers, obtains vector representations at the global level and vector representations at the speaker level and inputs them into the understanding unit. The understanding unit performs reasoning and retrieval on the dialogue samples in layers, which facilitates the generation network to understand the dialogue samples from multiple angles, and can better understand the internal logic of the dialogue samples and improve the coherence of the generated responses. At the same time, the understanding unit inputs the input global-level vector representation and the speaker-level vector representation into their respective corresponding understanding branches, and the cascaded reasoning modules in the corresponding understanding branches iteratively perform reasoning and retrieval on the input vector representations. Therefore, the generation network fully learns the internal logic information of the dialogue samples, gradually mines and integrates the contextual information in the dialogue samples, and improves the accuracy of the generated responses.
[0163] The following describes the method for implementing a conversation provided by the embodiment of the present application in conjunction with the exemplary application and implementation of the terminal provided by the embodiment of the present application. Figure 6 , Figure 6 200 is a flow chart of a method for implementing a dialogue provided in an embodiment of the present application. The method S200 comprises the following steps:
[0164] S201: Obtain the conversation to be replied.
[0165] The dialogue to be replied is the dialogue text to be replied, and the last sentence is a question. In some embodiments, the chat scene in which the dialogue to be replied is located can be a multi-person chat or a two-person chat.
[0166] S202: Input the dialogue to be replied to into the dialogue model and output the reply statement.
[0167] The dialogue model is trained by using the method for training a dialogue model in any of the above embodiments. A sentence to be replied is input into the dialogue model, and the dialogue model outputs a corresponding reply sentence.
[0168] It can be understood that the dialogue model is trained by the method for training a dialogue model in the above embodiment, and has the same structure and function as the dialogue model in the above embodiment, which will not be described in detail here.
[0169] The present application also provides a computer-readable storage medium, which stores computer-executable instructions for causing an electronic device to execute the method for training a dialogue model provided in the present application. For example, Figure 5 The method for training the dialogue model shown or the dialogue implementation method provided in the embodiment of the present application.
[0170] In some embodiments, the storage medium may be a memory such as FRAM, ROM, PROM, EPROM, EE PROM, flash memory, magnetic surface memory, optical disk, or CD-ROM; or it may be various devices including one or any combination of the above memories.
[0171] In some embodiments, executable instructions may be in the form of a program, software, software module, script or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a stand-alone program or as a module, component, subroutine or other unit suitable for use in a computing environment.
[0172] As an example, executable instructions may, but need not, correspond to a file in a file system, may be stored as part of a file storing other programs or data, such as in one or more scripts in a HyperText Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple coordinated files (e.g., files storing one or more modules, subroutines, or code portions).
[0173] As an example, executable instructions may be deployed to be executed on one computing device (including devices such as smart terminals and servers), or on multiple computing devices located in one location, or on multiple computing devices distributed in multiple locations and interconnected by a communication network.
[0174] An embodiment of the present application also provides a computer-readable storage medium, which stores a computer program. The computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method for training a dialogue model or the method for realizing dialogue in the aforementioned embodiment.
[0175] It should be noted that the device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0176] Through the description of the above embodiments, it can be clearly understood by those skilled in the art that each embodiment can be implemented by means of software plus a general hardware platform, or by hardware. It can be understood by those skilled in the art that all or part of the processes in the above embodiment methods can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.
[0177] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Under the concept of the present application, the technical features in the above embodiments or different embodiments can also be combined, the steps can be implemented in any order, and there are many other changes in different aspects of the present application as described above, which are not provided in detail for the sake of simplicity. Although the present application has been described in detail with reference to the aforementioned embodiments, a person of ordinary skill in the art should understand that the technical solutions described in the aforementioned embodiments can still be modified, or some of the technical features can be replaced by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for training a dialogue model, characterized in that: The method comprises: Acquire several groups of dialogue samples, where the dialogue samples include at least one question sentence and at least one reply sentence, wherein the last reply sentence is marked as a true label; Obtaining a global text at a global level and a speaker text at a speaker level from the conversation sample in a layered manner; Inputting the global text and the speaker text into a generative network for encoding and then decoding to obtain a predicted label; Calculating the loss between the predicted label and the true label, and iteratively training the generative network according to the sum of the losses corresponding to the plurality of groups of dialogue samples until convergence, to obtain the dialogue model; The generation network includes an encoder and a decoder, wherein the encoder includes a representation unit and a comprehension unit; The representation unit is used to encode the input global text and the speaker text respectively to obtain a global vector representation and a speaker vector representation; The understanding unit includes a plurality of understanding branches, which respectively correspond to the global level and each speaker level in the dialogue sample, and the understanding branch includes a plurality of cascaded reasoning modules, which are used to extract context features from the vector representation of the corresponding level to obtain the context feature vector of the corresponding level; The decoder is used to decode the context feature vector corresponding to the global level and the context feature vector corresponding to the speaker level according to the generated word vector to obtain the predicted label.
2. The method according to claim 1, characterized in that: The representation unit includes a word segmentation encoding module, a semantic parsing module and a position embedding module, wherein the word segmentation encoding module is used to perform word segmentation operations on each sentence in the input text, encode each word, and obtain sentence encoding; The semantic parsing module is used to perform semantic parsing on the sentence code to obtain a semantic vector; the semantic vectors corresponding to each sentence in the input text constitute a semantic vector set of the input text; The position embedding module is used to introduce the position information of each semantic vector into the input semantic vector set to obtain the vector representation of the input text.
3. The method according to claim 2, characterized in that The representation unit also includes a first attention module, which is used to perform attention processing on the input vector representation; the vector representation output by the first attention module serves as the final vector representation of the input text.
4. The method according to claim 1, characterized in that: The reasoning module includes a feature extraction module, a second attention module and a fusion module, wherein the feature extraction module is used to extract features from an input feature vector; The second attention module is used to perform attention processing on the feature vector input to the feature extraction module and the feature vector output by the feature extraction module; The fusion module is used to fuse the feature vector output by the feature extraction module and the feature vector output by the second attention module to obtain the context feature vector of the corresponding level.
5. The method according to claim 4, characterized in that The feature extraction module includes a bidirectional long short-term memory network.
6. The method according to claim 1, characterized in that The decoder includes a third attention module, a feedforward neural network and a recurrent neural network; The third attention module is used to perform attention processing on the generated word vector, the context feature vector corresponding to the global level and the context feature vector corresponding to the speaker level; The feedforward neural network is used to perform mapping processing on the output of the third attention module; The recurrent neural network decodes the output of the feedforward neural network to obtain the predicted label.
7. A method for implementing a dialogue, characterized in that: include: Get the conversations to be replied; Inputting the dialogue to be replied into the dialogue model and outputting a reply sentence; The dialogue model is trained using the method for training a dialogue model as described in any one of claims 1-6.
8. An electronic device, characterized in that: include: at least one processor, and A memory communicatively connected to the at least one processor, wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor so that the at least one processor can execute the method of any one of claims 1-7.
9. A non-transitory computer-readable storage medium, characterized in that: The non-transitory computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable an electronic device to execute the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Multi-round dialogue method and system fusing context information
CN112818105A
Speech synthesis model training method, speech synthesis method and speech synthesis device
CN116052638A
Context-aware dialogue information extraction system and method
CN117933226A
Human-machine collaborative conversation interaction system and method
US20230394247A1
Method and apparatus for training text generation model, and storage medium and computer device
WO2023108981A1
