A few-shot relation extraction method based on prompt and contrastive learning
By constructing the PromptRE model and combining template embedding and contrastive learning, the accuracy problem of single-sample or few-sample relation extraction was solved, achieving efficient relation extraction in new fields and improving the model's representation and generalization capabilities.
Patent Information
- Application Number
- CN202211261819.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-14
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2042-10-14
AI Technical Summary
Existing technologies are not very accurate in extracting relationships with a single or few samples, especially when incorporating additional knowledge, making it difficult to cover new domains, and their transfer and generalization capabilities are insufficient.
We employ a prompting and contrastive learning approach to construct the relation extraction model PromptRE. By utilizing the implicit information of the pre-trained language model through template embedding, BERT encoding, global representation optimization, and attention-related contrastive learning, we optimize the model's representation capabilities and train it in conjunction with relational information.
It significantly improves the accuracy of relation extraction in single-sample or few-sample scenarios, enhances the model's representational and generalization capabilities, and is particularly effective in adapting to new domains.
Smart Images

Figure CN115544179B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of sample relationship extraction, and in particular to a few-shot relationship extraction method based on prompt and contrast learning. BACKGROUND
[0002] The existing relationship extraction (RE) task is to obtain the relationship between a sentence and two entities, and the structured language information generated for it is not only easy to process, but also contains key information about a certain aspect of the sentence. RE is a key link for many NLP downstream tasks, including knowledge base enrichment and question answering. Due to the advantages of relationship extraction task for structured language processing and its own scalability, many tasks can be completed through relationship extraction, such as sentiment analysis and event extraction. Although the existing method can effectively complete the relationship extraction task through a large amount of data analysis, the relationship extraction field is numerous, and the data scarcity makes it have higher value to complete the relationship extraction task with a small amount of data. In addition, the single sample learning scenario is a more difficult scenario in few-shot learning, but this scenario is more in line with the needs of human habits in reality. Due to the extreme scarcity of data, higher learning and representation capabilities of the model are required.
[0003] At present, one-shot / few-shot relationship extraction research mainly has two ideas: one is to capture the entity information with higher importance in the sentence to construct a more representative vector representation; the other is to introduce additional information, such as relationship description information, entity category information, or knowledge base structure relationship information, to enrich the implicit knowledge of the representation vector. Although focusing on entity information can improve the effect to a certain extent, there are hidden dangers of poor migration and generalization ability, because the same relationship may contain extremely rare entity information; in addition, the integration of entity information is not ideal for identifying the social relationship between people, and the integration of additional knowledge is helpful for the task, but it is difficult to cover new fields that lack additional knowledge, which will be not conducive to the few-shot relationship extraction task in practical application. SUMMARY
[0004] In view of the above problems existing in the prior art, the technical problem to be solved by the present application is how to improve the accuracy of relationship extraction in the case of few samples.
[0005] To solve the above technical problems, the present application adopts the following technical solution: a few-shot relationship extraction method based on prompt and contrast learning, comprising the following steps:
[0006] S1: select a public statement instance dataset O, the O includes several base classes, and each base class has a labeled instance, divide the data in O into a simulated query set Q and a simulated support set S, wherein Q includes a plurality of query samples, each query sample is composed of a natural language sentence, a head entity and a tail entity; S includes a plurality of support samples, each support sample is composed of a natural language sentence, a head entity, a tail entity, a relation label and relation information, and the relation information is composed of a relation name and a relation description;
[0007] S2: build a relation extraction model PromptRE, PromptRE includes a template embedding module, a BERT encoding module, a global representation optimization module and an attention-related contrast learning module;
[0008] Train PromptRE using simulated query set Q and simulated support set S, as follows:
[0009] S21: input the i-th query sample into the template embedding module to obtain the prompt template embedding corresponding to the query sample;
[0010] Input the natural language sentence, head entity and tail entity of a support sample into the template embedding module to obtain the prompt template embedding corresponding to the support sample;
[0011] S22: input the prompt template embedding corresponding to the query sample in S21 into the BERT encoding module to obtain the corresponding hidden layer representation k = 1, 2,..., K, the hidden layer representation corresponding to the prompt template embedding corresponding to each query sample in the simulated query set Q constitutes a vector space
[0012] Input the prompt template embedding corresponding to the support sample in S21 into the BERT encoding module to obtain the corresponding hidden layer representation j = 1, 2,..., N; k = 1, 2,..., K, the hidden layer representation corresponding to the prompt template embedding corresponding to each support sample in the simulated support set S constitutes a vector space
[0013] Wherein, K represents the size of batchsize, N represents the number of auxiliary judgments for each support sample in the support set, Support set and its corresponding N auxiliary judgments, represents The number of tokens of the corresponding prompt template embedding, represents The number of tokens of the corresponding prompt template embedding, D represents the dimension of the hidden layer vector of each token;
[0014] S23: constructing prompt features of the query sample in S21 and the prompt features of the support sample
[0015]
[0016]
[0017] wherein,
[0018] S24: inputting the relationship information of the support sample in S21 into the BERT encoding module to obtain the hidden layer representation R corresponding to the relationship information j , j = 1, 2,..., N, the hidden layer representations corresponding to the relationship information of all support samples in the simulation support set S constitute a set wherein tokens in R j , the BERT encoding module in this step has the same parameters as the BERT encoding module in S22;
[0019] S25: fusing the hidden layer representation corresponding to the prompt template embedding of the support sample and the hidden layer representation corresponding to the relationship information of the support sample to obtain the fusion feature of the support sample As the formula:
[0020]
[0021] wherein: r jc ∈ R j , r jc represents the hidden layer representation corresponding to the first token "[CLS]", represents the hidden layer representation corresponding to the token "[unused0]", represents the hidden layer representation corresponding to the token "[unused2]".
[0022] S26: the final representation of the query sample The formula is as follows:
[0023]
[0024] S27: using the method of S21-S26 to traverse all query samples in the simulation query set Q to obtain the final representation corresponding to each query sample;
[0025] using the method of S21-S25 to traverse all support samples in the simulation support set S to obtain the support fusion feature corresponding to each support sample;
[0026] S28: Calculate the probability of the predicted relationship label of the ith query sample being the relationship label contained in a certain support sample of the simulation support set S, which is calculated as follows:
[0027]
[0028] where exp represents square;
[0029] S29: Set the relationship label contained in the support sample corresponding to the maximum probability value as the relationship label of the ith query sample, calculate the prediction loss of the ith query sample, and update the parameters of PromptRE using gradient descent method according to the loss. When the loss no longer changes, stop iteration to obtain the trained PromptRE;
[0030] S3: Input the instance to be queried into the trained PromptRE, which is the predicted relationship label of the instance to be queried.
[0031] As a preferred, the calculation method of the prediction loss of the ith query sample in S29 is as follows:
[0032] S291: Calculate the global optimization loss L F of the global representation optimization module by using the cross-entropy loss function, which is calculated as follows:
[0033] L F = -(1-z i (Q)) γ log(z i (Q)); (6)
[0034] where γ represents a hyperparameter;
[0035] S292: Calculate the attention loss of the attention-related contrastive learning module:
[0036] 1) Obtain the representation R jn corresponding to the relationship name in the relationship information of the support sample and the representation R jd corresponding to the relationship description, which is calculated as follows:
[0037] R jn = ∑ t∈n r jt / |n|; (7)
[0038] R jd = ∑ t∈d r jt / |d|; (8)
[0039] where r jt∈R, R represents the relationship label in the support sample, n is the subscript set of the relationship name in R, d is the subscript set of the relationship description in R, |n| is the number of tokens of the relationship name contained in R, |d| is the number of tokens of the relationship description;
[0040] 2) Building the connection between the relationship name in the relationship information of the support sample and the global representation Building the connection between the relationship description in the relationship information and the global representation The formula is as follows:
[0041]
[0042]
[0043] Where, FFN(·) represents a full connection layer, represents vector connection;
[0044] 3) The formula for calculating the attention loss is as follows:
[0045]
[0046] Where, Cos(·) is a cosine similarity function, and η represents a hyperparameter
[0047] S293: The formula for calculating the prediction loss L of the i-th query sample is as follows:
[0048] L = L c + λL F ; (12)
[0049] Where, λ is a hyperparameter.
[0050] Compared with the prior art, the present application has at least the following advantages:
[0051] 1. In the specific scene of one-shot relationship extraction, we designed a prompt template in combination with prototype learning, and fine-tuned a pre-trained language model at a normal scale to obtain excellent model performance. (This method uses templates to build interpretable relationship representations and further utilizes the implicit information of pre-trained language models to improve the representation space representation ability and alleviate the significant gap in prediction quality between single samples and few samples.
[0052] 2. In order to further improve the performance of the model under a small number of samples, a focus-related contrast learning model is proposed, which reasonably utilizes the internal differences of relationship information to optimize the model representation ability, aiming to fully understand the representation of relationship information and reasonably apply it to result prediction. BRIEF DESCRIPTION OF DRAWINGS
[0053] Figure 1 Fig. 1 is a schematic diagram of the operation framework structure of the PromptRE model of the present application.
[0054] Figure 2 Fig. 4 is a schematic diagram of the construction process of the prompt information enhancement of the present application.
[0055] Figure 3 Fig. 8 is a comparison chart of the ablation experiment data of each model.
[0056] Figure 4 Fig. 11 is the experimental results of different lamda in the experiment of the present application. DETAILED DESCRIPTION
[0057] The present application will be further described below.
[0058] Reference Figure 1 A few-shot relation extraction method based on prompt and contrastive learning, comprising the following steps:
[0059] S1: Selecting a public sentence instance dataset O, the O contains several base classes, and each base class has multiple sets of instances with labels, dividing the data in O into a simulated query set Q and a simulated support set S, wherein Q includes multiple query samples, each query sample is composed of a natural language sentence, a head entity and a tail entity; S includes multiple support samples, each support sample is composed of a natural language sentence, a head entity, a tail entity, a relation label and relation information, and the relation information is composed of a relation name and a relation description; the relation label is the label of the relation between the head entity and the tail entity.
[0060] S2: Constructing a relation extraction model PromptRE, PromptRE includes a template embedding module, a BERT encoding module, a global representation optimization module and an attention-related contrastive learning module;
[0061] Training PromptRE using simulated query set Q and simulated support set S, specifically as follows:
[0062] S21: Inputting the i-th query sample into the template embedding module to obtain the prompt template embedding corresponding to the query sample.
[0063] Inputting the natural language sentence, head entity and tail entity of a support sample into the template embedding module to obtain the prompt template embedding corresponding to the support sample.
[0064] S22: Inputting the prompt template embedding corresponding to the query sample in S21 into the BERT encoding module to obtain the corresponding hidden layer representation k = 1, 2,..., K, the hidden layer representation corresponding to the prompt template embedding corresponding to each query sample in the simulated query set Q constitutes a vector space
[0065] The prompt template corresponding to the support sample in S21 is embedded into the input BERT encoding module to obtain the corresponding hidden layer representation j = 1, 2,..., N; k = 1, 2,..., K, the hidden layer representation corresponding to the prompt template embedding of each support sample in the support set S is simulated to form a vector space
[0066] Wherein, K represents the size of the batchsize, N represents the number of support samples corresponding to each support sample in the support set S The N auxiliary judgments corresponding to the support set, represents The number of tokens after the corresponding prompt template embedding, represents The number of tokens after the corresponding prompt template embedding, and D represents the dimension of the hidden layer vector of each token.
[0067] Specifically, referring to Figure 2 The prompt template embedding PTE obtained by fusing the original input (including the query and support instance inputs) and the prompt template. Figure 2 In the above formula, “[x]” is a placeholder that replaces the original input, “[head]” and “[tail]” represent the head entity and the tail entity respectively. The head entity and the tail entity are wrapped with “[unused0] / [unused1]” and “[unused2] / [unused3]” respectively to uniformly represent the feature representation of the two entities, and the tokens representing the head entity and the tail entity are synchronized to the prompt template.
[0068] S23: Constructing prompt features of query samples in S21 and support sample prompt features
[0069]
[0070]
[0071] Wherein,
[0072] S24: Inputting the relationship information of the support sample in S21 into the BERT encoding module to obtain the hidden layer representation R corresponding to the relationship information j , j = 1, 2,..., N, the hidden layer representation corresponding to the relationship information of all support samples in the support set S is simulated to form a set Wherein represents Rj the number of tokens, the BERT encoding module in this step is the same as the BERT encoding module parameter in S22.
[0073] S25: Fuse the prompt template embedding corresponding to the support sample into the hidden layer representation corresponding to the support sample and the hidden layer representation of the relationship information corresponding to the support sample to obtain the fusion feature of the support sample As the formula:
[0074]
[0075] Where: r jc ∈R j , r jc The first token “[CLS]” corresponds to the hidden layer representation, The token “[unused0]” corresponds to the hidden layer representation, The token “[unused2]” corresponds to the hidden layer representation.
[0076] S26: The final representation of the query sample The formula is as follows:
[0077]
[0078] S27: Use the method of S21-S26 to traverse all query samples in the simulated query set Q to obtain the final representation corresponding to each query sample;
[0079] Use the method of S21-S25 to traverse all support samples in the simulated support set S to obtain the support fusion feature corresponding to each support sample;
[0080] S28: The formula for calculating the probability of the predicted relationship label of the i-th query sample being the relationship label contained in a support sample in the simulated support set S is as follows:
[0081]
[0082] Where, exp represents square;
[0083] S29: Set the relationship label contained in the support sample corresponding to the maximum probability value as the i-th query sample, calculate the prediction loss of the i-th query sample, and update the parameters of PromptRE according to the loss using gradient descent method, when the loss no longer changes, stop iteration to obtain the trained PromptRE;
[0084] S3: Input the query instance into the trained PromptRE, and the input is the predicted relationship label of the query instance.
[0085] Specifically, the calculation method of the i-th query sample prediction loss in S29 is as follows:
[0086] S291: The global representation optimization module calculates the global optimization loss L F , as follows:
[0087] L F =-(1-z i (Q)) γ log(z i (Q)); (6)
[0088] Where γ represents a hyperparameter, and generally γ = 1.
[0089] S292: The attention-related contrastive learning module calculates the attention loss:
[0090] 1) Obtain the representation R jn and the representation R jd corresponding to the relationship name and the relationship description in the relationship information of the support sample, as follows:
[0091] R jn =∑ t∈n r jt / |n|; (7)
[0092] R jd =∑ t∈d r jt / |d|; (8)
[0093] Where r jt ∈R, R represents the relationship label in the support sample, n is the index set of the relationship name in R, d is the index set of the relationship description in R, |n| is the number of tokens of the relationship name contained in R, and |d| is the number of tokens of the relationship description.
[0094] 2) Establish the connection between the relationship name and the global representation in the relationship information of the support sample Establish the connection between the relationship description and the global representation in the relationship information , as follows:
[0095]
[0096]
[0097] Where FFN(·) represents a fully connected layer, represents vector connection.
[0098] 3) The formula for calculating the attention loss is as follows:
[0099]
[0100] where Cos(·) is the cosine similarity function, and η represents a hyperparameter.
[0101] S293: The formula for calculating the i-th query sample prediction loss L is as follows:
[0102] L = L c + λL F ; (12)
[0103] where λ is a hyperparameter.
[0104] The present application successfully integrates the auxiliary role of the knowledge contained in the pre-training language model to the downstream task through the prompt template embedding method, and we utilize the relationship information to make the representation ability of our model stronger. In addition, through the prompt template embedding method, the dependence of the model representation on the head entity and the tail entity representation is reduced, which makes the model have more advantages in extracting part of the relationship label.
[0105] Experimental verification:
[0106] 1. Data
[0107] FewRei is used as the experimental data set. In addition, due to the small amount of data of FewRel, we also introduce the Wiki-KB data set. First, we clean the data with missing labels, such as instances with only part of the entity labels or no relationship description. For convenience, for each instance of a sentence, we only take one pair of entity relationship triplets, and then we exclude the classes with less than 20 instances. The data set information is shown in Table 1.
[0108] Table 1: Data set parameters
[0109] Dataset #cls. #insts. FewRel 80 56000 Wiki-KB 212 354113
[0110] 2. Evaluation and implementation details
[0111] We evaluate the model of the present application by randomly sampling 10,000 tasks from the validation data. Since one-shot learning is a special case of few-shot learning, we use the official evaluation script of one-shot learning to consider only the case where the reference instance has only one, and experiment with N classes in relations 5 and 10. The performance of the model is evaluated by the average accuracy of the query set of multiple N-way-l-shot tasks.
[0112] The above method is implemented using PyTorch and trained on 1 RTX3060 GPU. We use the Transformer library of Huggingface3 and use the uncased model of BERTbase as the encoder for fair comparison. The AdamW optimizer is used to minimize the loss. We make a table of manually set hyperparameters, where maxlength is the maximum length of the sentence encoded by the BERT encoder. Through statistics, we found that the average length of FewRel instances is about 34, while the average length of Wiki-KB* instances is about 36, and more than 90% of the tokens of the sentence are within 50. Therefore, the maximum length of the input is suitable for this experiment.
[0113] Table 2 Hyperparameter table
[0114] Hyper-parameters Value λ 1 η 10 γ 1 Leaming rate 2e-5 Max length 128 Hiddensize 768
[0115] 3. Comparison results
[0116] The model of the present application will be compared with the following several baselines.
[0117] 1) Proto-BERT: Because the method of the present application is based on Prototype network and BERT, we also compare Proto-BERT without relationship information.
[0118] 2) REGRAB: A Bayesian meta-learning method with external global relationship graph.
[0119] 3) HCRP: A hybrid contrastive relationship prototype method.
[0120] 4) HCRP+CP: An entity masked contrastive pre-training framework for response. They first constructed a large-scale dataset from Wiki data for pre-training, which contains 744 relationships and 867,278 sentences.
[0121] 5) BERT-PAIR: A method for measuring the similarity of sentence pairs.
[0122] Table 3 Accuracy on two datasets
[0123]
[0124] Table 3 shows the test results of the validation set on the FewRel1.0 dataset. It can be seen that our method is better. The strong baseline model is greatly improved, especially in the case of N = 10, which has a significant improvement, indicating that our model has sufficient representation ability and generalization ability. At the same time, we also apply the model to the wiki-KB dataset, and our model also performs well in the case of large data and labels. The improvement of our model is mainly in the following aspects:
[0125] 1. Prompt makes full use of pre-trained language models, so our model can be compared with models pre-trained on corresponding tasks. 2. Our contrastive learning method introduces additional information and maintains consistency between training and testing.
[0126] 4. Ablation experiment
[0127] See Figure 3 , we compare the effectiveness of the two methods by eliminating the contrastive learning loss. For accuracy, we use a large amount of data on Wiki-KB for experiments. We conduct experiments on N = 5 and N = 10, and the data results are similar in both cases. The content of the ablation experiment is as follows.
[0128] promptRE: The instant optimization method for relationship extraction in a single scene proposed by the invention.
[0129] Mask_only: We ignore the Focus-RelatedContrastiveLearning part to conduct experiments based on promptRE.
[0130] CLS_wprompt: We use templates to modify the original input, but use the "[CLS]" mark instead of the prompt function to conduct experiments.
[0131] CLS_woprompt: We skip the prompt template embedding stage and directly input the original input into the BERT Encoder.
[0132] First, promptRE and Mask_only with prompt are significantly better than Figure 3prompt. Moreover, introducing the contrastive learning loss when using prompt still improves the absolute accuracy of the model by about 1%, which indicates the effectiveness of our contrastive learning method. To rule out the biased input due to the prompt template, we compare whether merging the template into the input without prompt affects the results. CLS_wprompt means using CLS as the sentence relation representation after input templating, and CLS_woprompt is directly sent to the model without prompt template processing. It can be seen that when N = 10, the modification of the input with the template almost does not affect the model effect, but when N = 5, the use of the template can slightly improve the model effect. It can be seen from the figure that our method has a significant advantage in all cases, which proves the effectiveness of our instant training method.
[0133] 5. Template analysis
[0134] We designed several different prompt samples to join the model to compare the possible impact of prompts on downstream tasks under different design ideas. We artificially designed several different templates and analyzed the impact of these templates. For each template, “[X]” is a placeholder for the input sentence. ‘[head]’ and ‘[tail]’ represent the head entity and tail entity respectively, which will be filled with special markers, representing the head entity and tail entity respectively. We tried to combine sentences and entities in different ways, as shown in Table 4. We found that different templates do have an impact on accuracy. However, none of the prompt template embeddings showed worse results, which indicates that the method of the present application is weakly related to the template. Based on this, our contrastive experiment uses the first template as a measure of the model.
[0135] Table 4 Different prompt template experiments
[0136] Prompt Template N=5 N=10 [X] relation between [head] and [tail] is [MASK] 96.18 93.67 The relation between [head] and [tail] in the [X] is [MASK] 97.30 94.25 [X], the relation of [head] and [tail] is [MASK] 98.94 97.68 [X] [head] [tail] [MASK] 98.68 97.91
[0137] 5. Hyperparameter experiment
[0138] To further illustrate the effectiveness of our model, we conducted a hyperparameter experiment. The loss function of the present application consists of two parts. In order to find the most suitable loss function ratio, we conducted experiments. We take every 0.2 to experiment, and the experimental results are shown in Figure 4 When λ is 0, the model ignores the prototype network model of the global representation, and the experimental results are equivalent to random prediction results. Therefore, we omitted the histogram when λ = 0. Next, from Figure 4 It can be seen that when N is different, the best choice is at different positions. When N = 10, the best choice is 0.6, and the accuracy when λ = 1 is very close. And when N = 5, the optimal choice is 1. Therefore we simply use λ = 1 as the optimal value of the parameter to operate the model.
[0139] Finally, it is to be explained that the above embodiments are only used to illustrate the technical solutions of the present application but not to limit the present application. Although the present application has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solutions of the present application can be modified or equivalently replaced without departing from the purpose and scope of the present application, and all of them should be covered in the scope of the claims of the present application.
Claims
1. A method for extracting relationships from very few samples based on cueing and contrastive learning, characterized in that: Includes the following steps: S1: Select the public statement instance dataset O, which contains several base classes, and each base class has labeled instances. Divide the data in O into a simulated query set Q and a simulated support set S. Q includes multiple query samples, each consisting of a natural statement, a head entity, and a tail entity. S includes multiple support samples, each consisting of a natural statement, a head entity, a tail entity, a relation label, and relation information. The relation information consists of a relation name and a relation description. S2: Construct the relation extraction model PromptRE, which includes a template embedding module, a BERT encoding module, a global representation optimization module, and an attention-related contrastive learning module; PromptRE is trained using a simulated query set Q and a simulated support set S, as follows: S21: Input the i-th query sample into the template embedding module to obtain the prompt template embedding corresponding to the query sample; Input the natural language statement, header entity, and footer entity of a support sample into the template embedding module to obtain the prompt template embedding corresponding to that support sample. S22: Embed the prompt template corresponding to the query sample mentioned in S21 into the input BERT encoding module to obtain the corresponding hidden layer representation. The hidden layer representation corresponding to the prompt template embedding for each query sample in the simulated query set Q constitutes a vector space. The prompt template corresponding to the support sample described in S21 is embedded into the input BERT encoding module to obtain the corresponding hidden layer representation. The hidden layer representation corresponding to the prompt template embedding of each support sample in the simulated support set S constitutes a vector space. Where K represents the batch size, and N represents the number of batches. It supports N auxiliary judgments within the set. express The number of tokens embedded in the corresponding prompt template. express The number of tokens embedded in the corresponding prompt template, where D represents the dimension of the hidden layer vector for each token; S23: Construct the hint features of the query sample described in S21 and supporting sample cue features in, S24: Input the relational information of the support samples described in S21 into the BERT encoding module to obtain the hidden layer representation R corresponding to the relational information. j Let j = 1, 2, ..., N. The hidden layer representations corresponding to the relational information of all support samples in the simulated support set S are used to construct a set. in R represents j The number of tokens, and the parameters of the BERT encoding module in this step are the same as those of the BERT encoding module in S22; S25: The fused feature of the support sample is obtained by fusing the hidden layer representation corresponding to the prompt template embedding of the support sample and the hidden layer representation of the relation information corresponding to the support sample. As in the formula: Where: r jc ∈R j r jc The hidden layer representation corresponding to the first token "[CLS]" is shown below. This represents the hidden layer representation corresponding to token "[unused0]". This represents the hidden layer representation corresponding to token "[unused2]". S26: The final representation of the query sample The formula is as follows: S27: Use the methods in S21-S26 to traverse all query samples in the simulated query set Q to obtain the final representation corresponding to each query sample; The S21-S25 method is used to traverse all support samples in the simulated support set S to obtain the branch fusion feature corresponding to each support sample; S28: The formula for calculating the probability that the predicted relation label of the i-th query sample is the relation label contained in a support sample of the simulated support set S is as follows: Where exp represents the square; S29: Let the relation label contained in the support sample corresponding to the maximum probability value be the i-th query sample. Calculate the prediction loss of the i-th query sample and update the parameters of PromptRE using gradient descent based on the loss. Stop the iteration when the loss no longer changes to obtain the trained PromptRE. S3: Input the query instance into the trained PromptRE. The input will be the predicted relation label of the query instance.
2. The method for extracting relationships from very few samples based on cueing and contrastive learning as described in claim 1, characterized in that: The calculation method for the prediction loss of the i-th query sample in S29 is as follows: S291: The global optimization loss L is calculated in the global representation optimization module using the cross-entropy loss function. F The formula is as follows: L F =-(1-z i (Q)) γ log(z i (Q)); (6) Where γ represents the hyperparameter; S292: Calculate attention loss in the attention-related contrastive learning module: 1) Obtain the representation R corresponding to the relation name in the relation information of the supporting samples. jn The representation R corresponding to the relation description jd The formula is as follows: R jn =∑ t∈n r jt / |n|; (7) R jd =∑ t∈d r jt / |d|; (8) Where r jt ∈R, where R represents the relation label in the supporting sample, n is the set of subscripts of relation names in R, d is the set of subscripts of relation descriptions in R, |n| is the number of tokens of relation names contained in R, and |d| is the number of tokens of relation descriptions; 2) Construct the relationship between relation names and global representations in the relation information supporting the samples. Constructing the connection between relation descriptions and global representations in relational information The formula is as follows: Where FFN(·) represents a fully connected layer, Indicates vector concatenation; 3) The formula for calculating attention loss is as follows: Where Cos(·) is the cosine similarity function, and η represents the hyperparameter. S293: The formula for calculating the prediction loss L of the i-th query sample is as follows: L=L c +λL F ; (12) Where λ is a hyperparameter.
Citation Information
Patent Citations
Small sample medical relationship classification method based on multilayer attention mechanism
CN113505225A
Contrast task adaptive learning method for few-sample intention recognition
CN113516209A