A method for improving robustness of few-shot learning based on contrastive learning
By constructing adversarial and contrasting datasets and using contrast learning loss functions, the problem of insufficient robustness of fine-tuning and prompt adjustment methods in small sample learning scenarios is solved, which significantly improves the robustness and prediction accuracy of model adversarial and contrast perturbations. .
Patent Information
- Application Number
- CN202210565539.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-23
- Publication Date
- 2024-12-24
- Estimated Expiration
- 2042-05-23
AI Technical Summary
The existing fine-tuning and prompt adjustment methods are not robust enough when fighting and contrasting perturbations in small sample learning scenarios, especially in low-resource scenarios, and it is difficult to effectively resist attacks of synonyms or antonyms replacement.
By constructing adversarial and contrast datasets, the distance between embedded representations of pre-trained language models is calculated using the contrast learning loss function, and the model parameters are updated in combination with the original model loss function to improve the robustness of model adversarial and contrast perturbations. Specific steps include constructing a synonym and antonyms dictionary, generating adversarial and contrast samples, and calculating the updated gradients of the model using the contrast learning loss function.
It significantly improves the robustness of the model under adversarial and contrast perturbation, can better distinguish sentences with the same semantics and different semantics, and improves the prediction accuracy in small sample learning scenarios.
Smart Images

Figure CN114781651B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of natural language processing, and in particular to a method for improving the robustness of small sample learning based on contrastive learning. Background Art
[0002] Few-sample learning is a basic task for artificial intelligence to reach the level of human intelligence. An important characteristic of human intelligence is that humans have the ability to learn from a small number of samples and have strong generalization ability, that is, to draw inferences from one example and to integrate knowledge.
[0003] Few-shot learning studies how to learn from a small number of samples. Because of this, few-shot learning has become a very important frontier research direction in the field of deep learning in recent years. In the field of natural language processing, few-shot learning is mainly based on existing pre-trained models such as BERT (Bidirectional Encoder Representation from Transformers), RoBERTa (Robustly Optimized BERT), GPT-3 (Generative Pre-training), etc. to handle downstream tasks. The current mainstream method is to fine-tune or prompt tuning the above pre-trained models.
[0004] The idea of fine-tuning is to adapt the pre-trained language model to downstream tasks without training a new model from scratch. Therefore, it can not only save computing resources, but also effectively utilize the existing knowledge in the pre-trained model. In the past many years, the fine-tuning method has achieved excellent results in multiple tasks, so fine-tuning the pre-trained language model for specific tasks has become a consensus in the natural language community. However, this method requires sufficient labeled training data and replaces the output layer of the pre-trained language model with a task-specific head. In addition, many studies have shown that the problem with the fine-tuning method is that there is a significant formal difference in the objective function between pre-training and fine-tuning, which hinders the knowledge transfer from the pre-trained language model to downstream tasks.
[0005] In order to make up for the above shortcomings of the fine-tuning method, prompt tuning proposes to adapt the downstream task to the pre-trained language model. A prompt usually consists of two parts: a template and a label word. In prompt tuning, by fusing the input sentence and the template, the downstream task can be rewritten into a corresponding cloze problem. At this time, the pre-trained language model is used to predict the blank content in the cloze, and then the words predicted by the model are further mapped into labels. Compared with the fine-tuning method, the prompt tuning method does not require a large amount of labeled training data and additional neural network layers designed for specific tasks, and has achieved better results in a variety of small sample downstream tasks.
[0006] Although fine-tuning and prompt tuning methods perform well in small sample learning scenarios, they are not robust enough when faced with adversarial or contrastive attacks, such as synonym or antonym replacement. Recently, some research institutions have attempted to perform adversarial training on pre-trained language models to obtain more robust models. However, most of these methods are based on fine-tuning and have a large amount of training data, which is not suitable for low-resource (small sample) scenarios and prompt tuning methods. In addition, there are also studies designing new objective functions for small sample fine-tuning, such as using contrastive learning to make samples of the same class closer and vice versa, but it still cannot resist adversarial or contrastive perturbation attacks. Summary of the invention
[0007] In order to make up for the shortcomings of the above-mentioned background technology, the present invention proposes a method for improving the robustness of small sample learning based on contrastive learning to solve the problem that the fine-tuning method and the prompt adjustment method have poor robustness when encountering adversarial and contrast disturbances in small sample learning scenarios.
[0008] The technical problem of the present invention is solved by the following technical solutions:
[0009] The present invention discloses a method for improving the robustness of small sample learning based on contrastive learning, comprising the following steps: S1, preprocessing an original data set to construct an adversarial data set and a contrast data set; S2, inputting the original data set, the adversarial data set and the contrast data set into a pre-trained language model respectively to obtain corresponding embedding representations, and using a contrastive learning loss function to calculate the distance between the three embedding representations; S3, calculating the gradient of model update according to the contrastive learning loss function and the loss function of the original model, and training the model with the goal of reducing the overall loss.
[0010] In some embodiments, step S1 specifically includes: S1.1, constructing a corresponding synonym dictionary and antonym dictionary based on the original data set; S1.2, performing synonym replacement on the data in the original data set to construct an adversarial data set by searching the constructed synonym dictionary and antonym dictionary, and performing antonym replacement on the data in the original data set to construct a comparative data set.
[0011] In some embodiments, step S1 further includes: S1.3, calculating the set of all perturbations of the sentence of each original sample in the original data set, the formula is as follows:
[0012]
[0013] Where x is a sentence, Perturb is the perturbation of the sentence, is the sentence after perturbation x, that is, the sentence after replacing synonyms or antonyms; c i is the i-th character or word in It is c i synonyms or antonyms of yes The i-th word or phrase in Perturb(x) is the perturbed sentence of the original sentence. A collection of; This means that if a position i is disturbed, there will be a
[0014] In some embodiments, in step S2, the specific method for obtaining the corresponding embedded representation includes: forming a positive example pair with each original sample in the original data set and its corresponding positive example, forming a negative example pair with each original sample in the original data set and its corresponding negative example, and obtaining the corresponding embedded representation through a pre-trained language model, and calculating the semantic similarity between each original sample and the corresponding positive example and negative example.
[0015] Furthermore, two sentences with the same semantics belong to a positive pair, and two sentences with different semantics belong to a negative pair.
[0016] Furthermore, the formula for calculating the semantic similarity between two sentences is as follows:
[0017]
[0018] In the formula, x a and x b represents sentence a and sentence b, exp() is to find the exponential, h is the feature representation of sentence x, represents the embedding of sentence a at the [CLS] position after being processed by the model, Represents the embedding of sentence b at the [CLS] position after being processed by the model.
[0019] Furthermore, in step S2, the calculation formula for calculating the distance between the three embedding representations using the contrastive learning loss function is as follows:
[0020]
[0021] In the formula, x is a sentence, and the sentence with a semantic distance from x or even completely opposite to x is x. ant , the sentence that is closer to x in semantics is x syn , (x, x syn ) constitutes a positive pair, (x, x ant ) constitute a negative example pair.
[0022] Furthermore, in step S3, the calculation formula of the overall loss is as follows:
[0023] L=λ1L ori +λ2L cl
[0024] Where, L ori Indicates that L cl The objective function of the loss of other models except λ1 and λ2 represents the weights learned by the model.
[0025] In some embodiments, the method further includes: S4, updating model parameters through a back-propagation mechanism, iterating until the model converges, and using the optimal model to test on a test set of a downstream task to obtain a prediction result.
[0026] The present invention also discloses a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of any of the above methods are implemented.
[0027] The beneficial effects of the present invention compared with the prior art include: the small sample learning robustness improvement method based on contrastive learning provided by the present invention first preprocesses the original data set, constructs an adversarial data set and a contrast data set, and then respectively inputs the original data set, the adversarial data set and the contrast data set into the pre-trained language model to obtain corresponding embedding representations, and uses the contrastive learning loss function to calculate the distance between the three embedding representations, and then calculates the gradient of the model update according to the contrastive learning loss function and the loss function of the original model, and trains the model with the goal of smaller overall loss, wherein constructing adversarial and contrast data sets provides data support for model robustness learning, and using the contrastive learning objective function to calculate the loss can better obtain the similarity between the original sample and the adversarial sample, and can also better distinguish the difference between the original sample and the contrast sample, thereby improving the robustness of the model when subjected to adversarial or contrast disturbances. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] Figure 1 It is a flowchart of a method for improving the robustness of small sample learning based on contrastive learning according to an embodiment of the present invention.
[0029] Figure 2 It is a flowchart of a method for improving the robustness of small sample learning based on contrastive learning according to a specific embodiment of the present invention.
[0030] Figure 3 It is a schematic diagram of the model structure of a method for improving the robustness of small sample learning based on contrastive learning in a specific embodiment of the present invention. DETAILED DESCRIPTION
[0031] The present invention is further described below with reference to the accompanying drawings and in combination with preferred embodiments. It should be noted that the embodiments and features in the embodiments of the present application can be combined with each other without conflict.
[0032] It should be noted that the directional terms such as left, right, up, down, top, and bottom in this embodiment are merely relative concepts or are based on the normal use status of the product, and should not be considered as restrictive.
[0033] The present invention proposes a fine-tuning and prompt tuning method based on contrastive learning, namely, contrastive learning with fine-tuning and contrastive learning with prompt tuning, referred to as CLFT and CLPT respectively, which enable the model to shorten the distance between the original sample and the adversarial sample and extend the distance between the original sample and the contrast sample, thereby improving the robustness of the model under adversarial and contrast disturbances in small sample learning scenarios.
[0034] like Figure 1 As shown, the small sample learning robustness improvement method based on contrastive learning provided by the embodiment of the present invention includes the following steps:
[0035] S1. Preprocess the original dataset and construct adversarial dataset and comparison dataset.
[0036] Specifically, step S1 includes:
[0037] S1.1, construct corresponding synonym dictionary and antonym dictionary based on the original data set;
[0038] S1.2. By searching the constructed synonym dictionary and antonym dictionary, the data in the original dataset are replaced with synonyms to construct an adversarial dataset, and the data in the original dataset are replaced with antonyms to construct a comparative dataset.
[0039] S1.3. Calculate the set of all perturbations of the sentence of each original sample in the original dataset. The formula is as follows:
[0040]
[0041] Where x is a sentence, Perturb is the perturbation of the sentence, is the sentence after perturbation x, that is, the sentence after replacing synonyms or antonyms; c i is the i-th character or word in It is c i synonyms or antonyms of yes The i-th word or phrase in Perturb(x) is the perturbed sentence of the original sentence. A collection of; This means that if a position i is disturbed, there will be a
[0042] S2. Input the original dataset, adversarial dataset and comparison dataset into the pre-trained language model respectively to obtain the corresponding embedding representations, and use the contrastive learning loss function to calculate the distance between the three embedding representations.
[0043] Specifically, in step S2, the specific method for obtaining the corresponding embedded representation includes: forming a positive example pair with each original sample in the original data set and its corresponding positive example, forming a negative example pair with each original sample in the original data set and its corresponding negative example, and obtaining the corresponding embedded representation through a pre-trained language model, and calculating the semantic similarity between each original sample and the corresponding positive example and negative example.
[0044] Among them, two sentences with the same semantics belong to the positive pair, and two sentences with different semantics belong to the negative pair.
[0045] The formula for calculating the semantic similarity between two sentences is as follows:
[0046]
[0047] In the formula, x a and x b represents sentence a and sentence b, exp() is to find the exponential, h is the feature representation of sentence x, represents the embedding of sentence a at the [CLS] position after being processed by the model, Represents the embedding of sentence b at the [CLS] position after being processed by the model.
[0048] Furthermore, the formula for calculating the distance between the three embedding representations using the contrastive learning loss function is as follows:
[0049]
[0050] In the formula, x is a sentence, and the sentence with a semantic distance from x or even completely opposite to x is x. ant , the sentence that is closer to x in semantics is x syn , (x, x syn ) constitutes a positive pair, (x, x ant ) constitute a negative example pair.
[0051] S3. Calculate the gradient of model update based on the contrast learning loss function and the loss function of the original model, and train the model with the goal of reducing the overall loss.
[0052] The calculation formula of the overall loss is as follows:
[0053] L=λ1L ori +λ2L cl
[0054] Where, L ori Indicates that L cl The objective function of the loss of other models except λ1 and λ2 represents the weights learned by the model.
[0055] S4. Update the model parameters through the back-propagation mechanism, iterate until the model converges, and use the optimal model to test on the test set of the downstream task to obtain the prediction results.
[0056] Another preferred embodiment of the present invention discloses a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the steps of the method for improving the robustness of small sample learning based on contrastive learning are implemented as described above.
[0057] The following is a specific example of a method for improving the robustness of small sample learning based on contrastive learning.
[0058] This example proposes an improved method for the task of dialogue emotion recognition. Figure 2 : is a flowchart of the method for improving the robustness of small sample learning based on contrastive learning in this embodiment, which mainly includes four parts: adversarial and contrast data construction, contrast loss calculation, model training and model prediction results. The specific model structure of this embodiment is as follows Figure 3As shown. First, the original dataset is preprocessed, including the construction of synonym and antonym dictionaries, the original dataset is replaced with synonyms using the synonym dictionary to construct the corresponding adversarial dataset, and the original dataset is replaced with antonyms using the antonym dictionary to construct the corresponding comparative dataset. Then the above three datasets are respectively input into the pre-trained language model to obtain the corresponding embedding representations, and the distance between the three embedding representations is calculated using the contrastive learning loss function. The gradient of the model update is calculated according to the contrastive learning loss function and the loss function of the original model, and the model is trained with the goal of smaller overall loss. The model parameters are updated through the back-propagation mechanism and iterated until the model converges. Finally, the optimal model is used to test on the test set of the downstream task to obtain the prediction results. The dataset includes a training set, a validation set, and a test set. During the fine-tuning and prompt tuning process, since the parameters of the model are different after each round of training with the training set, and the accuracy obtained after verification with the validation set is also different, the optimal model parameters (the validation set has the highest accuracy) are saved for the final test of the test set. The specific method is as follows:
[0059] 1. Data structure:
[0060] The CLFT and CLPT methods proposed in this embodiment can be more robust to semantic perturbations in low-resource scenarios, that is, they can better distinguish between sentences with the same semantics and sentences with different semantics. Therefore, this embodiment uses a synonym and antonym replacement method to generate samples. In addition, the model of this embodiment uses a contrastive learning loss function to narrow the distance between samples with the same semantics and push the distance between samples with different semantics. Therefore, it is very important to construct appropriate positive and negative pairs. In this embodiment, two sentences with the same semantics belong to a positive pair, and two sentences with different semantics belong to a negative pair.
[0061] In order to better preserve the syntactic consistency and fluency of the generated sentences, and to generate more difficult sentences, replace as few words as possible in the original sample. Intuitively, when a random word or phrase in a sentence is replaced by a synonym, the generated sentence is likely to be consistent with the original sample semantics. Similarly, when antonyms are used to replace representative words or phrases in a sentence, it is also easy to generate samples whose semantics are irrelevant or even opposite to the original sample. For example, given the sample "There is a little wear on the outer packaging, and it feels good after listening", the word "a little" can be replaced with "some", so that a positive sample with similar semantics "There is a little wear on the outer packaging, and it feels good after listening" is obtained; if "good" is replaced with its antonym "bad", then a negative sample with opposite semantics "There is a little wear on the outer packaging, and it feels bad after listening" can be obtained.
[0062] Specifically, for each input sample x, two samples with completely different semantics are generated. The sample with semantics far away from x or even completely opposite to x is recorded as x ant , and the sample with a semantic distance close to x is recorded as x syn First, we use the LPT (Language Technology Platform) tool to segment the data in the dataset, and construct a synonym and antonym dictionary based on the segmentation results, especially for the representative words. syn and x ant It can be generated by looking up the dictionary and replacing the corresponding synonyms or antonyms.
[0063] The construction of synonym and antonym dictionaries can be based on existing dictionaries. If a word in a sentence cannot be found in an existing dictionary after LPT segmentation, the word and the corresponding synonym or antonym can be manually or automatically added to the synonym or antonym dictionary. When generating contrast or adversarial samples, LPT also segmented the original sample, and some of the words were randomly selected and replaced with corresponding synonyms or antonyms. More specifically, for each word or word c in the input sentence, i , consider a replaceable word set S(c i ), where c i ∈x and x={c1,c2,…,c n}. (c1 is the first word or phrase in the sentence, c2 is the second, n is the nth and last one) Indicates c i A perturbation of , that is, a synonym or antonym. Therefore, the calculation formula for the set of all perturbations of sample x is as follows:
[0064]
[0065] Where x is a sentence, Perturb is the perturbation of the sentence, is the sentence after perturbation x, that is, the sentence after replacing synonyms or antonyms; c i is the i-th character or word in It is c i synonyms or antonyms of yes and the i-th word or phrase; the perturbed sentence of the original sentence of Perturb(x) is A collection of; This means that if a position i is disturbed, there will be a
[0066] 2. Objective function:
[0067] This embodiment provides a new objective function, which includes a loss function of contrastive learning, for fine-tuning and prompt tuning methods of pre-trained models in small sample learning scenarios. The goal of the contrastive learning loss function is to obtain sample similarity in positive pairs and distinguish samples in negative pairs. The contrastive learning objective function mainly focuses on samples perturbed by synonyms and antonyms, so each original sample in the dataset (for example, four public datasets, namely, the sentiment classification dataset EPRSTMT (E-commerce Product Review Dataset for Sentiment Analysis), the natural language inference dataset OCNLI (Original Chinese Natural Language Inference), the short text matching dataset BUSTM (XiaoBu Dialogue Short Text Matching), and the reference resolution dataset CLUEWSC (The Chinese Winograd Schema Challenge dataset) corresponds to only one positive example and one negative example, and the contrastive learning objective function only calculates the feature similarity of one positive example pair and one negative example pair for each data. In addition, the contrastive learning objective function can be used in combination with other objective functions.
[0068] Formally, (x, x syn ) form a positive pair, (x, x ant ) constitute a negative example pair. Therefore, in the feature space, h and h syn The distance metric between them should be as close as possible, and h and h ant The distance metric between them should be as close as possible, where h is the feature representation of x and h syn For x syn The feature representation of h ant For x ant In addition, the embedding of the [CLS] (a position for classification) position is used, that is, h [cls] As a representative of the sentence corresponding feature h. [CLS] The dot product between embeddings can be used to calculate the semantic similarity between different sentences:
[0069]
[0070] In the formula, x a and x b represents sentence a and sentence b, exp() is to find the exponential, h is the feature representation of sentence x, represents the embedding of sentence a at the [CLS] position after being processed by the model, Represents the embedding of sentence b at the [CLS] position after being processed by the model.
[0071] Since only one positive example and one negative example are generated for each sample in the original data set, the contrastive learning objective function is defined as follows:
[0072]
[0073] The contrastive learning objective function is different from the general contrastive function strategy. The latter usually randomly samples multiple data from the data set as negative samples. The goal of this embodiment is to improve the robustness of the fine-tuning and prompt tuning methods when the model is subjected to adversarial or contrastive attacks. Therefore, this embodiment focuses on the generated x syn and x ant In addition, the fine-tuning and prompt tuning methods themselves have objective functions, such as classification loss and MLM loss. For classification loss, the goal is to minimize the probability distribution between the predicted category and the true label. For MLM loss, some words of the input sentence are masked by the [MASK] symbol (mask, replace a word or word in the sentence with the [MASK] symbol), and the goal is to better reconstruct the masked words. This embodiment removes all words except the above L cl The loss of other models is recorded as the objective function L ori , then the final overall loss is calculated as follows:
[0074] L=λ1L ori +λ2L cl
[0075] Where λ1 and λ2 represent the weights learned by the model.
[0076] 3. Model training:
[0077] This embodiment can use, including but not limited to BERT-base as the backbone network of the model structure, and can use, including but not limited to chinese_roberta_wwm_ext (full-word mask pre-training model) to initialize the weight of the pre-trained model. The model can be implemented using tools including but not limited to transformer (a model based on an encoder-decoder structure) and pytorch (an open source Python machine learning library). The number of training generations (epochs) can be set to, but not limited to, 125, and the model optimizer can use, including but not limited to AdamW.
[0078] 4. Model prediction:
[0079] Once the model is trained, predictions can be made. Embodiments of the present invention can make predictions on tasks including but not limited to sentiment analysis, natural language reasoning, short text matching, and coreference resolution.
[0080] A specific example of the method for improving the robustness of small sample learning based on contrastive learning using this embodiment is as follows:
[0081] 1. Fine-tuning
[0082] Original sentence: It is not full screen, and the screen is too narrow (label: negative example, the original pre-trained model predicted correctly)
[0083] Adversarial perturbation (synonym replacement): not full screen, and the screen is very narrow (label: negative example, the original pre-trained model predicted incorrectly)
[0084] Contrast perturbation (antonym replacement): This is full screen, and the screen is very wide (label: positive example, the original pre-trained model predicted an error)
[0085] After using the CLFT of this embodiment, the above three sentences can all be predicted correctly.
[0086] 2. Prompt tuning
[0087] Original sentence: It is not full screen, and the screen is too narrow (label: negative example)
[0088] Use the T5 model to generate the prompt "Very [MASK]!"
[0089] The original sentence becomes: Very [MASK]! It’s not full screen, and the screen is too narrow (label word: bad --> label: negative example, the model will fill in the word "bad" at the [MASK] position, just like filling in the blanks, and then the word "bad" is further mapped to the label "negative example")
[0090] Adversarial perturbation (synonym replacement): Very [MASK]! Not full screen, and the screen is very narrow (label word: bad --> label: negative example, the original pre-trained model predicted an error)
[0091] Contrast perturbation (antonym replacement): [MASK]! This is full screen, and the screen is very wide (label word: good --> label: positive example, the original pre-trained model predicted an error)
[0092] After using the CLPT of this embodiment, the above three sentences can all be predicted correctly.
[0093] In the embodiment of the present invention, by using the public tool LTP (Language Technology Plat) and the constructed synonym and antonym word lists, the model can automatically generate preliminary adversarial and comparative samples. Then, the generated adversarial and comparative samples are manually corrected to ensure the fluency and semantic accuracy of the sentences. Finally, using the contrastive learning method, more robust CLFT and CLPT models can be learned based on the original data set and the constructed adversarial and comparative data sets. The CLFT and CLPT methods proposed in the embodiment of the present invention can better obtain the similarity between the original sample and the adversarial sample, and can also better distinguish the difference between the original sample and the comparative sample. The specific verification process and verification results are as follows:
[0094] First, based on public datasets, such as the sentiment classification dataset EPRSTMT (E-commerce Product Review Dataset for Sentiment Analysis), the natural language inference dataset OCNLI (Original Chinese Natural Language Inference), the short text matching dataset BUSTM (XiaoBu Dialogue Short Text Matching) and the reference resolution dataset CLUEWSC (The Chinese Winograd Schema Challenge dataset), the corresponding adversarial (synonym perturbation) dataset and the comparative (antonym perturbation) dataset are constructed. For the fine-tuning method, the original dataset and its corresponding adversarial dataset and the comparative dataset are directly input into the three-way identical pre-trained language model (including but not limited to the BERT model), and then the CLFT loss is calculated for the output of the three-way model, and the model is fine-tuned according to the loss value to update the model parameters. For the prompt tuning method, first use the T5 model to generate prompts. If the original data is "not full screen, and the screen is too narrow", and the prompt generated by the T5 model is "very [MASK]!", then the data of the prompt tuning method is "very [MASK]! Not full screen, and the screen is too narrow". Similarly, construct adversarial and contrast datasets through synonym and antonym replacement methods, and input them into the pre-trained language model to predict the label word of the [MASK] position. For example, the above example can be "good" and "bad", and then map the label word to a label. For example, the above "good" corresponds to the positive class, and "bad" corresponds to the negative class. Similarly, calculate the CLPT loss for the output of the three-way model, and perform prompt tuning on the model according to the loss value, and update the model parameters. The verification results are shown in the following table:
[0095]
[0096] It can be seen that the CLFT and CLPT methods of this embodiment have higher accuracy, especially in the case of adversarial and contrast disturbances, the robustness of the model can still be guaranteed.
[0097] For the robustness of small sample learning in natural language processing, the use of synonyms and antonyms to construct adversarial and contrast data and the use of contrastive learning objective function to calculate loss proposed by the present invention are complementary. The present invention mainly involves four points: First, construct corresponding synonym and antonym dictionaries according to the data set; Second, by searching the constructed dictionary, the data in the original data set are replaced with synonyms or antonyms to construct adversarial or contrast samples. Third, construct positive and negative pairs, and obtain their corresponding embeddings through the pre-trained model. Fourth, calculate the similarity between the corresponding embeddings through the loss function based on contrastive learning, and sum it with other losses. Among them, the use of synonyms and antonyms to construct adversarial and contrast data sets provides data support for model robustness learning, and the use of contrastive learning objective function to calculate loss can narrow the similarity distance between positive pairs and make the feature difference between negative pairs more obvious. Compared with the existing methods, the present invention has higher prediction performance after the model is disturbed by synonyms or antonyms, and can improve the robustness of the model.
[0098] The above contents are further detailed descriptions of the present invention in combination with specific preferred embodiments, and it cannot be determined that the specific implementation of the present invention is limited to these descriptions. For those skilled in the art of the present invention, several equivalent substitutions or obvious variations can be made without departing from the concept of the present invention, and the performance or use is the same, which should be regarded as belonging to the protection scope of the present invention.
Claims
1. A method for improving the robustness of small sample learning based on contrastive learning, characterized in that: The following steps are involved: S1. Preprocess the original data set to construct adversarial data sets and comparison data sets; S2, input the original dataset, adversarial dataset and comparison dataset into the pre-trained language model respectively, obtain the corresponding embedding representations, and use the contrastive learning loss function to calculate the distance between the three embedding representations; S3. Calculate the gradient of model update based on the contrast learning loss function and the loss function of the original model, and train the model with the goal of reducing the overall loss. In step S2, the specific method of obtaining the corresponding embedded representation includes: forming a positive example pair with each original sample in the original data set and its corresponding positive example, forming a negative example pair with each original sample in the original data set and its corresponding negative example, and obtaining the corresponding embedded representation through a pre-trained language model, and calculating the semantic similarity between each original sample and the corresponding positive example and negative example; Two sentences with the same semantics belong to a positive pair, and two sentences with different semantics belong to a negative pair; The formula for calculating the semantic similarity between two sentences is as follows: In the formula, x a and x b represents sentence a and sentence b, exp() is to find the exponential, h is the feature representation of sentence x, represents the embedding of sentence a at the [CLS] position after being processed by the model, Represents the embedding of sentence b at the [CLS] position after being processed by the model; In step S2, the calculation formula for calculating the distance between the three embedding representations using the contrastive learning loss function is as follows: In the formula, x is a sentence, and the sentence with a semantic distance from x or even completely opposite to x is x. ant , the sentence that is closer to x in semantics is x syn , (x, x syn ) constitutes a positive pair, (x, x ant ) constitute a negative example pair.
2. The method for improving robustness of small sample learning based on contrastive learning according to claim 1, characterized in that: Step S1 specifically includes: S1.1, construct corresponding synonym dictionary and antonym dictionary based on the original data set; S1.
2. By searching the constructed synonym dictionary and antonym dictionary, the data in the original dataset are replaced with synonyms to construct an adversarial dataset, and the data in the original dataset are replaced with antonyms to construct a comparative dataset.
3. The method for improving robustness of small sample learning based on contrastive learning as claimed in claim 2, characterized in that: Step S1 also includes: S1.
3. Calculate the set of all perturbations of the sentence of each original sample in the original dataset. The formula is as follows: Where x is a sentence, Perturb is the perturbation of the sentence, is the sentence after perturbation x, that is, the sentence after replacing synonyms or antonyms; c i is the i-th character or word in It is c i synonyms or antonyms of yes The i-th word or phrase in Perturb(x) is the perturbed sentence of the original sentence. A collection of; This means that if a position i is disturbed, there will be a 4. The method for improving robustness of small sample learning based on contrastive learning according to claim 1, characterized in that: In step S3, the calculation formula of the overall loss is as follows: L=λ1L ori +λ2L cl Where, L ori Indicates that L cl The objective function of the loss of other models except λ1 and λ2 represents the weights learned by the model.
5. The method for improving robustness of small sample learning based on contrastive learning according to claim 1, characterized in that: The method further comprises: S4. Update the model parameters through the back-propagation mechanism, iterate until the model converges, and use the optimal model to test on the test set of the downstream task to obtain the prediction results.
6. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.
