A text processing method, product, device and medium
By inserting new knowledge adapters into large language models, it solves the time-consuming and resource problems when integrating new knowledge, maintains model performance, improves text reasoning efficiency and feature fusion capabilities, and adapts to the dependence on old and new knowledge.
Patent Information
- Application Number
- CN202410853486.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-28
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2044-06-28
AI Technical Summary
Existing large language models need to be retrained when incorporating new knowledge, which is time-consuming and requires a large amount of computing resources. Incremental training may cause the model to forget learned knowledge, thereby reducing the efficiency of text reasoning.
By inserting a new knowledge adapter into the large language model, using the injected new knowledge samples for training, keeping the parameters of the large model unchanged, the new knowledge adapter is stacked by multiple sub-modules, including linear layers and activation functions, and is related to the previous ones in the transformer block. Feed neural network modules are placed side by side, features are fused through gated units, and the last layer of the transformer block is inserted to reduce interference.
It achieves the efficient and low-computing power of integrating new knowledge into large language models, keeping original knowledge from being forgotten, improving text reasoning efficiency, enhancing feature fusion capabilities, adapting to the dependence on old and new knowledge, and supporting incremental learning.
Smart Images

Figure CN118396126B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of large language models, and in particular to a text processing method, product, device and medium. Background Art
[0002] Large Language Model (LLM) usually refers to a deep learning model that is trained with a large amount of data and has a large number of parameters. The development of large language models has brought great progress to the fields of natural language processing (NLP) and artificial intelligence. However, new knowledge will appear in practice, and this new knowledge needs to be incorporated into the model so that the model can understand and generate relevant content.
[0003] In order to incorporate new knowledge into the model, related technologies usually retrain the entire large model, which is very time-consuming and requires a lot of computing resources. Incremental training of large language models may cause the model to forget the knowledge it has learned, which in turn leads to a decrease in the efficiency of text reasoning when the model is used to reason about the text.
[0004] It can be seen from this that how to integrate new knowledge into the existing large language model efficiently and with low computing power without letting the model forget the knowledge it has already learned, and thereby improve the efficiency of text reasoning using the model, is a technical problem that people in this field urgently need to solve. Summary of the invention
[0005] The purpose of the present invention is to provide a text processing method, product, device and medium to solve the technical problems that when incorporating new knowledge into the model, retraining the entire large model is time-consuming and requires a lot of computing resources, and incremental training of the large language model may cause the model to forget the knowledge that has been learned, thereby resulting in a decrease in the efficiency of text reasoning when the model is used to reason about the text.
[0006] In order to solve the above technical problems, the present invention provides a text processing method, comprising:
[0007] Get the text to be inferred;
[0008] In the case where it is detected that the text to be inferred contains new knowledge, the text to be inferred is input into the new knowledge adapter and the large language model; wherein the new knowledge adapter is inserted into the converter block in the large language model, and is placed in parallel with the feedforward neural network module in the converter block, and the adaptation features output by the new knowledge adapter are fused with the original features output by the feedforward neural network module in the converter block through a gating unit; the new knowledge adapter is formed by stacking a plurality of submodules, and each submodule contains a linear layer and an activation function; when training, the new knowledge adapter is trained using the injected new knowledge samples, and the parameters in the large language model are kept unchanged during the training process;
[0009] The text to be inferred is predicted by using the new knowledge adapter and the large language model to obtain a prediction result corresponding to the text to be inferred.
[0010] On the one hand, the new knowledge adapter is inserted into a target transformer block in a transformer block in the large language model; wherein the target transformer block is a transformer block located at the last layer among all transformer blocks in the large language model.
[0011] On the other hand, determining the function corresponding to the new knowledge adapter includes:
[0012] Apply weights and biases to the injected new knowledge samples for linear transformation and obtain the linear transformation results;
[0013] Apply activation function to transform the linear transformation result;
[0014] The results of the activation function transformation corresponding to each sub-module are stacked up to obtain the function corresponding to the new knowledge adapter.
[0015] On the other hand, the function expression corresponding to the new knowledge adapter is:
[0016] ;
[0017] in, It means text. Represents the function corresponding to the new knowledge adapter, Indicates The weights of the linear transformation, Indicates The linear transformation bias, It indicates the number of submodules. represents the activation function, It indicates stacking processing;
[0018] Among them, the expression of the activation function is:
[0019] .
[0020] On the other hand, the corresponding expression for fusing the adaptation features output by the new knowledge adapter and the original features output by the feedforward neural network module in the converter block through the gating unit is:
[0021] ;
[0022] in, represents the output result of the gating unit, represents the normalized function, represents the weight matrix of the gating unit, represents the bias vector of the gating unit, represents the adaptation characteristics output by the new knowledge adapter, It represents the original features output by the feedforward neural network module in the transformer block.
[0023] On the other hand, determining the output result after the new knowledge adapter and the large language model includes:
[0024] Obtaining a first weight configured for the output result of the new knowledge adapter and a second weight adapted for the output result of the large language model; wherein the sum of the first weight and the second weight is 1;
[0025] Obtaining a first product result of the adaptation feature output by the new knowledge adapter and the first weight;
[0026] Obtaining a second product result of the original feature output by the feedforward neural network module in the converter block and the second weight;
[0027] The sum of the first multiplication result and the second multiplication result is used as the output result after passing through the new knowledge adapter and the large language model.
[0028] On the other hand, determining the number of submodules in the new knowledge adapter includes:
[0029] Obtaining the number of new knowledge samples injected into the new knowledge adapter;
[0030] The number of submodules in the new knowledge adapter is determined according to the number of the injected new knowledge samples; wherein the number of submodules in the new knowledge adapter is positively correlated with the number of the injected new knowledge samples.
[0031] On the other hand, detecting whether the text to be inferred contains new knowledge includes:
[0032] Detecting whether the text to be inferred contains new knowledge by a new knowledge identifier; wherein the new knowledge identifier is formed by stacking a plurality of transform encoder blocks;
[0033] Training the new knowledge identifier includes:
[0034] Obtaining original corpus samples for training the large language model;
[0035] Acquire new corpus and expand the new corpus to obtain expanded corpus;
[0036] Randomly sampling and selecting negative samples from the original corpus sample and randomly sampling and selecting positive samples from the expanded corpus;
[0037] Take keywords from each sample for comparative learning and classification to determine whether the sample belongs to new knowledge or old knowledge; wherein the sample includes positive samples and negative samples.
[0038] On the other hand, acquiring new corpus and expanding the new corpus includes:
[0039] Acquire the new corpus, and expand the new corpus in at least one of the following ways; wherein the new corpus has the same semantics as the expanded corpus;
[0040] Method 1: Obtain the reply content corresponding to the new corpus mentioned in the reply;
[0041] Adding the reply content to the new corpus;
[0042] Method 2: Perform synonym replacement and sentence structure adjustment on the new corpus to expand the new corpus.
[0043] On the other hand, the detecting whether the text to be inferred contains new knowledge by a new knowledge identifier includes:
[0044] Acquire a feature set representing the expanded corpus in the new knowledge identifier and acquire a feature representing the text to be inferred;
[0045] Obtaining similarities between features representing the text to be inferred and each feature in the feature set;
[0046] If it is detected that the similarity is greater than or equal to a preset value, determining that the feature characterizing the text to be inferred belongs to the feature set, and determining that the text to be inferred contains new knowledge detected by the new knowledge identifier;
[0047] If it is detected that the similarity is less than the preset value, it is determined that the text to be inferred contains no new knowledge as detected by the new knowledge identifier.
[0048] In another aspect, the method further comprises:
[0049] In the case where it is detected that the text to be inferred does not contain new knowledge, inputting the text to be inferred into the large language model;
[0050] The large language model is used to perform inference prediction on the text to be inferred, so as to obtain a prediction result corresponding to the text to be inferred.
[0051] On the other hand, training the new knowledge adapter and / or training the new knowledge identifier includes:
[0052] Using a preset optimizer and selecting a preset learning rate to control the convergence of the new knowledge adapter and / or to control the convergence of the new knowledge identifier;
[0053] In the case where it is detected that the new knowledge adapter has converged and / or the new knowledge identifier has converged, it is determined that the training of the new knowledge adapter and / or the training of the new knowledge identifier is completed.
[0054] In order to solve the above technical problem, the present invention also provides a computer program product, including a computer program / instruction, which implements the steps of the above text processing method when executed by a processor.
[0055] In order to solve the above technical problems, the present invention further provides a text processing device, comprising:
[0056] Memory for storing computer programs;
[0057] A processor is used to implement the steps of the above-mentioned text processing method when executing the computer program.
[0058] In order to solve the above technical problem, the present invention also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the above text processing method are implemented.
[0059] The text processing method provided by the present invention includes: obtaining the text to be inferred; when it is detected that the text to be inferred contains new knowledge, inputting the text to be inferred into a new knowledge adapter and a large language model; performing inference and prediction on the text to be inferred through the new knowledge adapter and the large language model to obtain a prediction result corresponding to the text to be inferred.
[0060] The beneficial effects of the present invention are that, firstly, in order to integrate new knowledge into the model, the method uses the injected new knowledge samples for training in the new knowledge adapter, and keeps the parameters in the large language model unchanged during the training process, that is, only the parameters of the new knowledge adapter are trained based on the existing large language model. Since the parameters of the new knowledge adapter are much smaller than the parameters in the large language model, it is possible to inject new knowledge into the large language model efficiently, with low computing power and low cost, and ensure that the original capabilities of the large model are not forgotten. Since the efficiency of model training is improved, the efficiency of using the model to reason about text is further improved; secondly, compared with the method of using a large language model alone for reasoning, in the method provided by the present invention, the new knowledge adapter is inserted into the converter block in the large language model, and is placed in parallel with the feedforward neural network module in the converter block, and the adaptation features output by the new knowledge adapter are fused with the original features output by the feedforward neural network module in the converter block through a gating unit, so that when there is new knowledge, the new knowledge adapter and the large language model can be used for reasoning, and the reasoning of new knowledge is guaranteed as much as possible; thirdly, compared with the method of using a single-layer network, the new knowledge adapter in the method provided by the present invention is formed by stacking multiple sub-modules, and each sub-module contains a linear layer and an activation function. Through the stacking of sub-modules, more complex functions can be learned with fewer parameters, thereby achieving efficient use of parameters.
[0061] In addition, the new knowledge adapter is inserted into the last layer of the transformer block in the large language model, which can reduce the interference with the learned features of the previous layers of the model and maintain the performance of the model on the original task; and it can be processed first in the back-propagation process, which improves the processing efficiency;
[0062] When determining the function corresponding to the new knowledge adapter, the results of the activation function transformation corresponding to each submodule are stacked to obtain the function corresponding to the new knowledge adapter, so that the model can integrate the features from different submodules, thereby enhancing the feature fusion capability of the model;
[0063] When determining the expression corresponding to the fusion of the adapted features output by the new knowledge adapter through the gating unit and the original features output by the feedforward neural network module in the transformer block, the weights and biases are taken into account, so that the fusion ratio of the two features can be dynamically controlled according to the weights and biases, so that the model can adaptively adjust its dependence on the new knowledge and the original knowledge;
[0064] The first weight configured for the output result of the new knowledge adapter and the second weight adapted for the output result of the large language model are combined to determine the output result after passing through the new knowledge adapter and the large language model, so that the contribution of different features can be better controlled according to the size of the first weight and the second weight, so that the model can better adapt to the new knowledge;
[0065] The number of sub-modules in the new knowledge adapter is positively correlated with the number of injected new knowledge samples, which ensures that when there are more new knowledge samples, the incremental learning ability of the model is supported by increasing the number of sub-modules;
[0066] The new knowledge identifier formed by stacking multiple transform encoder blocks detects whether the text to be inferred contains new knowledge, which can effectively extract text features. Stacking multiple blocks can further deepen the ability of feature extraction. When training the new knowledge identifier, it includes obtaining original corpus samples, expanding new corpus, randomly sampling positive and negative samples, and performing comparative learning and classification of keywords. By expanding new corpus, the model can learn the latest information and knowledge points and improve the coverage of new knowledge areas. And through keyword extraction, the model can focus on learning the most representative parts of the text and improve learning efficiency. Expanding the corpus can increase the diversity of training data and improve the model's adaptability to different expressions.
[0067] When it is detected that the text to be inferred does not contain new knowledge, it is input into the large language model for inference prediction. Since the large language model has been trained with a large amount of data, it can use its existing knowledge to make accurate inference predictions on the text; for texts that do not contain new knowledge, the computational overhead of new knowledge recognition and processing is avoided, saving resources.
[0068] In addition, the present invention also provides a computer program product, a text processing device and a computer-readable storage medium, which have the same or corresponding technical features as the above-mentioned text processing method and have the same effects as above. BRIEF DESCRIPTION OF THE DRAWINGS
[0069] In order to more clearly illustrate the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0070] Figure 1 A flowchart of a text processing method provided by an embodiment of the present invention;
[0071] Figure 2 A schematic diagram of a new knowledge adapter in a large language model provided by an embodiment of the present invention;
[0072] Figure 3 A schematic diagram of a new knowledge identifier provided by an embodiment of the present invention;
[0073] Figure 4A structural diagram of a text processing device provided by an embodiment of the present invention;
[0074] Figure 5 A structural diagram of a text processing device provided in accordance with another embodiment of the present invention. DETAILED DESCRIPTION
[0075] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0076] The core of the present invention is to provide a text processing method, product, device and medium to solve the technical problems that when incorporating new knowledge into the model, retraining the entire large model is time-consuming and requires a lot of computing resources, and incremental training of the large language model may cause the model to forget the knowledge that has been learned, thereby resulting in a decrease in the efficiency of text reasoning when the model is used to reason about the text.
[0077] In recent years, pretrained language models (PLMs) have emerged by pre-training Transformer models on large-scale corpora, demonstrating powerful capabilities in various natural language processing tasks. Researchers have found that the performance of the model can be improved by increasing its size. As the number of model parameters and the amount of training data continue to increase, these larger language models have not only achieved significant performance improvements, but also demonstrated special capabilities that some small-scale language models (such as Bidirectional Encoder Representations from Transformers (BERT)) do not have. The emergence and development of large language models has had a revolutionary impact on the fields of natural language processing and artificial intelligence.
[0078] However, despite the progress that the development of large language models has brought to the fields of natural language processing and artificial intelligence, it also faces some challenges and problems. One of the outstanding issues is how to effectively inject new knowledge and the latest language information into the already trained large language models. Due to the size and complexity of large language models, it becomes more difficult to update and maintain these models. Whenever there are new language phenomena, vocabulary, or cultural features, this new knowledge needs to be incorporated into the model so that the model can understand and generate content related to it. However, simply retraining the entire large model is very time-consuming and computationally resource-intensive. Large language models encounter difficulties in solving these tasks.
[0079] To solve this problem, a straightforward approach is to periodically update the large language model with new data. However, fine-tuning a large language model is very expensive, and incremental training of a large language model is likely to lead to catastrophic forgetting. Therefore, it is necessary to develop effective methods to incorporate new knowledge into an existing large language model to keep it up to date without causing the model to forget what it has already learned.
[0080] Formal description of the technical problem solved by the present invention: For an existing large language model , whose training corpus is , there are new ones that do not exist in The factual knowledge in The object of the present invention is to Knowledge injection , while ensuring The ability to apply existing knowledge.
[0081] In order to solve this problem, the present invention designs a knowledge injection technology based on adapters, which is implemented by adding some parameter-trainable modules to each transformer block in the original pre-trained model. During the training process, the original large model parameters are frozen, and only the adapter parameters are trained. Therefore, a result equivalent to full model fine-tuning can be obtained at a lower training and storage cost, which can efficiently inject new knowledge into the large model, thereby improving the efficiency of using the model for text reasoning.
[0082] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Figure 1 A flowchart of a text processing method provided by an embodiment of the present invention, such as Figure 1 As shown, the method includes:
[0083] S10: Obtain the text to be inferred;
[0084] S11: when it is detected that the text to be inferred contains new knowledge, the text to be inferred is input into the new knowledge adapter and the large language model;
[0085] S12: Perform inference prediction on the text to be inferred through the new knowledge adapter and the large language model to obtain a prediction result corresponding to the text to be inferred.
[0086] There is no limitation on the text to be inferred, which is determined according to the actual situation. In practice, the text to be inferred may or may not involve new knowledge. It should be noted that the new knowledge in the embodiment of the present invention refers to the knowledge that is not involved in the existing large model, and the old knowledge refers to the knowledge involved in the existing large model. In order to perform reasoning and prediction on the text to be inferred, when it is detected that the text to be inferred contains new knowledge, if the existing large language model is still used, then because the new knowledge in the text to be inferred is not involved in the large language model, it is impossible to accurately predict the text to be inferred using only the existing large language model. Therefore, a new knowledge adapter is proposed in the embodiment of the present invention, and the new knowledge adapter is used to inject knowledge into the large language model.
[0087] The new knowledge adapter is inserted into the transformer block in the large language model, and is placed in parallel with the feedforward neural network module in the transformer block. The adapted features output by the new knowledge adapter are fused with the original features output by the feedforward neural network module in the transformer block through the gating unit. The new knowledge adapter is formed by stacking multiple submodules, and each submodule contains a linear layer and an activation function. There is no limit on the number of submodules contained in the new knowledge adapter. In implementation, determining the number of submodules in the new knowledge adapter includes:
[0088] Get the number of new knowledge samples injected into the new knowledge adapter;
[0089] The number of submodules in the new knowledge adapter is determined according to the number of injected new knowledge samples; wherein the number of submodules in the new knowledge adapter is positively correlated with the number of injected new knowledge samples.
[0090] By setting the number of sub-modules in the new knowledge adapter to be positively correlated with the number of injected new knowledge samples, it is ensured that when there are many new knowledge samples, the incremental learning ability of the model is supported by increasing the number of sub-modules.
[0091] In order to improve the efficiency of training, reduce computing power, and maintain the knowledge that the large language model has learned, the new knowledge adapter uses the injected new knowledge samples for training during training, and keeps the parameters in the large language model unchanged during the training process.
[0092] There is no restriction on the position where the new knowledge adapter is inserted into the transformer block in the large language model. In order to reduce interference with the learned features of the previous layers of the model, maintain the performance of the model on the original task and improve processing efficiency, in implementation, the new knowledge adapter is inserted into the target transformer block in the transformer block in the large language model; wherein the target transformer block is the transformer block located at the last layer among all the transformer blocks in the large language model.
[0093] In order to enable the new knowledge adapter to integrate features from different sub-modules and enhance the feature fusion capability of the model, the functions corresponding to the new knowledge adapter are determined to include:
[0094] Apply weights and biases to the injected new knowledge samples for linear transformation and obtain the linear transformation results;
[0095] Apply activation function to transform the linear transformation result;
[0096] The results of the activation function transformation corresponding to each sub-module are stacked up to obtain the function corresponding to the new knowledge adapter.
[0097] The function corresponding to the new knowledge adapter is expression (1):
[0098] ; (1)
[0099] in, It means text. Represents the function corresponding to the new knowledge adapter, Indicates The weights of the linear transformation, Indicates The linear transformation bias, Also corresponds to the new adapter The parameters of the stacking submodules, It indicates the number of submodules. represents the activation function, It indicates stacking processing;
[0100] Among them, the activation function is expression (2):
[0101] ; (2)
[0102] The adapted features output by the new knowledge adapter are fused with the original features output by the feedforward neural network module in the transformer block through the gating unit, corresponding to expression (3):
[0103] ; (3)
[0104] in, represents the output result of the gate control unit, represents the normalized function, represents the weight matrix of the gating unit, represents the bias vector of the gating unit, It represents the adaptation characteristics of the new knowledge adapter output. represents the raw features output by the feedforward neural network module in the transformer block.
[0105] It can be seen that when determining the corresponding expression for fusing the adapted features output by the new knowledge adapter through the gating unit with the original features output by the feedforward neural network module in the transformer block, the weights and biases are taken into account, so that the fusion ratio of the two features can be dynamically controlled according to the weights and biases, so that the model can adaptively adjust its dependence on new knowledge and original knowledge.
[0106] In order to adjust the ratio of the adapted features output by the new knowledge adapter to the original features output by the feedforward neural network module in the transformer block, in implementation, determining the output results after the new knowledge adapter and the large language model includes:
[0107] Obtain a first weight configured for the output result of the new knowledge adapter and a second weight adapted for the output result of the large language model; wherein the sum of the first weight and the second weight is 1;
[0108] Obtaining a first product result of the adaptation feature output by the new knowledge adapter and the first weight;
[0109] Obtain a second product result of the original feature output by the feedforward neural network module in the transformer block and the second weight;
[0110] The sum of the first product result and the second product result is taken as the output result after the new knowledge adapter and the large language model.
[0111] There is no limitation on the specific values of the first weight and the second weight, as long as the sum of the first weight and the second weight is 1.
[0112] The output result after the new knowledge adapter and the large language model is expression (4):
[0113] ; (4)
[0114] in, is the output result after the new knowledge adapter and the large language model. It represents the weight.
[0115] It should be noted that , , and are trainable parameters and , the size of these parameters is related to the large model itself. For example, if the hidden layer output dimension of a large model is 4096, then , and so on. The range can be 2 to 8. For example, if it is set to 2, the new knowledge adapter is composed of 2 stacked sub-modules. In practice, it can be adjusted according to the amount of injected knowledge.
[0116] In order to improve the processing capability of the model, in the implementation, the new knowledge adapter is inserted into the transformer block in the large language model and placed in parallel with the feedforward neural network module in the transformer block, and the adaptation features output by the new knowledge adapter are fused with the original features output by the feedforward neural network module in the transformer block through the gating unit, including:
[0117] Multiple new knowledge adapters are inserted into the transformer block in the large language model and placed in parallel with the feedforward neural network module in the transformer block, and the adapted features output by the new knowledge adapters are fused with the original features output by the feedforward neural network module in the transformer block through the gating unit; wherein each new knowledge adapter is applicable to different fields respectively;
[0118] After obtaining the text to be inferred, it also includes:
[0119] Extract features of the text to be inferred;
[0120] Determine the target domain to which the text to be inferred belongs according to the characteristics of the text to be inferred;
[0121] When it is detected that the text to be inferred contains new knowledge, inputting the text to be inferred into the new knowledge adapter and the large language model includes:
[0122] When it is detected that the text to be inferred contains new knowledge, a target new knowledge adapter corresponding to the target domain is determined according to a pre-established correspondence between the domain to which the new knowledge adapter is applicable and the new knowledge adapter;
[0123] The text to be inferred is input into the target new knowledge adapter and the large language model.
[0124] In this method, by setting up new knowledge adapters suitable for different fields, the text to be inferred can be predicted more specifically, thereby improving the accuracy of the text reasoning to be inferred; and improving the processing capability of the model.
[0125] In order to enable those skilled in the art to better understand the new knowledge adapter provided by the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0126] Figure 2 A schematic diagram of a new knowledge adapter in a large language model provided by an embodiment of the present invention, such as Figure 2 As shown, Figure 2The new knowledge adapter in is inserted into the transformer block of the last layer of the large language model, and is placed in parallel with the feed-forward neural network (FFN) module of the transformer block at this layer. It also receives the representation of the multi-head attention mechanism and is then fused with the output of the FFN using a gating unit to obtain the knowledge-injected representation for subsequent autoregressive language model operations. Figure 2 The structure of the Zhongxin Knowledge Adapter is composed of K stacked sub-modules, each of which includes a linear layer and an activation function. The overall mathematical expression is the above-mentioned expression (1) and expression (2).
[0127] Then, the adapted features output by the new knowledge adapter are fused with the original features output by the feedforward neural network module in the transformer block through a gating mechanism, which is shown in the above expressions (3) and (4).
[0128] In the process of training the new knowledge adapter, the present invention freezes the parameters in the original large model and uses the corpus injected with knowledge The training task is the same as that of the large model, that is, the next word prediction is used as the task. Training the new knowledge adapter includes: using a preset optimizer and selecting a preset learning rate to control the convergence of the new knowledge adapter. When the convergence of the new knowledge adapter is detected, it is determined that the training of the new knowledge adapter is completed.
[0129] There is no limitation on the optimizer used and the learning rate set. For example, AdamW is used as the optimizer in the training process, and the learning rate is 1e-5. The training is stopped when the model converges. Note that in the new knowledge adapter part, the present invention sets the Dropout to 0.4 to 0.5, such as setting the Dropout to 0.45.
[0130] The above describes a method of injecting knowledge into a large language model using an adapter. For a large model that injects new knowledge, it is also necessary to ensure that its original capabilities are not forgotten and that it can still complete existing knowledge tasks. For this reason, an embodiment of the present invention constructs a new knowledge identifier to determine whether the current input task requires the injected new knowledge. Detecting whether the text to be inferred contains new knowledge includes:
[0131] Detecting whether the text to be inferred contains new knowledge by a new knowledge identifier; wherein the new knowledge identifier is formed by stacking a plurality of transform encoder blocks;
[0132] Training the new knowledge recognizer involves:
[0133] Obtain the original corpus samples for training large language models;
[0134] Acquire new corpus and expand the new corpus to obtain expanded corpus;
[0135] Randomly sample negative samples from the original corpus sample and randomly sample positive samples from the expanded corpus;
[0136] Take keywords from each sample for comparative learning and classification to determine whether the sample belongs to new knowledge or old knowledge; among them, the sample includes positive samples and negative samples.
[0137] Acquiring new corpora and expanding them includes:
[0138] Acquire new corpus and expand the new corpus by at least one of the following methods; wherein the new corpus has the same semantics as the expanded corpus;
[0139] Method 1: Get the reply content corresponding to the new corpus;
[0140] Add the reply content to the new corpus;
[0141] Method 2: Expand the new corpus by replacing synonyms and adjusting sentence structures.
[0142] Training the new knowledge recognizer involves:
[0143] Use a preset optimizer and select a preset learning rate to control the convergence of the new knowledge identifier;
[0144] When it is detected that the new knowledge identifier has converged, it is determined that the training of the new knowledge identifier is completed.
[0145] In order to enable those skilled in the art to better understand the new knowledge identifier provided by the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Figure 3 A schematic diagram of a new knowledge identifier provided by an embodiment of the present invention is shown in FIG. Figure 3 As shown, the new knowledge identifier is mainly formed by stacking multiple transformer encoder blocks (Transformer encoderBlock). After the text is input into the new knowledge identifier, the new knowledge identifier outputs the classification of the text. There is no limit on the number of transformer encoder blocks. For example, the new knowledge identifier is formed by stacking 4 transformer encoder blocks. It should be noted that the text input into the new knowledge identifier can be the word at the beginning of the entire sentence, and the meaning of the entire sentence is represented by the word at the beginning of the entire sentence.
[0146] In the new knowledge identifier, first, a large model that injects new knowledge is used to expand the corpus by retelling. , the way to repeat is to use the following prompts:
[0147] Rephrase and expand the content as following:
[0148] Will Enter the content in and add the reply content later , which is the expanded corpus In the embodiment of the present invention, the model is trained by contrastive learning. is the positive example of the model, and the sampled corpus The samples in are negative examples. Specifically, for A sample in ,exist Randomly sample positive samples ,exist Randomly sample negative samples , take the classification representation of each sample for comparative learning and classification.
[0149] The distance in the present invention is measured using the Euclidean distance, the ratio of positive and negative samples is 1:2048, and the loss function used is as follows:
[0150] ;
[0151] in is a hyperparameter, which is set to 0.4 here. When training the knowledge identifier, the present invention uses AdamW as the optimizer, the learning rate is 1e-5, and the training is stopped when the model converges. The model is then used for subsequent reasoning.
[0152] The new knowledge identifier provided in this embodiment is used to determine whether the current input task requires the injected new knowledge.
[0153] After the construction and training of the new knowledge adapter and the new knowledge identifier are completed, the new knowledge adapter is used for large model reasoning. The new knowledge identifier is used to detect whether the text to be reasoned contains new knowledge, including:
[0154] Obtaining a feature set representing the expanded corpus in the new knowledge identifier and obtaining features representing the text to be inferred;
[0155] Obtaining the similarity between the feature representing the text to be inferred and each feature in the feature set;
[0156] If it is detected that the similarity is greater than or equal to a preset value, it is determined that the feature representing the text to be inferred belongs to the feature set, and it is determined that the text to be inferred contains new knowledge detected by the new knowledge identifier;
[0157] If it is detected that the similarity is less than a preset value, it is determined that the text to be inferred contains no new knowledge as detected by the new knowledge identifier.
[0158] There is no limit on the preset value. The smaller the preset value, the higher the representation similarity.
[0159] When it is detected that the text to be inferred contains new knowledge, the text to be inferred is input into the new knowledge adapter and the large language model, and the new knowledge adapter and the large language model are used to perform inference prediction on the text to be inferred to obtain a prediction result corresponding to the text to be inferred;
[0160] When it is detected that the text to be inferred does not contain new knowledge, the text to be inferred is input into the large language model;
[0161] The text to be inferred is inferred and predicted through a large language model to obtain the prediction result corresponding to the text to be inferred.
[0162] In order to enable those skilled in the art to better understand the process of large model reasoning using the new knowledge adapter provided by the present invention, the present invention is further described in detail below in conjunction with specific implementation methods.
[0163] After completing the above model construction and training, the present invention proposes the following steps for large model reasoning using the new knowledge adapter:
[0164] S13: New knowledge identifier will The samples of the corpus are represented to form a collection of representations ;
[0165] S14: New knowledge identifier accepts reasoning input (i.e. text), characterize it as ;
[0166] S15: Use K-Nearest Neighbors (KNN) algorithm to determine Is the representation of In the feature space expressed; if it belongs to, it means the input Involving new knowledge; if not, it means that this reasoning input does not contain new knowledge;
[0167] S16: When judging input When new knowledge is involved, the new knowledge adapter and the original large model are used for reasoning operations; when the input When no new knowledge is included, only the original large model is used for reasoning operations.
[0168] In this way, during the reasoning stage, new knowledge can be used for flexible reasoning, and the big model will not forget its original capabilities due to the new knowledge; and new knowledge adapters can be efficiently constructed to achieve efficient and low-cost new knowledge injection without fine-tuning the parameters of the entire big model, so that the capabilities of the big model are continuously enhanced.
[0169] In the above embodiments, the text processing method is described in detail, and the present invention also provides embodiments of a text processing device and a text processing equipment. It should be noted that the present invention describes the embodiments of the device part from two perspectives, one is based on the functional module perspective, and the other is based on the hardware perspective.
[0170] Figure 4 This is a structural diagram of a text processing device provided by an embodiment of the present invention. This embodiment is based on the perspective of functional modules and includes:
[0171] A first acquisition module 10, used to acquire the text to be inferred;
[0172] The first input module 11 is used to input the text to be inferred into the new knowledge adapter and the large language model when it is detected that the text to be inferred contains new knowledge; wherein the new knowledge adapter is inserted into the transformer block in the large language model, and is placed in parallel with the feedforward neural network module in the transformer block, and the adaptation features output by the new knowledge adapter are fused with the original features output by the feedforward neural network module in the transformer block through the gating unit; the new knowledge adapter is formed by stacking a plurality of submodules, and each submodule contains a linear layer and an activation function; when training, the new knowledge adapter is trained using the injected new knowledge samples, and the parameters in the large language model are kept unchanged during the training process;
[0173] The prediction module 12 is used to perform inference prediction on the text to be inferred through the new knowledge adapter and the large language model to obtain the prediction result corresponding to the text to be inferred.
[0174] In some embodiments, the text processing device includes a first determination module for determining a function corresponding to the new knowledge adapter;
[0175] The first determination module specifically includes:
[0176] A transformation and acquisition module, used for applying weights and biases to the injected new knowledge samples to perform linear transformation and obtain linear transformation results;
[0177] A transformation module, used to transform the linear transformation result by applying an activation function;
[0178] The stacking module is used to stack the results of the activation function transformation corresponding to each sub-module to obtain the function corresponding to the new knowledge adapter.
[0179] In some embodiments, the text processing device includes a fusion module for fusing the adapted features output by the new knowledge adapter with the original features output by the feedforward neural network module in the transformer block through a gating unit.
[0180] In some embodiments, the text processing device includes a second determination module for determining the output result after the new knowledge adapter and the large language model;
[0181] The second determination module specifically includes:
[0182] A second acquisition module is used to acquire a first weight configured for the output result of the new knowledge adapter and a second weight adapted for the output result of the large language model; wherein the sum of the first weight and the second weight is 1;
[0183] A third acquisition module is used to obtain a first product result of the adaptation feature output by the new knowledge adapter and the first weight;
[0184] A fourth acquisition module, used to obtain a second product result of the original feature output by the feedforward neural network module in the converter block and the second weight;
[0185] As a module, it is used to take the sum of the first product result and the second product result as the output result after passing through the new knowledge adapter and the large language model.
[0186] In some embodiments, the text processing device includes a third determination module, the third determination module is used to determine the number of submodules in the new knowledge adapter;
[0187] The third determination module specifically includes:
[0188] A fifth acquisition module, used to acquire the number of new knowledge samples injected into the new knowledge adapter;
[0189] Determine submodules, used to determine the number of submodules in the new knowledge adapter according to the number of injected new knowledge samples; wherein the number of submodules in the new knowledge adapter is positively correlated with the number of injected new knowledge samples.
[0190] In some embodiments, the text processing device includes a detection module for detecting whether the text to be inferred contains new knowledge;
[0191] The detection module specifically includes:
[0192] A detection submodule, used for detecting whether the text to be inferred contains new knowledge through a new knowledge identifier; wherein the new knowledge identifier is formed by stacking a plurality of transform encoder blocks;
[0193] In some embodiments, the text processing device includes a training module for training a new knowledge identifier;
[0194] The training modules include:
[0195] The sixth acquisition module is used to obtain the original corpus samples when training the large language model;
[0196] A seventh acquisition module is used to acquire new corpus and expand the new corpus to obtain expanded corpus;
[0197] A selection module is used to randomly sample negative samples from the original corpus sample and randomly sample positive samples from the expanded corpus;
[0198] The fourth determination module is used to compare and learn the keywords of each sample to determine whether the sample belongs to new knowledge or old knowledge; wherein the sample includes positive samples and negative samples.
[0199] In some embodiments, the detection submodule specifically includes:
[0200] An eighth acquisition module, used to acquire a feature set representing the expanded corpus in the new knowledge identifier and to acquire features representing the text to be inferred;
[0201] A ninth acquisition module, which acquires similarities between features representing the text to be inferred and features in the feature set;
[0202] A fifth determination module is used to determine that the feature representing the text to be inferred belongs to the feature set if the similarity is greater than or equal to a preset value, and to determine that the text to be inferred contains new knowledge detected by the new knowledge identifier;
[0203] The sixth determination module is used to determine that the text to be inferred does not contain new knowledge if the similarity is detected to be less than a preset value through the new knowledge identifier.
[0204] In some embodiments, the text processing device further includes:
[0205] A second input module is used to input the text to be inferred into the large language model when it is detected that the text to be inferred does not contain new knowledge;
[0206] The inference prediction module is used to perform inference prediction on the text to be inferred through a large language model to obtain a prediction result corresponding to the text to be inferred.
[0207] Since the embodiments of the device part correspond to the embodiments of the method part, the embodiments of the device part refer to the description of the embodiments of the method part, which will not be described here. And it has the same beneficial effects as the text processing method mentioned above.
[0208] Figure 5 This is a structural diagram of a text processing device provided by another embodiment of the present invention. This embodiment is based on the hardware perspective, such as Figure 5 As shown, the text processing device includes:
[0209] A memory 20, used for storing computer programs;
[0210] The processor 21 is used to implement the steps of the text processing method mentioned in the above embodiment when executing the computer program.
[0211] Among them, the processor 21 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 21 can be implemented in at least one hardware form of a digital signal processor (DSP), a field-programmable gate array (FPGA), and a programmable logic array (PLA). The processor 21 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 21 may be integrated with a graphics processing unit (GPU), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 21 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.
[0212] The memory 20 may include one or more computer-readable storage media, which may be non-transitory. The memory 20 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 20 is at least used to store the following computer program 201, wherein, after the computer program is loaded and executed by the processor 21, it can implement the relevant steps of the text processing method disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 20 may also include an operating system 202 and data 203, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 202 may include Windows, Unix, Linux, etc. The data 203 may include, but is not limited to, the data involved in the above-mentioned text processing method, etc.
[0213] In some embodiments, the text processing device may also include a display screen 22 , an input / output interface 23 , a communication interface 24 , a power supply 25 , and a communication bus 26 .
[0214] Those skilled in the art will understand that Figure 5 The structure shown in the figure does not constitute a limitation on the text processing device, and may include more or fewer components than shown in the figure.
[0215] The text processing device provided by the embodiment of the present invention includes a memory and a processor. When the processor executes the program stored in the memory, it can implement the following method: the text processing method has the same effect as above.
[0216] The present invention also provides a computer program product, comprising a computer program / instruction, which implements the steps of the above-mentioned text processing method when executed by a processor.
[0217] Finally, the present invention also provides an embodiment corresponding to a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps recorded in the above method embodiment are implemented.
[0218] It is understandable that if the method in the above embodiment is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium to execute all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc. Various media that can store program codes.
[0219] The computer-readable storage medium provided by the present invention includes the above-mentioned text processing method, and the effect is the same as above.
[0220] The above is a detailed introduction to a text processing method, product, device and medium provided by the present invention. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the embodiments can be referenced to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description. It should be pointed out that for ordinary technicians in this technical field, without departing from the principle of the present invention, several improvements and modifications can be made to the present invention, and these improvements and modifications also fall within the scope of protection of the present invention.
[0221] It should also be noted that, in this specification, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.
Claims
1. A text processing method, It is characterized in that include: Get the text to be inferred; In the case where it is detected that the text to be inferred contains new knowledge, the text to be inferred is input into the new knowledge adapter and the large language model; wherein the new knowledge adapter is inserted into the converter block in the large language model, and is placed in parallel with the feedforward neural network module in the converter block, and the adaptation features output by the new knowledge adapter are fused with the original features output by the feedforward neural network module in the converter block through a gating unit; the new knowledge adapter is formed by stacking a plurality of submodules, and each submodule contains a linear layer and an activation function; when training, the new knowledge adapter is trained using the injected new knowledge samples, and the parameters in the large language model are kept unchanged during the training process; Performing reasoning and prediction on the text to be inferred by using the new knowledge adapter and the large language model to obtain a prediction result corresponding to the text to be inferred; Detecting whether the text to be inferred contains new knowledge includes: Detecting whether the text to be inferred contains new knowledge by a new knowledge identifier; wherein the new knowledge identifier is formed by stacking a plurality of transform encoder blocks; The detecting whether the text to be inferred contains new knowledge by a new knowledge identifier includes: Acquire a feature set representing the expanded corpus in the new knowledge identifier and acquire a feature representing the text to be inferred; Obtaining similarities between features representing the text to be inferred and each feature in the feature set; If it is detected that the similarity is greater than or equal to a preset value, determining that the feature characterizing the text to be inferred belongs to the feature set, and determining that the text to be inferred contains new knowledge detected by the new knowledge identifier; If it is detected that the similarity is less than the preset value, it is determined that the text to be inferred contains no new knowledge as detected by the new knowledge identifier.
2. The text processing method according to claim 1, It is characterized in that The new knowledge adapter is inserted into a target transformer block in the transformer blocks in the large language model; wherein the target transformer block is a transformer block located at the last layer among all transformer blocks in the large language model.
3. The text processing method according to claim 2, It is characterized in that Determining the function corresponding to the new knowledge adapter includes: Apply weights and biases to the injected new knowledge samples for linear transformation and obtain the linear transformation results; Apply activation function to transform the linear transformation result; The results of the activation function transformation corresponding to each sub-module are stacked up to obtain the function corresponding to the new knowledge adapter.
4. The text processing method according to claim 3, It is characterized in that The function expression corresponding to the new knowledge adapter is: ; in, It means text. Represents the function corresponding to the new knowledge adapter, Indicates The weights of the linear transformation, Indicates The linear transformation bias, It indicates the number of submodules. represents the activation function, It indicates stacking processing; Among them, the expression of the activation function is: 。 5. The text processing method according to claim 1, It is characterized in that The corresponding expression for fusing the adaptation features output by the new knowledge adapter and the original features output by the feedforward neural network module in the converter block through the gating unit is: ; in, represents the output result of the gating unit, represents the normalized function, represents the weight matrix of the gating unit, represents the bias vector of the gating unit, represents the adaptation characteristics output by the new knowledge adapter, It represents the original features output by the feedforward neural network module in the transformer block.
6. The text processing method according to claim 5, It is characterized in that Determining the output result after the new knowledge adapter and the large language model includes: Obtaining a first weight configured for the output result of the new knowledge adapter and a second weight adapted for the output result of the large language model; wherein the sum of the first weight and the second weight is 1; Obtaining a first product result of the adaptation feature output by the new knowledge adapter and the first weight; Obtaining a second product result of the original feature output by the feedforward neural network module in the converter block and the second weight; The sum of the first multiplication result and the second multiplication result is used as the output result after passing through the new knowledge adapter and the large language model.
7. The text processing method according to claim 1, It is characterized in that Determining the number of submodules in the new knowledge adapter includes: Obtaining the number of new knowledge samples injected into the new knowledge adapter; The number of submodules in the new knowledge adapter is determined according to the number of the injected new knowledge samples; wherein the number of submodules in the new knowledge adapter is positively correlated with the number of the injected new knowledge samples.
8. A text processing method according to any one of claims 1 to 7, It is characterized in that Training the new knowledge identifier includes: Obtaining original corpus samples for training the large language model; Acquire new corpus and expand the new corpus to obtain expanded corpus; Randomly sampling and selecting negative samples from the original corpus sample and randomly sampling and selecting positive samples from the expanded corpus; Take keywords from each sample for comparative learning and classification to determine whether the sample belongs to new knowledge or old knowledge; wherein the sample includes positive samples and negative samples.
9. The text processing method according to claim 8, It is characterized in that The acquiring of new corpus and expansion of the new corpus includes: Acquire the new corpus, and expand the new corpus in at least one of the following ways; wherein the new corpus has the same semantics as the expanded corpus; Method 1: Obtain the reply content corresponding to the new corpus mentioned in the reply; Adding the reply content to the new corpus; Method 2: Perform synonym replacement and sentence structure adjustment on the new corpus to expand the new corpus.
10. The text processing method according to claim 1, It is characterized in that The method further comprises: In the case where it is detected that the text to be inferred does not contain new knowledge, inputting the text to be inferred into the large language model; The large language model is used to perform inference prediction on the text to be inferred, so as to obtain a prediction result corresponding to the text to be inferred.
11. The text processing method according to claim 8, It is characterized in that Training the new knowledge adapter and / or training the new knowledge identifier includes: Using a preset optimizer and selecting a preset learning rate to control the convergence of the new knowledge adapter and / or to control the convergence of the new knowledge identifier; In the case where it is detected that the new knowledge adapter has converged and / or the new knowledge identifier has converged, it is determined that the training of the new knowledge adapter and / or the training of the new knowledge identifier is completed.
12. A computer program product comprising a computer program / instructions, It is characterized in that When the computer program / instructions are executed by a processor, the steps of the text processing method described in any one of claims 1 to 11 are implemented.
13. A text processing device, It is characterized in that include: Memory for storing computer programs; A processor, configured to implement the steps of the text processing method as claimed in any one of claims 1 to 11 when executing the computer program.
14. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the text processing method according to any one of claims 1 to 11 are implemented.
