Text classification model training method and device
Variant text is generated by performing target repeated operations on the text to form positive sample pairs, and the contrastive learning method is used to train the text classification model, which solves the problems of text noise and feature sparseness and improves the training effect of the text classification model.
Patent Information
- Application Number
- CN202310331553.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-30
- Publication Date
- 2024-10-01
AI Technical Summary
In text classification, the existing technology is difficult to optimize the classification effect when faced with text noise interference and sparse features, and the contrastive learning method easily introduces noise, resulting in limited improvement in model performance.
Variant texts are generated by performing target repeated operations on sample texts to form positive sample pairs, and the text classification model is trained using contrastive learning methods to reduce the distance between positive samples and improve the model training effect.
This method increases the number of positive samples without significantly affecting the semantics of the text, improves the training efficiency and effect of the text classification model, avoids the introduction of noise, and optimizes the problems in the existing technology.
Smart Images

Figure CN118733761A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present disclosure relate to the field of natural language processing technology, and specifically to a training method and device for a text classification model and a text classification method. Background Art
[0002] Text is one of the most common forms of information expression. With the increasing amount and complexity of text data, using various methods to classify text can help to mine valuable information. Currently, text classification has strong application value in many areas such as social media data analysis, question classification in question-answering systems, and text information retrieval.
[0003] With the continuous development of deep learning technology, using deep learning technology for text classification is one of the commonly used text classification methods. Specifically, the text features rich in semantic information are extracted through neural networks, and then classification and judgment are made based on the text features. Although text classification based on deep learning technology has achieved certain results, the text classification effect needs to be further optimized or improved in some specific scenarios. For example, text noise interference and sparse text features will affect the text classification effect. Summary of the invention
[0004] The embodiments of the present disclosure provide a method and device for training a text classification model.
[0005] In a first aspect, an embodiment of the present disclosure provides a method for training a text classification model, the method comprising: obtaining a sample text set; for a text in the sample text set, performing a target repetition operation on the text to obtain a variant text corresponding to the text, and using the text and the corresponding variant text to form a positive sample text pair; using the positive sample text pairs corresponding to the text in the sample text set, based on a contrastive learning method, training an initial text classification model to obtain a trained text classification model.
[0006] In a second aspect, an embodiment of the present disclosure provides a text classification method, which includes: obtaining a text to be processed; inputting the text to be processed into a pre-trained text classification model to obtain a text classification result, wherein the text classification model is trained using the method described in the first aspect above.
[0007] In a third aspect, an embodiment of the present disclosure provides a training device for a text classification model, the device comprising: a sample acquisition unit, configured to acquire a sample text set; a repetition unit, configured to perform a target repetition operation on the text in the sample text set to obtain a variant text corresponding to the text, and to use the text and the corresponding variant text to form a positive sample text pair; a training unit, configured to use the positive sample text pairs corresponding to the text in the sample text set to train an initial text classification model based on a contrastive learning method to obtain a trained text classification model.
[0008] In a fourth aspect, an embodiment of the present disclosure provides a text classification device, comprising: a text acquisition unit, configured to acquire a text to be processed; a classification unit, configured to input the text to be processed into a pre-trained text classification model to obtain a text classification result, wherein the text classification model is trained using the method described in the first aspect above.
[0009] In a fifth aspect, an embodiment of the present disclosure provides an electronic device, comprising: one or more processors; a storage device for storing one or more programs; when the one or more programs are executed by the one or more processors, the one or more processors implement the method described in the first aspect.
[0010] In a sixth aspect, an embodiment of the present disclosure provides a computer-readable medium having a computer program stored thereon, which implements the method described in the first aspect when executed by a processor.
[0011] The training method and device of the text classification model provided by the embodiments of the present disclosure perform target repetitive operations on sample texts to obtain corresponding variant texts, and form the sample text and its corresponding variant text into a positive sample text pair, and use the contrastive learning method to train the text classification model. While increasing the positive samples, since the repeated operations on the characters of the text generally do not have a significant impact on the semantics of the text, the semantic similarity of the two texts in the positive sample text pair is ensured, that is, the distance between the positive samples is shortened, thereby improving the training effect of the text classification model. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] Other features, objects and advantages of the present disclosure will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:
[0013] Figure 1 is an exemplary system architecture diagram in which an embodiment of the present disclosure may be applied;
[0014] Figure 2 is a flowchart of an embodiment of a method for training a text classification model according to the present disclosure;
[0015] Figure 3 It is a schematic diagram of the target of contrastive learning;
[0016] Figure 4 This is a diagram of the training process of a standard neural network and a neural network with Dropout applied;
[0017] Figure 5 It is a schematic diagram of the process of forming contrast loss based on momentum contrast;
[0018] Figure 6 is a structural schematic diagram of a text classification model according to the present disclosure;
[0019] Figure 7 is a flowchart of an embodiment of a text classification method according to the present disclosure;
[0020] Figure 8 This is a schematic diagram of the application scenario of text classification;
[0021] Fig. 9 is a structural schematic diagram of an embodiment of a training device for a text classification model according to the present disclosure;
[0022] Fig.10 is a structural schematic diagram of an embodiment of a text classification device according to the present disclosure;
[0023] Fig.11 It is a schematic diagram of the structure of an electronic device suitable for implementing the embodiments of the present disclosure. DETAILED DESCRIPTION
[0024] The present disclosure is further described in detail below in conjunction with the accompanying drawings and embodiments. It is understood that the specific embodiments described herein are only used to explain the relevant invention, rather than to limit the invention. It is also necessary to explain that, for ease of description, only the parts related to the relevant invention are shown in the accompanying drawings.
[0025] It should be noted that in the technical solution of the present invention, the collection / collection, update, analysis, use, transmission, storage and other aspects of user personal information involved are in compliance with the provisions of relevant laws and regulations, are used for legal and reasonable purposes, are not shared, leaked or sold outside of these legal uses, and are subject to supervision and management by national regulatory authorities. Necessary measures should be taken for user personal information to selectively block the use or access to personal information data to prevent illegal access to such personal information data, ensure that persons who have access to personal information data comply with the provisions of relevant laws and regulations, and ensure the security of user personal information. In addition, once these user personal information data are no longer needed, the risks should be minimized by limiting or even prohibiting data collection and / or deleting data.
[0026] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present disclosure may be combined with each other. The present disclosure will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0027] Figure 1 An exemplary architecture 100 of an embodiment of a text classification model training method or a text classification model training apparatus of the present disclosure is shown.
[0028] like Figure 1 As shown, the system architecture 100 may include terminal devices 101, 102, 103, a network 104 and a server 105. The network 104 is used to provide a medium for communication links between the terminal devices 101, 102, 103 and the server 105. The network 104 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0029] The terminal devices 101, 102, 103 interact with the server 105 through the network 104 to receive or send messages, etc. Various client applications may be installed on the terminal devices 101, 102, 103. For example, browser applications, search applications, shopping applications, instant messaging tools, information flow applications, etc.
[0030] Terminal devices 101, 102, 103 can be hardware or software. When terminal devices 101, 102, 103 are hardware, they can be various electronic devices with display screens, including but not limited to smart phones, tablet computers, e-book readers, laptop portable computers, desktop computers, etc. When terminal devices 101, 102, 103 are software, they can be installed in the electronic devices listed above. It can be implemented as multiple software or software modules (for example, multiple software or software modules for providing distributed services), or it can be implemented as a single software or software module. No specific limitation is made here.
[0031] The server 105 may be a server that provides various services, such as a backend server that provides support for various applications installed on the terminal devices 101, 102, and 103. The server 105 may obtain a sample text set from the terminal devices 101, 102, and 103, etc., perform a target repetition operation on the text in the obtained sample text set to obtain a variant text corresponding to the text, and form a positive sample text pair with the text and the corresponding variant text, and use the positive sample text pair to train a text classification model based on a contrastive learning method.
[0032] It should be noted that the above sample text set may also be directly stored locally on the server 105 , and the server 105 may directly extract and process the locally stored sample text set. In this case, the terminal devices 101 , 102 , 103 and the network 104 may not exist.
[0033] It should be noted that the training method of the text classification model provided in the embodiments of the present disclosure is generally executed by the server 105 , and accordingly, the training device of the text classification model is generally set in the server 105 .
[0034] It should also be noted that a model training application may also be installed in the terminal devices 101, 102, and 103, and the terminal devices 101, 102, and 103 may also train a text classification model based on the model training application. In this case, the training method of the text classification model may also be executed by the terminal devices 101, 102, and 103, and accordingly, the training device of the text classification model may also be set in the terminal devices 101, 102, and 103. In this case, the exemplary system architecture 100 may not have the server 105 and the network 104.
[0035] It should be noted that the server 105 can be hardware or software. When the server is hardware, it can be implemented as a distributed server cluster consisting of multiple servers, or it can be implemented as a single server. When the server is software, it can be implemented as multiple software or software modules (for example, multiple software or software modules for providing distributed services), or it can be implemented as a single software or software module. No specific limitation is made here.
[0036] It should be understood that Figure 1 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0037] Continue to refer Figure 2 , which shows a process 200 of an embodiment of a text classification model training method according to the present disclosure. The text classification model training method includes the following steps:
[0038] Step 201: Obtain a sample text set.
[0039] In this embodiment, the sample text set may be composed of a number of texts, and the texts therein may be used as samples for training a text classification model. The text may be text of any content. For example, comments, search information, email content, etc. The text classification model may be used to implement text classification, i.e., determine the category to which the text belongs.
[0040] The execution subject of the training method of the text classification model (such as Figure 1The server 105 shown, etc.) can obtain the sample text set from a local or other storage device, data source, etc.
[0041] Step 202: For the text in the sample text set, perform a target repetition operation on the text to obtain a variant text corresponding to the text, and use the sample and the corresponding variant text to form a positive sample text pair.
[0042] In this embodiment, the target repeat operation may refer to various operations for repeating text, which may be flexibly set according to actual application scenarios or application requirements. For example, the target repeat operation may indicate which words in the text are repeated, how to repeat, the number of times to repeat, and so on.
[0043] As an example, the target repetition operation may include first segmenting the text, then tagging each segmented word, and obtaining the part of speech of each segmented word. Then, a character in a specified part of speech (such as pronoun, adverb, conjunction, interjection, quantifier, adjective, preposition, auxiliary word, onomatopoeia, etc.) may be selected as a target character, and then the target character may be repeated, or the word in which each target character is located may be repeated.
[0044] Taking a specific example as an illustration, the text is "I like this apple". Through word segmentation and part-of-speech tagging, we can select the pronoun "I" as the target word for repeated operations, and the resulting variant text can be "I like this apple" or "I I like this apple".
[0045] For each text in the sample text set, after obtaining the variant text corresponding to the text, the sample and its corresponding variant text can be combined into a positive sample text pair, thereby obtaining positive sample text pairs corresponding to each text.
[0046] Step 203: Using the positive sample text pairs corresponding to the texts in the sample text set, based on the contrastive learning method, the initial text classification model is trained to obtain a trained text classification model.
[0047] In this embodiment, contrastive learning is a self-supervised learning method that does not require manually annotated category label information and directly uses the sample data itself as supervisory information for model training. Generally, contrastive learning compares sample data with positive samples and negative samples in feature space to learn more discriminative and contrastive feature expressions of sample data. The goal of contrastive learning can be expressed as reducing the distance between positive samples and expanding the distance between positive samples and negative samples to obtain the true distribution distance of positive samples and negative samples in the original space.
[0048] Reference Figure See Figure 3As shown in FIG, it shows a schematic diagram of the target of contrastive learning. Figure 3 As shown, the goal of contrastive learning may be to reduce the distance 301 between positive samples in the “XYZ” three-dimensional space, and to increase the distance 302 between positive samples and negative samples.
[0049] The goal of contrastive learning can also be briefly expressed by the following formula:
[0050] score(f(x),f(x) + ))>>score(f(x),f(x - ))
[0051] Among them, "score()" can represent similarity. "f()" represents the feature vector. "x" and "x + " is a positive sample text. "x - " is the negative sample text. ">>" means much greater than. The above formula can mean that the similarity between the feature vectors of positive samples is much greater than the similarity between the feature vectors of positive samples and negative samples.
[0052] The text classification model can be various neural network models for implementing text classification. Specifically, the structure of the text classification model can be preset by a technician. For example, the initial text classification model can be constructed by referring to the structural design of various existing text classification models.
[0053] After obtaining the positive sample text pairs corresponding to each text in the sample text set, the initial text classification model can be trained using a contrastive learning method based on an existing machine learning method to obtain a trained text classification model.
[0054] Generally, when training the initial text classification model, several negative sample texts can also be obtained, and the positive sample texts and the negative sample texts form a negative sample text pair, and then the positive sample text pair and the negative sample text pair are used to train the initial text classification model. Among them, the negative sample text pair can be constructed using various methods. For example, any two texts with low repetition words can form a negative sample text pair.
[0055] The focus of contrastive learning methods usually includes how to construct positive sample pairs and negative sample pairs. The method provided by the above-mentioned embodiment of the present disclosure forms variant texts by performing target repeated operations on texts, and the variant texts and the texts form positive sample pairs. Since performing repeated operations on texts has little impact on the semantics of the texts, it can also be said that the semantic similarity between the variant texts after repeated operations on the texts and the original texts is usually very high, thereby realizing a low-cost and convenient method for constructing positive sample pairs, and enabling the text classification model to learn to narrow the distance between positive samples, thereby improving the training efficiency and training effect of the text classification model.
[0056] Optionally, the target repetition operation may include: copying the entire text to obtain a copied text corresponding to the text, and then concatenating the text and the corresponding copied text. Wherein, for any text, the copied text of the text is exactly the same as the text. Concatenation generally may refer to sequential concatenation.
[0057] As an example, if the text is "I like this apple", the copied text of this text can be "I like this text", and the variant text obtained by concatenating this text and the copied text can be "I like this appleI like this apple".
[0058] By copying the entire text and concatenating the copied text and the original text, a relatively convenient repetition operation can be achieved without affecting the semantics of the original text.
[0059] Optionally, the target repetition operation may include: selecting a character from the text as the target character, and then adding the target character at the target position of the text.
[0060] Specifically, various methods can be used to select a character from the text as the target character, which can be specifically set by technicians according to actual needs. For example, a character can be randomly selected from the text as the target character. Another example is that a character can be selected from the text according to the grammatical structure of the text (such as selecting the character corresponding to the subject or predicate or object as the target character).
[0061] It should be noted that the number of characters selected from the text as the target character can be one, or more than one, etc.
[0062] Optionally, the random repetition count of the text can be determined as the number of target characters. The random repetition count can represent the number of characters involved in the target repetition operation performed on the text. For example, if the random repetition count for the text "I like this apple" is 2, it means that the repetition operation can be performed on two characters in this text. If the two randomly selected characters are "I" and "like", then the variant text obtained after performing the repetition operation on the two characters "I" and "like" can be "I I like like this apple".
[0063] Optionally, the random repetition count of the text can be determined according to a preset maximum repetition rate and the total number of characters included in the text. Wherein, the maximum repetition rate can be preset by technicians. The total number of characters included in the text can be obtained by methods such as word segmentation or statistics. Generally, the maximum repetition count can be calculated by multiplying the maximum repetition rate by the total number of characters included in the text. The maximum repetition count can represent the maximum number of characters that can be repeated in the text. Then, a number not greater than the maximum repetition count can be randomly generated as the random repetition count. Specifically, the following formula can be used to represent the maximum repetition count:
[0064] dup len ∈[0, max(int(dup rate *N))]
[0065] Among them, "dup len " indicates the maximum number of repeated words. "max()" indicates the maximum value. "int()" indicates an integer. "dup rate " indicates the number of random repeated words. "N" indicates the total number of words contained in the text.
[0066] Optionally, after determining the number of randomly repeated characters, characters to be repeated may be randomly selected from the character sequence corresponding to the text according to uniform distribution to obtain a repeated character sequence. The uniform distribution may refer to that the positions of the characters to be repeated randomly selected from the character sequence corresponding to the text in the subsequence conform to the uniform distribution. Specifically, the repeated character sequence may be represented by the following formula:
[0067] dup set =uniform(range=[1, N], num=dup len )
[0068] Among them, "dup set " indicates a repeated word sequence. "uniform()" indicates a randomly generated real number. "range()" indicates the range of randomly generated real numbers. "N" indicates the total number of words contained in the text, that is, the total number of words contained in the subsequence. "num" indicates the number of randomly generated real numbers. "dup len " indicates the maximum number of repeated words.
[0069] By randomly selecting words from the text and repeating them according to the number of random repeated words, more randomness can be introduced when generating variant texts, thereby improving the richness of positive sample pairs.
[0070] The target position for adding the target word can also be flexibly set by the technician according to actual needs. For example, the target position can be a randomly selected position. For another example, the target position can be a position within a certain number of words before and after the word corresponding to the target word in the original text. As an example, the target position can include the position before or after the word corresponding to the target word in the original text.
[0071] By selecting words from the text and repeating them, variant texts can be obtained through fewer repetitive operations without causing too much impact on the semantics of the text.
[0072] Optionally, a word may be selected from the text as a target word by the following steps:
[0073] Step 1: Segment the text to obtain the word sequence corresponding to the text.
[0074] In this step, various existing word segmentation methods may be used to segment the text to obtain a word sequence corresponding to the text.
[0075] Step 2: Determine whether there is a word consisting of only a single character in the word sequence.
[0076] In this step, generally, the words in the word sequence may include words formed by a single character, or may include words formed by more than two characters. Taking "I like apples" as an example, the corresponding word sequence may include {I, like, apples}. Among them, "I" is a word that only includes a single character. "Like" and "apple" are words that do not only include a single character. Specifically, it is possible to determine whether there is a word that only includes a single character in the word sequence by counting the number of characters included in each word segment.
[0077] Step three: in response to determining that there is a word including only a single character in the word sequence, determine the single character as a target character.
[0078] In this step, if there is a word including only a single character in the word sequence, this single character can be selected as the target character. Taking the above-mentioned "I like apples" as an example, "I" can be determined as the target character.
[0079] Since the words formed by single characters have clear meanings, repeating these single characters usually does not affect the semantics of the original text, so repeating these single characters to obtain variant texts can ensure that the semantics of the variant texts are similar to those of the original text.
[0080] Optionally, you can also select a word from the text as the target word by the following steps:
[0081] Step 1: Determine whether there is a word containing reduplication in the word sequence.
[0082] In this step, reduplication, also known as tautology, generally refers to a phrase consisting of two identical characters or words. For example, the word sequence corresponding to the text "I was deeply moved" may include {I, being, deeply, moved,}, where "deeply" is a word including the reduplication "deeply".
[0083] For another example, the word sequence corresponding to the text "looks clean and tidy" includes {looks, clean and tidy}, in which "clean and tidy" is the word that includes the reduplication of "clean and tidy".
[0084] Step 2: In response to determining that there is a word including a reduplication in the word sequence, determine the character in the reduplication as the target character.
[0085] In this step, if it is determined that there are words including reduplication in the word sequence, the words in the reduplication can be directly determined as target words. Taking the above text "I was deeply moved" and "looks clean" as examples, "deep", "dry" and "clean" can be determined as target words.
[0086] Since reduplication itself is usually composed of repeated characters, repeating the repeated characters will hardly affect the semantics of the original text, thus ensuring that the semantics of the variant text is similar to that of the original text.
[0087] In some optional implementations of this embodiment, the text classification model may include an encoding network and a classification network. The encoding network may be used to generate a text vector corresponding to the text. The text vector may be used to represent the features of the text. The classification network may be used to determine the category to which the text belongs based on the text vector.
[0088] The specific network structure of the encoding network can be set by a technician. For example, the encoding network can be constructed based on the structural design of various existing feature extraction networks. As an example, the encoding network can be constructed based on RoBERTa (A Robustly Optimized BERT Pretraining Approach).
[0089] The specific network structure of the classification network can be set by a technician. For example, various existing classification networks can be referenced. The various categories of the text can be preset by the technician according to the actual application scenario. The classification network can output the category to which the specified text belongs in these preset categories.
[0090] Optionally, the classification network can be constructed based on a deep convolutional neural network, for example, based on a double-layer convolutional structure, etc.
[0091] Optionally, the text classification model can be trained based on the Dropout technique. Figure 4 As shown in FIG. 1 , a schematic diagram of the training process of a standard neural network and a neural network with Dropout applied is shown. Dropout technology usually refers to the process of training a neural network. For a certain layer of a neural network in one iteration, some neurons can be randomly selected for temporary discard (or abnormality) before training and optimization. In the next iteration, some neurons are randomly hidden until the training is completed.
[0092] In existing contrastive learning, for text classification tasks, data enhancement methods such as synonym replacement, random insertion, word order rearrangement, etc. are usually used to construct positive sample pairs. However, these methods are prone to introduce noise and may also distort the semantics of the text, so that the training model using positive sample pairs constructed in this way has limited improvement on the model's performance.
[0093] In addition, there are some methods based on the Dropout strategy, which input the same text into the encoding network in the model twice. Since Dropout will randomly stop some neurons from working with a certain probability, two feature vectors of the text can be obtained, and then the model can be trained by using the distance between the two feature vectors as the distance between the positive sample pairs. However, for some models that encode the feature vector of the text by position embedding, the lengths of the two feature vectors obtained by encoding the same text based on Dropout are usually the same, while the lengths of the two feature vectors obtained by encoding different texts may be different. As a result, the encoding network may learn to use the length of the feature vector as a feature to distinguish between positive and negative samples, and thus believe that texts with the same or similar lengths of corresponding feature vectors are more similar. However, in reality, the length of the corresponding feature vector of the text has no direct correlation with the semantics of the text, which leads to training bias in the encoding network.
[0094] To address this problem, the prior art usually controls the length of text by random insertion and random deletion, thereby controlling the length of the text feature vector. However, random insertion may introduce additional noise and thus distort the semantics of the text, and random deletion may also greatly distort the semantics of the text.
[0095] In response to the above-mentioned various existing problems, the present invention repeats the words in the text based on the Dropout strategy and uses this to construct positive sample pairs. Since the repetition of individual words in the text usually has little effect on the semantics of the text and also makes the length of the text very flexible, this method can avoid the above-mentioned problems of adding additional noise to the text, distorting the semantics of the text, and causing the encoding network to use length as a feature, thereby improving the training effect of the text classification model.
[0096] As an example, the original text is "I like this apple, it looks fresh, I think it should be delicious."
[0097] The text obtained after random insertion may be "I don't like this apple, it looks very fresh and I think it should be delicious."
[0098] After random deletion, the resulting text might be "This apple of mine looks very good, and I think it should be delicious."
[0099] The variant text obtained by adopting the method based on word repetition of the present invention may be "I like this apple, it looks very fresh, I think it should be delicious", "I like this apple, it looks very fresh, I think it should be delicious".
[0100] It can be seen from the above examples that the variant text obtained by random insertion and random deletion may add additional noise and may also distort the semantics of the original text. However, the variant text obtained by the method of the present disclosure has basically the same semantics as the original text. Therefore, the method provided by the above embodiment of the present disclosure proposes a new method for constructing positive sample pairs based on word repetition, which is completely different from the existing method for constructing positive sample pairs based on Dropout technology, and this method can also optimize some problems existing in the above-mentioned prior art.
[0101] In some optional implementations of this embodiment, the text classification model can also be trained based on the momentum contrast technology. Specifically, momentum contrast usually refers to maintaining a fixed-size queue by building a dynamic dictionary with a queue and an average moving encoder, and reusing the encoding embedding from the previous mini-batch sample (MINI-BATCH) to expand the negative sample pairs. The queue always embeds the sentence of the current MINI-BATCH into the queue, and embeds the oldest sentence at the front of the queue out of the queue. Since the sentence embeddings entering the queue come from the previous MINI-BATCH, the momentum update model is maintained by taking the moving average of its parameters, and the momentum model is used to generate the sentence embeddings entering the queue. More negative sample features can be learned based on the continuous updating of the queue.
[0102] like Figure 5 As shown, it shows a schematic diagram of the process of forming contrast loss based on momentum contrast. Negative sample (x0 key , x1 key , x2 key , ...) are encoded by a slowly advancing momentum encoder and then queued. The dictionary {k0, k1, k2, ...} is dynamically defined by a set of data samples. The dictionary is constructed as a queue. When the MINI-BATCH is reached, the sample at the front of the queue is dequeued. The queue always maintains a fixed length. The encoder parameter "θ e ” and the momentum encoder parameter “θ m " can be updated based on the following formula:
[0103] θ m ←λθ m +(1-λ)θ e
[0104] Among them, "λ" represents the momentum coefficient, which can be in the range of [0, 1). The parameters of the encoder are updated through back-propagation, and the parameters of the momentum encoder are introduced to generate sentence embeddings for the queue.
[0105] In the prior art, in an unsupervised training scenario, for each text, many texts other than the text itself can be used as negative samples, so that the number of negative samples is usually sufficient. However, using more negative samples for model training requires increasing the number of data samples captured by the model for each training (i.e., BATCH-SIZE). Generally, due to hardware limitations, it is usually impossible to increase BATCH-SIZE indefinitely, and an increase in BATCH-SIZE will also weaken the generalization ability of the model.
[0106] To address this problem, the present invention enables a text classification model to learn more negative sample features during training by using momentum comparison, without increasing BATCH-SIZE, thereby solving the problems existing in the above-mentioned prior art.
[0107] Continue to refer Figure 6 , which shows the structural diagram of the text classification model. Figure 6 As shown in Figure 2, the text classification model can construct positive sample pairs based on contrastive learning. The input of the text classification model can be a word sequence corresponding to the text (such as "W0, W1, ..., W n-1 , W n Specifically, the word sequence corresponding to the text is input into the encoding network built based on RoBERTa to obtain a deep bidirectional feature representation, that is, the feature vector corresponding to the text is obtained. The feature vector of the text is then input into a two-layer CNN (Convolutional Neural Network) to integrate local information. At the same time, the convolution kernel is used to capture information of different lengths in the text. At the same time, the Dropout mechanism is added to make the model parameter update not only rely on some features, so as to avoid overfitting and other situations. Finally, the final classification result of the text is obtained through SoftMax classification.
[0108] The method provided by the above-mentioned embodiment of the present disclosure proposes a method for constructing positive sample pairs for contrastive learning based on the idea of word repetition, and then training a text classification model. Using the idea of word repetition to construct positive sample pairs for contrastive learning will not have a significant impact on the semantics of the text, nor will it add too much additional noise, and it can also make the length of the text change more flexible, etc., solving some problems existing in the existing contrastive learning-based text classification model training, thereby improving the training effect of the text classification model.
[0109] Further references Figure 7, which shows a process 700 of an embodiment of a text classification method. The process 700 of the text classification method includes the following steps:
[0110] Step 701, obtaining the text to be processed.
[0111] In this embodiment, the text to be processed may be any text, for example, comment text, email content, news content, search text, etc.
[0112] Specifically, the execution subject of the text classification method can adopt various methods to obtain the text to be processed from local or other storage devices, data sources, etc.
[0113] It should be noted that the execution subject of the text classification method and the execution subject of the training method of the above-mentioned text classification model may be the same as or different from each other.
[0114] Step 702: input the text to be processed into a pre-trained text classification model to obtain a text classification result.
[0115] In this embodiment, the text classification model can be used as described above Figure 2 The method described in the embodiment is used for training. The text to be processed is processed using the text classification model to obtain a text classification result corresponding to the text to be processed.
[0116] In different application scenarios, the text classification results can be flexibly changed. Figure 8 The diagram of application scenarios of text classification shown in the figure shows that, according to different needs, text classification can be applied to spam filtering, personalized news recommendation, question classification in question-answering systems, text information retrieval, microblog topic analysis, online public opinion monitoring, online comment sentiment analysis, social media data analysis, etc.
[0117] The method provided by the above-mentioned embodiment of the present disclosure performs text classification on the text to be processed through the above-mentioned pre-trained text classification model, and can obtain more accurate text classification results, thereby improving the application effect of text classification in various application scenarios.
[0118] Further references Fig. 9 As an implementation of the methods shown in the above figures, the present disclosure provides an embodiment of a training device for a text classification model. Figure 2 Corresponding to the method embodiment shown, the device can be specifically applied to various electronic devices.
[0119] like Fig. 9As shown, the training device 900 of the text classification model provided in this embodiment includes a sample acquisition unit 901, a repetition unit 902 and a training unit 903. The sample acquisition unit 901 is configured to acquire a sample text set; the repetition unit 902 is configured to perform a target repetition operation on the text in the sample text set to obtain a variant text corresponding to the text, and to form a positive sample text pair using the text and the corresponding variant text; the training unit 903 is configured to train the initial text classification model based on the contrastive learning method using the positive sample text pairs corresponding to the text in the sample text set to obtain a trained text classification model.
[0120] In the text classification model training device 900, the specific processing of the sample acquisition unit 901, the repetition unit 902 and the training unit 903 and the technical effects thereof can be referred to in Figure 2 The relevant descriptions of step 201, step 202 and step 203 in the corresponding embodiment are not repeated here.
[0121] In some optional implementations of this embodiment, the above-mentioned target repetition operation includes: copying the entire text to obtain a copied text corresponding to the text; and splicing the text and the corresponding copied text.
[0122] In some optional implementations of this embodiment, the target repetition operation includes: selecting a character from a text as a target character; and adding the target character at a target position of the text.
[0123] In some optional implementations of this embodiment, the target position includes a previous position and / or a next position of the target word in the text.
[0124] In some optional implementations of the present embodiment, the above-mentioned target repetition operation includes: segmenting the text to obtain a word sequence corresponding to the text; determining whether there is a word consisting of only a single character in the word sequence; in response to determining that there is a word consisting of only a single character in the word sequence, determining the single character as the target character.
[0125] In some optional implementations of this embodiment, the above-mentioned target repetition operation includes: determining whether there is a word including a reduplication in the word sequence; in response to determining that there is a word including a reduplication in the word sequence, determining the character in the reduplication as the target character.
[0126] In some optional implementations of this embodiment, the target repetition operation includes: determining the number of randomly repeated words in the text as the number of target words.
[0127] In some optional implementations of the present embodiment, the above-mentioned text classification model includes an encoding network and a classification network, wherein the encoding network is used to generate a text vector corresponding to the text, and the classification network is used to determine the category to which the corresponding text belongs based on the text vector. The text classification model is trained based on the Dropout technology and the momentum comparison technology, and the classification network is constructed using a deep convolutional neural network.
[0128] The device provided by the above-mentioned embodiment of the present disclosure obtains a sample text set through a sample acquisition unit; the repetition unit performs a target repetition operation on the text in the sample text set to obtain a variant text corresponding to the text, and uses the text and the corresponding variant text to form a positive sample text pair; the training unit uses the positive sample text pairs corresponding to the text in the sample text set to train the initial text classification model based on the contrastive learning method to obtain a trained text classification model. Since repeating the words of the text generally does not have a major impact on the semantics of the text, the semantic similarity of the two texts in the positive sample text pair is guaranteed, that is, the distance between the positive samples is shortened, thereby improving the training effect of the text classification model.
[0129] Further references Fig.10 As an implementation of the methods shown in the above figures, the present disclosure provides an embodiment of a text classification device. Figure 8 Corresponding to the method embodiment shown, the device can be specifically applied to various electronic devices.
[0130] like Figure 8 As shown, the text classification device 1000 provided in this embodiment includes a text acquisition unit 1001 and a classification unit 1002. The text acquisition unit 1001 is configured to acquire the text to be processed; the classification unit 1002 is configured to input the text to be processed into a pre-trained text classification model to obtain a text classification result, wherein the text classification model uses the following Figure 2 The method described in the embodiment is trained.
[0131] In this embodiment, in the text classification device 1000, the specific processing of the text acquisition unit 1001 and the classification unit 1002 and the technical effects thereof can be referred to in Figure 2 The relevant descriptions of step 801 and step 802 in the corresponding embodiment are not repeated here.
[0132] The apparatus provided by the above-mentioned embodiment of the present disclosure obtains the text to be processed through the text acquisition unit; the classification unit inputs the text to be processed into a pre-trained text classification model to obtain a text classification result, wherein the text classification model uses Figure 8The method described in the embodiment is trained to obtain relatively accurate text classification results, thereby improving the application effect of text classification in various application scenarios.
[0133] Reference below Fig.11 , which shows an electronic device (eg, Figure 1 The terminal device in the embodiment of the present disclosure may include but is not limited to mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Fig.11 The server shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0134] like Fig.11 As shown, the electronic device 1100 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 1101, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1102 or a program loaded from a storage device 1108 to a random access memory (RAM) 1103. In the RAM 1103, various programs and data required for the operation of the electronic device 1100 are also stored. The processing device 1101, the ROM 1102, and the RAM 1103 are connected to each other via a bus 1104. An input / output (I / O) interface 1105 is also connected to the bus 1104.
[0135] Typically, the following devices may be connected to the I / O interface 1105: an input device 1106 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 1107 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 1108 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 1109. The communication device 1109 may allow the electronic device 1100 to communicate with other devices wirelessly or by wire to exchange data. Although Fig.11 The electronic device 1100 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead. Fig.11 Each block shown in the figure may represent one device, or may represent multiple devices as required.
[0136] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 1109, or installed from a storage device 1108, or installed from a ROM 1102. When the computer program is executed by the processing device 1101, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.
[0137] It should be noted that the computer-readable medium described in the embodiments of the present disclosure may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the embodiments of the present disclosure, the computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by or in combination with an instruction execution system, device or device. In the embodiments of the present disclosure, the computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.
[0138] The computer-readable medium may be included in the electronic device; or it may exist independently without being installed in the electronic device. The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: obtains a sample text set; performs a target repetitive operation on the text in the sample text set to obtain a variant text corresponding to the text, and uses the text and the corresponding variant text to form a positive sample text pair; uses the positive sample text pairs corresponding to the text in the sample text set to train the initial text classification model based on the contrastive learning method to obtain a trained text classification model.
[0139] Computer program code for performing the operations of embodiments of the present disclosure may be written in one or more programming languages or a combination thereof, including object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on a user's computer, partially on a user's computer, as a separate software package, partially on a user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer via any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0140] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0141] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware. The units described may also be arranged in a processor, for example, may be described as: a processor includes a sample acquisition unit, a repetition unit, and a training unit. The names of these units do not constitute limitations on the units themselves in certain circumstances, for example, the sample acquisition unit may also be described as a "unit for acquiring a sample text set".
[0142] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of the invention involved in the embodiments of the present disclosure is not limited to the technical solutions formed by a specific combination of the above-mentioned technical features, but should also cover other technical solutions formed by any combination of the above-mentioned technical features or their equivalent features without departing from the above-mentioned inventive concept. For example, the above-mentioned features are replaced with the technical features with similar functions disclosed in the embodiments of the present disclosure (but not limited to) to form a technical solution.
Claims
1. A training method for a text classification model, comprising: Get a sample text set; For a text in the sample text set, performing a target repetition operation on the text to obtain a variant text corresponding to the text, and using the text and the corresponding variant text to form a positive sample text pair; The initial text classification model is trained based on a contrastive learning method using positive sample text pairs corresponding to the texts in the sample text set to obtain a trained text classification model.
2. The method according to claim 1, wherein: The target repetition operation includes: Copy the entire text to obtain the copied text corresponding to the text; Concatenate this text with the corresponding copied text.
3. The method according to claim 1, wherein: The target repetition operation includes: Selecting words from the text as target words; The target word is added at the target position of the text.
4. The method according to claim 3, wherein: The target position includes a previous position and / or a next position of the target word in the text.
5. The method according to claim 3, wherein: The step of selecting a word from the text as a target word comprises: Segment the text to obtain the word sequence corresponding to the text; Determining whether there is a word including only a single character in the word sequence; In response to determining that a word including only a single character exists in the word sequence, the single character is determined as a target character.
6. The method according to claim 5, wherein: The step of selecting a word from the text as a target word further includes: Determining whether there is a word including reduplication in the word sequence; In response to determining that a word including a reduplication exists in the word sequence, a character in the reduplication is determined as a target character.
7. The method according to claim 3, wherein: The target repetition operation also includes: The number of randomly repeated words in the text is determined as the number of target words.
8. The method according to any one of claims 1 to 7, wherein: The text classification model includes an encoding network and a classification network, wherein the encoding network is used to generate a text vector corresponding to a text, and the classification network is used to determine the category to which the corresponding text belongs according to the text vector. The text classification model is trained based on the Dropout technology and the momentum comparison technology, and the classification network is constructed using a deep convolutional neural network.
9. A method for classifying text, comprising: Get the text to be processed; The text to be processed is input into a pre-trained text classification model to obtain a text classification result, wherein the text classification model is trained using the method described in any one of claims 1-8.
10. A training device for a text classification model, wherein: The device comprises: A sample acquisition unit, configured to acquire a sample text set; a repeating unit configured to perform a target repeating operation on a text in the sample text set to obtain a variant text corresponding to the text, and to form a positive sample text pair using the text and the corresponding variant text; The training unit is configured to train the initial text classification model based on a contrastive learning method using positive sample text pairs corresponding to the texts in the sample text set to obtain a trained text classification model.
11. A text classification device, comprising: A text acquisition unit, configured to acquire a text to be processed; The classification unit is configured to input the text to be processed into a pre-trained text classification model to obtain a text classification result, wherein the text classification model is trained using the method described in any one of claims 1-8.
12. An electronic device comprising: one or more processors; a storage device having one or more programs stored thereon; When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 9.
13. A computer readable medium having a computer program stored thereon, wherein: When the program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.
Citation Information
Patent Citations
Text-based sentiment classification method and device, computer equipment and storage medium
CN114358201A
Contrast learning sample construction method and device, computer equipment and storage medium
CN115062607A
Log anomaly detection method, device and system and medium
CN115834195A
Text classification model training method and device
CN115858786A
Cited By
Full-text retrieval enhancement method, system and equipment for hydraulic power plant knowledge and medium
CN120104777A
