Text classification method and device, equipment and storage medium

By establishing the correlation between category tokens and sample tokens, and using downsampling technology to process long texts, the accuracy of text classification is improved, the problem of low accuracy of multi-label classification in the existing technology is solved, and a higher level of accuracy is achieved. Accuracy of classification results.

CN118779446AActive Publication Date: 2024-10-15IFLYTEK CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202410779956.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-17
Publication Date
2024-10-15
Estimated Expiration
2044-06-17

AI Technical Summary

Technical Problem

When existing text classification methods deal with texts involving different topics, the accuracy of the classification results is not high. Especially in multi-label classification scenarios, it is difficult to accurately identify multiple categories of text.

Method used

By obtaining the token of the text to be classified, and using the prefix information and classification model, we establish the correlation between tokens of different categories and the correlation between sample tokens and category tokens. The obtained classification model and prefix information are trained to improve classification accuracy. . Specific methods include downsampling when the number of tokens is greater than the preset number, obtaining feature information, and inputting it into the classification model to obtain classification results.

Benefits of technology

It improves the accuracy of text classification, especially in multi-label classification scenarios, it can more accurately identify different categories of text, enhances the accuracy of classification models and prefix information, and solves the problem of low classification results in the existing technology. .

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118779446A_ABST
    Figure CN118779446A_ABST
Patent Text Reader

Abstract

The invention provides a text classification method and device, equipment and a storage medium. The method comprises the steps of obtaining a to-be-classified text; determining at least two tokens corresponding to the text to be classified; based on the at least two tokens, the at least one piece of prefix information and a classification model, obtaining a classification result of the to-be-classified text; the classification model and the prefix information are obtained by inputting at least one sample token corresponding to a sample text and category tokens corresponding to the sample prefix information into an initial classification model and training the initial classification model on the basis of the correlation between the category tokens and the correlation between the sample tokens and the category tokens; each piece of prefix information is used for modeling semantics of each category. According to the invention, the text classification accuracy can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a text classification method, device, equipment and storage medium. Background Art

[0002] Text classification technology is a method of classifying text using machine learning and natural language processing technology. Its core idea is to divide text data into different categories or labels in order to better understand and process large-scale text data.

[0003] Existing text classification methods include statistical-based methods such as Naive Bayes or Support Vector Machines, or deep learning-based methods such as Convolutional Neural Networks or Recurrent Neural Networks, as well as traditional rule and pattern matching methods.

[0004] However, the same text may involve different topics, such as sports and entertainment. When the above text classification method classifies texts involving different topics, the classification results are not accurate. Summary of the invention

[0005] The present invention provides a text classification method, device, equipment and storage medium, which are used to solve the defect that the classification result of text classification in the prior art is not accurate enough, and achieve the purpose of improving the accuracy of the classification result of text classification.

[0006] The present invention provides a text classification method, comprising: Get the text to be classified; Determine at least two tokens corresponding to the text to be classified; Based on the at least two tokens, at least one prefix information and a classification model, a classification result of the text to be classified is determined; the classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and a category token corresponding to the sample prefix information into an initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token, and each prefix information is used to model the semantics of each category.

[0007] According to a text classification method provided by the present invention, the classification result of the text to be classified is determined based on the at least two tokens, at least one prefix information and a classification model, including: When the number of the tokens is greater than a preset number, downsampling the at least two tokens to obtain at least one feature information; The at least one feature information and the prefix information are input into the classification model to obtain a classification result output by the classification model.

[0008] According to a text classification method provided by the present invention, when the number of the tokens is greater than a preset number, downsampling the at least two tokens to obtain at least one feature information includes: Determining a sampling interval based on the number of the tokens and the preset number; The at least two tokens are sampled at intervals based on the sampling interval to obtain the at least one feature information.

[0009] According to a text classification method provided by the present invention, when the number of the tokens is greater than a preset number, downsampling the at least two tokens to obtain at least one feature information includes: Determining a target quantity based on the quantity of the tokens and the preset quantity; The average values ​​of the embedding vectors of the target number of the tokens are determined in sequence to obtain the at least one feature information.

[0010] According to a text classification method provided by the present invention, the classification model includes a plurality of text classification layers and a downsampling layer arranged between two adjacent text classification layers; The step of determining the classification result of the text to be classified based on the at least two tokens, the at least one prefix information and the classification model includes: The at least two tokens and the at least one prefix information are input into a first text classification layer, first target feature information corresponding to the at least two tokens output by the first text classification layer is downsampled through a downsampling layer connected to the first text classification layer, the downsampled first target feature information is input into a second text classification layer, and second target feature information output by the second text classification layer is downsampled through a downsampling layer connected to the second text classification layer, until the classification result is output.

[0011] According to a text classification method provided by the present invention, the classification model is trained based on the following method: obtaining at least two sample tokens corresponding to the sample text and a category token corresponding to the sample prefix information; The at least two sample tokens and the category token are input into the initial classification model, and the correlation between the category tokens and the correlation between the sample token and the category token are determined through a self-attention mechanism; the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token to obtain the classification model and the prefix information.

[0012] According to a text classification method provided by the present invention, the method further includes: In the case where the classification result indicates that the text to be classified does not include illegal content, the text to be classified is input into a large language model to obtain reply information corresponding to the text to be classified; The reply information is determined as a new text to be classified, and a new classification result corresponding to the new text to be classified is determined; when the new classification result indicates that the reply information does not include illegal content, the reply information is output through the large language model.

[0013] The present invention also provides a text classification device, comprising: An acquisition module is used to obtain the text to be classified; A determination module, used to determine at least two tokens corresponding to the text to be classified; An input module is used to determine the classification result of the text to be classified based on the at least two tokens, at least one prefix information and a classification model; the classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information into an initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token, and each prefix information is used to model the semantics of each category.

[0014] 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 when the processor executes the program, any of the above-mentioned text classification methods is implemented.

[0015] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the text classification method described above is implemented.

[0016] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above-mentioned text classification methods.

[0017] The text classification method, device, equipment and storage medium provided by the present invention can determine the classification result of the text to be classified by obtaining the text to be classified and determining at least two tokens corresponding to the text to be classified, based on the at least two tokens, at least one pre-trained prefix information for modeling the semantics of each category and the classification model. Among them, the classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information into the initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token. Since the classification model and the prefix information are trained by establishing the correlation between different category tokens and the correlation between the category token and each sample token, in this way, after the correlation is constructed, if there is a conceptual intersection between different categories modeled by multiple prefix information, when the classification model and prefix information trained in the above manner are classified, the intersection and correlation between the above different categories will be taken into account, so that the classification model and prefix information trained are more accurate. Therefore, when the text to be classified is classified based on the classification model and prefix information, the accuracy of the classification result is also higher. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are 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.

[0019] Figure 1 A flowchart of a text classification method provided by an embodiment of the present invention.

[0020] Figure 2 This is one of the structural schematic diagrams of the downsampling method provided in an embodiment of the present invention.

[0021] Figure 3 The second structural diagram of the downsampling method provided in the embodiment of the present invention.

[0022] Figure 4 The third structural diagram of the downsampling method provided in an embodiment of the present invention.

[0023] Figure 5 The fourth structural diagram of the downsampling method provided in an embodiment of the present invention.

[0024] Figure 6 A schematic diagram of downsampling through a model provided in an embodiment of the present invention.

[0025] Figure 7 A schematic diagram of the training process of the classification model provided in an embodiment of the present invention.

[0026] Figure 8 A schematic diagram of the structure of a text classification device provided in an embodiment of the present invention.

[0027] Fig. 9 An example physical structure diagram of an electronic device is provided. DETAILED DESCRIPTION

[0028] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0029] Text classification technology has a wide range of applications in fields such as sentiment analysis, spam filtering, and news classification. In the prior art, text classification is usually performed through statistical methods, deep learning methods, or traditional rule and pattern matching methods. The above methods have high classification accuracy when classifying a single type of text. However, in text classification tasks, each text may be assigned multiple labels or categories. A specific example of multi-label text classification is content classification on social media. Consider a social media platform where users can post content on various topics, such as sports, entertainment, and technology. The content posted by each user may involve multiple topics, so these contents need to be classified by multi-labels. For example, a piece of content posted by a user may involve both sports and technology, such as discussing the impact of the artificial intelligence industry on the sports industry. In this case, when multi-label classification is performed on this content, the two labels of sports and technology need to be identified at the same time. Through multi-label text classification, social media platforms can better provide users with personalized recommendations and content filtering services. For example, relevant content can be recommended to users based on their interest tags, or content that users are not interested in can be filtered out. Therefore, how to accurately classify texts involving different categories is a technical problem that needs to be solved urgently.

[0030] In view of the above problems, the embodiment of the present invention proposes a text classification method, in which the text to be classified can be classified by a classification model, wherein when training the classification model, at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information are input into the initial classification model, and the correlation between the category tokens and the correlation between the sample token and the category token are extracted by the initial classification model and then trained. Since each category word has semantics itself and different category labels also have correlation, the correlation between different category tokens and the correlation between the category token and each sample token can be established in this embodiment. In this way, if there is a conceptual intersection between different labels of the multi-sample prefix information after the correlation is established, it can be more helpful for multi-label classification. For example, there are both violent and vulgar categories in the security category of the large language model, and there is an intersection between these two category concepts. Therefore, after the correlation is established, the accuracy of the trained classification model and prefix information is higher. Therefore, when the text to be classified is classified based on the classification model and prefix information, the accuracy of the classification result is also higher.

[0031] Combine the following Figures 1 to 7 The text classification method provided by the embodiment of the present invention is described. The embodiment of the present invention can be applied to text classification in any scenario, and can be particularly applied to the scenario of reviewing the input content and output content of a large language model, wherein the review of the input content and the output content essentially lies in classifying the input content and the output content. The executor of the method can be an electronic device such as a terminal device, a computer, a server, a server cluster, or a specially designed text classification device, or a text classification device set in the electronic device, and the text classification device can be implemented by software, hardware, or a combination of both.

[0032] Figure 1 A flowchart of a text classification method provided by an embodiment of the present invention is shown in FIG. Figure 1 As shown, the method includes: Step 101: obtaining text to be classified.

[0033] In this embodiment, the text to be classified can be understood as text that needs to be classified, which can be text input by a user or text obtained from the Internet by crawling.

[0034] Step 102: Determine at least two tokens corresponding to the text to be classified.

[0035] In this step, the text to be classified can be decomposed into a token sequence, which includes at least two tokens. A token can be understood as the smallest unit or element that the model can process, such as a word segment or phrase fragment in the text to be classified, or a character or a combination of n consecutive words or characters.

[0036] Step 103: input at least two tokens and at least one prefix information into the classification model to determine the classification result of the text to be classified output by the classification model.

[0037] Among them, the classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information into the initial classification model, and the initial classification model is trained based on the correlation between category tokens and the correlation between sample tokens and category tokens. Each prefix information is used to characterize the semantics of each category.

[0038] In this step, after determining at least two tokens corresponding to the text to be classified, the obtained at least two tokens and at least one pre-trained prefix information can be input into the classification model to obtain the classification result of the text to be classified. Among them, each prefix information is used to model the semantics of each category, that is, each prefix information has the ability to determine the category of the text to be classified after pre-training.

[0039] When training the classification model and each prefix information, obtain at least one sample token corresponding to the sample text, and obtain at least one sample prefix information by initialization, so as to determine the category token corresponding to each sample prefix information. Input each sample token and each category token into the initial classification model, and through the self-attention mechanism, the correlation between the category tokens and the correlation between each category token and each sample token can be established, so as to train the initial classification model based on these correlations to obtain the trained prefix information and classification model. By establishing the correlation between different category tokens and the correlation between the category token and each sample token, if there is a conceptual intersection between different labels of multiple prefix information after the correlation is constructed, it can be more helpful for multi-label classification. Moreover, each prefix information after training is used to model the semantics of each category, and the different categories of the text to be classified can be identified through this prefix information.

[0040] The text classification method provided by the embodiment of the present invention can determine the classification result of the text to be classified by obtaining the text to be classified and determining at least two tokens corresponding to the text to be classified, based on the at least two tokens, at least one pre-trained prefix information for modeling the semantics of each category and the classification model. The classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information into the initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token. Since the classification model and the prefix information are trained by establishing the correlation between different category tokens and the correlation between the category token and each sample token, if there is a conceptual intersection between different categories modeled by multiple prefix information after the correlation is constructed, the classification model and the prefix information trained in the above manner will take into account the intersection and correlation between the above different categories when classifying, so that the classification model and the prefix information obtained by training are more accurate. Therefore, when the text to be classified is classified based on the classification model and the prefix information, the accuracy of the classification result is also higher.

[0041] Exemplarily, based on the above embodiment, when at least two tokens and prefix information are input into the classification model, the classification result of the text to be classified output by the classification model can be determined based on the following method: When the number of tokens is greater than a preset number, at least two tokens are downsampled to obtain at least one feature information; the at least one feature information and prefix information are input into the classification model to obtain a classification result output by the classification model.

[0042] Specifically, text classification tasks are relatively easy for short texts. With the application of large models, various language models can now also process long text information. Usually, text classification tasks have strong restrictions on the amount of calculation. For texts with hundreds or thousands of tokens, the computational overhead is very huge. For some pre-trained language models, such as BERT or ERNIE, the maximum input length they can receive is pre-set, such as 256 tokens or 512 tokens, so how to deal with the field of view of long text is a key issue in long text classification.

[0043] In the prior art, a long text is usually divided into small segments by a sliding window method, and the multiple short texts obtained after the segmentation are input into the classification model. However, the segmentation method in the prior art will cause contradictions between the different parts after segmentation. If the long text is a text with a transition relationship between the front and back, if a simple segmentation is performed, it will cause a contradiction between the two parts of the text after segmentation, resulting in errors in text classification. For example, if the text to be classified itself is a sentence with sarcastic meaning, and the first half of the sentence is a text containing a tone of praise, and the second half of the sentence is a sentence with opposite emotional polarity, if the text to be classified is truncated by a sliding window method, it will cause a contradiction between the two parts of the text after segmentation. If the maximum input length of the model is set to a larger value, it will greatly increase the amount of calculation of the model.

[0044] In view of the above problem, in an embodiment of the present invention, when the number of tokens corresponding to the text to be classified is greater than a preset number, at least two tokens can be downsampled to obtain at least one feature information, so that the at least one feature information after downsampling and the prefix information obtained by training are input into the classification model to classify the text to be classified. The preset number can be understood as the maximum input length of the model, or it can be a preset value.

[0045] When the number of tokens is less than or equal to a preset number, there is no need to downsample the at least two tokens, and the at least two tokens and the prefix information can be directly input into the classification model for classification.

[0046] In this embodiment, when the number of tokens is greater than the preset number, at least two tokens can be downsampled, so that the obtained at least one feature information and prefix information are input into the classification model to obtain the classification result output by the classification model. By downsampling at least two tokens, the purpose of increasing the field of view of the model can be achieved without truncating the text to be classified, and while increasing the field of view of the model, no additional computing power consumption will be increased.

[0047] Based on the above embodiments, the embodiments of the present invention propose multiple ways to downsample at least two tokens, including directly downsampling the input token and downsampling the input data through a model.

[0048] In one possible implementation, when the number of tokens is greater than a preset number, at least two tokens are downsampled to obtain at least one feature information. The sampling interval can be determined based on the number of tokens and the preset number, and the at least two tokens are sampled at intervals based on the sampling interval to obtain at least one feature information.

[0049] In the specific implementation process, the text to be classified is sampled only when it is a long text, that is, when the number of its corresponding tokens is greater than the preset number. Usually for long texts, there is a certain correlation and complementary relationship between the contexts of the text. As long as the entire segment after the maximum input length is not cut off, the semantics can be restored to a certain extent. In addition, BERT and Ernie are models trained by masking, which have strong semantic recovery capabilities. Therefore, in this embodiment, it can be considered to use mask dimensionality reduction to perform downsampling.

[0050] Specifically, the sampling interval can be determined based on the number of all tokens and the preset number. For example, assuming that the maximum input length of the model is 512, that is, the preset number is 512, then for texts within the length of 512*2, double mask downsampling is used, that is, the sampling interval is 2, and for texts within the length of 512*3, triple mask downsampling is used, that is, the sampling interval is 3. In this way, the length of the sampled text can always be kept within the length range of 512. It should be understood that the longer the text, the sparser the sampling, and the shorter the text, the denser the sampling.

[0051] After the sampling interval is determined, at least two tokens may be sampled at intervals based on the sampling interval to obtain at least one feature information.

[0052] Figure 2 One of the structural schematic diagrams of the downsampling method provided in an embodiment of the present invention, Figure 3 The second structural diagram of the downsampling method provided by the embodiment of the present invention is as follows: Figure 2 and Figure 3 As shown in , assuming that the maximum input length of the BERT model is 512 and the length of the text to be classified is within 512*2, the sampling interval is 2, such as Figure 2 or Figure 3 As shown, for at least two tokens corresponding to the text to be classified, one token can be sampled every other token, and the length of the feature information obtained after sampling is at least 1 / 2 of the length of the two tokens. Since the maximum input length of the BERT model is 512, the actual field of view of the BERT model will become 1024 through the above mask downsampling method. Among them, the mask described in the embodiment of the present invention can be understood as discarding the masked token. For example, in Figure 2 In the example above, you can discard the second token, the fourth token, the sixth token, etc. Figure 3 In the example above, you can discard the first token, the third token, the fifth token, and so on.

[0053] For mask downsampling with other sampling intervals, Figure 2 or Figure 3 The mask downsampling method with a sampling interval of 2 shown in is similar and will not be repeated here.

[0054] In this embodiment, after determining the sampling interval based on the number of tokens and the preset number, at least two tokens can be sampled at intervals based on the sampling interval to obtain at least one feature information. Through the above-mentioned interval sampling downsampling method, not only the field of view of the classification model can be improved, and the accuracy of the classification results can be improved, but the downsampling method is also relatively simple, thereby improving the efficiency of downsampling.

[0055] In another possible implementation, when the number of tokens is greater than a preset number, at least two tokens are downsampled to obtain at least one feature information. The target number can be determined based on the number of tokens and the preset number, and the average values ​​of the embedding vectors of the target number of tokens are determined in turn to obtain at least one feature information.

[0056] Specifically, the target number can be determined based on the number of all tokens and the preset number. For example, assuming that the maximum input length of the model is 512, that is, the preset number is 512, then for texts within the length of 512*2, the target number is determined to be 2, and for texts within the length of 512*3, the target number is determined to be 3. After determining the target number, the average value of the embedding vectors of the target number of tokens can be determined in turn to obtain at least one feature information. In this way, the length of the sampled text can always be kept within the length range of 512.

[0057] Figure 4 The third structural diagram of the downsampling method provided by the embodiment of the present invention is as follows: Figure 4 As shown in the figure, assuming that the maximum input length of the BERT model is 512 and the length of the text to be classified is within 512*2, the target number is 2. For at least two tokens corresponding to the text to be classified, the embedding vectors of the two adjacent tokens can be averaged to obtain at least one feature information. The length of the feature information obtained after sampling in the above manner is at least 1 / 2 of the length of the two tokens. Since the maximum input length of the BERT model is 512, the actual field of view of the BERT model will become 1024 through the above mask downsampling method.

[0058] Figure 5 A fourth structural diagram of the downsampling method provided in an embodiment of the present invention is as follows: Figure 5As shown in the figure, assuming that the maximum input length of the BERT model is 512 and the length of the text to be classified is within 512*3, the target number is 3. For at least two tokens corresponding to the text to be classified, the embedding vectors of the three adjacent tokens can be averaged to obtain at least one feature information. The length of the feature information obtained after sampling in the above manner is 1 / 3 of the length of at least two tokens. Since the maximum input length of the BERT model is 512, the actual field of view of the BERT model will become 1536 through the above mask downsampling method.

[0059] In this embodiment, after determining the target number based on the number of tokens and the preset number, the average values ​​of the embedding vectors of the target number of tokens can be determined in turn to obtain at least one feature information. By using the above-mentioned downsampling method to determine the average value of the embedding vectors of the target number of tokens, on the one hand, the field of view of the classification model can be improved and the accuracy of the classification results can be improved. On the other hand, the information of the downsampled tokens can be retained without increasing the computational complexity of the model itself.

[0060] It should be understood that both of the above two downsampling methods are to enhance the field of view by downsampling at the data level. The following will describe in detail the method of downsampling at the model level without downsampling at the data level.

[0061] In another possible implementation, the classification model may include multiple text classification layers and a downsampling layer arranged between two adjacent text classification layers. When determining the classification result of the text to be classified based on at least two tokens, at least one prefix information and the classification model, at least two tokens and at least one prefix information may be input into the first text classification layer, and the first target feature information corresponding to the at least two tokens output by the first text classification layer may be downsampled through the downsampling layer connected to the first text classification layer, the downsampled first target feature information may be input into the second text classification layer, and the second target feature information output by the second text classification layer may be downsampled through the downsampling layer connected to the second text classification layer, until the classification result is output.

[0062] Specifically, the structure of the classification model may be improved. The classification model includes a plurality of text classification layers, and a downsampling layer may be provided between two adjacent text classification layers. Figure 6 A schematic diagram of downsampling through a model provided in an embodiment of the present invention, such as Figure 6As shown, taking the BERT model as an example, the BERT model includes multiple text classification layers BERT layer, and a subsampling layer conv1D Subsampling is set between two adjacent text classification layers. After obtaining at least two tokens corresponding to the text to be classified, the at least two tokens and at least one prefix information can be input into the first text classification layer in the classification model, that is, the first text classification layer receives a longer input that has not been subsampled. The first text classification layer processes the at least two tokens and outputs the first target feature information. After the first target feature information passes through the subsampling layer connected to the first text classification layer, it will be subsampled through conv1D. After that, the subsampled first target feature information is input into the second text classification layer for processing and outputs the second target feature information. After the second target feature information passes through the subsampling layer connected to the second text classification layer, it will continue to be subsampled through conv1D until the classification result is output from the last text classification layer.

[0063] like Figure 6 As shown in the figure, in the above downsampling process, when at least two tokens of full length pass through each BERT layer, their length does not change. However, after adding a conv1D layer for downsampling between two BERT layers and performing double downsampling, their length will be reduced to half of the original length. The maximum input length of the second BERT layer can be set to half of that of the first BERT layer. After a Conv1D, another downsampling will be performed. The maximum input length of the third BERT layer is set to one quarter of that of the first BERT layer. After two downsamplings, the maximum input length of the n-2 layer BERT is one quarter of the initial input. In this way, the model can receive independent semantic representations for each token and keep the computational complexity of the n-2 layer unchanged.

[0064] In this embodiment, a downsampling layer can be set between two adjacent text classification layers, and the feature information output by the previous text classification layer is downsampled through the downsampling layer. On the one hand, this method can maintain the semantics of each input token, and on the other hand, it can expand the field of view of the classification model while reducing the amount of calculation.

[0065] Exemplarily, based on the above embodiments, the classification model described in the above embodiments is trained based on the following method: At least two sample tokens corresponding to the sample text and a category token corresponding to the sample prefix information are obtained, and the at least two sample tokens and the category token are input into the initial classification model. The correlation between the category tokens and the correlation between the sample tokens and the category tokens are determined through a self-attention mechanism, so that the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample tokens and the category tokens to obtain the classification model and prefix information.

[0066] Specifically, each category of the text itself has semantics. The usual processing method is to use BERT or Ernie's [CLS] token to map it to various dimensions and use the category index as the category representation. However, in the above method, the semantics of the category is not utilized, the relationship between categories is not modeled, and the relationship between the category and all tokens in the input sequence is not modeled.

[0067] In view of the above problems, in an embodiment of the present invention, category semantics can be modeled through sample prefix information, and the correlation between categories and the correlation between categories and each sample token of the input sample text can be modeled.

[0068] Figure 7 A schematic diagram of the training process of the classification model provided in the embodiment of the present invention is shown in FIG. Figure 7 As shown, the obtained sample text is decomposed to obtain a sample token sequence, which includes at least two sample tokens. In addition, a class token corresponding to a sample prefix information (class prefix) can also be initialized. The at least two sample tokens and the class token obtained are input into the initial classification model. After passing through the BERTLayer layer in the initial classification model, the correlation between the class tokens can be determined through the self-attention mechanism, as well as the correlation between each class token and each sample token. In this embodiment, the [CLS] token is removed, that is, only at least two sample tokens and the class token are input, and the [CLS] token is not input. The class token establishes the relationship between the categories in the forward calculation process and also models the relationship between the input sample token. The class token of each category is used to determine whether the category is included in the input sequence.

[0069] After establishing the correlation between category tokens and the correlation between sample tokens and category tokens, the initial classification model can be trained based on these correlations. For a certain category token, the category token can be used to perform a binary classification prediction of whether it is the category corresponding to the category token, and the network parameters of the initial classification model can be adjusted based on the prediction results and the category labels corresponding to the sample text, so as to obtain the trained classification model and prefix information.

[0070] In this embodiment, when training the classification model and prefix information, the correlation between category tokens and the correlation between sample tokens and category tokens can be determined through the self-attention mechanism, so that the prefix information trained through the correlation can model category semantics. Moreover, since the classification model takes into account the correlation between category tokens and the correlation between sample tokens and category tokens, it can accurately identify different categories when the same text involves multiple categories. When classifying such texts based on prefix information and the classification model, the accuracy of the classification results can be greatly improved.

[0071] Exemplarily, based on the above embodiments, when the classification result represents that the text to be classified does not include illegal content, the text to be classified is input into the large language model to obtain reply information corresponding to the text to be classified, and the reply information is determined as a new text to be classified, and a new classification result corresponding to the new text to be classified is determined, and when the new classification result represents that the reply information does not include illegal content, the reply information is output through the large language model.

[0072] Specifically, when the large language model receives content input by the user, it needs to review the input content. If it is normal and risk-free content, it will be normally input into the large language model, and risky content will be blocked. Therefore, the classification method provided in the embodiment of the present invention can be used to review the input content and output content of the large language model.

[0073] Exemplarily, if the text to be classified is the input content to be input into the large language model, the classification result of the text to be classified is obtained by the method described in the aforementioned embodiments. When the classification result indicates that the text to be classified does not contain illegal content, the text to be classified will be normally input into the large language model, thereby determining the reply information corresponding to the text to be classified. If the classification result indicates that the text to be classified contains illegal content, the text to be classified will be intercepted, that is, the text to be classified will not be input into the large language model.

[0074] After the reply information is determined, the classification result of the reply information can be determined in the manner described in the aforementioned embodiments, such as determining the classification result corresponding to the reply information based on the token corresponding to the reply information, at least one prefix information and the classification model. If the classification result corresponding to the reply information indicates that the reply information does not include illegal content, the reply information will be output normally through the large language model. If the classification result corresponding to the reply information indicates that the reply information does not include illegal content, the reply information will be output normally through the large language model. If the classification result corresponding to the reply information indicates that the reply information includes illegal content, the reply information will be intercepted.

[0075] In this embodiment, when the classification result indicates that the text to be classified does not include illegal content, the text to be classified is input into the large language model, which can ensure that the text input into the large language model is risk-free and harmless. After the large language model determines the reply information, the reply information can also be classified. When the classification result corresponding to the reply information indicates that the reply information does not include illegal content, the reply information is output through the large language model, thereby ensuring that the text output from the large language model is risk-free and harmless, thereby improving the security of the large language model.

[0076] The text classification device provided by the embodiment of the present invention is described below. The text classification device described below and the text classification method described above can be referred to each other.

[0077] Figure 8 A schematic diagram of the structure of a text classification device provided by an embodiment of the present invention, referring to Figure 8 As shown, the text classification device 800 includes: An acquisition module 11 is used to acquire the text to be classified; A determination module 12, used to determine at least two tokens corresponding to the text to be classified; The determination module 12 is also used to determine the classification result of the text to be classified based on the at least two tokens, at least one prefix information and the classification model; the classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information into an initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token, and each prefix information is used to model the semantics of each category.

[0078] In an exemplary embodiment, the determining module 12 is specifically configured to: When the number of the tokens is greater than a preset number, downsampling the at least two tokens to obtain at least one feature information; The at least one feature information and the prefix information are input into the classification model to obtain a classification result output by the classification model.

[0079] In an exemplary embodiment, the apparatus further comprises: a sampling module, wherein: The determination module 12 is further configured to determine a sampling interval based on the number of the tokens and the preset number; A sampling module is used to perform interval sampling on the at least two tokens based on the sampling interval to obtain the at least one feature information.

[0080] In an exemplary embodiment, the determination module 12 is further configured to: Determining a target quantity based on the quantity of the tokens and the preset quantity; The average values ​​of the embedding vectors of the target number of the tokens are determined in sequence to obtain the at least one feature information.

[0081] In an exemplary embodiment, the classification model includes a plurality of text classification layers and a downsampling layer arranged between two adjacent text classification layers; The determination module 12 is specifically used for: The at least two tokens and the at least one prefix information are input into a first text classification layer, first target feature information corresponding to the at least two tokens output by the first text classification layer is downsampled through a downsampling layer connected to the first text classification layer, the downsampled first target feature information is input into a second text classification layer, and second target feature information output by the second text classification layer is downsampled through a downsampling layer connected to the second text classification layer, until the classification result is output.

[0082] In an exemplary embodiment, the classification model is trained based on the following method: Obtain at least two sample tokens corresponding to the sample text and a category token corresponding to the sample prefix information; The at least two sample tokens and the category token are input into the initial classification model, and the correlation between the category tokens and the correlation between the sample token and the category token are determined through a self-attention mechanism; the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token to obtain the classification model and the prefix information.

[0083] In an exemplary embodiment, the apparatus further comprises: an input module and an output module, wherein: An input module, used for inputting the text to be classified into a large language model to obtain reply information corresponding to the text to be classified when the classification result indicates that the text to be classified does not include illegal content; The determination module 12 is further used to determine the reply information as a new text to be classified, and determine a new classification result corresponding to the new text to be classified; An output module is used to output the reply information through the large language model when the new classification result indicates that the reply information does not include illegal content.

[0084] The device of this embodiment can be used to execute the method of any embodiment in the text classification method side embodiment. Its specific implementation process and technical effects are similar to those in the text classification method side embodiment. For details, please refer to the detailed introduction in the text classification method side embodiment, which will not be repeated here.

[0085] Fig. 9 An example of a physical structure diagram of an electronic device is shown in FIG. Fig. 9 As shown, the electronic device may include: a processor 910, a communication interface 920, a memory 930 and a communication bus 940, wherein the processor 910, the communication interface 920 and the memory 930 communicate with each other through the communication bus 940. The processor 910 may call the logic instructions in the memory 930 to execute the text classification method, which includes: obtaining the text to be classified; determining at least two tokens corresponding to the text to be classified; determining the classification result of the text to be classified based on the at least two tokens, at least one prefix information and a classification model; the classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information into the initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token, and each prefix information is used to model the semantics of each category.

[0086] In addition, the logic instructions in the above-mentioned memory 930 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0087] On the other hand, the present invention also provides a computer program product, which includes a computer program, which can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the text classification method provided by the above-mentioned methods, which includes: obtaining a text to be classified; determining at least two tokens corresponding to the text to be classified; determining a classification result of the text to be classified based on the at least two tokens, at least one prefix information and a classification model; the classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and a category token corresponding to the sample prefix information into an initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token, and each prefix information is used to model the semantics of each category.

[0088] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the text classification method provided by the above-mentioned methods, the method comprising: obtaining a text to be classified; determining at least two tokens corresponding to the text to be classified; determining a classification result of the text to be classified based on the at least two tokens, at least one prefix information and a classification model; the classification model and each prefix information are obtained by inputting at least one sample token corresponding to the sample text and a category token corresponding to the sample prefix information into an initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token, and each prefix information is used to model the semantics of each category.

[0089] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0090] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0091] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A text classification method, characterized in that: include: Get the text to be classified; Determine at least two tokens corresponding to the text to be classified; Determine a classification result of the text to be classified based on the at least two tokens, the at least one prefix information and the classification model; The classification model and the prefix information are obtained by inputting at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information into the initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample tokens and the category tokens. The prefix information is used to model the semantics of each category.

2. The text classification method according to claim 1, characterized in that: The step of determining the classification result of the text to be classified based on the at least two tokens, the at least one prefix information and the classification model includes: When the number of the tokens is greater than a preset number, downsampling the at least two tokens to obtain at least one feature information; The at least one feature information and the prefix information are input into the classification model to obtain a classification result output by the classification model.

3. The text classification method according to claim 2, characterized in that: When the number of the tokens is greater than a preset number, downsampling the at least two tokens to obtain at least one feature information includes: Determining a sampling interval based on the number of the tokens and the preset number; The at least two tokens are sampled at intervals based on the sampling interval to obtain the at least one feature information.

4. The text classification method according to claim 2, characterized in that: When the number of the tokens is greater than a preset number, downsampling the at least two tokens to obtain at least one feature information includes: Determining a target quantity based on the quantity of the tokens and the preset quantity; The average values ​​of the embedding vectors of the target number of the tokens are determined in sequence to obtain the at least one feature information.

5. The text classification method according to claim 2, characterized in that: The classification model includes a plurality of text classification layers and a downsampling layer arranged between two adjacent text classification layers; The step of determining the classification result of the text to be classified based on the at least two tokens, the at least one prefix information and the classification model includes: The at least two tokens and the at least one prefix information are input into a first text classification layer, first target feature information corresponding to the at least two tokens output by the first text classification layer is downsampled through a downsampling layer connected to the first text classification layer, the downsampled first target feature information is input into a second text classification layer, and second target feature information output by the second text classification layer is downsampled through a downsampling layer connected to the second text classification layer, until the classification result is output.

6. The text classification method according to any one of claims 1 to 5, characterized in that: The classification model is trained based on the following method: Obtain at least two sample tokens corresponding to the sample text and a category token corresponding to the sample prefix information; Inputting the at least two sample tokens and the category token into the initial classification model, and determining the correlation between the category tokens and the correlation between the sample tokens and the category token through a self-attention mechanism; The initial classification model is trained based on the correlation between the category tokens and the correlation between the sample token and the category token to obtain the classification model and the prefix information.

7. The text classification method according to any one of claims 1 to 5, characterized in that: The method further comprises: In the case where the classification result indicates that the text to be classified does not include illegal content, the text to be classified is input into a large language model to obtain reply information corresponding to the text to be classified; The reply information is determined as a new text to be classified, and a new classification result corresponding to the new text to be classified is determined; when the new classification result indicates that the reply information does not include illegal content, the reply information is output through the large language model.

8. A text classification device, characterized in that: include: An acquisition module is used to obtain the text to be classified; A determination module, used to determine at least two tokens corresponding to the text to be classified; The determination module is used to determine the classification result of the text to be classified based on the at least two tokens, at least one prefix information and the classification model; The classification model and the prefix information are obtained by inputting at least one sample token corresponding to the sample text and the category token corresponding to the sample prefix information into the initial classification model, and the initial classification model is trained based on the correlation between the category tokens and the correlation between the sample tokens and the category tokens. The prefix information is used to model the semantics of each category.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the text classification method according to any one of claims 1 to 7 is implemented.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the text classification method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Relationship extraction method and device fusing relation phrase knowledge, and electronic equipment

    CN113705237A

  • Natural language processing method and device based on knowledge guidance prefix fine tuning, computing equipment, and storage medium

    CN113987209A

  • Text classification method based on label semantic learning and attention adjustment mechanism

    CN115221325A