Joint entity relation extraction method of multi-scale dilated convolution and prototype contrast learning
Patent Information
- Application Number
- CN202611098288.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-23
- Publication Date
- 2026-10-30
AI Technical Summary
[0005]本发明的目的是提供多尺度扩张卷积与原型对比学习的联合实体关系抽取方法,解决了现有抽取方法存在的抽取精度低的问题
(1)多尺度上下文建模能力显著提升,兼顾局部精细特征与全局长距离语义,有效应对舆情长文本的抽取需求: BERT自注意力机制能够建立token间的全局依赖,为捕获舆情文本中人物与其所属组织、活动地点之间往往相隔数十个词甚至跨越多句话的长距离语义关联提供了基础;但自注意力在局部n-gram、短语级模式的刻画粒度上相对粗糙,容易弱化局部关键片段的表征强度。为此,本发明引入3组膨胀率逐层递增(d=1、2、4)的串行扩张卷积结构,以线性复杂度由局部词级(3个token)逐步扩展至短语级(7个token)、句级(15个token)感受野,对BERT输出的全局表示进行局部特征的精细化增强,弥补自注意力在局部模式提取上的不足。通过全局自注意力与局部多尺度卷积的协同建模,模型既能保留对长距离实体关联的捕获能力,又能强化局部边界与短语级特征的辨识度,从而提升复杂舆情文本中实体识别和关系抽取的完整性与准确性。
Smart Images

Figure CN123012760A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of information extraction technology in natural language processing, and relates to a joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning. Background Technology
[0002] Information extraction is one of the core tasks in the field of natural language processing. Its goal is to identify entities and semantic relationships between entities from unstructured text, and to construct structured triples (head entity, relation, tail entity) to provide basic support for downstream applications such as knowledge graph construction, intelligent question answering, and text mining. Information extraction is usually decomposed into two sub-tasks: Named Entity Recognition (NER) and Relation Extraction (RE).
[0003] With the rapid development of the internet, online public opinion information has experienced explosive growth. In terms of social governance and public opinion monitoring, the accurate identification of key entities (such as individuals, organizations, and locations) and their relationships within public opinion events is crucial for understanding the evolution of public opinion and maintaining social stability. If the accuracy of entity identification and relationship extraction declines, the reliability of public opinion early warning and situation analysis will be significantly compromised. However, real public opinion texts often exhibit diverse entity types, complex semantic relationships, and significant long-distance dependencies, placing extremely high demands on the accuracy of information extraction.
[0004] Traditional pipelined methods process named entity recognition and relation extraction as two independent, sequential stages: first, entities are identified, and then relations are extracted based on the recognition results. This approach suffers from two core flaws: first, error propagation—errors in upstream entity recognition can be amplified and cascaded down to downstream relation extraction; second, task fragmentation—the semantic dependencies between the two subtasks are ignored, the entity recognition stage cannot utilize relational information to aid judgment, and the relation extraction stage cannot reverse-correct entity boundaries, resulting in limited overall extraction accuracy in complex public opinion texts. Summary of the Invention
[0005] The purpose of this invention is to provide a joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning, which solves the problem of low extraction accuracy in existing extraction methods.
[0006] The technical solution adopted in this invention is a joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning, which specifically includes the following steps: Step 1: Obtain the joint entity relationship extraction dataset, preprocess the dataset, and divide it into training set, validation set, and test set; Step 2: Input the text sequence into the pre-trained language model BERT; Step 3: Input the hidden state sequence output by BERT into the multi-scale dilated convolution module to output enhanced representation features; Step 4: Extract the fused representation of the candidate entity Span; Step 5: Perform named entity recognition; Step 6: Construct a relation representation and input it into a relation classifier for relation type prediction; Step 7: Compare the feature space distribution of the loss constraint relationship using the prototype.
[0007] The invention is further characterized by: The specific process of step 1 is as follows: Read the JSON format annotation file, enumerate all candidate Spans with a length not exceeding 10 for each sentence, construct the entity annotation matrix and relation annotation matrix, randomly sample negative entity Spans and negative relation pairs, generate batch-encoded sequences, mask matrices and annotation vectors, and divide the data into training set, validation set and test set.
[0008] The specific process of step 2 is as follows: The input sentence is segmented into subwords using the BERT WordPiece tokenizer. [CLS] and [SEP] tags are added to the beginning and end of the sequence, respectively. The token ID sequence is then input into the BERT-base-cased pre-trained model. The BERT model contains 12 layers of bidirectional Transformer encoders, and the output of the last layer is used as the initial token-level context representation. L is the sequence length, and the output vector at the [CLS] position contains the preliminary global semantics of the entire sentence.
[0009] The specific process of step 3 is as follows: Step 3.1, Save the residual branch: Perform a linear transformation on the input H. Preserve original semantic information for residual connections; Step 3.2: Transpose H into a convolutional format (Batch, 768, L), and process it by dilating the convolutional layer to make the length of the output sequence consistent with the input. Step 3.3: After each group of convolutional operations, ReLU activation and LayerNorm normalization are performed sequentially. Step 3.4: After the last set of convolutional outputs, transpose H to (Batch, L, 768) format, add it to the residual branch H_res, and then perform final LayerNorm normalization to obtain the enhanced token representation. .
[0010] The specific process of step 4 is as follows: Step 4.1: For the entity mask matrix of the candidate Span, after applying a minimum value to the positions where the mask is 0, take the Max-pool to mask irrelevant token features outside the Span, thus obtaining the semantic representation inside the Span. ; Step 4.2: Locate the start token position start_idx and end token position end_idx of the Span from the entity mask using position indexing operations, and extract the corresponding token hidden states h_start and h_end from H'; the start token usually contains clues about the entity's type, while the end token marks the right boundary of the entity; Step 4.3: Project h_start and h_end through independent linear layers respectively: (1) Step 4.4: Using the concatenation of span_pool, z_start, and z_end as input, calculate the adaptive weights through the gating layer: (2) Step 4.5: Weave the two semantic representations by gating weights, and output the result after LayerNorm normalization: (3) Here, ⊙ represents element-wise multiplication.
[0011] The specific process of step 5 is as follows: Step 5.1: Extract the hidden state at position 0 in H' corresponding to the [CLS] token as the global context representation. ; Step 5.2: Query the length embedding table based on the Span length to obtain... ; Step 5.3: Concatenate the features output from Steps 4, 5.1, and 5.2: (4) Step 5.4: After Dropout (rate=0.1) regularization, input the entity classification linear layer (Linear, 1561→|entity_types|). During training, calculate the cross-entropy loss L_entity. During inference, use Softmax to calculate the class probability and take argmax to output the predicted entity type. Spans of type None (index=0) are considered negative examples.
[0012] The specific process of step 6 is as follows: Step 6.1: During inference, enumerate all ordered entity pairs from the predicted non-None entities; during training, use the labeled positive relation pairs and the negative relation pairs sampled in step 1 to enable the model to learn to recognize unrelated None. Step 6.2: Extract the boundary-aware fusion representations (fused) generated in Step 4 for the two types of entities and concatenate them to obtain the entity pair representations. ; Step 6.3: For the context token region between the two entities, obtain the relational context representation through a masked Max-pool. For adjacent or overlapping nested entity pairs that result in empty context regions without intermediate words, the values are directly set to zero. Step 6.4: Embed the lengths of the two entities to obtain... ; Step 6.5: Concatenate rel_ctx, entity_pairs, and size_pair to form a relational representation. ; Step 6.6: After applying Dropout to rel_repr, input the relation classifier. During training, use BCEWithLogitsLoss to calculate the multi-label classification loss L_relation. During inference, compare the Sigmoid probability with the threshold to determine the predicted relation type.
[0013] The specific process of step 7 is as follows: Step 7.1: Project the relational representation rel_repr onto a D=128-dimensional low-dimensional space using a linear projection layer, followed by ReLU activation and L2 normalization, to obtain... : (5) Step 7.2: Perform L2 normalization on the prototype matrix; Step 7.3: For each positive relation type cls_id within the batch, where index≥1, filter the corresponding positive samples, calculate the cosine similarity of its corresponding type's K sub-prototypes, and take the maximum value: (6) Step 7.4: For each negative example relation type `other_cls` within the same batch, calculate the cosine similarity between the positive example sample and the prototype of the negative example type. To adapt to multi-label scenarios, if the current sample has other positive example relations besides `cls_id`, then the sample is masked out of the `other_cls` set, and then the global maximum value among all valid negative classes is taken. (7) Wherein, s_neg is the maximum cosine similarity between the sample and all valid negative example relation type prototypes, used for hard negative example mining; Step 7.5, calculate the Margin contrastive loss and take the mean: (8) Step 7.6: Perform a weighted summation of the three loss functions and backpropagate to update all parameters: (9) Among them, L_entity is responsible for the accuracy of entity classification, L_relation is responsible for the accuracy of relation classification, and L_PCL is responsible for the inter-class separability of the relation feature space. Step 7.7: Employ a differentiated learning rate strategy, combined with the AdamW optimizer and linear learning rate preheating for end-to-end training.
[0014] The beneficial effects of this invention are as follows: (1) The multi-scale context modeling capability is significantly improved, taking into account both local fine features and global long-distance semantics, effectively addressing the extraction needs of long texts of public opinion: The BERT self-attention mechanism can establish global dependencies between tokens, providing a foundation for capturing long-distance semantic relationships between people and their organizations and activity locations in public opinion texts, which are often separated by dozens of words or even span multiple sentences; however, self-attention is relatively coarse in its characterization of local n-gram and phrase-level patterns, which can easily weaken the representation strength of local key segments. To this end, this invention introduces three sets of serially dilated convolutional structures with progressively increasing dilation rates (d=1, 2, 4), which gradually expand the receptive field from local word level (3 tokens) to phrase level (7 tokens) and sentence level (15 tokens) with linear complexity, thereby enhancing the local features of the global representation output by BERT and compensating for the shortcomings of self-attention in local pattern extraction. By using global self-attention and local multi-scale convolution for collaborative modeling, the model can retain the ability to capture long-distance entity associations while enhancing the recognition of local boundaries and phrase-level features, thereby improving the completeness and accuracy of entity recognition and relationship extraction in complex public opinion texts.
[0015] (2) Accurate capture of entity boundary semantics to improve the recognition accuracy of complex public opinion entities: The boundary-aware gating representation module independently models the semantic information of the beginning and end boundaries of entities and dynamically integrates boundary information and internal information with an adaptive gating mechanism. This enables the model to accurately locate entity boundaries when processing long entities commonly found in public opinion texts (such as "XX City Network Public Opinion Emergency Response Leading Group Office" and other multi-word compound names), avoiding truncation or overflow caused by boundary ambiguity.
[0016] (3) The ability to distinguish relationship features is greatly enhanced, ensuring the accuracy of public opinion relationship analysis: The prototype comparison learning module maintains multiple learnable sub-prototype vectors for each relationship type, and uses Margin Loss to constrain the clustering of similar relationship features and the separation of dissimilar relationship features. In the scenario of public opinion figure relationship analysis, different relationship types (such as employment relationship, cooperation relationship, and kinship relationship) may have similar expressions in semantics. This module effectively reduces the confusion rate between similar relationships by explicitly optimizing the inter-class distribution of the feature space, thus ensuring the accuracy of public opinion analysis.
[0017] (4) Module collaborative design and end-to-end training facilitate the practical engineering deployment of public opinion systems: The three innovative modules and the BERT backbone network form a unified end-to-end joint training framework, eliminating the need for additional pre-training stages or complex multi-step training processes. The model can be directly embedded into the text analysis pipeline of existing public opinion monitoring systems to complete entity extraction and relationship construction from the input text, forming a structured public opinion knowledge graph with good engineering integrability and practical application value. Attached Figure Description
[0018] Figure 1 This is the overall framework diagram of the joint entity relation extraction method of multi-scale dilated convolution and prototype contrast learning of the present invention. Figure 2 This is a structural diagram of the multi-scale dilated convolution module (Dilated Context Block) in the joint entity relation extraction method of multi-scale dilated convolution and prototype contrast learning of the present invention; Figure 3 This is a structural diagram of the Boundary-Aware Representation module in the joint entity relationship extraction method of multi-scale dilated convolution and prototype contrastive learning of this invention. Figure 4 This is a structural diagram of the Prototype Contrastive Loss module in the joint entity relation extraction method of multi-scale dilated convolution and prototype contrastive learning of the present invention. Figure 5 This is a flowchart of the forward computation of the overall model of the joint entity relation extraction method of multi-scale dilated convolution and prototype contrast learning of this invention. Detailed Implementation
[0019] The following detailed description is provided in conjunction with specific implementation methods.
[0020] This invention provides a joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning, such as... Figure 1 As shown, the specific steps include the following: Step 1: Obtain the joint entity relation extraction dataset, preprocess the dataset, enumerate candidate entities Span and construct negative samples, and divide the dataset into training set, validation set and test set. Specifically, this invention is experimentally verified on publicly available benchmark datasets, primarily targeting general news and public opinion scenarios: CoNLL04 contains 4 entity types (Loc, Peop, Org, Other) and 5 relation types (Live, Work, OrgBI, Kill, LocIn), with a total of 1441 sentences. Its person-organization-location relation structure is highly consistent with the public opinion monitoring scenario, making it a core dataset for evaluating the ability to extract public opinion entity relations.
[0021] The data preprocessing workflow is as follows: Read the JSON-formatted annotation file; enumerate all candidate spans with a length not exceeding 10 for each sentence; construct entity annotation matrices and relation annotation matrices; randomly sample negative entity spans (100 negative examples) and negative relation pairs (100 negative examples); generate batch-encoded sequences, mask matrices, and annotation vectors; divide the data according to training set / validation set / test set; and generate standardized batch inputs. In public opinion scenarios, text lengths vary greatly, and entity densities differ; therefore, the design of the negative sampling strategy needs to balance coverage and computational efficiency.
[0022] Step 2: Input the text sequence into the pre-trained language model BERT to obtain the token-level contextual hidden state representation H∈R^(L×768): The input sentence is segmented into subwords using the BERT WordPiece tokenizer. [CLS] and [SEP] tags are added to the beginning and end of the sequence, respectively. The token ID sequence is then input into the BERT-base-cased pre-trained model. The BERT model contains a 12-layer bidirectional Transformer encoder, with each layer having a hidden state dimension of 768. The calculation formula for the self-attention mechanism is as follows: (1) Where Q, K, and V are the query, key, and value matrices, respectively, dk=64 is the dimension of the key vector, and softmax is the normalized exponential function. Formula (1) can explain the basic computational principle of generating token-level context representations within the BERT encoder, providing a theoretical basis for obtaining the preliminary representation H in subsequent steps of this invention. Based on this, this invention further enhances local features through a multi-scale dilated convolution module. The output of the last layer is taken as the preliminary token-level context representation H∈R^(L×768) (L is the sequence length). The output vector corresponding to the [CLS] position contains the preliminary global semantics of the entire sentence. This hidden state matrix H is passed as input to the subsequent multi-scale dilated convolution module for feature enhancement.
[0023] Step 3: Input the hidden state sequence output by BERT into the multi-scale dilated convolution module (Dilated ContextBlock), and perform multi-scale context enhancement on the token representation through three sets of serial convolutions with different dilation rates, and output the enhanced representation H'∈R^(L×768); like Figure 2 As shown in the scenario of public opinion monitoring, a single news item may simultaneously describe the complex relationships between multiple individuals and organizations, and the semantic connections between entities often span long text distances. While BERT's self-attention mechanism can model global dependencies, its granularity in capturing local n-gram patterns is not fine enough. The expanded convolutional module compensates for this deficiency with a progressively larger receptive field, as follows: Step 3.1, Save the residual branch: Perform a linear transformation on the input H. The original semantic information is preserved for residual connections, where H_res is the residual branch representation obtained after the linear transformation, and W_res is the learnable weight matrix corresponding to the linear transformation. The necessity of introducing a linear projection layer is that the original output H of BERT mainly contains the general semantics of the pre-training stage, while the enhanced semantics extracted by the multi-scale dilated convolution are for specific extraction tasks. The linear transformation can adaptively map the original feature space, making it better aligned with the task feature space output by the deep convolution. At the same time, the residual design ensures that even if the convolutional layer fails to extract effective incremental features, the model at least retains the original quality of the BERT output. Step 3.2: Transpose H into a convolutional format (Batch, 768, L), and pass it through 3 sets of dilated convolutional layers in sequence. The dilation rate di∈{1, 2, 4} of the i-th convolutional layer, the kernel size k=3, and the padding=di×(k-1) / 2 to ensure that the length of the output sequence is consistent with that of the input. Step 3.3: ReLU activation and LayerNorm normalization are performed sequentially after each group of convolutional operations. The three groups of convolutions adopt a serial structure (the output of the previous group is used as the input of the next group), so that the receptive field expands layer by layer: layer d=1 covers 3 tokens (local word level), layer d=2 expands the receptive field to 7 tokens (phrase level) based on layer d=1, and layer d=4 further expands the receptive field to 15 tokens (sentence level), forming a multi-scale feature pyramid that is progressively larger. Step 3.4: After the output of the third convolutional group, it is transposed to (Batch, L, 768) format, added to the residual branch H_res, and then normalized by the final LayerNorm to obtain the enhanced token representation H'∈R^(L×768). The receptive fields of the three sets of sequential convolutions are 3, 7 (equivalent), and 15 (equivalent) tokens, respectively, to progressively achieve multi-scale context modeling from local word level to global sentence level, enabling the model to effectively capture cross-sentence entity relationships in public opinion texts. The module structure is as follows: Figure 2 As shown.
[0024] Step 4: Based on the enhanced token representation H', the fusion representation of the candidate entity Span is extracted using the boundary-aware gating representation module. The Max-pool semantics inside the Span and the start / end boundary token semantics are adaptively fused through the gating mechanism. like Figure 3 As shown, the Boundary-Aware Representation module constructs a refined representation for each candidate entity Span that integrates internal and boundary semantics. In public opinion texts, entities such as organization names and location descriptions often consist of multiple words. The difficulty in identifying these entities lies not in determining their type, but in accurately locating the beginning and end boundaries of the entity. Traditional max-pooling operations treat all tokens within a Span equally, losing the unique indicative information of boundary tokens. This module is designed to address this deficiency.
[0025] Step 4.1: For the entity mask matrix of the candidate Span, after applying a minimum value to the positions where the mask is 0 (i.e., the position outside the Span), take the Max-pool to mask the irrelevant token features outside the Span, and obtain the semantic representation inside the Span span_pool∈R^768. This vector summarizes the overall semantics of the entity. Step 4.2: Locate the start token position start_idx and end token position end_idx of the Span from the entity mask using position indexing operations, and extract the corresponding token hidden states h_start and h_end from H'. The start token usually contains clues about the entity's type, while the end token marks the right boundary of the entity; Step 4.3: Project h_start and h_end through independent linear layers respectively: (2) Where z_start and z_end are the boundary feature vectors obtained by linear projection of the hidden states h_start and h_end of the starting and ending tokens, respectively, and Ws and We are the learnable projection weight matrices corresponding to the starting and ending positions, respectively. Independent projection matrices Ws and We are used for the start and end boundaries because the starting position is biased towards type indication and the ending position is biased towards boundary marker. Independent parameters enable the model to learn these two different semantic mappings. Step 4.4: Using the concatenation of span_pool, z_start, and z_end as input, calculate the adaptive weights through the gating layer: (3) Where g is the adaptive weight vector output by the gating layer, Wg and bg are the learnable weight matrix and bias vector of the gating layer, respectively, and σ is the sigmoid activation function. This weight is determined by both the internal semantics of the Span and the semantics of its beginning and end boundaries, realizing a data-driven adaptive fusion strategy. Step 4.5: Weave the two semantic representations by gating weights, and output the result after LayerNorm normalization: (4) Where ⊙ represents element-wise multiplication, and fused is the boundary-aware fusion representation of the final output of the candidate Span, which is the result obtained by weighted combination of the gating weight g with semantic components such as span_pool, z_start, and z_end, followed by LayerNorm normalization. Compared with the traditional single Max-pooling representation, this module enables the model to dynamically adjust the contribution ratio of internal semantics and boundary semantics according to the specific situation of the entity, significantly improving the recognition accuracy of long entities commonly found in public opinion texts. The module structure is as follows: Figure 3 As shown.
[0026] Step 5: Use the concatenation of [CLS] global representation, boundary-aware fusion representation and Span length embedding as input to the entity classifier to complete named entity recognition; Step 5.1: Extract the hidden state at position 0 in H' corresponding to the [CLS] token as the global context representation entity_ctx∈R^768. This vector encodes the semantic information of the entire sentence and provides contextual reference for entity classification. Step 5.2: Query the length embedding table based on the Span length to obtain size_emb∈R^25. The design motivation for length embedding is that in public opinion texts, short entities (such as abbreviations) and long entities (such as full organization names) have systematic differences in semantic roles, and embedding allows the model to establish prior knowledge of this. Step 5.3, splicing the three features: (5) Among them: entity_ctx provides the full sentence context, fused describes the semantics of the entity itself, and size_emb provides morphological priors; Step 5.4: After Dropout (rate=0.1) regularization, input the entity classification linear layer (Linear, 1561→|entity_types|). During training, calculate the cross-entropy loss L_entity. During inference, use Softmax to calculate the class probability and take argmax to output the predicted entity type. Spans of type None (index=0) are considered negative examples.
[0027] Step 6: Based on the boundary-aware fusion representation of the entity span, the entity pair context max-pool representation, and the length embedding pairs, construct the relation representation and input it into the relation classifier for relation type prediction; specifically, it includes the following steps: Step 6.1: During inference, all ordered entity pairs are enumerated from the predicted non-None entities. During training, labeled positive relation pairs and negative relation pairs sampled in Step 1 are used to ensure that the model can learn to recognize "None". To avoid memory overflow, relation candidates are processed in batches, with a maximum of 100 pairs per batch. Ordered enumeration ensures the directionality of directed relations. In scenarios with high entity density in public opinion texts, batch processing ensures computational feasibility without changing the model output. Step 6.2: Extract the boundary-aware fusion representations fused generated in step 4 for the two entities and concatenate them to obtain the entity pair representation entity_pairs∈R^1536; Step 6.3: For the context token region between two entities (excluding the entities themselves), obtain the relation context representation rel_ctx∈R^768 through masked Max-pooling. For empty context regions with no intermediate words due to adjacent or overlapping nested entity pairs, directly set them to zero. The context between entities is the core source of clues for relation judgment. In public opinion texts, there are often words that directly express the relationship between two entities. Extracting this part of the information separately allows the model to focus its attention on the most critical relation trigger words. Step 6.4: Embed the lengths of the two entities to obtain... ; Step 6.5: Concatenate rel_ctx, entity_pairs, and size_pair to form a relational representation. ; Step 6.6: After applying Dropout to rel_repr, input it into the relation classifier. During training, use BCEWithLogitsLoss to calculate the multi-label classification loss L_relation. During inference, compare the Sigmoid probability with a threshold of 0.4 to determine the predicted relation type. The reason for using multi-label instead of multi-class is that the same pair of entities may have multiple relationships in the public opinion text.
[0028] Step 7: During training, the entity classification loss, relation classification loss, and prototype contrastive learning loss are jointly calculated. The prototype contrastive loss is used to constrain the relation feature space distribution, thereby improving the relation category discrimination ability, as detailed below: like Figure 4 As shown, in the analysis of public opinion relationships, different relationship types naturally share semantic similarities. Traditional cross-entropy loss only focuses on the classification correctness of a single sample, neglecting the feature space distribution between different categories, leading to blurred decision boundaries between similar relationship types. Prototype contrastive learning is designed to compensate for this structural deficiency.
[0029] Step 7.1: Maintain the learnable prototype matrix Prototypes∈R^(C×K×D), where C is the number of relation types (including the "no relation / None" category), K=3 is the number of sub-prototypes per category, and D=128 is the prototype dimension. Initialization uses a normal distribution with a mean of 0 and a standard deviation of 0.02. The setting of K=3 is based on the following considerations: the same relation type presents diverse expressions in different contexts; the three sub-prototypes capture different semantic sub-patterns; if K is too small, the coverage will be insufficient; if it is too large, the training will be unstable. Step 7.2: Project the relation representation rel_repr onto a D=128-dimensional low-dimensional space using a linear projection layer. After ReLU activation and L2 normalization, proj_repr∈R^128 is obtained. (6) Where Wp is the learnable weight matrix of the linear projection layer, and proj_repr is the low-dimensional relation representation obtained after projection and normalization. Dimensional reduction projection makes the storage and computational overhead of the prototype vectors controllable, and L2 normalization maps all vectors to the unit hypersphere, making the calculation of cosine similarity comparable; Step 7.3: Perform L2 normalization on the prototype matrix as well; Step 7.4: For each positive relation type cls_id (index≥1) within the batch, filter the corresponding positive samples, calculate their cosine similarity with the K sub-prototypes of the corresponding type, and take the maximum value: (7) Where s_pos is the maximum cosine similarity between the sample and its positive example relation type sub-prototype. This indicates the calculation of cosine similarity. Taking the maximum value instead of the average means that a sample is considered a match as long as it is sufficiently close to any one of the sub-prototypes, corresponding to the design principle that "multiple expressions of the same relation type are each covered by a sub-prototype." Step 7.5: For each negative example relation type other_cls (other_cls ≠ cls_id) within the same batch, calculate the cosine similarity between the positive example sample and the prototype of the negative example type; to adapt to multi-label scenarios, if the current sample also has other positive example relations besides cls_id, then it is masked out from the other_cls set to avoid mistakenly rejecting real co-occurring relations as negative examples; then, take the global maximum value among all valid negative classes (difficult negative example mining): (8) Step 7.6, calculate the Margin contrastive loss and take the mean: (9) Here, margin is a preset interval threshold with a value of 0.5, and L_PCL is the prototype contrast loss. This loss function requires that the similarity between a positive sample and a positive prototype is at least 0.5 higher than its similarity with any negative prototype; otherwise, a penalty is imposed. The margin value of 0.5 strikes a balance between "overly lenient" and "overly strict". Step 7.7: Combine the three loss functions into a weighted sum and backpropagate to update all parameters: (10) Among them, L_entity is responsible for the accuracy of entity classification, L_relation is responsible for the accuracy of relation classification, and L_PCL is responsible for the inter-class separability of the relation feature space. Each of these three optimizes the model from a different dimension. A weight of 0.1 makes PCL an auxiliary constraint, preventing it from overly dominating the optimization direction—in the early stages of training, the PCL value is usually large (the prototype vectors have not yet been learned properly), and if the weight is too large, the model will ignore the main task; as training progresses, the prototype vectors gradually converge, and PCL tends to stabilize, at which point its auxiliary effect on the main task is most significant. Step 7.8 employs a differentiated learning rate strategy: the learning rate for the BERT layer is set to 5 × 10^-5, and the learning rate for newly added modules is set to 5 × 10^-4 (10 times that of the BERT layer). End-to-end training is performed using the AdamW optimizer (weight decay of 0.01) and linear learning rate preheating (preheating ratio of 0.1). The necessity of differentiated learning rates lies in the fact that the BERT layer has already been pre-trained on massive unsupervised corpora, and its parameters are already in a relatively optimal local region, requiring only minor fine-tuning; while newly added modules such as dilated convolutions, boundary awareness, and prototype contrast are randomly initialized, requiring a larger stride to quickly converge to the effective region. A 10-fold ratio has been proven in practice to both accelerate convergence and avoid destroying the pre-trained knowledge of the BERT layer. The module structure is as follows: Figure 4 As shown.
[0030] like Figure 5 As shown, Figure 5 This paper demonstrates the complete forward computation process of the overall model of this invention from input text to output prediction results: The input text sequence is first fed into BERT for encoding in step 2 to obtain a token-level context representation H; H is then subjected to multi-scale context enhancement by the multi-scale dilated convolution module in step 3 to obtain an enhanced representation H'; Based on H', the boundary-aware gated representation module in step 4 extracts the fused representation of the candidate entity Span; Step 5 concatenates fused with the global context representation entity_ctx and the length embedding size_emb and inputs it into the entity classifier to complete named entity recognition and output the entity type prediction result; For entities predicted as non-None, Step 6 constructs a relation representation rel_repr based on its fused representation fused, the inter-entity context representation rel_ctx, and the length embedding size_pair, and inputs it into the relation classifier to obtain the relation type prediction result; During the training phase, Step 7 further calculates the prototype contrast loss L_PCL based on the relation representation, and weights it with the entity classification loss L_entity and the relation classification loss L_relation to obtain the total loss, which is used for backpropagation to update the model parameters. Figure 5 The boxes in the middle correspond to the functional modules in the model, and the arrows indicate the direction of data flow between the modules. They intuitively present the sequential connection relationship and tensor dimension change process between the modules such as BERT encoding, multi-scale feature enhancement, boundary-aware fusion, entity recognition, relationship construction and classification, and prototype contrast learning in this invention, which makes it easy to understand the end-to-end inference and training process of this invention.
[0031] Step 8: Evaluate the model on the test set, and conduct comparative experiments and ablation experiments with existing methods.
[0032] Experimental environment: Operating system Windows 10, graphics card NVIDIA GPU, deep learning framework PyTorch, pre-trained model BERT-base-cased, programming language Python 3.8. Training hyperparameters: 25 training epochs, batch size 2, maximum sequence length 512, maximum span length 10, relation filtering threshold 0.4, dropout rate 0.1, and embedding dimension 25.
[0033] The model was evaluated on the test set of the CoNLL04 public dataset, with Micro-averaging and Macro-averaging used to calculate Precision, Recall, and F1-score for NER and RE tasks, respectively. The relation determination criteria were divided into a strict standard (requiring correct relation type, correct span of both entities, and correct type) and a lenient standard (requiring only correct relation type and correct span of both entities). The model was compared with baseline methods such as SpERT and Table-Sequence, and ablation experiments were conducted with four variations (removing the dilated convolution module, removing the boundary-aware module, removing the prototype contrastive learning module, and changing the dilated convolution to four groups and performing GELU), to verify the independent contributions of the three innovative components and the rationality of the network structure parameter settings.
[0034] Table 1
[0035] As shown in Table 1, the method of this invention exhibits strong and balanced performance in both entity recognition and relation extraction tasks: In the entity recognition task, the Entity R of this invention reaches 90.08, which is the highest value among all methods in the table; the Entity P reaches 91.33, second only to ECA Net (94.14), and higher than multi-head, SpERT, MRC4ERE and other methods; the Entity F1 reaches 90.04, lower than Table-Sequence (90.10) and TavlERT-CNN (90.50), but higher than SpERT, MRC4ERE, ECA Net, FedJ and multi-head and other methods. In relation extraction, the Relation P of this invention reaches 75.84, second only to ECA Net (82.42), and higher than methods such as multi-head, SpERT, and MRC4ERE. The Relation F1 reaches 72.75, lower than ECA Net (77.55), Table-Sequence (73.6), and TavlERT-CNN (73.2), but higher than FedJ, MRC4ERE, SpERT, and multi-head methods. The Relation R is 69.91, which is insufficient among methods with this metric, representing a relatively weak point for this invention. Overall, ECA Net slightly outperforms this invention in relation extraction precision and some metrics such as F1, but its Entity R is only 80.80, the lowest among the methods in the table, indicating insufficient entity recall. This invention achieves the best performance in entity recall and maintains a high level in other metrics, with better balance among the metrics, making it more suitable for practical applications such as public opinion monitoring that require high entity coverage completeness.
[0036] Table 2
[0037] The ablation experiment results in Table 2 show that the complete model achieved the best performance on both NER F1 and RE F1 (91.33 and 72.75). After removing the dilated convolution module, the NER F1 dropped to 88.72 and the RE F1 dropped to 71.93, showing the most significant decrease among all variants. This indicates that the multi-scale dilated convolution module plays an important role in capturing long-distance contextual semantic relationships in public opinion texts. After removing the boundary-aware module (replacing it with max-pool), the NER F1 dropped to 90.26, indicating that this module directly contributes to the accurate localization of entity boundaries. After removing the prototype contrastive learning module (weight=0), the NER F1 and RE F1 dropped to 89.86 and 72.11, respectively, indicating that prototype contrastive loss helps enhance the inter-class separability of the relation feature space and reduce the confusion rate of similar relation types. The variant with 4 groups of dilated convolutions and GELU activation performed worse (89.78 / 72.64) than the original 3-group ReLU activation design, validating the rationality of the current choice of the number of convolution groups and activation function. In summary, all three innovative modules contribute positively to the model performance, with the dilated convolution module making the most significant contribution.
Claims
1. A joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning, characterized by: Specifically, the steps include the following: Step 1: Obtain the joint entity relationship extraction dataset, preprocess the dataset, and divide it into training set, validation set, and test set; Step 2: Input the text sequence into the pre-trained language model BERT; Step 3: Input the hidden state sequence output by BERT into the multi-scale dilated convolution module to output enhanced representation features; Step 4: Extract the fused representation of the candidate entity Span; Step 5: Perform named entity recognition; Step 6: Construct a relation representation and input it into a relation classifier for relation type prediction; Step 7: Compare the feature space distribution of the loss constraint relationship using the prototype.
2. The joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning according to claim 1, characterized in that: The specific process of step 1 is as follows: Read the JSON format annotation file, enumerate all candidate Spans with a length not exceeding 10 for each sentence, construct the entity annotation matrix and relation annotation matrix, randomly sample negative entity Spans and negative relation pairs, generate batch-encoded sequences, mask matrices and annotation vectors, and divide the data into training set, validation set and test set.
3. The joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning according to claim 2, characterized in that: The specific process of step 2 is as follows: The input sentence is segmented into subwords using the BERT WordPiece tokenizer. [CLS] and [SEP] tags are added to the beginning and end of the sequence, respectively. The token ID sequence is then input into the BERT-base-cased pre-trained model. The BERT model contains 12 layers of bidirectional Transformer encoders, and the output of the last layer is used as the initial token-level context representation. L is the sequence length, and the output vector at the [CLS] position contains the preliminary global semantics of the entire sentence.
4. The joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning according to claim 3, characterized in that: The specific process of step 3 is as follows: Step 3.1, Save the residual branch: Perform a linear transformation on the input H. Preserve original semantic information for residual connections; Step 3.2: Transpose H into a convolutional format (Batch, 768, L), and process it by dilating the convolutional layer to make the length of the output sequence consistent with the input. Step 3.3: After each group of convolutional operations, ReLU activation and LayerNorm normalization are performed sequentially. Step 3.4: After the last set of convolutional outputs, transpose H to (Batch, L, 768) format, add it to the residual branch H_res, and then perform final LayerNorm normalization to obtain the enhanced token representation. .
5. The joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning according to claim 4, characterized in that: The specific process of step 4 is as follows: Step 4.1: For the entity mask matrix of the candidate Span, after applying a minimum value to the positions where the mask is 0, take the Max-pool to mask irrelevant token features outside the Span, thus obtaining the semantic representation inside the Span. ; Step 4.2: Locate the start token position start_idx and end token position end_idx of the Span from the entity mask using position indexing operations, and extract the corresponding token hidden states h_start and h_end from H'; the start token usually contains clues about the entity's type, while the end token marks the right boundary of the entity; Step 4.3: Project h_start and h_end through independent linear layers respectively: (1) Step 4.4: Using the concatenation of span_pool, z_start, and z_end as input, calculate the adaptive weights through the gating layer: (2) Step 4.5: Weave the two semantic representations by gating weights, and output the result after LayerNorm normalization: (3) Here, ⊙ represents element-wise multiplication.
6. The joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning according to claim 5, characterized in that: The specific process of step 5 is as follows: Step 5.1: Extract the hidden state at position 0 in H' corresponding to the [CLS] token as the global context representation. ; Step 5.2: Query the length embedding table based on the Span length to obtain... ; Step 5.3: Concatenate the features output from Steps 4, 5.1, and 5.2: (4) Step 5.4: After Dropout (rate=0.1) regularization, input the entity classification linear layer (Linear, 1561→|entity_types|). During training, calculate the cross-entropy loss L_entity. During inference, use Softmax to calculate the class probability and take argmax to output the predicted entity type. Spans of type None (index=0) are considered negative examples.
7. The joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning according to claim 6, characterized in that: The specific process of step 6 is as follows: Step 6.1: During inference, enumerate all ordered entity pairs from the predicted non-None entities; during training, use the labeled positive relation pairs and the negative relation pairs sampled in step 1 to enable the model to learn to recognize unrelated None. Step 6.2: Extract the boundary-aware fusion representations (fused) generated in Step 4 for the two types of entities and concatenate them to obtain the entity pair representations. ; Step 6.3: For the context token region between the two entities, obtain the relational context representation through a masked Max-pool. For adjacent or overlapping nested entity pairs that result in empty context regions without intermediate words, the values are directly set to zero. Step 6.4: Embed the lengths of the two entities to obtain... ; Step 6.5: Concatenate rel_ctx, entity_pairs, and size_pair to form a relational representation. ; Step 6.6: After applying Dropout to rel_repr, input the relation classifier. During training, use BCEWithLogitsLoss to calculate the multi-label classification loss L_relation. During inference, compare the Sigmoid probability with the threshold to determine the predicted relation type.
8. The joint entity relation extraction method based on multi-scale dilated convolution and prototype contrastive learning according to claim 7, characterized in that: The specific process of step 7 is as follows: Step 7.1: Project the relational representation rel_repr onto a D=128-dimensional low-dimensional space using a linear projection layer, followed by ReLU activation and L2 normalization, to obtain... : (5) Step 7.2: Perform L2 normalization on the prototype matrix; Step 7.3: For each positive relation type cls_id within the batch, where index≥1, filter the corresponding positive samples, calculate the cosine similarity of its corresponding type's K sub-prototypes, and take the maximum value: (6) Step 7.4: For each negative example relation type `other_cls` within the same batch, calculate the cosine similarity between the positive example sample and the prototype of the negative example type. To adapt to multi-label scenarios, if the current sample has other positive example relations besides `cls_id`, then the sample is masked out of the `other_cls` set, and then the global maximum value among all valid negative classes is taken. (7) Step 7.5, calculate the Margin contrastive loss and take the mean: (8) Step 7.6: Perform a weighted summation of the three loss functions and backpropagate to update all parameters: (9) Among them, L_entity is responsible for the accuracy of entity classification, L_relation is responsible for the accuracy of relation classification, and L_PCL is responsible for the inter-class separability of the relation feature space. Step 7.7: Employ a differentiated learning rate strategy, combined with the AdamW optimizer and linear learning rate preheating for end-to-end training.
