Medical knowledge base semantic matching method and device oriented to imbalance of positive and negative samples
By using a BGE encoder and DBSCAN for deduplication and the Annoy approximate nearest neighbor algorithm to filter training data in an intelligent medical dialogue system, and combining a loss function with topic-intention consistency constraints and knowledge distillation techniques, the training process of the student model was optimized, solving the problem of imbalance between positive and negative samples in the medical knowledge base, and improving the accuracy of semantic matching and the generalization ability of the model.
Patent Information
- Application Number
- CN202510973096.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2025-06-30
- Filing Date
- 2025-07-15
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2045-07-15
AI Technical Summary
In intelligent medical dialogue systems, the query hit rate of medical knowledge base and the accuracy of robot interaction are affected by the imbalance of positive and negative samples. Existing technologies suffer from model convergence defects, gradient dilution problems, and insufficient FocalLoss prediction accuracy, resulting in poor performance of semantic matching models in medical scenarios.
Training data is selected using a BGE encoder combined with DBSCAN deduplication and Annoy approximate nearest neighbor algorithm. A multi-level selection process is constructed through a focus loss function constrained by topic and intent consistency and a distillation loss function. Knowledge distillation is performed by combining a lightweight BERT and ERNIE-health model to optimize the training process of the student model.
It significantly increased the ratio of similar to dissimilar samples in the training data, alleviated the problem of imbalanced training data, improved the accuracy of semantic matching and the generalization ability of the model, and enhanced the ability to match semantics specific to the medical field.
Smart Images

Figure CN120994772A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of intelligent medical dialogue, and in particular to a medical knowledge base semantic matching method and device for positive and negative sample imbalance. BACKGROUND
[0002] In the field of intelligent medical dialogue systems, the query hit rate of medical question and answer knowledge base and the accuracy of robot interaction are highly dependent on the accurate understanding of medical professional data by the semantic matching model. However, the current technical solution has the following significant deficiencies:
[0003] 1. Scarcity and distribution imbalance of medical dialogue data
[0004] In the intelligent consultation scenario of the medical and health field, patient symptom descriptions have high diversity, and the data distribution of symptom-diagnosis categories in the medical knowledge base is extremely unbalanced. For example: high data acquisition difficulty: the precise question and answer pair data that meet the clinical specification only accounts for 10%-15% of the overall data set, while a large amount of redundant and irrelevant data (such as the combination of symptoms and non-corresponding treatment plans) accounts for more than 80%; high labeling cost: in the existing data extraction process, labeling personnel need to label a large amount of invalid negative samples (such as "diabetes" and "fracture treatment plan" that are not related), which accounts for more than 60% of the total labeling cost, resulting in serious waste of resources.
[0005] 2. Difficulties in model training driven by unbalanced data
[0006] (1) Model convergence defects and local optimal solution problems
[0007] Under the class imbalance data of the medical scene, the traditional similarity model (such as the Siamese Network or interactive Cross Encoder architecture) has the following defects: insufficient gradient correction ability, the gradient contribution of the minority class samples in the training process is excessively diluted, resulting in a large number of flat areas in the loss surface, and finally falling into a local optimal solution.
[0008] (2) Prediction accuracy defects of FocalLoss
[0009] Although FocalLoss can improve model convergence by dynamically adjusting the weights of difficult and easy samples (when γ parameter ≥ 2), there are the following technical contradictions in the medical scene:
[0010] 1) Increased misjudgment rate of extreme samples: the predicted similarity score of obviously dissimilar query pairs (such as "diabetes symptoms" and "fracture treatment plan") is still higher than the threshold (such as 0.4), resulting in a decrease in F1-score on the test set to below 0.6;
[0011] 2) Category boundary blurring: FocalLoss lacks targeted optimization for medical field-specific semantic gaps (e.g., synonym mapping error rate > 30% between "chest pain" and "angina pectoris"), resulting in insufficient key feature extraction capability. SUMMARY
[0012] The present application aims to propose a medical knowledge base semantic matching method and device for positive and negative sample imbalance in view of the above-mentioned technical problems.
[0013] In a first aspect, the present application provides a medical knowledge base semantic matching method for positive and negative sample imbalance, comprising the following steps:
[0014] Obtain the search words and customer questions in the medical scenario dialogue and form a query set, construct candidate sample pairs in combination with the matching questions in the medical knowledge base, screen and label the candidate sample pairs, and adjust the proportion of positive sample pairs and negative sample pairs to obtain training data; extract the theme and intent of the sample pairs in the training data respectively, classify the sample pairs in the training data according to the theme and intent, and obtain theme and intent consistency classification results; Figure 1
[0015] Input the sample pairs in the training data into the student model to obtain first projection vectors and first similarity vectors, input the sample pairs in the training data into the teacher model to obtain second projection vectors and second similarity vectors; construct a focal loss function based on the theme and intent consistency classification results and the first similarity vectors of the sample pairs in the training data, construct a distillation loss function based on the output results of the student model and the output results of the teacher model, construct a total loss function based on the focal loss function and the distillation loss function, complete knowledge distillation from the teacher model to the student model using the total loss function, and obtain a trained student model; Figure 1 Figure 1
[0016] Obtain the customer question to be matched and perform preliminary retrieval on it using the medical knowledge base to obtain a plurality of preliminary retrieval matching questions; input the customer question to be matched and each preliminary retrieval matching question into the trained student model to output the corresponding similarity vectors, and determine the matching question with the highest semantic similarity to the customer question to be matched according to the similarity vectors corresponding to all preliminary retrieval matching questions and output the corresponding reply statement.
[0017] As a preferred, obtaining the search words and customer questions in the medical scenario dialogue and forming a query set, constructing candidate sample pairs in combination with the matching questions in the medical knowledge base, screening and labeling the candidate sample pairs, and adjusting the proportion of positive sample pairs and negative sample pairs to obtain training data, specifically comprising:
[0018] The BGE encoder is used to vectorize each customer question in the query set to obtain a corresponding text vector, and the DBSCAN clustering algorithm is used to remove duplicates from all customer questions in the query set according to the text vector to obtain a deduplicated query set.
[0019] The medical knowledge base is manually cleaned and vectorized using the BGE encoder to construct a vector library.
[0020] The approximate nearest neighbor algorithm is used to retrieve the top N matching questions in the vector library that are semantically similar to the query samples in the deduplicated query set, and form a candidate sample pair set with the corresponding query samples.
[0021] A prompt word for judging semantic adaptability is constructed, and a large language model is used to judge the semantic adaptability of each candidate sample pair in the candidate sample pair set by using the prompt word for judging semantic adaptability, and output the label of each candidate sample pair, and retain all candidate sample pairs with similar labels and part of the candidate sample pairs with different labels.
[0022] The BGE encoder and the DBSCAN clustering algorithm are used to perform semantic deduplication on all candidate sample pairs with similar labels and part of the candidate sample pairs with different labels, and then manually annotate to obtain corresponding similarity labels.
[0023] As a preferred embodiment, the focus loss function based on the thematic intention Figure 1 consistency constraint is represented as:
[0024] loss focal-consistency =-α(1-p) γ1 β(1-C) γ2 log(p);
[0025] Wherein, loss focal-consistency represents the focus loss function based on the thematic intention Figure 1 consistency constraint, α and β represent the first balance factor and the second smoothing factor, γ1 and γ2 represent the first modulation factor and the second adjustment factor respectively, p represents the probability of selecting the corresponding type in the first similarity vector according to the similarity label of the sample pair in the training data, and C represents the thematic intention Figure 1 consistency coefficient determined according to the thematic and intention Figure 1 consistency classification result, which is represented as:
[0026]
[0027] As a preferred embodiment, the sample pairs in the training data are input into the student model to obtain the first projection vector and the first similarity vector, and the sample pairs in the training data are input into the teacher model to obtain the second projection vector and the second similarity vector, which specifically includes:
[0028] The student model comprises a first embedding layer and a first feature extraction layer connected in sequence and a first feature projection layer and a first similarity prediction module arranged in parallel. A sample pair in training data is input into the student model, first passes through the first embedding layer to obtain a corresponding first embedding vector, the first embedding vector is input into the first feature extraction layer to obtain a first feature vector, and the first feature vector is input into the first feature projection layer and the first similarity prediction module respectively to obtain a first projection vector and a first similarity vector, as shown in the following formula:
[0029] h s-proj =project_linear(h s );
[0030] p ss =softmax(mlp(h s ));
[0031] wherein h s represents the first feature vector, h s-proj represents the first projection vector, project_linear represents a linear layer, p ss represents the first similarity vector, and the dimension of the first similarity vector is 2, corresponding to a similar probability and an unlike probability respectively; mlp represents a multi-layer perception, softmax represents a softmax function, and sigmoid represents a sigmoid activation function.
[0032] The teacher model comprises a second embedding layer and a second feature extraction layer connected in sequence and a second feature projection layer and a second similarity prediction module arranged in parallel. A sample pair in training data is input into the student model, first passes through the second embedding layer to obtain a corresponding second embedding vector, the second embedding vector is input into the second feature extraction layer to obtain a second feature vector, and the second feature vector is input into the second feature projection layer and the second similarity prediction module respectively to obtain a second projection vector and a second similarity vector, as shown in the following formula:
[0033] h t-proj =project_linear(h t );
[0034] p ts =softmax(mlp(h t ));
[0035] wherein h t represents the second feature vector, h t-proj represents the second projection vector, and p ts represents the second similarity vector, and the dimension of the second similarity vector is 2, corresponding to a similar probability and an unlike probability respectively.
[0036] Preferably, a distillation loss function is constructed based on the output results of the student model and the teacher model, specifically including:
[0037] A cross-entropy loss function is constructed based on the first projection feature and the second projection feature, and used as the first distillation loss function, as shown in the following equation:
[0038] loss hidden =MSE(h t-proj ,h s-proj );
[0039] Where, loss hidden Let MSE represent the first distillation loss function, and MSE represent the cross-entropy loss function.
[0040] The cross-entropy loss function is constructed based on the first and second similarity vectors and used as the second distillation loss function, as shown in the following equation:
[0041] loss s_logits =MSE(p ts ,p ss );
[0042] Where, loss s_logits This represents the second distillation loss function.
[0043] As a preferred option, the total loss function is expressed as:
[0044] loss total =μ1loss hidden +μ2loss s_logits +μ3loss focal-consistency ;
[0045] Where, loss total Represents the total loss function, loss focal-consistency Indicates based on the theme Figure 1 The focus loss function of consistency constraints, loss s_logits Denotes the second distillation loss function, loss hidden Let μ1, μ2, and μ3 represent the first distillation loss function, and μ1, μ2, and μ3 represent the corresponding weighting coefficients, respectively.
[0046] Secondly, the present invention provides a semantic matching device for a medical knowledge base with imbalanced positive and negative samples, comprising:
[0047] The training data construction module is configured to obtain search words and customer questions in a medical scene dialogue and form a query set, construct candidate sample pairs in combination with matching questions in a medical knowledge base, screen and label the candidate sample pairs, and adjust the proportion of positive sample pairs and negative sample pairs to obtain training data; topics and intents of the sample pairs in the training data are extracted respectively, the sample pairs in the training data are classified according to the topics and intents to obtain topic-intent consistency classification results. Figure 1
[0048] The knowledge distillation module is configured to input the sample pairs in the training data into a student model to obtain first projection vectors and first similarity vectors, input the sample pairs in the training data into a teacher model to obtain second projection vectors and second similarity vectors; the topic-intent consistency classification results and the first similarity vectors are used to construct a focal loss function based on the topics and intents of the sample pairs in the training data, the output results of the student model and the output results of the teacher model are used to construct a distillation loss function, the focal loss function and the distillation loss function are used to construct a total loss function, and the knowledge distillation from the teacher model to the student model is completed by using the total loss function to obtain a trained student model. Figure 1 Figure 1 The knowledge distillation module is configured to input the sample pairs in the training data into a student model to obtain first projection vectors and first similarity vectors, input the sample pairs in the training data into a teacher model to obtain second projection vectors and second similarity vectors; the topic-intent consistency classification results and the first similarity vectors are used to construct a focal loss function based on the topics and intents of the sample pairs in the training data, the output results of the student model and the output results of the teacher model are used to construct a distillation loss function, the focal loss function and the distillation loss function are used to construct a total loss function, and the knowledge distillation from the teacher model to the student model is completed by using the total loss function to obtain a trained student model.
[0049] The semantic matching module is configured to obtain a customer question to be matched and perform preliminary retrieval on the customer question to be matched by using a medical knowledge base to obtain a plurality of preliminary retrieval matching questions; the customer question to be matched and each preliminary retrieval matching question are input into the trained student model to output corresponding similarity vectors, and the matching question with the highest semantic similarity to the customer question to be matched is determined according to the similarity vectors corresponding to all the preliminary retrieval matching questions and a corresponding reply sentence is output.
[0050] In a third aspect, the present application provides an electronic device, comprising one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by the one or more processors, the one or more processors implement the method described in any of the implementation manners of the first aspect.
[0051] In a fourth aspect, the present application provides a computer-readable storage medium, which stores a computer program, and the computer program is executed by a processor to implement the method described in any of the implementation manners of the first aspect.
[0052] In a fifth aspect, the present application provides a computer program product, which comprises a computer program, and the computer program is executed by a processor to implement the method described in any of the implementation manners of the first aspect.
[0053] Compared with the prior art, the present application has the following beneficial effects:
[0054] (1) The medical knowledge base semantic matching method for positive and negative sample imbalance provided in the present application greatly improves the proportion of similar and dissimilar samples in the training data through the multi-level screening process of the BGE encoder combined with DBSCAN deduplication, Annoy approximate nearest neighbor algorithm matching and large language model filtering, to a certain extent, the problem of training data imbalance is alleviated.
[0055] (2) The medical knowledge base semantic matching method for positive and negative sample imbalance provided in the present application amplifies the loss contribution of low consistency sample pairs through dynamic weight distribution of theme and intent consistency results, solves the problem of gradient dilution of traditional models for minority class samples. Figure 1
[0056] (3) The medical knowledge base semantic matching method for positive and negative sample imbalance provided in the present application forces the model to focus on the implicit entity and demand type specific to the medical field through the dual constraints of theme classification and intent classification, and through the dual distillation of the first distillation loss function and the second distillation loss function, the performance of the student model is close to that of the teacher model, effectively improving the accuracy of semantic matching. BRIEF DESCRIPTION OF DRAWINGS
[0057] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.
[0058] Figure 1 The flowchart of the medical knowledge base semantic matching method for positive and negative sample imbalance of the embodiments of the present application;
[0059] Figure 1 The schematic diagram of the medical knowledge base semantic matching device for positive and negative sample imbalance of the embodiments of the present application;
[0060] Figure 1 The hardware structure schematic diagram of the electronic device provided in the embodiments of the present application. DETAILED DESCRIPTION
[0061] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below with reference to the drawings. Obviously, the described embodiments are only some of the embodiments of the present application, not all. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.
[0062] Figure 1 The embodiment of the application provides a medical knowledge base semantic matching method for positive and negative sample imbalance, and the method comprises the following steps:
[0063] S1, obtaining search words and customer questions in a medical scene dialogue and forming a query set, combining matching questions in a medical knowledge base to construct a candidate sample pair, screening and labeling the candidate sample pair, adjusting the proportion of positive sample pairs and negative sample pairs, obtaining training data; respectively extracting the theme and intent of the sample pair in the training data, classifying the sample pair in the training data according to the theme and intent, obtaining theme and intent Figure 1 consistency classification results.
[0064] In a specific embodiment, obtaining search words and customer questions in a medical scene dialogue and forming a query set, combining matching questions in a medical knowledge base to construct a candidate sample pair, screening and labeling the candidate sample pair, adjusting the proportion of positive sample pairs and negative sample pairs, obtaining training data, specifically comprising:
[0065] The BGE encoder is used for vectorization processing of each customer question in the query set, to obtain a corresponding text vector, and the DBSCAN clustering algorithm is used for de-duplication processing of all customer questions in the query set according to the text vector, to obtain a de-duplicated query set;
[0066] The medical knowledge base is manually cleaned and vectorized using the BGE encoder to construct a vector library;
[0067] The approximate nearest neighbor algorithm is used to retrieve the top N matching questions in the vector library that are semantically similar to the query samples in the de-duplicated query set and form a candidate sample pair set with the corresponding query samples;
[0068] A prompt word for judging semantic adaptability is constructed, a large language model is used to judge the semantic adaptability of each candidate sample pair in the candidate sample pair set in combination with the prompt word for judging semantic adaptability, and the label of each candidate sample pair is output, and all candidate sample pairs with similar labels and part of the candidate sample pairs with different labels are retained;
[0069] The BGE encoder and the DBSCAN clustering algorithm are further used to perform semantic de-duplication on all candidate sample pairs with similar labels and part of the candidate sample pairs with different labels, and manual labeling is further performed to obtain corresponding similarity labels.
[0070] Specifically, the embodiment of the present application extracts search words and customer questions in real medical conversations from an online medical consultation platform to form an original query set, each query sample in the query set including search words and customer questions, uses a BGE encoder to perform semantic vectorization on the customer questions, removes semantic duplicate samples in combination with a DBSCAN clustering algorithm (ε = 0.03, min_samples = 2), and outputs a de-duplicated query set.
[0071] Obtain a medical knowledge base, the medical knowledge base as a QA base, containing a plurality of question and answer pairs composed of matching questions and their corresponding answers, in the preparation stage of the medical knowledge base, manually clean the medical knowledge base, and remove outdated or non-standard question and answer pairs; respectively, the field of the matching question (i.e., the Q field) of the cleaned QA base uses a BGE encoder to construct a vector library.
[0072] Further, each query sample in the de-duplicated query set is retrieved in the vector library using the Annoy approximate nearest neighbor algorithm to obtain the Top5 matching questions in terms of semantic similarity, forming a candidate sample pair set. In one embodiment, a Qwen2.5-72B large model is called as a large language model to perform semantic adaptability judgment on the candidate sample pairs in the medical field, and the large language model outputs a similar label or a dissimilar label for each candidate sample pair in the candidate sample pair set, wherein the candidate sample pairs with the similar label are retained, and the candidate sample pairs with the dissimilar label are randomly retained with a probability of 10%. Through the above method, the large language model is used to evaluate and filter the field adaptability of the candidate sample pair set, and the BGE encoder and the DBSCAN clustering algorithm are used to perform semantic de-duplication on the filtered candidate sample pairs to obtain training data with optimized labeling effect. Through the above method, the cost of manual labeling can be greatly reduced, and only the training data with optimized labeling effect needs to be manually labeled to obtain training data with relatively balanced number of positive and negative sample pairs. The proportion of similar and dissimilar labels extracted by the negative sample screening method is improved from 1:9 to 3:7, which to some extent alleviates the problem of unbalanced training data.
[0073] S2, input the sample pairs in the training data into the student model to obtain a first projection vector and a first similarity vector, and input the sample pairs in the training data into the teacher model to obtain a second projection vector and a second similarity vector; construct a topic intention consistency classification result based on the similarity label, topic and intention of the sample pairs in the training data, and the first similarity vector. Figure 1 Figure 1 The focus loss function of the consistency constraint is constructed based on the output result of the student model and the output result of the teacher model, the distillation loss function is constructed based on the focus loss function and the distillation loss function, the total loss function is constructed by using the distillation loss function to complete the knowledge distillation from the teacher model to the student model, and the trained student model is obtained.
[0074] In specific embodiments, the sample pair in the training data is input into the student model to obtain a first projection vector and a first similarity vector, and the sample pair in the training data is input into the teacher model to obtain a second projection vector and a second similarity vector, specifically comprising:
[0075] The student model comprises a first embedding layer and a first feature extraction layer connected in sequence and a first feature projection layer and a first similarity prediction module arranged in parallel, the sample pair in the training data is input into the student model, first passes through the first embedding layer to extract a corresponding first embedding vector, the first embedding vector is input into the first feature extraction layer to obtain a first feature vector, and the first feature vector is input into the first feature projection layer and the first similarity prediction module respectively to obtain the first projection vector and the first similarity vector, as shown in the following formula:
[0076] h s-proj =project_linear(h s );
[0077] p ss =softmax(mlp(h s ));
[0078] Wherein, h s represents the first feature vector, h s-proj represents the first projection vector, project_linear represents a linear layer, p ss represents the first similarity vector, which has a dimension of 2, corresponding to the probability of similarity and the probability of dissimilarity respectively; mlp represents a multi-layer perception, softmax represents a softmax function, and sigmoid represents a sigmoid activation function.
[0079] The teacher model comprises a second embedding layer and a second feature extraction layer connected in sequence and a second feature projection layer and a second similarity prediction module arranged in parallel, the sample pair in the training data is input into the student model, first passes through the second embedding layer to extract a corresponding second embedding vector, the second embedding vector is input into the second feature extraction layer to obtain a second feature vector, and the second feature vector is input into the second feature projection layer and the second similarity prediction module respectively to obtain the second projection vector and the second similarity vector, as shown in the following formula:
[0080] h t-proj =project_linear(ht );
[0081] p ts =softmax(mlp(h) t ));
[0082] Among them, h t Let h represent the second eigenvector. t-proj p represents the second projection vector. ts Let represent the second similarity vector, which has a dimension of 2, and represents the probabilities of similarity and dissimilarity, respectively.
[0083] Specifically, in the embodiments of this application, a lightweight BERT model (such as tiny-BERT, tiny-ERNIE model) is used as the student model. Its structure mainly includes a first embedding layer, a first feature extraction layer, a first feature projection layer, and a first similarity prediction module. The sample pairs in the training data are first input into the first embedding layer to obtain a first embedding vector. The first embedding vector includes a token sequence (input_ids), an attention mask (attention_mask), and a sentence segmentation identifier (token_type_ids). The first embedding vector is input into the first feature extraction layer, which adopts an encoder structure to extract a first feature vector. The first feature vector is then input into the first feature projection layer and the first similarity prediction module to obtain a first projection vector and a first similarity vector. The first feature projection layer is a linear layer.
[0084] Furthermore, a BERT-like model with a large number of parameters (such as the ERNIE-health model) can be used as a teacher model. This teacher model can be a pre-trained model with relatively strong semantic matching capabilities. By performing knowledge distillation between this teacher model and the student model, the student model can learn the semantic matching capabilities of the teacher model. The overall structure of the teacher model is similar to that of the student model, and will not be elaborated further here.
[0085] In a specific embodiment, a distillation loss function is constructed based on the output results of the student model and the teacher model, specifically including:
[0086] A cross-entropy loss function is constructed based on the first projection feature and the second projection feature, and used as the first distillation loss function, as shown in the following equation:
[0087] loss hidden =MSE(h t-proj ,h s-proj );
[0088] Where, loss hidden Let MSE represent the first distillation loss function, and MSE represent the cross-entropy loss function.
[0089] The cross-entropy loss function is constructed based on the first and second similarity vectors and used as the second distillation loss function, as shown in the following equation:
[0090] loss s_logits =MSE(p ts ,p ss );
[0091] Where, loss s_logits This represents the second distillation loss function.
[0092] In specific embodiments, based on the subject matter Figure 1 The focus loss function of consistency constraints is expressed as:
[0093] loss focal-consistency = -α(1-p) γ1 β(1-C) γ2 log(p);
[0094] Where, loss focal-consistency Indicates based on the theme Figure 1 The focus loss function is constrained by consistency, where α and β represent the first balance factor and the second smoothing factor, γ1 and γ2 represent the first modulation factor and the second adjustment factor, respectively, p represents the probability of selecting the corresponding type in the first similarity vector based on the similarity labels of sample pairs in the training data, and C represents the probability of selecting the corresponding type in the first similarity vector based on the similarity labels of the topic and the meaning. Figure 1 The theme determined by the consistency classification results Figure 1 The consistency coefficient is expressed as:
[0095]
[0096] In a specific embodiment, the total loss function is expressed as:
[0097] loss total =μ1loss hidden +μ2loss s_logits +μ3loss focal-consistency ;
[0098] Where, loss total Represents the total loss function, loss focal-consistency Indicates based on the theme Figure 1 The focus loss function of consistency constraints, loss s_logits This represents the second distillation loss function, loss hidden Let μ1 represent the first distillation loss function.
[0099] μ2 and μ3 represent the corresponding weighting coefficients, respectively.
[0100] Specifically, embodiments of this application integrate topic classification information and intent classification information of sample pairs in the training data to construct a multi-dimensional constraint mechanism, guiding the student model to focus on key semantic features during training. The core idea is that sample pairs with inconsistent topics or intents have low relevance in semantic similarity tasks; therefore, by introducing topic-intent-inconsistent information... Figure 1 Consistency constraints dynamically adjust the weights of sample pairs in the training data within the loss function, thereby improving the student model's generalization ability on imbalanced data such as medical dialogues. The following steps constrain the model's convergence direction:
[0101] (1) Classification information extraction: Extract the topic (text1_topic, text2_topic) and intent (text1_intent, text2_intent) of the input sample pair (text1, text2). The topic contains implicit entity features (such as "dermatology" and "hair repair"), while the intent represents the type of user need (such as "encryption" and "remodeling").
[0102] (2) Based on the degree of matching between the topic and the intent, the sample pairs are divided into three categories to obtain the degree of matching between the topic and the intent. Figure 1 Consistency classification results: Completely consistent (Score=3): Both topic and intent match (text1_topic=text2_topic and text1_intent=text2_intent); Partially consistent (Score=2): Either topic or intent matches (text1_topic=text2_topic or text1_intent=text2_intent); Completely inconsistent (Score=1): Neither topic nor intent matches.
[0103] (3) Hierarchical design of consistency penalty: linking the topic with the meaning Figure 1 Consistency classification results mapped to topic meaning Figure 1 Consistency coefficients (e.g., 3→0.97, 2→0.5, 1→0.03) are introduced into the loss function as constraint signals. By assigning weights, the loss contribution of low-consistency sample pairs (e.g., Score=1 or 2) is amplified, alleviating the data imbalance problem.
[0104] Therefore, by combining topic and intent constraints with focus loss, a topic-intention-based loss function is constructed. Figure 1 The focus loss function of consistency constraints, based on topic meaning Figure 1 In the focus loss function with consistency constraints, the probability of the corresponding dimension in the first similarity vector is selected based on the similarity label corresponding to the sample pair in the training data, and the topic meaning obtained by mapping is used. Figure 1 Consistency coefficient. By introducing a penalty mechanism, probability and topic intent...Figure 1 The greater the value of the consistency coefficient is, the smaller the proportion in the focal loss function is, and the punishment mechanism can make the student model pay more attention to the samples with inconsistent topic intention and insufficient model prediction confidence in the training process, so as to optimize the problem of data imbalance.
[0105] Further, the first projection vector obtained in the student model and the second projection vector obtained in the teacher model are used to construct a first distillation loss function, and the first similarity vector output by the student model and the second similarity vector output by the teacher model are used to construct a second distillation loss function. The topic consistency constraint focal loss function, the first distillation loss function and the second distillation loss function are used to construct a total loss function, and the total loss function is minimized to realize knowledge distillation of the teacher model to the student model, so that the trained student model can achieve better semantic matching effect. Figure 1 Further, the first projection vector obtained in the student model and the second projection vector obtained in the teacher model are used to construct a first distillation loss function, and the first similarity vector output by the student model and the second similarity vector output by the teacher model are used to construct a second distillation loss function. The topic consistency constraint focal loss function, the first distillation loss function and the second distillation loss function are used to construct a total loss function, and the total loss function is minimized to realize knowledge distillation of the teacher model to the student model, so that the trained student model can achieve better semantic matching effect.
[0106] S3, obtaining a client question to be matched and performing preliminary retrieval on the client question to be matched by using a medical knowledge base to obtain a plurality of preliminary retrieval matched questions; inputting the client question to be matched and each preliminary retrieval matched question into the trained student model to output a corresponding similarity vector, and determining a matched question with the highest semantic similarity to the client question to be matched according to the similarity vectors corresponding to all the preliminary retrieval matched questions and outputting a corresponding reply sentence.
[0107] Specifically, the trained student model is deployed, after obtaining the client question to be matched, the client question to be matched is first retrieved by using the medical knowledge base to obtain the preliminary retrieval matched question. Then, the client question to be matched and each preliminary retrieval matched question are input into the trained student model to obtain the corresponding similarity vector, and the matched question with the highest similarity is found by using the similarity vector, and the corresponding reply sentence is output.
[0108] Further, the medical knowledge base semantic matching method for positive and negative sample imbalance proposed in the embodiment of the present application is compared with the existing semantic matching model based on Bert-base and trained by using the focal loss in the indexes on the thyroid department and the skin department test set, and the results are shown in Table 1; the medical knowledge base semantic matching method for positive and negative sample imbalance proposed in the embodiment of the present application is compared with the existing Text-sim model in the indexes on the thyroid department and the skin department test set, and the results are shown in Table 2, the accuracy and F1 of the medical knowledge base semantic matching method for positive and negative sample imbalance proposed in the embodiment of the present application are higher in different departments, which further illustrates the effectiveness of the present application.
[0109] Table 1
[0110] Accuracy Recall F1 Bert-base+focal loss 0.8440 0.5670 0.6783 The present invention 0.8701 0.6100 0.7171
[0111] Table 2
[0112] Precision Recall F1 Text-sim model 0.6311 0.6694 0.7251 The present invention 0.8094 0.6034 0.7981
[0113] Further reference Figure 2 As an implementation of the method shown in the above figures, the present application provides an embodiment of a medical knowledge base semantic matching device for positive and negative sample imbalance. The device embodiment corresponds to the method embodiment shown in Figure 1 , and the device can be specifically applied to various electronic devices.
[0114] The embodiment of the present application provides a medical knowledge base semantic matching device for positive and negative sample imbalance, which comprises:
[0115] The training data construction module 1 is configured to obtain search words and customer questions in a medical scene dialogue and form a query set, construct candidate sample pairs in combination with matching questions in a medical knowledge base, screen and label the candidate sample pairs, and adjust the proportion of positive sample pairs and negative sample pairs to obtain training data; extract the theme and intent of the sample pairs in the training data respectively, classify the sample pairs in the training data according to the theme and intent, and obtain theme and intent consistency classification results. Figure 1
[0116] The knowledge distillation module 2 is configured to input the sample pairs in the training data into a student model to obtain a first projection vector and a first similarity vector, input the sample pairs in the training data into a teacher model to obtain a second projection vector and a second similarity vector; construct a focus loss function based on the theme and intent consistency classification results and the first similarity vector based on the similarity labels of the sample pairs in the training data, construct a distillation loss function based on the output results of the student model and the output results of the teacher model, and construct a total loss function based on the focus loss function and the distillation loss function. The knowledge distillation from the teacher model to the student model is completed by using the total loss function, and a trained student model is obtained. Figure 1 Figure 1
[0117] The semantic matching module 3 is configured to obtain a customer question to be matched and perform preliminary retrieval on it by using a medical knowledge base to obtain a plurality of preliminary retrieval matching questions; input the customer question to be matched and each preliminary retrieval matching question into the trained student model to output a corresponding similarity vector, and determine the matching question with the highest semantic similarity with the customer question to be matched according to the similarity vectors corresponding to all preliminary retrieval matching questions and output the corresponding reply statement.
[0118] Figure 3 The hardware structure schematic diagram of the electronic device provided by the embodiment of the present application is shown in FIG. 1.Figure 3 The electronic device of the embodiment includes a processor 301 and a memory 302. The memory 302 is configured to store computer-executable instructions. The processor 301 is configured to execute the computer-executable instructions stored in the memory to implement each step performed by the electronic device in the above embodiment. Details can be referred to the related description in the method embodiment.
[0119] Optionally, the memory 302 can be independent or integrated with the processor 301.
[0120] When the memory 302 is independent, the electronic device further includes a bus 303 configured to connect the memory 302 and the processor 301.
[0121] The embodiment of the application further provides a computer storage medium, and the computer storage medium stores computer-executable instructions. When the processor 301 executes the computer-executable instructions, the method described above is implemented.
[0122] The embodiment of the application further provides a computer program product, and the computer program product includes a computer program. When the computer program is executed by the processor 301, the method described above is implemented.
[0123] In the embodiments of the application, it should be understood that the disclosed devices and methods can be implemented in other manners. For example, the described device embodiments are merely schematic. For example, the division of the modules is merely a logical function division. There can be another division manner for the actual implementation. For example, a plurality of modules or features can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the displayed or discussed mutual couplings or direct couplings or communication connections can be indirect couplings or communication connections through some interfaces, devices or modules, and can be electrical, mechanical or other forms.
[0124] The modules illustrated as separate components can or can not be physical separate, and the components illustrated as modules can or can not be physical units, i.e., can be located in one place, or can be distributed on a plurality of network units. Some or all of the modules can be selected according to actual needs to implement the embodiments of the application.
[0125] In addition, each functional module in each embodiment of the application can be integrated in a processing unit, or each module can be a physical unit, or two or more modules can be integrated in one unit. The units formed by the above modules can be implemented in the form of hardware, or in the form of hardware plus software function units.
[0126] The integrated modules realized in the form of software function modules can be stored in a computer readable storage medium. The software function modules are stored in a storage medium and include a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) or the processor 301 to execute part of the steps of the various embodiments of the method.
[0127] It should be understood that the processor 301 can be a central processing unit (CPU), and can also be other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), etc. The general-purpose processor can be a microprocessor or the processor 301 can also be any conventional processor 301, etc. The steps of the method disclosed in combination with the application can be directly embodied as the execution of the processor 301 in hardware, or be executed by a combination of hardware and software modules in the processor 301.
[0128] The memory 302 can include a high-speed RAM memory, and can also include a non-volatile storage NVM, for example at least one disk memory, and can also be a U disk, a mobile hard disk, a read-only memory, a magnetic disk or an optical disk, etc.
[0129] The bus 303 can be an industry standard architecture (ISA), a peripheral component interconnect (PCI) bus, or an extended industry standard architecture (EISA) bus, etc. The bus 303 can be divided into an address bus, a data bus, a control bus, etc. For the convenience of representation, the bus 303 in the drawings of the present application does not limit only one bus 303 or one type of bus 303.
[0130] The storage medium can be realized by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk. The storage medium can be any available medium that can be accessed by a general-purpose or special-purpose computer.
[0131] An example storage medium is coupled to the processor 301 such that the processor 301 can read information from, and can write information to, the storage medium. Of course, the storage medium can be a part of the processor 301. Consistent with the teachings provided herein, the processor 301 can execute instructions stored in the storage medium or it can be dedicated in hardware to perform its functions. The storage medium can include one or more machine-readable storage media involving any type of storage hardware. For example, the storage medium can include any type of tangible memory, such as volatile or non-volatile memory including dynamic random access memory (DRAM), static random access memory (SRAM), erasable programmable read only memory (EPROM), electrically erasable programmable read only memory (EEPROM), flash memory, or a combination of such memories. The storage medium can also include non-tangible machine-readable storage media, such as a communication network.
[0132] Those skilled in the art can understand that all or part of the steps of the above-mentioned method embodiments can be completed by program instruction related hardware. The foregoing program can be stored in a computer readable storage medium. The program executes the steps of the above-mentioned method embodiments when executed; and the foregoing storage medium includes ROM, RAM, magnetic disc or optical disc and various storage media that can store program codes.
[0133] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, and are not intended to limit the present application; although the present application has been described in detail with reference to the above embodiments, those skilled in the art should understand that they can still modify the technical solutions recorded in the above embodiments, or make equivalent replacement for part or all of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A medical knowledge base semantic matching method for positive and negative sample imbalance, characterized in that, The method comprises the following steps: Obtaining search words and customer questions in a medical scenario dialogue and forming a query set, combining matching questions in a medical knowledge base to construct candidate sample pairs, screening and labeling the candidate sample pairs, and adjusting the proportion of positive sample pairs and negative sample pairs to obtain training data; Respectively extracting the theme and intent of the sample pairs in the training data, classifying the sample pairs in the training data according to the theme and intent to obtain a theme and intent consistency classification result; Inputting the sample pairs in the training data into a student model to obtain a first projection vector and a first similarity vector, inputting the sample pairs in the training data into a teacher model to obtain a second projection vector and a second similarity vector, constructing a focus loss function based on theme and intent consistency constraints according to the similarity labels of the sample pairs in the training data, the theme and intent consistency classification result, and the first similarity vector, constructing a distillation loss function based on the output results of the student model and the output results of the teacher model, constructing a total loss function based on the focus loss function and the distillation loss function, and completing knowledge distillation from the teacher model to the student model by using the total loss function to obtain a trained student model; Obtaining a customer question to be matched and performing preliminary retrieval on it by using the medical knowledge base to obtain a plurality of preliminary retrieval matching questions; inputting the customer question to be matched and each preliminary retrieval matching question into the trained student model to output a corresponding similarity vector, and determining a matching question with the highest semantic similarity to the customer question to be matched according to the similarity vectors corresponding to all preliminary retrieval matching questions and outputting a corresponding reply sentence.
2. The medical knowledge base semantic matching method for positive and negative sample imbalance according to claim 1, characterized in that, Obtaining search words and customer questions in a medical scenario dialogue and forming a query set, combining matching questions in a medical knowledge base to construct candidate sample pairs, screening and labeling the candidate sample pairs, and adjusting the proportion of positive sample pairs and negative sample pairs to obtain training data, specifically comprising: Using a BGE encoder to perform vectorization processing on each customer question in the query set to obtain a corresponding text vector, and using a DBSCAN clustering algorithm to perform deduplication processing on all customer questions in the query set according to the text vector to obtain a deduplicated query set; Artificially cleaning the medical knowledge base and performing vectorization processing using a BGE encoder to construct a vector library; Using an approximate nearest neighbor algorithm to retrieve matching questions with a semantic similarity of the top N in the vector library from query samples in the deduplicated query set and forming a candidate sample pair set with the corresponding query samples; Constructing a prompt word for judging semantic adaptability, using a large language model to judge the semantic adaptability of each candidate sample pair in the candidate sample pair set in combination with the prompt word for judging semantic adaptability, and outputting the label of each candidate sample pair, retaining all candidate sample pairs with similar labels and part of the candidate sample pairs with dissimilar labels; The BGE encoder and the DBSCAN clustering algorithm are used for semantic deduplication on all candidate sample pairs with similar labels and part of candidate sample pairs with dissimilar labels, and manual annotation is performed to obtain corresponding similarity labels.
3. The medical knowledge base semantic matching method for positive and negative sample imbalance according to claim 1, characterized in that, The focus loss function based on the theme intention consistency constraint is represented as: loss focal-consistency = -a(1 - p) γ1 β(1 - C) γ2 log(p); wherein, loss focal-consistency represents the focus loss function based on the theme-intent consistency constraint, a and β represent the first balance factor and the second smoothing factor, γ1 and γ2 represent the first modulation factor and the second adjustment factor respectively, p represents the probability of selecting the corresponding type in the first similarity vector according to the similarity label of the sample pair in the training data, C represents the theme-intent consistency coefficient determined according to the theme-intent consistency classification result, and is represented as:
4. The medical knowledge base semantic matching method for positive and negative sample imbalance according to claim 1, characterized in that, The sample pairs in the training data are input into the student model to obtain first projection vectors and first similarity vectors, and the sample pairs in the training data are input into the teacher model to obtain second projection vectors and second similarity vectors, specifically including: The student model includes a first embedding layer and a first feature extraction layer connected in sequence, and a first feature projection layer and a first similarity prediction module arranged in parallel, the sample pairs in the training data are input into the student model, and the corresponding first embedding vectors are extracted through the first embedding layer, the first embedding vectors are input into the first feature extraction layer to obtain first feature vectors, and the first feature vectors are input into the first feature projection layer and the first similarity prediction module respectively to obtain first projection vectors and first similarity vectors, as shown in the following formula: h s-proj = project_linear(h s ); p ss = softmax(mlp(h s )); wherein h s represents the first feature vector, h s-proj represents the first projection vector, project_linear represents a linear layer, p ss represents the first similarity vector, which has a dimension of 2, corresponding to the probability of similarity and the probability of dissimilarity, respectively; mlp represents a multi-layer perception, and softmax represents a softmax function; The teacher model includes a second embedding layer and a second feature extraction layer connected in sequence, and a second feature projection layer and a second similarity prediction module arranged in parallel, the sample pairs in the training data are input into the student model, and the corresponding second embedding vectors are extracted through the second embedding layer, the second embedding vectors are input into the second feature extraction layer to obtain second feature vectors, and the second feature vectors are input into the second feature projection layer and the second similarity prediction module respectively to obtain second projection vectors and second similarity vectors, as shown in the following formula: h t-proj = project_linear(h t ); p ts = softmax(mlp(h t )) ; where h t represents the second feature vector, h t-proj represents the second projection vector, p ts represents the second similarity vector, which has a dimension of 2, corresponding to the probability of similarity and the probability of dissimilarity, respectively.
5. The medical knowledge base semantic matching method for positive and negative sample imbalance according to claim 4, characterized in that, A distillation loss function is constructed based on the student model and the teacher model, specifically including: A cross-entropy loss function is constructed according to the first projection feature and the second projection feature and is used as a first distillation loss function, as shown in the following formula: loss hidden = MSE(h t-proj ,h s-proj ); wherein loss hidden represents a first distillation loss function, and MSE represents a cross-entropy loss function. A cross-entropy loss function is constructed according to the first similarity vector and the second similarity vector and is used as a second distillation loss function, as shown in the following formula: loss s_logits = MSE(p ts , p ss ); where loss s_logits denotes the second distillation loss function.
6. The medical knowledge base semantic matching method for positive and negative sample imbalance according to claim 1, characterized in that, The total loss function is represented as: loss total = h1loss hidden + μ2loss s_logits + μ3loss focal-consistency ; wherein loss total denotes the total loss function, loss focal-consistency denotes the focus loss function based on the topic intention consistency constraint, loss s_logits denotes the second distillation loss function, loss hidden denotes the first distillation loss function, and μ1, μ2, and μ3 respectively denote corresponding weight coefficients.
7. A medical knowledge base semantic matching device for positive and negative sample imbalance, comprising: including: The training data construction module is configured to obtain search words and customer sentences in medical scene dialogues and form a query set, construct candidate sample pairs in combination with matching sentences in a medical knowledge base, screen and label the candidate sample pairs, and adjust the proportion of positive sample pairs and negative sample pairs to obtain training data; The theme and intention of the sample pairs in the training data are extracted respectively, and the sample pairs in the training data are classified according to the theme and intention to obtain theme and intention consistency classification results; The knowledge distillation module is configured to input a sample pair in the training data into a student model to obtain a first projection vector and a first similarity vector, input the sample pair in the training data into a teacher model to obtain a second projection vector and a second similarity vector, construct a focal loss function based on theme-intent consistency constraint according to a similarity label of the sample pair in the training data, a theme-intent consistency classification result, and the first similarity vector, construct a distillation loss function based on an output result of the student model and an output result of the teacher model, construct a total loss function based on the focal loss function and the distillation loss function, complete knowledge distillation from the teacher model to the student model by using the total loss function, and obtain a trained student model; The semantic matching module is configured to acquire a client question to be matched, perform preliminary retrieval on the client question to be matched by using the medical knowledge base, and obtain a plurality of pieces of preliminary retrieval matching questions; input the client question to be matched and each piece of preliminary retrieval matching question into the trained student model, output a corresponding similarity vector, determine a matching question with the highest semantic similarity to the client question to be matched according to similarity vectors corresponding to all pieces of preliminary retrieval matching question, and output a corresponding reply sentence. 8.An electronic device, comprising: one or more processors; storage configured to store one or more programs, when the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1-6.
9. A computer-readable storage medium having stored thereon a computer program, characterized in that, The program is executed by the processor to implement the method according to any one of claims 1-6.
10. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to implement the method according to any one of claims 1-6.
Citation Information
Patent Citations
Training method of medical semantic matching model and medical knowledge matching method and device
CN113780006A
Model distillation method and system and text retrieval method
CN114328834A
Knowledge distillation-based target detection model training method
CN119295739A
Multi-granularity fusion model-based intelligent semantic chinese sentence matching method, and device
WO2021164199A1