Small sample relationship classification method and system based on metric learning
Through the small-sample relationship classification method based on metric learning, the prototype network and the relationship twin network are used to calculate the similarity and weighting of sentence feature vectors, which solves the problem of poor performance in classifying small-sample relationship categories in the existing technology and achieves Excellent performance on FewRel dataset.
Patent Information
- Application Number
- CN202210743784.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-27
- Publication Date
- 2024-08-20
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The existing technology relies on a large amount of labeled data in the relationship classification task, which is especially inapplicable to a small number of samples in the long-tail distribution relationship category. Moreover, the deep learning model has insufficient generalization ability in the case of small samples, resulting in an impact on the classification performance.
A small-sample relationship classification method based on metric learning is used to calculate the similarity between the sentence feature vector and the prototype center through the prototype network, relationship twin network and cross-fusion module, perform weighting processing and information fusion, and improve the model's classification of a small number of sample relationship categories. ability.
It effectively improves the classification performance of long-tail relationship categories and improves the model's generalization ability in the case of few samples, especially on the FewRel data set.
Smart Images

Figure CN114997331B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of relationship classification, and in particular to a small sample relationship classification method and system based on metric learning. Background Art
[0002] As an important issue in natural language processing, relation classification can classify the semantic relationship between entity pairs in a sentence. Relation classification belongs to the problem of relation extraction for limited categories. It uses the data of labeled relations for training, identifies the relationship categories between labeled entity pairs in the text, and summarizes them to form structured data that can be understood by computers, which is conducive to further application in tasks such as information retrieval, knowledge graph construction, and question-answering systems. Deep learning imitates the learning method of the human brain and draws on relevant knowledge such as statistics to form feature representations of data. In recent years, the research on deep learning methods has been continuously deepened and applied to natural language processing tasks. Deep learning models can extract and combine shallow features to form abstract deep-level feature representations. Compared with traditional natural language processing methods, deep learning methods can extract deeper information from sentences. The deep learning-based method constructs a model from sentence information input to target task output by learning feature extraction, reducing the burden of manually constructing features. The mapping between input and output of deep learning networks through linear operations also makes deep learning more suitable for classification tasks. Traditional supervised learning classification methods rely on a large amount of manually labeled data. The labeling work consumes a lot of manpower and is affected by objective factors such as people's different knowledge backgrounds, which can lead to inaccurate labeled data.
[0003] In order to solve these problems, a remote supervision method based on deep learning is proposed. In remote supervision learning, the text in the corpus is automatically labeled by aligning the text with the knowledge graph in a heuristic way, thereby obtaining a large amount of labeled data. It is believed that if the entity pairs labeled in the text already have a relationship in the knowledge graph, then this relationship is used to label the text containing this entity pair in the corpus, so that large-scale training data can be automatically constructed. However, the relationships represented in the texts containing the same entity pairs in the corpus may not be completely consistent, which will introduce noise data that does not belong to the relationship. At the same time, although a large amount of labeled data can be obtained through training, the relationships in the knowledge graph show a long-tail distribution, and there are only a few instances in most relationship categories, while most of the data obtained are concentrated in a small number of relationship categories. Therefore, the classification method that requires a large amount of labeled data is not applicable to relationship categories containing a small number of samples, and the performance of deep learning will also be affected.
[0004] Inspired by the fact that humans can summarize rules and learn with only a small number of samples, researchers hope that machine learning can simulate the way the human brain learns, and have the same learning ability based on a small amount of data as humans for categories that have not appeared in the learning process, thus deriving small sample learning. The goal of small sample learning is to learn new categories from a small amount of data, that is, the model trained with a small number of samples meets the need to correctly classify new categories during testing. Through training, the model learns the generalization ability to adapt to learning between different categories, so that when facing new categories in the test, there is no need to change the method, and the classification task of the new category can be adapted based on the learned generalization knowledge. Relation classification tasks under small sample learning are currently the focus of research in relation classification tasks. Summary of the invention
[0005] The purpose of the embodiments of the present invention is to provide a small sample relationship classification method and system based on metric learning, aiming to solve the problems raised in the above background technology.
[0006] To achieve the above object, the present invention provides the following technical solutions:
[0007] The small sample relation classification method based on metric learning includes the following steps:
[0008] Step S1: The sentences in the support set and query set are obtained through the encoder to obtain the feature vectors of the sentences; the mean between all support instances in each category is calculated to obtain the prototype center of each category as the category representative; the distance between the query instance and the prototype center of each category is calculated to classify the query instance;
[0009] Step S2: Calculate the similarity between the query instance and each support instance in the support set through the relational twin network; obtain the weight of each support instance in the category based on the calculated similarity; adjust the calculation method of the prototype center to a weighted calculation of each instance in the category according to the calculated weight;
[0010] Step S3: Cross-fuse the query instance features and the prototype center features of each category to obtain the influence factors of the query instance and each category under the influence of each other, and obtain the prototype center of the fusion semantics under the influence of the influence factors and query instance
[0011] Furthermore, in step S1, in the relation classification task, for the N-way K-shot setting of the small sample problem, the set It consists of K instances of each category under N relation categories, where Represents one of the N*K sentence instances, r i Represents one of the N relationship categories corresponding to the sentence annotation, the prototype center p iRepresents the mean of all instance embedding vectors in the category:
[0012]
[0013] Among them, f represents the feature vector calculated by the encoder of the sentence instance;
[0014] After obtaining the prototype center of each category in the support set, the similarity between the query instance and each category is obtained by measuring the distance between the query instance and the prototype center of each category. The probability distribution of the query instance belonging to each category is calculated through the softmax function, and the query instance is divided into the category closest to the prototype center.
[0015] Furthermore, in step S2, the specific operation of obtaining the similarity between the query instance and the support instance is:
[0016] After the feature vectors of the two sentences pass through the fully connected layer, they are put into the distance calculation function for similarity calculation, and a value between 0 and 1 is obtained as the similarity score of the two sentences. The calculation formula of the similarity score is:
[0017]
[0018] σ represents the sigmoid activation function, the similarity score between two instances It means that the similarity between two sentence vectors is in the range of 0-1 under the action of the activation function.
[0019] Furthermore, in step S2, the specific operation of normalizing the weights is:
[0020] The softmax function is used to reduce the sum of the weights of each instance in the same category to 1. Combined with the attention mechanism, the similarity weights of the support instances in the same category in the support set to the target query instance are used to weight the calculation of the prototype center. The weight calculation formula is:
[0021]
[0022] The calculation formula of the prototype center pi strengthened by the relational twin network is:
[0023]
[0024] The prototype center obtained after weighted processing combines the information of the query instance, enhances the instance information that is more similar to the query instance, and enhances the category information with the same relationship as the query instance in the subsequent classification task.
[0025] Furthermore, in step S3, the prototype center of the fusion semantics is obtained under the influence of the influence factor and query instance The calculation formula is:
[0026]
[0027]
[0028] in, The function indicates that the mean of the obtained matrix is calculated in the feature direction.
[0029] A small sample relation classification system based on metric learning, including:
[0030] A prototype network module, wherein the prototype network module is used to construct a small sample relationship classification architecture;
[0031] A relational twin network enhanced prototype center module, which is used to calculate the similarity score between the query instance and each supporting instance, and adjust the prototype center calculation method according to the similarity score;
[0032] A cross fusion module is used to fuse information of query features and class features.
[0033] Furthermore, the prototype network module adopts an episode-based segmented training method during training. For a sample set Q used for query classification in training, the query instance q belongs to the relation r i The probability is:
[0034]
[0035] Where d represents the distance calculation function, and N represents the number of all relations in the relation set;
[0036] During the training process, learning is performed by minimizing the cross entropy loss function:
[0037]
[0038] Here, |Q| represents the number of query instances.
[0039] Furthermore, the specific operation of the episode-based segmented training method is:
[0040] First, extract the N required training episodes from the N relationship categories in the training set. S categories, and from the training set N S Randomly select K instances from each category S The sample instances required for episode training constitute the support set, and then from the training set N S Randomly sample K instances from the remaining instances of the categoriesQ The query instances required for episode training constitute the query set. In the training, each category calculates the prototype center p of the category by taking the average of the supporting instances. i Finally, the loss value is updated by optimizing the distance from the query instance to the center of each category prototype to achieve model training.
[0041] Compared with the prior art, the present invention has the following beneficial effects:
[0042] This paper proposes a small-sample relationship classification method based on metric learning for relationship categories that only contain a small number of samples in long-tail relationships. The small-sample relationship classification method is constructed using the prototype network in metric learning, and the relationship twin network and cross-fusion layer are used to further improve the relationship classification effect. This method performs well on the FewRel dataset. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 It is a flow chart of the present invention.
[0044] Figure 2 This is a structural diagram of the relational twin network in the present invention.
[0045] Figure 3 This is a cross-fusion structure diagram in the present invention. DETAILED DESCRIPTION
[0046] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0047] The specific implementation of the present invention is described in detail below in conjunction with specific embodiments.
[0048] An embodiment of the present invention provides a small sample relationship classification method based on metric learning, comprising the following steps:
[0049] Step S1: The sentences in the support set and query set are obtained through the encoder to obtain the feature vectors of the sentences; the mean between all support instances in each category is calculated to obtain the prototype center of each category as the category representative; the distance between the query instance and the prototype center of each category is calculated to classify the query instance;
[0050] Step S2: Calculate the similarity between the query instance and each support instance in the support set through the relational twin network; obtain the weight of each support instance in the category based on the calculated similarity; adjust the calculation method of the prototype center to a weighted calculation of each instance in the category according to the calculated weight;
[0051] Step S3: Cross-fuse the query instance features and the prototype center features of each category to obtain the influence factors of the query instance and each category under the influence of each other, and obtain the prototype center of the fusion semantics under the influence of the influence factors and query instance
[0052] In the embodiment of the present invention, preferably, the input is two parts of data: a support set and a query set, wherein the support set contains N relationship categories and each relationship category contains K support instances. Figure 1 In the figure, Encoder is the instance encoder; RSN is the relational twin network; FC is the fully connected layer; Fusion Layer is the fusion layer; Distance is the distance calculation function.
[0053] As a preferred embodiment of the present invention, in step S1, in the relationship classification task, for the N-way K-shot setting of the small sample problem, the set It consists of K instances of each category under N relation categories, where Represents one of the N*K sentence instances, r i Represents one of the N relationship categories corresponding to the sentence annotation, the prototype center p i Represents the mean of all instance embedding vectors in the category:
[0054]
[0055] Among them, f represents the feature vector calculated by the encoder of the sentence instance;
[0056] After obtaining the prototype center of each category in the support set, the similarity between the query instance and each category is obtained by measuring the distance between the query instance and the prototype center of each category. The probability distribution of the query instance belonging to each category is calculated through the softmax function, and the query instance is divided into the category closest to the prototype center.
[0057] In the embodiment of the present invention, preferably, the query instance is classified into the category to which the prototype center of the category is closer.
[0058] As a preferred embodiment of the present invention, in step S2, the specific operation of obtaining the similarity between the query instance and the support instance is:
[0059] After the feature vectors of the two sentences pass through the fully connected layer, they are put into the distance calculation function for similarity calculation, and a value between 0 and 1 is obtained as the similarity score of the two sentences. The calculation formula of the similarity score is:
[0060]
[0061] σ represents the sigmoid activation function, the similarity score between two instances It means that the similarity between two sentence vectors is in the range of 0-1 under the action of the activation function.
[0062] In the embodiment of the present invention, preferably, and f(q) are the supporting instances respectively The feature vector calculated by the encoder and the query instance q are calculated by the fully connected layer and the similarity is measured by the distance calculation function. The Euclidean distance is used as the distance metric.
[0063] As a preferred embodiment of the present invention, in step S2, the specific operation of normalizing the weights is:
[0064] The softmax function is used to reduce the sum of the weights of each instance in the same category to 1. Combined with the attention mechanism, the similarity weights of the support instances in the same category in the support set to the target query instance are used to weight the calculation of the prototype center. The weight calculation formula is:
[0065]
[0066] Prototype center p strengthened by relational twin network i The calculation formula is:
[0067]
[0068] The prototype center obtained after weighted processing combines the information of the query instance, enhances the instance information that is more similar to the query instance, and enhances the category information with the same relationship as the query instance in the subsequent classification task.
[0069] As a preferred embodiment of the present invention, in step S3, the prototype center of the fusion semantics is obtained under the influence of the influence factor. and query instance The calculation formula is:
[0070]
[0071]
[0072] in, The function indicates that the mean of the obtained matrix is calculated in the feature direction.
[0073] An embodiment of the present invention provides a small sample relationship classification system based on metric learning, including:
[0074] A prototype network module, wherein the prototype network module is used to construct a small sample relationship classification architecture;
[0075] A relational twin network enhanced prototype center module, which is used to calculate the similarity score between the query instance and each supporting instance, and adjust the prototype center calculation method according to the similarity score;
[0076] A cross fusion module is used to fuse information of query features and class features.
[0077] In the embodiment of the present invention, preferably, the relational twin network enhanced prototype center module is composed of two parts, including two completely identical encoders with shared weights and similarity calculation. The input is two sentence instances, and the output is the similarity of the relationship between the entity pairs in the two sentences. The structure is as follows: Figure 2 As shown in Figure 1, in order to capture the semantics of the sentence, the two input instances are first passed through the encoder to obtain the feature vector. First, the relationship category r i Prototype Center p i The feature matrix of fusion information is obtained by the softmax function, and the vector obtained by averaging the matrix in the feature direction passes through two fully connected layers to obtain the prototype center p i and the query instance q on each other. At the end of the cross-fusion module, the prototype center p is respectively i The feature vector of the query instance q is weighted and processed as the vector representation before the final classification. Since the semantic information of the query instance and the prototype center as the category representative is combined, the query instance is better utilized, and a more targeted feature vector is obtained under the influence of each other. Therefore, when classifying, the categories processed by the cross-fusion layer are more distinguishable for the query instance.
[0078] As a preferred embodiment of the present invention, the prototype network module adopts an episode-based segmented training method during training. For a sample set Q used for query classification in training, the query instance q belongs to the relation r i The probability is:
[0079]
[0080] Where d represents the distance calculation function, and N represents the number of all relations in the relation set;
[0081] During the training process, learning is performed by minimizing the cross entropy loss function:
[0082]
[0083] Here, |Q| represents the number of query instances.
[0084] In the embodiment of the present invention, preferably, d is a distance calculation function, and the distance metric is generally calculated using Euclidean distance.
[0085] As a preferred embodiment of the present invention, the specific operation of the episode-based segmented training method is:
[0086] First, extract the N required training episodes from the N relationship categories in the training set. S categories, and from the training set N S Randomly select K instances from each category S The sample instances required for episode training constitute the support set, and then from the training set N S Randomly sample K instances from the remaining instances of the categories Q The query instances required for episode training constitute the query set. In the training, each category calculates the prototype center p of the category by taking the average of the supporting instances. i Finally, the loss value is updated by optimizing the distance from the query instance to the center of each category prototype to achieve model training.
[0087] In the embodiment of the present invention, preferably, sample data needs to be extracted for training at each stage. For tasks where each category in the training set contains K samples under N relationship categories, a value N not greater than N is set. S As the number of relation categories in each episode, samples are drawn from the training set using equal probability random sampling without replacement to calculate the loss of each episode.
[0088] The above are only preferred embodiments of the present invention. It should be pointed out that, for those skilled in the art, several modifications and improvements can be made without departing from the concept of the present invention. These should also be regarded as the protection scope of the present invention. These will not affect the effect of the implementation of the present invention and the practicality of the patent.
Claims
1. Small sample relationship classification method based on metric learning, It is characterized in that The following steps are involved: Step S1: The sentences in the support set and query set are passed through the encoder to obtain the feature vectors of the sentences; the mean between all support instances in each category is calculated to obtain the prototype center of each category as the category representative; Calculate the distance between the query instance and the center of each category prototype and classify the query instance; Step S2: Calculate the similarity between the query instance and each support instance in the support set through the relational twin network; obtain the weight of each support instance in the category based on the calculated similarity; adjust the calculation method of the prototype center to a weighted calculation of each instance in the category according to the calculated weight; Step S3: Cross-fuse the query instance features and the prototype center features of each category to obtain the influence factors of the query instance and each category under the influence of each other, and obtain the prototype center of the fusion semantics under the influence of the influence factors and query instance ; In step S1, in the relation classification task, for the N-way K-shot setting of the small sample problem, the set It consists of K instances of each category under N relation categories, where represents one of the N*K sentence instances, Indicates one of the N relationship categories corresponding to the sentence annotation, prototype center Represents the mean of all instance embedding vectors in the category: in, f Represents the feature vector calculated by the encoder for the sentence instance; After obtaining the prototype center of each category in the support set, the similarity between the query instance and each category is obtained by measuring the distance between the query instance and the prototype center of each category. The probability distribution of the query instance belonging to each category is calculated by the softmax function, and the query instance is divided into the category closest to the prototype center. In step S2, the specific operation of obtaining the similarity between the query instance and the support instance is: After the feature vectors of the two sentences pass through the fully connected layer, they are put into the distance calculation function for similarity calculation, and a value between 0 and 1 is obtained as the similarity score of the two sentences. The calculation formula of the similarity score is: ; Represents the sigmoid activation function, the similarity score between two instances Indicates that the similarity between two sentence vectors is in the range of 0-1 under the action of the activation function; In step S2, the specific operation of normalizing the weights is: The softmax function is used to reduce the sum of the weights of each instance in the same category to 1. Combined with the attention mechanism, the similarity weights of the support instances in the same category in the support set to the target query instance are used to weight the calculation of the prototype center. The weight calculation formula is: ; Prototype Center Enhanced by Relational Twin Network The calculation formula is: ; The prototype center obtained after weighted processing combines the information of the query instance, enhances the instance information that is more similar to the query instance, and enhances the category information with the same relationship as the query instance in the subsequent classification task; In step S3, the prototype center of the fusion semantics is obtained under the influence of the influence factor. and query instance The calculation formula is: ; in, The function indicates that the mean of the obtained matrix is calculated in the feature direction.
2. Small sample relation classification system based on metric learning, It is characterized in that include: A prototype network module, wherein the prototype network module is used to construct a small sample relationship classification architecture; A relational twin network enhanced prototype center module, which is used to calculate the similarity score between the query instance and each supporting instance, and adjust the prototype center calculation method according to the similarity score; A cross fusion module, which is used to fuse the query features and the central features of each category prototype; In the relation classification task of the prototype network module, for the N-way K-shot setting of the small sample problem, the set It consists of K instances of each category under N relation categories, where represents one of the N*K sentence instances, Indicates one of the N relationship categories corresponding to the sentence annotation, prototype center Represents the mean of all instance embedding vectors in the category: in, f Represents the feature vector calculated by the encoder for the sentence instance; After obtaining the prototype center of each category in the support set, the similarity between the query instance and each category is obtained by measuring the distance between the query instance and the prototype center of each category. The probability distribution of the query instance belonging to each category is calculated by the softmax function, and the query instance is divided into the category closest to the prototype center. In the relational twin network enhanced prototype center module, the specific operation of calculating the similarity score between the query instance and each supporting instance is: After the feature vectors of the two sentences pass through the fully connected layer, they are put into the distance calculation function for similarity calculation, and a value between 0 and 1 is obtained as the similarity score of the two sentences. The calculation formula of the similarity score is: ; Represents the sigmoid activation function, the similarity score between two instances Indicates that the similarity between two sentence vectors is in the range of 0-1 under the action of the activation function; The specific operations for adjusting the calculation method of the prototype center according to the similarity score are as follows: The softmax function is used to reduce the sum of the weights of each instance in the same category to 1. Combined with the attention mechanism, the similarity weights of the support instances in the same category in the support set to the target query instance are used to weight the calculation of the prototype center. The weight calculation formula is: ; Prototype Center Enhanced by Relational Twin Network The calculation formula is: ; The prototype center obtained after weighted processing combines the information of the query instance, enhances the instance information that is more similar to the query instance, and enhances the category information with the same relationship as the query instance in the subsequent classification task; In the cross-fusion module, the prototype center of the fusion semantics is obtained under the influence of the influencing factors. and query instance The calculation formula is: ; in, The function indicates that the mean of the obtained matrix is calculated in the feature direction.
3. The small sample relationship classification system based on metric learning according to claim 2, It is characterized in that The prototype network module adopts an episode-based segmented training method during training. , query instance Belong to relationship The probability is: ; in, d represents the distance calculation function, and N represents the number of all relations in the relation set; During the training process, learning is performed by minimizing the cross entropy loss function: in, Indicates the number of query instances.
4. The small sample relationship classification system based on metric learning according to claim 3, It is characterized in that The specific operation of the episode-based segmented training method is: First, extract an episode from the N relationship categories in the training set. categories, and from the training set Randomly select instances of each category from The sample instances required for episode training constitute the support set, and then from the training set Randomly sample from the remaining instances of the class The query instances required for episode training constitute the query set. For each category in training, the prototype center of the category is calculated by taking the average of the supporting instances. Finally, the loss value is updated by optimizing the distance from the query instance to the center of each category prototype to achieve model training.
