Model training method and device
By combining general and domain-specific corpora in natural language processing, differentially matching training data for training special models, and dynamically adjusting the learning rate, the problem of reduced accuracy of pre-trained models in specific domain tasks is solved. Achieved better domain offset and performance improvements.
Patent Information
- Application Number
- CN202010639435.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-07-03
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2040-07-03
AI Technical Summary
The accuracy of existing pre-trained models will decrease when performing reading comprehension tasks in specific fields, and there is a lack of effective continued training solutions to achieve field shift.
By obtaining corpus in general fields and corpus in specific fields, the data ratio is determined based on differences, and training data is generated by mixing it for training special models. It combines general models with domain-related adaptation structures and dynamically adjusts the learning rate to avoid overfitting. .
It improves the performance of the dedicated model, balances the domain and universality, improves the domain shift effect of the model, and improves the accuracy of specific tasks.
Smart Images

Figure CN111797609B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of natural language processing, and in particular to a model training method and device. Background Art
[0002] In recent years, machine reading comprehension has been widely used in dynamic information extraction from various articles and the assistance of various question-answering robots. In the field of natural language processing, trained neural network models are generally used to perform reading comprehension tasks in specific fields (such as finance and insurance, policies and regulations, education, communications and IT). However, the existing pre-trained models are all in general fields. Using pre-trained models in general fields to perform reading comprehension tasks in specific fields will lose a certain degree of accuracy. Therefore, it is necessary to continue training the pre-trained models in general fields to achieve domain shift, but there is no good solution in the prior art for how to implement the continued training process. Summary of the invention
[0003] The purpose of the embodiments of the present application is to provide a model training method and device to improve the above-mentioned technical problems.
[0004] To achieve the above objectives, this application provides the following technical solutions:
[0005] In a first aspect, an embodiment of the present application provides a model training method, comprising: obtaining a general model, wherein the general model is a pre-trained, task-independent language model; obtaining a first corpus and a second corpus; wherein the first corpus is a corpus in a general field, and the second corpus is a corpus in a target field related to a target task, the target task is a natural language processing task, and the target field is the field to which the target task belongs; determining a first data ratio based on the difference between the first corpus and the second corpus, and mixing the first corpus with the second corpus according to the first data ratio to obtain first training data; wherein the difference between the first corpus and the second corpus is negatively correlated with the first data ratio; using the first training data to train a special model for performing the target task; wherein the special model includes the general model and an adaptation structure related to the target task.
[0006] In the above method, the general model is a pre-trained model that is independent of the task, and the dedicated model is a model used to perform the target task in a specific field. The dedicated model includes the general model, so the solution of training the dedicated model can also be regarded as a solution for continuing to train the general model to achieve domain shift. The above target task is a natural language processing task, but it is not limited to a reading comprehension task, and can also be a text classification task, a named entity recognition task, etc.
[0007] In this scheme, first, the first training data is generated by mixing the first corpus (data in the general field) with the second corpus (data in the specific field), which ensures that the first training data has good knowledge strength and does not overly favor domain knowledge, abandoning the language expression in the general field, or even causing problems such as overfitting, which is conducive to improving the performance of the trained special model. Secondly, based on the difference between the first corpus and the second corpus, the scheme determines the ratio of the two types of data in the first training data, which is conducive to reasonably balancing the domain and generality of the model, and further improving the performance of the trained special model.
[0008] In an implementation of the first aspect, determining the data ratio based on the difference between the first corpus and the second corpus includes: obtaining a first difference coefficient, the first difference coefficient is positively correlated with the frequency of occurrence of keywords in the target field in the test corpus in the target field; calculating a second difference coefficient based on the difference between the text length in the first corpus and the text length in the second corpus, the second difference coefficient is positively correlated with the difference between the text lengths; and determining the first data ratio based on the first difference coefficient and the second difference coefficient.
[0009] In the above implementation, the keywords in the target domain can be specified by domain experts. These keywords basically only appear in the corpus in the target domain, and rarely appear in the general domain. Therefore, the first difference coefficient can characterize the difference between the first corpus and the second corpus at the knowledge level, or mainly describe the difference between the two types of corpora due to their different domains. The second difference coefficient is calculated based on the difference between the text lengths in the first corpus and the second corpus, so it characterizes the difference between the first corpus and the second corpus at the language structure level, or mainly describes the difference caused by the language characteristics of the two types of corpora. Combining these two differences can comprehensively and effectively reflect the difference between the first corpus and the second corpus.
[0010] In an implementation of the first aspect, the target task is an extractive reading comprehension task, and the second difference coefficient is calculated based on the difference between the text length in the first corpus and the text length in the second corpus, including: calculating the average length L1 of the reading comprehension articles, the average length L2 of the questions, and the average length L3 of the answers in the first corpus; calculating the average length P1 of the reading comprehension articles, the average length P2 of the questions, and the average length P3 of the answers in the second corpus; calculating the second difference coefficient based on the difference between P1 and L1, the difference between P2 and L2, and the difference between P3 and L3; wherein the second difference coefficient is positively correlated with the difference between P1 and L1, the difference between P2 and L2, and the difference between P3 and L3, respectively.
[0011] Extractive reading comprehension tasks refer to reading comprehension tasks in which the answers to questions are fragments of the original text of the article. The three elements of extractive reading comprehension tasks are the article, the question, and the answer. Therefore, when targeting extractive reading comprehension tasks, the length of the article in the corpus includes the length of the article, the question, and the answer. Therefore, when calculating the second difference coefficient between the first corpus and the second corpus, it is necessary to consider the differences in the length of the article, the length of the question, and the length of the answer in the two types of corpora at the same time.
[0012] In an implementation of the first aspect, the use of the first training data to train a dedicated model for performing the target task includes: in the process of training the dedicated model using the first training data, regularly using a validation set to evaluate the degree of convergence of the dedicated model, and setting a learning rate used in the training process according to the degree of convergence; wherein the learning rate is set to be negatively correlated with the degree of convergence.
[0013] In the above implementation, the adjustment of the learning rate is a dynamic adjustment based on the degree of convergence of the model, that is, the learning rate is adjusted according to the actual performance of the model training, especially in the later stage of training, so that the learning rate can decay with the convergence of the model. The adjustment method is flexible and effective.
[0014] In an implementation of the first aspect, the learning rate is a decaying learning rate with warm-up, and in the process of training the dedicated model using the first training data, the degree of convergence of the dedicated model is evaluated regularly using a validation set, and the learning rate used in the training process is set according to the degree of convergence, including: in the learning rate decay stage in the process of training the dedicated model using the first training data, the degree of convergence of the dedicated model is evaluated regularly using a validation set, and the value of the learning rate is reduced according to the degree of convergence; wherein the amount of reduction in the learning rate value is negatively correlated with the degree of convergence.
[0015] The learning rate decay with warm-up uses a smaller learning rate at the beginning of training, and then selects the preset learning rate for learning after the model is stable, which is called learning rate warm-up. This is conducive to accelerating the convergence of the model and improving the training effect of the model. After using the preset learning rate for a period of time, the learning rate begins to decay, avoiding problems such as overfitting due to excessive learning rate when the model has tended to converge. In the process of learning rate decay, the learning rate is dynamically adjusted using the validation set. The adjustment method is flexible and objective, which is also conducive to improving the training effect.
[0016] In an implementation of the first aspect, the target task is an extractive reading comprehension task, the answer to the target task satisfies a first statistical law, and obtaining a second corpus includes: searching and / or constructing corpus for extractive reading comprehension according to the target field, and screening out corpus whose answers satisfy the first statistical law as the second corpus.
[0017] In the above implementation, the process of acquiring the second corpus is highly adapted to the target task actually to be performed by the trained dedicated model, so that the acquired corpus is more targeted and the training effect is more ideal (for the target task to be performed).
[0018] In an implementation of the first aspect, obtaining a general model includes: obtaining an original general model, wherein the original general model is a pre-trained language model within the general domain; obtaining a third corpus and a fourth corpus; wherein the third corpus is a corpus within the general domain, and the fourth corpus is a corpus within the target domain; determining a second data ratio based on a difference between the third corpus and the fourth corpus, and mixing the third corpus with the fourth corpus according to the second data ratio to obtain second training data; wherein the difference between the third corpus and the fourth corpus is negatively correlated with the second data ratio; and using the second training data to train the original general model to obtain the general model.
[0019] The original general model may refer to an existing public model, such as ERNIE, BERT model, etc. The above implementation method first fine-tunes the parameters of the original general model through training to obtain a general model, and then builds a special model based on the general model and further trains it. Among them, the parameters of the original general model are fine-tuned, and the second training data used is similar to the first training data, which is also composed of data in the general field and data in the target field, and the data ratio is also similar to the first training data, which is conducive to improving the quality of the trained general model.
[0020] It should be pointed out that the second training data and the first training data are different in form, because the first training data is aimed at downstream target tasks, such as reading comprehension tasks, text classification tasks, etc.; while the second training data is irrelevant to the target task and is used by upstream pre-training tasks, such as Masked LM, Next SentencePrediction and other tasks.
[0021] In a second aspect, an embodiment of the present application provides a model training method, comprising: obtaining an original general model, wherein the original general model is a pre-trained language model within a general domain; obtaining a third corpus and a fourth corpus; wherein the third corpus is a corpus within the general domain, and the fourth corpus is a corpus within the target domain to which a target task belongs, and the target task is a natural language processing task; determining a second data ratio based on the difference between the third corpus and the fourth corpus, and mixing the third corpus with the fourth corpus according to the second data ratio to obtain second training data; wherein the difference between the third corpus and the fourth corpus is negatively correlated with the second data ratio; using the second training data to train the original general model to obtain a general model; wherein the general model is used to form a special model for the adaptation structure related to the target task, and the special model is a model for performing the target task.
[0022] The method provided in the second aspect is similar to the method provided in the last implementation of the first aspect in obtaining a general model, but in the method provided in the second aspect, a special model is allowed to be directly constructed based on the general model, and the special model is allowed to be directly put into use without training (of course, the special model can also be put into use after training). Since the data in the general domain and the data in the target domain are reasonably mixed in the process of training to generate the general model, the obtained general model achieves a good balance between domain and generality, so that the performance of the special model constructed based on the general model is also improved.
[0023] In a third aspect, an embodiment of the present application provides a model training device, comprising: a first model acquisition module, used to acquire a general model, wherein the general model is a pre-trained, task-independent language model; a first data acquisition module, used to acquire a first corpus and a second corpus; wherein the first corpus is a corpus in a general field, and the second corpus is a corpus related to a target task in a target field, wherein the target task is a natural language processing task, and the target field is the field to which the target task belongs; a first data mixing module, used to determine a first data ratio based on the difference between the first corpus and the second corpus, and mix the first corpus with the second corpus according to the first data ratio to obtain first training data; wherein the difference between the first corpus and the second corpus is negatively correlated with the first data ratio; a first training module, used to use the first training data to train a special model for performing the target task; wherein the special model includes the general model and an adaptation structure related to the target task.
[0024] In a fourth aspect, an embodiment of the present application provides a model training device, comprising: a second model acquisition module, which acquires an original general model, wherein the original general model is a pre-trained language model within a general domain; a second data acquisition module, which acquires a third corpus and a fourth corpus; wherein the third corpus is a corpus within the general domain, and the fourth corpus is a corpus within the target domain to which a target task belongs, and the target task is a natural language processing task; a second data mixing module, which determines a second data ratio based on the difference between the third corpus and the fourth corpus, and mixes the third corpus with the fourth corpus according to the second data ratio to obtain second training data; wherein the difference between the third corpus and the fourth corpus is negatively correlated with the second data ratio; a second training module, which uses the second training data to train the original general model to obtain a general model; wherein the general model is used to form a special model for the adaptation structure related to the target task, and the special model is a model for performing the target task.
[0025] In a fifth aspect, an embodiment of the present application provides a computer-readable storage medium, on which computer program instructions are stored. When the computer program instructions are read and executed by a processor, the method provided by the first aspect or any possible implementation of the first aspect is executed.
[0026] In a sixth aspect, an embodiment of the present application provides an electronic device, comprising: a memory and a processor, wherein the memory stores computer program instructions, and when the computer program instructions are read and run by the processor, the method provided by the first aspect or any possible implementation of the first aspect is executed. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments of the present application will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0028] Figure 1 A flow chart of a model training method provided in an embodiment of the present application is shown;
[0029] Figure 2 A module diagram of a model training device provided in an embodiment of the present application is shown;
[0030] Figure 3 A module diagram of another model training device provided in an embodiment of the present application is shown;
[0031] Figure 4 A schematic diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0032] The technical scheme in the embodiment of the present application will be described below in conjunction with the accompanying drawings in the embodiment of the present application. It should be noted that similar reference numerals and letters represent similar items in the following drawings, so that once a certain item is defined in an accompanying drawing, it is not necessary to further define and explain it in the subsequent drawings. The term "include", "comprise" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or equipment including a series of elements not only includes those elements, but also includes other elements that are not clearly listed, or also includes elements inherent to such process, method, article or equipment. In the absence of more restrictions, the elements defined by the sentence "comprise one..." do not exclude the existence of other identical elements in the process, method, article or equipment including the elements. The terms "first", "second", "third" etc. are only used to distinguish descriptions and cannot be understood as indicating or implying relative importance.
[0033] Figure 1 A flow chart of a model training method provided in an embodiment of the present application is shown. The method can be, but is not limited to, executed by an electronic device. Figure 4 The possible structure of the electronic device is shown in the following text. Figure 4 Reference Figure 1 , the method comprising:
[0034] Step S110: Acquire a general model.
[0035] The general model is a language model that is unrelated to a specific natural language processing task. Its design purpose is mainly to learn the expression of the language itself, and is not used (or in most cases cannot be directly used) to perform specific tasks; in contrast, the dedicated model (appearing in step S140) is a model used to perform a specific natural language processing task. For simplicity, the specific natural language processing task will be referred to as the target task in the following text. The target task can be a reading comprehension task, a text classification task, a named entity recognition task, etc.
[0036] The target task often involves a specific field, such as finance and insurance, policies and regulations, education, communications and IT, etc., which may be referred to as the target field. In contrast, some texts do not belong to a specific field, such as those from daily conversations, which can be considered to belong to the general field.
[0037] Both the general model and the special model can be implemented by neural network in structure. The special model can be composed of the general model and the adaptation structure related to the target task. For example, adding a fully connected layer and sofmax layer after the general model can form a special model that can be used to perform the target task. The fully connected layer and sofmax layer here are the adaptation structures related to the target task, which can be designed differently according to different target tasks. In most cases, the general model is the main structure of the special model, and the adaptation structure is relatively simple.
[0038] The general model is a pre-trained model. The task of training the general model can be called a pre-training task. The pre-training task can also be called an upstream task, and the target task can be called a downstream task. Pre-training tasks are generally unrelated to specific fields. Commonly used pre-training tasks include Masked LM, Next Sentence Prediction, etc. There are two main ways to obtain a general model:
[0039] Method 1: Obtain directly from public channels, such as the BERT Chinese model released by Google and the ERNIE model released by Baidu. These models are often trained using corpora in general fields. The model input can be a sentence, and the output is a vectorized representation of the sentence, or it can be regarded as extracting the features of the sentence. Subsequently, corresponding downstream tasks can be performed based on these features. Relatively speaking, ERNIE has some advantages in Chinese processing, specifically:
[0040] (1) Compared with the BERT Chinese model, the Masked LM task in ERNIE’s pre-training task uses word masking that is more suitable for Chinese habits, which enables ERNIE to perform better when processing Chinese tasks.
[0041] (2) Compared with the BERT Chinese model, the quantity and quality of ERNIE’s pre-training corpus are better, making ERNIE more effective in performing Chinese tasks.
[0042] Therefore, if the target task is a Chinese task, the ERNIE model can be selected first. However, it should be understood that since language models are also constantly being improved, it is not ruled out that the quality of the BERT Chinese model will exceed that of the ERNIE model after improvement, so the model selection strategy is not static.
[0043] Method 2: First, obtain a model from a public channel. We may call the model obtained at this time the original general model. Then, fine-tune the parameters of the original general model through training to obtain a general model. For example, you can use the corpus in the target domain to fine-tune the parameters of the original general model so that the original general model is appropriately shifted to the target domain. Regarding method 2, we will further illustrate it with examples later, so we will not introduce it in detail here.
[0044] Relatively speaking, it is simpler to obtain a general model through method one. The general model obtained through method two may have better quality due to parameter fine-tuning, and is also more conducive to the subsequent training of special models.
[0045] Step S120: Obtain the first corpus and the second corpus.
[0046] The first corpus refers to the corpus in the general field, and the second corpus refers to the corpus in the target field that is related to the target task. It can be understood that the higher the degree of adaptation of the second corpus to the target task, the better the special model obtained by training will achieve when performing the target task.
[0047] Taking the target task as an extractive reading comprehension task, the so-called extractive reading comprehension task means: answering questions about an article after reading it, and the answer to the question must be a fragment of the original text (such as a word or sentence in the original text). If the answer to the target task satisfies the first statistical law, the corpus for extractive reading comprehension can be searched and / or constructed according to the target field, and the corpus whose answer satisfies the first statistical law can be selected as the second corpus. The following is a specific example to explain its meaning:
[0048] (1) If the answer to the target task is short (here, the short answer is the first statistical rule), for example, the answer is some named entities or time (for example, in the air ticket booking scenario, it is necessary to analyze the destination and departure time of the air ticket from a paragraph spoken by the customer), then you can first search and / or construct a corpus for extractive reading comprehension based on the target domain, and then select the corpus with shorter answers and / or answers that are named entities or time as the second corpus.
[0049] (2) If the answer to the target task is long (here, the long answer is the first statistical rule), for example, the answer is an explanation of certain concepts or an elaboration of certain facts (for example, in the teaching scenario of history class, the answer is the impact of overthrowing the feudal monarchy on Chinese society based on the content of the text), then you can first search and / or construct the corpus for extractive reading comprehension based on the target field, and then select the corpus with long answers and / or answers that are explanations of certain concepts or elaborations of certain facts as the second corpus.
[0050] (3) If the answers to the target task can be statistically analyzed to reveal a certain pattern, for example, if a question-answering robot is designed to answer science questions, it is difficult to find a statistical pattern from the length of the answers, but it can be determined that the answers are all science-related, so the answer belonging to science is the first statistical pattern. In this case, we can first search and / or construct a corpus for extractive reading comprehension based on the target domain, and then select the corpus with answers belonging to science as the second corpus.
[0051] The acquisition of corpus can be performed automatically by the program or manually, or the program can perform a preliminary screening and then verify or further screen it manually.
[0052] Step S130: determining a first data ratio based on the difference between the first corpus and the second corpus, and mixing the first corpus with the second corpus according to the first data ratio to obtain first training data.
[0053] As mentioned before, the general model is independent of the specific task. Therefore, in order to make the obtained special model relevant to the target task, a second corpus related to the target task must be introduced into the training data of the special model to achieve domain shift.
[0054] However, in the scheme of the present application, the first training data does not use only the second corpus, but a mixture of the first corpus and the second corpus. The reason is that although the data in a specific field (the second corpus) has high-intensity domain knowledge, its language expression ability is insufficient, and although the data in a general field (the first corpus) has no or basically no domain knowledge, its language expression ability is strong. The mixed use of the two types of corpora ensures that the first training data has good knowledge strength, and prevents it from being overly biased towards domain knowledge, while giving up the language expression ability in the general field, or even causing problems such as overfitting, which is beneficial to improving the performance of the trained dedicated model.
[0055] Furthermore, the ratio of the first corpus to the second corpus in the first training data (i.e., the first data ratio, for example, may refer to the ratio of the number of samples of the first corpus to the number of samples of the second corpus in the first training data) is very important, because this ratio directly determines the balance between the domain and universality of the dedicated model. Therefore, reasonably setting the first data ratio is conducive to improving the performance of the trained dedicated model.
[0056] In the scheme of the present application, the first data ratio is determined based on the difference between the first corpus and the second corpus, and the difference between the first corpus and the second corpus is negatively correlated with the first data ratio. If the difference between the first corpus and the second corpus is greater, it means that the domain nature of the target domain is stronger. At this time, the proportion of the first corpus in the first training data will be smaller, because more domain data is needed to reflect this feature of the strong domain; conversely, if the difference between the first corpus and the second corpus is smaller, it means that the domain nature of the target domain is weaker. At this time, the proportion of the first corpus in the first training data will be larger. The present application does not limit the specific form of the negative correlation. For example, in the simplest case, it can be an inverse proportional relationship, but it can also be other relationships.
[0057] In some implementations, the first data ratio may be calculated as follows:
[0058] Step A: Obtain a first difference coefficient (denoted as λ1); wherein the first difference coefficient is positively correlated with the frequency of occurrence of keywords in the target domain in the test corpus in the target domain. The present application does not limit the specific form of the positive correlation relationship. For example, in the simplest case, it can be a positive proportional relationship, but it can also be other relationships.
[0059] Keywords in the target field can be specified by domain experts. These keywords are representative and basically only appear in the corpus in the target field, and rarely appear in the general field. For example, keywords in the communication and IT fields such as "modulation", "demodulation", and "frequency division multiplexing" are rarely used in the general field. Therefore, the first difference coefficient can characterize the difference between the first corpus and the second corpus at the knowledge level, or it mainly describes the difference between the two types of corpora due to the different fields they belong to. There is no limit to the selection method of the test corpus in the target field, but there is no necessary connection between the corpus and the second corpus. The following lists the first difference coefficients calculated by the above method in some fields:
[0060] field Financial and insurance fields Policy and regulations Education Communications and IT fields First coefficient of difference 0.75 0.91 0.32 1
[0061] Among them, the first difference coefficient in the field of communication and IT was artificially set to 1 (artificially determined to have a strong domain specificity in this field), and the first difference coefficients in other fields were calculated accordingly with reference to the first difference coefficient in the field of communication and IT (not exceeding 1).
[0062] The value of the first difference coefficient is only related to the field to which the corpus belongs, so it can be used for a long time after being calculated, without needing to be updated or at least not frequently. For example, the first difference coefficient can be calculated in advance and the result can be stored in a table similar to the above, and the table can be directly queried when the first data ratio needs to be calculated.
[0063] Step B: Calculate a second difference coefficient (denoted as λ1) based on the difference between the text length in the first corpus and the text length in the second corpus; wherein the second difference coefficient is positively correlated with the difference between the text lengths. The present application does not limit the specific form of the positive correlation, for example, in the simplest case it can be a positive proportional relationship, but it can also be other relationships.
[0064] Since the second difference coefficient is calculated based on the difference in text length between the first corpus and the second corpus, it represents the difference between the first corpus and the second corpus at the language structure level, or mainly describes the difference brought about by the language characteristics of the two types of corpora themselves.
[0065] For example, when the target task is an extractive reading comprehension task, the first coefficient of difference can be calculated as follows:
[0066] First, calculate the average length L1 of the reading comprehension articles, the average length L2 of the questions, and the average length L3 of the answers in the first corpus (for example, the SQUAD dataset);
[0067] Then, calculate the average length of the reading comprehension passages P1, the average length of the questions P2, and the average length of the answers P3 in the second corpus;
[0068] Finally, the second difference coefficient is calculated according to the difference between P1 and L1, the difference between P2 and L2, and the difference between P3 and L3; wherein the second difference coefficient is positively correlated with the difference between P1 and L1, the difference between P2 and L2, and the difference between P3 and L3, respectively.
[0069] For example, in an optional solution, the second difference coefficient calculation formula is as follows:
[0070] λ2=g(P1 / L1)*g(P2 / L2)*g(P3 / L3)
[0071] Among them, g(.) is a predefined function. If x>y, then g(x / y)=x / y, otherwise g(x / y)=y / x.
[0072] It can be understood that the above calculation formula for the second difference coefficient is only an example, and different formulas may be used in different schemes. For example, certain coefficients may be multiplied on the right side of the equation, or the three parts on the right side of the equation may be weighted summed instead of multiplied, or the difference in text length may not necessarily be defined as a ratio, but may also be defined as the absolute value of the difference, and so on.
[0073] It should also be understood that the above is an example of an extractive reading comprehension task. If it is not an extractive reading comprehension task, concepts such as articles, questions, and answers may not exist in the corpus. In this case, another method must be used to calculate the difference in text length, such as calculating the difference in sentence length in the corpus.
[0074] Step C: determining a first data ratio according to the first difference coefficient and the second difference coefficient.
[0075] For example, the first data ratio can be defined as 1:(λ1*λ2), and of course other definitions can be adopted, such as 1:(λ1+λ2), etc., which are not limited in this application. The specific definition method can be determined according to the actual performance of the trained dedicated model.
[0076] In addition, it is not excluded that in some implementations, the first data ratio is determined only according to the first difference coefficient or the second difference coefficient. For example, for reading comprehension tasks, there is a SQUAD dataset that is widely recognized by the academic community, but for other natural language processing tasks, there may not be such a well-defined dataset. In this case, the first difference coefficient can be directly used as the first data ratio.
[0077] As for how to mix the two types of corpora according to the first data, this application does not limit it. For example, they can be mixed together evenly according to the first data ratio, or randomly mixed according to the first data ratio, and so on.
[0078] After obtaining the first training data, the first training data can also be cleaned to improve the data quality. Of course, the data cleaning step is optional. For example, if the quality of the first training data already meets the training requirements, there is no need to perform data cleaning. In addition, data cleaning can also be performed after obtaining the first corpus and the second corpus, and it is not necessary to wait until the first training data is obtained. Data cleaning projects include but are not limited to:
[0079] (1) converting the full-width symbols and half-width symbols in the first training data into a unified full-width and half-width form, such as converting all of them into half-width symbols, or converting all of them into full-width symbols;
[0080] (2) Convert the punctuation marks in the first training data into a unified punctuation form. For example, the same punctuation marks in Chinese and English texts may have different symbols (such as the period in Chinese and English is a circle and a dot respectively), and a unified form is needed to avoid the computer system thinking that they belong to symbols with different meanings.
[0081] When performing data cleaning, you can select one or more of the above steps to perform according to your needs.
[0082] The form of the samples in the first training data is related to the target task. For example, if the target task is an extractive reading comprehension task, each sample in the first training data may include an article, a question, and two labels (used to indicate the starting and ending positions of the answer in the article).
[0083] Step S140: Train the dedicated model using the first training data.
[0084] Before executing step S140, a dedicated model should be constructed based on the general model obtained in step S110 and the adaptation structure related to the target task. The trained dedicated model can be used to perform the target task. For example, if the target task is an extractive reading comprehension task, the model input can be the article and the question, and the output can be the start and end positions of the answer in the article.
[0085] How to update the model parameters according to the training data is a prior art and will not be described in detail here. The following mainly introduces the setting of the learning rate parameter during the training of the dedicated model:
[0086] In some implementations of the present application, in the process of training the dedicated model using the first training data, the validation set is periodically used to evaluate the convergence degree of the dedicated model, and the learning rate used in the training process is set according to the convergence degree.
[0087] The periodicity here can refer to a specified number of training steps (one step is defined as updating the model parameters once with a batch of data) or a specified duration. The degree of convergence of the model can be defined based on the prediction error of the model: for example, it can be directly defined as the prediction error or the mapping value of the prediction error; for another example, a benchmark error can be preset as the error of the model in an ideal convergence state, and the actual error currently calculated by the model is subtracted from the benchmark error, and the difference or the mapping value of the difference is used as the degree of convergence of the model, and so on.
[0088] Among them, the learning rate is set to be negatively correlated with the degree of convergence. That is to say, when the dedicated model is still early to converge, the learning rate should be set larger to speed up the convergence of the model. The closer the dedicated model is to convergence, the smaller the learning rate should be set to avoid problems such as model instability and overfitting.
[0089] It should be pointed out that in the above implementation, the adjustment of the learning rate is a dynamic adjustment based on the degree of convergence of the model, that is, the learning rate is adjusted according to the actual performance of the model on the validation set, rather than adjusting a fixed value each time. The adjustment method is flexible, objective and effective, which is conducive to improving the quality of the specialized model obtained through training.
[0090] Furthermore, the above learning rate can be set as a warm-up attenuated learning rate, that is, a smaller learning rate is used in the initial stage of training, and the preset learning rate is selected for learning after the model is stable, which is the so-called learning rate warm-up. This is conducive to accelerating the convergence speed of the model and improving the training effect of the model. After using the preset learning rate for a period of time, the learning rate is started to avoid instability, overfitting and other problems caused by excessive learning rate when the model has tended to converge. In the process of learning rate decay, the degree of convergence of the dedicated model can be regularly evaluated using the validation set in the above manner, and the value of the learning rate can be reduced according to the degree of convergence; wherein, the reduction in the learning rate value is negatively correlated with the degree of convergence.
[0091] As mentioned above, there are at least two ways to obtain the general model in step S110. The second way is further described below with an example:
[0092] In some implementations, the general model can be obtained as follows:
[0093] Step A: Obtain an original general model; the original general model is a pre-trained language model in a general field, such as the BERT Chinese model released by Google and the ERNIE model released by Baidu. In a Chinese environment, the ERNIE model can be preferred.
[0094] Step B: Obtain the third corpus and the fourth corpus; wherein the third corpus is a corpus in the general field, and the fourth corpus is a corpus in the target field. The acquisition of the third corpus and the fourth corpus can refer to step S120, but it should be pointed out that since the general model is irrelevant to the specific task, the fourth corpus can directly select the data in the target field, and does not involve the problem of adaptation to the target task.
[0095] Step C: Determine a second data ratio based on the difference between the third corpus and the fourth corpus, and mix the third corpus with the fourth corpus according to the second data ratio to obtain second training data.
[0096] This step can be implemented with reference to step S130, and will not be described again. However, it should be noted that the second training data and the first training data are different in form, because the first training data is for the downstream target task, while the second training data is used by the upstream pre-training task. For example, each sample in the second training data may include two consecutive sentences.
[0097] Step D: Use the second training data to train the original general model to obtain a general model.
[0098] This step can be implemented with reference to step S140. Alternatively, the learning rate can be adjusted in a simpler manner, such as attenuating the learning rate to 70% of the previous one after each training round (a round is defined as when all training data are used once).
[0099] The above implementation method obtains a general model by fine-tuning the parameters of the original model. The second training data used is similar to the first training data, and is also composed of data in the general domain and data in the target domain. The data ratio is also similar to the first training data. Therefore, the general model can take into account both domain specificity and generality, and its quality is high, which is conducive to the training of subsequent special models.
[0100] In the above-proposed schemes, the obtained general model is used to build a special model, and the special model is further trained to finally obtain a model that can be used to perform the target task. However, it is not excluded that in other schemes, after building a special model using the obtained general model (for example, through steps A to D above), the special model is directly put into use without adjusting the parameters of the special model (of course, the special model can be put into use after adjusting the parameters). These schemes reasonably mix the data in the general field and the data in the target field in the process of training and generating the general model, so that the obtained general model achieves a good balance between domain and generality, thereby improving the performance of the special model built based on the general model. Therefore, when the training time is limited, it is not excluded that such an approach can be adopted.
[0101] Figure 2 FIG. 2 shows a functional module diagram of a model training device 200 provided in an embodiment of the present application. Figure 2 , the model training device 200 comprises:
[0102] A first model acquisition module 210 is used to acquire a general model, where the general model is a pre-trained, task-independent language model;
[0103] A first data acquisition module 220 is used to acquire a first corpus and a second corpus; wherein the first corpus is a corpus in a general field, and the second corpus is a corpus related to a target task in a target field, wherein the target task is a natural language processing task, and the target field is the field to which the target task belongs;
[0104] A first data mixing module 230 is used to determine a first data ratio based on the difference between the first corpus and the second corpus, and mix the first corpus with the second corpus according to the first data ratio to obtain first training data; wherein the difference between the first corpus and the second corpus is negatively correlated with the first data ratio;
[0105] The first training module 240 is used to train a dedicated model for performing the target task using the first training data; wherein the dedicated model includes the general model and an adaptation structure related to the target task.
[0106] In one implementation of the model training device 200, the first data mixing module 230 determines the data ratio based on the difference between the first corpus and the second corpus, including: obtaining a first difference coefficient, the first difference coefficient is positively correlated with the frequency of occurrence of keywords in the target field in the test corpus in the target field; calculating a second difference coefficient based on the difference between the text length in the first corpus and the text length in the second corpus, the second difference coefficient is positively correlated with the difference between the text lengths; determining the first data ratio based on the first difference coefficient and the second difference coefficient.
[0107] In one implementation of the model training device 200, the target task is an extractive reading comprehension task, and the first data mixing module 230 calculates a second difference coefficient based on the difference between the text length in the first corpus and the text length in the second corpus, including: calculating the average length L1 of the reading comprehension articles, the average length L2 of the questions, and the average length L3 of the answers in the first corpus; calculating the average length P1 of the reading comprehension articles, the average length P2 of the questions, and the average length P3 of the answers in the second corpus; calculating the second difference coefficient based on the difference between P1 and L1, the difference between P2 and L2, and the difference between P3 and L3; wherein the second difference coefficient is positively correlated with the difference between P1 and L1, the difference between P2 and L2, and the difference between P3 and L3, respectively.
[0108] In one implementation of the model training device 200, the first training module 240 uses the first training data to train a dedicated model for performing the target task, including: in the process of training the dedicated model using the first training data, regularly using a validation set to evaluate the degree of convergence of the dedicated model, and setting a learning rate used in the training process according to the degree of convergence; wherein the learning rate is set to be negatively correlated with the degree of convergence.
[0109] In one implementation of the model training device 200, the learning rate is a decaying learning rate with warm-up. In the process of training the special model using the first training data, the first training module 240 periodically uses the validation set to evaluate the convergence degree of the special model, and sets the learning rate used in the training process according to the convergence degree, including: in the learning rate decay stage in the process of training the special model using the first training data, periodically uses the validation set to evaluate the convergence degree of the special model, and reduces the value of the learning rate according to the convergence degree; wherein the reduction amount of the learning rate value is negatively correlated with the convergence degree.
[0110] In one implementation of the model training device 200, the target task is an extractive reading comprehension task, the answer to the target task satisfies a first statistical law, and the first data acquisition module 220 acquires a second corpus, including: searching and / or constructing corpus for extractive reading comprehension according to the target field, and screening out corpus whose answers satisfy the first statistical law as the second corpus.
[0111] In one implementation of the model training device 200, the first model acquisition module 210 acquires a general model, including: acquiring an original general model, wherein the original general model is a pre-trained language model within the general domain; acquiring a third corpus and a fourth corpus; wherein the third corpus is a corpus within the general domain, and the fourth corpus is a corpus within the target domain; determining a second data ratio based on the difference between the third corpus and the fourth corpus, and mixing the third corpus with the fourth corpus according to the second data ratio to obtain second training data; wherein the difference between the third corpus and the fourth corpus is negatively correlated with the second data ratio; and using the second training data to train the original general model to obtain the general model.
[0112] The model training device 200 provided in the embodiment of the present application, its implementation principle and the technical effects produced have been introduced in the aforementioned method embodiment. For the sake of brief description, for matters not mentioned in the device embodiment, reference may be made to the corresponding content in the method embodiment.
[0113] Figure 3FIG. 3 shows a functional module diagram of a model training device 300 provided in an embodiment of the present application. Figure 3 , the model training device 300 includes:
[0114] A second model acquisition module 310 acquires an original general model, where the original general model is a pre-trained language model in a general domain;
[0115] The second data acquisition module 320 acquires a third corpus and a fourth corpus; wherein the third corpus is a corpus in the general field, the fourth corpus is a corpus in the target field to which the target task belongs, and the target task is a natural language processing task;
[0116] A second data mixing module 330 determines a second data ratio based on the difference between the third corpus and the fourth corpus, and mixes the third corpus with the fourth corpus according to the second data ratio to obtain second training data; wherein the difference between the third corpus and the fourth corpus is negatively correlated with the second data ratio;
[0117] The second training module 340 uses the second training data to train the original general model to obtain a general model; wherein the general model is used to form a special model with an adaptation structure related to the target task, and the special model is a model used to perform the target task.
[0118] The model training device 300 provided in the embodiment of the present application, its implementation principle and the technical effects produced have been introduced in the aforementioned method embodiment. For the sake of brief description, for matters not mentioned in the device embodiment, reference may be made to the corresponding content in the method embodiment.
[0119] Figure 4 FIG. 4 shows a possible structure of an electronic device 400 provided in an embodiment of the present application. Figure 4 The electronic device 400 includes: a processor 410, a memory 420 and a communication interface 430. These components are interconnected and communicate with each other through a communication bus 440 and / or other forms of connection mechanisms (not shown).
[0120] The memory 420 includes one or more (only one is shown in the figure), which may be, but not limited to, a random access memory (RAM), 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), etc. The processor 410 and other possible components may access the memory 420, and read and / or write data therein.
[0121] The processor 410 includes one or more (only one is shown in the figure), which can be an integrated circuit chip with signal processing capabilities. The above-mentioned processor 410 can be a general-purpose processor, including a central processing unit (CPU), a micro control unit (MCU), a network processor (NP) or other conventional processors; it can also be a dedicated processor, including a graphics processor (GPU), 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. In addition, when there are multiple processors 410, some of them can be general-purpose processors and the other part can be dedicated processors.
[0122] The communication interface 430 includes one or more (only one is shown in the figure), which can be used to communicate directly or indirectly with other devices to exchange data. The communication interface 430 can include an interface for wired and / or wireless communication.
[0123] One or more computer program instructions may be stored in the memory 420 , and the processor 410 may read and execute these computer program instructions to implement the model training method and other desired functions provided in the embodiments of the present application.
[0124] Understandably, Figure 4 The structure shown is for illustration only. The electronic device 400 may also include Figure 4 More or fewer components as shown, or with Figure 4 Different configurations are shown. Figure 4 The components shown in the figure can be implemented by hardware, software or a combination thereof. The electronic device 400 may be a physical device, such as a PC, a laptop, a tablet computer, a mobile phone, a server, an embedded device, etc., or a virtual device, such as a virtual machine, a virtualized container, etc. Moreover, the electronic device 400 is not limited to a single device, but may also be a combination of multiple devices or a cluster consisting of a large number of devices.
[0125] The present application also provides a computer-readable storage medium on which computer program instructions are stored. When the computer program instructions are read and executed by a computer processor, the model training method provided by the present application is executed. For example, the computer-readable storage medium can be implemented as Figure 4 The memory 420 in the electronic device 400.
[0126] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, 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 communication interfaces, and the indirect coupling or communication connection of the devices or units can be electrical, mechanical or other forms.
[0127] In addition, the units described as separate components may or may not be physically separated, and the components shown 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 solution of this embodiment.
[0128] Furthermore, the functional modules in the various embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.
[0129] The above description is only an embodiment of the present application and is not intended to limit the protection scope of the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A model training method, characterized in that: include: Obtaining a general model, where the general model is a pre-trained, task-independent language model; Acquire a first corpus and a second corpus; wherein the first corpus is a corpus in a general field, and the second corpus is a corpus in a target field related to a target task, the target task is a natural language processing task, and the target field is the field to which the target task belongs; Determining a first data ratio based on the difference between the first corpus and the second corpus, and mixing the first corpus with the second corpus according to the first data ratio to obtain first training data; wherein the difference between the first corpus and the second corpus is negatively correlated with the first data ratio; Using the first training data to train a dedicated model for performing the target task; wherein the dedicated model includes the general model and an adaptation structure related to the target task; The obtaining of the general model comprises: Acquire an original general model, where the original general model is a pre-trained language model in the general domain; Acquire a third corpus and a fourth corpus; wherein the third corpus is corpus in the general field, and the fourth corpus is corpus in the target field; Determining a second data ratio based on the difference between the third corpus and the fourth corpus, and mixing the third corpus with the fourth corpus according to the second data ratio to obtain second training data; wherein the difference between the third corpus and the fourth corpus is negatively correlated with the second data ratio; The original general model is trained using the second training data to obtain the general model.
2. The training method according to claim 1, characterized in that: The determining of the data ratio based on the difference between the first corpus and the second corpus includes: Obtaining a first difference coefficient, where the first difference coefficient is positively correlated with the frequency of occurrence of the keyword in the target domain in the test corpus in the target domain; Calculating a second difference coefficient according to the difference between the text length in the first corpus and the text length in the second corpus, wherein the second difference coefficient is positively correlated with the difference between the text lengths; The first data ratio is determined according to the first difference coefficient and the second difference coefficient.
3. The training method according to claim 2, characterized in that: The target task is an extractive reading comprehension task, and the second difference coefficient is calculated according to the difference between the text length in the first corpus and the text length in the second corpus, including: Calculate the average length L1 of the reading comprehension articles, the average length L2 of the questions, and the average length L3 of the answers in the first corpus; Calculate the average length P1 of the reading comprehension articles, the average length P2 of the questions, and the average length P3 of the answers in the second corpus; The second difference coefficient is calculated according to the difference between P1 and L1, the difference between P2 and L2, and the difference between P3 and L3; wherein the second difference coefficient is positively correlated with the difference between P1 and L1, the difference between P2 and L2, and the difference between P3 and L3, respectively.
4. The training method according to claim 1, characterized in that: The using the first training data to train a dedicated model for performing the target task includes: In the process of training the dedicated model using the first training data, the convergence degree of the dedicated model is evaluated regularly using a validation set, and a learning rate used in the training process is set according to the convergence degree; wherein the learning rate is set to be negatively correlated with the convergence degree.
5. The training method according to claim 4, characterized in that: The learning rate is a decaying learning rate with warm-up. In the process of training the dedicated model with the first training data, the convergence degree of the dedicated model is evaluated periodically with a validation set, and the learning rate used in the training process is set according to the convergence degree, including: During the learning rate decay stage in the process of training the dedicated model using the first training data, the validation set is periodically used to evaluate the convergence degree of the dedicated model, and the value of the learning rate is reduced according to the convergence degree; wherein the amount of reduction in the learning rate value is negatively correlated with the convergence degree.
6. The training method according to claim 1, characterized in that: The target task is an extractive reading comprehension task, the answer to the target task satisfies a first statistical rule, and obtaining a second corpus includes: According to the target domain, corpus for extractive reading comprehension is searched and / or constructed, and corpus whose answers satisfy the first statistical law is screened out as the second corpus.
7. A model training device, characterized in that: include: A first model acquisition module, used to acquire a general model, where the general model is a pre-trained, task-independent language model; A first data acquisition module is used to acquire a first corpus and a second corpus; wherein the first corpus is a corpus in a general field, and the second corpus is a corpus related to a target task in a target field, wherein the target task is a natural language processing task, and the target field is the field to which the target task belongs; A first data mixing module, configured to determine a first data ratio based on the difference between the first corpus and the second corpus, and mix the first corpus with the second corpus according to the first data ratio to obtain first training data; wherein the difference between the first corpus and the second corpus is negatively correlated with the first data ratio; A first training module, used to train a dedicated model for performing the target task using the first training data; wherein the dedicated model includes the general model and an adaptation structure related to the target task; The first model acquisition module acquires a general module, including: acquiring an original general model, wherein the original general model is a pre-trained language model within the general domain; acquiring a third corpus and a fourth corpus; wherein the third corpus is a corpus within the general domain, and the fourth corpus is a corpus within the target domain; determining a second data ratio based on the difference between the third corpus and the fourth corpus, and mixing the third corpus with the fourth corpus according to the second data ratio to obtain second training data; wherein the difference between the third corpus and the fourth corpus is negatively correlated with the second data ratio; and using the second training data to train the original general model to obtain the general model.
