Text generation model training method and device, storage medium and computer device

By extracting text and topic vectors from multi-turn dialogues from a deep learning model and constructing a loss function for iterative training, the problem of insufficient relevance between response text and context topic is solved, thereby improving the generation accuracy and logical fit of response text.

CN114238549BActive Publication Date: 2025-10-24PING AN TECH (SHENZHEN) CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202111534333.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-15
Publication Date
2025-10-24
Estimated Expiration
2041-12-15

AI Technical Summary

Technical Problem

Existing deep learning models fail to effectively consider the contextual relevance of multi-turn dialogues when generating response text, resulting in response texts deviating from the overall logic of multi-turn dialogues and insufficient generation accuracy.

Method used

By acquiring the question text and context text from multi-turn dialogues, a text vector matrix is ​​generated using an initial text vector generation model, and context and response text topic vectors are extracted using an initial topic vector extraction model. A loss function is constructed and iteratively trained to ensure that the response text is deeply related to the context topic.

Benefits of technology

It improves the accuracy of response text generation in multi-turn dialogues, making the generated response text deeply relevant to the context of the dialogue topic and in line with the overall logic of multi-turn dialogues.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114238549B_ABST
    Figure CN114238549B_ABST
Patent Text Reader

Abstract

The application discloses a text generation model training method and device, a storage medium and computer equipment, and relates to the technical field of machine learning. The method comprises the following steps: obtaining question text and multiple context texts in a multi-turn dialogue; generating a first text vector matrix corresponding to the multiple context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model; extracting a context text theme vector corresponding to the first text vector matrix and a reply text theme vector corresponding to the reply text vector matrix by using an initial theme vector extraction model; constructing a loss function based on the context text theme vector and the reply text theme vector; and iteratively training the initial text vector generation model and the initial theme vector extraction model based on the loss function to construct a text generation model. The application is suitable for training of a text generation model.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of machine learning, in particular to a text generation model training method and device, a storage medium and a computer device. BACKGROUND

[0002] In natural language processing, multi-turn dialogue is always a research hotspot, and how to accurately and effectively generate reply text for multi-turn dialogue is of great significance.

[0003] At present, usually based on the understanding of the context in the multi-turn dialogue, a deep learning model is used to generate the corresponding reply text. However, in the process of training the deep learning model, whether the generated reply text is deeply related to the context dialogue topic in the multi-turn dialogue is not considered, which leads to the deviation of the generated reply text from the overall logic of the multi-turn dialogue, and cannot guarantee the generation accuracy of the reply text. SUMMARY

[0004] The present application provides a text generation model training method and device, a storage medium and a computer device, which can ensure that the generated reply text is deeply related to the context dialogue topic, so as to make the generated reply text fit the overall logic of the multi-turn dialogue and improve the generation accuracy of the reply text.

[0005] According to a first aspect of the present application, a text generation model training method is provided, comprising:

[0006] obtaining question text and a plurality of context texts in a multi-turn dialogue;

[0007] generating a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model;

[0008] extracting a context text topic vector corresponding to the first text vector matrix and a reply text topic vector corresponding to the reply text vector matrix by using an initial topic vector extraction model;

[0009] constructing a loss function based on the context text topic vector and the reply text topic vector;

[0010] iteratively training the initial text vector generation model and the initial topic vector extraction model based on the loss function to construct a text generation model.

[0011] According to a second aspect of the present application, a text generation model training device is provided, comprising:

[0012] an acquisition unit configured to acquire question text and a plurality of context texts in a multi-turn dialogue;

[0013] a generation unit configured to generate, by using an initial text vector generation model, a first text vector matrix corresponding to the plurality of context texts and a reply text vector matrix corresponding to the question text;

[0014] an extraction unit configured to extract, by using an initial topic vector extraction model, a context text topic vector corresponding to the first text vector matrix and a reply text topic vector corresponding to the reply text vector matrix;

[0015] a construction unit configured to construct a loss function based on the context text topic vector and the reply text topic vector;

[0016] a training unit configured to iteratively train the initial text vector generation model and the initial topic vector extraction model based on the loss function, and construct a text generation model.

[0017] According to a third aspect of the present application, there is provided a computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements the following steps:

[0018] obtaining a question text and a plurality of context texts in a multi-turn dialogue;

[0019] generating, by using an initial text vector generation model, a first text vector matrix corresponding to the plurality of context texts and a reply text vector matrix corresponding to the question text;

[0020] extracting, by using an initial topic vector extraction model, a context text topic vector corresponding to the first text vector matrix and a reply text topic vector corresponding to the reply text vector matrix;

[0021] constructing a loss function based on the context text topic vector and the reply text topic vector;

[0022] iteratively training the initial text vector generation model and the initial topic vector extraction model based on the loss function, and constructing a text generation model.

[0023] According to a fourth aspect of the present application, there is provided a computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein the processor implements the following steps when executing the program:

[0024] obtaining a question text and a plurality of context texts in a multi-turn dialogue;

[0025] generating, by using an initial text vector generation model, a first text vector matrix corresponding to the plurality of context texts and a reply text vector matrix corresponding to the question text;

[0026] extract a context text topic vector corresponding to the first text vector matrix and a reply text topic vector corresponding to the reply text vector matrix by using an initial topic vector extraction model respectively;

[0027] construct a loss function based on the context text topic vector and the reply text topic vector;

[0028] iteratively train the initial text vector generation model and the initial topic vector extraction model based on the loss function, and construct a text generation model.

[0029] According to the text generation model training method, device, storage medium and computer equipment provided by the present application, compared with the current method of generating a corresponding reply text by using a deep learning model, the present application obtains a question text and a plurality of context texts in a multi-turn dialogue; and generates a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model. At the same time, the present application extracts a context text topic vector corresponding to the first text vector matrix and a reply text topic vector corresponding to the reply text vector matrix by using an initial topic vector extraction model respectively; and constructs a loss function based on the context text topic vector and the reply text topic vector. Finally, the present application iteratively trains the initial text vector generation model and the initial topic vector extraction model based on the loss function, and constructs a text generation model. Thus, by extracting a context topic vector and a reply text topic vector, and constructing a loss function based on the context text topic vector and the reply text topic vector, the present application trains the text generation model, which can make the reply text topic vector and the context topic vector as similar as possible, thereby ensuring that the generated reply text is deeply related to the context dialogue topic, making the generated reply text fit the overall logic of the multi-turn dialogue, and further improving the generation accuracy of the reply text in the multi-turn dialogue. BRIEF DESCRIPTION OF DRAWINGS

[0030] The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this application, illustrate embodiments of the application and together with the description serve to explain the application. In the drawings:

[0031] Figure 1 a flow chart of a text generation model training method provided by an embodiment of the present application is shown;

[0032] Figure 2 a flow chart of another text generation model training method provided by an embodiment of the present application is shown;

[0033] Figure 3A structure schematic diagram of a training device of a text generation model provided by an embodiment of the present application is shown.

[0034] Figure 4 A structure schematic diagram of another training device of a text generation model provided by an embodiment of the present application is shown.

[0035] Figure 5 A physical structure schematic diagram of a computer device provided by an embodiment of the present application is shown. DETAILED DESCRIPTION

[0036] The present application will be described in detail below with reference to the accompanying drawings and in conjunction with embodiments. It should be noted that the embodiments in the present application and the features in the embodiments can be combined with each other without conflict.

[0037] Currently, in the process of training a deep learning model, whether the generated reply text is deeply related to the context conversation topic in the multi-turn conversation is not considered, which leads to that the reply text generated by using the deep learning model deviates from the overall logic of the multi-turn conversation, and the generation accuracy of the reply text cannot be guaranteed.

[0038] In order to solve the above problems, an embodiment of the present application provides a training method of a text generation model, as shown in the following formula: Figure 1 The method comprises the following steps.

[0039] 101、Obtain question text and a plurality of context texts in a multi-turn conversation.

[0040] Among them, the multi-turn conversation includes at least three sentence texts, the last sentence text is taken as the question text, and the other sentence texts are taken as the context texts. In order to overcome the defect that the generated reply text cannot be deeply related to the context conversation topic in the prior art, which leads to that the generated reply text deviates from the overall logic of the multi-turn conversation, in the process of training the text generation model, an embodiment of the present application can extract a context topic vector and a reply text topic vector, and use the context topic vector and the reply text topic vector to construct a loss function, and train the text generation model based on the loss function, so as to ensure that the reply text generated by using the text generation model can be deeply related to the context conversation topic, and fit the overall logic of the multi-turn conversation. An embodiment of the present application is mainly applied to the scene of training a reply text generation model in a multi-turn conversation. The execution subject of the embodiment of the present application is a device or equipment capable of training the reply text generation model, which can be specifically arranged on the server side.

[0041] For the embodiment of the application, in order to train the reply text generation model of the multi-turn dialogue, it is necessary to collect sample sentences in the multi-turn dialogue in advance, at least three sample sentences are included in a multi-turn dialogue, the last sample sentence is taken as the question text, and the other sample sentences are taken as the context text. During the training of the text generation model, the real reply text corresponding to the question text is known. For example, four sample sentences are included in the multi-turn dialogue, sample sentence 1 is "Let's go out to eat!", sample sentence 2 is "It's not a good day today, where do you want to eat?", sample sentence 3 is "I know a newly opened store, you can take the subway to eat there.", and sample sentence 4 is "That's good, you can't be drenched by the rain.", during the training of the text generation model, the above sample sentence 1, sample sentence 2 and sample sentence 3 are taken as the context text, and the sample sentence 4 (the last sample sentence) is taken as the question text. Then, based on the context text, the predicted reply text corresponding to the question text is predicted, and the reply text topic vector and the context text topic vector are extracted. Finally, based on the predicted reply text, the real reply text, the reply text topic vector and the context text topic vector, a loss function is constructed, and the text generation model is trained based on the loss function.

[0042] 102. Generating, by using the initial text vector generation model, the first text vector matrix corresponding to the plurality of context texts respectively and the reply text vector matrix corresponding to the question text.

[0043] The initial text vector generation model includes an initial encoder, an initial decoder and a first initial feedforward neural network. For the embodiment of the application, during the training of the text generation model, in order to generate the first text vector matrix corresponding to the plurality of context texts respectively and the reply text vector matrix corresponding to the question text, step 102 specifically includes: inputting the embedding vector matrix corresponding to the plurality of context texts respectively into the initial encoder for encoding to obtain the first text vector matrix corresponding to the plurality of context texts respectively; splicing the plurality of first text vector matrices to obtain the context text vector matrix corresponding to the multi-turn dialogue; inputting the context text vector matrix and the embedding vector corresponding to the question text into the initial decoder for decoding to obtain the second text vector matrix corresponding to the question text; and inputting the second text vector matrix into the first initial feedforward neural network to obtain the reply text vector matrix corresponding to the question text.

[0044] Further, the inputting the embedding vector matrix corresponding to the plurality of context texts into the initial encoder for encoding to obtain the first text vector matrix corresponding to the plurality of context texts respectively comprises: performing linear transformation on the embedding vectors corresponding to the plurality of context texts respectively to obtain a first query matrix, a first key matrix and a first value matrix corresponding to the plurality of context texts respectively; performing transpose processing on the first key matrix, multiplying the transposed first key matrix with the first query matrix to obtain an attention intensity matrix corresponding to the plurality of context texts respectively; inputting the attention intensity matrix into a softmax layer in the initial encoder for normalization processing to obtain an attention coefficient matrix corresponding to the plurality of context texts respectively; and multiplying the attention coefficient matrix with the first value matrix to obtain the first text vector matrix corresponding to the plurality of context texts respectively. Meanwhile, the initial decoder comprises a first attention layer and a second attention layer, and the inputting the context text vector matrix and the embedding vector corresponding to the question text into the initial decoder for decoding to obtain the second text vector matrix corresponding to the question text comprises: inputting the embedding vector corresponding to the question text into the first attention layer for text feature extraction to obtain a third text vector matrix corresponding to the question text; determining a second query matrix corresponding to the question text according to the third text vector matrix; determining a second key matrix and a second value matrix corresponding to the question text according to the context text vector matrix respectively; and calculating the second text vector matrix corresponding to the question text according to the second query matrix, the second key matrix and the second value matrix.

[0045] Specifically, the text generation model of the embodiment of the present application mainly comprises a text vector generation model and a topic vector extraction model. The text vector generation model mainly consists of a feedforward neural network, an encoder and a decoder in a transformer model. The text vector generation model comprises 6 encoders connected in a head-to-tail manner and 6 decoders connected in a head-to-tail manner. The output vector of a previous encoder is used as the input vector of a next encoder, and the output vector of a previous decoder is used as the input vector of a next encoder. Each encoder comprises two sub-layers, i.e., an attention layer and a feedforward neural network layer. The connection between the sub-layers uses LayerNorm and residual connection. Each decoder comprises three sub-layers, i.e., a first attention layer, a second attention layer and a feedforward neural network layer. The connection between the sub-layers also uses LayerNorm and residual connection. In the embodiment of the present application, the encoder is used to generate a first text vector matrix corresponding to a plurality of context texts respectively, and the decoder and the feedforward neural network are used to extract a reply text vector matrix corresponding to a question text.

[0046] Specifically, when generating the first text vector matrix corresponding to each of the plurality of context texts by using the initial encoder, first, the plurality of context texts are subjected to word segmentation processing to obtain each word segment contained in each context text, and then the existing word2vec method is used to determine the embedding vector corresponding to each word segment, and based on the embedding vector corresponding to each word segment contained in each context text, an embedding vector matrix corresponding to each context text is formed, the number of rows of the embedding vector matrix represents the number of word segments, and the number of columns of the embedding vector matrix represents the dimension of the embedding vector corresponding to each word segment, such as 256 dimensions. In this way, the embedding vector matrix corresponding to each of the plurality of context texts can be obtained in the above manner.

[0047] Further, the embedding vector matrix corresponding to each context text is input into the attention layer in the initial encoder, and by performing corresponding linear transformation on the embedding vector matrix corresponding to the context text, the first query matrix, the first key matrix and the first value matrix corresponding to each context text can be obtained, and based on the first query matrix, the first key matrix and the first value matrix, the first text vector matrix corresponding to each context text is calculated, and the specific formula is as follows:

[0048]

[0049] wherein Q is the first query matrix, K is the first key matrix, V is the first value matrix, d K is the number of columns of the first key matrix, multiplying the transposed first key matrix K and the first query matrix Q can obtain the attention intensity matrix corresponding to each context text, and then the d K The attention intensity matrix is scaled, the scaled attention intensity matrix is input into the softmax layer for normalization processing to obtain the attention coefficient matrix corresponding to each context text, and then the attention coefficient matrix is multiplied with the first value matrix V to obtain the encoding vector matrix corresponding to each context text, further, the encoding vector matrix is input into the feedforward neural network layer in the initial encoder for processing, and the processed encoding vector matrix is input into the next initial encoder for processing, the processing process of each initial encoder is the same as the above process, and the vector matrix output by the last initial encoder is the first text vector matrix, thereby obtaining the first text vector matrix corresponding to each context text.

[0050] Further, in order to generate the reply text corresponding to the question text by using the context text, it is necessary to splice the first text vector matrix corresponding to each context text, take the spliced first text vector matrix as the context text vector matrix, and then input the context text vector matrix and the embedding vector matrix corresponding to the question text into the initial decoder for decoding to obtain the second text vector matrix corresponding to the question text, so as to generate the reply text vector matrix based on the second text vector matrix. When decoding by using the initial decoder, first, the question text is processed by word segmentation to obtain each word in the question text, and the existing word2vec method is used to determine the embedding vector corresponding to each word, and based on the embedding vector corresponding to each word in the question text, the embedding vector matrix corresponding to the question text is composed, and further, the embedding vector matrix corresponding to the question text is input into the first attention layer in the initial decoder for processing to obtain the third text vector matrix corresponding to the question text. The processing process of the embedding vector matrix of the question text by the first attention layer in the initial decoder is exactly the same as the processing process of the attention layer in the initial encoder, and will not be described here.

[0051] Further, the obtained third text vector matrix is input into the second attention layer in the initial decoder for processing. Specifically, first, the obtained third text vector matrix is linearly transformed, and the linearly transformed third text vector matrix is taken as the second query matrix of the question text, then the spliced context text vector matrix is linearly transformed, and the linearly transformed context text vector is taken as the second key matrix and the second value matrix of the question text respectively, and then the second query matrix, the second key matrix and the second value matrix are used to calculate the decoding vector matrix corresponding to the question text. It should be noted that the specific process of calculating the decoding vector matrix corresponding to the question text by using the second query matrix, the second key matrix and the second value matrix is exactly the same as the process of calculating the encoding vector matrix corresponding to each context text by using the first query matrix, the first key matrix and the first value matrix, and will not be described here. Further, the decoding vector matrix output by the second attention layer is input into the feedforward neural network in the initial decoder for processing, and the processed decoding vector is input into the next initial decoder for decoding processing, and the vector matrix output by the last initial decoder is the second text vector matrix. Further, the second text vector matrix finally output by the initial decoder is input into the first feedforward neural network in the initial text vector generation model for processing to generate the reply text vector matrix corresponding to the question text, so as to generate the predicted reply text corresponding to the question text by using the reply text vector matrix.

[0052] 103. Utilize an initial topic quantity extraction model to respectively extract the context text topic vector corresponding to the first text vector matrix and the reply text topic vector corresponding to the reply text vector matrix.

[0053] Among them, the initial topic vector extraction model includes multiple second feedforward neural networks. For the embodiment of the present invention, in order to ensure that the generated reply text is deeply related to the context topic, it is necessary to use the initial topic vector extraction model to respectively extract the context topic vector corresponding to the first text vector matrix and the reply text topic vector corresponding to the reply text vector matrix. Specifically, the topic vector parameters corresponding to each second feedforward neural network can be pre-initialized, and the dimension corresponding to the topic vector parameter is the same as the number of columns corresponding to the first text vector matrix and the reply text vector matrix. Then, the first text vector matrix corresponding to each context text is multiplied by multiple topic vector parameters respectively, and the multiplication result is input into the corresponding second feedforward neural network for topic vector extraction to obtain multiple first topic vectors. Finally, the multiple first topic vectors are added to obtain the context text topic vector corresponding to each first text vector matrix. Similarly, the reply text vector matrix is ​​multiplied by multiple topic vector parameters, and the multiplication result is input into the corresponding second feedforward neural network for topic vector extraction to obtain multiple second topic vectors. Finally, the multiple second topic vectors are added to obtain the reply text topic vector corresponding to the reply text vector matrix. The specific process of extracting the context text topic vector and the reply text topic vector is shown in step 203.

[0054] 104. Construct a loss function based on the context text topic vector and the reply text topic vector.

[0055] According to the embodiment of the present invention, after extracting the context text topic vector and the reply text topic vector, the cosine similarities between the multiple context text topic vectors and the reply text topic vector are calculated respectively, and the calculated cosine similarities are added together to construct a loss function.

[0056] 105. Iteratively train the initial text vector generation model and the initial topic vector extraction model based on the loss function to construct a text generation model.

[0057] Among them, the text generation model includes a text vector generation model and a topic vector extraction model. The text vector generation model includes an encoder, a decoder and a first feedforward neural network. The topic vector extraction model includes multiple second feedforward neural networks and their corresponding topic vector parameters. For an embodiment of the present invention, after the loss function is constructed, the loss function is used to jointly train the initial encoder, initial decoder and first initial feedforward neural network in the initial text vector generation model, as well as multiple second feedforward neural networks and their corresponding topic vector parameters to construct a text generation model.

[0058] According to the training method of the text generation model provided by the application, compared with the current method of generating corresponding reply text by using a deep learning model, the application obtains question text and a plurality of context texts in a multi-turn dialogue, generates a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model, simultaneously extracts a context text theme vector corresponding to the first text vector matrix and a reply text theme vector corresponding to the reply text vector matrix by using an initial theme vector extraction model, constructs a loss function based on the context text theme vector and the reply text theme vector, and finally iteratively trains the initial text vector generation model and the initial theme vector extraction model based on the loss function to construct a text generation model. Thus, the context theme vector and the reply text theme vector are extracted, the loss function is constructed based on the context text theme vector and the reply text theme vector, the text generation model is trained, the reply text theme vector and the context theme vector are made as similar as possible, the generated reply text is related to the context dialogue theme in a deep level, the generated reply text is consistent with the overall logic of the multi-turn dialogue, and the generation accuracy of the reply text in the multi-turn dialogue is improved.

[0059] Further, in order to better illustrate the training process of the text generation model, as a refinement and expansion of the above embodiment, the embodiment of the application provides another training method of a text generation model, as shown in Figure 2 The method comprises the following steps:

[0060] 201, obtaining question text and a plurality of context texts in a multi-turn dialogue.

[0061] For the embodiment of the application, in order to train the reply text generation model of the multi-turn dialogue, sample sentences in the multi-turn dialogue need to be collected in advance, and the last sample sentence in the sample sentences is taken as the question text, and the other sample sentences are taken as the context texts.

[0062] 202, generating a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model.

[0063] For the embodiment of the present application, the reply text generation model comprises a text vector generation model and a topic vector extraction model. In order to train the reply text generation model, an initial text vector generation model needs to be initialized in advance, and the initial text vector generation model is used to generate a first text vector matrix corresponding to each context text and a reply text vector matrix corresponding to the question text. The specific generation process and steps of the first text vector matrix and the reply text vector matrix are exactly the same as those of step 102, and will not be repeated here.

[0064] 203. Extracting a context text topic vector corresponding to the first text vector matrix and a reply text topic vector corresponding to the reply text vector matrix by using the initial topic vector extraction model.

[0065] The initial topic vector extraction model comprises a plurality of second initial feedforward neural networks. For the embodiment of the present application, in order to extract the context text topic vector and the reply text topic vector, step 203 specifically comprises: determining topic vector parameters corresponding to the plurality of second initial feedforward neural networks respectively; multiplying the first text vector matrix and the reply text vector matrix by the plurality of topic vector parameters respectively to obtain a plurality of first multiplication results corresponding to the first text vector matrix and a plurality of second multiplication results corresponding to the reply text vector matrix; inputting the plurality of first multiplication results and the plurality of second multiplication results into the corresponding second initial feedforward neural networks respectively for topic vector extraction to obtain a plurality of first topic vectors and a plurality of second topic vectors; adding the plurality of first topic vectors to obtain the context text topic vector corresponding to the first text vector matrix; and adding the plurality of second topic vectors to obtain the reply text topic vector corresponding to the reply text vector matrix.

[0066] Specifically, first, a topic vector parameter is initialized for each second initial feedforward neural network. Different second initial feedforward neural networks correspond to different topic vector parameters. Then, each first text vector matrix is multiplied by a plurality of topic vector parameters to obtain a plurality of first multiplication results. Next, the plurality of first multiplication results are input into the corresponding second initial feedforward neural networks for topic vector extraction to obtain a plurality of first topic vectors. The specific formula is as follows:

[0067] T i =FFN m (soft max(MeanPooling(X i )·tv m ))

[0068] Wherein, X i represents the first text vector matrix, and tv mrepresent a first topic vector, and a plurality of first topic vectors can be obtained according to the above formula, and the context text topic vectors corresponding to each first text vector matrix can be obtained by adding the plurality of first topic vectors, and the reply text topic vectors corresponding to the reply text vector matrix can be extracted in the same way, so as to construct the loss function based on the context text topic vectors and the reply text topic vectors. i represent a first topic vector, and a plurality of first topic vectors can be obtained according to the above formula, and the context text topic vectors corresponding to each first text vector matrix can be obtained by adding the plurality of first topic vectors, and the reply text topic vectors corresponding to the reply text vector matrix can be extracted in the same way, so as to construct the loss function based on the context text topic vectors and the reply text topic vectors.

[0069] 204、respectively calculate the similarity between the plurality of context text topic vectors and the reply text topic vectors, and construct the similarity loss function based on the calculated plurality of similarities.

[0070] For the embodiment of the application, after obtaining the context text topic vectors corresponding to the plurality of first text vector matrices and the reply text topic vectors corresponding to the reply text vector matrix, the cosine similarity between each context text topic vector and the reply text topic vector is calculated, and the similarity loss function is constructed by adding the calculated cosine similarities.

[0071] 205、based on the reply text vector matrix, predict the predicted reply text corresponding to the multi-turn dialogue, and obtain the real reply text corresponding to the multi-turn dialogue, and construct the text loss function based on the real reply text and the predicted reply text.

[0072] For the embodiment of the application, the reply text vector matrix is processed by the full connection layer and the activation function respectively, so that the word segmentation probability value corresponding to each word segmentation index position in the predicted reply text can be obtained. Based on the word segmentation probability value, the reply word segmentation at each word segmentation index position can be obtained by mapping to the preset dictionary, and the reply text can be formed by splicing each reply word segmentation. On this basis, in order to ensure that the generated reply text contains the key words in the context text as much as possible and avoid generating a general reply such as "yes" or "good", the embodiment of the application locks the key words in the context text and replaces the reply word segmentation at the corresponding position with the key words during the generation of the predicted reply text, so that the key words can be contained in the reply text as much as possible and the reply text can be avoided to be a general answer. Based on this, the method comprises: determining the key words corresponding to the predicted reply text from the plurality of context texts; inputting the reply text vector matrix into the full connection layer for processing, and inputting the processed reply text vector matrix into the activation function for processing to obtain the word segmentation probability value corresponding to each position index of the predicted reply text; based on the word segmentation probability value, determining the reply word segmentation at each position index and determining whether there is a target word segmentation probability value greater than a preset probability value in each word segmentation probability value; if the target word segmentation probability value exists, replacing the reply word segmentation at the position index corresponding to the target word segmentation probability value with the key words; and generating the predicted reply text according to the key words and the reply word segmentation at other position indexes.

[0073] Specifically, first, the key words are determined from the plurality of context texts, then the reply text vector matrix is input into the full connection layer and the activation function for processing to obtain the word segmentation probability value corresponding to each word segmentation index position in the predicted reply text. The reply word segmentation at each index position in the reply text can be determined by the word segmentation probability value and the preset dictionary. Further, it is determined whether there is a target word segmentation probability value greater than a preset probability value in each word segmentation probability value. If the target word segmentation probability value exists, the reply word segmentation at the word segmentation position index corresponding to the target word segmentation probability value is replaced with the key words, and then the predicted reply text corresponding to the question text in the multi-round dialogue is formed based on the key words and other reply word segmentations.

[0074] Further, after obtaining the predicted reply text, a text loss function is constructed based on the predicted reply text and the real reply text corresponding to the multi-round dialogue, and the specific formula is as follows:

[0075]

[0076] Wherein, L G represents the text loss function, P i represents the predicted reply word segmentation at the index position i, R iL represents the length of the predicted reply text, that is, the number of reply words contained in the predicted reply text, for example, the true reply text is "good / that / we / now / go / eat", the predicted reply text is "good / that / we / now / go / walk / ya", the predicted reply text and the true reply text contain 6 reply words in common, the last two reply words in the predicted reply text are different from the true reply text, and therefore L G = 4 / 6.

[0077] In a specific application scenario, the determining of the keyword corresponding to the predicted reply text from the plurality of context texts comprises: determining a longest context text in the plurality of context texts; calculating a similarity matrix between the longest context text and other context texts according to a first text vector matrix corresponding to the longest context text and first text vector matrices corresponding to the other context texts; transversely splicing the calculated similarity matrices, and summing each row of the spliced similarity matrix to obtain a sum result corresponding to each row of the spliced similarity matrix; determining a maximum sum result from the sum results corresponding to the rows, and determining a row index corresponding to the maximum sum result; and determining a word at the row index in the longest context text as the keyword.

[0078] Specifically, first, the longest context text in the plurality of context texts is taken as a target text that may contain a keyword, and it should be noted that in the embodiment of the application, a context text can also be randomly selected as a target text that may contain a keyword, or the first sentence in the plurality of context texts is taken as a target text that may contain a keyword. Then, a similarity matrix between the target text and other context texts is calculated according to a first text vector matrix corresponding to the target text and first text vector matrices corresponding to the other context texts, and the calculated similarity matrices are transversely spliced. Next, the spliced similarity matrix is summed by row, and a maximum sum result and a row index corresponding thereto are determined, and a word at the row index in the target text is determined as a target word. In this way, the keyword in the context text can be locked according to the above manner, so as to generate the keyword in the reply text.

[0079] 206、Based on the similarity loss function and the text loss function, the initial text vector generation model and the initial topic vector extraction model are iteratively trained to construct a text generation model.

[0080] For the embodiment of the application, after the similarity loss function and the text loss function are constructed, the initial encoder, the initial decoder and the first initial feedforward neural network in the initial text vector generation model, and the plurality of second feedforward neural networks and the corresponding topic vector parameters are trained by using the similarity loss function and the text loss function, and the text generation model is constructed.

[0081] According to the training method of the text generation model provided by the application, compared with the current method of generating the corresponding reply text by using the deep learning model, the application obtains the question text and the plurality of context texts in the multi-turn dialogue, generates the first text vector matrix corresponding to the plurality of context texts and the reply text vector matrix corresponding to the question text by using the initial text vector generation model, at the same time, extracts the context text topic vector corresponding to the first text vector matrix and the reply text topic vector corresponding to the reply text vector matrix by using the initial topic vector extraction model, constructs the loss function based on the context text topic vector and the reply text topic vector, and finally iteratively trains the initial text vector generation model and the initial topic vector extraction model based on the loss function to construct the text generation model. Therefore, by extracting the context topic vector and the reply text topic vector, constructing the loss function based on the context text topic vector and the reply text topic vector, and training the text generation model, the reply text topic vector and the context topic vector can be as similar as possible, so that the generated reply text is deeply related to the context dialogue topic, the generated reply text fits the overall logic of the multi-turn dialogue, and the generation accuracy of the reply text in the multi-turn dialogue is improved.

[0082] Further, as a specific implementation of Figure 1 , the embodiment of the application provides a training device of a text generation model, as shown in Figure 3 , the device comprises an acquisition unit 31, a generation unit 32, an extraction unit 33, a construction unit 34 and a training unit 35.

[0083] The acquisition unit 31 can be used to acquire the question text and the plurality of context texts in the multi-turn dialogue.

[0084] The generation unit 32 can be used to generate the first text vector matrix corresponding to the plurality of context texts and the reply text vector matrix corresponding to the question text by using the initial text vector generation model.

[0085] The extraction unit 33 can be used to extract the context text topic vector corresponding to the first text vector matrix and the reply text topic vector corresponding to the reply text vector matrix by using the initial topic vector extraction model.

[0086] The construction unit 34 can be configured to construct a loss function based on the context text topic vector and the reply text topic vector.

[0087] The training unit 35 can be configured to iteratively train the initial text vector generation model and the initial topic vector extraction model based on the loss function to construct a text generation model.

[0088] In a specific application scenario, in order to generate a first text vector matrix and a reply text vector matrix, the generation unit 32 can include an encoding module 321, a splicing module 322, a decoding module 323, and a generation module 324. Figure 4

[0089] The encoding module 321 can be configured to input the embedding vector matrix corresponding to each of the plurality of context texts into the initial encoder for encoding to obtain a first text vector matrix corresponding to each of the plurality of context texts.

[0090] The splicing module 322 can be configured to splice a plurality of first text vector matrices to obtain a context text vector matrix corresponding to the multi-turn dialogue.

[0091] The decoding module 323 can be configured to input the context text vector matrix and the embedding vector corresponding to the question text into the initial decoder for decoding to obtain a second text vector matrix corresponding to the question text.

[0092] The generation module 324 can be configured to input the second text vector matrix into the first initial feedforward neural network to obtain a reply text vector matrix corresponding to the question text.

[0093] Further, in order to generate a first text vector matrix corresponding to the context text, the encoding module 321 can include a transformation submodule, a multiplication submodule, and a first processing submodule.

[0094] The transformation submodule can be configured to perform a corresponding linear transformation on the embedding vectors corresponding to the plurality of context texts to obtain a first query matrix, a first key matrix, and a first value matrix corresponding to each of the plurality of context texts.

[0095] The multiplication submodule can be configured to perform transpose processing on the first key matrix, multiply the transposed first key matrix with the first query matrix to obtain an attention intensity matrix corresponding to each of the plurality of context texts.

[0096] ​The first processing submodule can be configured to input the attention intensity matrix into a softmax layer in the initial encoder for normalization processing to obtain an attention coefficient matrix corresponding to each of the plurality of context texts.

[0097] The multiplication submodule can be further configured to multiply the attention coefficient matrix and the first value matrix to obtain a first text vector matrix corresponding to each of the plurality of context texts.

[0098] Further, in order to generate a second text vector matrix corresponding to the question text, the decoding module 323 comprises an extraction submodule, a first determination submodule and a calculation submodule.

[0099] The extraction submodule can be configured to input the embedding vector corresponding to the question text into the first attention layer for text feature extraction to obtain a third text vector matrix corresponding to the question text.

[0100] The first determination submodule can be configured to determine a second query matrix corresponding to the question text according to the third text vector matrix.

[0101] The first determination submodule can be further configured to determine a second key matrix and a second value matrix corresponding to the question text according to the context text vector matrix, respectively.

[0102] The calculation submodule can be configured to calculate a second text vector matrix corresponding to the question text according to the second query matrix, the second key matrix and the second value matrix.

[0103] In a specific application scenario, the initial topic vector extraction model comprises a plurality of second initial feedforward neural networks, and the extraction unit 33 comprises a determination module 331, a multiplication module 332, an extraction module 333 and an addition module 334.

[0104] The determination module 331 can be configured to determine topic vector parameters corresponding to the plurality of second initial feedforward neural networks, respectively.

[0105] The multiplication module 332 can be configured to multiply the first text vector matrix and the reply text vector matrix with the plurality of topic vector parameters, respectively, to obtain a plurality of first multiplication results corresponding to the first text vector matrix and a plurality of second multiplication results corresponding to the reply text vector matrix.

[0106] The extraction module 333 can be configured to input the plurality of first multiplication results and the plurality of second multiplication results into the corresponding second initial feedforward neural networks, respectively, for topic vector extraction to obtain a plurality of first topic vectors and a plurality of second topic vectors extracted.

[0107] The adding module 334 can be configured to add the extracted first topic vectors to obtain context text topic vectors corresponding to the first text vector matrix.

[0108] The adding module 334 can also be configured to add the extracted second topic vectors to obtain reply text topic vectors corresponding to the reply text vector matrix.

[0109] In a specific application scenario, the loss function includes a similarity loss function and a text loss function, and the construction unit 34 includes a construction module 341 and a prediction module 342.

[0110] The construction module 341 can be configured to calculate similarities between the context text topic vectors and the reply text topic vectors respectively, and construct the similarity loss function based on the calculated similarities.

[0111] The prediction module 342 can be configured to predict a predicted reply text corresponding to the multi-turn dialogue based on the reply text vector matrix.

[0112] The construction module 341 can also be configured to obtain a real reply text corresponding to the multi-turn dialogue, and construct the text loss function based on the real reply text and the predicted reply text.

[0113] Further, in order to predict the predicted reply text corresponding to the multi-turn dialogue, the prediction module 342 includes a second determination submodule, a second processing submodule, a determination submodule, a replacement submodule, and a generation submodule.

[0114] The second determination submodule can be configured to determine a keyword corresponding to the predicted reply text from the plurality of context texts.

[0115] The second processing submodule can be configured to input the reply text vector matrix into a full connection layer for processing, and input the processed reply text vector matrix into an activation function for processing to obtain word segmentation probability values corresponding to each position index of the predicted reply text.

[0116] The determination submodule can be configured to determine a reply word segmentation at each position index based on the word segmentation probability values, and determine whether there is a target word segmentation probability value greater than a preset probability value in each word segmentation probability value.

[0117] The replacement submodule can be configured to replace the reply word segmentation at the position index corresponding to the target word segmentation probability value with the keyword if the target word segmentation probability value exists.

[0118] The generation submodule can be used for generating the predicted reply text according to the keyword and the reply word segmentation at other position indexes.

[0119] Further, the second determination submodule can be specifically used for determining a longest context text in the plurality of context texts; calculating a similarity matrix between the longest context text and other context texts according to a first text vector matrix corresponding to the longest context text and first text vector matrices corresponding to the other context texts; transversely splicing the calculated similarity matrices, and summing each row of the spliced similarity matrix to obtain a sum result corresponding to each row of the spliced similarity matrix; determining a maximum sum result from the sum results corresponding to the rows, and determining a row index corresponding to the maximum sum result; and determining a word segmentation at the row index in the longest context text as the keyword.

[0120] It should be noted that other corresponding descriptions of the functions of the training device of the text generation model provided in the embodiments of the present application can be referred to the corresponding descriptions of the method shown in Figure 1 The corresponding descriptions of the method shown in

[0121] Based on the above method shown in Figure 1 Correspondingly, the embodiments of the present application also provide a computer readable storage medium having a computer program stored thereon, which is executed by a processor to implement the following steps: obtaining a question text in a multi-turn dialogue and a plurality of context texts; generating a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model; extracting a context text theme vector corresponding to the first text vector matrix and a reply text theme vector corresponding to the reply text vector matrix by using an initial theme vector extraction model; constructing a loss function based on the context text theme vector and the reply text theme vector; and iteratively training the initial text vector generation model and the initial theme vector extraction model based on the loss function to construct a text generation model.

[0122] Based on the above method shown in Figure 1 and the device shown in Figure 3 Based on the above method shown in Figure 1 and the device shown in Figure 3 The embodiments of the present application also provide an entity structure diagram of a computer device, as shown in Figure 5As shown, the computer device comprises a processor 51, a memory 52, and a computer program stored on the memory 52 and executable on the processor, wherein the memory 52 and the processor 51 are arranged on a bus 53, and the processor 51 implements the following steps when executing the program: obtaining question text in multi-round dialogue and a plurality of context texts; generating a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model; extracting a context text theme vector corresponding to the first text vector matrix and a reply text theme vector corresponding to the reply text vector matrix by using an initial theme vector extraction model; constructing a loss function based on the context text theme vector and the reply text theme vector; and iteratively training the initial text vector generation model and the initial theme vector extraction model based on the loss function to construct a text generation model.

[0123] By the technical scheme of the present application, the question text in multi-round dialogue and a plurality of context texts can be obtained, and a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text can be generated by using an initial text vector generation model. At the same time, a context text theme vector corresponding to the first text vector matrix and a reply text theme vector corresponding to the reply text vector matrix can be extracted by using an initial theme vector extraction model. A loss function can be constructed based on the context text theme vector and the reply text theme vector. Finally, the initial text vector generation model and the initial theme vector extraction model can be iteratively trained based on the loss function to construct a text generation model. Thus, by extracting the context theme vector and the reply text theme vector and constructing the loss function based on the context text theme vector and the reply text theme vector to train the text generation model, the reply text theme vector and the context theme vector can be made as similar as possible, so as to ensure that the generated reply text is deeply related to the context dialogue theme, and the generated reply text fits the overall logic of the multi-round dialogue, thereby improving the generation accuracy of the reply text in the multi-round dialogue.

[0124] Obviously, those skilled in the art should understand that the above-mentioned modules or steps of the present application can be realized by general computing devices, which can be concentrated on a single computing device or distributed on a network composed of multiple computing devices, and alternatively, they can be realized by program codes executable by computing devices, so as to be stored in storage devices and executed by computing devices, and in some cases, the steps shown or described herein can be executed in different orders, or they can be respectively manufactured into individual integrated circuit modules, or multiple modules or steps among them can be manufactured into a single integrated circuit module. Thus, the present application is not limited to any specific combination of hardware and software.

[0125] The above description is only the preferred embodiment of the present application, and is not intended to limit the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. A method for training a text generation model, the method comprising: The method comprises the following steps: obtaining question text and a plurality of context texts in a multi-round dialogue; generating a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model; extracting a context text theme vector corresponding to the first text vector matrix and a reply text theme vector corresponding to the reply text vector matrix by using an initial theme vector extraction model; constructing a loss function based on the context text theme vector and the reply text theme vector; iteratively training the initial text vector generation model and the initial theme vector extraction model based on the loss function to construct a text generation model; The method further comprises: predicting a predicted reply text corresponding to the multi-round dialogue based on the reply text vector matrix; The method of predicting a predicted reply text corresponding to the multi-round dialogue based on the reply text vector matrix comprises: determining a keyword corresponding to the predicted reply text from the plurality of context texts; inputting the reply text vector matrix into a fully connected layer for processing, and inputting the processed reply text vector matrix into an activation function for processing to obtain a word segmentation probability value corresponding to each position index of the predicted reply text: based on the word segmentation probability value, determining the reply word segmentation at the position index, and determining whether there is a target word segmentation probability value greater than a preset probability value in each word segmentation probability value; if the target word segmentation probability value exists, replacing the reply word segmentation at the position index corresponding to the target word segmentation probability value with the keyword; generating the predicted reply text according to the keyword and the reply word segmentation at other position indexes.

2. The method of claim 1, wherein, The initial text vector generation model comprises an initial encoder, an initial decoder and a first initial feedforward neural network, and the method of generating a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text by using an initial text vector generation model comprises: inputting an embedding vector matrix corresponding to each of the plurality of context texts into the initial encoder for encoding to obtain a first text vector matrix corresponding to each of the plurality of context texts; splicing a plurality of first text vector matrices to obtain a context text vector matrix corresponding to the multi-round dialogue; inputting the context text vector matrix and an embedding vector corresponding to the question text into the initial decoder for decoding to obtain a second text vector matrix corresponding to the question text; inputting the second text vector matrix into the first initial feedforward neural network to obtain a reply text vector matrix corresponding to the question text.

3. The method of claim 2, wherein, The method of inputting an embedding vector matrix corresponding to each of the plurality of context texts into the initial encoder for encoding to obtain a first text vector matrix corresponding to each of the plurality of context texts comprises: performing linear transformation on the embedding vectors corresponding to the plurality of context texts to obtain a first query matrix, a first key matrix and a first value matrix corresponding to each of the plurality of context texts; Transposing the first key matrix, multiplying the transposed first key matrix and the first query matrix to obtain the attention intensity matrix corresponding to the plurality of context texts respectively; Inputting the attention intensity matrix into the softmax layer in the initial encoder for normalization processing to obtain the attention coefficient matrix corresponding to the plurality of context texts respectively; Multiplying the attention coefficient matrix and the first value matrix to obtain the first text vector matrix corresponding to the plurality of context texts respectively; The initial decoder includes a first attention layer and a second attention layer, and the context text vector matrix and the embedding vector corresponding to the question text are input into the initial decoder for decoding to obtain the second text vector matrix corresponding to the question text, including: Inputting the embedding vector corresponding to the question text into the first attention layer for text feature extraction to obtain the third text vector matrix corresponding to the question text; According to the third text vector matrix, determining the second query matrix corresponding to the question text; According to the context text vector matrix, respectively determining the second key matrix and the second value matrix corresponding to the question text; According to the second query matrix, the second key matrix and the second value matrix, calculating the second text vector matrix corresponding to the question text.

4. The method of claim 1, wherein, The initial topic vector extraction model includes a plurality of second initial feedforward neural networks, and the initial topic vector extraction model is used to extract the context text topic vector corresponding to the first text vector matrix and the reply text topic vector corresponding to the reply text vector matrix, including: Determining the topic vector parameters corresponding to the plurality of second initial feedforward neural networks respectively; Multiplying the first text vector matrix and the reply text vector matrix with the plurality of topic vector parameters respectively to obtain a plurality of first multiplication results corresponding to the first text vector matrix and a plurality of second multiplication results corresponding to the reply text vector matrix; Inputting the plurality of first multiplication results and the plurality of second multiplication results into the corresponding second initial feedforward neural networks respectively for topic vector extraction to obtain a plurality of first topic vectors and a plurality of second topic vectors; Adding the plurality of first topic vectors to obtain the context text topic vector corresponding to the first text vector matrix; Adding the plurality of second topic vectors to obtain the reply text topic vector corresponding to the reply text vector matrix.

5. The method of claim 1, wherein, The loss function includes a similarity loss function and a text loss function, and the loss function is constructed based on the context text topic vector and the reply text topic vector, including: Respectively calculating the similarity between a plurality of context text topic vectors and the reply text topic vector, and constructing the similarity loss function based on the calculated plurality of similarities; Obtaining the real reply text corresponding to the multi-round dialogue, and constructing the text loss function based on the real reply text and the predicted reply text.

6. The method of claim 1, wherein, The key word corresponding to the predicted reply text is determined from the plurality of context texts, including: determining a longest context text in the plurality of context texts; calculating a similarity matrix between the longest context text and other context texts according to a first text vector matrix corresponding to the longest context text and first text vector matrices corresponding to the other context texts; horizontally splicing the calculated similarity matrices and summing the spliced similarity matrix by row to obtain a sum result corresponding to each row in the spliced similarity matrix; determining a maximum sum result from the sum results corresponding to the rows and determining a row index corresponding to the maximum sum result; determining a word segmentation at the row index in the longest context text as the keyword. 7.A device for training a text generation model, comprising: comprise: an acquisition unit configured to acquire question text and a plurality of context texts in a multi-turn dialogue; a generation unit configured to generate, by using an initial text vector generation model, a first text vector matrix corresponding to each of the plurality of context texts and a reply text vector matrix corresponding to the question text; an extraction unit configured to extract, by using an initial topic vector extraction model, a context text topic vector corresponding to the first text vector matrix and a reply text topic vector corresponding to the reply text vector matrix; a construction unit configured to construct a loss function based on the context text topic vector and the reply text topic vector; a training unit configured to iteratively train the initial text vector generation model and the initial topic vector extraction model based on the loss function to construct a text generation model; the training device of the text generation model further comprises: predicting a predicted reply text corresponding to the multi-turn dialogue based on the reply text vector matrix; the predicting a predicted reply text corresponding to the multi-turn dialogue based on the reply text vector matrix comprises: determining a keyword corresponding to the predicted reply text from the plurality of context texts; inputting the reply text vector matrix into a full connection layer for processing, inputting the processed reply text vector matrix into an activation function for processing to obtain a word segmentation probability value corresponding to each position index of the predicted reply text: determining a reply word segmentation at the position index based on the word segmentation probability value and determining whether there is a target word segmentation probability value greater than a preset probability value in each word segmentation probability value; if there is the target word segmentation probability value, replacing the reply word segmentation at the position index corresponding to the target word segmentation probability value with the keyword; generating the predicted reply text according to the keyword and the reply word segmentations at other position indexes.

8. A computer-readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the method of any one of claims 1 to 6.

9. A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The computer program is executed by the processor to implement the steps of the method of any one of claims 1 to 6.

Citation Information

Patent Citations

  • Conversation generation method and system, readable storage medium and computer equipment

    CN110321417A