Named entity recognition model training method and device and storage medium
By introducing the weight matrix and attention mechanism of label position information into the named entity recognition model, the problem of difficulty in distinguishing tokens in different positions of the same named entity type is solved, and the accuracy and performance of named entity recognition are improved.
Patent Information
- Application Number
- CN202310362781.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-06
- Publication Date
- 2024-10-15
AI Technical Summary
Existing named entity recognition models perform poorly in distinguishing tokens of the same named entity type but in different positions, resulting in insufficient recognition performance.
By introducing the position information of the token corresponding to the label in the named entity, a weight matrix is constructed and combined with the pre-trained language model and attention mechanism to generate the final vector representation to optimize the model output.
The recognition accuracy of the named entity recognition model for tokens in different locations is improved, and the overall recognition performance of the model is improved.
Smart Images

Figure CN118780285A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of machine learning and natural language processing (NLP), and in particular to a training method, device and storage medium for a named entity recognition (NER) model. Background Art
[0002] Named entity recognition (also known as entity identification, entity chunking, and entity extraction) is a basic task in NLP and an important basic tool for many NLP tasks such as information extraction, question-answering systems, syntactic analysis, and machine translation. Named entity recognition (also known as entity recognition, entity chunking, and entity extraction) aims to locate and classify named entities in text into predefined entity types, such as names of people, names of organizations, names of places, time expressions, quantities, monetary values, percentages, etc.
[0003] At present, the named entity recognition method based on sequence tagging uses a pre-trained language model (such as the BERT model) as the underlying text feature encoder to encode and represent the token sequence of the text, and then uses a fully connected layer to classify each token in the token sequence of the text, and finally uses Softmax or Conditional Random Field (CRF) to make the final label judgment.
[0004] For traditional named entity recognition tasks, named entity recognition models usually do not know what the specific labels they output are, nor what the labels mean. The model simply outputs the training text as numbers, which are pre-set for each label. In order to improve the recognition performance of named entity recognition models for named entities, the Label Knowledge Enhanced Representation (LER) model introduces label annotation information to describe the labels. For example, for the label "name", its label annotation information can be: a person's title, a word that others use to call you. Through the attention mechanism, each word in the training text obtains the meaning of each label, and then the model decides which label to output. Compared with traditional named entity recognition models (such as BERT-MRC), the LEAR model has a certain improvement in performance.
[0005] However, the LEAR model describes the label itself. For the named entity task based on sequence labeling, for example, taking the BIO label as an example, although the label starting with "B-" and the label starting with "I-" belong to the same category, the label meanings they express are different, and the method in the LEAR model cannot distinguish them well. For example, "B-name" and "I-name" only have the same processing method for "name" in the LEAR model, but there is still a positional difference between the two: "B-name" represents the first word of "name", and "I-name" represents the subsequent word of "name". Therefore, it is necessary to further improve the above method to improve the performance of the named entity recognition model. Summary of the invention
[0006] At least one embodiment of the present application provides a method, device and storage medium for training a named entity recognition model, which can improve the performance of the trained named entity recognition model.
[0007] In order to solve the above technical problems, this application is implemented as follows:
[0008] In a first aspect, an embodiment of the present application provides a training method for a named entity recognition model, wherein the named entity recognition model includes an encoder and a decoder, wherein the encoder includes a pre-trained language model and an attention mechanism model, and the training method includes:
[0009] Acquire a plurality of training texts with annotated labels, wherein the labels are used to annotate the named entity types to which tokens in the training texts belong; construct a label annotation for each label, wherein when the label is a label corresponding to a named entity, the label annotation includes a position indicating token, and the position indicating token is used to indicate the position of the token corresponding to the label in the named entity;
[0010] According to all the label annotations, a weight matrix is generated, wherein each row of the weight matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in turn, and the value of the element corresponding to the position indication token in the label annotation is k, and the value of the element corresponding to other tokens except the position indication token is 0, wherein k is a learnable parameter in the training process of the named entity recognition model;
[0011] Inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation;
[0012] Inputting the first vector representation of the training text and the first vector representation of the label annotation into the attention mechanism model, calculating the first relationship between the training text and the label annotation; weighting the first relationship using the weight matrix to obtain a second relationship, and generating a final vector representation of the training text based on the second relationship;
[0013] Inputting the final vector representation of the training text into the decoder, and obtaining a label corresponding to each token in the training text output by the decoder;
[0014] The named entity recognition model is optimized according to the label corresponding to each token in the training text output by the decoder and the labeled labels in the training text to obtain a trained named entity recognition model.
[0015] Optionally, generating a weight matrix includes:
[0016] Unify the number of tokens for all label annotations based on the maximum number of tokens in all label annotations;
[0017] Initialize an all-0 matrix, where each row in the all-0 matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in sequence;
[0018] The value of the element corresponding to the position indication token in the label annotation in the all-0 matrix is set to k to obtain the weight matrix, wherein the initial value of k is 1.
[0019] Optionally, inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation includes:
[0020] Inputting the training text and the label annotation into the pre-trained language model to obtain the ID of the training text and the ID of the label annotation represented by numerical values;
[0021] Based on the ID of the training text, a first vector representation of the training text is generated; based on the ID of the label annotation, a first vector representation of the label annotation is generated.
[0022] Optionally, calculating the first relationship between the training text and the label annotation includes:
[0023] The first vector representation of the training text is weighted by a first weight parameter to obtain a second vector representation of the training text; the first vector representation of the label annotation is weighted by a second weight parameter to obtain a second vector representation of the label annotation, wherein the first weight parameter and the second weight parameter are learnable parameters;
[0024] Based on the second vector representation of the training text and the second vector representation of the label annotation, a first relationship between the training text and the label annotation is calculated.
[0025] Optionally, the using the weight matrix to weight the first relationship to obtain the second relationship includes:
[0026] The weight matrix is dimensionally expanded so that the dimension of the expanded weight matrix is the same as the dimension of the first relationship; and the expanded weight matrix and the first relationship are added to obtain the second relationship.
[0027] Optionally, generating a final vector representation of the training text based on the second relationship includes:
[0028] Calculate a third vector representation of the training text according to the second relationship and the second vector representation of all label annotations, wherein the third vector representation of the training text is a vector representation at a token level;
[0029] Converting the third vector representation of the training text into a sentence-level vector representation to obtain a fourth vector representation of the training text;
[0030] The fourth vector representation of the training text is combined with the second vector representation of the training text to obtain a final vector representation of the training text.
[0031] Optionally, the tag is a BIO tag, a BMES tag or a BIOSE tag.
[0032] Optionally, the above method further includes:
[0033] Use the trained named entity recognition model to recognize named entities.
[0034] In a second aspect, an embodiment of the present application provides a training device for a named entity recognition model, wherein the named entity recognition model includes an encoder and a decoder, wherein the encoder includes a pre-trained language model and an attention mechanism model, and the training device includes:
[0035] A first acquisition module is used to acquire a plurality of training texts that have been annotated with labels, wherein the labels are used to annotate the named entity types to which the tokens in the training texts belong; a label annotation is constructed for each label, wherein when the label is a label corresponding to a named entity, the label annotation includes a position indicating token, and the position indicating token is used to indicate the position of the token corresponding to the label in the named entity;
[0036] A first generating module is used to generate a weight matrix according to all the label annotations, wherein each row of the weight matrix corresponds to a label annotation, each element in each row corresponds to a token in the label annotation in turn, and the value of the element corresponding to the position indicating token in the label annotation is k, and the value of the element corresponding to other tokens except the position indicating token is 0, wherein k is a learnable parameter in the training process of the named entity recognition model;
[0037] A first acquisition module, used for inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation;
[0038] A second acquisition module is used to input the first vector representation of the training text and the first vector representation of the label annotation into the attention mechanism model, calculate the first relationship between the training text and the label annotation; use the weight matrix to weight the first relationship to obtain a second relationship, and generate a final vector representation of the training text based on the second relationship;
[0039] A third obtaining module is used to input the final vector representation of the training text into the decoder to obtain a label corresponding to each token in the training text output by the decoder;
[0040] The optimization module is used to optimize the named entity recognition model according to the label corresponding to each token in the training text output by the decoder and the labeled labels in the training text to obtain a trained named entity recognition model.
[0041] Optionally, the first generating module is further used to:
[0042] Unify the number of tokens for all label annotations based on the maximum number of tokens in all label annotations;
[0043] Initialize an all-0 matrix, where each row in the all-0 matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in sequence;
[0044] The value of the element corresponding to the position indication token in the label annotation in the all-0 matrix is set to k to obtain the weight matrix, wherein the initial value of k is 1.
[0045] Optionally, the first obtaining module is further used to:
[0046] Inputting the training text and the label annotation into the pre-trained language model to obtain the ID of the training text and the ID of the label annotation represented by numerical values;
[0047] Based on the ID of the training text, a first vector representation of the training text is generated; based on the ID of the label annotation, a first vector representation of the label annotation is generated.
[0048] Optionally, the second obtaining module is further used to:
[0049] The first vector representation of the training text is weighted by a first weight parameter to obtain a second vector representation of the training text; the first vector representation of the label annotation is weighted by a second weight parameter to obtain a second vector representation of the label annotation, wherein the first weight parameter and the second weight parameter are learnable parameters;
[0050] Based on the second vector representation of the training text and the second vector representation of the label annotation, a first relationship between the training text and the label annotation is calculated.
[0051] Optionally, the second acquisition module is further used to: expand the dimension of the weight matrix so that the dimension of the expanded weight matrix is the same as the dimension of the first relationship; and add the expanded weight matrix and the first relationship to obtain the second relationship.
[0052] Optionally, the second obtaining module is further used to: calculate a third vector representation of the training text according to the second relationship and the second vector representations of all label annotations, wherein the third vector representation of the training text is a vector representation at a token level;
[0053] Converting the third vector representation of the training text into a sentence-level vector representation to obtain a fourth vector representation of the training text;
[0054] The fourth vector representation of the training text is combined with the second vector representation of the training text to obtain a final vector representation of the training text.
[0055] Optionally, the above device further includes:
[0056] The entity recognition module is used to recognize named entities using the trained named entity recognition model.
[0057] In a third aspect, an embodiment of the present application provides a computer-readable storage medium, on which a program is stored. When the program is executed by a processor, the steps of the method described above are implemented.
[0058] Compared with the prior art, the training method and device for the named entity recognition model provided in the embodiments of the present application introduce the position information of the token corresponding to the label in the named entity during the training process of the named entity recognition model, so that the model can better understand the different positions of different tokens in the same named entity, thereby enabling the trained named entity recognition model to more accurately identify the named entities, thereby improving the recognition performance of the model. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present application. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0060] Figure 1 A flowchart of a method for training a named entity recognition model according to an embodiment of the present application;
[0061] Figure 2 A schematic diagram of the structure of a training device for a named entity recognition model according to an embodiment of the present application;
[0062] Figure 3 Another structural schematic diagram of the training device for the named entity recognition model according to an embodiment of the present application. DETAILED DESCRIPTION
[0063] In order to make the technical problems, technical solutions and advantages to be solved by the application clearer, the following will be described in detail in conjunction with the accompanying drawings and specific embodiments. In the following description, specific details such as specific configurations and components are provided only to help fully understand the embodiments of the application. Therefore, it should be clear to those skilled in the art that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the application. In addition, for clarity and brevity, the description of known functions and structures has been omitted.
[0064] It should be understood that the "one embodiment" or "an embodiment" mentioned throughout the specification means that the specific features, structures or characteristics related to the embodiment are included in at least one embodiment of the present application. Therefore, "in one embodiment" or "in one embodiment" appearing throughout the specification may not necessarily refer to the same embodiment. In addition, these specific features, structures or characteristics can be combined in one or more embodiments in any suitable manner. The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described here can be implemented in an order other than those illustrated or described here. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices. "And / or" in the specification and claims represents at least one of the connected objects.
[0065] In the various embodiments of the present application, it should be understood that the size of the serial numbers of the following processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0066] The following description provides examples and does not limit the scope, applicability, or configuration set forth in the claims. Changes may be made to the functions and arrangements of the elements discussed without departing from the spirit and scope of the present disclosure. Various examples may appropriately omit, replace, or add various procedures or components. For example, the described methods may be performed in an order different from that described, and various steps may be added, omitted, or combined. In addition, features described with reference to certain examples may be combined in other examples.
[0067] Please refer to Figure 1 The embodiment of the present application provides a method for training a named entity recognition model, wherein the named entity recognition model includes an encoder and a decoder, wherein the encoder includes a pre-trained language model and an attention mechanism model, and the method includes:
[0068] Step 11, obtain a plurality of training texts with annotated labels, wherein the labels are used to annotate the named entity types to which the tokens in the training texts belong; construct a label annotation for each label, wherein, when the label is a label corresponding to a named entity, the label annotation includes a position indication token, and the position indication token is used to indicate the position of the token corresponding to the label in the named entity.
[0069] In the embodiment of the present application, the token refers to the granularity of the text processed by the pre-trained language model, which can be a single Chinese character in Chinese, a word or subword in English, etc. The pre-trained language model includes but is not limited to any of the following models: BERT model, RoBERTa model, ALBERT model, etc. This article mainly uses the BERT model as an example for explanation.
[0070] Here, the embodiment of the present application obtains a training set, and the training set includes a plurality of labeled training texts. Specifically, the tokens in the training texts are pre-labeled with corresponding labels, and the labels can be labeled according to the named entity type to which the token belongs and the positional relationship between the token and the named entity type. In other words, different types of labels can reflect the named entity type to which the token belongs and the positional relationship between the token and the named entity type. Specifically, the embodiment of the present application can be labeled using label systems such as BIO, BIOSE or BMES commonly used in named entity recognition, that is, the label can be a BIO label, a BIOSE label or a BMES label. This article will mainly use the BIO label as an example for explanation, and the methods of the embodiments of the present application are all applicable.
[0071] Taking the BIO tag as an example, for the "name" in the named entity, there are two tags, namely B-Name and I-Name. The tag B-Name indicates that the named entity to which the token belongs is name, and the token is the starting token of the named entity. The tag I-Name indicates that the named entity to which the token belongs is name, and the token is the subsequent token of the named entity. For the tag O, it means that the token does not belong to any named entity, that is, the token is a non-named entity. Here, non-named entities can also be regarded as a special type of named entity.
[0072] In order to introduce the semantic information of the label, the embodiment of the present application annotates the meaning of each label and constructs the corresponding label annotation. The label annotation is a text description, usually including multiple tokens. Wherein, when the label is a label corresponding to a named entity, the label annotation of the label contains a position indication token to indicate the position of the token corresponding to the label in the named entity. When the label is a label corresponding to a non-named entity, the label annotation of the label may not contain a position indication token. In this way, a dictionary can be obtained based on the constructed label annotation, and the dictionary includes the label annotations of all labels.
[0073] Table 1 shows an example of a labeled training text, the training text is "Xiao ming lives in Beijing", where the label corresponding to the token "Xiao" is "B-Name", which indicates that the token "Xiao" is the starting token of the name entity "Xiao ming". The label corresponding to the token "ming" is "I-Name", which indicates that the token "ming" is the subsequent token of the name entity "Xiao ming". The labels corresponding to other tokens in the above training text are "O", that is, these tokens are non-named entities.
[0074] Table 1
[0075]
[0076] Table 2 shows an example of a tag annotation. A related tag annotation explains the meaning of the tag. The embodiment of the present application further adds a position indication token based on the BIO tag annotation, such as "The begin character of..." to indicate that the token is the starting token in the named entity, and "The inside character of..." to indicate that the token is the subsequent token in the named entity, thereby indicating the position of the token corresponding to the tag in the named entity. For highlighting, an underscore is added under the position indication token in Table 2. In addition, for the "O" tag, a tag annotation of a blank character can be generated.
[0077] Table 2
[0078]
[0079] Step 12, generate a weight matrix based on all the label annotations, each row of the weight matrix corresponds to a label annotation, and each element in each row corresponds to the token in the label annotation in turn, and the value of the element corresponding to the position indication token in the label annotation is k, and the elements corresponding to other tokens except the position indication token are 0, wherein k is a learnable parameter in the training process of the named entity recognition model.
[0080] Here, we can first unify the number of tokens of all label annotations according to the maximum number of tokens in all label annotations. Assuming that the maximum number of tokens in all label annotations is m, we can fill blank characters (such as [PAD]) for label annotations with a token number less than m to make the number of tokens contained in the label annotation reach m. Then, initialize an all-0 matrix, such as initializing an all-0 matrix with a dimension of n*m, where each row in the all-0 matrix corresponds to a label annotation, and each element in each row corresponds to the token in the label annotation in turn, where n is the number of label annotations. Then, set the value of the element in the all-0 matrix corresponding to the position indication token in the label annotation to k to obtain the weight matrix, where the initial value of k is 1.
[0081] Tables 3 and 4 show a method of constructing a weight matrix. First, a matrix n*m with all zeros is initialized, where n represents the number of labels and m represents the maximum length of the label annotation. In Table 3, it is assumed that n=3 and m=11, that is, there are 3 label annotations in total and the maximum token length of all label annotations is 11. For each label annotation, the value of the element corresponding to the position indication token in the matrix is changed from 0 to k according to the corresponding position, and finally the weight matrix Wp shown in Table 4 is obtained. In Table 3, only some tokens in the position indication token, such as the elements corresponding to "begin" and "inside", are set to k. Of course, the embodiment of the present application can also set all tokens in the position indication token to k.
[0082] Table 3
[0083]
[0084] Table 4
[0085] 0 k 0 0 0 0 0 0 0 0 0 0 k 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[0086] Step 13: input the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation.
[0087] Here, the training text and label annotation are input into the pre-trained language model to obtain the ID of the training text and the ID of the label annotation represented by numerical values; then, based on the ID of the training text, a first vector representation of the training text is generated; based on the ID of the label annotation, a first vector representation of the label annotation is generated.
[0088] For example, you can use the word segmenter of BERT (pre-trained model) to convert the training text and label annotations into the ID form of BERT tokens represented by numbers to obtain the ID of the training text. text and the ID of the tag annotationlabel annotation Here, the ID of the training text text and the ID of the tag annotation label annotation It consists of the IDs of the tokens it contains. Suppose the ID of a training text is text =(ID token1 ,ID token2 ,…,ID tokenn ), which is composed of n token IDs, each ID token The ID of a token in the training text (BERT ID). Similarly, assuming the label annotation includes m tokens, the label annotation ID label annotation =(ID labeltoken1 ,ID labeltoken2 ,…,ID labeltokenm ), each ID labeltoken The ID (BERT ID) of a token in the label annotation. Here m and n may not be the same, because when initially constructed, the maximum length of the training text is not necessarily the same as the maximum length of the label annotation. One way to calculate the first vector representation (hidden layer representation) of the training text and the first vector representation (hidden layer representation) of the label annotation is as follows:
[0089] h text =BERT(ID token ).
[0090] h label annotation =BERT(ID labeltoken )
[0091] where h text h is the vector representation of a token of the training text (hidden layer representation); label annotation A vector representation (hidden layer representation) of a token representing a label annotation; BERT is a pre-trained language model used to convert numbers into vector representations (hidden layer representations); ID token and ID labeltoken They are ID text and ID label annotation The ID of a token in the .
[0092] After calculating all h text and h label annotation After that, we can get the first vector representation (hidden layer representation) H of the training text text =(h text1 ,h text2 ,…,h textn ) and the first vector representation (hidden layer representation) of the label annotation H label annotation =(h label annotation1 ,h label annotation2 ,…,h label annotationm), each h is a vector of d dimensions (the hidden layer dimension of the pre-trained language model). Then we can get the first hidden layer representation H of all label annotations labels =(H label1 annotation ,H label2 annotation ,…,H labelm annotation ). Among them, H text The dimensions of H are (maximum number of tokens in all training texts, hidden layer dimensions), labels The dimensions of are (number of labels, maximum number of tokens in all label annotations, hidden layer dimensions).
[0093] Step 14: input the first vector representation of the training text and the first vector representation of the label annotation into the attention mechanism model, calculate the first relationship between the training text and the label annotation; use the weight matrix to weight the first relationship to obtain the second relationship, and generate the final vector representation of the training text based on the second relationship.
[0094] Here, the attention mechanism is used to calculate the first relationship between the first vector representation of the training text and the first vector representation of the label annotation. Specifically, the first weight parameter w 1 The first vector representation H of the training text text Weighted, to obtain the second vector representation Q of the training text; using the second weight parameter w 2 The first vector annotated with the label represents H labels Weighted, the second vector representation K of the label annotation is obtained, and the first weight parameter w 1 and the second weight parameter w 2 is a model parameter in the attention mechanism model, and the first weight parameter w 1 and the second weight parameter w 2 is a learnable parameter that can be updated during the named entity recognition model training process. Then, based on the second vector representation Q of the training text and the second vector representation K of the label annotation, the first relationship between the training text and the label annotation is calculated. A calculation method of the above process can be expressed by the following formula:
[0095]
[0096] Among them, QK is used to represent the relationship (first relationship) between the training text and the label annotation; softmax is an algorithm used to compress numerical values between 0 and 1; the superscript T represents the transposed matrix, which is used to calculate the matrix multiplication of Q and K.
[0097] The embodiment of the present application also uses the weight matrix Wp to weight the first relationship QK to obtain a second relationship. Specifically, the weight matrix Wp can be dimensionally expanded so that the dimension of the expanded weight matrix is the same as the dimension of the first relationship, and then the expanded weight matrix and the first relationship are added to obtain the second relationship, thereby introducing the information of the position indication token in the label annotation into the model. A calculation method of the above process can be expressed by the following formula:
[0098] relation=Wp*sentence_max_length⊕QK
[0099] Where relation is used to represent the second relation mentioned above; the dimension of Wp is (number of tags, maximum number of tokens in all tag annotations), and Wp*sentence_max_length here means expanding Wp to the dimension of (maximum number of tokens in all training texts, number of tags, maximum number of tokens in all tag annotations). ⊕ means adding the corresponding elements in the two matrices Wp*sentence_max_length and QK. In this way, the dimension of relation is (maximum number of tokens in all training texts, number of tags, maximum number of tokens in all tag annotations).
[0100] Then, based on the second relation, the final vector representation of the training text is generated. Specifically, the third vector representation R of the training text can be calculated based on the second relation and the second vector representation K of all label annotations, wherein the third vector representation R of the training text is a token-level vector representation. Then, the third vector representation R of the training text is converted into a sentence-level vector representation to obtain a fourth vector representation R* of the training text. Then, the fourth vector representation R* of the training text is combined with the second vector representation Q of the training text to obtain the final vector representation H of the training text. final One way to calculate the above process can be expressed by the following formula:
[0101] R=K*sentence_max_length⊙relation
[0102] Here, R is used to represent the token-level vector representation of the training text after combining the vector representations of all label annotations; K*sentence_max_length here means expanding K to the dimension of (maximum number of tokens in all training texts, number of labels, maximum number of tokens in all label annotations, hidden layer dimension). ⊙ represents the multiplication of the corresponding elements in the two matrices K*sentence_max_length and relation. In this way, the dimension of R is (maximum number of tokens in all training texts, number of labels, maximum number of tokens in all label annotations, hidden layer dimension).
[0103] The fourth vector representation R* of the training text is a sentence-level vector representation of the training text combined with the vector representations of all label annotations. One calculation method is as follows:
[0104] R*=∑ t=1 R[:,:,t,:]
[0105] In the embodiment of the present application, R has 4 dimensions. In the above formula, "," is used to separate the 4 dimensions of R, and the 3 ":" and 1 "t" in the above formula correspond to one dimension of R respectively. Among them, ":" represents all elements of R in one dimension. t means taking the tth element of the corresponding dimension. The above formula represents the addition of each token-level vector representation in R, that is, the addition of all numbers of R in the third dimension (the dimension of the maximum number of tokens in all label annotations), thereby converting the token-level vector representation R into a sentence-level vector representation R*, and the dimension of R* is (the maximum number of tokens in all training texts, the number of labels, and the hidden layer dimension).
[0106] Finally, add R* to Q to get the final vector representation H of the training text final In addition, before adding, Q needs to be expanded to the dimension (maximum number of tokens in all training texts, number of labels, hidden layer dimension), which can be expressed as follows:
[0107]
[0108] Among them, Q*num_labels means expanding Q to the dimension (maximum number of tokens in all training texts, number of labels, hidden layer dimension), It means that the corresponding elements in the two matrices R* and num_labels are added.
[0109] Step 15, inputting the final vector representation of the training text into the decoder, and obtaining the label corresponding to each token in the training text output by the decoder.
[0110] Here, the final vector representation of the training text is input into the decoder to obtain the label corresponding to the token output by the decoder, which is expressed as follows:
[0111] output = argmax(w 3 H final )
[0112] Among them, argmax is an algorithm to obtain the index of the maximum value, which is used to obtain the result predicted by the model; 3 is a learnable parameter.
[0113] Step 16, optimizing the named entity recognition model according to the label corresponding to each token in the training text output by the decoder and the labeled labels in the training text to obtain a trained named entity recognition model.
[0114] Here, the embodiment of the present application can calculate the difference between the label output by the decoder and the label in the training text, and iteratively optimize the model parameters according to the difference, so as to obtain a trained named entity recognition model.
[0115] When performing named entity recognition, the existing named entity recognition model usually only classifies the output results into a certain category, and the model cannot understand the specific meaning of this category very well. In the above method of the embodiment of the present application, firstly, a corresponding label annotation is constructed for the label, so that the named entity recognition model can obtain the meaning of the label. Furthermore, on the basis of the above label annotation and sequence labeling, the embodiment of the present application also introduces a weighted attention mechanism, so that the difference between labels of the same category but representing different positions is more obvious, and this obviousness can be learned, and will not cause the balance to be too tilted, that is, on the basis of the original label attention mechanism, the position information of the token corresponding to the label in the named entity is introduced. For example, for "B-name" and "I-name", although they both belong to the same named entity "name", their corresponding tokens are in different positions in the named entity. In the process of training the named entity recognition model, the embodiment of the present application introduces the above position information, so that the model can better understand the different positions of the tokens in the named entity, and then the trained named entity recognition model can more accurately identify the named entity, thereby improving the recognition performance of the model.
[0116] After the named entity recognition model is trained, the embodiment of the present application can also use the trained named entity recognition model to recognize named entities.
[0117] Based on the above method, the present application also provides a device for implementing the above method. Figure 2The embodiment of the present application provides a training device for a named entity recognition model, wherein the named entity recognition model includes an encoder and a decoder, wherein the encoder includes a pre-trained language model and an attention mechanism model, and wherein the training device includes:
[0118] The first acquisition module 21 is used to acquire a plurality of training texts with annotated labels, wherein the labels are used to annotate the named entity types to which the tokens in the training texts belong; construct a label annotation for each label, wherein when the label is a label corresponding to a named entity, the label annotation includes a position indicating token, and the position indicating token is used to indicate the position of the token corresponding to the label in the named entity;
[0119] The first generating module 22 is used to generate a weight matrix according to all the label annotations, wherein each row of the weight matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in turn, and the value of the element corresponding to the position indicating token in the label annotation is k, and the value of the element corresponding to other tokens except the position indicating token is 0, wherein k is a learnable parameter in the training process of the named entity recognition model;
[0120] A first obtaining module 23, used for inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation;
[0121] A second obtaining module 24 is used to input the first vector representation of the training text and the first vector representation of the label annotation into the attention mechanism model, calculate the first relationship between the training text and the label annotation; use the weight matrix to weight the first relationship to obtain a second relationship, and generate a final vector representation of the training text based on the second relationship;
[0122] A third obtaining module 25 is used to input the final vector representation of the training text into the decoder to obtain a label corresponding to each token in the training text output by the decoder;
[0123] The optimization module 26 is used to optimize the named entity recognition model according to the label corresponding to each token in the training text output by the decoder and the labeled labels in the training text to obtain a trained named entity recognition model.
[0124] Through the above modules, the embodiment of the present application introduces the position information of the token corresponding to the label in the named entity during the training process of the named entity recognition model, so that the model can better understand the different positions of the token in the named entity, and then the trained named entity recognition model can more accurately identify the named entity, thereby improving the recognition performance of the model.
[0125] Optionally, the first generating module 22 is further used for:
[0126] Unify the number of tokens for all label annotations based on the maximum number of tokens in all label annotations;
[0127] Initialize an all-0 matrix, where each row in the all-0 matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in sequence;
[0128] The value of the element corresponding to the position indication token in the label annotation in the all-0 matrix is set to k to obtain the weight matrix, wherein the initial value of k is 1.
[0129] Optionally, the first obtaining module 23 is further used for:
[0130] Inputting the training text and the label annotation into the pre-trained language model to obtain the ID of the training text and the ID of the label annotation represented by numerical values;
[0131] Based on the ID of the training text, a first vector representation of the training text is generated; based on the ID of the label annotation, a first vector representation of the label annotation is generated.
[0132] Optionally, the second obtaining module 24 is further used for:
[0133] The first vector representation of the training text is weighted by a first weight parameter to obtain a second vector representation of the training text; the first vector representation of the label annotation is weighted by a second weight parameter to obtain a second vector representation of the label annotation, wherein the first weight parameter and the second weight parameter are learnable parameters;
[0134] Based on the second vector representation of the training text and the second vector representation of the label annotation, a first relationship between the training text and the label annotation is calculated.
[0135] Optionally, the second obtaining module 24 is further used to: expand the dimension of the weight matrix so that the dimension of the expanded weight matrix is the same as the dimension of the first relationship; and add the expanded weight matrix and the first relationship to obtain the second relationship.
[0136] Optionally, the second obtaining module 24 is further used to: calculate a third vector representation of the training text according to the second relationship and the second vector representation of all label annotations, wherein the third vector representation of the training text is a vector representation at a token level;
[0137] Converting the third vector representation of the training text into a sentence-level vector representation to obtain a fourth vector representation of the training text;
[0138] The fourth vector representation of the training text is combined with the second vector representation of the training text to obtain a final vector representation of the training text.
[0139] Optionally, the above device further includes:
[0140] The entity recognition module is used to recognize named entities using the trained named entity recognition model.
[0141] It should be noted that the various devices provided in the above embodiments are devices corresponding to the training method of the above-mentioned named entity recognition model, and the implementation methods in the above-mentioned embodiments are all applicable to the embodiments of the device, and can also achieve the same technical effects. The above-mentioned device provided in the embodiment of the present application can implement all the method steps implemented in the above-mentioned method embodiment, and can achieve the same technical effects. The parts and beneficial effects that are the same as those in the method embodiment in this embodiment will not be specifically repeated here.
[0142] Please refer to Figure 3 , the embodiment of the present application also provides a hardware structure block diagram of a model training device, wherein the named entity recognition model includes an encoder and a decoder, and the encoder includes a pre-trained language model and an attention mechanism model, such as Figure 3 As shown, the model training device 300 includes:
[0143] Processor 302; and
[0144] a memory 304 in which computer program instructions are stored,
[0145] When the computer program instructions are executed by the processor, the processor 302 is caused to perform the following steps:
[0146] Acquire a plurality of training texts with annotated labels, wherein the labels are used to annotate the named entity types to which tokens in the training texts belong; construct a label annotation for each label, wherein when the label is a label corresponding to a named entity, the label annotation includes a position indicating token, and the position indicating token is used to indicate the position of the token corresponding to the label in the named entity;
[0147] According to all the label annotations, a weight matrix is generated, wherein each row of the weight matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in turn, and the value of the element corresponding to the position indication token in the label annotation is k, and the value of the element corresponding to other tokens except the position indication token is 0, wherein k is a learnable parameter in the training process of the named entity recognition model;
[0148] Inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation;
[0149] Inputting the first vector representation of the training text and the first vector representation of the label annotation into the attention mechanism model, calculating the first relationship between the training text and the label annotation; weighting the first relationship using the weight matrix to obtain a second relationship, and generating a final vector representation of the training text based on the second relationship;
[0150] Inputting the final vector representation of the training text into the decoder, and obtaining a label corresponding to each token in the training text output by the decoder;
[0151] The named entity recognition model is optimized according to the label corresponding to each token in the training text output by the decoder and the labeled labels in the training text to obtain a trained named entity recognition model.
[0152] It should be noted that each system provided in the above embodiments is a device corresponding to the training method of the above-mentioned named entity recognition model, and the implementation methods in the above-mentioned embodiments are applicable to the embodiments of the device, and can also achieve the same technical effect. The above-mentioned device provided in the embodiment of the present application can implement all the method steps implemented in the above-mentioned method embodiment, and can achieve the same technical effect. The parts and beneficial effects that are the same as those in the method embodiment in this embodiment will not be specifically repeated here.
[0153] The model training device 300 also includes a network interface 301 , an input device 303 , a hard disk 305 , and a display device 306 .
[0154] The above-mentioned interfaces and devices can be interconnected through a bus architecture. The bus architecture can be a bus and bridge that can include any number of interconnected buses. Specifically, one or more central processing units (CPUs) and / or graphics processing units (GPUs) represented by processor 302, and various circuits of one or more memories represented by memory 304 are connected together. The bus architecture can also connect various other circuits such as peripheral devices, voltage regulators, and power management circuits together. It can be understood that the bus architecture is used to achieve connection and communication between these components. In addition to the data bus, the bus architecture also includes a power bus, a control bus, and a status signal bus, which are all well known in the art, so they are not described in detail herein.
[0155] The network interface 301 can be connected to a network (such as the Internet, a local area network, etc.), receive data such as training text from the network, and save the received data in the hard disk 305.
[0156] The input device 303 can receive various instructions input by the operator and send them to the processor 302 for execution. The input device 303 can include a keyboard or a pointing device (for example, a mouse, a trackball, a touch pad or a touch screen).
[0157] The display device 306 can display the results obtained by the processor 302 executing instructions, such as displaying the model training progress.
[0158] The memory 304 is used to store programs and data necessary for the operation of the operating system, as well as data such as intermediate results during the calculation process of the processor 302.
[0159] It is understood that the memory 304 in the embodiment of the present application can be a volatile memory or a non-volatile memory, or can include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM) or a flash memory. The volatile memory can be a random access memory (RAM), which is used as an external cache. The memory 304 of the apparatus and method described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0160] In some implementations, the memory 304 stores the following elements, executable modules or data structures, or a subset thereof, or an extended set thereof: an operating system 3041 and an application program 3042 .
[0161] The operating system 3041 includes various system programs, such as a framework layer, a core library layer, a driver layer, etc., which are used to implement various basic services and process hardware-based tasks. The application 3042 includes various application programs, such as a browser, etc., which are used to implement various application services. The program for implementing the method of the embodiment of the present application can be included in the application 3042.
[0162] The method disclosed in the above embodiment of the present application can be applied to the processor 302, or implemented by the processor 302. The processor 302 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 302. The above processor 302 can be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components, and can implement or execute the methods, steps and logic block diagrams disclosed in the embodiments of the present application. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor. The steps of the method disclosed in the embodiment of the present application can be directly embodied as a hardware decoding processor to perform, or the hardware and software modules in the decoding processor are combined and executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 304, and the processor 302 reads the information in the memory 304 and completes the steps of the above method in combination with its hardware.
[0163] It is understood that the embodiments described herein can be implemented in hardware, software, firmware, middleware, microcode or a combination thereof. For hardware implementation, the processing unit can be implemented in one or more application specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), general purpose processors, controllers, microcontrollers, microprocessors, other electronic units for performing the functions described in the present application or a combination thereof.
[0164] For software implementation, the techniques described herein can be implemented by modules (e.g., procedures, functions, etc.) that perform the functions described herein. The software code can be stored in a memory and executed by a processor. The memory can be implemented in the processor or outside the processor.
[0165] Specifically, when the computer program is executed by the processor 302, the following steps may also be implemented:
[0166] Unify the number of tokens for all label annotations based on the maximum number of tokens in all label annotations;
[0167] Initialize an all-0 matrix, where each row in the all-0 matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in sequence;
[0168] The value of the element corresponding to the position indication token in the label annotation in the all-0 matrix is set to k to obtain the weight matrix, wherein the initial value of k is 1.
[0169] Specifically, when the computer program is executed by the processor 302, the following steps may also be implemented:
[0170] Inputting the training text and the label annotation into the pre-trained language model to obtain the ID of the training text and the ID of the label annotation represented by numerical values;
[0171] Based on the ID of the training text, a first vector representation of the training text is generated; based on the ID of the label annotation, a first vector representation of the label annotation is generated.
[0172] Specifically, when the computer program is executed by the processor 302, the following steps may also be implemented:
[0173] The first vector representation of the training text is weighted by a first weight parameter to obtain a second vector representation of the training text; the first vector representation of the label annotation is weighted by a second weight parameter to obtain a second vector representation of the label annotation, wherein the first weight parameter and the second weight parameter are learnable parameters;
[0174] Based on the second vector representation of the training text and the second vector representation of the label annotation, a first relationship between the training text and the label annotation is calculated.
[0175] Specifically, when the computer program is executed by the processor 302, the following steps may also be implemented:
[0176] The weight matrix is dimensionally expanded so that the dimension of the expanded weight matrix is the same as the dimension of the first relationship; and the expanded weight matrix and the first relationship are added to obtain the second relationship.
[0177] Specifically, when the computer program is executed by the processor 302, the following steps may also be implemented:
[0178] Calculate a third vector representation of the training text according to the second relationship and the second vector representation of all label annotations, wherein the third vector representation of the training text is a vector representation at a token level;
[0179] Converting the third vector representation of the training text into a sentence-level vector representation to obtain a fourth vector representation of the training text;
[0180] The fourth vector representation of the training text is combined with the second vector representation of the training text to obtain a final vector representation of the training text.
[0181] Specifically, when the computer program is executed by the processor 302, the following steps may also be implemented:
[0182] Use the trained named entity recognition model to recognize named entities.
[0183] It should be noted here that the above-mentioned device provided in the embodiment of the present application can implement all the method steps implemented in the above-mentioned method embodiment, and can achieve the same technical effect. The parts and beneficial effects of this embodiment that are the same as the method embodiment will not be described in detail here.
[0184] In some embodiments of the present application, a computer-readable storage medium is further provided, on which a program is stored, and when the program is executed by a processor, the following steps are implemented:
[0185] Acquire a plurality of training texts with annotated labels, wherein the labels are used to annotate the named entity types to which tokens in the training texts belong; construct a label annotation for each label, wherein when the label is a label corresponding to a named entity, the label annotation includes a position indicating token, and the position indicating token is used to indicate the position of the token corresponding to the label in the named entity;
[0186] According to all the label annotations, a weight matrix is generated, wherein each row of the weight matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in turn, and the value of the element corresponding to the position indication token in the label annotation is k, and the value of the element corresponding to other tokens except the position indication token is 0, wherein k is a learnable parameter in the training process of the named entity recognition model;
[0187] Inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation;
[0188] Inputting the first vector representation of the training text and the first vector representation of the label annotation into the attention mechanism model, calculating the first relationship between the training text and the label annotation; weighting the first relationship using the weight matrix to obtain a second relationship, and generating a final vector representation of the training text based on the second relationship;
[0189] Inputting the final vector representation of the training text into the decoder, and obtaining a label corresponding to each token in the training text output by the decoder;
[0190] The named entity recognition model is optimized according to the label corresponding to each token in the training text output by the decoder and the labeled labels in the training text to obtain a trained named entity recognition model.
[0191] When the program is executed by the processor, it can implement all the implementation methods in the training method of the above-mentioned named entity recognition model and can achieve the same technical effect. To avoid repetition, it will not be repeated here.
[0192] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0193] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0194] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0195] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiments of the present application.
[0196] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0197] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as USB flash drives, mobile hard drives, ROM, RAM, magnetic disks, or optical disks.
[0198] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A training method for a named entity recognition model, wherein the named entity recognition model comprises an encoder and a decoder, wherein the encoder comprises a pre-trained language model and an attention mechanism model, wherein: The training method comprises: Acquire a plurality of training texts with annotated labels, wherein the labels are used to annotate the named entity types to which tokens in the training texts belong; construct a label annotation for each label, wherein when the label is a label corresponding to a named entity, the label annotation includes a position indicating token, and the position indicating token is used to indicate the position of the token corresponding to the label in the named entity; According to all the label annotations, a weight matrix is generated, wherein each row of the weight matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in turn, and the value of the element corresponding to the position indication token in the label annotation is k, and the value of the element corresponding to other tokens except the position indication token is 0, wherein k is a learnable parameter in the training process of the named entity recognition model; Inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation; Inputting the first vector representation of the training text and the first vector representation of the label annotation into the attention mechanism model, calculating the first relationship between the training text and the label annotation; weighting the first relationship using the weight matrix to obtain a second relationship, and generating a final vector representation of the training text based on the second relationship; Inputting the final vector representation of the training text into the decoder, and obtaining a label corresponding to each token in the training text output by the decoder; The named entity recognition model is optimized according to the label corresponding to each token in the training text output by the decoder and the labeled labels in the training text to obtain a trained named entity recognition model.
2. The method according to claim 1, characterized in that The step of generating a weight matrix includes: Unify the number of tokens for all label annotations based on the maximum number of tokens in all label annotations; Initialize an all-0 matrix, where each row in the all-0 matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in sequence; The value of the element corresponding to the position indication token in the label annotation in the all-0 matrix is set to k to obtain the weight matrix, wherein the initial value of k is 1.
3. The method according to claim 1, characterized in that The step of inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation includes: Inputting the training text and the label annotation into the pre-trained language model to obtain the ID of the training text and the ID of the label annotation represented by numerical values; Based on the ID of the training text, a first vector representation of the training text is generated; based on the ID of the label annotation, a first vector representation of the label annotation is generated.
4. The method according to claim 1, characterized in that Calculating a first relationship between the training text and the label annotation includes: The first vector representation of the training text is weighted by a first weight parameter to obtain a second vector representation of the training text; the first vector representation of the label annotation is weighted by a second weight parameter to obtain a second vector representation of the label annotation, wherein the first weight parameter and the second weight parameter are learnable parameters; Based on the second vector representation of the training text and the second vector representation of the label annotation, a first relationship between the training text and the label annotation is calculated.
5. The method according to claim 1, characterized in that The step of weighting the first relationship by using the weight matrix to obtain the second relationship includes: The weight matrix is dimensionally expanded so that the dimension of the expanded weight matrix is the same as the dimension of the first relationship; and the expanded weight matrix and the first relationship are added to obtain the second relationship.
6. The method according to claim 1, characterized in that Generating a final vector representation of the training text based on the second relationship includes: Calculate a third vector representation of the training text according to the second relationship and the second vector representation of all label annotations, wherein the third vector representation of the training text is a vector representation at a token level; Converting the third vector representation of the training text into a sentence-level vector representation to obtain a fourth vector representation of the training text; The fourth vector representation of the training text is combined with the second vector representation of the training text to obtain a final vector representation of the training text.
7. The method according to claim 1, characterized in that The label is a BIO label, a BMES label or a BIOSE label.
8. The method according to claim 1, characterized in that Also includes: Use the trained named entity recognition model to recognize named entities.
9. A training device for a named entity recognition model, wherein the named entity recognition model comprises an encoder and a decoder, wherein the encoder comprises a pre-trained language model and an attention mechanism model, wherein: The training device comprises: A first acquisition module is used to acquire a plurality of training texts that have been annotated with labels, wherein the labels are used to annotate the named entity types to which the tokens in the training texts belong; a label annotation is constructed for each label, wherein when the label is a label corresponding to a named entity, the label annotation includes a position indication token, and the position indication token is used to indicate the position of the token corresponding to the label in the named entity; A first generating module is used to generate a weight matrix according to all the label annotations, wherein each row of the weight matrix corresponds to a label annotation, each element in each row corresponds to a token in the label annotation in turn, and the value of the element corresponding to the position indicating token in the label annotation is k, and the value of the element corresponding to other tokens except the position indicating token is 0, wherein k is a learnable parameter in the training process of the named entity recognition model; A first acquisition module, used for inputting the training text and the label annotation into the pre-trained language model to obtain a first vector representation of the training text and a first vector representation of the label annotation; A second acquisition module is used to input the first vector representation of the training text and the first vector representation of the label annotation into the attention mechanism model, calculate the first relationship between the training text and the label annotation; use the weight matrix to weight the first relationship to obtain a second relationship, and generate a final vector representation of the training text based on the second relationship; A third obtaining module is used to input the final vector representation of the training text into the decoder to obtain a label corresponding to each token in the training text output by the decoder; The optimization module is used to optimize the named entity recognition model according to the label corresponding to each token in the training text output by the decoder and the labeled labels in the training text to obtain a trained named entity recognition model.
10. The device according to claim 9, characterized in that The first generating module is further used for: Unify the number of tokens for all label annotations based on the maximum number of tokens in all label annotations; Initialize an all-0 matrix, where each row in the all-0 matrix corresponds to a label annotation, and each element in each row corresponds to a token in the label annotation in sequence; The value of the element corresponding to the position indication token in the label annotation in the all-0 matrix is set to k to obtain the weight matrix, wherein the initial value of k is 1.
11. The device according to claim 9, characterized in that The first obtaining module is further used for: Inputting the training text and the label annotation into the pre-trained language model to obtain the ID of the training text and the ID of the label annotation represented by numerical values; Based on the ID of the training text, a first vector representation of the training text is generated; based on the ID of the label annotation, a first vector representation of the label annotation is generated.
12. The device according to claim 9, characterized in that The second obtaining module is further used for: The first vector representation of the training text is weighted by a first weight parameter to obtain a second vector representation of the training text; the first vector representation of the label annotation is weighted by a second weight parameter to obtain a second vector representation of the label annotation, wherein the first weight parameter and the second weight parameter are learnable parameters; Based on the second vector representation of the training text and the second vector representation of the label annotation, a first relationship between the training text and the label annotation is calculated.
13. The device according to claim 9, characterized in that The second obtaining module is further used to: perform dimension expansion on the weight matrix so that the dimension of the expanded weight matrix is the same as the dimension of the first relationship; and add the expanded weight matrix and the first relationship to obtain the second relationship.
14. The device according to claim 9, characterized in that The second obtaining module is further used to: calculate a third vector representation of the training text according to the second relationship and the second vector representations of all label annotations, wherein the third vector representation of the training text is a vector representation at a token level; Converting the third vector representation of the training text into a sentence-level vector representation to obtain a fourth vector representation of the training text; The fourth vector representation of the training text is combined with the second vector representation of the training text to obtain a final vector representation of the training text.
15. The device according to claim 9, characterized in that Also includes: The entity recognition module is used to recognize named entities using the trained named entity recognition model.
16. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method for training a named entity recognition model as described in any one of claims 1 to 6 are implemented.
