A zero-shot classification method for medical entities based on language model
Through the zero-sample classification method of medical entities based on the language model, the BERT model is used to construct cloze templates and predicted probability values, which solves the problem of dependence on annotated corpus in the existing technology and achieves efficient medical entity classification, reaching a classification of 85.60 Effect.
Patent Information
- Application Number
- CN202110063300.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-18
- Publication Date
- 2024-10-15
- Estimated Expiration
- 2041-01-18
AI Technical Summary
Existing medical entity classification methods require a large amount of annotated corpus for training, and the annotation process is time-consuming and laborious, affecting the accuracy of the model and difficult to meet the needs of entity recognition in medical texts.
A zero-sample classification method for medical entities based on language models is adopted, a cloze template is constructed through the pre-trained BERT model, the entity classification problem is converted into a cloze problem, and the pre-trained model is used to predict the probability value for entity classification, avoiding the need for labeling data. dependence.
It achieves medical entity classification without the need to label training data, improving classification efficiency and accuracy. The Precision, Recall and F-score on the CCKS2019 data set reached 85.60.
Smart Images

Figure CN112686044B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to an entity classification technology, and in particular to a medical entity zero-sample classification method based on a language model. Background Art
[0002] Information extraction in the medical and health field is closely related to people's lives. With the development of science and technology, the number of medical texts has increased dramatically. Human power alone is far from meeting the needs of medical entity identification. Therefore, the classification of medical entities in medical texts has become a hot issue in the current medical and health field. Medical entity classification is an important step in understanding language. The quality of medical entity classification results has a great impact on the results of subsequent natural language processing tasks such as question-answering systems. The goal of the medical entity classification task is to classify an entity into a predefined classification system given the context of an entity and the medical text in which it is located.
[0003] With the significant breakthroughs in deep learning technology in recent years, related technologies have been widely used in the field of natural language processing (NLP), such as BiLSTM+CRF, BERT, etc. However, most entity classification problems currently require a large amount of training corpus to train the model, and the quality of the corpus annotation and the amount of corpus greatly affect the accuracy of the trained model. On the other hand, the expected annotation is also a time-consuming and laborious task. Summary of the invention
[0004] In view of the shortcomings of entity classification schemes in the prior art, the present invention provides a medical entity zero-sample classification method based on a language model, which only requires training a BERT model in the medical field to achieve zero-sample entity classification without the need for labeled entity classification training corpus.
[0005] The present invention adopts the following technical solutions:
[0006] A zero-shot classification method for medical entities based on a language model comprises the following steps:
[0007] S1: Use the pre-training corpus to train the BERT model to obtain the pre-trained medical BERT model;
[0008] S2: construct corresponding cloze templates according to the categories of entities to be identified;
[0009] S3: Generate a cloze question for each entity in each sentence in the entity classification problem test set using the constructed cloze template;
[0010] S4: Use the pre-trained medical BERT model to predict the probability value of each cloze problem, and take the category with the largest probability value as the category corresponding to the entity.
[0011] Specifically, the pre-training medical BERT model in S1 includes the following steps:
[0012] S11: Prepare the corpus and corresponding dictionary required for pre-training the BERT model, read each file, perform regular sentence segmentation on each article, and store them in the texts list. For each text in texts, use Jieba word segmentation to obtain the word list of word segmentation results, and generate a random number between 0 and 1 for each word. Traverse all words, use the BERT word segmenter to divide the word into smaller characters according to the characters in the dictionary, namely: word_tokens, and obtain the word_token_ids in the corresponding dictionary, and append the word_token_ids to the token_ids of the single sentence. Set mask_rate = 0.15, and compare the rand value of the word with the size of mask_rate. If the rand value is greater than or equal to mask_rate, it means that the current word is not masked, and the word_mask_ids corresponding to each character is 0; if the rand value is less than mask_rate, each character is processed in turn, replaced with [MASK] with an 80% probability, kept unchanged with a 10% probability, and replaced with a random token with a 10% probability to obtain token_ids and mask_ids. Take the first 510 characters of token_ids and mask_ids at most, and then add [CLS] and [SEP] at the beginning and end. If the sum of the length of the current token_ids and instance is greater than 512, it is truncated. Otherwise, padding is performed to a length of 512 and put into instances. Write the processed token_ids and instance data into tfrecord;
[0013] S12: Build the model input interface, ID sequence: ids, sentence sequence segments_ids, position sequence: pos, mask matrix: mask (where mask is represented by all 1s);
[0014] S13: Construct the word embedding layer token_emb, the position embedding layer pos_emb, and the sentence embedding layer segments_emb, respectively, through the word vector matrix W token , position vector matrix W pos , sentence type vector matrix W segments, convert the corresponding sequence into the corresponding feature matrix;
[0015] S14: Constructing a multi-head attention layer with 12 layers of 2D mask matrix multi_head_2D_mask i , the formula is:
[0016]
[0017]
[0018] S15: Construct the reverse embedding layer reverse_emb to convert the feature matrix into the probability distribution of the corresponding words. The specific formula is:
[0019]
[0020] S16: According to the input interface ids, segments_ids, pos, mask, and token_emb, pos_emb, segments_emb, multi_head_2D_mask i , reverse_emb, build the model calculation graph in turn, and calculate the probability distribution p_pred of the output word sequence:
[0021] emb=ln(add(token emb(ids) ,pos_emb(pos),mask_emb(mask)))
[0022]
[0023]
[0024]
[0025]
[0026] S17: According to the constructed ids, segments_ids, pos, and mask, the model is input to calculate the actual output p_pred of the model. According to the constructed output and the p_pred, the loss of the model for the current batch of data is calculated by cross entropy. The model batch_size is set to 4069, the learning_rate is set to 0.00176, and the weight_decay_rate is set to 0.01. The parameters in the model are derived to obtain the gradients. The Adam optimizer is used to update the parameters in the model to obtain the pre-trained model.
[0027] Specifically, the process of constructing a cloze template of the category of the entity to be identified in step S2 includes the following steps:
[0028] S21: construct a category list list of entities to be identified;
[0029] S22: Definition cloze question:
[0030] P(W|S mask )
[0031] Where W represents a classification category, S mask It represents a text with a MASK bit, P(W|S mask ) means to fill W into S mask The probability of the MASK position;
[0032] S23: constructing a cloze template with context and without context for each category in the category list list, wherein the cloze template includes a text [text] of the entity to be identified, the entity to be identified [entity] and the category [type] of the entity to be identified;
[0033] Among them, the template with context relationship is expressed as:
[0034] S1(text,entity,type)=[text], [entity] in the above text is a kind of [mask],
[0035] W1(type)=[type]
[0036] The template without context is represented as:
[0037] S2(entity,type)=[entity] is a type of [mask].
[0038] W2(type)=[type].
[0039] Specifically, the process of using the cloze template to generate the cloze question in step S3 includes the following steps:
[0040] S31: Take each sentence in the entity classification problem test set as text, then mark all entities in the sentence and the position of the entity in the sentence, and get the original data for training and testing, the format of which is:
[0041] {text:some text,entities[type,start pos ,end pos ,entity]}
[0042] Among them, text represents a sentence, type represents the category of the entity, and start pos Indicates the starting position of the entity in the sentence, end pos Indicates the end position of the entity in the sentence, and entity indicates the entity to be identified, where a sentence may contain multiple entities;
[0043] S32: For each entity in each sentence text in the S31 data, a cloze question is generated using the constructed cloze template, wherein N cloze questions are generated for each entity, where N is the number of categories of the pre-constructed entity.
[0044] Specifically, the process of predicting the probability of the cloze test question in step S4 includes the following steps:
[0045] S41: Define the classification model, the model architecture is the BERT model architecture in S1;
[0046] S42: serialize the cloze questions in S3 and convert them into id representation;
[0047] S43: construct input token_ids=[CLS]+[x] n +[SEP]+[0] s_max-n-2 , where [CLS] is the ID corresponding to the start symbol, [x] n is the Token ID sequence corresponding to the serialized medical text, [SEP] is the ID corresponding to the end symbol, [0] s_max-n-2 It is used to fill the variable-length medical text sequence into a fixed-length sequence by adding 0. s_max is the input sequence length, and the sentence sequence segment_ids = [0, ..., 0] s_max , the sentence sequence is a vector of s_max dimension, the type sequence is used to distinguish two sentences input into the model at the same time, the feature value of the first sentence is set to 0, the feature value of the second sentence is set to 1, and the sentence types are all set to 0;
[0048] Output sequence output_ids = [0,...entity1,...,entity n 0] s_max , the output sequence is a vector of s_max dimensions, where entity n Indicates the entity category corresponding to the id;
[0049] S44: token_ids, segment_ids and output_idss are used as input to the model.
[0050] The model output is the probability of each character in the cloze test question:
[0051] P(x i |x i ∈S)
[0052] Among them, S represents the input sentence, x i A character in the representation;
[0053] S46: Convert this probability sequence into the probability value of the cloze output, and the conversion formula is:
[0054]
[0055] S47: N cloze questions are constructed for the same entity, and these N cloze questions are input into the model. The model outputs N probability values, and finally the category corresponding to the cloze question with the largest probability value is taken as the final category corresponding to the entity.
[0056] S44: In S3, N cloze questions are constructed for the same entity, where N is the number of categories of the entity constructed in advance. These N cloze questions are input into the model, and the model outputs N probability values. Finally, the category corresponding to the cloze question with the largest probability value is taken as the final category corresponding to the entity.
[0057] Compared with the prior art, the present invention has the following advantages or beneficial effects: the entity classification problem is converted into a cloze problem by constructing a natural language query, and the classification problem is converted into a cloze problem. On the other hand, there is no need to annotate training data or train a model, and entity classification can be achieved by only constructing a cloze problem corresponding to the entity classification. The method achieves Precision of 85.60, Recall of 85.60, and F-score of 85.60 on the CCKS2019 dataset. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] After reading the detailed description of the present invention with reference to the accompanying drawings, the readers will understand various aspects of the present invention more clearly.
[0059] Figure 1 A flowchart of a medical entity zero-sample classification method based on a language model according to one embodiment of the present invention is shown. DETAILED DESCRIPTION
[0060] In order to make the technical content disclosed in this application more detailed and complete, reference may be made to the accompanying drawings and the following various specific embodiments of the present invention, in which the same reference numerals represent the same or similar components. However, it should be understood by those skilled in the art that the embodiments provided below are not intended to limit the scope of the present invention. In addition, the accompanying drawings are only used for schematic illustration and are not drawn according to their original size.
[0061] The specific implementation modes of various aspects of the present invention are further described in detail below with reference to the accompanying drawings.
[0062] Figure 1 According to one embodiment of the present invention, a medical entity zero-sample classification method based on a language model is shown, comprising the following steps:
[0063] S1: Use the pre-training corpus to train the BERT model to obtain the pre-trained medical BERT model;
[0064] S2: construct corresponding cloze templates according to the categories of entities to be identified;
[0065] S3: Generate a cloze question for each entity in each sentence in the entity classification problem test set using the constructed cloze template;
[0066] S4: Use the pre-trained medical BERT model to predict the probability value of each cloze problem, and take the category with the largest probability value as the category corresponding to the entity.
[0067] Specifically, the pre-training medical BERT model in S1 includes the following steps:
[0068] S11: Prepare the corpus and corresponding dictionary required for pre-training the BERT model, read each file, perform regular sentence segmentation on each article, and store them in the texts list. For each text in texts, use Jieba word segmentation to obtain the word list of word segmentation results, and generate a random number between 0 and 1 for each word. Traverse all words, use the BERT word segmenter to divide the word into smaller characters according to the characters in the dictionary, namely: word_tokens, and obtain the word_token_ids in the corresponding dictionary, and append the word_token_ids to the token_ids of the single sentence. Set mask_rate = 0.15, and compare the rand value of the word with the size of mask_rate. If the rand value is greater than or equal to mask_rate, it means that the current word is not masked, and the word_mask_ids corresponding to each character is 0; if the rand value is less than mask_rate, each character is processed in turn, replaced with [MASK] with an 80% probability, kept unchanged with a 10% probability, and replaced with a random token with a 10% probability to obtain token_ids and mask_ids. Take the first 510 characters of token_ids and mask_ids at most, and then add [CLS] and [SEP] at the beginning and end. If the sum of the length of the current token_ids and instance is greater than 512, it is truncated. Otherwise, padding is performed to a length of 512 and put into instances. Write the processed token_ids and instance data into tfrecord;
[0069] S12: Build the model input interface, ID sequence: ids, sentence sequence segments_ids, position sequence: pos, mask matrix: mask (where mask is represented by all 1s);
[0070] S13: Construct the word embedding layer token_emb, the position embedding layer pos_emb, and the sentence embedding layer segments_emb, respectively, through the word vector matrix W token , position vector matrix W pos , sentence type vector matrix W segments , convert the corresponding sequence into the corresponding feature matrix;
[0071] S14: Constructing a multi-head attention layer with 12 layers of 2D mask matrix multi_head_2D_mask i , the formula is:
[0072]
[0073]
[0074] S15: Construct the reverse embedding layer reverse_emb to convert the feature matrix into the probability distribution of the corresponding words. The specific formula is:
[0075]
[0076] S16: According to the input interface ids, segments_ids, pos, mask, and token_emb, pos_emb, segments_emb, multi_head_2D_mask i , reverse_emb, build the model calculation graph in turn, and calculate the probability distribution p_pred of the output word sequence:
[0077] emb=ln(add(token emb(ids) ,pos_emb(pos),mask_emb(mask)))
[0078]
[0079]
[0080]
[0081]
[0082] S17: According to the constructed ids, segments_ids, pos, and mask, the model is input to calculate the actual output p_pred of the model. According to the constructed output and the p_pred, the loss of the model for the current batch of data is calculated by cross entropy. The model batch_size is set to 4069, the learning_rate is set to 0.00176, and the weight_decay_rate is set to 0.01. The parameters in the model are derived to obtain the gradients. The Adam optimizer is used to update the parameters in the model to obtain the pre-trained model.
[0083] Specifically, the process of constructing a cloze template of the category of the entity to be identified in step S2 includes the following steps:
[0084] S21: construct a category list list of entities to be identified;
[0085] S22: Definition cloze question:
[0086] P(W|S mask )
[0087] Where W represents a classification category, S mask It represents a text with a MASK bit, P(W|S mask ) means to fill W into S mask The probability of the MASK position;
[0088] S23: constructing a cloze template with context and without context for each category in the category list list, wherein the cloze template includes a text [text] of the entity to be identified, the entity to be identified [entity] and the category [type] of the entity to be identified;
[0089] Among them, the template with context relationship is expressed as:
[0090] S1(text,entity,type)=[text], [entity] in the above text is a kind of [mask],
[0091] W1(type)=[type]
[0092] The template without context is represented as:
[0093] S2(entity,type)=[entity] is a type of [mask].
[0094] W2(type)=[type].
[0095] Specifically, the process of using the cloze template to generate the cloze question in step S3 includes the following steps:
[0096] S31: Take each sentence in the entity classification problem test set as text, then mark all entities in the sentence and the position of the entity in the sentence, and get the original data for training and testing, the format of which is:
[0097] {text:some text,entities[type,start pos ,end pos ,entity]}
[0098] Among them, text represents a sentence, type represents the category of the entity, and start pos Indicates the starting position of the entity in the sentence, end pos Indicates the end position of the entity in the sentence, and entity indicates the entity to be identified, where a sentence may contain multiple entities;
[0099] S32: For each entity in each sentence text in the S31 data, a cloze question is generated using the constructed cloze template, wherein N cloze questions are generated for each entity, where N is the number of categories of the pre-constructed entity.
[0100] Specifically, the process of predicting the probability of the cloze test question in step S4 includes the following steps:
[0101] S41: Define the classification model, the model architecture is the BERT model architecture in S1;
[0102] S42: serialize the cloze questions in S3 and convert them into id representation;
[0103] S43: construct input token_ids=[CLS]+[x] n +[SEP]+[0] s_max-n-2 , where [CLS] is the ID corresponding to the start symbol, [x] n is the Token ID sequence corresponding to the serialized medical text, [SEP] is the ID corresponding to the end symbol, [0] s_max-n-2 It is used to fill the variable-length medical text sequence into a fixed-length sequence by adding 0. s_max is the input sequence length, and the sentence sequence segment_ids = [0, ..., 0] s_max , the sentence sequence is a vector of s_max dimension, the type sequence is used to distinguish two sentences input into the model at the same time, the feature value of the first sentence is set to 0, the feature value of the second sentence is set to 1, and the sentence types are all set to 0;
[0104] Output sequence output_ids = [0,...entity1,...,entity n 0] s_max , the output sequence is a vector of s_max dimensions, where entity n Indicates the entity category corresponding to the id;
[0105] S44: token_ids, segment_ids and output_idss are used as input to the model.
[0106] The model output is the probability of each character in the cloze test question:
[0107] P(x i |x i ∈S)
[0108] Among them, S represents the input sentence, x i A character in the representation;
[0109] S46: Convert this probability sequence into the probability value of the cloze output, and the conversion formula is:
[0110]
[0111] S47: N cloze questions are constructed for the same entity, and these N cloze questions are input into the model. The model outputs N probability values, and finally the category corresponding to the cloze question with the largest probability value is taken as the final category corresponding to the entity.
[0112] S44: In S3, N cloze questions are constructed for the same entity, where N is the number of categories of the entity constructed in advance. These N cloze questions are input into the model, and the model outputs N probability values. Finally, the category corresponding to the cloze question with the largest probability value is taken as the final category corresponding to the entity.
[0113] In summary, the present invention proposes a zero-sample classification method for medical entities based on a language model, which converts the entity classification problem into a cloze problem by constructing a natural language query. The language model is used to predict the probability of the answer to the cloze problem, and the answer to the cloze problem is obtained as the entity category.
[0114] The above contents are only for explaining the technical idea of the present invention and cannot be used to limit the protection scope of the present invention. Any changes made on the basis of the technical solution in accordance with the technical idea proposed by the present invention shall fall within the protection scope of the claims of the present invention.
Claims
1. A zero-shot classification method for medical entities based on language model, It is characterized in that The steps include: S1: Use the pre-training corpus to train the BERT model to obtain the pre-trained medical BERT model; S2: Construct a corresponding cloze template according to the category of the entity to be identified; In step S2, the process of constructing a cloze template for the category of the entity to be identified includes the following steps: S21: construct a category list list of entities to be identified; S22: Definition cloze question: P(W|S mask ) Where W represents a classification category, S mask It represents a text with a MASK bit, P(W|S mask ) means to fill W into S mask The probability of the MASK position; S23: constructing a cloze template with context and without context for each category in the category list list, wherein the cloze template includes a text [text] of the entity to be identified, the entity to be identified [entity] and the category [type] of the entity to be identified; Among them, the template with context relationship is expressed as: S1(text, entity, type) = [text], where [entity] is a type of [mask], W1(type)=[type] The template without context is represented as: S2(entity, type) = [entity] is a type of [mask], W2(type) = [type]; S3: Generate a cloze question for each entity in each sentence in the entity classification problem test set using the constructed cloze template; The step S3 comprises the following steps: S31: Take each sentence in the entity classification problem test set as text, then mark all entities in the sentence and the position of the entity in the sentence, and get the original data for training and testing, the format of which is: {text:some text,entities[type,start pos ,end pos ,entity]} Among them, text represents a sentence, type represents the category of the entity, and start pos Indicates the starting position of the entity in the sentence, end pos Indicates the end position of the entity in the sentence, and entity indicates the entity to be identified, where a sentence may contain multiple entities; S32: for each entity in each sentence text in the S31 data, using the constructed cloze template to generate a cloze question, wherein each entity needs to generate N cloze questions, where N is the number of categories of the pre-constructed entity; S4: Use the pre-trained medical BERT model to predict the probability value of each cloze problem, and take the category with the largest probability value as the category corresponding to the entity.
2. The medical entity zero-sample classification method based on a language model as claimed in claim 1, It is characterized in that In step S1, pre-training the medical BERT model includes the following steps: S11: Prepare the corpus and corresponding dictionary required for pre-training BERT model, read each file, perform regular sentence segmentation on each article and store them in the texts list, use Jieba word segmentation to segment each text in texts, get the word list of segmentation results, generate a random number between 0 and 1 for each word, traverse all words, use Bert word segmenter to divide the word into smaller characters according to the characters in the dictionary, namely: word_tokens, and get the word_token_ids in the corresponding dictionary, and append word_token_ids to token_ids; set mask_rate = 0.15, compare the rand value of the word with the size of mask_rate, if the rand value is greater than or equal to mask_rate, it means If the current word is not masked, the word_mask_ids corresponding to each character is 0; if the rand value is less than mask_rate, process each character in turn, replace it with [MASK] with an 80% probability, keep it unchanged with a 10% probability, and replace it with a random token with a 10% probability to obtain token_ids and mask_ids. Take the first 510 characters of token_ids and mask_ids at most, and then add [CLS] and [SEP] at the beginning and end. If the sum of the length of the current token_ids and instance is greater than 512, it is truncated. Otherwise, padding is performed to a length of 512 and put into instances; write the processed token_ids and instance data into tfrecord; S12: Build the model input interface, ID sequence: ids, sentence sequence segments_ids, position sequence: pos, mask matrix: mask, where mask is represented by all 1s; S13: Construct the word embedding layer token_emb, the position embedding layer pos_emb, and the sentence embedding layer segments_emb, respectively, through the word vector matrix W token , position vector matrix W pos , sentence type vector matrix W segments , convert the corresponding sequence into the corresponding feature matrix; S14: Constructing a multi-head attention layer with 12 layers of 2D mask matrix multi_head_2D_mask i , the formula is: S15: Construct the reverse embedding layer reverse_emb to convert the feature matrix into the probability distribution of the corresponding words. The specific formula is: S16: According to the input interface ids, segments_ids, pos, mask, and token_emb, pos_emb, segments_emb, multi_head_2D_mask i , reverse_emb, build the model calculation graph in turn, and calculate the probability distribution p_pred of the output word sequence: emb=ln(add(token_emb(ids), pos_emb(pos), mask_emb(mask))) S17: According to the constructed ids, segments_ids, pos, and mask, the model is input to calculate the actual output p_pred of the model. According to the constructed output and the p_pred, the loss of the model for the current batch of data is calculated by cross entropy. The model batch_size is set to 4069, the learning_rate is set to 0.00176, and the weight_decay_rate is set to 0.
01. The parameters in the model are derived to obtain the gradients. The Adam optimizer is used to update the parameters in the model to obtain the pre-trained model.
3. The medical entity zero-sample classification method based on a language model as claimed in claim 1, It is characterized in that The step S4 comprises the following steps: S41: define the classification model, the model architecture is the BERT model architecture in step S1; S42: Serialize the cloze test questions in step S3 and convert them into id representation; S43: construct input token_ids=[CLS]+[x] n +[SEP]+[0] s_max-n-2 , where [CLS] is the ID corresponding to the start symbol, [x] n is the Token ID sequence corresponding to the serialized medical text, [SEP] is the ID corresponding to the end symbol, [0] s_max-n-2 It is used to fill the variable-length medical text sequence into a fixed-length sequence by adding 0. s_max is the input sequence length, and the sentence sequence segment_ids = [0, ..., 0] s_max , the sentence sequence is a vector of s_max dimension, the type sequence is used to distinguish two sentences input into the model at the same time, the feature value of the first sentence is set to 0, the feature value of the second sentence is set to 1, and the sentence types are all set to 0; Output sequence output_ids = [0, entity 1 , ..., entity n ,0] s_max , the output sequence is a vector of s_max dimensions, where entity n Indicates the entity category corresponding to the id; S44: token_ids, segment_ids and output_ids are used as inputs to the model. The model output is the probability of each character in the cloze test question: P(x i |x i ∈S) Among them, S represents the input sentence, x i Represents a character in a sentence; S46: Convert this probability sequence into the probability value of the cloze output, and the conversion formula is: S47: N cloze questions are constructed for the same entity, and these N cloze questions are input into the model. The model outputs N probability values, and finally the category corresponding to the cloze question with the largest probability value is taken as the final category corresponding to the entity.
