A method, related apparatus, equipment, and storage medium for generating counterfactual samples.

CN116776863BActive Publication Date: 2026-09-01TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210224101.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-07
Publication Date
2026-09-01
Estimated Expiration
2042-03-07

AI Technical Summary

Technical Problem

[0004]然而,发明人发现现有方案中至少存在如下问题,反事实样本的生成依赖于人工标注和编写

Benefits of technology

[0086]本申请实施例中,提供了一种反事实样本的生成方法,首先,通过文本分类模型获取原始文本样本的类别概率分布。然后,可根据类别概率分布,计算原始文本样本中每个文本单元所对应的关联分值。于是,根据每个文本单元所对应的关联分值,从原始文本样本中确定N个因果片段,并对N个因果片段中的M个因果片段进行遮盖处理,得到目标文本样本。基于此,通过文本生成模型获取目标文本样本的M个目标片段,最后,可将原始文本样本中的M个因果片段替换为M个目标片段,得到反事实样本。通过上述方式,利用类别概率分布与原始文本样本中每个文本单元之间的关联分值,可识别出原始文本样本中影响类别预测结果的因果片段。再通过文本生成模型对这些因果片段重新生成目标片段,将因果片段替换为目标片段之后,即可获得反事实样本。由此,能够在无需人工介入的情况下,自动生成反事实样本,从而降低数据增广的成本。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116776863B_ABST
    Figure CN116776863B_ABST
Patent Text Reader

Abstract

This application discloses a method for generating counterfactual samples, applicable to fields including but not limited to human-computer interaction. The method includes: obtaining a category probability distribution based on an original text sample using a text classification model; determining the association score of each text unit in the original text sample based on the category probability distribution; identifying N causal segments from the original text sample based on the association score of each text unit; masking M of the N causal segments to obtain a target text sample; obtaining M target segments based on the target text sample using a text generation model; and replacing the M causal segments in the original text sample with the M target segments to obtain a counterfactual sample. This application also provides an apparatus, device, and storage medium. This application can automatically generate counterfactual samples without human intervention, thereby reducing the cost of data augmentation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of artificial intelligence technology, and in particular to a method, apparatus, device and storage medium for generating counterfactual samples. Background Technology

[0002] A counterfactual sample is a new sample whose semantics are reversed by slightly altering the preconditions of the original sample, thus resulting in a change in the corresponding label (i.e., a counterfactual sample). For example, if the original sample is "children are playing football on the grass," a counterfactual sample could be "children are playing football on the field."

[0003] Counterfactual sample generation methods are often used to evaluate model robustness. Currently, it is usually necessary to manually label causal segments (rationales) and then write them to generate counterfactual samples. For example, the original sample is "children are playing football on the grass," and the labeled causal segments are "grass" and "playing football." Based on this, "grass" can be replaced with "field" and "playing football" can be replaced with "playing football," thus obtaining a counterfactual sample.

[0004] However, the inventors discovered that existing solutions have at least the following problems: the generation of counterfactual samples relies on manual annotation and writing. That is, it requires manual annotation of causal segments in the original samples that affect the label prediction results, thus leading to high costs in obtaining counterfactual samples. Summary of the Invention

[0005] This application provides a method, related apparatus, device, and storage medium for generating counterfactual samples. This application enables the automatic generation of counterfactual samples without human intervention, thereby reducing the cost of data augmentation.

[0006] In view of this, this application provides a method for generating counterfactual samples, comprising:

[0007] Based on the original text sample, the category probability distribution is obtained through a text classification model. The original text sample includes T text units, where T is an integer greater than 1.

[0008] Based on the category probability distribution, determine the association score corresponding to each text unit in the original text sample;

[0009] Based on the correlation score corresponding to each text unit, N causal segments are determined from the original text sample, where each causal segment includes at least one text unit, and N is an integer greater than or equal to 1;

[0010] Masking is performed on M causal segments out of N causal segments to obtain the target text sample, where M is an integer greater than or equal to 1 and less than or equal to N;

[0011] Based on the target text sample, M target fragments are obtained through a text generation model, where the target fragments correspond to the causal fragments.

[0012] By replacing M causal segments in the original text sample with M target segments, a counterfactual sample is obtained.

[0013] Another aspect of this application provides a sample generation apparatus, comprising:

[0014] The acquisition module is used to obtain the category probability distribution based on the original text sample through a text classification model. The original text sample includes T text units, where T is an integer greater than 1.

[0015] The determination module is used to determine the association score corresponding to each text unit in the original text sample based on the category probability distribution;

[0016] The determination module is also used to determine N causal segments from the original text sample based on the correlation score corresponding to each text unit, wherein each causal segment includes at least one text unit, and N is an integer greater than or equal to 1;

[0017] The processing module is used to mask M causal segments out of N causal segments to obtain the target text sample, where M is an integer greater than or equal to 1 and less than or equal to N;

[0018] The acquisition module is also used to acquire M target fragments based on the target text sample through a text generation model, wherein the target fragments correspond to the causal fragments;

[0019] The processing module is also used to replace M causal segments in the original text sample with M target segments to obtain counterfactual samples.

[0020] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0021] The determination module is also used to determine the probability value corresponding to the true classification label based on the original text sample after obtaining the category probability distribution through the text classification model. The original text sample has been labeled as the true classification label, and each probability value in the category probability distribution corresponds to a classification label.

[0022] The determination module is also used to perform the step of determining the association score corresponding to each text unit in the original text sample according to the category probability distribution if the probability value corresponding to the true classification label is greater than or equal to the first probability threshold.

[0023] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0024] The acquisition module is specifically used to segment the original text sample into text units to obtain T text units.

[0025] Generate T embedding vectors from T text units, where each embedding vector corresponds to a text unit.

[0026] Based on T embedding vectors, the category probability distribution is obtained through a text classification model.

[0027] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0028] The determination module is specifically used to calculate the classification loss value based on the category probability distribution and the true classification label corresponding to the original text sample through the classification loss function;

[0029] The first partial derivative is calculated based on the classification loss value;

[0030] For each text unit in the original text sample, the second partial derivative is calculated based on the embedding vector corresponding to the text unit;

[0031] For each text unit in the original text sample, the association score corresponding to the text unit is determined based on the first partial derivative and the second partial derivative corresponding to the text unit.

[0032] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0033] The acquisition module is specifically used to obtain the average embedding vector based on T embedding vectors through the hidden layer in the text classification model.

[0034] Based on the average embedding vector, the category probability distribution is obtained through the output layer of the text classification model.

[0035] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0036] The acquisition module is specifically used to generate a two-dimensional matrix based on T embedding vectors;

[0037] Based on a two-dimensional matrix, at least two feature vectors are obtained through the convolutional layer in a text classification model.

[0038] The target feature vector is obtained through the pooling layer in the text classification model based on at least two feature vectors.

[0039] Based on the target feature vector, the category probability distribution is obtained through the fully connected layer in the text classification model.

[0040] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0041] The acquisition module is specifically used to obtain the hidden state corresponding to at least one time step based on T embedding vectors through the bidirectional coding network in the text classification model.

[0042] Based on the hidden state corresponding to at least one time step, the category probability distribution is obtained through the fully connected layer in the text classification model.

[0043] In one possible design, in another implementation of another aspect of the embodiments of this application, the T text units include a first text unit and a second text unit, wherein the first text unit and the second text unit are adjacent text units;

[0044] The determination module is specifically used to determine the first text unit as a text unit in the candidate causal segment if the correlation score corresponding to the first text unit is greater than or equal to the correlation score threshold.

[0045] If the correlation score corresponding to the second text unit is greater than or equal to the correlation score threshold, then the second text unit will be regarded as another text unit in the candidate causal segment.

[0046] If the correlation score corresponding to the second text unit is less than the correlation score threshold, then the candidate causal segment will be taken as one of the N causal segments.

[0047] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0048] The acquisition module is specifically used to obtain encoded feature vectors based on target text samples through the encoder in the text generation model.

[0049] Based on the encoded feature vector, the probability value corresponding to each preset text unit in the dictionary at the first time step is obtained through the decoder in the text generation model;

[0050] After sorting the probability values ​​of each preset text unit in descending order at the first time step, the first P preset text units are selected as P first candidate text units, where P is an integer greater than or equal to 1.

[0051] Based on the encoded feature vector and P first candidate text units, the probability value corresponding to each preset text unit in the dictionary at the second time step is obtained through the decoder in the text generation model. The second time step is the next time step adjacent to the first time step.

[0052] After sorting the probability values ​​of each preset text unit in descending order in the second time step, the first P preset text units are taken as P second candidate text units.

[0053] Based on P first candidate text units and P second candidate text units, generate M target fragments.

[0054] In one possible design, in another implementation of another aspect of the embodiments of this application, the sample generation device further includes a training module;

[0055] The acquisition module is also used to acquire training text samples, wherein the training text samples include Q text units, where Q is an integer greater than 1;

[0056] The acquisition module is also used to obtain training feature vectors based on training text samples through the encoder in the text generation model;

[0057] The acquisition module is also used to obtain the probability value of each preset text unit in the dictionary at the current time step based on the trained feature vector through the decoder in the text generation model;

[0058] The acquisition module is also used to acquire the probability value corresponding to the target text unit based on the probability value corresponding to each preset text unit at the current time step;

[0059] The training module is used to update the model parameters of the text generation model based on the probability values ​​corresponding to the target text units.

[0060] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0061] The acquisition module is specifically used to acquire the target category label;

[0062] Based on the target text sample and the target classification label, the encoded feature vector is obtained through the encoder in the text generation model;

[0063] Based on the encoded feature vector, the probability value corresponding to each preset text unit in the dictionary at the first time step is obtained through the decoder in the text generation model;

[0064] After sorting the probability values ​​of each preset text unit in descending order at the first time step, the first P preset text units are selected as P first candidate text units, where P is an integer greater than or equal to 1.

[0065] Based on the encoded feature vector and P first candidate text units, the probability value corresponding to each preset text unit in the dictionary at the second time step is obtained through the decoder in the text generation model. The second time step is the next time step adjacent to the first time step.

[0066] After sorting the probability values ​​of each preset text unit in descending order in the second time step, the first P preset text units are taken as P second candidate text units.

[0067] Based on P first candidate text units and P second candidate text units, generate M target fragments for the target classification label.

[0068] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0069] The acquisition module is also used to acquire training text samples, wherein the training text samples have been labeled with the true classification labels and the training text samples include Q text units, where Q is an integer greater than 1;

[0070] The acquisition module is also used to obtain training feature vectors based on training text samples and real classification labels through the encoder in the text generation model;

[0071] The acquisition module is also used to obtain the probability value of each preset text unit in the dictionary at the current time step based on the trained feature vector through the decoder in the text generation model;

[0072] The acquisition module is also used to acquire the probability value corresponding to the target text unit based on the probability value corresponding to each preset text unit at the current time step;

[0073] The training module is also used to update the model parameters of the text generation model based on the probability value corresponding to the target text unit, the true classification label, and the remaining classification labels, where the remaining classification labels are other classification labels besides the true classification labels.

[0074] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0075] The processing module is specifically used to replace M causal segments in the original text sample with M target segments to obtain candidate counterfactual samples;

[0076] Based on candidate counterfactual samples, the probability distribution of candidate categories of candidate counterfactual samples is obtained through a text classification model;

[0077] Based on the probability distribution of candidate categories, determine the candidate probability value of the candidate counterfactual sample relative to the target classification label;

[0078] Candidate counterfactual samples whose candidate probability value is greater than or equal to the second probability threshold are considered counterfactual samples.

[0079] In one possible design, in another implementation of another aspect of the embodiments of this application,

[0080] The acquisition module is specifically used to display the original text sample and at least one optional category control on the sample generation interface, wherein each optional category control corresponds to a category label;

[0081] In response to a selection operation on a target category control, obtain the target category label, wherein the target category control belongs to at least one selectable category control, and the target category label is the category label corresponding to the target category control.

[0082] In another aspect, this application provides a computer device including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the methods described above.

[0083] Another aspect of this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the methods described above.

[0084] Another aspect of this application provides a computer program product, including a computer program that, when executed by a processor, implements the methods described above.

[0085] As can be seen from the above technical solutions, the embodiments of this application have the following advantages:

[0086] This application provides a method for generating counterfactual samples. First, the category probability distribution of the original text sample is obtained through a text classification model. Then, based on the category probability distribution, the association score corresponding to each text unit in the original text sample is calculated. Then, based on the association score corresponding to each text unit, N causal segments are determined from the original text sample, and M of these N causal segments are masked to obtain a target text sample. Based on this, M target segments of the target text sample are obtained through a text generation model. Finally, the M causal segments in the original text sample are replaced with the M target segments to obtain the counterfactual sample. By utilizing the association score between the category probability distribution and each text unit in the original text sample, causal segments affecting the category prediction results in the original text sample can be identified. Then, the target segments are regenerated using the text generation model, and after replacing the causal segments with the target segments, the counterfactual sample is obtained. Therefore, counterfactual samples can be automatically generated without manual intervention, thereby reducing the cost of data augmentation. Attached Figure Description

[0087] Figure 1 This is a schematic diagram of the framework of the automatic classification system in the embodiments of this application;

[0088] Figure 2 This is a schematic diagram of the architecture of the sample generation system in an embodiment of this application;

[0089] Figure 3 This is a flowchart illustrating a counterfactual sample generation method in an embodiment of this application.

[0090] Figure 4 This is a schematic diagram illustrating the generation of counterfactual samples in an embodiment of this application;

[0091] Figure 5 This is a schematic diagram of the structure of a text classification model in an embodiment of this application;

[0092] Figure 6 This is another structural diagram of the text classification model in the embodiments of this application;

[0093] Figure 7 This is another structural diagram of the text classification model in the embodiments of this application;

[0094] Figure 8 This is a schematic diagram of the structure of the text generation model in an embodiment of this application;

[0095] Figure 9 This is a schematic diagram of a text unit prediction based on a greedy search algorithm in an embodiment of this application;

[0096] Figure 10This is a schematic diagram of a text unit prediction based on a cluster search algorithm in an embodiment of this application;

[0097] Figure 11 This is another structural diagram of the text generation model in the embodiments of this application;

[0098] Figure 12 This is a schematic diagram illustrating the generation of counterfactual samples based on target classification labels in an embodiment of this application;

[0099] Figure 13 This is a schematic diagram of an interface change based on interactive operation in an embodiment of this application;

[0100] Figure 14 This is a schematic diagram of a sample generation device in an embodiment of this application;

[0101] Figure 15 This is a schematic diagram of the structure of a terminal in an embodiment of this application;

[0102] Figure 16 This is a schematic diagram of the server structure in an embodiment of this application. Detailed Implementation

[0103] This application provides a method, related apparatus, device, and storage medium for generating counterfactual samples. This application enables the automatic generation of counterfactual samples without human intervention, thereby reducing the cost of data augmentation.

[0104] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a particular order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented, for example, in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “corresponding to,” and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0105] With the gradual development of Artificial Intelligence (AI) technology, research and applications have been carried out in multiple fields, and the data scale requirements of AI models are also gradually increasing. In classification tasks based on Natural Language Processing (NLP), if the amount of training data for different categories differs significantly, the model may overfit, severely affecting the accuracy of predictions. Supervised learning often requires a large amount of labeled data. When the data requirement reaches hundreds of thousands, millions, or even more, the high cost of manually labeling data deters many. Therefore, data augmentation techniques can be used to expand the data scale, utilizing limited labeled data to obtain more training data, reducing overfitting in the network, and training a model with stronger generalization ability. Thus, data augmentation not only reduces labeling costs but also improves model robustness and generalization, preventing biased learning due to insufficient data.

[0106] AI (Artificial Intelligence) is a 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 that knowledge to achieve optimal results. In other words, AI is a comprehensive technology within computer science that attempts to understand the essence of intelligence and produce a new kind of intelligent machine that can react in a way similar to human intelligence. AI studies the design principles and implementation methods of various intelligent machines, enabling them to have perception, reasoning, and decision-making capabilities. AI technology is a comprehensive discipline involving a wide range of fields, encompassing both hardware and software technologies. Basic AI technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing technology, operating / interactive systems, and mechatronics. AI software technologies mainly include computer vision, speech processing, NLP (Natural Language Processing), and Machine Learning (ML) / deep learning, autonomous driving, and intelligent transportation.

[0107] Natural Language Processing (NLP) is an important area within computer science and AI. It studies the theories and methods for enabling effective communication between humans and computers using natural language. NLP is a science that integrates linguistics, computer science, and mathematics. Therefore, research in this field involves natural language—the language people use in daily life—and thus it has a close connection with linguistic research. NLP technologies typically include text processing, semantic understanding, machine translation, question answering, and knowledge graphs.

[0108] This application utilizes automatically generated counterfactual samples as augmented data. Counterfactual samples are high-quality data augmentation samples that can combat model bias. Therefore, using counterfactual samples for model training can improve the robustness and generalization of the model. It can be applied to systems that rely on text classification models, including but not limited to search engines, recommendation systems, and dialogue systems. For example, taking a customer service scenario, to reduce the manpower required for after-sales service, an intelligent chatbot can be used to answer customer questions. The user's intent category is primarily determined by their question. Training the text classification model using the augmented data provided in this application can improve the accuracy of question category recognition to a certain extent.

[0109] A system framework for automatic text classification based on a text classification model, such as: Figure 1 As shown. Please refer to [the original text]. Figure 1 , Figure 1 This is a schematic diagram of the framework of the automatic classification system in this application embodiment. As shown in the figure, the system is mainly divided into a system input layer, a system algorithm encapsulation layer, and an output layer. The input content includes, but is not limited to, text titles, text subtitles, text body, and text obtained after speech conversion. The system algorithm encapsulation layer encapsulates the classifier for classifying the text and mines more samples (i.e., including counterfactual samples) to achieve iterative updates of the model. The system output is the category to which the input text belongs.

[0110] To obtain more Counterfactual Augmented Data (CAD), this application proposes a method for generating counterfactual samples, which is applied to... Figure 2The sample generation system shown in the figure includes at least one of a server and a terminal. The client is deployed on the terminal. The client can run on the terminal via a browser or as a standalone application (APP). The specific form of the client is not limited here. The server involved in this application can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDNs), and big data and artificial intelligence platforms. The terminal can be a smartphone, tablet, laptop, PDA, personal computer, smart TV, smartwatch, in-vehicle device, wearable device, etc., but is not limited to these. The terminal and server can be directly or indirectly connected via wired or wireless communication, which is not limited here. The number of servers and terminals is also not limited. The solution provided in this application can be completed independently by the terminal, independently by the server, or jointly by the terminal and the server. This application does not impose any specific limitations on this.

[0111] Taking a sample generation system including a terminal as an example, the system first retrieves original text samples from the database, then calls the text classification model and text generation model stored on the terminal to process the original text samples, obtaining one or more counterfactual samples. Finally, these counterfactual samples can be stored in the database.

[0112] Given that this application involves some technical terms, explanations will be provided below for ease of understanding.

[0113] (1) Counterfactual sample: refers to a new sample whose semantics are reversed by slightly changing the premise of the original sample, resulting in a change in the corresponding label. For example, the original text sample is "Children are playing football on the grass", and its corresponding counterfactual sample could be "Children are playing football in the field".

[0114] (2)CAD: Add counterfactual samples to the dataset to obtain a more generalizable dataset.

[0115] (3) Rationale: This refers to the key segment in a sample that determines the label prediction result. A sample may include one or more causal segments. Changing the causal segment usually turns the sample into a counterfactual sample. For example, if the original text sample is "This phone runs smoothly", the causal segment that determines that it can be predicted as "positive sentiment" is "smooth". If it is replaced with "lag", the generated counterfactual sample is "This phone runs laggy".

[0116] Based on the above introduction, the method for generating counterfactual samples in this application will be described below. Please refer to [link / reference]. Figure 3 The counterfactual sample generation method in this application embodiment can be executed by a computer device, which can be a terminal or a server, including:

[0117] 110. Based on the original text sample, obtain the category probability distribution through a text classification model, where the original text sample includes T text units, and T is an integer greater than 1;

[0118] In one or more embodiments, an original text sample is obtained, comprising T text units, which may be characters, words, or symbols. It is understood that the original text samples involved in this application support language types including, but not limited to, Chinese and English.

[0119] For example, taking the original text sample "Children are playing football on the grass" as an example, its T text units include "child", "son", "men", "on", "grass", "ground", "on", "kick" and "ball".

[0120] For example, taking the original text sample "thank you for inviting me to your party last week" as an example, its T text units include "thank", "you", "for", "inviting", "me", "to", "your", "party", "last", and "week".

[0121] Based on this, the original text sample can be used as input to the text classification model, and the text classification model can output the probability distribution of the category corresponding to the original text sample.

[0122] 120. Based on the category probability distribution, determine the association score corresponding to each text unit in the original text sample;

[0123] In one or more embodiments, the classification label of the original text sample can be predicted based on the category probability distribution, and the association score corresponding to each text unit in the original text sample can also be calculated.

[0124] Specifically, in one scenario, based on the predicted classification label of the original text sample, the semantic similarity between each text unit and the predicted classification label is determined, and this semantic similarity is directly used as the association score of the text unit. In another scenario, N text segments (n-grams) in the original text sample can be enumerated, and the semantic similarity between them and the predicted classification label can be calculated. The association score of the text unit is then determined based on this semantic similarity. Finally, in yet another scenario, the gradient can be calculated based on the class probability distribution and the embedding vector of the text unit, and the 2-norm of the gradient can be used as the association score of the text unit.

[0125] 130. Based on the correlation score corresponding to each text unit, determine N causal segments from the original text sample, where each causal segment includes at least one text unit, and N is an integer greater than or equal to 1;

[0126] In one or more embodiments, based on the correlation score corresponding to each text unit, text units with correlation scores greater than or equal to a correlation score threshold are selected as candidate text units. Adjacent candidate text units can be considered as the same causal segment. Based on this, N causal segments can be obtained from the original text sample, and each causal segment includes at least one text unit.

[0127] 140. Mask M of N causal segments to obtain the target text sample, where M is an integer greater than or equal to 1 and less than or equal to N;

[0128] In one or more embodiments, for the original text sample, all N causal segments can be masked (i.e., M equals N), or only some causal segments can be masked (i.e., M is less than N), thereby obtaining the target text sample.

[0129] Specifically, assuming the original text sample is "Children are playing football on the grass," for example, N causal segments include "children" and "playing football." Based on this, if both causal segments are masked, the resulting target text sample is represented as " <x>On the grass <y>If a causal segment is masked, the resulting target text sample is represented as "". <x>"Playing football on the grass." If another causal segment is masked, the resulting target text sample is represented as "Children are playing football on the grass." <y>".

[0130] 150. Based on the target text sample, obtain M target segments through a text generation model, where the target segments correspond to the causal segments;

[0131] In one or more embodiments, the target text sample is used as input to the text generation model, and M target fragments can be obtained through the text generation model. Each target fragment corresponds to a causal fragment, and the target fragment and the causal fragment have the same position in the sample.

[0132] Specifically, either greedy search or beam search can be used to output the probability value of each preset text unit in the dictionary at different time steps. Based on the probability values, M target segments can be obtained. Greedy search selects the preset text unit with the highest probability at each time step. Beam search records the top few paths with the highest probabilities at each time step, and multiple paths are searched simultaneously in the next time step. Therefore, it avoids missing hidden high-probability words.

[0133] 160. Replace M causal segments in the original text sample with M target segments to obtain a counterfactual sample.

[0134] In one or more embodiments, M target fragments are used to replace M causal fragments in the original text sample to obtain a counterfactual sample.

[0135] Specifically, for ease of understanding, please refer to Figure 4 , Figure 4 This is a schematic diagram illustrating the generation of counterfactual samples in an embodiment of this application. As shown in the figure, suppose the original text sample is "This phone is reasonably priced, you won't regret buying it," where N causal segments include "reasonably priced" and "you won't regret buying it." Based on this, both of these causal segments can be masked, resulting in the target text sample "This phone is reasonably priced." <x>Buy it <y>Furthermore, assuming that the M target segments include "a rip-off" and "regret", then by replacing the M causal segments with the M target segments, the resulting counterfactual sample is "This phone is a rip-off, I regret buying it".

[0136] This application provides a method for generating counterfactual samples. By utilizing the correlation scores between the category probability distribution and each text unit in the original text sample, causal segments affecting the category prediction results can be identified. Then, a text generation model is used to regenerate target segments from these causal segments. After replacing the causal segments with the target segments, counterfactual samples are obtained. Therefore, counterfactual samples can be automatically generated without manual intervention, thereby reducing the cost of data augmentation.

[0137] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, after obtaining the category probability distribution based on the original text sample through a text classification model, it may further include:

[0138] Based on the category probability distribution, determine the probability value corresponding to the true classification label. The original text sample has been labeled as the true classification label, and each probability value in the category probability distribution corresponds to a classification label.

[0139] If the probability value corresponding to the true classification label is greater than or equal to the first probability threshold, then the step of determining the association score corresponding to each text unit in the original text sample based on the category probability distribution is executed.

[0140] In one or more embodiments, a method for confidence filtering of original text samples is described. As can be seen from the foregoing embodiments, for a given original text sample, its corresponding true classification label can also be obtained. Since the original text sample can directly use already labeled samples, the true classification label of the original text sample is thus obtained.

[0141] Specifically, taking three categories as an example: "Sports," "Entertainment," and "Technology," the category probability distribution is represented as a 3-dimensional vector. Assume the true category label of the original text sample is "Sports," and that the category probability distribution of the original text sample is (0.8, 0.1, 0.1). The probability value for "Sports" is 0.8, for "Entertainment" is 0.1, and for "Technology" is 0.1. It is clear that the probability value corresponding to the true category label (i.e., "Sports") is 0.8. Taking a first probability threshold of 0.7 as an example, the probability value corresponding to the true category label (i.e., "Sports") is greater than the first probability threshold (i.e., 0.7). Therefore, the original text sample is determined to be a high-confidence sample. Thus, the original text sample can be further processed.

[0142] Secondly, this application provides a method for confidence filtering of original text samples. This method allows for the selection of high-confidence original text samples for subsequent processing, thereby reducing the impact of noise on the generated results and improving the sample generation effect.

[0143] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, the category probability distribution is obtained based on the original text sample through a text classification model, which may specifically include:

[0144] The original text sample is segmented into text units to obtain T text units;

[0145] Generate T embedding vectors from T text units, where each embedding vector corresponds to a text unit.

[0146] Based on T embedding vectors, the category probability distribution is obtained through a text classification model.

[0147] In one or more embodiments, a method for obtaining the category probability distribution is described. As can be seen from the foregoing embodiments, the text unit can be a character, word, or symbol, etc. Taking the original text sample as Chinese text, and the text unit as "character" as an example, the original text sample can be directly segmented into text units to obtain T text units.

[0148] Specifically, each text unit needs to be converted into a fixed-length embedding vector. One approach is to use a trained embedding layer to encode the text units, thereby obtaining the embedding vector corresponding to each text unit. These T embedding vectors are then used as input to a text classification model, which outputs the corresponding class probability distribution.

[0149] It is understood that the embedding layer is used as the front end of the model. The embedding layer involved in this application can be part of the text classification model or a part independent of the text classification model. The embedding vector involved in this application can be 300-dimensional or 738-dimensional, etc., without limitation here.

[0150] Secondly, this application provides a method for obtaining category probability distribution. By using this method, each text unit in the original text sample is embedded, which serves as the basis for predicting the category to which the original text sample belongs, thereby increasing the feasibility and operability of the solution.

[0151] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, the association score corresponding to each text unit in the original text sample is determined according to the category probability distribution, which may specifically include:

[0152] Based on the category probability distribution and the true classification labels corresponding to the original text samples, the classification loss value is calculated using the classification loss function;

[0153] The first partial derivative is calculated based on the classification loss value;

[0154] For each text unit in the original text sample, the second partial derivative is calculated based on the embedding vector corresponding to the text unit;

[0155] For each text unit in the original text sample, the association score corresponding to the text unit is determined based on the first partial derivative and the second partial derivative corresponding to the text unit.

[0156] In one or more embodiments, a method for calculating the association score corresponding to a text unit is described. As can be seen from the foregoing embodiments, the gradient magnitude of each text unit in the input original text sample can be used as a measure of the strength of causal association based on the classification loss function of the classifier, that is, the corresponding association score can be obtained.

[0157] Specifically, taking a given original text sample as an example, the embedding vector of the i-th text unit in the original text sample is represented as e. i Taking the cross-entropy loss function as the classification loss function as an example, the classification loss value can be calculated in the following way:

[0158] L = -∑p(x)logq(x);

[0159] Where L represents the classification loss value. x represents the original text sample. p(x) represents the probability distribution obtained based on the true classification label, for example, (1,0,0). q(x) represents the class probability distribution, for example, (0.8,0.1,0.1).

[0160] Based on the classification loss value and the embedding vector of the i-th text unit, the association score is calculated as follows:

[0161]

[0162] Among them, s i This represents the association score of the i-th text unit. e represents the first partial derivative. i This represents the embedding vector of the i-th text unit. This represents the second partial derivative. Let || represent the gradient corresponding to the i-th text unit. || ||2 represents the calculation of the 2-norm.

[0163] Furthermore, this application provides a method for calculating the association score corresponding to a text unit. Using this method, the gradient is calculated using the embedding vector of each text unit and the predicted category probability distribution. The 2-norm of the gradient is then used as the association score of the corresponding text unit, thus providing a feasible basis for the implementation of the scheme and improving its feasibility and operability.

[0164] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, the category probability distribution is obtained through a text classification model based on T embedding vectors, which may specifically include:

[0165] Based on T embedding vectors, the average embedding vector is obtained through the hidden layer in the text classification model.

[0166] Based on the average embedding vector, the category probability distribution is obtained through the output layer of the text classification model.

[0167] In one or more embodiments, a method for text classification based on the fastText model is introduced. As can be seen from the foregoing embodiments, after obtaining the embedding vector corresponding to each text unit, the corresponding category probability distribution can be output by the text classification model. The text classification model can be the fastText model.

[0168] Specifically, for ease of understanding, please refer to Figure 5 , Figure 5 This is a schematic diagram of the text classification model in an embodiment of this application. As shown in the figure, the fastText model includes an input layer, a hidden layer, and an output layer. The original text sample (x) can be divided into T text units (i.e., x1, x2, ..., x...). T-1 ,x T Based on this, T embedding vectors are obtained (i.e., e1, e2, ..., e...). T-1 ,e T The input layer receives T embedding vectors. Based on this, the hidden layer sums the T embedding vectors from the input layer and then averages them to obtain the average embedding vector. The class probability distribution is then calculated using the average embedding vector from the output layer. The output layer can be either a normalized exponential function (softmax) layer or a hierarchical normalized exponential function (hiererachical softmax) layer.

[0169] Furthermore, this application provides a method for text classification based on the fastText model. Through this method, since the fastText model is suitable for tasks with a large number of samples and numerous class labels, it can achieve excellent prediction results. At the same time, the fastText model has high training efficiency.

[0170] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, the category probability distribution is obtained through a text classification model based on T embedding vectors, which may specifically include:

[0171] Generate a two-dimensional matrix based on T embedding vectors;

[0172] Based on a two-dimensional matrix, at least two feature vectors are obtained through the convolutional layer in a text classification model.

[0173] The target feature vector is obtained through the pooling layer in the text classification model based on at least two feature vectors.

[0174] Based on the target feature vector, the category probability distribution is obtained through the fully connected layer in the text classification model.

[0175] In one or more embodiments, a method for text classification based on the TextCNN model is introduced. As can be seen from the foregoing embodiments, after obtaining the embedding vector corresponding to each text unit, the corresponding category probability distribution can be output by the text classification model. The text classification model can be a Text Convolutional Neural Network (TextCNN) model.

[0176] Specifically, for ease of understanding, please refer to Figure 6 , Figure 6 This is another structural diagram of the text classification model in this application embodiment. As shown in the figure, the TextCNN model includes a convolutional layer, a pooling layer, and a fully connected layer. Assume the original text sample (x) can be divided into 6 text units (i.e., x1, x2, x3, x4, x5, x6), resulting in 6 embedding vectors (i.e., e1, e2, e3, e4, e5, e6). Assume the embedding vector of each text unit is 5-dimensional, thus obtaining a 6×5 two-dimensional matrix. Therefore, features are extracted through the convolutional layer, yielding at least two feature vectors. Figure 6 Let's take the extraction of 4 feature vectors as an example.

[0177] It is understandable that a convolutional layer is a feature extraction layer. Hyperparameters can be set to specify how many convolutional kernels the convolutional layer contains. For a certain convolutional kernel, it can be regarded as a sliding window of size k*d, where k is the window size specified by the convolutional kernel and d is the dimension of the embedding vector.

[0178] After obtaining at least two feature vectors (e.g., four feature vectors), one approach is to use a max pooling layer to maximize each feature vector and then concatenate them together as the output of the max pooling layer, thus obtaining the target feature vector. Another approach is to use an average pooling layer to average each feature vector and then concatenate them together as the output of the average pooling layer, again obtaining the target feature vector. Finally, after passing through a fully connected layer, a softmax activation function is applied to output the class probability distribution, thus obtaining the probability value for each class label.

[0179] Furthermore, this application provides a method for text classification based on the TextCNN model. Through this method, the TextCNN model, due to its simple network structure, offers advantages such as fewer model parameters, lower computational cost, and faster training speed.

[0180] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, the category probability distribution is obtained through a text classification model based on T embedding vectors, which may specifically include:

[0181] Based on T embedding vectors, the hidden state corresponding to at least one time step is obtained through the bidirectional coding network in the text classification model;

[0182] Based on the hidden state corresponding to at least one time step, the category probability distribution is obtained through the fully connected layer in the text classification model.

[0183] In one or more embodiments, a method for text classification based on a TextRNN model is introduced. As can be seen from the foregoing embodiments, after obtaining the embedding vector corresponding to each text unit, the corresponding category probability distribution can be output through a text classification model. The text classification model can be a text recursive neural network (TextRNN) model.

[0184] Specifically, for ease of understanding, please refer to Figure 7 , Figure 7 This is another structural diagram of the text classification model in this application embodiment. As shown in the figure, the TextRNN model includes several forward recurrent units, several backward recurrent units, and a fully connected layer. The original text sample (x) can be divided into T text units (i.e., x1, x2, ..., x...). T-1 ,x T Based on this, T embedding vectors are obtained (i.e., e1, e2, ..., e...). T-1 ,e T Therefore, an embedding vector can be input at each time step to calculate the hidden state corresponding to the current time step. The hidden state is then used as the input for the current time step and passed to the next time step. This, combined with the embedding vector corresponding to the next time step, serves as the input for the recurrent unit. This process is repeated until every text unit in the original text sample has been processed. Since the original text sample has T text units, it requires T time steps.

[0185] In one scenario, the hidden states at the first and last time steps can be concatenated as input to the fully connected layer, and then passed through a softmax activation function to output the class probability distribution. In another scenario, the average of the hidden states at each time step can be used as input to the fully connected layer, and then passed through a softmax activation function to output the class probability distribution. Yet another scenario can use the hidden state at the last time step as input to the fully connected layer, and then pass through a softmax activation function to output the class probability distribution.

[0186] It should be noted that the recurrent unit can be a recursive neural network (RNN), a long short-term memory network (LSTM), or a gated recurrent unit (GRU), etc., and there is no limitation here.

[0187] Furthermore, this application provides a method for text classification based on the TextRNN model. Through this method, the RNN-based text classification model is highly flexible and offers a variety of structures. Moreover, the TextRNN model can capture long-range dependencies in sequences, thereby improving the accuracy of text classification.

[0188] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, the T text units include a first text unit and a second text unit, wherein the first text unit and the second text unit are adjacent text units;

[0189] Based on the association score corresponding to each text unit, N causal segments are determined from the original text sample, which may include:

[0190] If the correlation score corresponding to the first text unit is greater than or equal to the correlation score threshold, then the first text unit will be regarded as a text unit in the candidate causal segment.

[0191] If the correlation score corresponding to the second text unit is greater than or equal to the correlation score threshold, then the second text unit will be regarded as another text unit in the candidate causal segment.

[0192] If the correlation score corresponding to the second text unit is less than the correlation score threshold, then the candidate causal segment will be taken as one of the N causal segments.

[0193] In one or more embodiments, a method for automatically identifying N causal fragments is provided. As can be seen from the foregoing embodiments, after obtaining the association score corresponding to each text unit, a text unit with an association score greater than or equal to the association score threshold may be used as a text unit included in a causal fragment. Generally, T text units include an adjacent first text unit and a second text unit. If the association score corresponding to the first text unit is greater than or equal to the association score threshold, the first text unit is first used as a text unit in a candidate causal fragment. Similarly, it is continuously determined whether the association score corresponding to the second text unit is greater than or equal to the association score threshold. If the association score corresponding to the second text unit is greater than or equal to the association score threshold, the second text unit is used as another text unit in the candidate causal fragment, and at this time, the candidate causal fragment includes the first text unit and the second text unit. Conversely, if the association score corresponding to the second text unit is less than the association score threshold, the candidate causal fragment is used as a causal fragment.

[0194] Specifically, for ease of understanding, the phrase "this mobile phone is reasonably priced" in the original text sample is taken as an example. For ease of understanding, please refer to Figure 1 , Table 1 is a schematic diagram of the corresponding relationship between each text unit and the association score in the original text sample.

[0195] Table 1

[0196] this 0.1 department 0.3 hand 0.2 machine 0.2 price 0.3 grid 0.2 male 0.8 road 0.9

[0197] Assuming that the association score threshold is 0.8, based on Table 1, it can be seen that the association scores of the text unit "gong" and the text unit "dao" are both greater than or equal to the association score threshold, and these two text units are adjacent to each other. Therefore, "gong dao" can be used as a causal fragment.

[0198] Next, in the embodiments of the present application, a method for automatically identifying N causal fragments is provided. Through the above method, the association score can be used to directly identify text units that meet the conditions from the original text samples. Therefore, a large number of counterfactual samples can be automatically generated without manual intervention. The generated counterfactual samples can be used as additional training data for data augmentation training, which in turn can improve the robustness and generalization of various downstream text classification applications.

[0199] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, obtaining M target fragments through a text generation model based on a target text sample may specifically include:

[0200] Based on the target text sample, obtaining an encoded feature vector through an encoder in the text generation model;

[0201] Based on the encoded feature vector, the probability value corresponding to each preset text unit in the dictionary at the first time step is obtained through the decoder in the text generation model;

[0202] After sorting the probability values ​​of each preset text unit in descending order at the first time step, the first P preset text units are selected as P first candidate text units, where P is an integer greater than or equal to 1.

[0203] Based on the encoded feature vector and P first candidate text units, the probability value corresponding to each preset text unit in the dictionary at the second time step is obtained through the decoder in the text generation model. The second time step is the next time step adjacent to the first time step.

[0204] After sorting the probability values ​​of each preset text unit in descending order in the second time step, the first P preset text units are taken as P second candidate text units.

[0205] Based on P first candidate text units and P second candidate text units, generate M target fragments.

[0206] In one or more embodiments, a method for generating M target fragments is described. As can be seen from the foregoing embodiments, target text samples are input into a pre-trained text generation model to generate corresponding target fragments. It is understood that the text generation model can employ a T5 model or a sequence-to-sequence (Seq2Seq) model, etc., where using a bidirectional encoder to encode the input target text sample more fully, and the generated target fragments can overcome the length limitations of the original fragments.

[0207] Specifically, for ease of understanding, please refer to Figure 8 , Figure 8 This is a schematic diagram of the text generation model in an embodiment of this application. As shown in the figure, the target text sample is "the price of this mobile phone". <x>as an example, after the target text sample is subjected to feature processing, a plurality of embedding vectors are obtained, and these embedding vectors form a list. Then, the list corresponding to the target text sample is input into an encoder, and an encoded feature vector is obtained through the encoder. A decoder performs decoding based on the encoded feature vector, and sequentially decodes to obtain probability values corresponding to each preset text unit in a dictionary at each time step. Finally, a corresponding target segment is generated by combining the probability values of each preset text unit at each time step.

[0208] By way of example, taking a greedy search algorithm as an example, for ease of understanding, please refer to Figure 9 , Figure 9 which is a schematic diagram of predicting text units based on the greedy search algorithm in an embodiment of the present application. As shown in the figure, it is assumed that at a first time step, the probability value of the preset text unit "go" predicted is "0.4", the probability value of the preset text unit "love" is "0.5", and the probability value of the preset text unit "think" is "0.1". After sorting the probability values corresponding to each preset text unit at the first time step from largest to smallest, the first preset text unit is taken as a first candidate text unit, that is, the first candidate text unit is "love". Based on this, the encoded feature vector and the embedding vector corresponding to the first candidate text unit are used as the input of the decoder, so that at a second time step, the probability value of the preset text unit "you" predicted is "0.4", the probability value of the preset text unit "him" is "0.3", and the probability value of the preset text unit "her" is "0.3". After sorting the probability values corresponding to each preset text unit at the second time step from largest to smallest, the first preset text unit is taken as a second candidate text unit, that is, the second candidate text unit is "you".

[0209] It can be seen that starting from "I", when selecting the next preset text unit, "love" is selected; starting from "love", when selecting the next preset text unit, "you" is selected, and the total probability value is 0.5×0.4=0.2. Thus, a target segment "love you" is obtained.

[0210] By way of example, taking a beam search algorithm as an example, for ease of understanding, please refer to Figure 10 , Figure 10 It is a schematic diagram of predicting text units based on the beam search algorithm in the embodiments of the present application. As shown in the figure, taking the configuration of the beam number as 2 (that is, P=2) as an example, it is assumed that the probability value of the preset text unit "go" predicted in the first time step is "0.4", the probability value of the preset text unit "love" is "0.5", and the probability value of the preset text unit "want" is "0.1". After sorting the probability values corresponding to each preset text unit at the first time step from largest to smallest, the first two preset text units are used as two first candidate text units, that is, the first candidate text units are "love" and "go". Based on this, the encoded feature vector and the embedding vector corresponding to the first candidate text unit are used as the input of the decoder. Accordingly, under one condition, the probability value of the preset text unit "you" predicted at the second time step is "0.4", the probability value of the preset text unit "him" is "0.3", and the probability value of the preset text unit "her" is "0.3". Under another condition, the probability value of the preset text unit "do" predicted at the second time step is "0.05", the probability value of the preset text unit "eat" is "0.05", and the probability value of the preset text unit "play" is "0.9".

[0211] It can be seen that in one case, starting from "I", "love" is selected when selecting the next preset text unit. Starting from "love", "you" is selected when selecting the next preset text unit, and its total probability value is 0.5×0.4=0.2. Accordingly, a target segment "love you" is obtained. In another case, starting from "I", "go" is selected when selecting the next preset text unit. Starting from "go", "play" is selected when selecting the next preset text unit, and its total probability value is 0.4×0.9=0.36. Accordingly, the group with a higher total probability value is preferentially selected, that is, the target segment "go play" is obtained.

[0212] Secondly, in the embodiments of the present application, a method for generating M target segments is provided. Through the above method, the target text sample can be used to directly predict the text units belonging to the target segment through the trained text generation model. On one hand, a feasible method is provided for the implementation of the solution, thereby improving the feasibility and operability of the solution. On the other hand, using a sequence-to-sequence model allows the generated target segment to break through the length limitation of causal segments.

[0213] Optionally, in the above Figure 3 On the basis of the corresponding respective embodiments, in another optional embodiment provided by the embodiments of the present application, the method may further include:

[0214] Obtaining training text samples, wherein the training text samples include Q text units, and Q is an integer greater than 1;

[0215] Based on training text samples, training feature vectors are obtained through the encoder in the text generation model;

[0216] Based on the trained feature vectors, the probability value corresponding to each preset text unit in the dictionary at the current time step is obtained through the decoder in the text generation model;

[0217] Based on the probability value corresponding to each preset text unit at the current time step, obtain the probability value corresponding to the target text unit;

[0218] The model parameters of the text generation model are updated based on the probability value corresponding to the target text unit.

[0219] In one or more embodiments, a training method for a text generation model is described. As can be seen from the foregoing embodiments, training text samples can be used during the training phase. First, the training text samples are masked. For example, if the training text sample is "This phone is reasonably priced," after masking, partially generated text is obtained, namely, "This phone is reasonably priced..." <x>Based on this, training text samples are used as input to the encoder in the text generation model to obtain training feature vectors. These training feature vectors are then used as input to the decoder, which obtains the probability value corresponding to each preset text unit in the dictionary at the current time step (i.e., the t-th time step). Based on this, the preset text unit with the highest probability value can be selected as the target text unit.

[0220] Specifically, the loss value for each time step can be calculated using the following generation loss function:

[0221] L gen =-logp θ (y t |y <t ,x);

[0222] Among them, L gen Let represent the generation loss function. Let x represent the training text sample. y t This represents the target text unit corresponding to the current time step. <t This represents the partially generated text after masking. θ This represents the probability value of generating a text unit at the current time step after the text generation model is used.

[0223] This application provides a training method for a text generation model. By using training text samples as model input, the training of the text generation model is guided. This provides a concrete and feasible basis for the implementation of the solution, thereby improving its feasibility and operability.

[0224] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, M target fragments are obtained based on the target text sample through a text generation model, which may specifically include:

[0225] Obtain the target category label;

[0226] Based on the target text sample and the target classification label, the encoded feature vector is obtained through the encoder in the text generation model;

[0227] Based on the encoded feature vector, the probability value corresponding to each preset text unit in the dictionary at the first time step is obtained through the decoder in the text generation model;

[0228] After sorting the probability values ​​of each preset text unit in descending order at the first time step, the first P preset text units are selected as P first candidate text units, where P is an integer greater than or equal to 1.

[0229] Based on the encoded feature vector and P first candidate text units, the probability value corresponding to each preset text unit in the dictionary at the second time step is obtained through the decoder in the text generation model. The second time step is the next time step adjacent to the first time step.

[0230] After sorting the probability values ​​of each preset text unit in descending order in the second time step, the first P preset text units are taken as P second candidate text units.

[0231] Based on P first candidate text units and P second candidate text units, generate M target fragments for the target classification label.

[0232] In one or more embodiments, another method for generating M target fragments is introduced. As described in the foregoing embodiments, target text samples and target classification labels are input into a pre-trained text generation model to generate corresponding target fragments. It is understood that the text generation model can employ a T5 model or a sequence-to-sequence (Seq2Seq) model, etc., where using a bidirectional encoder to encode the input target text samples more fully, and the generated target fragments can overcome the length limitations of the original fragments.

[0233] Specifically, for ease of understanding, please refer to Figure 11 , Figure 11 This is another structural diagram of the text generation model in this application embodiment. As shown in the figure, the target text sample is "this mobile phone price". <x>Taking the target classification label as "positive" as an example, the target text sample, after feature processing, yields multiple embedding vectors, which form a list. The feature-processed target classification label is appended to the list as a vector. The list corresponding to the target text sample and the feature vectors corresponding to the target classification label are then input to the encoder, which obtains the encoded feature vectors. The decoder decodes based on these encoded feature vectors, sequentially obtaining the probability value of each preset text unit in the dictionary at each time step. Finally, by combining the probability values ​​of each preset text unit at each time step, the corresponding target segment is generated.

[0234] It should be noted that in practical applications, greedy search algorithms or bundle search algorithms can be used to determine the target segment, which will not be elaborated here.

[0235] Based on this, the process of generating counterfactual samples based on target classification labels will be described below with illustrations. Please refer to [link / reference]. Figure 12 , Figure 12 This is a schematic diagram illustrating the generation of counterfactual samples based on target classification labels in an embodiment of this application. As shown in the figure, in the stage of identifying causal fragments, two causal fragments can be found from the original text sample based on the true classification labels: "fair" and "no regrets". In the label-guided generation stage, the corresponding target text sample can be generated, namely, "the price of this phone". <x>Buy it <y>After adding the target classification label (i.e., the "negative" classification label), candidate counterfactual samples are generated, namely, "This phone is overpriced, I regret buying it." In the post-processing screening stage, if the probability value of the candidate counterfactual sample corresponding to the target classification label is greater than or equal to the second probability threshold, then the candidate counterfactual sample is used as a counterfactual sample.

[0236] Secondly, this application provides a method for generating M target fragments. Using the target text samples and target classification labels, a trained text generation model can directly predict text units belonging to the target fragments. This provides a feasible approach to implementation, thereby improving the feasibility and operability of the solution. Furthermore, using a sequence-to-sequence model allows the generated target fragments to overcome the length limitations of causal fragments. In addition, it can selectively output counterfactual samples related to the target classification labels, thus increasing the diversity of sample generation.

[0237] Optionally, in the above Figure 3 Based on the corresponding embodiments, another optional embodiment provided in this application may further include:

[0238] Obtain training text samples, where the training text samples have been labeled with the true classification labels, and the training text samples consist of Q text units, where Q is an integer greater than 1;

[0239] Based on training text samples and real classification labels, training feature vectors are obtained through the encoder in the text generation model;

[0240] Based on the trained feature vectors, the probability value corresponding to each preset text unit in the dictionary at the current time step is obtained through the decoder in the text generation model;

[0241] Based on the probability value corresponding to each preset text unit at the current time step, obtain the probability value corresponding to the target text unit;

[0242] The model parameters of the text generation model are updated based on the probability value corresponding to the target text unit, the true classification label, and the remaining classification labels, where the remaining classification labels are the classification labels other than the true classification labels.

[0243] In one or more embodiments, a training method for a text generation model is described. As can be seen from the foregoing embodiments, during the training phase, training text samples along with the real classification labels can be input into the text generation model. First, the training text samples are masked. For example, if the training text sample is "This phone is reasonably priced," after masking, partially generated text is obtained, namely, "This phone is reasonably priced..." <x>Based on this, training text samples and real classification labels are used as inputs to the encoder in the text generation model to obtain training feature vectors. These training feature vectors are then used as inputs to the decoder, which obtains the probability value corresponding to each preset text unit in the dictionary at the current time step (i.e., the t-th time step). Based on this, the preset text unit with the highest probability value can be selected as the target text unit.

[0244] Specifically, the loss value for each time step can be calculated using the following generation loss function:

[0245] L gen =-logp θ (y t |y <t ,x,l g );

[0246] Among them, L gen This represents the generation loss function. x represents the training text sample. g This represents the true classification label of the training text sample. t This represents the target text unit corresponding to the current time step. <t This represents the partially generated text after masking. θ This represents the probability value of generating a text unit at the current time step after the text generation model is used.

[0247] To enhance the text generation model's utilization of input classification labels and improve the control of these labels over the generated text, the following dissimilarity loss function can be used to calculate the loss value at each time step:

[0248]

[0249] Among them, L unlike Represents the dissimilarity loss function. L|{l g } represents the remaining category labels. l represents the category label selected from the remaining category labels.

[0250] As can be seen, the generation probability under each of the remaining classification labels can be used as the dissimilarity loss. Finally, the generation loss and dissimilarity loss are weighted and summed using the following loss function:

[0251] L = L gen +αL unlike ;

[0252] Where L represents the total loss value, and α represents the control coefficient.

[0253] Furthermore, this application provides a training method for a text generation model. Using the above method, real classification labels are used as model input to guide the self-supervised training of the text generation model. It is evident that utilizing real classification labels enhances the text generation model's use of input labels, improving the control of labels over generated text. In addition, a dissimilarity loss function is proposed, ensuring that generated text units not only have a high generation probability under the current input label but also a low generation probability under other labels. This improves the feasibility and operability of the solution.

[0254] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, M causal segments in the original text sample are replaced with M target segments to obtain a counterfactual sample, which may specifically include:

[0255] Replace M causal segments in the original text sample with M target segments to obtain candidate counterfactual samples;

[0256] Based on candidate counterfactual samples, the probability distribution of candidate categories of candidate counterfactual samples is obtained through a text classification model;

[0257] Based on the probability distribution of candidate categories, determine the candidate probability value of the candidate counterfactual sample relative to the target classification label;

[0258] Candidate counterfactual samples whose candidate probability value is greater than or equal to the second probability threshold are considered counterfactual samples.

[0259] In one or more embodiments, a method for confidence filtering of candidate counterfactual samples is described. As described in the foregoing embodiments, candidate counterfactual samples are obtained by replacing each causal segment in the original text sample with the corresponding target segment. For the generated candidate counterfactual samples, the confidence level of the candidate counterfactual sample can also be determined according to the pre-set target classification label. That is, after obtaining the candidate counterfactual samples, the candidate counterfactual samples can be divided into several text units, and the embedding vector corresponding to each text unit can be obtained. Then, the embedding vector corresponding to each text unit in the candidate counterfactual samples is used as the input of a text classification model, and the candidate class probability distribution of the candidate counterfactual samples is output by the text classification model.

[0260] Specifically, taking three categories as an example: "Sports," "Entertainment," and "Technology," the candidate category probability distribution is represented as a 3-dimensional vector. Assume the target category of the candidate counterfactual sample is "Technology," and that the candidate category probability distribution is (0.1, 0.1, 0.8). The probability values ​​are 0.1 for "Sports," 0.1 for "Entertainment," and 0.8 for "Technology." Therefore, the probability value corresponding to the target category (i.e., "Technology") is 0.8. Taking a second probability threshold of 0.7 as an example, the probability value corresponding to the target category (i.e., "Technology") is greater than the second probability threshold (0.7). Therefore, the candidate counterfactual sample is determined to be a high-confidence sample. Thus, the candidate counterfactual sample can be used as a counterfactual sample.

[0261] Furthermore, this application provides a method for confidence-based filtering of candidate counterfactual samples. While the above method strengthens the association between candidate counterfactual samples and the target classification label through the text generation model, the candidate counterfactual samples may still contain noise, and it is difficult to strictly guarantee the association with the target classification label. Data augmentation relies on high-quality samples. Therefore, adding a post-processing filtering step can select candidate counterfactual samples with high confidence as counterfactual samples, thereby reducing the impact of noise on the generation results, improving the sample generation effect, and meeting the needs of data augmentation.

[0262] Optionally, in the above Figure 3 Based on the corresponding embodiments, in another optional embodiment provided by this application, obtaining the target classification label may specifically include:

[0263] The original text sample and at least one optional category control are displayed on the sample generation interface, wherein each optional category control corresponds to a category label;

[0264] In response to a selection operation on a target category control, obtain the target category label, wherein the target category control belongs to at least one selectable category control, and the target category label is the category label corresponding to the target category control.

[0265] In one or more embodiments, a method for generating counterfactual samples based on a human-computer interaction interface is described. As can be seen from the foregoing embodiments, the original text samples come from a database. Therefore, a human-computer interaction interface can also be provided, allowing users to select one or more original text samples to generate corresponding counterfactual samples. Simultaneously, a target classification label can be selected so that the generated counterfactual samples closely approximate the selected target classification label.

[0266] Specifically, for ease of understanding, please refer to Figure 13 , Figure 13 This is a schematic diagram of an interface change based on interactive operations in an embodiment of this application, such as... Figure 13 As shown in Figure (A), S1 indicates the original text sample; for example, the figure shows three original text samples. S2 indicates the true category label corresponding to each original text sample; for example, the true category label is "positive". S3 indicates the optional category control, and the optional category control indicated by S3 corresponds to the "positive" category label. S4 indicates the optional category control, and the optional category control indicated by S4 corresponds to the "neutral" category label. S5 indicates the optional category control, and the optional category control indicated by S5 corresponds to the "negative" category label.

[0267] For example, clicking the optional category control indicated by S5, where this optional category control is the target category control, determines the target category label to be the "negative" category label. Based on this, the following is displayed: Figure 13 Figure (B) shows the sample generation interface. The sample generation interface displays the counterfactual samples corresponding to each original text sample. For example, the original text sample is "This phone is reasonably priced, you won't regret buying it", and its corresponding counterfactual sample is "This phone is overpriced, you'll regret buying it".

[0268] It should be noted that, Figure 13 The layout of interface elements, text content, and display method shown are for illustrative purposes only and should not be construed as limiting this application.

[0269] Furthermore, this application provides a method for generating samples based on a human-computer interaction interface. Through this method, users can also select one or more target classification labels via the human-computer interaction interface to obtain counterfactual samples belonging to that target classification label. Therefore, by providing a human-computer interaction interface, the needs of users can be better met, thereby improving the user experience.

[0270] The sample generation apparatus in this application is described in detail below. Please refer to [link / reference]. Figure 14 , Figure 14 This is a schematic diagram of one embodiment of the sample generation device in this application. The sample generation device 20 includes:

[0271] The acquisition module 210 is used to obtain the category probability distribution based on the original text sample through a text classification model, wherein the original text sample includes T text units, and T is an integer greater than 1;

[0272] The determination module 220 is used to determine the association score corresponding to each text unit in the original text sample based on the category probability distribution;

[0273] The determination module 220 is also used to determine N causal segments from the original text sample based on the correlation score corresponding to each text unit, wherein each causal segment includes at least one text unit, and N is an integer greater than or equal to 1;

[0274] The processing module 230 is used to mask M causal segments out of N causal segments to obtain target text samples, where M is an integer greater than or equal to 1 and less than or equal to N;

[0275] The acquisition module 210 is also used to acquire M target fragments based on the target text sample through a text generation model, wherein the target fragments and causal fragments have a corresponding relationship;

[0276] The processing module 230 is also used to replace M causal segments in the original text sample with M target segments to obtain counterfactual samples.

[0277] This application provides a sample generation apparatus. Using this apparatus, by utilizing the correlation score between the category probability distribution and each text unit in the original text sample, causal segments in the original text sample that affect the category prediction results can be identified. Then, a text generation model is used to regenerate target segments from these causal segments. After replacing the causal segments with the target segments, counterfactual samples can be obtained. Therefore, counterfactual samples can be automatically generated without human intervention, thereby reducing the cost of data augmentation.

[0278] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0279] The determination module 220 is also used to determine the probability value corresponding to the real classification label based on the original text sample after obtaining the category probability distribution through the text classification model. The original text sample has been labeled as the real classification label, and each probability value in the category probability distribution corresponds to a classification label.

[0280] The determination module 220 is also used to perform the step of determining the association score corresponding to each text unit in the original text sample according to the category probability distribution if the probability value corresponding to the true classification label is greater than or equal to the first probability threshold.

[0281] This application provides a sample generation apparatus. Using this apparatus, high-confidence original text samples can be selected for subsequent processing, thereby reducing the impact of noise on the generation results and improving the sample generation effect.

[0282] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0283] The acquisition module 210 is specifically used to segment the original text sample into text units to obtain T text units.

[0284] Generate T embedding vectors from T text units, where each embedding vector corresponds to a text unit.

[0285] Based on T embedding vectors, the category probability distribution is obtained through a text classification model.

[0286] In this application embodiment, a sample generation apparatus is provided. Using this apparatus, each text unit in the original text sample is embedded, which serves as the basis for predicting the category to which the original text sample belongs, thereby increasing the feasibility and operability of the solution.

[0287] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0288] The determination module 220 is specifically used to calculate the classification loss value based on the category probability distribution and the true classification label corresponding to the original text sample through the classification loss function;

[0289] The first partial derivative is calculated based on the classification loss value;

[0290] For each text unit in the original text sample, the second partial derivative is calculated based on the embedding vector corresponding to the text unit;

[0291] For each text unit in the original text sample, the association score corresponding to the text unit is determined based on the first partial derivative and the second partial derivative corresponding to the text unit.

[0292] In this embodiment, a sample generation device is provided. Using this device, the gradient is calculated based on the embedding vector of each text unit and the predicted category probability distribution. The 2-norm of the gradient is then used as the association score for the corresponding text unit, thus providing a feasible basis for the implementation of the scheme and improving its feasibility and operability.

[0293] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0294] The acquisition module 210 is specifically used to obtain the average embedding vector based on T embedding vectors through the hidden layer in the text classification model.

[0295] Based on the average embedding vector, the category probability distribution is obtained through the output layer of the text classification model.

[0296] This application provides a sample generation device. Using this device, the fastText model, suitable for tasks with a large number of samples and numerous class labels, can achieve excellent prediction results. At the same time, the fastText model has high training efficiency.

[0297] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0298] Module 210 is specifically used to generate a two-dimensional matrix based on T embedding vectors;

[0299] Based on a two-dimensional matrix, at least two feature vectors are obtained through the convolutional layer in a text classification model.

[0300] The target feature vector is obtained through the pooling layer in the text classification model based on at least two feature vectors.

[0301] Based on the target feature vector, the category probability distribution is obtained through the fully connected layer in the text classification model.

[0302] This application provides a sample generation device. Using this device, the TextCNN model, due to its simple network structure, offers advantages such as fewer model parameters, lower computational cost, and faster training speed.

[0303] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0304] The acquisition module 210 is specifically used to acquire the hidden state corresponding to at least one time step based on T embedding vectors through the bidirectional coding network in the text classification model.

[0305] Based on the hidden state corresponding to at least one time step, the category probability distribution is obtained through the fully connected layer in the text classification model.

[0306] This application provides a sample generation device. Using this device, the RNN-based text classification model is highly flexible and offers a variety of structures. Furthermore, the TextRNN model can capture long-range dependencies in sequences, thereby improving the accuracy of text classification.

[0307] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation device 20 provided in this application, the T text units include a first text unit and a second text unit, wherein the first text unit and the second text unit are adjacent text units;

[0308] The determination module 220 is specifically used to determine the first text unit as a text unit in the candidate causal segment if the correlation score corresponding to the first text unit is greater than or equal to the correlation score threshold.

[0309] If the correlation score corresponding to the second text unit is greater than or equal to the correlation score threshold, then the second text unit will be regarded as another text unit in the candidate causal segment.

[0310] If the correlation score corresponding to the second text unit is less than the correlation score threshold, then the candidate causal segment will be taken as one of the N causal segments.

[0311] In this embodiment, a sample generation device is provided. Using the above device, text units that meet the conditions can be directly identified from the original text samples by utilizing the association score. Thus, a large number of counterfactual samples can be automatically generated without human intervention. The generated counterfactual samples can be used as additional training data and for data augmentation training, thereby improving the robustness and generalization of various downstream text classification applications.

[0312] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0313] The acquisition module 210 is specifically used to obtain the encoded feature vector based on the target text sample through the encoder in the text generation model;

[0314] Based on the encoded feature vector, the probability value corresponding to each preset text unit in the dictionary at the first time step is obtained through the decoder in the text generation model;

[0315] After sorting the probability values ​​of each preset text unit in descending order at the first time step, the first P preset text units are selected as P first candidate text units, where P is an integer greater than or equal to 1.

[0316] Based on the encoded feature vector and P first candidate text units, the probability value corresponding to each preset text unit in the dictionary at the second time step is obtained through the decoder in the text generation model. The second time step is the next time step adjacent to the first time step.

[0317] After sorting the probability values ​​of each preset text unit in descending order in the second time step, the first P preset text units are taken as P second candidate text units.

[0318] Based on P first candidate text units and P second candidate text units, generate M target fragments.

[0319] This application provides a sample generation device. Using this device, text units belonging to the target segment can be directly predicted using a trained text generation model based on the target text sample. This provides a feasible approach to implementation, thereby improving the feasibility and operability of the solution. Furthermore, using a sequence-to-sequence model allows the generated target segment to overcome the length limitations of causal segments.

[0320] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation device 20 provided in this application, the sample generation device 20 further includes a training module 240;

[0321] The acquisition module 210 is also used to acquire training text samples, wherein the training text samples include Q text units, where Q is an integer greater than 1;

[0322] The acquisition module 210 is also used to acquire training feature vectors based on training text samples through the encoder in the text generation model;

[0323] The acquisition module 210 is also used to obtain the probability value of each preset text unit in the dictionary at the current time step based on the trained feature vector through the decoder in the text generation model;

[0324] The acquisition module 210 is also used to acquire the probability value corresponding to the target text unit based on the probability value corresponding to each preset text unit at the current time step;

[0325] Training module 240 is used to update the model parameters of the text generation model based on the probability value corresponding to the target text unit.

[0326] This application provides a sample generation device. Using this device, training text samples are used as model input to guide the training of the text generation model. This provides a concrete and feasible basis for the implementation of the solution, thereby improving its feasibility and operability.

[0327] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0328] Module 210 is specifically used to obtain the target category label;

[0329] Based on the target text sample and the target classification label, the encoded feature vector is obtained through the encoder in the text generation model;

[0330] Based on the encoded feature vector, the probability value corresponding to each preset text unit in the dictionary at the first time step is obtained through the decoder in the text generation model;

[0331] After sorting the probability values ​​of each preset text unit in descending order at the first time step, the first P preset text units are selected as P first candidate text units, where P is an integer greater than or equal to 1.

[0332] Based on the encoded feature vector and P first candidate text units, the probability value corresponding to each preset text unit in the dictionary at the second time step is obtained through the decoder in the text generation model. The second time step is the next time step adjacent to the first time step.

[0333] After sorting the probability values ​​of each preset text unit in descending order in the second time step, the first P preset text units are taken as P second candidate text units.

[0334] Based on P first candidate text units and P second candidate text units, generate M target fragments for the target classification label.

[0335] This application provides a sample generation apparatus. Using this apparatus, text units belonging to the target segment can be directly predicted using a trained text generation model based on the target text sample and the target classification label. This provides a feasible method for implementing the solution, thereby improving its feasibility and operability. Furthermore, using a sequence-to-sequence model allows the generated target segment to overcome the length limitations of causal segments. In addition, it can selectively output counterfactual samples related to the target classification label, thereby increasing the diversity of sample generation.

[0336] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0337] The acquisition module 210 is also used to acquire training text samples, wherein the training text samples have been labeled with the true classification labels, and the training text samples include Q text units, where Q is an integer greater than 1;

[0338] The acquisition module 210 is also used to acquire training feature vectors based on training text samples and real classification labels through the encoder in the text generation model;

[0339] The acquisition module 210 is also used to obtain the probability value of each preset text unit in the dictionary at the current time step based on the trained feature vector through the decoder in the text generation model;

[0340] The acquisition module 210 is also used to acquire the probability value corresponding to the target text unit based on the probability value corresponding to each preset text unit at the current time step;

[0341] The training module 240 is also used to update the model parameters of the text generation model based on the probability value corresponding to the target text unit, the true classification label and the remaining classification label, where the remaining classification label is the classification label other than the true classification label.

[0342] This application provides a sample generation device. Using this device, real classification labels are used as model input to guide the self-supervised training of the text generation model. It is evident that utilizing real classification labels enhances the text generation model's use of input labels, improving the control of labels over the generated text. Furthermore, a dissimilarity loss function is proposed, ensuring that the generated text units not only have a high generation probability under the current input label but also a low generation probability under other labels. This improves the feasibility and operability of the solution.

[0343] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0344] Processing module 230 is specifically used to replace M causal segments in the original text sample with M target segments to obtain candidate counterfactual samples;

[0345] Based on candidate counterfactual samples, the probability distribution of candidate categories of candidate counterfactual samples is obtained through a text classification model;

[0346] Based on the probability distribution of candidate categories, determine the candidate probability value of the candidate counterfactual sample relative to the target classification label;

[0347] Candidate counterfactual samples whose candidate probability value is greater than or equal to the second probability threshold are considered counterfactual samples.

[0348] This application provides a sample generation apparatus. While the above apparatus strengthens the association between candidate counterfactual samples and the target classification label through a text generation model, candidate counterfactual samples may still contain noise, and it is difficult to strictly guarantee the association with the target classification label. Data augmentation relies on high-quality samples. Therefore, adding a post-processing screening step can select high-confidence candidate counterfactual samples as counterfactual samples, thereby reducing the impact of noise on the generation results, improving the sample generation effect, and meeting the needs of data augmentation.

[0349] Optionally, in the above Figure 14 Based on the corresponding embodiments, in another embodiment of the sample generation apparatus 20 provided in this application,

[0350] The acquisition module 210 is specifically used to display the original text sample and at least one optional category control on the sample generation interface, wherein each optional category control corresponds to a category label;

[0351] In response to a selection operation on a target category control, obtain the target category label, wherein the target category control belongs to at least one selectable category control, and the target category label is the category label corresponding to the target category control.

[0352] This application provides a sample generation device. Using this device, users can also select one or more target classification labels through a human-computer interaction interface to obtain counterfactual samples belonging to those target classification labels. Therefore, by providing a human-computer interaction interface, the needs of users can be better met, thereby improving the user experience.

[0353] This application also provides a terminal, such as... Figure 15 As shown, for ease of explanation, only the parts related to the embodiments of this application are shown. For specific technical details not disclosed, please refer to the method section of the embodiments of this application. In the embodiments of this application, a smartphone is used as an example for illustration:

[0354] Figure 15 This is a block diagram illustrating a portion of the structure of a smartphone related to the terminal provided in the embodiments of this application. (Reference) Figure 15 The smartphone includes components such as a radio frequency (RF) circuit 310, a memory 320, an input unit 330, a display unit 340, a sensor 350, an audio circuit 360, a wireless fidelity (WiFi) module 370, a processor 380, and a power supply 390. Those skilled in the art will understand that... Figure 15 The smartphone structure shown does not constitute a limitation on smartphones and may include more or fewer components than shown, or combine certain components, or have different component arrangements.

[0355] The following is combined Figure 15 A detailed introduction to the various components of a smartphone:

[0356] RF circuit 310 can be used for receiving and transmitting signals during information transmission or calls. Specifically, it receives downlink information from the base station and processes it with processor 380; additionally, it transmits uplink data to the base station. Typically, RF circuit 310 includes, but is not limited to, an antenna, at least one amplifier, a transceiver, a coupler, a low-noise amplifier (LNA), a duplexer, etc. Furthermore, RF circuit 310 can also communicate wirelessly with networks and other devices. The aforementioned wireless communication can use any communication standard or protocol, including but not limited to Global System for Mobile Communication (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Long Term Evolution (LTE), email, Short Message Service (SMS), etc.

[0357] The memory 320 can be used to store software programs and modules. The processor 380 executes various functions and data processing of the smartphone by running the software programs and modules stored in the memory 320. The memory 320 may mainly include a program storage area and a data storage area. The program storage area may store the operating system, applications required for at least one function (such as sound playback function, image playback function, etc.), etc.; the data storage area may store data created based on the use of the smartphone (such as audio data, phonebook, etc.). In addition, the memory 320 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device.

[0358] The input unit 330 can be used to receive input numerical or character information, and to generate key signal inputs related to user settings and function control of the smartphone. Specifically, the input unit 330 may include a touch panel 331 and other input devices 332. The touch panel 331, also known as a touch screen, can collect touch operations performed by the user on or near it (such as operations performed by the user using a finger, stylus, or any suitable object or accessory on or near the touch panel 331), and drive the corresponding connected devices according to a pre-set program. Optionally, the touch panel 331 may include two parts: a touch detection device and a touch controller. The touch detection device detects the user's touch position and the signal generated by the touch operation, and transmits the signal to the touch controller; the touch controller receives touch information from the touch detection device, converts it into touch point coordinates, and sends it to the processor 380, and can also receive and execute commands sent by the processor 380. In addition, the touch panel 331 can be implemented using various types such as resistive, capacitive, infrared, and surface acoustic wave. In addition to the touch panel 331, the input unit 330 may also include other input devices 332. Specifically, other input devices 332 may include, but are not limited to, one or more of the following: physical keyboard, function keys (such as volume control buttons, power buttons, etc.), mouse, joystick, etc.

[0359] The display unit 340 can be used to display information input by the user or information provided to the user, as well as various menus of the smartphone. The display unit 340 may include a display panel 341, optionally configured as a liquid crystal display (LCD), organic light-emitting diode (OLED), or similar form. Further, a touch panel 331 may cover the display panel 341. When the touch panel 331 detects a touch operation on or near it, it transmits the information to the processor 380 to determine the type of touch event. Subsequently, the processor 380 provides corresponding visual output on the display panel 341 based on the type of touch event. Although in Figure 15 In this embodiment, the touch panel 331 and the display panel 341 are two separate components to realize the input and output functions of the smartphone. However, in some embodiments, the touch panel 331 and the display panel 341 can be integrated to realize the input and output functions of the smartphone.

[0360] Smartphones may also include at least one sensor 350, such as a light sensor, a motion sensor, and other sensors. Specifically, the light sensor may include an ambient light sensor and a proximity sensor. The ambient light sensor can adjust the brightness of the display panel 341 according to the ambient light level, and the proximity sensor can turn off the display panel 341 and / or backlight when the smartphone is moved to the ear. As a type of motion sensor, an accelerometer sensor can detect the magnitude of acceleration in various directions (generally three axes). When stationary, it can detect the magnitude and direction of gravity and can be used for applications that recognize the smartphone's posture (such as landscape / portrait switching, related games, magnetometer posture calibration), vibration recognition functions (such as pedometers, taps), etc. Other sensors that smartphones may also be equipped with, such as gyroscopes, barometers, hygrometers, thermometers, and infrared sensors, will not be described in detail here.

[0361] Audio circuit 360, speaker 361, and microphone 362 provide an audio interface between the user and the smartphone. Audio circuit 360 converts received audio data into electrical signals and transmits them to speaker 361, where speaker 361 converts them into sound signals for output. On the other hand, microphone 362 converts collected sound signals into electrical signals, which are received by audio circuit 360, converted into audio data, and then processed by processor 380 before being transmitted via RF circuit 310 to, for example, another smartphone, or the audio data can be output to memory 320 for further processing.

[0362] WiFi is a short-range wireless transmission technology. Smartphones, through their WiFi modules (370), can help users send and receive emails, browse web pages, and access streaming media, providing wireless broadband internet access. Although Figure 15 WiFi module 370 is shown, but it is understood that it is not an essential component of a smartphone and can be omitted as needed without changing the nature of the invention.

[0363] The processor 380 is the control center of the smartphone, connecting various parts of the smartphone through various interfaces and lines. It performs various functions and processes data by running or executing software programs and / or modules stored in the memory 320, and by accessing data stored in the memory 320. Optionally, the processor 380 may include one or more processing units; optionally, the processor 380 may integrate an application processor and a modem processor, wherein the application processor mainly handles the operating system, user interface, and applications, and the modem processor mainly handles wireless communication. It is understood that the aforementioned modem processor may also not be integrated into the processor 380.

[0364] The smartphone also includes a power supply 390 (such as a battery) that powers various components. Optionally, the power supply can be logically connected to the processor 380 through a power management system, thereby enabling functions such as charging, discharging, and power consumption management through the power management system.

[0365] Although not shown, smartphones may also include a camera, Bluetooth module, etc., which will not be described in detail here.

[0366] The steps performed by the terminal in the above embodiments can be based on this Figure 15 The terminal structure shown.

[0367] This application also provides a server, such as... Figure 16 As shown, the server 400 can vary considerably due to different configurations or performance, and may include one or more central processing units (CPUs) 422 (e.g., one or more processors) and memory 432, and one or more storage media 430 (e.g., one or more mass storage devices) for storing applications 442 or data 444. The memory 432 and storage media 430 can be temporary or persistent storage. The program stored in the storage media 430 may include one or more modules (not shown in the figure), each module including a series of instruction operations on the server. Furthermore, the CPU 422 may be configured to communicate with the storage media 430 and execute the series of instruction operations stored in the storage media 430 on the server 400.

[0368] Server 400 may also include one or more power supplies 426, one or more wired or wireless network interfaces 450, one or more input / output interfaces 458, and / or one or more operating systems 441, such as Windows Server. TM Mac OS X TM Unix TM Linux TM FreeBSD TM etc.

[0369] The steps performed by the server in the above embodiments can be based on this Figure 16 The server structure shown.

[0370] This application also provides a computer device, including a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the steps of the methods described in the foregoing embodiments.

[0371] This application also provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the steps of the methods described in the foregoing embodiments.

[0372] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the methods described in the foregoing embodiments.

[0373] It is understood that in the specific implementation of this application, data related to user information and sample content are 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 related data must comply with the relevant laws, regulations and standards of the relevant countries and regions.

[0374] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.

[0375] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection between apparatuses or units through some interfaces, and may be electrical, mechanical, or other forms.

[0376] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0377] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0378] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, 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. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0379] The above-described embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.< / x> < / y> < / x> < / x> < / x> < / x> < / y> < / x> < / y> < / x> < / y> < / x>

Claims

1. A method for generating counterfactual samples, characterized in that, include: Based on the original text sample, the category probability distribution is obtained through a text classification model, wherein the original text sample includes T text units, and T is an integer greater than 1; Based on the category probability distribution and the true classification label corresponding to the original text sample, the classification loss value is calculated using the classification loss function; The first partial derivative is calculated based on the classification loss value; For each text unit in the original text sample, the second partial derivative is calculated based on the embedding vector corresponding to the text unit; For each text unit in the original text sample, the association score corresponding to the text unit is determined based on the first partial derivative and the second partial derivative corresponding to the text unit. Based on the correlation score corresponding to each text unit, N causal segments are determined from the original text sample, wherein each causal segment includes at least one text unit, and N is an integer greater than or equal to 1; Masking is performed on M of the N causal segments to obtain the target text sample, where M is an integer greater than or equal to 1 and less than or equal to N; Based on the target text sample, M target segments are obtained through a text generation model, wherein the target segments correspond to the causal segments and the target segments and the causal segments have the same position; The M causal segments in the original text sample are replaced with the M target segments to obtain a counterfactual sample.

2. The generation method according to claim 1, characterized in that, After obtaining the category probability distribution based on the original text samples through a text classification model, the method further includes: Based on the category probability distribution, the probability value corresponding to the true classification label is determined, wherein the original text sample has been labeled as the true classification label, and each probability value in the category probability distribution corresponds to a classification label; If the probability value corresponding to the true classification label is greater than or equal to the first probability threshold, then the step of determining the association score corresponding to each text unit in the original text sample based on the category probability distribution is executed.

3. The generation method according to claim 1, characterized in that, The process of obtaining the category probability distribution based on the original text samples using a text classification model includes: The original text sample is segmented into text units to obtain the T text units; T embedding vectors are generated based on the T text units, wherein the embedding vectors correspond to the text units; Based on the T embedding vectors, the category probability distribution is obtained through the text classification model.

4. The generation method according to claim 3, characterized in that, The step of obtaining the category probability distribution based on the T embedding vectors through the text classification model includes: Based on the T embedding vectors, the average embedding vector is obtained through the hidden layer in the text classification model; Based on the average embedding vector, the category probability distribution is obtained through the output layer of the text classification model.

5. The generation method according to claim 3, characterized in that, The step of obtaining the category probability distribution based on the T embedding vectors through the text classification model includes: Generate a two-dimensional matrix based on the T embedding vectors; Based on the two-dimensional matrix, at least two feature vectors are obtained through the convolutional layer in the text classification model; Based on the at least two feature vectors, the target feature vector is obtained through the pooling layer in the text classification model; Based on the target feature vector, the category probability distribution is obtained through the fully connected layer in the text classification model.

6. The generation method according to claim 1, characterized in that, The step of obtaining the category probability distribution based on the T embedding vectors through the text classification model includes: Based on the T embedding vectors, the hidden state corresponding to at least one time step is obtained through the bidirectional coding network in the text classification model; Based on the hidden state corresponding to the at least one time step, the category probability distribution is obtained through the fully connected layer in the text classification model.

7. The generation method according to claim 1, characterized in that, The T text units include a first text unit and a second text unit, wherein the first text unit and the second text unit are adjacent text units; The step of determining N causal segments from the original text sample based on the association score corresponding to each text unit includes: If the correlation score corresponding to the first text unit is greater than or equal to the correlation score threshold, then the first text unit is regarded as a text unit in the candidate causal segment. If the correlation score corresponding to the second text unit is greater than or equal to the correlation score threshold, then the second text unit is used as another text unit in the candidate causal segment; If the correlation score corresponding to the second text unit is less than the correlation score threshold, then the candidate causal segment is taken as one of the N causal segments.

8. The generation method according to any one of claims 1 to 7, characterized in that, The process of obtaining M target fragments based on the target text sample using a text generation model includes: Based on the target text sample, the encoded feature vector is obtained through the encoder in the text generation model; Based on the encoded feature vector, the probability value corresponding to each preset text unit in the dictionary at the first time step is obtained through the decoder in the text generation model; After sorting the probability values ​​corresponding to each preset text unit at the first time step from largest to smallest, the first P preset text units are selected as P first candidate text units, where P is an integer greater than or equal to 1. Based on the encoded feature vector and P first candidate text units, the probability value corresponding to each preset text unit in the dictionary at the second time step is obtained by the decoder in the text generation model, wherein the second time step is the next time step adjacent to the first time step; After sorting the probability values ​​of each preset text unit at the second time step from largest to smallest, the first P preset text units are selected as P second candidate text units. Based on the P first candidate text units and the P second candidate text units, the M target fragments are generated.

9. The generation method according to claim 8, characterized in that, The method further includes: Obtain training text samples, wherein the training text samples include Q text units, where Q is an integer greater than 1; Based on the training text samples, training feature vectors are obtained through the encoder in the text generation model; Based on the trained feature vector, the probability value corresponding to each preset text unit in the dictionary at the current time step is obtained through the decoder in the text generation model; Based on the probability value corresponding to each preset text unit at the current time step, obtain the probability value corresponding to the target text unit; The model parameters of the text generation model are updated based on the probability value corresponding to the target text unit.

10. The generation method according to any one of claims 1 to 7, characterized in that, The process of obtaining M target fragments based on the target text sample using a text generation model includes: Obtain the target category label; Based on the target text sample and the target classification label, the encoded feature vector is obtained through the encoder in the text generation model; Based on the encoded feature vector, the probability value corresponding to each preset text unit in the dictionary at the first time step is obtained through the decoder in the text generation model; After sorting the probability values ​​corresponding to each preset text unit at the first time step from largest to smallest, the first P preset text units are selected as P first candidate text units, where P is an integer greater than or equal to 1. Based on the encoded feature vector and P first candidate text units, the probability value corresponding to each preset text unit in the dictionary at the second time step is obtained by the decoder in the text generation model, wherein the second time step is the next time step adjacent to the first time step; After sorting the probability values ​​of each preset text unit at the second time step from largest to smallest, the first P preset text units are selected as P second candidate text units. Based on the P first candidate text units and the P second candidate text units, the M target fragments for the target classification label are generated.

11. The generation method according to claim 10, characterized in that, The method further includes: Obtain training text samples, wherein the training text samples have been labeled with true classification labels, and the training text samples include Q text units, where Q is an integer greater than 1; Based on the training text samples and the real classification labels, the training feature vector is obtained through the encoder in the text generation model; Based on the trained feature vector, the probability value corresponding to each preset text unit in the dictionary at the current time step is obtained through the decoder in the text generation model; Based on the probability value corresponding to each preset text unit at the current time step, obtain the probability value corresponding to the target text unit; The model parameters of the text generation model are updated based on the probability value corresponding to the target text unit, the true classification label, and the remaining classification labels, wherein the remaining classification labels are other classification labels besides the true classification labels.

12. The generation method according to claim 10, characterized in that, The step of replacing the M causal segments in the original text sample with the M target segments to obtain a counterfactual sample includes: The M causal segments in the original text sample are replaced with the M target segments to obtain candidate counterfactual samples; Based on the candidate counterfactual samples, the candidate category probability distribution of the candidate counterfactual samples is obtained through the text classification model; Based on the candidate category probability distribution, determine the candidate probability value of the candidate counterfactual sample relative to the target classification label; Candidate counterfactual samples whose candidate probability values ​​are greater than or equal to the second probability threshold are used as counterfactual samples.

13. The generation method according to claim 10, characterized in that, The process of obtaining the target category label includes: The original text sample and at least one optional category control are displayed on the sample generation interface, wherein each optional category control corresponds to a category label; In response to a selection operation on a target category control, the target category label is obtained, wherein the target category control belongs to the at least one optional category control, and the target category label is the category label corresponding to the target category control.

14. A sample generation device, characterized in that, include: The acquisition module is used to obtain the category probability distribution based on the original text sample through a text classification model, wherein the original text sample includes T text units, and T is an integer greater than 1; The determination module is used to calculate a classification loss value based on the category probability distribution and the true classification label corresponding to the original text sample using a classification loss function; calculate a first partial derivative based on the classification loss value; calculate a second partial derivative for each text unit in the original text sample based on the embedding vector corresponding to the text unit; and determine the association score corresponding to each text unit in the original text sample based on the first partial derivative and the second partial derivative corresponding to the text unit. The determining module is further configured to determine N causal segments from the original text sample based on the association score corresponding to each text unit, wherein each causal segment includes at least one text unit, and N is an integer greater than or equal to 1; The processing module is used to mask M causal segments out of the N causal segments to obtain a target text sample, wherein M is an integer greater than or equal to 1 and less than or equal to N; The acquisition module is further configured to acquire M target segments based on the target text sample using a text generation model, wherein the M target segments include M target segments, the target segments correspond to the causal segments, and the target segments and the causal segments have the same position; The processing module is further configured to replace the M causal segments in the original text sample with the M target segments to obtain a counterfactual sample.

15. The apparatus according to claim 14, characterized in that, The device further includes: The determination module is also used to determine the probability value corresponding to the true classification label based on the original text sample after obtaining the category probability distribution through the text classification model. The original text sample has been labeled as the true classification label, and each probability value in the category probability distribution corresponds to a classification label. The determination module is also used to perform the step of determining the association score corresponding to each text unit in the original text sample according to the category probability distribution if the probability value corresponding to the true classification label is greater than or equal to the first probability threshold.

16. The apparatus according to claim 14, characterized in that, The acquisition module is specifically used to segment the original text sample into text units to obtain T text units. Generate T embedding vectors from T text units, where each embedding vector corresponds to a text unit. Based on T embedding vectors, the category probability distribution is obtained through a text classification model.

17. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the generation method according to any one of claims 1 to 13.

18. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the generation method according to any one of claims 1 to 13.

19. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the generation method according to any one of claims 1 to 13.

Citation Information

Patent Citations

  • Text content derivation method, device, computer readable medium and electronic equipment

    CN113569539A

  • Adversarial text generation method and system for black box text classification model and medium

    CN113886559A