A text classification method, system, terminal and medium based on adaptive reward mechanism
By designing hierarchical loss functions and adaptive reward mechanisms in text classification, combined with technologies such as convolutional neural networks and bidirectional long short-term memory networks, the problems of insufficient label dependence and poor robustness of traditional methods in processing complex hierarchical text data are solved. problem, achieving more efficient and accurate text classification.
Patent Information
- Application Number
- CN202411155624.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-22
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2044-08-22
AI Technical Summary
When traditional text classification methods deal with complex hierarchical text data, label dependence is insufficient and the model robustness is poor, making it difficult to ensure the stability and consistency of classification results.
Design a hierarchical loss function and introduce an adaptive reward mechanism, process label hierarchical relationships through one-hot encoding, use cosine similarity to construct a hierarchical loss function, and combine convolutional neural networks, bidirectional long short-term memory networks and self-attention mechanisms to build a hierarchical text classification model , adjust the model parameters through the backpropagation algorithm.
It significantly improves the accuracy and robustness of text classification, enhances the model's ability to learn hierarchical relationships, provides more accurate hierarchical predictions, and is suitable for complex text classification tasks.
Smart Images

Figure CN118673152B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of text classification, and in particular to a text classification method, system, terminal and medium based on an adaptive reward mechanism. Background Art
[0002] Text data usually has rich information content and complex hierarchical structure, covering a variety of forms from simple single-sentence information to complex multi-level opinion information. Therefore, how to efficiently and accurately classify these text data has become a major challenge in classification processing.
[0003] Traditional text classification methods mainly rely on flat classification models, which treat text as independent classification tasks and ignore the hierarchical relationship between texts. These methods may be effective to a certain extent when dealing with simple, flat text data. However, when faced with text data with complex hierarchical structures, traditional methods are unable to cope with the situation, mainly in the following aspects: Insufficient label dependency: Traditional flat classification methods usually treat each text label as independent and do not fully consider the hierarchical relationship between labels; Poor model robustness: Due to the lack of systematic design of traditional methods when dealing with hierarchical data, when faced with the diversity and complexity of data, the robustness and adaptability of the model are poor, and it is difficult to ensure the stability and consistency of the classification results. Summary of the invention
[0004] To solve the above problems, the present invention provides a text classification method, system, terminal and medium based on an adaptive reward mechanism. By designing a hierarchical loss function and introducing an adaptive reward mechanism, the limitations of traditional methods in processing complex hierarchical text data are effectively solved, and the accuracy and efficiency of classification are significantly improved.
[0005] In a first aspect, the technical solution of the present invention provides a text classification method based on an adaptive reward mechanism, comprising the following steps:
[0006] Obtain text data and preprocess it to construct training samples; use one-hot encoding to process hierarchical labels during preprocessing;
[0007] Based on the hierarchical relationship between labels, an adaptive reward mechanism based on cosine similarity is introduced to construct a hierarchical loss function;
[0008] Construct a hierarchical text classification model, use training samples to train the hierarchical text classification model, and use the back propagation algorithm to adjust the model parameters based on the hierarchical loss function during the training process;
[0009] Use the trained hierarchical text classification model to classify the target text.
[0010] In an optional implementation, based on the hierarchical relationship between tags, an adaptive reward mechanism based on cosine similarity is introduced to construct a hierarchical loss function, specifically including:
[0011] Step 1: For each label level, construct a basic cross entropy loss function, expressed as:
[0012]
[0013] In the formula, Indicates Cross entropy loss at label level, Indicates The true label distribution at the label level, Indicates The predicted label probability distribution at the label level, Indicates the number of label levels;
[0014] Step 2: Taking hierarchical dependencies into account, a penalty factor is applied to the cross entropy when calculating the loss beyond the top layer, expressed as:
[0015]
[0016] In the formula, Indicates The penalty factor at the label level, ; represents an indicator function if For true, is equal to 0, otherwise it is equal to 1;
[0017] Step 3: Adjust the cross entropy loss of the label level except the first layer based on the penalty factor. The adjusted cross entropy loss is expressed as:
[0018]
[0019] In the formula, Indicates Weighting factors at the label level; ;
[0020] Step 4: Calculate the cosine similarity of each tag level. The expression is:
[0021]
[0022] In the formula, ;
[0023] Step 5: Introduce an adaptive reward mechanism to calculate the smooth reward. The expression is:
[0024]
[0025] In the formula, ;
[0026] Step 6, construct the total loss, the expression is:
[0027]
[0028] In the formula, represents the reward factor, Indicates taking the average value.
[0029] In an optional embodiment, the method further comprises the following steps:
[0030] Pre-configure the initial weighting factor and adjustment step size for each label level;
[0031] During the training process, if the prediction of the previous label level is wrong, the weighting factor for the next label level is increased based on the initial weighting factor according to the adjustment step size.
[0032] In an optional implementation, obtaining text data specifically includes:
[0033] Configure the crawler to simulate the user's browser behavior, including adding target request headers;
[0034] Run the crawler tool, parse the web page and set the waiting strategy;
[0035] Extract the content of HTML elements according to the regular expression of the crawler tool;
[0036] For data that needs to be obtained through API interfaces, use the HTTP request library to call these API interfaces;
[0037] Use redis's collection data structure to filter and store non-duplicate data sets;
[0038] Store the processed data in an Excel spreadsheet.
[0039] In an optional implementation, preprocessing the text data specifically includes:
[0040] Clean the text data and remove irrelevant characters, including using crawler tools to remove HTML tags from each row of Excel spreadsheets and using regular expressions to remove useless characters and extra spaces;
[0041] Use word segmentation tools to perform word segmentation on the cleaned text data;
[0042] Based on a predefined stop word list, stop words are removed from the text data after word segmentation processing;
[0043] The text data after word segmentation is processed by using the Word2Vector model for vector encoding.
[0044] Perform one-hot encoding on the labeled level labels.
[0045] In an optional embodiment, the hierarchical text classification model includes an input layer, a hidden layer, and an output layer;
[0046] The hidden layer first extracts the merged features through two layers of convolutional neural networks using three different sizes of convolution kernels, and then inputs the merged features into a bidirectional long short-term memory network, and then connects the self-attention mechanism after the bidirectional long short-term memory network for processing;
[0047] The output layer uses the softmax activation function.
[0048] In a second aspect, the technical solution of the present invention provides a text classification system based on an adaptive reward mechanism, comprising:
[0049] Sample construction module: obtain text data and preprocess it to construct training samples; use one-hot encoding to process hierarchical labels during the preprocessing process;
[0050] Hierarchical loss function construction module: Based on the hierarchical relationship between labels, an adaptive reward mechanism based on cosine similarity is introduced to construct a hierarchical loss function;
[0051] Hierarchical text classification model training module: build a hierarchical text classification model, use training samples to train the hierarchical text classification model, and use the back propagation algorithm to adjust the model parameters based on the hierarchical loss function during the training process;
[0052] Text classification execution module: Use the trained hierarchical text classification model to classify the target text.
[0053] In a third aspect, the technical solution of the present invention provides a terminal, including:
[0054] A memory, used for storing a text classification program based on an adaptive reward mechanism;
[0055] A processor is used to implement the steps of any of the above-mentioned text classification methods based on the adaptive reward mechanism when executing the text classification program based on the adaptive reward mechanism.
[0056] In a fourth aspect, the technical solution of the present invention provides a computer-readable storage medium, on which is stored a text classification program based on an adaptive reward mechanism. When the text classification program based on an adaptive reward mechanism is executed by a processor, the steps of the text classification method based on an adaptive reward mechanism as described in any of the above items are implemented.
[0057] The text classification method, system, terminal and medium based on the adaptive reward mechanism provided by the present invention have the following beneficial effects compared with the prior art: a simple one-hot encoding strategy is used to process the hierarchical relationship of labels, and a hierarchical loss function and a cosine similarity smoothing reward mechanism are introduced, which significantly improves the classification accuracy and robustness, enhances the model's ability to learn hierarchical relationships, and provides more accurate hierarchical predictions. The method is suitable for processing a large number of complex text classification tasks, especially in areas where the hierarchical relationship of labels needs to be considered, which also provides the method with broad application prospects. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] In order to more clearly illustrate the embodiments of the present invention or the technical solutions of the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0059] Figure 1 It is a flowchart of a text classification method based on an adaptive reward mechanism provided by an embodiment of the present invention.
[0060] Figure 2 It is a schematic diagram of the data preprocessing principle flow chart.
[0061] Figure 3 It is a schematic diagram of the hierarchical text classification model structure.
[0062] Figure 4 The present invention provides a text classification system based on an adaptive reward mechanism according to an embodiment of the present invention.
[0063] Figure 5 It is a schematic diagram of the structure of a terminal provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0064] In order to enable those skilled in the art to better understand the scheme of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0065] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art of the present invention. The terms used in the specification of the present invention herein are only for the purpose of describing specific embodiments and are not intended to limit the present invention.
[0066] Figure 1 : is a flowchart of a text classification method based on an adaptive reward mechanism provided by an embodiment of the present invention. Figure 1 The execution subject may be a text classification system based on an adaptive reward mechanism. The text classification method based on an adaptive reward mechanism provided in the embodiment of the present invention is executed by a computer device, and accordingly, the text classification system based on an adaptive reward mechanism runs in the computer device. According to different requirements, the order of the steps in the flowchart can be changed, and some can be omitted.
[0067] like Figure 1 As shown, the method includes the following steps.
[0068] S1, obtain text data and preprocess it to construct training samples; during the preprocessing process, use one-hot encoding to process the hierarchical labels.
[0069] S2, based on the hierarchical relationship between labels, introduces an adaptive reward mechanism based on cosine similarity to construct a hierarchical loss function.
[0070] S3, build a hierarchical text classification model, use training samples to train the hierarchical text classification model, and use the back propagation algorithm to adjust the model parameters based on the hierarchical loss function during the training process.
[0071] S4, classify the target text using the trained hierarchical text classification model.
[0072] This embodiment uses a simple one-hot encoding strategy to process the hierarchical relationship of labels, and introduces a hierarchical loss function and a cosine similarity smoothing reward mechanism, which significantly improves the accuracy and robustness of classification, enhances the model's ability to learn hierarchical relationships, and provides more accurate hierarchical predictions.
[0073] In order to further understand the present invention, a specific embodiment is provided below to further illustrate the present invention in detail.
[0074] 1. Data Acquisition
[0075] This specific embodiment obtains text data from a web page, trains a classification model based on the text data in the web page, and implements classification processing of the text data in the web page. The text data is obtained from the web page through the following steps.
[0076] Step 1: Configure the crawler tool to simulate the user's browser behavior, including adding the target request header.
[0077] Step 2: Run the crawler tool to parse the web page and set the waiting strategy.
[0078] Step 3: Extract the content of the HTML element according to the regular expression of the crawler tool.
[0079] Step 4: For data that needs to be obtained through API interfaces, use the HTTP request library to call these API interfaces.
[0080] Step 5: Use the redis collection data structure to filter and store non-duplicate data sets.
[0081] Step 6: Store the processed data in an Excel spreadsheet.
[0082] For example, the crawler tool selenium is used to simulate loading of several URLs pointing to the news bulletin webpage to obtain the content of the webpage. The selenium test is directly run in the browser to simulate the user's operation behavior.
[0083] First, Selenium is configured to simulate the browser behavior of ordinary users, including adding appropriate request headers to prevent websites from identifying crawlers and blocking IP addresses. Then, Selenium's WebDriver is used to parse the web page and set appropriate waiting strategies (such as explicit waiting) to ensure that all key elements of the web page have been loaded. If the request fails, the web page elements are loaded again after increasing the waiting time to ensure data integrity. Finally, through DOM operations, the content of the corresponding HTML elements is extracted according to the XPath expression, especially the body of the web page. The extracted content includes: FAQs and typical messages.
[0084] In addition, for data that needs to be obtained through specific API interfaces, use HTTP request libraries (such as requests) to call these interfaces to fully obtain the required data. Use redis to remove duplicate data to ensure the uniqueness of the data. Store the processed data in an Excel table to facilitate subsequent data analysis and processing.
[0085] 2. Data preprocessing
[0086] Figure 2 It is a schematic diagram of the data preprocessing principle flow chart, which specifically includes the following steps.
[0087] Step 1: Clean the text data and remove irrelevant characters, including using a crawler tool to remove HTML tags from each row of content in the Excel table in turn, and using regular expressions to remove useless characters and extra spaces.
[0088] The collected text data is cleaned to remove irrelevant characters, including numbers, English letters, blank characters, HTML tags and other noise.
[0089] For example, the crawled text data is stored in an Excel table, where one row represents a problem description. Each problem description contains the content described by the caller recorded by the operator. Some of the descriptions contain more or less unclear, inaccurate information, and some useless characters. For example, "Mr. Li: He is the owner of the west unit of x building x unit x building x in xxx community. The air conditioner installed in the storefront "Renhe Tea House" (xxxxxxxx) in the community is close to his home, which causes noise disturbance. Please handle and reply to the caller. "Processing results of Mr. Li's call inquiry". Use BeautifulSoup to remove HTML tags, use regular expressions to remove useless characters and extra spaces, and extract useful text information. The processed data is: "Mr. Li is the owner of the west unit of building No. in the community. The air-conditioning outdoor unit installed by Renhe Tea House in the storefront of the community is close to his home, causing noise disturbance. Please handle it and reply to the caller. Processing results of Mr. Li's call inquiry".
[0090] Step 2: Use the word segmentation tool to perform word segmentation on the cleaned text data.
[0091] An optional implementation method is to use the Jieba word segmentation tool to perform word segmentation processing on the cleaned text and divide the text into words or phrases. Specifically, the Jieba word segmentation tool provides a paddle mode to perform word segmentation and part-of-speech tagging on the cleaned data, and use the above example to illustrate and display the segmented data, "Mr. Li nr / whose r / is v / community n / number m / building n / unit nr / west household n / owner n / the r / community n / storefront n / Renhe nr / tea house n / installed v / of uj / air conditioner n / outdoor unit n / distance n / his home r / is d / closer to a / there is v / noise n / disturbing the public n / please v / process v / and c / reply v / caller v / person n / Mr. Li nr / caller v / query processing nr / result n ".
[0092] Step 3: based on a predefined stop word list, remove stop words from the text data after word segmentation processing.
[0093] This specific embodiment pre-customizes the stop word list, for example, summarizes the Chinese stop word list, the Harbin Institute of Technology stop word list, the Baidu stop word list, and the Sichuan University Machine Intelligence Laboratory stop word library, and calculates 500 candidate words with large word frequency but low TF-IDF value for each category, and selects 500 of them as exclusive stop words for the category through manual screening, and adds them to the stop word list. The screened stop words are shown in Table 1 below.
[0094] Table 1: Custom stop words
[0095]
[0096] When removing stop words, in addition to relying on the general stop word list and the exclusive stop word list, we also rely on parts of speech to filter out some meaningless words or characters, such as personal names (nr), place names (ns), organization names (nt), auxiliary words (u), conjunctions (c), pronouns (p), measure words (q), prepositions (p), etc., which are not helpful for understanding the document information. For example, in the following example: "Ms. Wang: At 9:10 on November 3rd, she took the No. 6 bus (from west to east) and the bus did not stop at the ××× station. On November 29th, she took the No. 6 bus and the bus stopped at the ××× station. Now she calls to suggest that the bus company unify the stop sites and hopes that the relevant unit can verify and handle it. (It has been explained that there is no stop at this site for the No. 6 bus)", the text information obtained after cleaning and word segmentation is "take, bus, station, stop, bus company".
[0097] Step 4, for the text data after word segmentation, use the Word2Vector model to perform vector encoding processing.
[0098] Convert the text data into 256-dimensional word vectors and input them into the model. Before that, it is necessary to train the word vector model (Word2Vector) to capture the semantic relationships between words. The Skip-gram model can be used for word vector training. After training, the word vectors are obtained. For example: "take: [0.0123456789, -0.3456789123, 0.56789012, 0.1234567890, 0.2345678901, -0.3456789012, 0.4567890123, -0.5678901234, 0.6789012345, -0.7890123456,...]".
[0099] Step 5, perform one-hot encoding processing on the already labeled hierarchical labels.
[0100] This specific embodiment performs one-hot encoding processing on the already labeled labels to reduce the computational complexity and improve the training efficiency.
[0101] (III) Construction and Training of Hierarchical Text Classification Model
[0102] Step 1, construct a hierarchical text classification model.
[0103] Figure 3 It is a schematic diagram of the hierarchical text classification model structure. The hierarchical text classification model of this specific embodiment includes an input layer, a hidden layer, and an output layer.
[0104] (1) Input Layer
[0105] The word vectors and label encodings are input into a neural network model for training.
[0106] (2) Hidden layer
[0107] A composite neural network structure is adopted, integrating a Convolutional Neural Network (CNN), a Bidirectional Long Short-Term Memory Network (BiLSTM), and a Self-Attention mechanism, aiming to enhance the model's ability to deeply understand text content.
[0108] (2.1) Convolutional Neural Network (CNN) layer: The hidden layer first contains two layers of Convolutional Neural Networks (CNN). These two CNNs use three different sizes of convolutional kernels (3, 4, 5) respectively, aiming to capture n-gram features of different lengths in the text. By extracting and combining these features of different lengths, the semantic information of the text can be enriched.
[0109] (2.2) Bidirectional Long Short-Term Memory Network (BiLSTM): Subsequently, the combined feature vectors are input into a Bidirectional Long Short-Term Memory Network (BiLSTM). By processing information in both the forward and backward directions simultaneously, BiLSTM can pay attention to the long-term dependencies and context information of the text, thus making up for the deficiency of CNN in capturing long-distance dependencies.
[0110] Exemplary:
[0111] When processing "taking the bus", BiLSTM not only pays attention to the relationship between "taking" and "bus", but also to their relationship with subsequent words (such as "stop", "parking").
[0112] When processing "parking bus company", BiLSTM pays attention to the relationship between "parking" and "bus company", and at the same time takes into account the context information of the words (such as "bus" and "stop") before "bus company".
[0113] (2.3) Self-Attention mechanism: The Self-Attention mechanism is connected after the BiLSTM layer. By calculating the importance of each word in the entire sequence and assigning different weights, it focuses more precisely on the key context information. The Self-Attention mechanism can be calculated in parallel, accelerating the training process and not suffering from the vanishing gradient problem like Recurrent Neural Networks (RNN).
[0114] An example of the Self-Attention mechanism is as follows.
[0115] (a) Relationships between words:
[0116] ① "Taking": Represents an action and is related to "bus".
[0117] ② "Bus": refers to a means of transportation, and is related to "station", "parking" and "bus company".
[0118] ③“Site”: indicates location, and is related to “parking”.
[0119] ④ "Stop": indicates an action, and is related to "station" and "bus".
[0120] ⑤“Bus company”: refers to an organization and is related to “bus”.
[0121] (b) Calculate the importance of each word:
[0122] ①For “riding”, the model will notice that it has a direct relationship with “bus” because “riding” is often the operation of a means of transportation.
[0123] ②For “bus”, the model will notice that it is related to “ride”, “station”, “parking”, and “bus company” because these words together describe the scene of taking the bus.
[0124] (c) Assign weights:
[0125] ① The model will assign a higher weight to the relationship between "ride" and "bus" because they are closely related in the sentence.
[0126] ② Similarly, the weights between “bus” and “station”, and “bus” and “parking” will also be high because these words together describe the operation of the bus.
[0127] ③“Bus company” and “bus” have higher weights because it is the management entity of the bus.
[0128] (d) Example weight matrix (attention score):
[0129] Table 2: Attention scores
[0130]
[0131] Each row represents the attention weight of the current word to other words. For example, the attention weight of "ride" to "bus" is 0.4, and the attention weight to "bus company" is 0.3. So the model tends to be more inclined to the relationship between "ride" and "bus", so it will pay more attention to "bus" when processing "ride".
[0132] (3) Output layer
[0133] The softmax function is used to convert the vector output by the model into the probability distribution of each classification label, so as to achieve the prediction of text categories and perform multi-classification tasks. It should be noted that the number of nodes in the output layer is the same as the number of label levels, and each output node outputs the label prediction result of one level.
[0134] Step 2: Construct a hierarchical loss function.
[0135] This specific embodiment introduces a hierarchical loss function based on the hierarchical relationship between labels, and introduces an adaptive reward mechanism based on cosine similarity for construction, fully considers the hierarchical relationship between labels, and introduces an adaptive reward mechanism, through the calculation formula of the total loss, so that the model can better capture and utilize these hierarchical information during the training process.
[0136] Constructing a hierarchical loss function specifically includes the process.
[0137] (1) For each label level, construct a basic cross entropy loss function, expressed as:
[0138] (1)
[0139] In the formula, Indicates Cross entropy loss at label level, Indicates The true label distribution at the label level, Indicates The predicted label probability distribution at the label level, Indicates the number of label levels.
[0140] (2) Taking into account the hierarchical dependencies, a penalty factor is applied to the cross entropy when calculating the loss beyond the top layer, expressed as:
[0141] (2)
[0142] In the formula, Indicates The penalty factor at the label level, ; represents an indicator function if For true, is equal to 0 otherwise it is equal to 1.
[0143] (3) Based on the penalty factor, the cross entropy loss of the label level except the first layer is adjusted. The adjusted cross entropy loss is expressed as:
[0144] (3)
[0145] In the formula, Indicates Weighting factors at the label level; .
[0146] In this specific embodiment, the weighting factors are dynamically adjusted according to the accuracy of the model's predictions at adjacent levels, and consideration of hierarchical dependencies is introduced. Specifically, the initial weighting factors and adjustment steps are configured in advance for each label level; during the training process, if the prediction of the previous label level is wrong, the weighting factor for the next label level is increased based on the initial weighting factor according to the adjustment step. For example, if the model predicts incorrectly at the second level, the loss weight of the third level prediction will increase. This approach not only encourages the model to improve prediction accuracy at the individual level, but also prompts it to consider hierarchical dependencies.
[0147] (4) Calculate the cosine similarity of each label level, the expression is:
[0148] (4)
[0149] In the formula, .
[0150] (5) Introduce an adaptive reward mechanism to calculate the smooth reward, the expression is:
[0151] (5)
[0152] In the formula, .
[0153] (6) Construct the total loss, expressed as:
[0154] (6)
[0155] In the formula, represents the reward factor, Indicates taking the average value.
[0156] The total loss formula (6) is the final constructed hierarchical loss function.
[0157] In the framework of this specific embodiment, the prediction of document labels is performed layer by layer, and the hierarchical loss function is used to measure the difference between the predicted results and the true labels. Specifically, the set of text objects is represented as X = {X1, X2, X3, ..., X N}, where N represents the total number of text objects. For each text object X j , and its corresponding label set is represented as L i = {L j1 , L j2 , ..., L jR}, L jiIndicates the number of labels of the jth text object at layer i. Each text object X j Contains a sequence of words, namely X j = [x1, x2, x3, ..., x K ], “K” is the total number of words in the text object.
[0158] To simplify the description, three levels of labels are taken as an example for analysis. Table 3 below is a label set for a text object.
[0159] Table 3: Label set of text objects
[0160] Label Set Text Object First-level label: Urban comprehensive Second-level label: Urban management Third-level label: Noise pollution Mr. Zhang: There is a subway under construction near the Laoshan Campus of the Affiliated Hospital of Qingdao University on Yinchuan East Road, Laoshan District, and the noise is disturbing the residents. I require departments to coordinate to stop the noise. Please ask the responsible department to implement the handling and give a reply.
[0161] The goal of this method is to guide the model to learn the hierarchical relationship between labels using a hierarchical loss function and accurately predict each text object X j The corresponding label set L. The core approach of this method is to design a hierarchical loss function, which aims to guide the model to capture and utilize the hierarchical relationship between labels during training.
[0162] This loss function is built on top of the cross entropy loss and is commonly used for multi-class problems. However, with one-hot encoding, the direct connection between labels is no longer at the encoding level. To address this issue, this method introduces a smoothed reward mechanism using cosine similarity. This mechanism evaluates the similarity between the model's predictions and the true labels and provides feedback.
[0163] When the cosine similarity between the predicted label and the true label is high, the smooth reward is close to 1, conversely, the smooth reward is close to 0, encouraging the model to make more accurate hierarchical predictions. In addition, in order to strengthen the model's learning of hierarchical relationships, this method adds a weight factor, lambda_weight, to the loss function.
[0164] Example description: For the above example, the model's prediction result is {"urban integration", "urban management", "construction disturbance"}. The loss calculated by formula (1) is ; For each penalty factor The calculation is as follows: The first-level label prediction is correct, ; The secondary label prediction is correct, ; If the third-level label prediction is wrong, then The loss after adjustment of the second-level label and the third-level label is calculated by formula (3):
[0165] , .
[0166] The reward is smoothed by calculating the difference between the predicted label and the true label: , , .
[0167] The total loss adjusted for cosine similarity is: By introducing the above adaptive reward mechanism, the model can better capture the hierarchical relationship between labels during the training process, thereby improving the accuracy and robustness of classification.
[0168] Step 3: Hierarchical text classification model training.
[0169] The hierarchical text model is trained using the preprocessed data, and the model parameters are adjusted using the back-propagation algorithm based on the hierarchical loss function, so that the model can accurately classify the input text data.
[0170] First, the preprocessed data is reasonably divided into a training set and a test set. The model is trained using the data in the training set to learn how to accurately classify text data. After training, the model will classify the text data in the training set and output preliminary classification results. An independent test set is then used to verify the accuracy and reliability of these classification results. The performance of the model can be evaluated by comparing the true labels of the test set with the classification results predicted by the model. If poor performance is found, the model parameters will be adjusted based on the evaluation results and the model will be retrained to optimize its classification ability. This process will be repeated until satisfactory classification accuracy and stability are achieved.
[0171] The following is a verification result. After crawling and data cleaning, a data set containing 100,000 data items with 3 levels and 91 labels was successfully constructed and applied. In order to comprehensively evaluate the performance of this method, it was compared with two mainstream text classification algorithms: one is a flat classification algorithm (such as Fast Text and SVM, which do not consider the hierarchical structure of the data), and the other is a hierarchical text classification algorithm (such as HDLTex, a representative hierarchical classification model). The comparison results are shown in Table 4 (comparison of classification performance with flat models) and Table 5 (comparison of classification performance with hierarchical type models), from which it can be clearly seen that the HC-CREX model of this method is superior to other models in terms of accuracy.
[0172] Table 4: Classification performance comparison with the flat model
[0173]
[0174] Table 5: Classification performance comparison with the hierarchical type model
[0175]
[0176] An embodiment of a text classification method based on an adaptive reward mechanism is described in detail above. Based on the text classification method based on an adaptive reward mechanism described in the above embodiment, an embodiment of the present invention also provides a text classification system based on an adaptive reward mechanism corresponding to the method.
[0177] Figure 4 4 is a schematic block diagram of a text classification system based on an adaptive reward mechanism provided by an embodiment of the present invention. The text classification system 400 based on an adaptive reward mechanism can be divided into multiple functional modules according to the functions performed by the text classification system, such as Figure 4 The functional modules may include: a sample construction module 410, a hierarchical loss function construction module 420, a text classification execution module 430, and a text classification execution module 440. The module referred to in the present invention refers to a series of computer program segments that can be executed by at least one processor and can complete fixed functions, which are stored in a memory.
[0178] Sample construction module 410: obtains text data and preprocesses it to construct training samples; during the preprocessing process, one-hot encoding is used to process the hierarchical labels.
[0179] Hierarchical loss function construction module 420: Based on the hierarchical relationship between labels, a hierarchical loss function is constructed by introducing an adaptive reward mechanism based on cosine similarity.
[0180] Hierarchical text classification model training module 430: construct a hierarchical text classification model, use training samples to train the hierarchical text classification model, and use the back propagation algorithm to adjust the model parameters based on the hierarchical loss function during the training process.
[0181] Text classification execution module 440: classifies the target text using the trained hierarchical text classification model.
[0182] In an optional embodiment, the hierarchical text classification model training module 430 is also used to pre-configure an initial weighting factor and an adjustment step for each label level; during the training process, if the prediction of the previous label level is wrong, the weighting factor for the next label level is increased based on the initial weighting factor according to the adjustment step.
[0183] The text classification system based on the adaptive reward mechanism of this embodiment is used to implement the aforementioned text classification method based on the adaptive reward mechanism. Therefore, the specific implementation method of the system can be seen in the embodiment section of the text classification method based on the adaptive reward mechanism in the previous text. Therefore, its specific implementation method can refer to the description of the corresponding embodiments of each part, which will not be introduced in detail here.
[0184] In addition, since the text classification system based on the adaptive reward mechanism of this embodiment is used to implement the aforementioned text classification method based on the adaptive reward mechanism, its function corresponds to that of the aforementioned method and will not be described in detail here.
[0185] Figure 5 A schematic diagram of the structure of a terminal 500 provided in an embodiment of the present invention includes: a processor 510, a memory 520 and a communication unit 530. The processor 510 is used to implement the following steps when implementing the text classification program based on the adaptive reward mechanism stored in the memory 520:
[0186] Obtain text data and preprocess it to construct training samples; use one-hot encoding to process hierarchical labels during preprocessing;
[0187] Based on the hierarchical relationship between labels, an adaptive reward mechanism based on cosine similarity is introduced to construct a hierarchical loss function;
[0188] Construct a hierarchical text classification model, use training samples to train the hierarchical text classification model, and use the back propagation algorithm to adjust the model parameters based on the hierarchical loss function during the training process;
[0189] Use the trained hierarchical text classification model to classify the target text.
[0190] The terminal 500 includes a processor 510, a memory 520 and a communication unit 530. These components communicate via one or more buses. It will be appreciated by those skilled in the art that the server structure shown in the figure does not limit the present invention, and it may be a bus structure or a star structure, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0191] The memory 520 can be used to store the execution instructions of the processor 510, and the memory 520 can be implemented by any type of volatile or non-volatile storage terminal or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. When the execution instructions in the memory 520 are executed by the processor 510, the terminal 500 can execute some or all of the steps in the following method embodiments.
[0192] The processor 510 is the control center of the storage terminal, and uses various interfaces and lines to connect various parts of the entire electronic terminal. It runs or executes software programs and / or modules stored in the memory 520, and calls data stored in the memory to perform various functions of the electronic terminal and / or process data. The processor can be composed of an integrated circuit (IC), for example, it can be composed of a single packaged IC, or it can be composed of multiple packaged ICs with the same or different functions. For example, the processor 510 can only include a central processing unit (CPU). In the embodiment of the present invention, the CPU can be a single computing core or multiple computing cores.
[0193] The communication unit 530 is used to establish a communication channel so that the storage terminal can communicate with other terminals, receive user data sent by other terminals or send user data to other terminals.
[0194] The present invention also provides a computer storage medium, wherein the storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM).
[0195] The computer storage medium stores a text classification program based on an adaptive reward mechanism, and when the text classification program based on an adaptive reward mechanism is executed by a processor, the following steps are implemented:
[0196] Obtain text data and preprocess it to construct training samples; use one-hot encoding to process hierarchical labels during preprocessing;
[0197] Based on the hierarchical relationship between labels, an adaptive reward mechanism based on cosine similarity is introduced to construct a hierarchical loss function;
[0198] Construct a hierarchical text classification model, use training samples to train the hierarchical text classification model, and use the back propagation algorithm to adjust the model parameters based on the hierarchical loss function during the training process;
[0199] Use the trained hierarchical text classification model to classify the target text.
[0200] Those skilled in the art can clearly understand that the technology in the embodiments of the present invention can be implemented by means of software plus a necessary general hardware platform. Based on this understanding, the technical solution in the embodiments of the present invention, in essence or in other words, the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, and other media that can store program codes, including several instructions for enabling a computer terminal (which can be a personal computer, a server, or a second terminal, a network terminal, etc.) to execute all or part of the steps of the methods described in each embodiment of the present invention.
[0201] In the several embodiments provided by the present invention, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0202] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0203] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0204] The above disclosure is only a preferred embodiment of the present invention, but the present invention is not limited thereto. Any non-creative changes that can be thought of by a person skilled in the art, as well as several improvements and modifications made without departing from the principle of the present invention, should fall within the protection scope of the present invention.
Claims
1. A text classification method based on an adaptive reward mechanism, characterized in that: The following steps are involved: Obtain and preprocess text data to construct training samples; One-hot encoding is used to process the hierarchical labels during preprocessing; Based on the hierarchical relationship between labels, an adaptive reward mechanism based on cosine similarity is introduced to construct a hierarchical loss function; Construct a hierarchical text classification model, use training samples to train the hierarchical text classification model, and use the back propagation algorithm to adjust the model parameters based on the hierarchical loss function during the training process; Use the trained hierarchical text classification model to classify the target text; Among them, based on the hierarchical relationship between labels, an adaptive reward mechanism based on cosine similarity is introduced to construct a hierarchical loss function, which specifically includes: Step 1: For each label level, construct a basic cross entropy loss function, expressed as: In the formula, Indicates Cross entropy loss at label level, Indicates The true label distribution at the label level, Indicates The predicted label probability distribution at the label level, Indicates the number of label levels; Step 2: Taking hierarchical dependencies into account, a penalty factor is applied to the cross entropy when calculating the loss beyond the top layer, expressed as: In the formula, Indicates The penalty factor at the label level, ; represents an indicator function if For true, is equal to 0, otherwise it is equal to 1; Step 3: Adjust the cross entropy loss of the label level except the first layer based on the penalty factor. The adjusted cross entropy loss is expressed as: In the formula, Indicates Weighting factors at the label level; ; Step 4: Calculate the cosine similarity of each tag level. The expression is: In the formula, ; Step 5: Introduce an adaptive reward mechanism to calculate the smooth reward. The expression is: In the formula, ; Step 6, construct the total loss, the expression is: In the formula, represents the reward factor, Indicates taking the average value.
2. The text classification method based on the adaptive reward mechanism according to claim 1 is characterized in that: The method further comprises the following steps: Pre-configure the initial weighting factor and adjustment step size for each label level; During the training process, if the prediction of the previous label level is wrong, the weighting factor for the next label level is increased based on the initial weighting factor according to the adjustment step size.
3. The text classification method based on the adaptive reward mechanism according to claim 1 or 2, characterized in that: Obtaining text data specifically includes: Configure the crawler to simulate the user's browser behavior, including adding target request headers; Run the crawler tool, parse the web page and set the waiting strategy; Extract the content of HTML elements according to the regular expression of the crawler tool; For data that needs to be obtained through API interfaces, use the HTTP request library to call these API interfaces; Use redis's collection data structure to filter and store non-duplicate data sets; Store the processed data in an Excel spreadsheet.
4. The text classification method based on the adaptive reward mechanism according to claim 3 is characterized in that: Preprocess the text data, including: Clean the text data and remove irrelevant characters, including using crawler tools to remove HTML tags from each row of Excel spreadsheets and using regular expressions to remove useless characters and extra spaces; Use word segmentation tools to perform word segmentation on the cleaned text data; Based on a predefined stop word list, stop words are removed from the text data after word segmentation processing; The text data after word segmentation is processed by using the Word2Vector model for vector encoding. Perform one-hot encoding on the labeled level labels.
5. The text classification method based on the adaptive reward mechanism according to claim 1 or 2, characterized in that: The hierarchical text classification model includes an input layer, a hidden layer, and an output layer; The hidden layer first extracts the merged features through two layers of convolutional neural networks using three different sizes of convolution kernels, and then inputs the merged features into a bidirectional long short-term memory network, and then connects the self-attention mechanism after the bidirectional long short-term memory network for processing; The output layer uses the softmax activation function.
6. A text classification system based on an adaptive reward mechanism, characterized in that: include, Sample construction module: obtains text data and preprocesses it to construct training samples; One-hot encoding is used to process the hierarchical labels during preprocessing; Hierarchical loss function construction module: Based on the hierarchical relationship between labels, an adaptive reward mechanism based on cosine similarity is introduced to construct a hierarchical loss function; Hierarchical text classification model training module: build a hierarchical text classification model, use training samples to train the hierarchical text classification model, and use the back propagation algorithm to adjust the model parameters based on the hierarchical loss function during the training process; Text classification execution module: classifies the target text using the trained hierarchical text classification model; Among them, the hierarchical loss function construction module is based on the hierarchical relationship between labels, and introduces an adaptive reward mechanism based on cosine similarity to construct a hierarchical loss function, which specifically includes: Step 1: For each label level, construct a basic cross entropy loss function, expressed as: In the formula, Indicates Cross entropy loss at label level, Indicates The true label distribution at the label level, Indicates The predicted label probability distribution at the label level, Indicates the number of label levels; Step 2: Taking hierarchical dependencies into account, a penalty factor is applied to the cross entropy when calculating the loss beyond the top layer, expressed as: In the formula, Indicates The penalty factor at the label level, ; represents an indicator function if For true, is equal to 0, otherwise it is equal to 1; Step 3: Adjust the cross entropy loss of the label level except the first layer based on the penalty factor. The adjusted cross entropy loss is expressed as: In the formula, Indicates Weighting factors at the label level; ; Step 4: Calculate the cosine similarity of each tag level. The expression is: In the formula, ; Step 5: Introduce an adaptive reward mechanism to calculate the smooth reward. The expression is: In the formula, ; Step 6, construct the total loss, the expression is: In the formula, represents the reward factor, Indicates taking the average value.
7. A terminal, characterized in that: include: A memory, used for storing a text classification program based on an adaptive reward mechanism; A processor, configured to implement the steps of the text classification method based on the adaptive reward mechanism as described in any one of claims 1 to 5 when executing the text classification program based on the adaptive reward mechanism.
8. A computer-readable storage medium, characterized in that: The readable storage medium stores a text classification program based on an adaptive reward mechanism, and when the text classification program based on an adaptive reward mechanism is executed by a processor, the steps of the text classification method based on an adaptive reward mechanism as described in any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Large-scale category hierarchical text classification method
CN113590819A
Construction method of government purchase item hierarchical classification model
CN113946678A
