Small-sample relation classification method and system based on multi-scale hierarchical prototype learning
By employing a multi-scale hierarchical prototype learning method, the challenges of data scarcity and semantically similar category differentiation in few-sample relation classification are addressed, achieving higher classification accuracy and generalization ability, and adapting to distribution shifts.
Patent Information
- Application Number
- CN202511130229.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-13
- Publication Date
- 2025-11-28
AI Technical Summary
Existing few-sample relation classification methods are costly and unscalable in constructing large-scale labeled datasets, and they ignore the semantic differences and inter-class similarities between supporting samples, resulting in inaccurate classification, especially when the performance degrades in semantically similar relation categories and distribution offsets.
We employ a multi-scale hierarchical prototype learning approach, which constructs multi-scale hierarchical prototype representations through cross-set collaboration, intra-class collaboration, and inter-class collaboration. We jointly optimize the cross-entropy loss and distribution alignment loss to generate dynamic prototype representations that alleviate distribution bias and enhance classification capabilities.
It significantly improves the model's classification accuracy and generalization performance under conditions with few samples, effectively distinguishes semantically similar relational categories and adapts to tasks with inconsistent distributions, thereby enhancing the model's robustness and generalization ability.
Smart Images

Figure CN121029995A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of natural language processing, in particular to a few-shot relation classification method and system based on multi-scale hierarchical prototype learning. BACKGROUND
[0002] Relation classification is a key task in natural language processing, aiming to identify the semantic relationship between entities from unstructured text. The core goal is to determine whether a given pair of entities in a text sentence has a certain predefined relationship type. Traditional relation classification methods usually rely on a large amount of labeled data for training, which can achieve good performance under sufficient data conditions, but in real-world applications, building large-scale labeled datasets is costly and non-scalable.
[0003] To solve the problem of data scarcity, researchers introduced the concept of few-shot learning, which aims to train a model to learn new classes with only a few labeled samples. This concept has achieved remarkable results in image classification, speech recognition, reinforcement learning, and other fields, and has been gradually introduced into the field of relation classification. In the few-shot relation classification task, the N-way K-shot setting is usually used, i.e., in each round of training or testing, K support samples are selected from N relationships, forming an N×K support set, which is used to simulate the real-world scenario of "new relationship with only a few examples." Then, a number of instances are extracted from the remaining samples in these N classes as a query set to test whether the model can correctly identify the relationship category to which it belongs.
[0004] Current mainstream few-shot relation classification methods are mostly based on the metric learning framework, which aims to learn a prototype representation for each class in the support set and match the query samples with these prototypes. Specifically, first, use pre-trained language models such as BERT to model the context semantics of instance sentences in the support set and query set, obtaining fixed-dimensional vector representations. Second, perform a simple average operation on the representations of each class of support instances to generate a prototype vector for that class, which serves as the semantic center of that class. Finally, calculate the similarity or distance between the query instance and each class prototype, normalize all class scores using the softmax function, and output the final prediction result.
[0005] The metric learning framework method generates a relationship prototype by simply performing an average operation on the support samples, ignoring the semantic differences or complementarity that may exist between the support samples. The prototype constructed in this way has weak expression ability and is easily affected by a single abnormal sample, resulting in inaccurate classification. At the same time, most methods ignore the hierarchical structure characteristics between relationship categories and lack the ability to model the in-class feature collaboration and inter-class similarity difference, and cannot effectively deal with semantically similar relationship categories. In real-world tasks, many relationship categories have high semantic overlap, for example, "place of birth" and "place of residence", and traditional models cannot fully distinguish these relationships, which easily leads to classification errors. Finally, the existing technology lacks a processing mechanism for the distribution difference between the support set and the query set. In the few-shot task, there is often a feature space distribution inconsistency between the support set and the query set, and most existing models do not explicitly process such distribution offset problems, thereby affecting the classification accuracy and generalization ability. SUMMARY
[0006] The purpose of the present application is to provide a few-shot relationship classification method and system based on multi-scale hierarchical prototype learning to solve the problems raised in the background art.
[0007] To achieve the above purpose, the present application provides the following technical solution: a few-shot relationship classification method based on multi-scale hierarchical prototype learning, the method comprising:
[0008] constructing a few-shot relationship classification task to generate a support set and a query set;
[0009] encoding the support set samples, query set samples and relationship description information using a pre-trained language model to generate semantic vector representations;
[0010] constructing a multi-scale hierarchical prototype representation through cross-set collaboration, in-class collaboration and inter-class collaboration;
[0011] calculating the distance between the query samples and the prototypes of each class and outputting the classification probability through softmax;
[0012] jointly optimizing the cross-entropy loss and the distribution alignment loss and iteratively updating the model parameters.
[0013] As a further scheme of the present application, the cross-set collaboration specifically comprises:
[0014] calculating the support set center point feature vector and the query set center point feature vector;
[0015] constructing a support-query mean vector pair and generating an adaptive weight factor based on the support-query mean vector pair through a fully connected layer and a Sigmoid function;
[0016] The query features are corrected to the feature center of the support set based on the adaptive weighting factor, generating a corrected query representation.
[0017] As a further embodiment of the present invention, the feature vector of the support set center point and the feature vector of the query set center point are respectively:
[0018] Where s and q refer to the supporting instances and query instances, respectively, S and Q refer to the supporting set and query set, respectively, and N... S and N Q These refer to the number of supported instances and the number of query instances, respectively. φ It is an encoder function;
[0019] The corrected query is represented as:
[0020] Where λ is the adaptive weighting factor. Sigmoid is an activation function, FC represents a fully connected layer, and q... ori For the original query features, q align This is the revised query representation.
[0021] As a further embodiment of the present invention, the intra-class collaboration specifically includes:
[0022] Calculate the similarity between the query sample and its supporting samples using the Gaussian kernel function:
[0023] Based on similarity, dynamic intra-class prototypes are generated to support weighted aggregation of samples and generate dynamic intra-class prototype representations.
[0024] As a further aspect of the present invention, the similarity calculation formula between the query sample and similar supporting samples is as follows:
[0025]
[0026] in, This represents the j-th supporting instance s in relation class i. ij The above-mentioned revised query instance represents q align The similarity score between them, exp(·) represents the exponential function, ||·|| 2 It is the Euclidean distance metric, and σ is the width of the Gaussian kernel;
[0027] The dynamic prototype within a class is represented as:
[0028] Where f φ Represents the encoder function, s ij This represents the j-th supporting instance in relation class i, and K represents the total number of instances.
[0029] As a further scheme of the present application, the inter-class cooperation specifically includes:
[0030] calculating the similarity distribution of the query sample and each class prototype;
[0031] adjusting the query feature based on the calculated similarity distribution.
[0032] As a further scheme of the present application, the similarity distribution is:
[0033]
[0034] The final query representation is:
[0035] wherein exp(·) represents an exponential function, ||·|| is a Euclidean distance measure, σ is the width of a Gaussian kernel, q is the modified query instance representation, p is the prototype center representation of the relation class i, and N represents the total number of classes. 2 align i
[0036] The present application also provides a few-shot relation classification system based on multi-scale hierarchical prototype learning, which is used to implement the few-shot relation classification method based on multi-scale hierarchical prototype learning, and the system comprises:
[0037] A data set generation module is configured to construct a few-shot relation classification task and generate a support set and a query set.
[0038] An encoding module is configured to encode the support set sample, the query set sample and the relation description information by using a pre-trained language model to generate a semantic vector representation.
[0039] A multi-scale cooperation module is configured to construct a multi-scale hierarchical prototype representation through three levels of cross-set cooperation, intra-class cooperation and inter-class cooperation.
[0040] A classification module is configured to calculate the distance between the query sample and each class prototype and output a classification probability through softmax.
[0041] A loss optimization module is configured to jointly optimize the cross-entropy loss and the distribution alignment loss and iteratively update the model parameters.
[0042] Compared with the prior art, the beneficial effects of the present application are: by introducing intra-class collaborative modeling mechanism, the contribution of each support sample to the prototype construction is dynamically adjustable, the accuracy of class center representation is improved; the ability to distinguish semantic similarity relationship is enhanced, by introducing inter-class interaction modeling and relationship description information, the recognition ability of the model to fine-grained relationship is improved. The cross-set feature alignment ability is improved, by introducing the distribution alignment module between the support set and the query set, the spatial offset between the prototype and the sample to be classified is significantly reduced; the overall classification precision and the model generalization performance are improved, by the multi-module joint modeling and optimization strategy, the model is superior to the existing advanced technical solutions under various settings of the standard few-shot relationship classification task. BRIEF DESCRIPTION OF DRAWINGS
[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiments or prior art description will be briefly introduced as follows. Obviously, the drawings in the following description are only some embodiments of the present application.
[0044] Figure 1 The flow chart of the few-shot relationship classification method based on multi-scale hierarchical prototype learning provided by the embodiments of the present application.
[0045] Figure 2 The architecture schematic diagram of the few-shot relationship classification method based on multi-scale hierarchical prototype learning provided by the embodiments of the present application.
[0046] Figure 3 The distribution comparison diagram of the semantic similar relationship classes in the embedding space provided by the embodiments of the present application. DETAILED DESCRIPTION
[0047] In order to make the technical problems to be solved by the present application, the technical solutions and beneficial effects more clearly, the present application will be further described in detail below in combination with the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application, and are not used to limit the present application.
[0048] As shown in Figure 1 and Figure 2 In the embodiments of the present application, the few-shot relationship classification method based on multi-scale hierarchical prototype learning, the method comprises steps S100 to S500:
[0049] Step S100, constructing a few-shot relationship classification task, generating a support set and a query set;
[0050] Step S200, encoding the support set samples, query set samples and relationship description information using a pre-trained language model to generate semantic vector representation;
[0051] Step S300, constructing multi-scale hierarchical prototype representation through three levels of cross-set cooperation, intra-class cooperation and inter-class cooperation;
[0052] Step S400, calculating the distance between the query sample and the prototype of each class, and outputting the classification probability through softmax;
[0053] Step S500, jointly optimizing the cross-entropy loss and the distribution alignment loss, and iteratively updating the model parameters.
[0054] In the embodiment, an N-way K-shot few-shot relation classification task is constructed, and a support set S and a query set Q are generated. Each sentence in the support set and the query set, as well as the relationship name and description information, is input into a BERT encoder for context modeling. The prototype representation is constructed in multi-scale hierarchy through three levels of cross-set interaction, intra-class interaction and inter-class interaction. The distance between each query sample and all class prototypes is calculated, and the classification probability is converted through a softmax function. Two types of loss functions, cross-entropy loss and distribution alignment loss, are constructed for joint optimization. The parameters are optimized through back propagation until convergence.
[0055] Specifically, the N-way representation task contains N different relationship categories, and K-shot means that K few labeled samples are provided for each relationship category during training. For example, in a 5-way-1-shot setting, the model needs to classify new query samples in the query set according to only one known sample in the support set S. The pre-trained language model is used to encode the support set samples, query set samples and relationship description information to generate semantic vector representation. In the representation step, the sentence instances in the support set, the query samples and the relationship description information are uniformly encoded into high-dimensional semantic vectors as the basis for subsequent representation and classification. For each sentence sample, the special symbols [eh] and [et] are used to mark the start and end positions of the "head entity" and "tail entity" in the sentence. The entity-labeled sentence is input into the BERT encoder to extract the hidden state representation of the [eh] and [et] corresponding positions in the sentence, and then the sentence representation vector is obtained by concatenation. The encoding result will be used to construct the prototype, query feature alignment and subsequent similarity calculation.
[0056] As a preferred embodiment of the present application, the cross-set cooperation specifically includes:
[0057] The support set center point feature vector and the query set center point feature vector are calculated.
[0058] The support-query mean vector pair is constructed, and the adaptive weight factor is generated based on the support-query mean vector pair through a fully connected layer and a Sigmoid function.
[0059] The query feature is corrected based on an adaptive weight factor to the support set feature center to generate a corrected query representation.
[0060] In the embodiment, cross-set coordination is used to alleviate the distribution inconsistency problem between the support set and the query set, and to improve the model generalization capability. First, the center point feature vector of the support set and the center point feature vector of the query set
[0061]
[0062] where s and q respectively indicate each support instance and query instance, S and Q respectively indicate the support set and the query set, N S and N Q respectively indicate the number of support instances and query instances, f φ is an encoder function;
[0063] Then, a support-query mean vector pair is constructed, input into a lightweight fully connected network, and an adaptive weight factor λ is output, where, Sigmoid is an activation function, an activation function is a function of introducing a nonlinear transformation in a neural network, used to help the model learn the mapping relationship between complex input and output, so that the network can fit more complex functions and expression ability, and FC represents a fully connected layer. Finally, the original query feature q ori is corrected to the support set feature center to obtain a corrected query representation q align , which is aligned in the feature space, which is beneficial to subsequent prototype matching. The corrected query representation is:
[0064] As a preferred embodiment of the present application, the intra-class coordination specifically includes:
[0065] The similarity between the query sample and the support sample of the same class is calculated using a Gaussian kernel function:
[0066] Based on the similarity, a dynamic intra-class prototype is generated to weight aggregate the support samples, and an intra-class dynamic prototype representation is generated.
[0067] In the embodiment, intra-class coordination is used to process the information coordination problem between multiple support samples in each class, and to enhance the detail representation capability of the prototype. First, for each query sample, the similarity between the query sample and the support sample of the same class is calculated using a Gaussian kernel function:
[0068]
[0069] where, indicates the jth support instance s ij in the relationship class i and the above corrected query instance representation qalign the similarity score between the two, exp(·) denotes the exponential function, and ||·|| is the Euclidean distance metric, and σ is the width of the Gaussian kernel. 2
[0070] Then the support samples are aggregated according to the similarity, and the intra-class dynamic prototype representation is generated:
[0071]
[0072] where f φ represents the encoder function, s ij represents the jth support instance in the i-th relation class, and K represents the total number of instances.
[0073] As a preferred embodiment of the present application, the inter-class cooperation specifically includes:
[0074] The similarity distribution between the query sample and each class prototype is calculated.
[0075] Based on the calculated similarity distribution, the query feature is adjusted.
[0076] In the present embodiment, the inter-class cooperation is used to enhance the discrimination between different classes and prevent the model from confusing the relation categories with similar semantics. First, the similarity distribution between each class prototype and the current query sample is calculated as:
[0077]
[0078] where exp(·) denotes the exponential function, and ||·|| is the Euclidean distance metric, and σ is the width of the Gaussian kernel. 2 align is the above modified query instance representation, p i is the prototype center representation of the i-th relation category, and N represents the total number of categories.
[0079] Then, based on the obtained similarity distribution, the query feature is adjusted once supervised, guided to be close to the correct class prototype and far away from other class prototypes, so as to improve the discrimination of the model, and the final query representation is:
[0080]
[0081] where q align is the above modified query instance representation, p i is the prototype center representation of the i-th relation category, and N represents the total number of categories.
[0082] A global loss term is designed to optimize the overall feature distribution of the support set and the query set, so that the distribution of the two in the embedding space is consistent, enhancing the migration ability and generalization of the model. First, for each query sample, the distance between it and all class prototypes is calculated in turn, and the distance is converted into a similarity score, which is converted into a probability distribution through a softmax layer:
[0083]
[0084] Where q m is the mth query instance, p i is the prototype center representation of the relationship class i.
[0085] Then the L2 distance is constructed as an alignment loss term and added to the total loss function:
[0086]
[0087] Where N s and N Q represent the number of instances in the support set and the query set respectively, s i and q m are the ith support instance and the mth query instance respectively.
[0088] Complete the final classification task and assign the query sample to the most likely relationship class.
[0089] Figure 3 The visualization results of 100 instances in each of the two semantically similar relationship classes in the embedding space are shown, and each color represents a relationship class. The left figure is the embedding distribution without using the multi-scale hierarchical prototype learning method of the present application. It can be clearly observed that there is a large amount of overlap and mixing of the two classes of instance points in the space, the class boundary is blurred, the intra-class distribution is discrete, indicating that the traditional method is difficult to effectively distinguish such semantically similar relationships. The right figure is the embedding distribution result after introducing the method of the present application. It can be seen that the two relationship classes form a clear clustering structure in the embedding space, the intra-class samples are more compact, the inter-class boundary is significantly clear, significantly enhancing the semantic discriminability and consistency. The comparison results verify the effectiveness and advantages of the present application in modeling semantically similar relationship classes.
[0090] The present application proposes a structural mechanism that extends the prototype representation to three levels of cross-set, intra-class and inter-class, breaking through the limitation of the existing prototype method that only relies on simple average to construct the prototype center. Through multi-level cooperative interaction between instances, the representation ability and class discrimination ability of the prototype are effectively enhanced.
[0091] In order to alleviate the feature distribution deviation problem between the support set and the query set under the condition of small samples, the cross-set collaborative alignment method is introduced, the difference between the two center points is calculated, and the query sample features are adaptively adjusted, so that the classification boundary is improved, and the robustness of the model in cross-task generalization is significantly improved.
[0092] Compared with the prior art, the classification accuracy in the small sample relation classification task is significantly improved, and through the hierarchical collaborative mechanism of cross-set, intra-class and inter-class, the problem of inaccurate prototype representation of the traditional prototype network under the condition of small samples can be effectively alleviated. Under multiple standard experimental settings (5-way / 10-way, 1-shot / 5-shot) of FewRel 1.0 and FewRel 2.0 data sets, the classification accuracy is significantly better than the existing advanced method, and the highest classification accuracy is improved by 4.29%. Especially in the most challenging 1-shot scene, the method shows superior generalization ability and stability. At the same time, the method improves the ability of the model to distinguish between semantically similar relation classes, introduces relation description information in inter-class interaction modeling, and combines a dynamic measurement mechanism, so that the model can simulate the understanding process of humans for new relation classes, thereby achieving clearer division between relations with similar semantics but different pragmatic functions. Visualization analysis shows that after using the method, the boundaries between different classes are more clear, and the intra-class samples are more compact. Finally, the method enhances the adaptability to inconsistent distribution of training samples, introduces a cross-set alignment module and a distribution alignment classifier, which can dynamically adjust the embedding space when the distribution difference between the support set and the query set is large, so that the query sample is more closely aligned with the corresponding class prototype in the feature space. This mechanism significantly improves the robustness and generalization ability of the model in tasks with large inter-class distribution differences.
[0093] The application also provides a few-shot relation classification system based on multi-scale hierarchical prototype learning, which is used to implement the few-shot relation classification method based on multi-scale hierarchical prototype learning.
[0094] The data set generation module is used to construct a few-shot relation classification task and generate a support set and a query set.
[0095] The encoding module is used to encode the support set samples, the query set samples and the relation description information by using a pre-trained language model to generate semantic vector representations.
[0096] The multi-scale collaborative module is used to construct multi-scale hierarchical prototype representations through three levels of cross-set collaboration, intra-class collaboration and inter-class collaboration.
[0097] The classification module is used to calculate the distance between the query sample and each class prototype, and output the classification probability through softmax.
[0098] The loss optimization module is configured to jointly optimize a cross-entropy loss and a distribution alignment loss, and iteratively update the model parameters.
[0099] The above merely describes the preferred embodiments of the present application and is not intended to limit the present application. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A few-shot relation classification method based on multi-scale hierarchical prototype learning, characterized in that, The method includes: Construct a few-sample relation classification task to generate a support set and a query set; The support set samples, query set samples, and relation description information are encoded using a pre-trained language model to generate semantic vector representations; A multi-scale hierarchical prototype representation is constructed by means of three levels: cross-set collaboration, intra-class collaboration, and inter-class collaboration. Calculate the distance between the query sample and each type of prototype, and output the classification probability using softmax. The model parameters are updated iteratively by jointly optimizing the cross-entropy loss and distribution alignment loss.
2. The few-shot relation classification method based on multi-scale hierarchical prototype learning according to claim 1, characterized in that, The cross-set collaboration specifically includes: Calculate the feature vectors of the support set centroids and the query set centroids; Construct support-query mean vector pairs, and generate adaptive weight factors based on support-query mean vector pairs through a fully connected layer and a Sigmoid function; The query features are corrected to the feature center of the support set based on the adaptive weighting factor, generating a corrected query representation.
3. The few-shot relation classification method based on multi-scale hierarchical prototype learning according to claim 2, characterized in that, The support set centroid feature vector and the query set centroid feature vector are respectively: Where s and q refer to the supporting instances and query instances, respectively, S and Q refer to the supporting set and query set, respectively, and N... S and N Q These refer to the number of supported instances and the number of query instances, respectively. φ It is an encoder function; The corrected query is represented as: Where λ is the adaptive weighting factor. Sigmoid is an activation function, FC represents a fully connected layer, and q... ori For the original query features, q align This is the revised query representation.
4. The few-shot relation classification method based on multi-scale hierarchical prototype learning according to claim 1, characterized in that, The intra-class collaboration specifically includes: Calculate the similarity between the query sample and its supporting samples using the Gaussian kernel function: Based on similarity, dynamic intra-class prototypes are generated to support weighted aggregation of samples and generate dynamic intra-class prototype representations.
5. The few-shot relation classification method based on multi-scale hierarchical prototype learning according to claim 4, characterized in that, The formula for calculating the similarity between the query sample and supporting samples of the same type is: in, This represents the j-th supporting instance s in relation class i. ij The above-mentioned revised query instance represents q align The similarity score between them, exp(·) represents the exponential function, ||·|| 2 It is the Euclidean distance metric, and σ is the width of the Gaussian kernel; The dynamic prototype within a class is represented as: Where f φ Represents the encoder function, s ij This represents the j-th supporting instance in relation class i, and K represents the total number of instances.
6. The few-shot relation classification method based on multi-scale hierarchical prototype learning according to claim 1, characterized in that, The inter-class collaboration specifically includes: Calculate the similarity distribution between the query sample and various prototypes; The query features are adjusted based on the calculated similarity distribution.
7. The few-shot relation classification method based on multi-scale hierarchical prototype learning according to claim 6, characterized in that, The similarity distribution is as follows: The final query is represented as: Where exp(·) represents the exponential function, ||·|| 2 It is the Euclidean distance metric, σ is the width of the Gaussian kernel, and q align This is a modified query instance representation, p i It is the prototype center representation of relation category i, and N represents the total number of categories.
8. A few-shot relation classification system based on multi-scale hierarchical prototype learning, used to implement the few-shot relation classification method based on multi-scale hierarchical prototype learning as described in any one of claims 1-7, characterized in that, The system includes: The dataset generation module is used to construct a few-sample relation classification task and generate a support set and a query set; The encoding module is used to encode support set samples, query set samples, and relation description information using a pre-trained language model to generate semantic vector representations; The multi-scale collaboration module is used to construct a multi-scale hierarchical prototype representation through three levels: cross-set collaboration, intra-class collaboration, and inter-class collaboration. The classification module is used to calculate the distance between the query sample and each type of prototype, and outputs the classification probability through softmax. The loss optimization module is used to jointly optimize the cross-entropy loss and distribution alignment loss, and iteratively update the model parameters.