A text classification method based on K-nearest neighbor algorithm and contrastive learning

By combining the K neighbor node algorithm and contrastive learning, using the joint training of the contrastive learning loss function and the cross-entropy loss function to expand positive and negative samples, the problem of insufficient robustness of existing text classification methods under adversarial attacks is solved, and the Higher classification accuracy and robustness.

CN113673242BActive Publication Date: 2024-10-15ZHEJIANG LAB +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110960433.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-20
Publication Date
2024-10-15
Estimated Expiration
2041-08-20

AI Technical Summary

Technical Problem

Existing text classification methods are insufficiently robust in the face of adversarial attacks and have difficulty maintaining high classification accuracy.

Method used

Using a method based on K-nearby node algorithm and contrastive learning, by constructing sentence vector representation, joint training using contrastive learning loss function and cross-entropy loss function, combined with momentum parameter update, expands the size of positive and negative samples, and improves the robustness of the model and classification accuracy.

Benefits of technology

The robustness and classification accuracy of the model are significantly improved, especially in the face of adversarial attacks, the performance is more stable, and the overall performance of text classification is improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113673242B_ABST
    Figure CN113673242B_ABST
Patent Text Reader

Abstract

The present invention discloses a text classification method based on a K-nearest neighbor algorithm and contrastive learning. In the training phase, the method uses contrastive learning to shorten the intra-class distance and increase the inter-class distance, and combines cross entropy loss to assist contrastive learning in joint training. In the inference process, a joint prediction is performed by combining a jointly trained model with a nearest neighbor algorithm to calculate the classification of a text to be inferred. The present invention can not only achieve a higher result in terms of text classification accuracy than the text classification method currently used in the industry, but also greatly improve the robustness of the model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to deep learning and natural language processing, and in particular to a text classification method based on a K-nearest neighbor algorithm and contrastive learning. Background Art

[0002] Text classification is a basic task in natural language processing. The current mainstream text classification method is to use a linear classifier for classification based on a large-scale pre-trained model (such as BERT). However, linear classifiers often do not have good robustness and can be easily fooled by adversarial attacks such as TextFooler or BertAttack. Summary of the invention

[0003] In order to solve the shortcomings of the prior art and improve the robustness while improving the classification accuracy of the model, the present invention adopts the following technical solutions:

[0004] A text classification method based on K-nearest neighbor algorithm and contrastive learning includes the following steps:

[0005] S1, during the training process, by constructing sentence vectors to represent k positive and negative samples, contrastive learning is performed to narrow the intra-class distance and widen the inter-class distance. The loss function of contrastive learning is as follows:

[0006]

[0007] Among them, M represents the number of positive samples, N represents the number of negative samples, q represents the vector representation of the sentence output by the pre-trained encoder encoder_q, and k represents the vector representation of the sentence output by the pre-trained encoder encoder_k. Encoder_q is the same as encoder_k, and k j represents the jth positive sample k + , k i Indicates traversing negative samples k- and k j A set of , exp(·) represents the exponential function, and τ is a hyperparameter;

[0008] Combined with the cross entropy loss function, joint training is performed, and the joint loss function is as follows:

[0009] L=λL ec +(1-λ)L sc

[0010]

[0011] Among them, λ represents the adjustment cross entropy loss function L ec And the loss function L of the contrastive learning sc The weight parameter between crepresents the category of q, C represents the number of categories of text classification, and F(·) represents the linear classifier;

[0012] Back propagate the loss function and update the parameters of encoder_q and linear classifier;

[0013] The joint loss function is the weighted sum of the cross entropy loss function and the supervised contrastive learning loss function. sc To assist the cross entropy loss function in training the model, contrastive learning is used to train the model so that the model can automatically cluster the embedding representations of the samples during training, thereby achieving better classification results.

[0014] S2, classify the text through the trained encoder_q and linear classifier.

[0015] Furthermore, in S2, the sentence vector representation q of the text to be predicted is obtained through the trained encoder_q, and the text classification is predicted using a joint prediction function, and the joint prediction function is as follows:

[0016]

[0017] Among them, S represents the probability value of the final classification, represents a hyperparameter, Softmax(·) represents an activation function, F(q) represents a trained linear classifier, and KNN(q) represents selecting K training samples closest to q in the sample space from the queue Q. According to the classification labels of the training samples, the probability value of the KNN model is given by voting, and the classification result is obtained by the probability value. When inferring the sample category, KNN and the linear classifier are used to jointly predict the classification of the sample to be predicted. The robustness of the model is significantly improved through the K neighboring node algorithm.

[0018] Furthermore, the K training samples closest to q are selected. Since the classification labels of the K training samples are known, K = s1 + s2 + ... + s c ,s i Indicates the number of samples whose classification labels belong to the i-th category, c indicates the number of training sample categories, and through the KNN model, the given q belongs to category y i The probability value is

[0019] Furthermore, the similarity between q and the training sample is calculated by a cosine function.

[0020] Furthermore, in S1, the momentum parameter of encoder_k is updated by the hyperparameter m:

[0021] θ k ←mθk +(1-m)θ q

[0022] where θ k represents the momentum parameter of encoder_k, θ q Represents the momentum parameter of encoder_q. In each batch iteration, the k obtained by encoding encoder_k is stored in queue Q. In order to make the samples in the queue represent, encoder_k is updated in each iteration by updating the momentum parameter so that the k obtained by it is close to the q obtained directly through encoder_q.

[0023] Furthermore, the queue Q replaces the element k therein in order of precedence.

[0024] Further, M elements k with the same classification label as the sample are obtained from the queue Q as positive samples k + , N elements k that are different from the classification label of the sample are regarded as negative samples k - .

[0025] The advantages and beneficial effects of the present invention are:

[0026] The present invention has not only made great improvements in the robustness of the model, but also improved the accuracy of the model accordingly. In addition, in order to use the K-nearest neighbor algorithm to predict the category of the sample, we added contrastive learning during the training process in order to narrow the distance between similar samples. At the same time, in the process of using contrastive learning, we introduced the MOCO training method, which greatly increased the scale of positive and negative samples. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 It is a flow chart of the method of the present invention.

[0028] Figure 2 It is a line graph showing the influence of the λ value of the present invention on the model accuracy on different data sets.

[0029] Figure 3 The present invention Line chart showing the impact of different values ​​on model accuracy on different data sets.

[0030] Figure 4a It is the sample space distribution diagram of the ordinary linear classifier.

[0031] Figure 4b This is the KNN-BERT sample space distribution diagram of the present invention.

[0032] Figure 5 It is a comparison chart of the classification accuracy test results of the model of the present invention.

[0033] Figure 6 It is a comparison diagram of the model robustness test results of the present invention. DETAILED DESCRIPTION

[0034] The specific implementation of the present invention is described in detail below in conjunction with the accompanying drawings. It should be understood that the specific implementation described here is only used to illustrate and explain the present invention, and is not used to limit the present invention.

[0035] A text classification method based on K-nearest neighbor algorithm and contrastive learning, such as Figure 1 As shown, the following steps are included:

[0036] Part 1: Model training process, specifically, is divided into the following steps:

[0037] Step 1.1: Use the pre-trained BERT model as the sample encoder encoder_q and the same pre-trained BERT model as the sample encoder encoder_k.

[0038] Step 1.2: Use the hyperparameter m=0.999 to update the parameters of encoder_k. Specifically, the formula for updating the momentum parameter is:

[0039] θ k ←mθ k +(1-m)θ q

[0040] where θ k represents the momentum parameter of the sample encoder encoder_k, θ q Represents the momentum parameter of the sample encoder encoder_q. Traditional contrastive learning uses the method of selecting positive and negative samples within the batch, so the number of positive and negative samples used in the training process is too small. MoCo uses the momentum update method. In each batch iteration, the samples encoded by the encoder are stored in a queue. In order to make the sample representation in the queue close to the sample representation directly obtained by the encoder in each iteration, the momentum parameter update method is used to update encoder_k;

[0041] The updates of the two encoders encoder_q and encoder_k are both iteratively updated, and each batch is updated based on the training data.

[0042] Step 1.3: Use encoder encoder_q to obtain the vector representation of the sentence [CLS]_q (i.e., the sentence representation q of the sample), and use encoder encoder_k to obtain the vector representation of the sentence [CLS]_k (i.e., the sentence representation k of the sample).

[0043] For example, for the training sentence "Beijing is the capital of China", BERT will add a token CLS to the beginning of the sentence and a token SEP to the end of the sentence during encoding. In general, the embedding vector of CLS is used as the representation of the entire sentence.

[0044] Step 1.4: Store [CLS]_k in a queue Q of size 32000 and replace the elements in Q in order;

[0045] Step 1.5: Get M samples with the same label as the sample from Q as positive samples k + , N samples with different sample labels are used as negative samples k - ;

[0046] Step 1.6: Use positive and negative samples to calculate the loss function of contrastive learning to narrow the distance between similar samples. Specifically, the contrastive learning loss function is as follows:

[0047]

[0048] Among them, q is the sentence representation output by encoder_q, k j represents the jth positive sample k + , k i Indicates traversal k - and k j , exp(·) represents the exponential function, τ is a hyperparameter, specifically, τ = 0.07.

[0049] Step 1.7: Use the cross entropy loss function to assist model training, taking λ = 0.01. Specifically, the loss function of model training is as follows:

[0050] L=λL ec +(1-λ)L sc

[0051]

[0052] Among them, λ represents the adjustment L ec and L sc The weight parameters between Figure 2 The values ​​of λ shown are the experimental results on the RTE and MRPC datasets. c represents the category of q or input sample x (q is the sentence representation of x after passing through encoder_q), C is the number of categories for text classification, and F(·) is a linear classifier.

[0053] The loss function L through contrastive learning scTo assist the cross entropy loss function in training the model. Its beneficial effect is that the model is trained using contrastive learning, so that the model can automatically cluster the embedding representation of the samples during the training process, thereby achieving better classification results.

[0054] Step 1.8: Back-propagate the model loss function and update the parameters of encoder_q and linear classifier.

[0055] Part 2: Use KNN and linear classifier to jointly predict the classification of the sample to be predicted. Specifically, it is divided into the following steps:

[0056] Step 2.1: Use encoder encoder_q to obtain the sentence representation q of the sample to be predicted;

[0057] Step 2.2: Use the joint prediction function to predict the sample classification, take Specifically, the joint prediction function is as follows:

[0058]

[0059] Among them, S is the probability value of the final model classification, is a hyperparameter. KNN(q) takes the K samples closest to q in the sample space from Q, and then gives the probability value of the KNN model by voting based on the labels of these samples.

[0060] Specifically, the cosine function is used to calculate the similarity of two samples, and the K training samples with the largest similarity are selected, because the classification information labels of these K training samples are known. Assuming that there are a total of c classifications of training samples, s1+s2+…+s c =K, where s i Indicates the number of sample labels belonging to the i-th category, so the sample to be predicted x given by the KNN model belongs to category y i The probability value is like Figure 3 shown The value is the impact on the model accuracy on the RTE, MRPC and MNLI datasets.

[0061] like Figure 4a , 4b As shown, the red dots and blue dots represent data points of two different categories. From the figure, we can see that the clustering effect of KNN-BERT's sample distribution is better than that of ordinary linear classifiers.

[0062] like Figure 5As shown in the figure, RTE, MRPC, QNLI, MNLI, SST-2, IMDB, and AG'News are all commonly used text classification data sets, BERT is a relatively common classification model, SCL-Train is a traditional contrastive learning + BERT classification model, MoCo is a classification model that uses the momentum parameter update method to expand positive and negative samples, and KNN-BERT is the classification model proposed by the present invention. It can be seen from the figure that the classification accuracy of the method proposed by the present invention is improved compared with the existing methods on each data set, and the improvement effect is better on the two data sets of RTE and MRPC with less data.

[0063] like Figure 6 As shown in the figure, IMDB and AG's News are two commonly used text classification datasets. Origin represents the original accuracy, Textfooler and BERT-Attack represent the classification accuracy under the two adversarial attack methods, BERT represents the traditional classification method, and KNN represents the method of the present invention. The experimental results show that when When , that is, when the model results are only given by the KNN classifier, the model has the best robustness.

[0064] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some or all of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A text classification method based on K-nearest neighbor algorithm and contrastive learning, characterized in that The steps include: S1, during the training process, by constructing sentence vectors to represent k positive and negative samples, contrastive learning is performed. The loss function of contrastive learning is as follows: Among them, M represents the number of positive samples, N represents the number of negative samples, q represents the vector representation of the sentence output by the pre-trained encoder encoder_q, and k represents the vector representation of the sentence output by the pre-trained encoder encoder_k. Encoder_q is the same as encoder_k, and k j represents the jth positive sample k + , k i Indicates traversal of negative samples k - and k j A set of , exp(·) represents the exponential function, and τ is a hyperparameter; Combined with the cross entropy loss function, joint training is performed, and the joint loss function is as follows: L=λL ec +(1-λ)L sc Among them, λ represents the adjustment cross entropy loss function L ec And the loss function L of the contrastive learning sc The weight parameter between c represents the category of q, C represents the number of categories of text classification, and F(·) represents the linear classifier; Back propagate the loss function and update the parameters of encoder_q and linear classifier; S2, classify the text through the trained encoder_q and linear classifier.

2. A text classification method based on K-nearest neighbor algorithm and contrastive learning according to claim 1, characterized in that In S2, the sentence vector representation q of the text to be predicted is obtained through the trained encoder_q, and the text classification is predicted using the joint prediction function. The joint prediction function is as follows: Among them, S represents the probability value of the final classification, represents a hyperparameter, Softmax(·) represents an activation function, F(q) represents a trained linear classifier, and KNN(q) represents selecting the K training samples closest to q from the queue Q. According to the classification labels of the training samples, the probability value of the KNN model is given by voting, and the classification result is obtained by the probability value.

3. A text classification method based on K-nearest neighbor algorithm and contrastive learning according to claim 2, characterized in that The K training samples closest to q are selected. Since the classification labels of the K training samples are known, K = s1 + s2 + ... + s c ,s i Indicates the number of samples whose classification labels belong to the i-th category, c indicates the number of training sample categories, and through the KNN model, the given q belongs to category y i The probability value is 4. A text classification method based on K-nearest neighbor algorithm and contrastive learning according to claim 2, characterized in that The similarity between q and the training sample is calculated using the cosine function.

5. A text classification method based on K-nearest neighbor algorithm and contrastive learning according to claim 1, characterized in that In S1, the momentum parameter of encoder_k is updated by the hyperparameter m: i k ←mθ k +(1-m)θ q where θ k represents the momentum parameter of encoder_k, θ q It represents the momentum parameter of encoder_q. The k obtained by encoder_k encoding is stored in queue Q. In each iteration, encoder_k is updated by updating the momentum parameter so that the k obtained by it is close to the q obtained directly through encoder_q.

6. A text classification method based on K-nearest neighbor algorithm and contrastive learning according to claim 5, characterized in that The queue Q replaces the element k therein in order.

7. A text classification method based on K-nearest neighbor algorithm and contrastive learning according to claim 5, characterized in that Get M elements k with the same classification label as the sample from the queue Q as positive samples k + , N elements k that are different from the classification label of the sample are regarded as negative samples k - .