Method and device for generating dialogue text, storage medium and electronic device
By performing perturbation-free and perturbation transmission in the dialogue model, and updating the historical information matrix with attribute classifiers, the problem of low efficiency and accuracy of dialogue text generation in the existing technology is solved, and the dialogue with controllable style and consistent attributes is achieved Text generation improves the user experience of chatbots.
Patent Information
- Application Number
- CN202311289708.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-07
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2043-10-07
AI Technical Summary
In the prior art, dialogue text generation efficiency and accuracy rate that conforms to attribute constraints are low, and it is difficult to effectively control the style and attributes of dialogue text.
By obtaining the initial dialogue text and target dialogue attributes in the target dialogue scene, the target dialogue model of multiple attention modules is used for non-perturbation and perturbation forward transmission, and the historical information matrix is updated in combination with the target attribute classifier to ensure the generated dialogue The text conforms to the target attribute.
It improves the efficiency and accuracy of dialogue text generation that conforms to attribute constraints, realizes controllable text generation, enhances the user immersion and interactive experience of the chat robot, and meets different scenarios and user needs.
Smart Images

Figure CN117216223B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computers, and in particular, to a method and device for generating a dialogue text, a storage medium, and an electronic device. Background Art
[0002] At present, the dialogue models that expect to generate dialogue content that conforms to attributes and styles mainly include the following three methods: template-based and rule-based methods, conditional dialogue generation models, and methods based on attribute classifiers for screening. Among them, the template-based and rule-based methods generate text according to pre-defined templates or rules, which requires a lot of manual design of templates and rules, and are difficult to adapt to complex and diverse generation tasks. The generated text may be relatively rigid and single. The conditional dialogue generation model requires the collection of a large amount of training data with attribute and style labels, and the control ability of the model may be limited by the diversity of dialogue training data. The method based on attribute classifier screening cannot screen out satisfactory responses that meet the attribute constraints if there is no response text that conforms to the target attributes and style among the multiple responses generated by the dialogue model.
[0003] Therefore, the related technology still has the technical problem of low efficiency and accuracy in generating dialogue texts that meet attribute constraints.
[0004] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention
[0005] The embodiments of the present application provide a method and device for generating a dialogue text, a storage medium, and an electronic device to at least solve the technical problem of low efficiency and accuracy in generating a dialogue text that complies with attribute constraints.
[0006] According to one aspect of an embodiment of the present application, a method for generating a dialogue text is provided, comprising: obtaining an initial dialogue text and a target dialogue attribute in a target dialogue scene, wherein the target dialogue attribute is used to indicate the dialogue style of the target dialogue scene; inputting the initial dialogue text into a target dialogue model for undisturbed forward transmission, and outputting undisturbed dialogue text, wherein the undisturbed dialogue text includes segmentations that do not conform to the target dialogue attribute, and the undisturbed dialogue text is composed of segmentations output by the target dialogue model, the target dialogue model is used to determine the output segmentations according to the input segmentations and the historical information matrix step by step according to the time step, and determine the corresponding dialogue text according to the output segmentations, the target dialogue model includes a plurality of attention modules, the historical information matrix is used to determine the output segmentations according to the input segmentations and the historical information matrix, and determine the corresponding dialogue text according to the output segmentations, the target dialogue model includes a plurality of attention modules, the historical information matrix is used to determine the output segmentations according to the output segmentations, and ... The information matrix includes key-value pairs corresponding to each attention module in the multiple attention modules; the representation vectors corresponding to each segmentation in the undisturbed dialogue text are respectively input into the target attribute classifier, the loss parameters of the target attribute classifier are respectively determined, and the historical information matrix is updated using the loss parameters, wherein the loss parameters are used to indicate the degree to which the segmentation corresponding to the representation vector conforms to the target dialogue attribute; the initial dialogue text is input into the target dialogue model for a perturbed forward pass, and a perturbed dialogue text is output, wherein the perturbed dialogue text represents the dialogue text output by the target dialogue model after the historical information matrix is updated using the loss parameters, and each segmentation in the perturbed dialogue text conforms to the target dialogue attribute.
[0007] According to another aspect of an embodiment of the present application, a device for generating a dialogue text is also provided, comprising: an acquisition module, used to acquire an initial dialogue text and a target dialogue attribute in a target dialogue scene, wherein the target dialogue attribute is used to indicate the dialogue style of the target dialogue scene; a first processing module, used to input the initial dialogue text into a target dialogue model for undisturbed forward transmission, and output undisturbed dialogue text, wherein the undisturbed dialogue text includes word segmentations that do not conform to the target dialogue attribute, and the undisturbed dialogue text is composed of word segmentations output by the target dialogue model, the target dialogue model is used to determine the output word segmentations according to the input word segmentations and the historical information matrix step by step according to the time step, and determine the corresponding dialogue text according to the output word segmentations, the target dialogue model includes multiple attention modules, the historical information matrix is used to determine the output word segmentations according to the input word segmentations and the historical information matrix according to the time step, and determine the corresponding dialogue text according to the output word segmentations, the target dialogue model includes multiple attention modules, the historical information matrix is used to determine the output word segmentations according to the output word segmentations, and ... The information matrix includes key-value pairs corresponding to each of the multiple attention modules; an updating module, which is used to input the representation vectors corresponding to each word segment in the unperturbed dialogue text into the target attribute classifier respectively, determine the loss parameters of the target attribute classifier respectively, and update the historical information matrix using the loss parameters, wherein the loss parameters are used to indicate the degree to which the word segment corresponding to the representation vector conforms to the target dialogue attribute; a second processing module, which is used to input the initial dialogue text into the target dialogue model for a perturbed forward pass, and output a perturbed dialogue text, wherein the perturbed dialogue text represents the dialogue text output by the target dialogue model after the historical information matrix is updated using the loss parameters, and each word segment in the perturbed dialogue text conforms to the target dialogue attribute.
[0008] Optionally, the device is further used to: input the initial dialogue text into the target dialogue model to generate t first representation vectors, wherein the target dialogue model includes an i-layer attention module, the first representation vector represents the output result of the i-layer attention module, the target dialogue model outputs t word segments in t time steps in an iterative loop, the j+1 word segment is determined by the j-th word segment output by the target dialogue model at the j-time step and the j-th historical information matrix input into the target dialogue model at the j+1 time step, the j-th historical information matrix is used to represent the i key-value pairs corresponding to the j-time step, the i key-value pairs correspond to the i-layer attention module one-to-one, and the t first representation vectors are used to determine the number of words consisting of the t word segments. The perturbed dialogue text, i is an integer greater than or equal to 2, t is an integer greater than or equal to j+1, and j is an integer greater than or equal to 1; the t first representation vectors are respectively input into the target attribute classifier, t classification results are determined, and the target history information matrix is updated, wherein the target history information matrix represents the history information matrix corresponding to the target segmentation output by the target dialogue model after the target dialogue model is input, and the target segmentation includes the segmentation determined by the t classification results that does not conform to the target dialogue attribute; the initial dialogue text is input into the target dialogue model, and t second representation vectors are determined by using the updated target history information matrix to generate the perturbed dialogue text, wherein the second representation vector is generated in the same manner as the first representation vector.
[0009] Optionally, the device is used to input the t first characterization vectors into a pre-trained target attribute classifier, determine t classification results, and update the target history information matrix in the following manner: input the t first characterization vectors into the target attribute classifier, determine t loss parameters, wherein the t classification results correspond one-to-one to the t loss parameters; determine a target loss parameter corresponding to the nth time step from the t loss parameters, wherein the target loss parameter represents a loss parameter that does not meet a preset loss condition, and n is a positive integer less than or equal to t; and update the target history information matrix according to the target loss parameter.
[0010] Optionally, the device is used to update the target history information matrix according to the target loss parameter in the following manner: obtain an initial tensor that allows training; determine the gradient of the target attribute classifier according to the target loss parameter, and use the gradient to train the initial tensor until a target tensor is obtained, wherein the target tensor represents an updated value for the target history information matrix; and determine the sum of the target history information matrix and the target tensor as the updated target history information matrix.
[0011] Optionally, the device is used to determine the gradient of the target attribute classifier according to the target loss parameter in the following manner, and use the gradient to train the initial tensor to obtain the target tensor: obtain a predetermined training step size, wherein the value of the training step size is positively correlated with the amplitude adjusted by the initial tensor in each round of training; train the initial tensor in each round according to the training step size, use the trained tensor to update the target history information matrix in each round, and recalculate the loss parameter and gradient corresponding to the nth time step in that round after each update of the target history information matrix, until the loss parameter corresponding to the nth time step meets the preset loss condition, and determine the trained tensor in that round as the target tensor, wherein the trained tensor in each round is jointly determined by the tensor before each round of training, the gradient corresponding to each round, and the update step size.
[0012] Optionally, the device is used to input the initial dialogue text into the target dialogue model for a perturbed forward pass and output the perturbed dialogue text in the following manner: input the initial dialogue text into the target dialogue model to generate the t second representation vectors, wherein the historical information matrix corresponding to the nth time step has completed the update; input the t second representation vectors into the target attribute classifier respectively, and redetermine t classification results, wherein the redetermined t classification results all indicate that the corresponding word segmentation conforms to the target dialogue attribute; perform a sampling operation on the t second representation vectors to obtain the perturbed dialogue text.
[0013] Optionally, the device is also used to: perform normalization operations on the t first characterization vectors and the t second characterization vectors, respectively, to obtain a first probability distribution and a second probability distribution; determine a target divergence value based on the first probability distribution and the second probability distribution, wherein the target divergence value is used to measure the difference between the first probability distribution and the second probability distribution; and update the target historical information matrix with the goal of minimizing the value of the target divergence value.
[0014] Optionally, the device is also used to: obtain unlabeled dialogue corpus data; preprocess the dialogue corpus data to obtain target dialogue corpus, wherein the target dialogue corpus represents a dialogue corpus between two objects; use the target dialogue corpus to pre-train an initial dialogue model to obtain the target dialogue model, wherein the initial dialogue model and the target dialogue model use a bidirectional attention mechanism on the input text and a unidirectional attention mechanism on the output text.
[0015] Optionally, the device is used to obtain the initial conversation text and target conversation attributes in the target conversation scenario in the following manner: obtain account data of a target account, wherein the target account represents an account participating in the target conversation scenario; and determine the target conversation attributes based on the account data.
[0016] Optionally, the device is used to determine the target conversation attribute based on the account data in at least one of the following ways: obtaining interaction data of the target account, and determining the target conversation attribute as a conversation attribute related to the interaction data; obtaining the search history of the target account, and determining the target conversation attribute as a conversation attribute related to the search history; obtaining the emotion type of the initial conversation text, and determining the target conversation attribute as a conversation attribute having the same emotion type as the initial conversation text.
[0017] According to another aspect of the embodiments of the present application, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the above-mentioned method for generating a dialogue text when it is run.
[0018] According to another aspect of the embodiment of the present application, a computer program product or a computer program is provided, the computer program product or the computer program includes computer instructions, and the computer instructions are stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the electronic device executes the method for generating a conversation text as described above.
[0019] According to another aspect of the embodiments of the present application, there is also provided an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the method for generating a conversation text through the computer program.
[0020] In an embodiment of the present application, an initial dialogue text and a target dialogue attribute in a target dialogue scene are obtained, wherein the target dialogue attribute is used to indicate the dialogue style of the target dialogue scene; the initial dialogue text is input into a target dialogue model for undisturbed forward transmission, and an undisturbed dialogue text is output, wherein the undisturbed dialogue text includes segmentations that do not conform to the target dialogue attributes, and the undisturbed dialogue text is composed of segmentations output by the target dialogue model, and the target dialogue model is used to determine the output segmentations according to the input segmentations and the historical information matrix step by step according to the time step, and determine the corresponding dialogue text according to the output segmentations, the target dialogue model includes multiple attention modules, and the historical information matrix includes key-value pairs corresponding to each attention module in the multiple attention modules; the representation vectors corresponding to each segmentation in the undisturbed dialogue text are respectively input into the target attribute classifier, and the loss parameters of the target attribute classifier are respectively determined, and the historical information matrix is updated using the loss parameters, wherein the loss parameters are used to represent The method comprises the following steps: the first step is to input the initial dialogue text into the target dialogue model for a perturbed forward pass, and output the perturbed dialogue text, wherein the perturbed dialogue text represents the dialogue text output by the target dialogue model after the historical information matrix is updated using the loss parameter, and each segmentation in the perturbed dialogue text conforms to the target dialogue attributes. In the inference stage of the dialogue model, an attribute discriminator is used to measure whether the generated text conforms to the target attributes and style, and the loss of the attribute discriminator is backward-transferred and returned to perturb and update the state of the dialogue model, thereby increasing the possibility of conforming to the attribute constraints and guiding the pre-trained dialogue model to generate a reply text that satisfies the attribute constraints, thereby achieving the technical effect of achieving style-controllable text generation and improving the generation efficiency and accuracy of dialogue texts that conform to the attribute constraints, thereby solving the technical problem of low generation efficiency and accuracy of dialogue texts that conform to the attribute constraints.
[0021] In addition, chatbots with specific attributes and styles can be customized according to user preferences and needs to enhance user immersion and participation and improve user interaction experience. Responses with attributes such as comfort, encouragement, and empathy can also be generated according to the user's emotional state to provide emotional support and psychological counseling. By achieving text generation with controllable attributes and styles, the dialogue model can better meet different scenarios and user needs, and improve the value and breadth of application of the product. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0023] Figure 1is a schematic diagram of an application environment of an optional method for generating a dialogue text according to an embodiment of the present application;
[0024] Figure 2 is a flowchart of an optional method for generating a dialogue text according to an embodiment of the present application;
[0025] Figure 3 is a schematic diagram of an optional method for generating a dialogue text according to an embodiment of the present application;
[0026] Figure 4 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application;
[0027] Figure 5 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application;
[0028] Figure 6 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application;
[0029] Figure 7 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application;
[0030] Figure 8 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application;
[0031] Fig. 9 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application;
[0032] Fig.10 is a schematic structural diagram of an optional device for generating a dialogue text according to an embodiment of the present application;
[0033] Fig.11 is a schematic structural diagram of an optional dialogue text generation product according to an embodiment of the present application;
[0034] Fig.12 It is a schematic diagram of the structure of an optional electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0035] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.
[0036] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0037] First, some nouns or terms that appear in the description of the embodiments of the present application are subject to the following interpretation:
[0038] transformer: A deep learning model structure based on attention mechanism.
[0039] Style controllable: During the text generation process, text that meets the attribute and style requirements is generated. These attributes can include emotions (such as positive, negative, neutral), topics (such as technology, sports, travel), etc.
[0040] The present application is described below in conjunction with embodiments:
[0041] According to one aspect of an embodiment of the present application, a method for generating a dialogue text is provided. Optionally, in this embodiment, the method for generating a dialogue text can be applied to Figure 1 In the hardware environment composed of the server 101 and the terminal device 103 shown in FIG. Figure 1As shown, the server 101 is connected to the terminal 103 via a network, and can be used to provide services for the terminal device or an application 107 installed on the terminal device. The application 107 can be a video application, an instant messaging application, a browser application, an educational application, a game application, etc. A database 105 may be set up on the server or independently of the server to provide data storage services for the server 101, for example, a game data storage server. The above-mentioned network may include but is not limited to: a wired network, a wireless network, wherein the wired network includes: a local area network, a metropolitan area network and a wide area network, and the wireless network includes: Bluetooth, WIFI and other networks that realize wireless communication. The terminal device 103 may be a terminal configured with an application 107, and may include but is not limited to at least one of the following: a mobile phone (such as an Android phone, an iOS phone, etc.), a laptop computer, a tablet computer, a PDA, a MID (Mobile Internet Devices), a PAD, a desktop computer, a smart TV, an intelligent voice interaction device, a smart home appliance, a vehicle-mounted terminal, an aircraft, a virtual reality (VR) terminal, an augmented reality (AR) terminal, a mixed reality (MR) terminal and other electronic devices. The above-mentioned server may be a single server, or a server cluster consisting of multiple servers, or a cloud server.
[0042] Optionally, in this embodiment, the method for generating the above-mentioned dialogue text may also be implemented by a server, for example, Figure 1 It is implemented in the server 101 shown; or it is implemented by the terminal device and the server together.
[0043] The above is only an example and is not specifically limited in this embodiment.
[0044] Optionally, as an optional implementation, as Figure 2 As shown, the method for generating the above-mentioned dialogue text may be executed by an electronic device, which may be a terminal device or a server, and includes:
[0045] S202, obtaining an initial dialogue text and a target dialogue attribute in a target dialogue scene, wherein the target dialogue attribute is used to indicate a dialogue style of the target dialogue scene;
[0046] Optionally, in this embodiment, the target dialogue scenario may include but is not limited to an intelligent interaction scenario based on artificial intelligence, wherein the target dialogue scenario may include but is not limited to intelligent customer service, intelligent strategy, intelligent after-sales, chatbots, etc.
[0047] Specifically, artificial intelligence (AI) is the theory, method, technology and application system that uses digital computers or machines controlled by digital computers to simulate, extend and expand human intelligence, perceive the environment, acquire knowledge and use knowledge to obtain the best results. In other words, artificial intelligence is a comprehensive technology in computer science that attempts to understand the essence of intelligence and produce a new intelligent machine that can respond in a similar way to human intelligence. Artificial intelligence is to study the design principles and implementation methods of various intelligent machines so that machines have the functions of perception, reasoning and decision-making.
[0048] Artificial intelligence technology is a comprehensive discipline that covers a wide range of fields, including both hardware-level and software-level technologies. The basic technologies of artificial intelligence generally include sensors, dedicated artificial intelligence chips, cloud computing, distributed storage, big data processing technology, operation / interaction systems, mechatronics and other technologies. Artificial intelligence software technology mainly includes computer vision technology, speech processing technology, natural language processing technology, and machine learning / deep learning.
[0049] Computer Vision (CV) is a science that studies how to make machines "see". To put it more specifically, it refers to machine vision such as using cameras and computers to replace human eyes to identify and measure targets, and further perform graphic processing so that computer processing becomes an image that is more suitable for human eye observation or transmission to instrument detection. As a scientific discipline, computer vision studies related theories and technologies, and attempts to establish an artificial intelligence system that can obtain information from images or multi-dimensional data. Computer vision technology usually includes image processing, image recognition, image semantic understanding, image retrieval, OCR, video processing, video semantic understanding, video content / behavior recognition, three-dimensional object reconstruction, 3D technology, virtual reality, augmented reality, simultaneous positioning and map construction, and other technologies, as well as common biometric recognition technologies such as face recognition and fingerprint recognition.
[0050] In an exemplary embodiment, the target dialogue scene can be converted into a dialogue scene corresponding to natural language by inputting an image and then recognizing the image.
[0051] The key technologies of speech technology include automatic speech recognition technology (ASR), text-to-speech technology (TTS) and voiceprint recognition technology. Enabling computers to listen, see, speak and feel is the future development direction of human-computer interaction, among which speech has become one of the most promising human-computer interaction methods in the future.
[0052] In an exemplary embodiment, the target dialogue scenario can be converted into a dialogue scenario corresponding to natural language by inputting speech and then recognizing the speech.
[0053] Natural language processing (NLP) is an important direction in the fields of computer science and artificial intelligence. It studies various theories and methods that can achieve effective communication between people and computers using natural language. Natural language processing is a science that integrates linguistics, computer science, and mathematics. Therefore, research in this field will involve natural language, that is, the language people use in daily life, so it is closely related to the study of linguistics. Natural language processing technology usually includes text processing, semantic understanding, machine translation, robot question answering, knowledge graph and other technologies.
[0054] In an exemplary embodiment, the target dialogue scenario may be created by inputting text and then using the text as the dialogue scenario corresponding to the initial dialogue text.
[0055] Machine Learning (ML) is a multi-disciplinary subject that involves probability theory, statistics, approximation theory, convex analysis, algorithm complexity theory and other disciplines. It specializes in studying how computers simulate or implement human learning behavior to acquire new knowledge or skills and reorganize existing knowledge structures to continuously improve their performance. Machine learning is the core of artificial intelligence and the fundamental way to make computers intelligent. Its applications are spread across all areas of artificial intelligence. Machine learning and deep learning usually include artificial neural networks, belief networks, reinforcement learning, transfer learning, inductive learning, and self-learning.
[0056] In an exemplary embodiment, the target dialogue scene can be converted into a dialogue scene corresponding to natural language by inputting an image and then recognizing the image.
[0057] Autonomous driving technology usually includes high-precision maps, environmental perception, behavioral decision-making, path planning, motion control and other technologies. Autonomous driving technology has broad application prospects.
[0058] In an exemplary embodiment, the target dialogue scenario may be a dialogue scenario corresponding to voice or text interaction with an application.
[0059] With the research and advancement of artificial intelligence technology, artificial intelligence technology has been studied and applied in many fields, such as common smart homes, smart wearable devices, virtual assistants, smart speakers, smart marketing, unmanned driving, automatic driving, drones, robots, smart medical care, smart customer service, etc. I believe that with the development of technology, artificial intelligence technology will be applied in more fields and play an increasingly important role.
[0060] Optionally, in this embodiment, the above-mentioned initial dialogue text can be understood as text input by the user, and the input method of the text can be input through a physical keyboard, a mouse, a virtual keyboard, etc., and can also include but are not limited to text recognized after input through voice, image, etc.
[0061] It should be noted that the target dialogue scenario may include but is not limited to a specific environment or scenario for communicating with a user in a robot dialogue system. The initial dialogue text refers to the starting text for communication between the robot and the user in this dialogue scenario.
[0062] Optionally, in this embodiment, the target dialogue attribute is used to indicate the dialogue style of the target dialogue scene. The dialogue style refers to the characteristics of the expression, tone, word usage, etc. in the dialogue. The target dialogue attributes may include but are not limited to the following aspects: formal, informal, friendly, serious, humorous, etc.
[0063] Of course, the above-mentioned target dialogue attributes may also include but are not limited to the type of dialogue, for example, the robot may be set to be feminine, masculine, etc., and may also be set to be relaxed, professional, interesting, etc.
[0064] In an exemplary embodiment, the target dialogue scenario is a bank's self-service robot, and the user needs to perform account inquiries and transfer operations. The initial dialogue text can be the business information entered by the user, and the target dialogue attributes can be formal and serious. In this scenario, the robot's response should follow the norms of banking business, with a formal tone and serious expression.
[0065] In another exemplary embodiment, the target dialogue scenario is a voice assistant of a smart speaker, and the user needs to ask for weather information and play music. The initial dialogue text can be a question or instruction spoken by the user, and the target dialogue attribute can be informal and friendly. In this scenario, the robot's reply can use informal spoken expressions, and the tone should be friendly and cordial, so as to communicate with the user in a relaxed manner.
[0066] In another exemplary embodiment, the target dialogue scenario is a control system for a smart home device, and the user needs to control the switch and adjustment of the home appliance through voice commands. The initial dialogue text can be a specific instruction spoken by the user, and the target dialogue attribute can be concise and practical. In this scenario, the robot's reply should be concise and clear, providing only necessary information so that the user can complete the operation quickly.
[0067] Through the above examples, by obtaining the initial conversation text and target conversation attributes in the target conversation scenario, the robot's expression and response content in the conversation can be guided to better meet the needs and expectations of users.
[0068] S204, inputting the initial dialogue text into the target dialogue model for undisturbed forward transmission, and outputting undisturbed dialogue text, wherein the undisturbed dialogue text includes word segmentations that do not conform to the target dialogue attributes, and the undisturbed dialogue text is composed of word segmentations output by the target dialogue model, the target dialogue model is used to determine the output word segmentations according to the input word segmentations and the historical information matrix according to the time step, and determine the corresponding dialogue text according to the output word segmentations, the target dialogue model includes multiple attention modules, and the historical information matrix includes key-value pairs corresponding to each attention module in the multiple attention modules;
[0069] Optionally, in this embodiment, the target dialogue model may include but is not limited to a Transformer model, wherein common transformer models include but are not limited to BERT (Bidirectional Encoder Representations from Transformers), which uses a transformer architecture and learns language representation from a large-scale text corpus through unsupervised learning. BERT can be fine-tuned in various natural language processing tasks, including text classification, named entity recognition, question and answer, etc. By encoding the input text into a sequence of word vectors and then processing it through a multi-layer transformer encoder, BERT can capture the contextual relationship between words to obtain a better semantic representation. In the fine-tuning stage, BERT can learn the representation of a specific task by performing supervised learning on the pre-training corpus. For example, in a text classification task, BERT can predict the category of the text by adding a classification layer based on the language representation learned in the pre-training stage. By using the transform model, BERT has achieved significant performance improvements in multiple natural language processing tasks and has become one of the important models in the field of natural language processing.
[0070] Optionally, in this embodiment, the forward pass can be understood as a process of calculating the input data through the weights and biases of different layers to finally obtain the output. In the forward propagation process, the input data X, weight W and bias b are known, and the final output result Y is obtained through a series of calculations. That is, the forward pass can be understood as inputting the original data into the neural network model and obtaining the corresponding output result.
[0071] It should be noted that Figure 3 is a schematic diagram of an optional method for generating a dialogue text according to an embodiment of the present application, such as Figure 3 As shown in the figure, a decoder structure of a transformer model is shown, in which the above initial dialogue text is input into the model from below to obtain the corresponding probability distribution, which is a common forward transmission method.
[0072] Optionally, in this embodiment, the above-mentioned undisturbed dialogue text can be understood as the dialogue text obtained by inputting the initial dialogue text into the pre-trained target dialogue model. The style of the dialogue text is uncontrollable, and word segmentations that do not conform to the target dialogue attributes are allowed to exist.
[0073] In an exemplary embodiment, Figure 4 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application, such as Figure 4 As shown, it includes: dialogue text 1, dialogue text 2, dialogue text 3, and dialogue text 4, wherein dialogue text 1 and dialogue text 3 are the initial dialogue texts input by the user, and dialogue text 2 and dialogue text 4 are the undisturbed dialogue texts automatically generated by the transformer model. Since the user selects the "serious" dialogue attribute, and dialogue text 2 and dialogue text 4 both include word segmentations that do not conform to the dialogue attribute, the dialogue text is the aforementioned undisturbed dialogue text.
[0074] Optionally, in this embodiment, the above-mentioned target dialogue model can be understood as a model for generating dialogue text, which is used to determine the output word segmentation step by step according to the input word segmentation and historical information matrix according to time steps, and determine the corresponding dialogue text according to the output word segmentation, including but not limited to gradually determining the output word segmentation according to the input word segmentation and historical information matrix, and splicing these word segmentations into the final dialogue text.
[0075] It should be noted that in the target dialogue model, the input word segmentation is the context information in the dialogue text, which may include the previous dialogue content, etc. The history information matrix is a matrix that records the dialogue history. A word segmentation represents the input of one time step and the output of another time step. The generation of dialogue is achieved by gradually determining the output word segmentation and splicing them together.
[0076] In an exemplary embodiment, the target dialogue model includes multiple attention modules, and the historical information matrix includes key-value pairs corresponding to each attention module in the multiple attention modules, which can be understood as the attention parameters (W) used by each attention module. Q , W K , W V ) is processed to obtain the representation vectors Q, K, V of the layer, where K and V constitute the above key-value pairs.
[0077] It should be noted that the optimization of large model reasoning performance can be achieved including but not limited to the use of KV Cache. This technology can improve reasoning performance by exchanging space for time without affecting any calculation accuracy.
[0078] For example, given an input text, the model will output an answer (length N), and N inference processes are performed in the process. That is, the model only outputs one token in one inference, and the output token will be concatenated with the input tokens, and then used as the input for the next inference, and this process is repeated until the terminator is encountered. In the inference process, in each time step, a token sequence is input, and the input token sequence is converted into a three-dimensional tensor [b, s, h] through the Embedding layer. After calculation, the calculation result is finally mapped to the vocabulary space through the logits layer, and the output tensor dimension is [b, s, vocab_size]. The output token of the current round is concatenated with the input tokens and used as the input tokens of the next round. After repeated multiple times, it can be concluded that the input data of the j+1th round only has one more token than the input data of the jth round, and all other things are the same. Therefore, the j+1th round of inference must include part of the calculation of the jth round. KV Cache caches the reusable calculation results of the current round, and directly reads the cached results in the next round of calculation.
[0079] After KV Cache configuration is enabled, the reasoning process can be divided into two stages:
[0080] S1, pre-filling stage: occurs during the calculation of the first output token. At this time, the cache is empty. During the calculation, it is necessary to calculate and save the key cache and value cache for each transformer layer. The cache is filled when the token is output.
[0081] S2, using KV Cache stage: occurs during the calculation of the second output token to the last token. At this time, the cache has value. Each round of reasoning only needs to read the cache, and the new key and value calculated in the current round are appended to the cache.
[0082] In an exemplary embodiment, the target dialogue model is a dialogue text generation model implemented using deep learning technology, which can gradually generate new dialogue text based on the input dialogue text and the historical information matrix, and maintain the semantic consistency of the input dialogue text.
[0083] The target dialogue model consists of multiple attention modules, each of which is responsible for generating output segmentation based on the input segmentation and historical information matrix. At each time step, the attention module combines the current segmentation and the key-value pairs in the historical information matrix to calculate a weighted vector to guide the next step of segmentation generation. The historical information matrix consists of key-value pairs corresponding to the attention modules in multiple attention modules. At each time step, the attention module calculates an attention weight based on the current segmentation and the historical information matrix to determine which key-value pairs play an important role in the generation of the current segmentation. By inputting the initial dialogue text into the target dialogue model for an unperturbed forward pass, the output unperturbed dialogue text can be obtained. The unperturbed dialogue text includes segmentations that do not meet the target dialogue attributes.
[0084] For example, suppose the input initial dialogue text is: "The weather is really nice today", and the target dialogue attribute requires the output dialogue text to have segmented words corresponding to "positive emotions". At the first time step, the target dialogue model will calculate an attention weight based on the initial dialogue text and the historical information matrix to determine the generation probability of the segmented words corresponding to "positive emotions". If the attention weight is high, the segmented words corresponding to "positive emotions" will be generated first, thus meeting the target dialogue attribute.
[0085] S206, inputting the representation vectors corresponding to each segmentation in the unperturbed dialogue text into the target attribute classifier respectively, determining the loss parameters of the target attribute classifier respectively, and updating the historical information matrix using the loss parameters, wherein the loss parameters are used to indicate the degree to which the segmentation corresponding to the representation vector conforms to the target dialogue attribute;
[0086] Optionally, in this embodiment, the representation vector corresponding to the above-mentioned word segmentation is generated by the above-mentioned multiple attention modules. Specifically, it can be understood that in the case of i attention modules, the representation vector generated by the i-th attention module is the representation vector corresponding to the above-mentioned word segmentation, wherein the input of each attention module is the output of the previous attention module. Unperturbed conversation text refers to preprocessed text that removes redundant information and retains the key content of the conversation. This processing method aims to extract the main information in the conversation to reduce interference to the classifier.
[0087] In order to better represent the semantic information of word segmentation, this embodiment adopts a representation vector as a representation method of word segmentation. The representation vector is usually generated by a pre-trained word vector model, which can better capture the semantics and contextual information of the word. The target attribute classifier is a model used to determine whether the word segmentation conforms to the target dialogue attribute. The classifier can be a traditional machine learning method or a deep learning model. The appropriate classifier is selected according to specific needs. The loss parameter is used to measure the degree to which the word segmentation corresponding to the representation vector conforms to the target dialogue attribute. By inputting the loss parameter into the target attribute classifier, it can be used to update the historical information matrix to provide reference and optimization in subsequent classification.
[0088] For example, suppose that the restaurant conversation is classified by attributes, such as "service attitude", etc. First, the conversation text is processed without disturbance to remove non-critical information.
[0089] Next, each word segment is converted into a representation vector and input into the target attribute classifier. The classifier will determine the optimization direction based on the loss parameter and determine whether the word segmentation meets the target attribute.
[0090] For example, for the sentence "The service attitude is very good, and the food quality is average", "service attitude" and "food quality" are input into the classifier as target attributes. The classifier can get the loss function value based on the representation vector and loss parameter, which reflects the degree to which the word segmentation meets the target attribute. According to the loss function value, the historical information matrix can be updated to provide reference and optimization in subsequent classification. The historical information matrix can contain information such as the historical classification results and loss parameters of each word segmentation, which is used to assist the training and optimization of the classifier. Through continuous iteration and updating, the accuracy of attribute classification of the dialogue text can be improved.
[0091] Optionally, in this embodiment, the above-mentioned use of the loss parameter to update the historical information matrix can be understood as adding an attribute classifier after the transformer dialogue model, taking the output vector of the last transformer layer as the input of the attribute classifier, calculating the attribute loss (the attribute classifier will calculate the loss parameter, which is used to indicate whether the currently generated text meets the desired attribute. The larger the loss parameter, the less it meets the desired attribute, and it should be minimized as much as possible). By freezing the model parameters of the transformer dialogue model, the loss of the attribute classifier is used for reverse propagation to update the historical information matrix H of each time step of the dialogue model. t , instead of updating the model parameters.
[0092] Specifically, by inserting t A trainable tensor T of the same shape t (As a reference to H tWhen backpropagating with the loss of the attribute classifier, the model parameters of the dialogue model are frozen, and the gradient of the model parameters is not calculated. Instead, the trainable tensor T is calculated. t gradient.
[0093] T t Denoted as H t The updated value of state H t +T t To generate will move the probability distribution, making the generated text more likely to have the desired attribute a, by updating the value T t Initialized to 0, use the gradient of the attribute classifier model to update T t The attribute classifier model measures the extent to which the generated text has the desired attribute a. Rewrite the attribute classifier model p(a|x) as p(a|H t +T t ), then T t The update process is:
[0094]
[0095] Among them, α is the update step size. Through the update process of the above formula, T is calculated t After that, the state H can be disturbed t , the update step size can be understood as the learning rate. The larger the value, the greater the update amplitude.
[0096] S208, inputting the initial dialogue text into the target dialogue model for a perturbed forward pass, and outputting a perturbed dialogue text, wherein the perturbed dialogue text represents the dialogue text output by the target dialogue model after the historical information matrix is updated using the loss parameter, and each word segment in the perturbed dialogue text conforms to the target dialogue attributes.
[0097] Optionally, in this embodiment, in the target dialogue model, the initial dialogue text input can be forward-passed with disturbance to output the disturbed dialogue text. The disturbance here refers to updating the historical information matrix to indicate that the target dialogue model is disturbed. Each word segmentation in the disturbed dialogue text conforms to the attributes of the target dialogue.
[0098] Exemplarily, assume that the target dialogue model is used to generate dialogues about travel. An initial dialogue text is input into the model for forward pass to obtain a dialogue text based on the model. Then, the historical information matrix is updated to a certain extent to perturb the target dialogue model. In the perturbed dialogue text, each segmented word should conform to the properties of the target dialogue, that is, related to the topic of travel. For example, if the initial dialogue text is "I want to travel", the perturbed dialogue text may become "I plan to travel, then visit the Eiffel Tower, and finally taste delicious food." Each segmented word in this dialogue text is related to travel and conforms to the properties of the target dialogue model.
[0099] Through this embodiment, the initial dialogue text and target dialogue attributes in the target dialogue scene are obtained, wherein the target dialogue attributes are used to indicate the dialogue style of the target dialogue scene; the initial dialogue text is input into the target dialogue model for undisturbed forward transmission, and the undisturbed dialogue text is output, wherein the undisturbed dialogue text includes segmentations that do not conform to the target dialogue attributes, and the undisturbed dialogue text is composed of segmentations output by the target dialogue model, and the target dialogue model is used to determine the output segmentations according to the input segmentations and the historical information matrix step by step according to the time step, and determine the corresponding dialogue text according to the output segmentations, the target dialogue model includes multiple attention modules, and the historical information matrix includes key-value pairs corresponding to each attention module in the multiple attention modules; the representation vectors corresponding to each segmentation in the undisturbed dialogue text are respectively input into the target attribute classifier, and the loss parameters of the target attribute classifier are respectively determined, and the historical information matrix is updated using the loss parameters, wherein the loss parameters are used to represent The characterization vector represents the degree to which the word segmentation corresponding to the target dialogue attribute conforms to the target dialogue attribute; the initial dialogue text is input into the target dialogue model for a perturbed forward pass, and the perturbed dialogue text is output, wherein the perturbed dialogue text represents the dialogue text output by the target dialogue model after the historical information matrix is updated using the loss parameter, and each word segmentation in the perturbed dialogue text conforms to the target dialogue attribute. In the inference stage of the dialogue model, the attribute discriminator is used to measure whether the generated text conforms to the target attribute and style, and the loss of the attribute discriminator is backward passed and returned to perturb and update the state of the dialogue model, thereby increasing the possibility of conforming to the attribute constraints and guiding the pre-trained dialogue model to generate a reply text that satisfies the attribute constraints, thereby achieving the technical effect of style-controllable text generation and improving the generation efficiency and accuracy of dialogue texts that conform to the attribute constraints, thereby solving the technical problem of low generation efficiency and accuracy of dialogue texts that conform to the attribute constraints.
[0100] In addition, chatbots with specific attributes and styles can be customized according to user preferences and needs to enhance user immersion and participation and improve user interaction experience. Responses with attributes such as comfort, encouragement, and empathy can also be generated according to the user's emotional state to provide emotional support and psychological counseling. By achieving text generation with controllable attributes and styles, the dialogue model can better meet different scenarios and user needs, and improve the value and breadth of application of the product.
[0101] As an optional solution, the method further includes: inputting the initial dialogue text into the target dialogue model to generate t first representation vectors, wherein the target dialogue model includes an i-layer attention module, the first representation vector represents the output result of the i-layer attention module, the target dialogue model outputs t segmented words in t time steps in an iterative loop, the j+1 segmented word is determined by the j-th segmented word output by the target dialogue model at the j-time step and the j-th historical information matrix input into the target dialogue model at the j+1 time step, the j-th historical information matrix is used to represent the i key-value pairs corresponding to the j-time step, the i key-value pairs correspond to the i-layer attention module one-to-one, and the t first representation vectors are used to determine the segmented words composed of t segmented words. The unperturbed dialogue text is generated, i is an integer greater than or equal to 2, t is an integer greater than or equal to j+1, and j is an integer greater than or equal to 1; the t first representation vectors are respectively input into the target attribute classifier, t classification results are determined, and the target history information matrix is updated, wherein the target history information matrix represents the history information matrix corresponding to the target word segmentation output by the target dialogue model after the target dialogue model is input, and the target word segmentation includes the word segmentation determined by the t classification results that does not conform to the target dialogue attribute; the initial dialogue text is input into the target dialogue model, and t second representation vectors are determined by using the updated target history information matrix to generate the perturbed dialogue text, wherein the second representation vector is generated in the same way as the first representation vector.
[0102] Optionally, in this embodiment, the target dialogue model can be understood as generating new segmentations each time according to the input segmentations and the historical information matrix, and inputting the segmentations output last time and the new historical information matrix to output updated segmentations next time.
[0103] It should be noted that in the target dialogue model, the initial dialogue text is input and t first representation vectors are generated. These vectors represent the output results of the i-layer attention module in the target dialogue model at different time steps. The target dialogue model uses an iterative loop to output a segmentation at each time step, and a total of t segmentations are output. The generation of the j+1th segmentation depends on the jth segmentation output by the target dialogue model at the jth time step and the jth historical information matrix. The jth historical information matrix is used to represent the i key-value pairs at the jth time step, where the i key-value pairs correspond one-to-one to the i-layer attention module. At the same time, the t first representation vectors also play a role in determining the undisturbed dialogue text composed of t segmentations. In this process, i represents an integer greater than or equal to 2, indicating that there are multiple layers of attention modules; t represents an integer greater than or equal to j+1, indicating how many time steps there are; j represents an integer greater than or equal to 1, indicating the current time step.
[0104] Through this embodiment, the target dialogue model can generate dialogue text with certain coherence and logic. This model has a wide range of applications in the fields of natural language processing and dialogue systems. By continuously optimizing the structure and parameters of the model, the quality and effect of dialogue generation can be further improved.
[0105] As an optional scheme, t first representation vectors are respectively input into a pre-trained target attribute classifier, t classification results are determined, and the target history information matrix is updated, including: t first representation vectors are respectively input into the target attribute classifier, t loss parameters are determined, wherein the t classification results correspond to the t loss parameters one by one; a target loss parameter corresponding to the nth time step is determined from the t loss parameters, wherein the target loss parameter represents a loss parameter that does not meet a preset loss condition, and n is a positive integer less than or equal to t; and the target history information matrix is updated according to the target loss parameter.
[0106] Optionally, in this embodiment, the one-to-one correspondence between the above t classification results and the t loss parameters can be understood as a classification structure determined by a loss parameter, and the preset loss condition can be set to a preset threshold. When the loss parameter is greater than the preset threshold, it is considered that the classification result indicates that the corresponding word segmentation does not meet the target dialogue attributes, and vice versa.
[0107] It should be noted that the above-mentioned nth time step can be understood as the time step corresponding to the loss parameter that does not meet the preset loss condition. For example, if the loss parameter obtained by inputting the representation vector generated in the first time step into the target attribute classifier does not meet the preset loss condition, then the first time step is the above-mentioned nth time step.
[0108] In an exemplary embodiment, in the field of machine learning, a target attribute classifier is an important tool for classifying an input first representation vector, specifically including but not limited to the following steps:
[0109] S1, input of the first representation vector and determination of the classification result:
[0110] For the given t first representation vectors, input them into the target attribute classifier for classification. The classifier will calculate the corresponding classification result based on the input feature vector. For example, for a face recognition system, the feature vector of the face image can be used as input, and the classifier will output the category of the person to which the face belongs. For a sentence, the representation vector corresponding to each word in the sentence can be used as input, and the classifier will output whether each representation vector conforms to the above target dialogue style.
[0111] S2, determine t loss parameters:
[0112] After the classification results are obtained, t loss parameters need to be determined. The loss parameters are indicators used to measure whether the output text conforms to the target conversation style.
[0113] S3, determine the target loss parameter corresponding to the nth time step:
[0114] The target loss parameter refers to the loss parameter that does not meet the preset loss condition. Among the t loss parameters, it is necessary to determine the target loss parameter corresponding to the nth time step. This can be achieved by setting a suitable threshold. For example, if the threshold of the error rate is set to 0.1, then when the error rate of a certain time step is greater than 0.1, the loss parameter corresponding to the time step is the target loss parameter.
[0115] S4, update the target history information matrix according to the target loss parameter:
[0116] The target history information matrix is an important data structure used to store historical information. According to the target loss parameter, the target history information matrix can be updated. The updating method can be determined according to specific needs.
[0117] As an optional scheme, the target history information matrix is updated according to the target loss parameter, including: obtaining an initial tensor that allows training; determining the gradient of the target attribute classifier according to the target loss parameter, and using the gradient to train the initial tensor until the target tensor is obtained, wherein the target tensor represents an updated value of the target history information matrix; and determining the sum of the target history information matrix and the target tensor as the updated target history information matrix.
[0118] Optionally, in this embodiment, the above initial tensor can be but is not limited to being initialized to 0, and the tensor shape is [batch_size, head_num, seq_len, hidden_size], where batch_size is the batch size, head_num is the number of attention heads, seq_len is the sequence length, and hidden_size is the hidden dimension.
[0119] Optionally, in this embodiment, the above-mentioned determination of the gradient of the target attribute classifier according to the target loss parameter and the use of the gradient to train the initial tensor until the target tensor is obtained may include but is not limited to inserting and H t A trainable tensor T of the same shape t (As a reference to H t When backpropagating with the loss of the attribute classifier, the model parameters of the dialogue model are frozen, and the gradient of the model parameters is not calculated. Instead, the trainable tensor T is calculated. t gradient.
[0120] T t Denoted as H t The updated value of state H t +T t To generate will move the probability distribution, making the generated text more likely to have the desired attribute a, by updating the value T t Initialized to 0, use the gradient of the attribute classifier model to update T t .
[0121] As an optional scheme, the gradient of the target attribute classifier is determined according to the target loss parameter, and the gradient is used to train the initial tensor to obtain the target tensor, including: obtaining a predetermined training step size, wherein the value of the training step size is positively correlated with the amplitude adjusted by the initial tensor in each round of training; training the initial tensor in each round according to the training step size, using the trained tensor to update the target history information matrix in each round, and recalculating the loss parameter and gradient corresponding to the nth time step of the round after each target history information matrix is updated, until the loss parameter corresponding to the nth time step meets the preset loss condition, and determining the tensor after the training of the round as the target tensor, wherein the tensor after each round of training is determined by the tensor before each round of training, the gradient corresponding to each round, and the update step size.
[0122] Optionally, in this embodiment, the above-mentioned training step size is used to represent the adjustment amplitude of the tensor in each round of training. The above-mentioned round-by-round training can be understood as when a certain word segmentation does not conform to the target dialogue style, its corresponding historical information matrix is modified and the loss function is recalculated. When the recalculated loss function still does not meet the preset loss condition, its corresponding historical information matrix is modified again, and the loss function is recalculated until the recalculated loss function meets the preset loss condition.
[0123] In an exemplary embodiment, the above attribute classifier model measures the extent to which the generated text has the desired attribute a. The attribute classifier model p(a|x) is rewritten as p(a|H t +T t ), then T t The update process is:
[0124]
[0125] As an optional solution, the initial dialogue text is input into the target dialogue model for a perturbed forward pass, and the perturbed dialogue text is output, including: inputting the initial dialogue text into the target dialogue model to generate t second representation vectors, wherein the historical information matrix corresponding to the nth time step is updated; inputting the t second representation vectors into the target attribute classifier respectively, and re-determining t classification results, wherein the re-determined t classification results all indicate that the corresponding word segmentation meets the target dialogue attribute; performing a sampling operation on the t second representation vectors to obtain the perturbed dialogue text.
[0126] Optionally, in this embodiment, the completion of the update of the historical information matrix corresponding to the nth time step can be understood as the loss parameters corresponding to the nth time step all satisfy the preset loss conditions, that is, the loss parameters corresponding to the nth time step are as small as possible.
[0127] It should be noted that the above-mentioned re-determined t classification results can be understood as modifying the corresponding historical information matrix and recalculating the above-mentioned loss parameters to re-obtain the above-mentioned t classification results.
[0128] As an optional scheme, the above method also includes: performing normalization operations on t first characterization vectors and t second characterization vectors respectively to obtain a first probability distribution and a second probability distribution; determining a target divergence value based on the first probability distribution and the second probability distribution, wherein the target divergence value is used to measure the difference between the first probability distribution and the second probability distribution; and updating the target historical information matrix with the goal of minimizing the value of the target divergence value.
[0129] Optionally, in this embodiment, the normalization operation performed on the t first characterization vectors and the t second characterization vectors respectively may include but is not limited to being implemented by a softmax function, such as Figure 3 As shown, the first probability distribution and the second probability distribution corresponding to the first characterization vector and the second characterization vector are obtained respectively through the linear transformation layer and the activation function.
[0130] In an exemplary embodiment, by perturbing the historical information matrix H of the dialogue model t , to make the generated text more consistent with the desired attribute a. Since it may affect the fluency of the generated text, in order to maintain the fluency of the generated text, when updating H t When , calculate the probability distribution p of the unperturbed forward pass t+1 and the probability distribution of the perturbed forward pass The KL divergence of , by minimizing the KL divergence loss, to maintain the fluency of the generated response after the disturbance, measures the difference between the two probability distributions, the closer the KL divergence, the smaller it is.
[0131] As an optional solution, the above method also includes: obtaining unlabeled dialogue corpus data; preprocessing the dialogue corpus data to obtain target dialogue corpus, wherein the target dialogue corpus represents the dialogue corpus between two objects; using the target dialogue corpus to pre-train the initial dialogue model to obtain a target dialogue model, wherein the initial dialogue model and the target dialogue model use a bidirectional attention mechanism on the input text and a unidirectional attention mechanism on the output text.
[0132] Optionally, in this embodiment, a social media platform can be used as a data source, and a large amount of open domain dialogue corpus can be captured by crawlers and other means. The collected raw dialogue corpus data is preprocessed and cleaned to improve data quality.
[0133] It should be noted that the preprocessing steps include: removing irrelevant information such as links, HTML tags, and advertisements; removing repeated, meaningless, or low-quality conversations, etc. Filter out conversations between three or more people, and only keep conversations between two people as training data. Separate different conversation texts with special markers [SEP].
[0134] The following shows a training data sample, where X is the input text and Y is the output text.
[0135] "X = Are hamsters suitable as pets? [SEP] I think they are suitable. My hamster is very well behaved and doesn't bite people. [SEP] How do you feed it?"
[0136] "Y = I bought some rat food, and a water bottle and stuff. If it doesn't eat, it'll be hungry, and if it's hungry, it'll eat."
[0137] The target dialogue model obtained by pre-training the initial dialogue model with the target dialogue data may include but is not limited to the following contents:
[0138] The dialogue model uses a transformer structure based on the self-attention mechanism. The transformer has strong modeling capabilities, good scalability, and can perform parallel computing well. A bidirectional attention mechanism is used on the input text, and each word in the input text can pay attention to all other words; a unidirectional attention mechanism is used on the output text, that is, attention from left to right, and each word in the output text can only pay attention to the previous word, but not the subsequent word. Encoding the input text with a bidirectional language model helps the dialogue model better understand the semantic information of the input text. The cross entropy loss function is used to measure the loss between the dialogue model's predicted response and the actual response.
[0139] As an optional solution, obtaining the initial conversation text and target conversation attributes in the target conversation scenario includes: obtaining account data of the target account, wherein the target account represents an account participating in the target conversation scenario; and determining the target conversation attributes based on the account data.
[0140] Optionally, in this embodiment, the above account data can be understood as data that has been actively confirmed and provided by the user, for example, the user clicks a button to select dialogue attributes, etc.
[0141] As an optional solution, determining the target conversation attribute based on the account data includes at least one of the following: obtaining the interaction data of the target account, and determining the target conversation attribute as the conversation attribute related to the interaction data; obtaining the search history of the target account, and determining the target conversation attribute as the conversation attribute related to the search history; obtaining the emotion type of the initial conversation text, and determining the target conversation attribute as the conversation attribute with the same emotion type as the initial conversation text.
[0142] Optionally, in this embodiment, the above interaction data may include but is not limited to interaction data such as clicks and long presses of the target account in the conversation scene. The above search history can be understood as the user's search history on the platform, including the type of user search, such as feminine products, etc. The above emotion type can be understood as the emotion type determined by the initial conversation text, such as positive emotion or negative emotion, etc.
[0143] The present application is further explained below with reference to specific examples:
[0144] The present application proposes a style-controllable dialogue generation method based on an attribute discriminator. Compared with a dialogue model that is directly trained to meet a certain attribute constraint condition, the present application does not require any changes or retraining to the pre-trained dialogue model. In the reasoning stage of the dialogue model, the present application uses an attribute discriminator to measure whether the generated text meets the desired attributes, and the loss of the attribute discriminator is passed back and forth to perturb and update the state of the dialogue model, thereby increasing the possibility of meeting the attributes, so as to guide the pre-trained dialogue model to generate a reply text that meets the attribute constraints, thereby achieving style-controllable dialogue generation. The present application can flexibly control the attribute constraints of the generated replies, while ensuring that the generated replies meet the attribute requirements, while maintaining the fluency of the generated replies.
[0145] In an exemplary embodiment, a chatbot with specific attributes and styles, such as feminine, formal, friendly, etc., can be customized according to the user's preferences and needs, to enhance the user's immersion and participation and improve the user's interactive experience. In addition, replies with attributes such as comfort, encouragement, and empathy can be generated according to the user's emotional state to provide emotional support and psychological counseling. In general, by achieving text generation with controllable attributes and controllable styles, the dialogue model can better meet different scenarios and user needs, and improve the value and breadth of application of the product.
[0146] Figure 5 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application, such as Figure 5 As shown, including but not limited to the following processes:
[0147] S502, collect open domain dialogue data:
[0148] Using social media platforms as data sources, we use crawlers and other means to capture a large amount of open-domain dialogue corpus. We pre-process and clean the collected raw dialogue corpus data to improve data quality.
[0149] The preprocessing steps include: removing irrelevant information such as links, HTML tags, and advertisements; removing repeated, meaningless, or low-quality conversations, etc. Conversations between three or more people are filtered out, and only conversations between two people are retained as training data.
[0150] S504, pre-trained general dialogue model:
[0151] The dialogue model uses a transformer structure based on the self-attention mechanism. The transformer has strong modeling capabilities, good scalability, and can perform parallel computing well. A bidirectional attention mechanism is used on the input text, and each word in the input text can pay attention to all other words; a unidirectional attention mechanism is used on the output text, that is, attention from left to right, and each word in the output text can only pay attention to the previous word, but not the subsequent word. Encoding the input text with a bidirectional language model helps the dialogue model better understand the semantic information of the input text. The cross entropy loss function is used to measure the loss between the dialogue model's predicted response and the actual response. Figure 6 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application. The model structure of the dialogue model is as follows: Figure 6 As shown, bos (begin of sentence) represents the beginning of a sentence, and eos (end of sentence) represents the end of a sentence. Usually, a special symbol bos is added at the beginning of X, and a special symbol eos is added at the end of X. Similarly, a special symbol bos is added at the beginning of Y, and a special symbol eos is added at the end of Y.
[0152] In an exemplary embodiment, a general dialogue model with a transformer model structure is pre-trained on a massive amount of open-domain dialogue corpus (on the order of billions). The pre-trained general dialogue model already has good human-computer interaction and dialogue capabilities, and can generate fluent responses that conform to the context. However, since the general dialogue model is an unconditional language model, the generated text content cannot meet the properties that need to be controlled.
[0153] S506, style-controllable reply generation:
[0154] By giving the input word sequence and output word sequence: X = {x0, x1, ..., x m}; Y = {y0,y1,…,y n}, the language model calculates the probability distribution of the word sequence P(Y|X). According to the chain rule, the probability distribution of the word sequence can be written as the product of conditional probabilities.
[0155]
[0156] The transformer dialogue model is used to model the probability distribution of word sequences. Based on the dialogue history, the transformer generates a complete reply text one word after another in an iterative loop. Let’s first briefly introduce the transformer using the concept of iterative loop. Define the history information matrix H t Contains the past key-value pairs in, is the key-value pair of all time steps from 0 to t of the i-th transformer layer. The transformer layer is composed of at least self-attention blocks, feedforward neural network blocks, etc. The key-value pair refers to K and V in the self-attention mechanism. Specifically, K / V is a matrix tensor with a shape of [batch_size, head_num, seq_len, hidden_size]. batch_size is the batch size, head_num is the number of attention heads, seq_len is the sequence length, and hidden_size is the hidden dimension. The transformer with efficient implementation is given the previous input word y. t In the case of t To generate the next word y t+1 The loop interpretation of the transformer can be expressed as the following mathematical expression.
[0157] o t+1 ,H t+1 =LM(y t ,H t )
[0158] Among them, t+1 That is, the word segmentation in the aforementioned undisturbed dialogue text;
[0159] y t+1 ~p t+1 =softmax(Wo t+1 )
[0160] Where W is a linear transformation matrix that maps the logits vector to the vector on the vocabulary. t+1 From the probability distribution p t+1 With the cached historical information matrix state H t , which can avoid repeated calculation of the previous word sequence {y0,y1,…,y t-1} forward pass, which can effectively reduce the inference time.
[0161] Figure 7 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application, wherein the attribute-controllable reply generation is schematically shown as follows Figure 7 As shown in the figure, an attribute classifier p(a|x) is trained. a represents the expected attribute, and the attribute classifier p(a|x) is used to measure whether the generated attribute meets the expected attribute. For example, attribute a represents the sentiment tendency, and the generated reply text is expected to have a positive sentiment tendency, rather than a negative sentiment tendency. Figure 8 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application, such as Figure 8As shown, the attribute classifier consists of a fully connected layer 802, a nonlinear activation function layer 804, and a fully connected layer 806. This attribute classifier is pre-trained on a sentiment classification dataset.
[0162] Add this attribute classifier after the transformer dialogue model, use the output vector of the last transformer layer as the input of the attribute classifier, and calculate the attribute loss, which measures whether the generated text meets the expected attributes. Freeze the model parameters of the transformer dialogue model, use the loss of the attribute classifier for back propagation, and update the state history information matrix H of the dialogue model. t , instead of updating the model parameters, insert t A trainable tensor T of the same shape t , when backpropagating with the loss of the attribute classifier, the model parameters of the dialogue model are frozen, and the gradient of the model parameters is not calculated. Instead, the trainable tensor T is calculated. t gradient.
[0163] T t Recorded as state H t The updated value of state H t +T t To generate will move the probability distribution, making the generated text more likely to have the desired attribute a. The value T will be updated t Initialized to 0, use the gradient of the attribute classifier model to update T t The attribute classifier model measures the extent to which the generated text has the desired attribute a. Rewrite the attribute classifier model p(a|x) as p(a|H t +T t ), then T t The update process is:
[0164]
[0165] Among them, α is the update step size. Through the update process of the above formula, T is calculated t After that, the state H can be disturbed t .
[0166] Disturbance state:
[0167] Forward pass with perturbation: That is the aforementioned disturbed dialogue text;
[0168] sampling:
[0169] By perturbing the state history information matrix H of the dialogue model t, to make the generated text more consistent with the expected attribute a, which may affect the fluency of the generated text. In order to maintain the fluency of the generated text, when updating H t When , calculate the probability distribution p of the unperturbed forward pass t+1 and the probability distribution of the perturbed forward pass The KL divergence of is minimized to maintain the smoothness of the generated response after the perturbation.
[0170]
[0171] The attribute classifier measures whether the generated text meets the expected attributes. The loss of the attribute classifier is used for back propagation to perturb the state history information matrix H of the dialogue model. t ; Based on the perturbed historical information matrix, the dialogue model re-performs the perturbed forward pass to generate a reply text that better conforms to the attribute constraints. In general, based on the attribute classifier, Fig. 9 is a schematic diagram of another optional method for generating a dialogue text according to an embodiment of the present application, such as Fig. 9 As shown in , the process of generating style-controllable responses is:
[0172] S902: The dialogue model performs a normal undisturbed forward pass to generate a probability distribution over the vocabulary.
[0173] S904, the loss is calculated through the attribute classifier, which measures the degree to which the generated text conforms to the desired attributes.
[0174] S906, perform a backward pass, return the loss of the attribute classifier model, and calculate the gradient.
[0175] S908, updating the internal state of the dialogue model to increase the possibility of matching the attributes.
[0176] S910, based on the updated internal state, the dialogue model performs a perturbed forward pass and samples and generates a new word from the newly obtained probability distribution.
[0177] Through this embodiment, there is no need to make any changes or retraining to the pre-trained dialogue model, nor is there any need to collect a large amount of dialogue training data with attribute and style labels. In the inference stage of the dialogue model, the present invention uses an attribute discriminator to measure whether the generated text meets the target attributes and style, and backward propagates and returns the loss of the attribute discriminator, perturbs and updates the state of the dialogue model, and increases the possibility of meeting the attribute constraints, so as to guide the pre-trained dialogue model to generate a reply text that meets the attribute constraints, thereby achieving style-controllable text generation. Compared with the conditional dialogue model, the present application does not need to collect a large amount of dialogue training data with attribute and style labels, nor does it need to retrain or fine-tune the pre-trained general dialogue model. It can flexibly control the attribute constraints of the generated reply, and maintain the fluency of the generated reply while making the generated reply meet the attribute requirements.
[0178] It is understandable that in the specific implementation of this application, related data such as user information is involved. When the above embodiments of this application are applied to specific products or technologies, user permission or consent is required, and the collection, use and processing of relevant data need to comply with relevant laws, regulations and standards of relevant countries and regions.
[0179] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0180] According to another aspect of the embodiment of the present application, a device for generating a dialogue text for implementing the above-mentioned method for generating a dialogue text is also provided. Fig.10 As shown, the device comprises:
[0181] An acquisition module 1002 is used to acquire an initial dialogue text and a target dialogue attribute in a target dialogue scene, wherein the target dialogue attribute is used to indicate a dialogue style of the target dialogue scene;
[0182] The first processing module 1004 is used to input the initial dialogue text into the target dialogue model for non-perturbation forward transmission, and output non-perturbation dialogue text, wherein the non-perturbation dialogue text includes segmented words that do not conform to the target dialogue attribute, and the non-perturbation dialogue text is composed of segmented words output by the target dialogue model, and the target dialogue model is used to determine the output segmented words according to the input segmented words and the historical information matrix step by step according to the time step, and determine the corresponding dialogue text according to the output segmented words, and the target dialogue model includes multiple attention modules, and the historical information matrix includes key-value pairs corresponding to each attention module in the multiple attention modules;
[0183] An updating module 1006 is used to input the representation vectors corresponding to each segmentation in the unperturbed dialogue text into the target attribute classifier, respectively, determine the loss parameters of the target attribute classifier, and update the historical information matrix using the loss parameters, wherein the loss parameters are used to indicate the degree to which the segmentation corresponding to the representation vector conforms to the target dialogue attribute;
[0184] The second processing module 1008 is used to input the initial dialogue text into the target dialogue model for a perturbed forward pass, and output a perturbed dialogue text, wherein the perturbed dialogue text represents the dialogue text output by the target dialogue model after the historical information matrix is updated using the loss parameter, and each word segment in the perturbed dialogue text conforms to the target dialogue attribute.
[0185] As an optional solution, the device is also used to: input the initial dialogue text into the target dialogue model to generate t first representation vectors, wherein the target dialogue model includes an i-layer attention module, the first representation vector represents the output result of the i-layer attention module, the target dialogue model outputs t segmented words in t time steps in an iterative loop, the j+1 segmented word is determined by the j-th segmented word output by the target dialogue model at the j-th time step and the j-th historical information matrix input into the target dialogue model at the j+1 time step, the j-th historical information matrix is used to represent the i key-value pairs corresponding to the j-th time step, the i key-value pairs correspond to the i-layer attention module one-to-one, and the t first representation vectors are used to determine the segmented word composed of the t segmented words. The non-disturbanced dialogue text, i is an integer greater than or equal to 2, t is an integer greater than or equal to j+1, and j is an integer greater than or equal to 1; the t first representation vectors are respectively input into the target attribute classifier, t classification results are determined, and a target history information matrix is updated, wherein the target history information matrix represents a history information matrix corresponding to target segmentations output by the target dialogue model after being input into the target dialogue model, and the target segmentations include segmentations determined by the t classification results that do not conform to the target dialogue attribute; the initial dialogue text is input into the target dialogue model, t second representation vectors are determined using the updated target history information matrix, and the perturbed dialogue text is generated, wherein the second representation vector is generated in the same manner as the first representation vector.
[0186] As an optional scheme, the device is used to input the t first characterization vectors into a pre-trained target attribute classifier, determine t classification results, and update the target history information matrix in the following manner: input the t first characterization vectors into the target attribute classifier, determine t loss parameters, wherein the t classification results correspond one-to-one to the t loss parameters; determine the target loss parameter corresponding to the nth time step from the t loss parameters, wherein the target loss parameter represents a loss parameter that does not meet a preset loss condition, and n is a positive integer less than or equal to t; and update the target history information matrix according to the target loss parameter.
[0187] As an optional scheme, the device is used to update the target historical information matrix according to the target loss parameter in the following manner: obtain an initial tensor that allows training; determine the gradient of the target attribute classifier according to the target loss parameter, and use the gradient to train the initial tensor until the target tensor is obtained, wherein the target tensor represents an updated value for the target historical information matrix; and determine the sum of the target historical information matrix and the target tensor as the updated target historical information matrix.
[0188] As an optional scheme, the device is used to determine the gradient of the target attribute classifier according to the target loss parameter in the following manner, and use the gradient to train the initial tensor to obtain the target tensor: obtain a predetermined training step size, wherein the value of the training step size is positively correlated with the amplitude adjusted by the initial tensor in each round of training; train the initial tensor in each round according to the training step size, use the trained tensor to update the target history information matrix in each round, and recalculate the loss parameter and gradient corresponding to the nth time step in that round after each update of the target history information matrix, until the loss parameter corresponding to the nth time step meets the preset loss condition, and determine the trained tensor in that round as the target tensor, wherein the trained tensor in each round is jointly determined by the tensor before each round of training, the gradient corresponding to each round, and the update step size.
[0189] As an optional solution, the device is used to input the initial dialogue text into the target dialogue model for a disturbed forward pass and output the disturbed dialogue text in the following manner: input the initial dialogue text into the target dialogue model to generate the t second representation vectors, wherein the historical information matrix corresponding to the nth time step has completed the update; input the t second representation vectors into the target attribute classifier respectively, and re-determine t classification results, wherein the re-determined t classification results all indicate that the corresponding word segmentation conforms to the target dialogue attribute; perform a sampling operation on the t second representation vectors to obtain the disturbed dialogue text.
[0190] As an optional scheme, the device is also used to: perform normalization operations on the t first characterization vectors and the t second characterization vectors respectively to obtain a first probability distribution and a second probability distribution; determine a target divergence value based on the first probability distribution and the second probability distribution, wherein the target divergence value is used to measure the difference between the first probability distribution and the second probability distribution; and update the target historical information matrix with the goal of minimizing the value of the target divergence value.
[0191] As an optional solution, the device is also used to: obtain unlabeled dialogue corpus data; preprocess the dialogue corpus data to obtain target dialogue corpus, wherein the target dialogue corpus represents a dialogue corpus between two objects; use the target dialogue corpus to pre-train an initial dialogue model to obtain the target dialogue model, wherein the initial dialogue model and the target dialogue model use a bidirectional attention mechanism on the input text and a unidirectional attention mechanism on the output text.
[0192] As an optional solution, the device is used to obtain the initial conversation text and target conversation attributes in the target conversation scenario in the following manner: obtain account data of the target account, wherein the target account represents an account participating in the target conversation scenario; and determine the target conversation attributes based on the account data.
[0193] As an optional scheme, the device is used to determine the target conversation attribute based on the account data in at least one of the following ways: obtaining the interaction data of the target account, and determining the target conversation attribute as a conversation attribute related to the interaction data; obtaining the search history of the target account, and determining the target conversation attribute as a conversation attribute related to the search history; obtaining the emotion type of the initial conversation text, and determining the target conversation attribute as a conversation attribute having the same emotion type as the initial conversation text.
[0194] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0195] According to one aspect of the present application, a computer program product is provided. The computer program product includes a computer program.
[0196] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0197] Fig.11 The structure block diagram of a computer system for implementing an electronic device according to an embodiment of the present application is schematically shown.
[0198] It should be noted that Fig.11 The computer system 1100 of the electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0199] like Fig.11As shown, the computer system 1100 includes a central processing unit 1101 (CPU), which can perform various appropriate actions and processes according to the program stored in the read-only memory 1102 (ROM) or the program loaded from the storage part 1108 to the random access memory 1103 (RAM). Various programs and data required for system operation are also stored in the random access memory 1103. The central processing unit 1101, the read-only memory 1102 and the random access memory 1103 are connected to each other through a bus 1104. An input / output interface 1105 (Input / Output interface, i.e., I / O interface) is also connected to the bus 1104.
[0200] The following components are connected to the input / output interface 1105: an input section 1106 including a keyboard, a mouse, etc.; an output section 1107 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 1108 including a hard disk, etc.; and a communication section 1109 including a network interface card such as a LAN card, a modem, etc. The communication section 1109 performs communication processing via a network such as the Internet. A drive 1110 is also connected to the input / output interface 1105 as needed. A removable medium 1111, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 1110 as needed so that a computer program read therefrom is installed into the storage section 1108 as needed.
[0201] In particular, according to an embodiment of the present application, the process described in each method flow chart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer readable medium, and the computer program contains a program code for executing the method shown in the flow chart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 1109, and / or installed from the removable medium 1111. When the computer program is executed by the central processor 1101, various functions defined in the system of the present application are executed.
[0202] In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 1109, and / or installed from the removable medium 1111. When the computer program is executed by the central processor 1101, various functions provided by the embodiments of the present application are performed.
[0203] According to another aspect of the embodiment of the present application, an electronic device for implementing the above-mentioned method for generating a dialogue text is also provided. The electronic device may be Figure 1 The terminal device or server shown in the figure. This embodiment is described by taking the electronic device as a terminal device as an example. Fig.12 As shown, the electronic device includes a memory 1202 and a processor 1204. The memory 1202 stores a computer program, and the processor 1204 is configured to execute the steps in any of the above method embodiments through the computer program.
[0204] Optionally, in this embodiment, the electronic device may be located in at least one network device among a plurality of network devices of a computer network.
[0205] Optionally, in this embodiment, the above-mentioned processor can be configured to execute the methods in each embodiment of the present application through a computer program.
[0206] Alternatively, a person skilled in the art may understand that: Fig.12 The structure shown is for illustration only. Fig.12 The structure of the electronic device is not limited. Fig.12 More or fewer components (such as network interfaces, etc.) as shown in, or with Fig.12 Different configurations are shown.
[0207] Among them, the memory 1202 can be used to store software programs and modules, such as the program instructions / modules corresponding to the method and device for generating the dialogue text in the embodiment of the present application. The processor 1204 executes various functional applications and data processing by running the software programs and modules stored in the memory 1202, that is, realizing the above-mentioned method for generating dialogue text. The memory 1202 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 1202 may further include a memory remotely arranged relative to the processor 1204, and these remote memories may be connected to the terminal via a network. Examples of the above-mentioned networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof. Among them, the memory 1202 can be specifically, but not limited to, used to store information such as dialogue text. As an example, such as Fig.12 As shown, the memory 1202 may include, but is not limited to, the acquisition module 1002, the first processing module 1004, the updating module 1006, and the second processing module 1008 in the device for generating the conversation text. In addition, other module units in the device for generating the conversation text may also be included but are not limited to, which will not be described in detail in this example.
[0208] Optionally, the transmission device 1206 is used to receive or send data via a network. Specific examples of the network may include a wired network and a wireless network. In one example, the transmission device 1206 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices and routers via a network cable so as to communicate with the Internet or a local area network. In one example, the transmission device 1206 is a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0209] In addition, the electronic device further includes: a display 1208 for displaying the conversation text; and a connection bus 1210 for connecting various module components in the electronic device.
[0210] In other embodiments, the terminal device or server may be a node in a distributed system, wherein the distributed system may be a blockchain system, and the blockchain system may be a distributed system formed by connecting the multiple nodes through network communication. The nodes may form a peer-to-peer network, and any form of computing device, such as a server, terminal or other electronic device, may become a node in the blockchain system by joining the peer-to-peer network.
[0211] According to one aspect of the present application, a computer-readable storage medium is provided, and a processor of an electronic device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the electronic device executes the method for generating dialogue text provided in various optional implementations of the above-mentioned dialogue text generation.
[0212] Optionally, in this embodiment, the above-mentioned computer-readable storage medium can be configured to store data for executing the methods in various embodiments of the present application.
[0213] Optionally, in this embodiment, a person of ordinary skill in the art may understand that all or part of the steps in the various methods of the above embodiments may be completed by instructing hardware related to the terminal device through a program, and the program may be stored in a computer-readable storage medium, and the storage medium may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, etc.
[0214] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0215] If the integrated units in the above embodiments are implemented in the form of software functional units and sold or used as independent products, they can be stored in the above computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for one or more electronic devices to execute all or part of the steps of the methods described in each embodiment of the present application.
[0216] In the above embodiments of the present application, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.
[0217] In the several embodiments provided in the present application, it should be understood that the disclosed application can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0218] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0219] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0220] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A method for generating a dialogue text, characterized in that: include: Acquire an initial dialogue text and a target dialogue attribute in a target dialogue scene, wherein the target dialogue attribute is used to indicate a dialogue style of the target dialogue scene; Inputting the initial dialogue text into the target dialogue model for non-perturbation forward transmission, and outputting non-perturbation dialogue text, wherein the non-perturbation dialogue text includes word segmentations that do not conform to the target dialogue attribute, and the non-perturbation dialogue text is composed of word segmentations output by the target dialogue model, and the target dialogue model is used to determine the output word segmentations according to the input word segmentations and the historical information matrix step by step according to the time step, and determine the corresponding dialogue text according to the output word segmentations, and the target dialogue model includes multiple attention modules, and the historical information matrix includes key-value pairs corresponding to each attention module in the multiple attention modules; Inputting the representation vectors corresponding to each word segment in the unperturbed dialogue text into the target attribute classifier respectively, determining the loss parameters of the target attribute classifier respectively, and updating the historical information matrix using the loss parameters, wherein the loss parameters are used to indicate the degree to which the word segment corresponding to the representation vector conforms to the target dialogue attribute; The initial dialogue text is input into the target dialogue model for a perturbed forward pass, and a perturbed dialogue text is output, wherein the perturbed dialogue text represents the dialogue text output by the target dialogue model after the historical information matrix is updated using the loss parameter, and each word segment in the perturbed dialogue text conforms to the target dialogue attribute.
2. The method according to claim 1, characterized in that The method further comprises: Input the initial dialogue text into the target dialogue model to generate t first representation vectors, wherein the target dialogue model includes an i-layer attention module, the first representation vector represents the output result of the i-layer attention module, the target dialogue model sequentially outputs t participles at t time steps in an iterative loop manner, the j+1th participle is determined by inputting the j-th participle output by the target dialogue model at the j-th time step and the j-th historical information matrix into the target dialogue model at the j+1th time step, the j-th historical information matrix is used to represent i key-value pairs corresponding to the j-th time step, the i key-value pairs correspond one-to-one to the i-layer attention module, the t first representation vectors are used to determine the undisturbed dialogue text composed of the t participles, i is an integer greater than or equal to 2, t is an integer greater than or equal to j+1, and j is an integer greater than or equal to 1; Input the t first representation vectors into the target attribute classifier respectively, determine t classification results, and update the target history information matrix, wherein the target history information matrix represents the history information matrix corresponding to the target segmentation output by the target dialogue model after being input into the target dialogue model, and the target segmentation includes the segmentation determined by the t classification results that does not conform to the target dialogue attribute; The initial dialogue text is input into the target dialogue model, t second representation vectors are determined using the updated target history information matrix, and the disturbed dialogue text is generated, wherein the second representation vector is generated in the same manner as the first representation vector.
3. The method according to claim 2, characterized in that The step of inputting the t first representation vectors into the pre-trained target attribute classifiers respectively, determining t classification results, and updating the target history information matrix comprises: Inputting the t first representation vectors into the target attribute classifier respectively to determine t loss parameters, wherein the t classification results correspond to the t loss parameters one by one; Determine a target loss parameter corresponding to the nth time step from the t loss parameters, wherein the target loss parameter represents a loss parameter that does not meet a preset loss condition, and n is a positive integer less than or equal to t; The target history information matrix is updated according to the target loss parameter.
4. The method according to claim 3, characterized in that The updating of the target history information matrix according to the target loss parameter comprises: Get the initial tensors that allow training; Determining the gradient of the target attribute classifier according to the target loss parameter, and using the gradient to train the initial tensor until a target tensor is obtained, wherein the target tensor represents an updated value of the target history information matrix; The sum of the target history information matrix and the target tensor is determined as the updated target history information matrix.
5. The method according to claim 4, characterized in that Determining the gradient of the target attribute classifier according to the target loss parameter, and using the gradient to train the initial tensor to obtain the target tensor, includes: Obtaining a predetermined training step length, wherein the value of the training step length is positively correlated with the amplitude adjusted by the initial tensor in round-by-round training; The initial tensor is trained round by round according to the training step length, and the target history information matrix is updated using the trained tensor in each round, and the loss parameter and gradient corresponding to the nth time step in the round are recalculated after each update of the target history information matrix, until the loss parameter corresponding to the nth time step meets the preset loss condition, and the trained tensor in the round is determined as the target tensor, wherein the trained tensor in each round is determined by the tensor before each round of training, the gradient corresponding to each round, and the update step length.
6. The method according to claim 3, characterized in that: The step of inputting the initial dialogue text into the target dialogue model for a perturbed forward pass and outputting the perturbed dialogue text comprises: Inputting the initial dialogue text into the target dialogue model to generate the t second representation vectors, wherein the historical information matrix corresponding to the n-th time step is updated; Inputting the t second representation vectors into the target attribute classifier respectively, and re-determining t classification results, wherein the re-determined t classification results all indicate that the corresponding segmented words meet the target dialogue attribute; A sampling operation is performed on the t second representation vectors to obtain the disturbed dialogue text.
7. The method according to claim 2, characterized in that The method further comprises: Performing normalization operations on the t first characterization vectors and the t second characterization vectors respectively to obtain a first probability distribution and a second probability distribution; Determining a target divergence value according to the first probability distribution and the second probability distribution, wherein the target divergence value is used to measure the difference between the first probability distribution and the second probability distribution; The target history information matrix is updated with the goal of minimizing the value of the target divergence value.
8. The method according to claim 1, characterized in that The method further comprises: Obtain unlabeled dialogue corpus data; Preprocessing the dialogue corpus data to obtain a target dialogue corpus, wherein the target dialogue corpus represents a dialogue corpus between two objects; The target dialogue corpus is used to pre-train an initial dialogue model to obtain the target dialogue model, wherein the initial dialogue model and the target dialogue model use a bidirectional attention mechanism on the input text and a unidirectional attention mechanism on the output text.
9. The method according to claim 1, characterized in that: The step of obtaining the initial dialogue text and the target dialogue attributes in the target dialogue scene includes: Acquire account data of a target account, wherein the target account represents an account participating in the target conversation scenario; The target conversation attribute is determined based on the account data.
10. The method according to claim 9, characterized in that The determining the target conversation attribute according to the account data includes at least one of the following: Acquiring interaction data of the target account, and determining the target conversation attribute as a conversation attribute related to the interaction data; Acquire the search history of the target account, and determine the target conversation attribute as a conversation attribute related to the search history; The emotion type of the initial dialogue text is obtained, and the target dialogue attribute is determined to be a dialogue attribute that is the same as the emotion type of the initial dialogue text.
11. A device for generating a dialogue text, characterized in that: include: An acquisition module, used to acquire an initial dialogue text and a target dialogue attribute in a target dialogue scene, wherein the target dialogue attribute is used to indicate a dialogue style of the target dialogue scene; A first processing module, configured to input the initial dialogue text into a target dialogue model for undisturbed forward transmission, and output undisturbed dialogue text, wherein the undisturbed dialogue text includes word segments that do not conform to the target dialogue attribute, and the undisturbed dialogue text is composed of word segments output by the target dialogue model, the target dialogue model is configured to determine output word segments according to input word segments and a history information matrix step by step according to time steps, and determine corresponding dialogue text according to the output word segments, the target dialogue model includes a plurality of attention modules, and the history information matrix includes key-value pairs corresponding to each of the plurality of attention modules; An updating module, used to input the representation vectors corresponding to each segmentation in the unperturbed dialogue text into the target attribute classifier, respectively, determine the loss parameters of the target attribute classifier, and update the historical information matrix using the loss parameters, wherein the loss parameters are used to indicate the degree to which the segmentation corresponding to the representation vector conforms to the target dialogue attribute; The second processing module is used to input the initial dialogue text into the target dialogue model for a perturbed forward pass, and output a perturbed dialogue text, wherein the perturbed dialogue text represents the dialogue text output by the target dialogue model after the historical information matrix is updated using the loss parameter, and each word segment in the perturbed dialogue text conforms to the target dialogue attribute.
12. The device according to claim 11, characterized in that The device is also used for: Input the initial dialogue text into the target dialogue model to generate t first representation vectors, wherein the target dialogue model includes an i-layer attention module, the first representation vector represents the output result of the i-layer attention module, the target dialogue model sequentially outputs t participles at t time steps in an iterative loop manner, the j+1th participle is determined by inputting the j-th participle output by the target dialogue model at the j-th time step and the j-th historical information matrix into the target dialogue model at the j+1th time step, the j-th historical information matrix is used to represent i key-value pairs corresponding to the j-th time step, the i key-value pairs correspond one-to-one to the i-layer attention module, the t first representation vectors are used to determine the undisturbed dialogue text composed of the t participles, i is an integer greater than or equal to 2, t is an integer greater than or equal to j+1, and j is an integer greater than or equal to 1; Input the t first representation vectors into the target attribute classifier respectively, determine t classification results, and update the target history information matrix, wherein the target history information matrix represents the history information matrix corresponding to the target segmentation output by the target dialogue model after being input into the target dialogue model, and the target segmentation includes the segmentation determined by the t classification results that does not conform to the target dialogue attribute; The initial dialogue text is input into the target dialogue model, t second representation vectors are determined using the updated target history information matrix, and the disturbed dialogue text is generated, wherein the second representation vector is generated in the same manner as the first representation vector.
13. The device according to claim 12, characterized in that The device is used to input the t first representation vectors into the pre-trained target attribute classifiers respectively, determine t classification results, and update the target history information matrix in the following manner: Inputting the t first representation vectors into the target attribute classifier respectively to determine t loss parameters, wherein the t classification results correspond to the t loss parameters one by one; Determine a target loss parameter corresponding to the nth time step from the t loss parameters, wherein the target loss parameter represents a loss parameter that does not meet a preset loss condition, and n is a positive integer less than or equal to t; The target history information matrix is updated according to the target loss parameter.
14. The device according to claim 13, characterized in that The device is used to update the target history information matrix according to the target loss parameter in the following manner: Get the initial tensors that allow training; Determining the gradient of the target attribute classifier according to the target loss parameter, and using the gradient to train the initial tensor until a target tensor is obtained, wherein the target tensor represents an updated value of the target history information matrix; The sum of the target history information matrix and the target tensor is determined as the updated target history information matrix.
15. The device according to claim 14, characterized in that The device is used to determine the gradient of the target attribute classifier according to the target loss parameter in the following manner, and train the initial tensor using the gradient to obtain the target tensor: Obtaining a predetermined training step length, wherein the value of the training step length is positively correlated with the amplitude adjusted by the initial tensor in round-by-round training; The initial tensor is trained round by round according to the training step length, and the target history information matrix is updated using the trained tensor in each round, and the loss parameter and gradient corresponding to the nth time step in the round are recalculated after each update of the target history information matrix, until the loss parameter corresponding to the nth time step meets the preset loss condition, and the trained tensor in the round is determined as the target tensor, wherein the trained tensor in each round is determined by the tensor before each round of training, the gradient corresponding to each round, and the update step length.
16. The device according to claim 13, characterized in that The device is used to input the initial dialogue text into the target dialogue model for a disturbed forward pass and output the disturbed dialogue text in the following manner: Inputting the initial dialogue text into the target dialogue model to generate the t second representation vectors, wherein the historical information matrix corresponding to the n-th time step is updated; Inputting the t second representation vectors into the target attribute classifier respectively, and re-determining t classification results, wherein the re-determined t classification results all indicate that the corresponding segmented words meet the target dialogue attribute; A sampling operation is performed on the t second representation vectors to obtain the disturbed dialogue text.
17. The device according to claim 12, characterized in that The device is also used for: Performing normalization operations on the t first characterization vectors and the t second characterization vectors respectively to obtain a first probability distribution and a second probability distribution; Determining a target divergence value according to the first probability distribution and the second probability distribution, wherein the target divergence value is used to measure the difference between the first probability distribution and the second probability distribution; The target history information matrix is updated with the goal of minimizing the value of the target divergence value.
18. The device according to claim 11, characterized in that The device is also used for: Obtain unlabeled dialogue corpus data; Preprocessing the dialogue corpus data to obtain a target dialogue corpus, wherein the target dialogue corpus represents a dialogue corpus between two objects; The target dialogue corpus is used to pre-train an initial dialogue model to obtain the target dialogue model, wherein the initial dialogue model and the target dialogue model use a bidirectional attention mechanism on the input text and a unidirectional attention mechanism on the output text.
19. The device according to claim 11, characterized in that The device is used to obtain the initial dialogue text and target dialogue attributes in the target dialogue scene in the following manner: Acquire account data of a target account, wherein the target account represents an account participating in the target conversation scenario; The target conversation attribute is determined based on the account data.
20. The device according to claim 19, characterized in that The device is used to determine the target conversation attribute according to the account data in at least one of the following ways: Acquiring interaction data of the target account, and determining the target conversation attribute as a conversation attribute related to the interaction data; Acquire the search history of the target account, and determine the target conversation attribute as a conversation attribute related to the search history; The emotion type of the initial dialogue text is obtained, and the target dialogue attribute is determined to be a dialogue attribute that is the same as the emotion type of the initial dialogue text.
21. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored computer program, wherein the computer program can be executed by an electronic device to perform the method described in any one of claims 1 to 10.
22. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method described in any one of claims 1 to 10 are implemented.
23. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to execute the method according to any one of claims 1 to 10 through the computer program.
Citation Information
Patent Citations
Method for controlling dialogue generation based on large-scale general pre-training model
CN113420129A
Data management system based on big data and intelligent security and protection
CN116798176A
Method for determining the presence of a wanted signal component
EP2148325A1
