Two-stage text classification method, device, storage medium and electronic device
Through the two-stage text classification method, semantic model and pellet model technology are used to solve the classification problem of complex labels and highly specialized content in agricultural information text, and the classification accuracy and generalization ability are improved.
Patent Information
- Application Number
- CN202411442285.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-16
- Publication Date
- 2025-03-21
- Estimated Expiration
- 2044-10-16
AI Technical Summary
Existing text classification methods are difficult to effectively map complex labels and highly specialized content semantic information when processing agricultural information texts, resulting in low classification accuracy.
The two-stage text classification method is adopted, and the data set is first vectorized through the semantic model, and the first-stage particle sphere model is constructed to obtain the real information of the semantic labels, and then the second-stage classification model is trained using this information and word vector sequence.
Improve the accuracy and generalization ability of text classification, especially when dealing with complex and highly specialized agricultural information texts, it can more effectively map semantic information to the markup space.
Smart Images

Figure CN118966238B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of text classification, and in particular to a two-stage text classification method, device, storage medium and electronic device. Background Art
[0002] Agricultural information texts involve complex, diverse and highly specialized industry issues. It is becoming increasingly difficult to quickly retrieve agricultural information from massive amounts of agricultural information texts.
[0003] Most of the existing text classification methods use a single pre-trained language model, which needs to be trained on a large amount of text data. Moreover, agricultural information text involves multiple industrial fields, so complex labels that lack tag information are difficult to map to the corresponding tag space under conventional methods. In addition, due to the high degree of specialization of agricultural information text content, it is difficult for ordinary word vectors to fully express the semantic information of the text. Therefore, this puts higher requirements on the classification method. Summary of the invention
[0004] In view of the deficiencies in the prior art, the purpose of the present invention is to provide a two-stage text classification method, device, storage medium and electronic device, aiming to solve the technical problems mentioned in the background technology.
[0005] In order to achieve the above object, the present invention is implemented by the following technical solutions:
[0006] A two-stage text classification method includes the following steps:
[0007] S10, obtaining a data set for training, and vectorizing the data set through a semantic model to obtain a word vector sequence of the data set;
[0008] S20, using the word vector sequence training to construct a first-stage granular ball model, wherein the first-stage granular ball model has a plurality of semantic labels;
[0009] S30, acquiring sample common information of each of the semantic labels according to the particle-sphere model in the first stage, and integrating and generating real semantic information according to the sample common information;
[0010] S40, using the real semantic information and the word vector sequence training to construct a second-stage classification model;
[0011] S50, obtaining a first target text to be classified, using the first-stage granular sphere model and a preset classifier to obtain an initial classification label and classification credibility of the first target text to be classified, and selecting the first target text to be classified that is lower than a classification credibility threshold according to the classification credibility to obtain a second target text to be classified;
[0012] S60, inputting the second target text to be classified into the second stage classification model to obtain a final classification label of the second target text to be classified.
[0013] According to one aspect of the above technical solution, in step S10:
[0014] The semantic model adopts the sentence_transformers model.
[0015] According to one aspect of the above technical solution, the specific steps of step S20 include:
[0016] The word vector sequences of the data set are clustered into two spheres using the Kmeans clustering algorithm;
[0017] The data samples in the word vector sequence are sequentially distributed to the sphere closest to the sphere to calculate the center of the sphere, the compactness, the number of data samples, the word vector sequence corresponding to the data sample, the semantic label and the purity of the data sample;
[0018] Obtaining a data sample number threshold index and a data sample purity threshold index according to a particle-ball classification algorithm, and judging whether the particle-ball is split based on the data sample number threshold index and the data sample purity threshold index;
[0019] Wherein, when the spheres simultaneously meet the data sample number threshold index and the data sample purity threshold index, the spheres stop splitting; when the spheres do not meet the data sample number threshold index and the data sample purity threshold index, the spheres continue to split using the Kmeans clustering algorithm;
[0020] Each of the spheres under the word vector sequence is integrated into a first-stage sphere model.
[0021] According to one aspect of the above technical solution, the specific steps of step S30 include:
[0022] S31, grouping and sorting the first-stage granule-sphere model according to the semantic label and the number of data samples, and assigning a contribution parameter to each granule-sphere under the first-stage granule-sphere model according to a parameter assignment algorithm;
[0023] S32, sorting each of the first-stage particle-sphere models according to the contribution parameter level, so as to divide each of the first-stage particle-sphere models into different levels;
[0024] S33, dividing the first-stage granular ball model of each level into two groups of granular ball units, and using an attention mechanism to focus on the common first-level intermediate semantics of the two groups of granular ball units;
[0025] Repeat the steps S31 to S33 to sequentially obtain the intermediate semantics of each level in the first-stage particle-sphere model of each level, and use the intermediate semantics of the final level as the common semantic information;
[0026] The public semantic information is combined with the contribution parameter to obtain the real semantic information.
[0027] According to one aspect of the above technical solution, the specific steps of step S40 include:
[0028] The real semantic information and the semantic information of the data sample are connected by using a feed-forward neural network FFN, and a fusion code is obtained after normalization processing;
[0029] Inputting the fusion code into a pre-trained semantic model and a preset text classification model for training to obtain a feature vector of shallow semantic information and a feature vector of deep semantic information in the data sample;
[0030] fusing the feature vector of the shallow semantic information and the feature vector of the deep semantic information to obtain a sentence level representation in the data sample;
[0031] The sentence level representation is classified by a classifier to obtain a trained second-stage classification model.
[0032] According to one aspect of the above technical solution, the specific steps of step S50 include:
[0033] Acquire a first target text to be classified, and obtain a semantic vector of the first target text to be classified;
[0034] Using a KNN classifier, calculate the K closest spheres of the first target text to be classified in the granularity space and obtain the corresponding distances;
[0035] Calculating the relative distances between K closest spheres and the first target text to be classified using the corresponding distances and the compactness of the spheres;
[0036] Obtaining an initial classification label and classification credibility of the first target text to be classified according to the relative distance;
[0037] A classification credibility threshold is preset to determine whether the first target text to be classified is successfully classified;
[0038] Among them, when the classification credibility of the first target text to be classified is higher than the classification credibility threshold, the first target text to be classified is successfully classified in the first stage granular model; when the classification credibility of the first target text to be classified is lower than the classification credibility threshold, the first target text to be classified fails to be classified in the first stage granular model and is marked as the second target text to be classified.
[0039] The present invention also provides a two-stage text classification device, comprising:
[0040] Acquisition module: used to acquire a data set for training, and vectorize the data set through a semantic model to obtain a word vector sequence in the data set;
[0041] In the acquisition module, the semantic model adopts the sentence_transformers model;
[0042] The first training module is used to train and construct a first-stage granular ball model using the word vector sequence, wherein the first-stage granular ball model has a plurality of semantic labels;
[0043] The first training module is specifically used to: cluster the word vector sequence of the data set into two spheres using the Kmeans clustering algorithm;
[0044] The data samples in the word vector sequence are sequentially distributed to the sphere closest to the sphere to calculate the center of the sphere, the compactness, the number of data samples, the word vector sequence corresponding to the data sample, the semantic label and the purity of the data sample;
[0045] Obtaining a data sample number threshold index and a data sample purity threshold index according to a particle-ball classification algorithm, and judging whether the particle-ball is split based on the data sample number threshold index and the data sample purity threshold index;
[0046] Wherein, when the spheres simultaneously meet the data sample number threshold index and the data sample purity threshold index, the spheres stop splitting; when the spheres do not meet the data sample number threshold index and the data sample purity threshold index, the spheres continue to split using the Kmeans clustering algorithm;
[0047] Integrate each of the spheres under the word vector sequence into a first-stage sphere model;
[0048] Integration module: used for obtaining the sample common information of each semantic label according to the particle-sphere model in the first stage, and integrating the sample common information to generate real semantic information;
[0049] The integration module is specifically used for:
[0050] S31, grouping and sorting the first-stage granule-sphere model according to the semantic label and the number of data samples, and assigning a contribution parameter to each granule-sphere under the first-stage granule-sphere model according to a parameter assignment algorithm;
[0051] S32, sorting each of the first-stage particle-sphere models according to the contribution parameter level, so as to divide each of the first-stage particle-sphere models into different levels;
[0052] S33, dividing the first-stage granular ball model of each level into two groups of granular ball units, and using an attention mechanism to focus on the common first-level intermediate semantics of the two groups of granular ball units;
[0053] Repeat the steps S31 to S33 to sequentially obtain the intermediate semantics of each level in the first-stage particle-sphere model of each level, and use the intermediate semantics of the final level as the common semantic information;
[0054] Combining the public semantic information with the contribution parameter to obtain real semantic information;
[0055] The second training module is used to train and construct a second-stage classification model using the real semantic information and the word vector sequence;
[0056] The second training module is specifically used to: connect the real semantic information and the semantic information of the data sample by using a feedforward neural network FFN, and obtain a fusion code after normalization processing;
[0057] Inputting the fusion code into a pre-trained semantic model and a preset text classification model for training to obtain a feature vector of shallow semantic information and a feature vector of deep semantic information in the data sample;
[0058] fusing the feature vector of the shallow semantic information and the feature vector of the deep semantic information to obtain a sentence level representation in the data sample;
[0059] Classifying the sentence-level representation by a classifier to obtain a trained second-stage classification model;
[0060] Classification module: used for obtaining the first target text to be classified, obtaining the initial classification label and classification credibility of the first target text to be classified by using the first stage granular sphere model and the preset classifier, and selecting the first target text to be classified that is lower than the classification credibility threshold according to the classification credibility to obtain the second target text to be classified;
[0061] The classification module is specifically used to: obtain a first target text to be classified, and obtain a semantic vector of the first target text to be classified;
[0062] Using a KNN classifier, calculate the K closest spheres of the first target text to be classified in the granularity space and obtain the corresponding distances;
[0063] Calculating the relative distances between K closest spheres and the first target text to be classified using the corresponding distances and the compactness of the spheres;
[0064] Obtaining an initial classification label and classification credibility of the first target text to be classified according to the relative distance;
[0065] A classification credibility threshold is preset to determine whether the first target text to be classified is successfully classified;
[0066] Wherein, when the classification credibility of the first target text to be classified is higher than the classification credibility threshold, the first target text to be classified is successfully classified in the first-stage granular sphere model; when the classification credibility of the first target text to be classified is lower than the classification credibility threshold, the first target text to be classified fails to be classified in the first-stage granular sphere model and is marked as the second target text to be classified;
[0067] Output module: input the second target text to be classified into the second stage classification model to obtain the final classification label of the second target text to be classified.
[0068] The present invention also provides a storage medium on which a computer program is stored. When the computer program is executed by a processor, the two-stage text classification method as described above is implemented.
[0069] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned two-stage text classification method when executing the computer program.
[0070] Compared with the prior art, the present invention has the following beneficial effects:
[0071] By providing a data set for training and vectorizing the data set to obtain a word vector sequence, data samples in the data set can be extracted to prepare for the subsequent semantics of the data samples. Then, the first-stage granular ball model is constructed through word vector sequence training, and the data samples can be clustered to find out the true semantic information of each semantic label. Then, the second-stage classification model is constructed through the true semantic information and word vector sequence training, thereby integrating the semantic information of the label so that the semantic information of the label can be better mapped to the corresponding tag space. When the first-stage granular ball model cannot obtain the final classification label more effectively, the first target text to be classified that is below the classification credibility threshold can be input into the second-stage classification model for classification, thereby improving the classification accuracy;
[0072] The two-stage text classification method in the present invention can make the overall model more generalizable and more efficient. BRIEF DESCRIPTION OF THE DRAWINGS
[0073] The above and / or additional aspects and advantages of the present invention will become apparent and easily understood from the description of the embodiments in conjunction with the following drawings, in which:
[0074] Figure 1 Flow chart of the two-stage text classification method in the first embodiment of the present invention;
[0075] Figure 2 It is a structural network diagram when the first ball model is grouped in the first embodiment of the present invention;
[0076] Figure 3 A structural network diagram for obtaining public semantic information in the first embodiment of the present invention;
[0077] Figure 4 It is a structural block diagram of a two-stage text classification device in a second embodiment of the present invention;
[0078] Figure 5 is a structural block diagram of an electronic device in a third embodiment of the present invention;
[0079] The following specific implementation manner will further illustrate the present invention in conjunction with the above-mentioned drawings. DETAILED DESCRIPTION
[0080] In order to facilitate the understanding of the present invention, the present invention will be described more fully below with reference to the relevant drawings. Several embodiments of the present invention are given in the drawings. However, the present invention can be implemented in many different forms and is not limited to the embodiments described herein. On the contrary, the purpose of providing these embodiments is to make the disclosure of the present invention more thorough and comprehensive.
[0081] It should be noted that when an element is referred to as being "fixed to" another element, it may be directly on the other element or there may be a central element. When an element is considered to be "connected to" another element, it may be directly connected to the other element or there may be a central element at the same time. The terms "vertical", "horizontal", "left", "right" and similar expressions used herein are for illustrative purposes only.
[0082] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which the present invention belongs. The terms used herein in the specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. The term "and / or" used herein includes any and all combinations of one or more of the related listed items.
[0083] Embodiment 1
[0084] See also Figure 1 , which shows a two-stage text classification method in a first embodiment of the present invention, comprising the following steps:
[0085] S10, obtaining a data set for training, and vectorizing the data set through a semantic model to obtain a word vector sequence in the data set;
[0086] S20, using the word vector sequence training to construct a first-stage granular ball model, wherein the first-stage granular ball model has a plurality of semantic labels;
[0087] S30, acquiring sample common information of each of the semantic labels according to the particle-sphere model in the first stage, and integrating and generating real semantic information according to the sample common information;
[0088] S40, using the real semantic information and the word vector sequence training to construct a second-stage classification model;
[0089] S50, obtaining a first target text to be classified, using the first-stage granular sphere model and a preset classifier to obtain an initial classification label and classification credibility of the first target text to be classified, and selecting the first target text to be classified that is lower than a classification credibility threshold according to the classification credibility to obtain a second target text to be classified;
[0090] S60, inputting the second target text to be classified into the second stage classification model to obtain a final classification label of the second target text to be classified.
[0091] It can be understood that the present invention provides a data set for training and vectorizes the data set to obtain a word vector sequence, so that data samples in the data set can be extracted to prepare for the subsequent semantics of the data samples. Then, the first-stage granular ball model is constructed through word vector sequence training, and the data samples can be clustered to find out the true semantic information of each semantic label. Then, the second-stage classification model is constructed through the true semantic information and word vector sequence training, so as to integrate the semantic information of the label, so that the semantic information of the label can be better mapped to the corresponding tag space. When the first-stage granular ball model cannot obtain the final classification label more effectively, the first target text to be classified that is lower than the classification credibility threshold can be input into the second-stage classification model for classification, thereby improving the accuracy of classification;
[0092] The two-stage text classification method in the present invention can make the overall model more generalizable and more efficient.
[0093] Furthermore, in step S10:
[0094] Based on the real semantic information of each category of the data set, each category of the data set is encoded in advance. In a specific implementation, for example, the real semantic information of one category is "agricultural technology", which is labeled as "0" after encoding, and the real semantic information of another category is "agricultural policy", which is labeled as "1" after encoding. The compiled labels are used in subsequent training;
[0095] The semantic model adopts the sentence_transformers model to extract feature vectors from the data set to obtain a word vector sequence of 768 dimensions.
[0096] Furthermore, the specific steps of step S20 include:
[0097] The word vector sequence of the data set is clustered into two spheres using the Kmeans clustering algorithm, and the two spheres are the initial spheres;
[0098] The data samples in the word vector sequence are sequentially distributed to the sphere closest to the sphere to calculate the center of the sphere, the compactness, the number of data samples, the word vector sequence corresponding to the data sample, the semantic label and the purity of the data sample;
[0099] Obtaining a data sample number threshold index and a data sample purity threshold index according to a particle-ball classification algorithm, and judging whether the particle-ball is split based on the data sample number threshold index and the data sample purity threshold index;
[0100] Wherein, when the spheres simultaneously meet the data sample number threshold index and the data sample purity threshold index, the spheres stop splitting; when the spheres do not meet the data sample number threshold index and the data sample purity threshold index, the spheres continue to split using the Kmeans clustering algorithm;
[0101] Each of the spheres under the word vector sequence is integrated into a first-stage sphere model.
[0102] In specific implementation, the calculation formula of the center and compactness of the sphere is defined as follows:
[0103] ;
[0104] ;
[0105] In the formula, r Indicates the compactness of the pellet. x i represents the data sample of the sphere, c represents the center of the sphere, n represents the number of data samples, Represents data samples x i To the center of the ball c The Euclidean distance of Indicates the selection of data samples within the sphere x i To the center of the ball c The maximum distance;
[0106] The purity of the data sample is an important indicator to measure whether the sphere stops splitting. When the purity of the sphere data sample is too low, it needs to be further split to make the first-stage sphere model closer to the real data sample distribution. Given a data set The first stage granular sphere model constructed , the sample purity is calculated to be , Zhongyu The number of data samples with the same label is , The total amount of data samples is , the sample purity calculation formula of the pellet is defined as follows:
[0107] ;
[0108] Set two threshold indicators for stopping the particle splitting: data sample number threshold indicator And data sample purity threshold index , where the data sample number threshold index Corresponding to the above data sample number information item, the data sample purity threshold index Corresponding to the above data sample purity information item, it is determined whether the particle ball continues to split according to the above obtained particle ball information item. Specifically:
[0109] When the particles and balls meet the threshold index at the same time and , the sphere has reached the final split state and stops splitting;
[0110] When the particle does not meet any threshold index and , the ball has not reached the final split state, and the Kmeans algorithm continues to be used for splitting;
[0111] Finally, each granular ball after splitting in the word vector sequence is integrated into the first-stage granular ball model.
[0112] It should be noted that the particle-sphere calculation classification model is an existing algorithm, and the present invention applies the algorithm in this field for the first time.
[0113] Furthermore, the specific steps of step S30 include:
[0114] S31, grouping and sorting the first-stage granule-sphere model according to the semantic label and the number of data samples, and assigning a contribution parameter to each granule-sphere under the first-stage granule-sphere model according to a parameter assignment algorithm;
[0115] S32, sorting each of the first-stage particle-sphere models according to the contribution parameter level, so as to divide each of the first-stage particle-sphere models into different levels;
[0116] S33, dividing the first-stage granular ball model of each level into two groups of granular ball units, and using an attention mechanism to focus on the common first-level intermediate semantics of the two groups of granular ball units;
[0117] Repeat the steps S31 to S33 to sequentially obtain the intermediate semantics of each level in the first-stage particle-sphere model of each level, and use the intermediate semantics of the final level as the common semantic information;
[0118] The public semantic information is combined with the contribution parameter to obtain the real semantic information.
[0119] See also Figure 2In the specific implementation, the first-stage granular ball models are grouped based on semantic labels, and the first-stage granular ball models are sorted from large to small according to the number of data samples in the granular balls, and contribution parameters are allocated to the granular balls in the sorted first-stage granular ball models. In this embodiment, gradient descent is used to allocate contribution parameters to each granular ball. These parameters are divided into 6 levels, representing contribution tendencies of different strengths. Gradient descent is calculated using the following formula:
[0120] ;
[0121] In the formula, is the weight value of the first level, that is, the contribution parameter of the first level, and the constant is the gradient descent rate.
[0122] According to the contribution parameters obtained above, the first-stage granular sphere model is divided into 6 levels. The purpose is to make the difference in contribution of the first-stage granular sphere models at different levels as large as possible, while the difference of the first-stage granular sphere models in the same level as small as possible.
[0123] Using the hierarchical contribution obtained above, the public semantic information of each layer is obtained through the attention mechanism, and finally the public semantic information of each layer is combined to obtain the true semantic information of the label.
[0124] See also Figure 3 In the specific implementation, the first-stage granular model samples of each layer are divided into two groups, and the attention mechanism is used to focus on their corresponding first-level intermediate semantics. Therefore, the non-common semantics between the two granular model samples are weakened, while the common semantics are retained. Then the above steps are repeated to obtain higher-level second-level intermediate semantics, third-level intermediate semantics, etc. in all first-level intermediate semantic groups until the common semantic information of each level is obtained. , the public semantic information of each level obtained above Contribution parameters Combine together to obtain the true semantic information of the semantic label , the real semantic information of the semantic label The definition is as follows:
[0125] ; k =1,2,3,4,5,6;
[0126] In the formula, Indicates The real semantic information of the label, is a parameterized function.
[0127] Furthermore, the specific steps of step S40 include:
[0128] The real semantic information and the semantic information of the data sample are connected by using a feed-forward neural network FFN, and a fusion code is obtained after normalization processing;
[0129] Inputting the fusion code into a pre-trained semantic model and a preset text classification model for training to obtain a feature vector of shallow semantic information and a feature vector of deep semantic information in the data sample;
[0130] fusing the feature vector of the shallow semantic information and the feature vector of the deep semantic information to obtain a sentence level representation in the data sample;
[0131] The sentence level representation is classified by a classifier to obtain a trained second-stage classification model.
[0132] In specific implementation, the definition formula of fusion coding is as follows:
[0133] ;
[0134] In the formula, represents normalization, It is the semantic information of the data sample;
[0135] The pre-trained semantic model uses the StructBert model, which introduces language structure information based on the Bert model to enhance the model's understanding and processing capabilities of text. This improvement enables the model to better understand the structure and context of the text when processing natural language, thereby improving the performance and applicability of the model. In this example, the fusion encoding is passed through a multi-layer Transformer structure to obtain a semantic feature vector CLS representing the entire text;
[0136] The preset text classification model uses the DualCNN model, which is divided into a convolutional layer, a dual-channel pooling layer, a fusion layer, and a fully connected layer. This dual-channel pooling CNN is used to extract local key information and context sequence information in the text, thereby enhancing the feature recognition of key features and the coverage of sequence information of the entire sample, thereby further improving the classification accuracy of the model. Specifically, the sentence feature vector is extracted through CNN, and then average pooling and maximum pooling are performed on the two channel dimensions respectively to obtain the feature vector of shallow semantic information. and the feature vector of deep semantic information , and then through fusion, a sentence-level representation is obtained, and finally the classifier is used for classification. The final trained StructBert-DualCNN model is obtained, which is the second-stage classification model;
[0137] In this embodiment, the feature vector of the shallow semantic information and the feature vector of deep semantic information They are formula (1) and formula (2) respectively:
[0138] Formula (1): ;
[0139] Formula (2): ;
[0140] In the formula, Indicates that the sentence is processed using the convolution kernel The first vectors, Yes The vector obtained after average pooling is is the total number of these vectors, summing up the vectors of all sentences Connect to get , similarly, replace the average pooling with the maximum pooling to get .
[0141] Furthermore, the specific steps of step S50 include:
[0142] Acquire a first target text to be classified, and obtain a semantic vector of the first target text to be classified;
[0143] Using a KNN classifier, calculate the K closest spheres of the first target text to be classified in the granularity space and obtain the corresponding distances;
[0144] Calculating the relative distances between K closest spheres and the first target text to be classified using the corresponding distances and the compactness of the spheres;
[0145] Obtaining an initial classification label and classification credibility of the first target text to be classified according to the relative distance;
[0146] A classification credibility threshold is preset to determine whether the first target text to be classified is successfully classified;
[0147] Among them, when the classification credibility of the first target text to be classified is higher than the classification credibility threshold, the first target text to be classified is successfully classified in the first stage granular model; when the classification credibility of the first target text to be classified is lower than the classification credibility threshold, the first target text to be classified fails to be classified in the first stage granular model and is marked as the second target text to be classified.
[0148] In the specific implementation, the first target text to be classified is obtained, and after the word vector sequence of the first target text to be classified is vectorized, the word vector sequence obtained is input into the first-stage granular sphere model, and the preset KNN classifier is used to obtain the initial classification label and classification credibility of the target text.
[0149] Set the classification credibility threshold indicator , the first target text to be classified after KNN classification is divided into high-credibility target text and low-credibility target text. Specifically:
[0150] When the classification credibility of the first target text to be classified is higher than the classification credibility threshold , the target text is determined to be a highly credible target text, that is, the first target text to be classified is successfully classified in the first-stage granular-sphere model;
[0151] When the classification confidence of the first target text to be classified is lower than the classification confidence threshold When the first target text to be classified is judged as a low-credibility target text, that is, the second target text to be classified. That is to say, the first target text to be classified fails to be classified in the first-stage granular sphere model and is further processed by the downstream second-stage classification model to obtain the final classification label.
[0152] In summary, the two-stage text classification method in the above embodiment of the present invention can make the overall model more generalizable and more efficient.
[0153] Embodiment 2
[0154] Please refer to Figure 4 , shown is a two-stage text classification device 40 in a second embodiment of the present invention, comprising:
[0155] Acquisition module 11: used to acquire a data set for training, and vectorize the data set through a semantic model to obtain a word vector sequence in the data set;
[0156] In the acquisition module 11, the semantic model adopts the sentence_transformers model;
[0157] The first training module 12 is used to train and construct a first-stage granular ball model using the word vector sequence, wherein the first-stage granular ball model has a plurality of semantic labels;
[0158] The first training module 12 is specifically used to: cluster the word vector sequence of the data set into two spheres using the Kmeans clustering algorithm;
[0159] The data samples in the word vector sequence are sequentially distributed to the sphere closest to the sphere to calculate the center of the sphere, the compactness, the number of data samples, the word vector sequence corresponding to the data sample, the semantic label and the purity of the data sample;
[0160] Obtaining a data sample number threshold index and a data sample purity threshold index according to a particle-ball classification algorithm, and judging whether the particle-ball is split based on the data sample number threshold index and the data sample purity threshold index;
[0161] Wherein, when the spheres simultaneously meet the data sample number threshold index and the data sample purity threshold index, the spheres stop splitting; when the spheres do not meet the data sample number threshold index and the data sample purity threshold index, the spheres continue to split using the Kmeans clustering algorithm;
[0162] Integrate each of the spheres under the word vector sequence into a first-stage sphere model;
[0163] Integration module 13: used for obtaining the sample common information of each semantic label according to the particle-sphere model in the first stage, and generating real semantic information according to the sample common information integration;
[0164] The integration module 13 is specifically used for:
[0165] S31, grouping and sorting the first-stage particle-sphere model according to the label and the number of data samples, and assigning a contribution parameter to each particle under the first-stage particle-sphere model according to a parameter assignment algorithm;
[0166] S32, sorting each of the first-stage particle-sphere models according to the contribution parameter level, so as to divide each of the first-stage particle-sphere models into different levels;
[0167] S33, dividing the first-stage granular ball model of each level into two groups of granular ball units, and using an attention mechanism to focus on the common first-level intermediate semantics of the two groups of granular ball units;
[0168] Repeat the steps S31 to S33 to sequentially obtain the intermediate semantics of each level in the first-stage particle-sphere model of each level, and use the intermediate semantics of the final level as the common semantic information;
[0169] Combining the public semantic information with the contribution parameter to obtain real semantic information;
[0170] The second training module 14 is used to train and construct a second-stage classification model using the real semantic information and the word vector sequence;
[0171] The second training module 14 is specifically used to: connect the real semantic information and the semantic information of the data sample by using a feedforward neural network FFN, and obtain a fusion code after normalization processing;
[0172] Inputting the fusion code into a pre-trained semantic model and a preset text classification model for training to obtain a feature vector of shallow semantic information and a feature vector of deep semantic information in the data sample;
[0173] Fusing the feature vector of the shallow semantic information with the feature vector of the deep semantic information to obtain a sentence level representation in the data sample;
[0174] Classifying the sentence-level representation by a classifier to obtain a trained second-stage classification model;
[0175] Classification module 15: used to obtain the first target text to be classified, obtain the initial classification label and classification credibility of the first target text to be classified by using the first stage granular sphere model and the preset classifier, and select the first target text to be classified that is lower than the classification credibility threshold according to the classification credibility to obtain the second target text to be classified;
[0176] The classification module 15 is specifically used to: obtain a first target text to be classified, and obtain a semantic vector of the first target text to be classified;
[0177] Using a KNN classifier, calculate the K closest spheres of the first target text to be classified in the granularity space and obtain the corresponding distances;
[0178] Calculating the relative distances between K closest spheres and the first target text to be classified using the corresponding distances and the compactness of the spheres;
[0179] Obtaining an initial classification label and classification credibility of the first target text to be classified according to the relative distance;
[0180] A classification credibility threshold is preset to determine whether the first target text to be classified is successfully classified;
[0181] Wherein, when the classification credibility of the first target text to be classified is higher than the classification credibility threshold, the first target text to be classified is successfully classified in the first-stage granular sphere model; when the classification credibility of the first target text to be classified is lower than the classification credibility threshold, the first target text to be classified fails to be classified in the first-stage granular sphere model and is marked as the second target text to be classified;
[0182] Output module 16: inputs the second target text to be classified into the second stage classification model to obtain a final classification label of the second target text to be classified.
[0183] Embodiment 3
[0184] The present invention also provides an electronic device, see Figure 5 , shown is an electronic device in the third embodiment of the present invention, including a memory 10, a processor 20, and a computer program 30 stored in the memory 10 and executable on the processor 20, and the processor 20 implements the above-mentioned two-stage text classification method when executing the computer program 30.
[0185] Among them, the memory 10 includes at least one type of storage medium, and the storage medium includes a flash memory, a hard disk, a multimedia card, a card-type memory (for example, an SD or DX memory, etc.), a magnetic memory, a magnetic disk, an optical disk, etc. In some embodiments, the memory 10 can be an internal storage unit of an electronic device, such as a hard disk of the electronic device. In other embodiments, the memory 10 can also be an external storage device, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. Further, the memory 10 can also include both an internal storage unit of the electronic device and an external storage device. The memory 10 can not only be used to store application software and various types of data installed in the electronic device, but also can be used to temporarily store data that has been output or is to be output.
[0186] Among them, in some embodiments, the processor 20 can be an electronic control unit (Electronic Control Unit, abbreviated as ECU, also known as a vehicle computer), a central processing unit (Central Processing Unit, CPU), a controller, a microcontroller, a microprocessor or other data processing chip, used to run the program code stored in the memory 10 or process data, such as executing access restriction programs, etc.
[0187] It should be pointed out that Figure 5 The structure shown does not constitute a limitation on the electronic device. In other embodiments, the electronic device may include fewer or more components than those shown in the figure, or combine certain components, or arrange the components differently.
[0188] The embodiment of the present invention further provides a storage medium on which a computer program is stored. When the computer program is executed by a processor, the two-stage text classification method as described above is implemented.
[0189] Those skilled in the art will appreciate that the logic and / or steps represented in the flowchart or otherwise described herein, for example, may be considered as an ordered list of executable instructions for implementing logical functions, and may be specifically implemented in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute instructions), or in conjunction with such instruction execution systems, devices or apparatuses. For purposes of this specification, "computer-readable medium" may be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in conjunction with such instruction execution systems, devices or apparatuses.
[0190] More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk case (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), an optical fiber device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be a paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, deciphering or, if necessary, processing in another suitable manner, and then stored in a computer memory.
[0191] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, multiple steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or a combination thereof: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0192] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "examples", "specific examples", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.
[0193] The above-mentioned embodiments only express several implementation methods of the present invention, and the description thereof is relatively specific and detailed, but it cannot be understood as limiting the scope of the patent of the present invention. It should be pointed out that, for ordinary technicians in this field, several variations and improvements can be made without departing from the concept of the present invention, which all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be subject to the attached claims.
Claims
1. A two-stage text classification method, characterized in that: The steps include: S10, obtaining a data set for training, and vectorizing the data set through a semantic model to obtain a word vector sequence in the data set; S20, using the word vector sequence training to construct a first-stage granular ball model, wherein the first-stage granular ball model has a plurality of semantic labels; S30, acquiring sample common information of each of the semantic labels according to the particle-sphere model in the first stage, and integrating and generating real semantic information according to the sample common information; S40, using the real semantic information and the word vector sequence training to construct a second-stage classification model; S50, obtaining a first target text to be classified, using the first-stage granular sphere model and a preset classifier to obtain an initial classification label and classification credibility of the first target text to be classified, and selecting the first target text to be classified that is lower than a classification credibility threshold according to the classification credibility to obtain a second target text to be classified; S60, inputting the second target text to be classified into the second stage classification model to obtain a final classification label of the second target text to be classified; The specific steps of step S20 include: The word vector sequences of the data set are clustered into two spheres using the Kmeans clustering algorithm; The data samples in the word vector sequence are sequentially distributed to the sphere closest to the sphere to calculate the center of the sphere, the compactness, the number of data samples, the word vector sequence corresponding to the data sample, the semantic label and the purity of the data sample; The calculation formula of the center and compactness of the sphere is defined as follows: ; ; In the formula, r Indicates the compactness of the pellet. x i represents the data sample of the sphere, c represents the center of the sphere, n represents the number of data samples, Represents data samples x i To the center of the ball c The Euclidean distance of Indicates the selection of data samples within the sphere x i To the center of the ball c The maximum distance; Given a data set The first stage granular sphere model constructed , the purity of the data sample is calculated to be , dataset The first stage granular sphere model The number of data samples with the same label is , dataset The total amount of data samples is , the calculation formula of the purity of the data sample of the particle ball is defined as follows: ; According to the particle-ball classification algorithm, a data sample number threshold index and a data sample purity threshold index are obtained, and based on the data sample number threshold index and the data sample purity threshold index, whether the particle ball is split is determined, specifically: Set two threshold indicators for stopping the particle splitting: data sample number threshold indicator And data sample purity threshold index , where the data sample number threshold indicator The information item corresponding to the number of data samples, the purity threshold index of the data samples The information item corresponding to the purity of the data sample, wherein when the particle ball satisfies the data sample number threshold index at the same time and the data sample purity threshold index When the particle stops splitting, the particle does not meet the data sample number threshold index and the data sample purity threshold index When , the sphere continues to split using the Kmeans clustering algorithm; Integrate each of the spheres under the word vector sequence into a first-stage sphere model; The specific steps of step S30 include: S31, grouping and sorting the first-stage granule-sphere model according to the semantic label and the number of data samples, and assigning a contribution parameter to each granule-sphere under the first-stage granule-sphere model according to a parameter assignment algorithm; S32, sorting each of the first-stage particle-sphere models according to the contribution parameter level, so as to divide each of the first-stage particle-sphere models into different levels; S33, dividing the first-stage granular ball model of each level into two groups of granular ball units, and using an attention mechanism to focus on the common first-level intermediate semantics of the two groups of granular ball units; Repeat the steps S31 to S33 to sequentially obtain the intermediate semantics of each level in the first-stage particle-sphere model of each level, and use the intermediate semantics of the final level as the common semantic information; The public semantic information is combined with the contribution parameter to obtain the real semantic information.
2. The two-stage text classification method according to claim 1, characterized in that: In step S10: The semantic model adopts the sentence_transformers model.
3. The two-stage text classification method according to claim 1, characterized in that: The specific steps of step S40 include: The real semantic information and the semantic information of the data sample are connected by using a feedforward neural network FFN, and a fusion code is obtained after normalization processing; Inputting the fusion code into a pre-trained semantic model and a preset text classification model for training to obtain a feature vector of shallow semantic information and a feature vector of deep semantic information in the data sample; fusing the feature vector of the shallow semantic information and the feature vector of the deep semantic information to obtain a sentence level representation in the data sample; The sentence level representation is classified by a classifier to obtain a trained second-stage classification model.
4. The two-stage text classification method according to claim 3 is characterized in that: The specific steps of step S50 include: Acquire a first target text to be classified, and obtain a semantic vector of the first target text to be classified; Using a KNN classifier, calculate the K closest spheres of the first target text to be classified in the granularity space and obtain the corresponding distances; Calculating the relative distances between K closest spheres and the first target text to be classified using the corresponding distances and the compactness of the spheres; Obtaining an initial classification label and classification credibility of the first target text to be classified according to the relative distance; A classification credibility threshold is preset to determine whether the first target text to be classified is successfully classified; Among them, when the classification credibility of the first target text to be classified is higher than the classification credibility threshold, the first target text to be classified is successfully classified in the first stage granular model; when the classification credibility of the first target text to be classified is lower than the classification credibility threshold, the first target text to be classified fails to be classified in the first stage granular model and is marked as the second target text to be classified.
5. A two-stage text classification device, characterized in that: include: Acquisition module: used to acquire a data set for training, and vectorize the data set through a semantic model to obtain a word vector sequence in the data set; The first training module is used to train and construct a first-stage granular ball model using the word vector sequence, wherein the first-stage granular ball model has a plurality of semantic labels; The first training module is specifically used for: The word vector sequences of the data set are clustered into two spheres using the Kmeans clustering algorithm; The data samples in the word vector sequence are sequentially distributed to the sphere closest to the sphere to calculate the center of the sphere, the compactness, the number of data samples, the word vector sequence corresponding to the data sample, the semantic label and the purity of the data sample; The calculation formula of the center and compactness of the sphere is defined as follows: ; ; In the formula, r Indicates the compactness of the pellet. x i represents the data sample of the sphere, c represents the center of the sphere, n represents the number of data samples, Represents data samples x i To the center of the ball c The Euclidean distance of Indicates the selection of data samples within the sphere x i To the center of the ball c The maximum distance; Given a data set The first stage granular sphere model constructed , the purity of the data sample is calculated to be , dataset The first stage granular sphere model The number of data samples with the same label is , dataset The total amount of data samples is , the calculation formula of the purity of the data sample of the particle ball is defined as follows: ; According to the particle-ball classification algorithm, a data sample number threshold index and a data sample purity threshold index are obtained, and based on the data sample number threshold index and the data sample purity threshold index, whether the particle ball is split is determined, specifically: Set two threshold indicators for stopping the particle splitting: data sample number threshold indicator And data sample purity threshold index , where the data sample number threshold indicator The information item corresponding to the number of data samples, the purity threshold index of the data samples The information item corresponding to the purity of the data sample, wherein when the particle ball satisfies the data sample number threshold index at the same time and the data sample purity threshold index When the particle stops splitting, the particle does not meet the data sample number threshold index and the data sample purity threshold index When , the sphere continues to split using the Kmeans clustering algorithm; Integrate each of the spheres under the word vector sequence into a first-stage sphere model; Integration module: used for obtaining the sample common information of each semantic label according to the particle-sphere model in the first stage, and integrating the sample common information to generate real semantic information; The integration module is specifically used for: S31, grouping and sorting the first-stage granule-sphere model according to the semantic label and the number of data samples, and assigning a contribution parameter to each granule-sphere under the first-stage granule-sphere model according to a parameter assignment algorithm; S32, sorting each of the first-stage particle-sphere models according to the contribution parameter level, so as to divide each of the first-stage particle-sphere models into different levels; S33, dividing the first-stage granular ball model of each level into two groups of granular ball units, and using an attention mechanism to focus on the common first-level intermediate semantics of the two groups of granular ball units; Repeat the steps S31 to S33 to sequentially obtain the intermediate semantics of each level in the first-stage particle-sphere model of each level, and use the intermediate semantics of the final level as the common semantic information; Combining the public semantic information with the contribution parameter to obtain real semantic information; The second training module is used to train and construct a second-stage classification model using the real semantic information and the word vector sequence; Classification module: used for obtaining the first target text to be classified, obtaining the initial classification label and classification credibility of the first target text to be classified by using the first stage granular sphere model and the preset classifier, and selecting the first target text to be classified that is lower than the classification credibility threshold according to the classification credibility to obtain the second target text to be classified; Output module: used for inputting the second target text to be classified into the second stage classification model to obtain the final classification label of the second target text to be classified.
6. A storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the two-stage text classification method according to any one of claims 1 to 4 is implemented.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the two-stage text classification method according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Text classification method and device based on target semantics, equipment and storage medium
CN113486670A
Visual text data classification method based on text clustering semantic cloud
CN117332787A