A sentence vector model training method

By performing data enhancement on sentences and improved training of the BERT model, combined with domain dictionary word segmentation, the problem of poor performance and high computational overhead of existing pre-trained models on Chinese texts has been solved, and more efficient text information retrieval has been achieved.

CN113553824BActive Publication Date: 2024-09-27山东中科慧心智能科技有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110766647.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-07-07
Publication Date
2024-09-27
Estimated Expiration
2041-07-07

AI Technical Summary

Technical Problem

Existing pre-trained models perform poorly in semantic similarity search and unsupervised tasks, especially on Chinese texts, and the training method of the classic BERT model is not suitable for Chinese texts, resulting in insufficient model generalization capabilities and high computational overhead.

Method used

By performing data enhancement processing on the original sentences, multiple sentence pairs and their similarity labels are constructed, and the BERT model is used for mask language model pre-training and binary classification pre-training. Combined with domain dictionary word segmentation, the model is fine-tuned to adapt to downstream tasks and reduce calculations. overhead.

Benefits of technology

It improves the performance of the model in semantic similarity search and unsupervised tasks, enhances the generalization ability of the model, reduces the computational overhead of massive text retrieval, and improves the efficiency of text information retrieval.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113553824B
    Figure CN113553824B
Patent Text Reader

Abstract

The present invention provides a sentence vector model training method, including: S1, performing data enhancement processing on multiple original sentences to obtain multiple similar sentences of each original sentence, and copying all the sentences in multiple copies to construct multiple sentence pairs and their similarity labels; S2, using a BERT model to perform pre-training based on the multiple sentence pairs to obtain a first BERT model, wherein the pre-training includes using sentence pairs to perform mask language model pre-training and using sentence pairs and similarity labels to perform pre-training to determine whether two sentences are similar; S3, using a classification data set corresponding to a task in a corresponding field to fine-tune the first BERT model to obtain a sentence vector model, which has good generalization ability. Based on the text retrieval method adopted by the sentence vector model, the text retrieval efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of natural language processing, and in particular to a sentence vector model training method. Background Art

[0002] In the 1960s, some key technologies of text retrieval technology achieved breakthroughs. During this period, some excellent systems and evaluation indicators emerged. In terms of evaluation indicators, the Cranfield evaluation organized by the Cranfield research group proposed many evaluation indicators that are still widely used today. In terms of systems, the SMART information retrieval system developed by Gerard Salton built a good research platform on which researchers can define their own document relevance measures to improve retrieval performance. In this way, as a research topic, text retrieval technology has a relatively complete experimental platform and evaluation indicators, and its research has naturally entered the fast lane. It is precisely because of this that in the 1970s and 1980s, many theories and models for text retrieval were proposed and proved to be effective for the data sets available at that time. The most famous of these is the vector space model proposed by Gerard Salton. To this day, this model is still one of the most commonly used models in the field of information retrieval. However, the lack of the object of retrieval - the text collection makes it impossible to verify the reliability of these technologies on massive texts. At that time, most of the research was aimed at a collection of thousands of documents. At this time, the Text Retrieval Conference (TREC) organized by the National Institute of Standards and Technology (NIST) of the United States changed this situation. The Text Retrieval Conference is an evaluation conference that provides participants with a large-scale text corpus, which greatly promotes the rapid development of information retrieval technology. The first conference was held in 1992. Soon after, the rise of the Internet provided a huge experimental field for information retrieval technology. From Yahoo to Google, a large number of practical text information retrieval systems began to appear and were widely used. These systems have actually changed the way humans obtain information and knowledge.

[0003] With the release of BERT at the end of 2018, the field of text retrieval has entered the era of pre-trained models. There are numerous pre-trained models such as GPT-2, Roberta, XLNet, Transformer-XL, AlBert, T5, etc. However, most models are not suitable for semantic similarity search or unsupervised tasks such as clustering. A common approach to solving clustering and semantic search is to map each sentence to a vector space so that semantically similar sentences are close. However, directly inputting the entire sentence into the pre-trained model to obtain the sentence vector of the sentence, and then using it as the sentence vector representation of the sentence, the sentence vector obtained in this way does not have semantic information, that is, the sentence vectors obtained for two similar sentences may be very different. In addition, not only are there significant differences in the semantics of the sentence vector representation, but it also requires that both sentences be input into the network, resulting in huge overhead: finding the most similar sentence pair from a set of 10,000 sentences requires about 50 million inference calculations (about 65 hours).

[0004] Classic BERT trains and infers single words as a character on Chinese data. This method is only suitable for embedding single English words, but it is not applicable to Chinese text. Each field in Chinese text has its own customized vocabulary. Transforming BERT into a method of word segmentation based on field dictionaries is obviously more in line with the semantic background of Chinese. In addition, the original BERT includes two pre-training tasks, namely Masked Language model training and Next Sentence Prediction. When training the Masked Language model in the original BERT model, 15% of the words in the sentence are randomly selected for prediction, that is, these 15% of words are randomly selected from the beginning, and the data will not change in the subsequent N rounds of training, which makes the training data uniform. Therefore, a large amount of training data needs to be collected to make up for the problem of insufficient training data. At the same time, the pre-trained model lacks certain generalization ability. Another pre-training task in the original BERT model is next sentence prediction, which is used to capture the relationship between sentences. This pre-training is only suitable for enabling the model to understand the semantic information at the sentence and paragraph levels more accurately. Summary of the invention

[0005] Therefore, the purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a sentence vector model training method.

[0006] The objective of the present invention is achieved through the following technical solutions:

[0007] According to a first aspect of the present invention, a sentence vector model training method is provided, comprising: S1, performing data enhancement processing on a plurality of original sentences to obtain a plurality of similar sentences for each original sentence, and copying all the sentences in multiple copies for constructing a plurality of sentence pairs and their similarity labels; S2, performing pre-training based on the plurality of sentence pairs using a BERT model to obtain a first BERT model, wherein the pre-training includes pre-training a masked language model using sentence pairs and pre-training for determining whether two sentences are similar using sentence pairs and similarity labels; S3, fine-tuning the first BERT model using a classification data set corresponding to a task in a corresponding field to obtain a sentence vector model.

[0008] In some embodiments of the present invention, the process of constructing a plurality of sentence pairs and their similarity labels includes:

[0009] Obtain multiple similar sentence pairs, and mark the similarity tags as similar, wherein each similar sentence pair consists of two sentences randomly selected from an original sentence and multiple similar sentences corresponding to the original sentence;

[0010] A plurality of dissimilar sentence pairs are obtained, and similarity labels are marked as dissimilar, wherein each dissimilar sentence pair is composed of two sentences randomly selected from a number of original sentences, or from an original sentence and similar sentences corresponding to different original sentences, or from dissimilar sentences corresponding to different original sentences.

[0011] In some embodiments of the present invention, the pre-training learning process includes:

[0012] Randomly mask a predetermined proportion of words in the sentence pair, perform masked language model pre-training in the BERT model, obtain the prediction results of the masked words, and calculate the first sub-loss based on the prediction results and the masked original words;

[0013] Input the sentence pair into the BERT model for binary classification pre-training to obtain a classification result indicating whether the two sentences in the sentence pair are similar, and calculate the second sub-loss based on the classification result and the similarity label;

[0014] The total loss of the weighted sum of the first sub-loss and the second sub-loss is used to update the BERT model parameters through back propagation to obtain the first BERT model.

[0015] In some embodiments of the present invention, fine-tuning the first BERT model using a classification dataset corresponding to a task in a corresponding field includes:

[0016] Input the samples in the classification data set into the twin network structure based on the first BERT model to obtain the classification results of the samples;

[0017] The first BERT model parameters are adjusted according to the classification results, and iterative training is performed. After the iterative training, the sentence vector model is obtained.

[0018] In some embodiments of the present invention, adjusting the first BERT model parameters according to the classification result includes:

[0019] The loss is calculated using the cross entropy loss function based on the classification results and the true results, and the first BERT model parameters are updated by stochastic gradient descent. When the loss is less than or equal to the specified threshold, the update is stopped.

[0020] In some embodiments of the present invention, before the sentence pair is input into the BERT model, the sentence pair is segmented using custom words in the domain dictionary.

[0021] In some embodiments of the present invention, the data enhancement process includes synonym replacement, random insertion, random exchange, random deletion and a combination thereof.

[0022] According to a second aspect of the present invention, there is provided a text retrieval method, comprising:

[0023] Inputting the query text into the sentence vector model obtained by the method of the first aspect described above to obtain a corresponding query text vector;

[0024] Obtaining the in-library text vectors extracted by the sentence vector model for each text in the text library;

[0025] Calculate the similarity between the text vectors in each library and the query text vector, and output the text in the text library with the highest similarity to the query text.

[0026] In some embodiments of the present invention, it further includes:

[0027] When the similarity is greater than a specified threshold, the text corresponding to the in-library text vector and the query text corresponding to the query text vector are taken as similar texts, wherein, among all similarities greater than the specified threshold, the text corresponding to the in-library text vector with the highest score is selected as the output text.

[0028] According to a third aspect of the present invention, there is provided a computer-readable storage medium, which contains a computer program, and the computer program can be executed by a processor to implement the sentence vector model training method of the first aspect and the steps of performing a text retrieval method based on the sentence vector model generated by the model training method.

[0029] According to a fourth aspect of the present invention, there is provided an electronic device comprising: one or more processors; and a memory, wherein the memory is used to store one or more executable instructions; the one or more processors are configured to implement the sentence vector model training method of the first aspect by executing the one or more executable instructions and to perform the text retrieval method steps of the second aspect using the sentence vector model generated by the method of the first aspect.

[0030] Compared with the prior art, the advantages of the present invention are:

[0031] 1. The sentence segmentation method based on the domain dictionary for input BERT model pre-training is more in line with the semantic background of Chinese. In addition, data enhancement is adopted and multiple copies are made to obtain a large amount of data. When training the masked language model, a predetermined proportion of words are randomly selected for each copy of the data for masking. During training, one piece of data has one masking method, and multiple copies of the same data form multiple different masking methods, so that the pre-trained model has strong generalization ability.

[0032] 2. The present invention removes the next sentence prediction pre-training task of the original BERT model, and instead improves the BERT model to better adapt to downstream tasks by performing a binary classification pre-training task of training text similarity in the form of input sentence pairs each time.

[0033] 3. The text retrieval method based on the sentence vector model of the present invention does not have a large overhead, solves the problem of quickly retrieving and recalling similar texts in a massive text library, and improves the efficiency of text information retrieval. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] The embodiments of the present invention are further described below with reference to the accompanying drawings, in which:

[0035] Figure 1 It is an overall flow chart of the sentence vector model training method and text retrieval according to an embodiment of the present invention;

[0036] Figure 2 A flow chart of a sentence vector model training method according to an embodiment of the present invention;

[0037] Figure 3 A schematic diagram of embedding representation of sentence pairs for training a sentence vector model according to an embodiment of the present invention;

[0038] Figure 4 A schematic diagram of a masked language model (MLM) training task for a sentence vector model according to an embodiment of the present invention;

[0039] Figure 5A schematic diagram of a binary classification training task for a sentence vector model according to an embodiment of the present invention;

[0040] Figure 6 A flowchart of fine-tuning the sentence vector model according to an embodiment of the present invention for binary classification based on a twin structure network;

[0041] Figure 7 The figure is a flow chart of a text retrieval method based on a sentence vector model according to an embodiment of the present invention. DETAILED DESCRIPTION

[0042] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below through specific embodiments in conjunction with the accompanying drawings. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

[0043] Before specifically introducing the embodiments of the present invention, some of the terms used therein are explained as follows:

[0044] BERT: BERT (Bidirectional Encoder Representations from Transformer) is a pre-trained language model. Its essence is to learn a good feature representation for a word through self-supervised learning based on a massive corpus (self-supervised learning refers to supervised learning that runs on data that is not manually labeled). In a specific natural language processing (NLP) task, the feature representation of BERT can be directly used as the word embedding feature of the task. Therefore, BERT provides a model for transfer learning of other tasks. The model can be fine-tuned according to the task or fixed as a feature extractor.

[0045] Twin network structure: The twin network structure is a "conjoined neural network (including two interconnected neural networks)", where "conjoined" means that it is achieved by sharing weights, and its function is to measure the similarity of two inputs. The twin network structure has two inputs (Input1 and Input2), and the two inputs are fed into two neural networks (Network1 and Network2). These two neural networks map the inputs to new spaces respectively, forming a representation of the input in the new space.

[0046] As mentioned in the background technology, the classic BERT trains and infers single words as a character on Chinese data. This method is only suitable for embedding single English words. In addition, the original BERT includes two pre-training tasks, namely Masked Language Model training and Next Sentence Prediction. When training the Masked Language Model, 15% of the words will be randomly selected for prediction at the beginning. In the subsequent N epochs of training, the selected words will no longer change. The training data is monotonous, and a large amount of training data needs to be collected to make up for the problem of insufficient training data. At the same time, the pre-trained model lacks a certain generalization ability. Another pre-training task in the original BERT model is the next sentence prediction, which is only suitable for the model to more accurately understand the semantic information at the sentence and paragraph levels.

[0047] When training the BERT model, the inventors realized that the way of segmenting the sentences input into the BERT model for pre-training based on the domain dictionary is more in line with the semantic background of Chinese. In addition, by performing data enhancement on the corresponding sentences and copying multiple copies, a large amount of training data is obtained. When training the masked language model, a predetermined proportion of words are randomly selected for masking for each copy of the data. During training, one sentence has one masking method, and multiple copies of the same sentences form a variety of different masking methods, so that the pre-trained model has a stronger generalization ability. Then remove the next sentence prediction task of the original BERT model, and improve the BERT model to better adapt to downstream tasks by training the binary classification task of text similarity or not in the form of input sentence pairs each time.

[0048] In order to intuitively demonstrate the application process of the present invention, Figure 1To explain, first, the BERT model is pre-trained to obtain the first BERT model. During pre-training, the training data is input into the BERT model in the form of several sentence pairs for MLM pre-training plus binary classification task pre-training. Here, the original BERT model's MLM pre-training and next sentence prediction (NSP) pre-training are transformed into MLM pre-training plus binary classification task pre-training, and the original NSP task is removed. By training the sentence pairs for binary classification task pre-training, the BERT model is improved to better adapt to downstream tasks. Secondly, the downstream task uses the classification data set corresponding to the task in the corresponding field to fine-tune the first BERT model to obtain a sentence vector model. The classification data set is input into the twin network structure built based on the first BERT model in the form of several sentence pairs for binary classification training to obtain a more robust sentence vector model. Finally, when performing text retrieval, the corresponding query text is input into the sentence vector model to obtain the corresponding query text vector. Before performing text retrieval, the corresponding text library is input into the sentence vector model to obtain a vector pool formed by several in-library text vectors, the query text vector is matched with the in-library text vectors in the vector pool, the in-library text vector with the highest similarity to the query text vector is obtained, and the text corresponding to the in-library text vector is output.

[0049] Based on the above research, according to one embodiment of the present invention, the present invention adopts an open source BERT language model (hereinafter referred to as BERT model), such as Figure 2 As shown, a sentence vector model training method is provided, including steps S1, S2, and S3. Each step is described in detail below.

[0050] Step S1: Perform data enhancement processing on multiple original sentences to obtain multiple similar sentences for each original sentence, and copy all sentences multiple times to construct multiple sentence pairs and their similarity labels.

[0051] According to one embodiment of the present invention, each original sentence is subjected to data enhancement processing, and the processing methods include synonym replacement, random insertion, random exchange, random deletion and their combination. Among them, random insertion and random exchange can be performed multiple times, and random deletion is to randomly delete words in the sentence with a fixed probability. The following are examples of the data enhancement processing methods:

[0052] Original sentence a: I would like to ask, my husband bought a house before marriage and registered the house under his name before marriage, how should the house be divided in case of divorce.

[0053] Using synonym replacement, replace "register" with "register", and obtain a similar sentence b: I would like to consult, my husband bought a house before marriage and registered the house under his name before marriage, how should the house be divided in case of divorce.

[0054] Random insertion method, randomly insert "please advise" and "divide", and obtain a similar sentence c: I would like to consult, please advise my husband bought a house before marriage and registered the house under his name before marriage, if we divorce, how to divide the house.

[0055] Randomly exchange "before marriage" and "bought" and "this" and "house", and get a similar sentence d: I would like to consult, my husband bought a house before marriage and registered the house under his name before marriage, if we divorce, how should the house be divided.

[0056] Using the random deletion method, delete "house" and obtain a similar sentence: I would like to consult. My husband bought a house before marriage and registered the house under his name before marriage. How should the house be divided in case of divorce.

[0057] According to one embodiment of the present invention, the process of constructing a plurality of sentence pairs and their similarity labels includes:

[0058] A plurality of similar sentence pairs are obtained, and similarity tags are marked as similar, wherein each similar sentence pair is composed of two sentences randomly selected from an original sentence and a plurality of similar sentences corresponding to the original sentence. For example, two sentences are randomly selected from the original sentence a and its similar sentences b, c, d, and e as a similar sentence pair.

[0059] A plurality of dissimilar sentence pairs are obtained, and similarity labels are marked as dissimilar, wherein each dissimilar sentence pair is composed of two sentences randomly selected from a number of original sentences, or from an original sentence and similar sentences corresponding to different original sentences, or from dissimilar sentences corresponding to different original sentences.

[0060] Among them, multiple similar sentence pairs are automatically regarded as positive sample pairs, and the similarity label is marked as 1, and multiple dissimilar sentence pairs are regarded as negative sample pairs, and the similarity label is marked as 0.

[0061] Step S2: pre-training the BERT model based on the multiple sentence pairs to obtain a first BERT model, wherein the pre-training includes pre-training a masked language model using sentence pairs and pre-training to determine whether two sentences are similar using sentence pairs and similarity labels.

[0062] According to one embodiment of the present invention, before pre-training the BERT model based on the multiple sentence pairs, that is, before the sentence pairs are input into the BERT model, the sentence pairs are segmented using custom vocabulary in a domain dictionary. The domain dictionary is an industry dictionary, such as a txt file, which stores custom vocabulary of a certain industry (telecommunication industry), such as: 5G package, Tianyi Cloud, traffic acceleration package, etc.

[0063] In addition, during pre-training, the BERT model is trained in multiple rounds and batches, and multiple sentence pairs are selected in each batch (the number of sentence pairs is generally a multiple of 8, and the commonly used values ​​are 16, 32, and 64) and input into the BERT model for training. The specific process of pre-training may include:

[0064] S21. Randomly mask a predetermined proportion of words in the sentence pair, perform masked language model pre-training in the BERT model, obtain the prediction results of the masked words, and calculate the first sub-loss based on the prediction results and the masked original words. The present invention initially copies all sentences into k copies, and randomly selects 15% of the words (Tokens) for masking in each sentence, wherein 80% of the randomly selected 15% of the words are replaced with [mask], 10% of the words in the 15% of the words remain unchanged, and 10% of the words in the 15% of the words are replaced with other words.

[0065] For example: when 15% of the words (Tokens) are randomly selected for masking, 80% of the 15% of the words are masked, for example: today, weather, great, suitable, go out, travel → today, weather, great, suitable, go out, [mask], 10% of the 15% of the words are randomly replaced by a masked word, for example: today, weather, great, suitable, go out, travel → today, weather, great, suitable, go out, running, 10% of the 15% of the words remain unchanged, for example: today, weather, great, suitable, go out, travel → today, weather, great, suitable, go out, travel; or the word weather in the copied sentence is masked, for example: today, weather, great, suitable, go out, travel → today, [mask], great, suitable, go out, travel.

[0066] That is to say, all sentences are copied multiple times, each for training in a different epoch interval. When pre-training the masked language model in each epoch interval, the currently used sentence is randomly masked, where one epoch interval includes one or more epochs. For example: Assume that there is only one epoch in an epoch interval, and each epoch uses a random masking method to mask the sentence. Multiple sentences have multiple different masking methods. When all sentences are trained for N epochs, the masked words (Tokens) of the same sentence will change in the training of these N epochs. This not only makes up for the lack of training data, but also makes the trained model more generalizable. Assuming that an epoch interval includes multiple epochs, random masking is performed in the first epoch of the epoch interval, and the masking methods of other epochs in the epoch interval remain unchanged.

[0067] S22. Input the sentence pairs into the BERT model for binary classification pre-training, obtain the classification results indicating whether the two sentences in the sentence pairs are similar, and calculate the second sub-loss according to the classification results and the similarity labels. Each time, the BERT model is improved to better adapt to downstream tasks by training the binary classification task of text similarity in the form of input sentence pairs.

[0068] like Figure 3 As shown in the figure, before the sentence after word segmentation is input into the BERT model, the sentence needs to be processed to obtain the sentence vector embedding representation. The sentence vector embedding representation includes word embedding vectors (Token Embeddings), segment embedding vectors (Segment Embeddings) and position embedding vectors (Position Embeddings). The word embedding vector, segment embedding vector and position embedding vector corresponding to each word are summed to obtain the token corresponding to the word. The tokens corresponding to multiple words are connected to form the sentence vector embedding representation. Figure 3 As shown, the input sentence vector embedding is represented as [CLS], today, weather, awesome, [SEP], today, weather, very good, [SEP]. Among them, Figure 3The word embedding vectors correspond to E[CLS], E today, E weather, E awesome, E[SEP], E today, E weather, E very good, E[SEP]; the corresponding segment embedding vectors are EA, EA, EA, EA, EB, EB, EB, EB, EB; the corresponding position embedding vectors are E1, E2, E3, E4, E5, E6, E7, E8. Whether in the form of a sentence or a sentence pair, the first token is [CLS], which is used to represent the entire sentence or sentence pair. In other words, [CLS] is set before each sentence or sentence pair for subsequent classification training tasks. [SEP] is used as a separator to split two input sentences. In the segment embedding vector, EA is used to indicate the first sentence in the sentence pair, and EB is used to indicate the second sentence in the sentence pair. EA and EB are used to distinguish two different sentences. The position embedding vectors correspond to E0, E1, E2, E3, E4, E5, E6, E7, E8, EN, etc., and E1 to EN are used to represent the position vector of each word.

[0069] According to an example of the present invention, mask language model pre-training is performed in the BERT model as follows: Figure 4 As shown in the figure, the input is a sentence embedding representation: [CLS], token1, [mask], ..., tokenN. Assuming there are N words, the word embedding vectors are E[CLS], EC1, EC2, ..., ECN, the corresponding segment embedding vectors are EA, EA, EA, ..., EB, and the corresponding position embedding vectors are E0, E1, E2, ...EN. Among them, token1 represents the first word vector obtained by summing the word embedding vector, segment embedding vector and position embedding vector corresponding to the first word, [mask] after token1 represents the word vector of the second masked word, and tokenN represents the word vector of the Nth word. The sentence vector embedding representation is input into the BERT model. After the MLM (Masked Language Model) training task is performed, the output vectors C, T1, T2, ..., TN are obtained, where C represents the classification result, T1, ..., TN represent the corresponding words token1, ..., tokenN, T2 represents the prediction result of the word corresponding to [mask], and the prediction result of the masked word is output.

[0070] According to an example of the present invention, binary classification pre-training is performed in the BERT model as follows Figure 5 As shown, the corresponding reference numerals have the same meaning as Figure 4Through the binary classification training task, the output features C, T1, T2, ..., TN are obtained, where C represents the classification result, and T1, T2, ..., TN represent the corresponding words token1, token2, ..., tokenN. The classification results of the two sentences after binary classification are output, that is, C is used to represent whether the two sentences are similar.

[0071] S23. Update the BERT model parameters by back propagation using the total loss of the weighted sum of the first sub-loss and the second sub-loss to obtain a first BERT model.

[0072] Step S3: Fine-tune the first BERT model using the classification data set corresponding to the task in the corresponding field to obtain a sentence vector model. The task in the corresponding field is to fine-tune the first BERT model using the classification data set in the field to perform domain adaptation when the model is actually applied to the classification task in the corresponding field after the first BERT model is pre-trained.

[0073] According to one embodiment of the present invention, step S3 may include:

[0074] S31. Input the samples in the classification data set into the twin network structure built based on the first BERT model to obtain the classification results of the samples. Figure 6 According to one embodiment of the present invention, the twin network structure includes two subnets, a vector splicing layer connecting the two subnets, and a classifier, each subnet includes a feature extraction layer and a pooling layer, the feature extraction layer uses the first BERT model, the sample includes a sentence pair and its similarity label, and the two sentences of the sentence pair obtain their corresponding sentence vectors through the two subnets respectively. According to an example of the present invention, the sample includes sentence A and sentence B and similarity labels, the sample is input into the twin network structure for feature extraction through the first BERT model, and the average pooling strategy (mean pooling) is adopted to obtain two sentence vectors u and v. The sentence vectors obtained from the two subnets are spliced ​​by the vector splicing strategy of the vector splicing layer, and the spliced ​​vectors are input into the Softmax classifier for binary classification to obtain the classification result of whether the sentence pair is similar. The two sentence vectors can be spliced ​​in the following ways: (u, v), (|uv|), (u*v), (|uv|, u*v), (u, v, u*v), (u, v, |uv|) and (u, v, |uv|, u*v). It is found through experiments that the classification result obtained by using the concatenation method of (u, v, |uv|) and the Softmax classifier is the most accurate. Therefore, according to a preferred embodiment of the present invention, the sentence vectors u and v are concatenated with the sentence vector difference |uv| to obtain (u, v, |uv|).

[0075] According to another embodiment of the present invention, relative to the twin network structure of the previous embodiment, a pooling layer is not set in the two subnets. The twin network structure may include two subnets, a vector concatenation layer connecting the two subnets, and a classifier, each subnet only includes a feature extraction layer, the feature extraction layer adopts the first BERT model, the sample includes a sentence pair and its similarity label, and the two sentences of the sentence pair obtain their corresponding sentence vectors through the two subnets respectively.

[0076] S32, adjusting the parameters of the first BERT model according to the classification results, performing iterative training, and obtaining the second BERT model, i.e., the sentence vector model, after the iterative training, wherein adjusting the parameters of the first BERT model according to the classification results may specifically be to calculate the loss using the cross entropy loss function according to the classification results and the true results, and updating the parameters of the first BERT model by stochastic gradient descent, wherein the update is stopped when the loss is less than or equal to a specified threshold. The loss value may be set to stop updating when the loss drops below 0.1, and the trained model is used as the sentence vector model for final inference.

[0077] Among them, the training weight parameters and the cross entropy loss function can be calculated as follows:

[0078]

[0079] Where N represents the number of samples in each batch, y i represents the label of sample i, similar sentence pairs are 1, dissimilar sentence pairs are 0, p i Represents the probability that sample i is predicted to be a similar sentence.

[0080] According to another embodiment of the present invention, the twin structure network is not the only way to fine-tune the first BERT model. For example, the first BERT model can also be fine-tuned in the form of triples. All sentences in the training set obtain corresponding sentence embedding vectors, and several forms such as<a,p,n> The triplet is then calculated, and then the triplet loss (TrapletLoss) is calculated, where a is the benchmark sample, or anchor sample, p is a sentence similar to a, called a positive sample, and n is a sentence dissimilar to a, called a negative sample.

[0081] The triplet loss can be calculated as follows:

[0082] L t =max(d(a,p)-d(a,n),0)

[0083] Among them, d(a,p) represents the distance between a and p, d(a,n) represents the distance between a and n, and max(·,0) represents the maximum value between the output · and 0. Therefore, the final optimization goal is to shorten the distance between a and p and increase the distance between a and n, thereby fine-tuning the first BERT model parameters.

[0084] However, according to experiments, it was found that fine-tuning the first BERT model through a twin network structure with a pooling layer built based on the first BERT model was the most effective.

[0085] Before performing text retrieval, the corresponding text library is input into the sentence vector model, and the text in the text library is extracted into a library text vector through the sentence vector model to obtain a vector pool formed by a number of library text vectors, and the vector pool is saved locally to avoid the need to extract vectors from a large amount of text in the text library each time a text is queried, thereby saving query time. In one embodiment, the present invention further provides a text retrieval method based on a number of library text vectors that have been saved locally, such as Figure 7 As shown, the following steps are included:

[0086] A1. Input the query text into the sentence vector model obtained by the sentence vector model training method described above to obtain the corresponding query text vector.

[0087] A2. Obtain the in-library text vectors extracted by the sentence vector model for each text in the text library.

[0088] A3. Calculate the similarity between each in-library text vector and the query text vector, and output the text in the text library with the highest similarity to the query text. When the similarity is greater than a specified threshold, the text corresponding to the in-library text vector and the query text corresponding to the query text vector are used as similar texts, wherein among all similarities greater than the specified threshold, the text corresponding to the in-library text vector with the highest score is selected as the output text.

[0089] According to one embodiment of the present invention, cosine similarity is used to calculate the similarity between sentence vectors to evaluate the similarity between original texts. The principle of cosine similarity is as follows: the cosine value of the angle between two vectors in the vector space is used as a measure of the difference between two individuals. The closer the value is to 1, the closer the angle is to 0, that is, the more similar the two vectors are, which is called cosine similarity. It measures the similarity of the proportions of two variables in various directions (attributes). If the distance of cosine similarity is greater than 0.5, the two sentences of the sentence pair are considered similar, otherwise they are not similar.

[0090] According to one embodiment of the present invention, a text retrieval method is integrated through the Faiss retrieval tool to index vectorized data and conduct effective searches on it, thereby improving retrieval efficiency and reducing computational overhead. Specifically, the in-library text vectors obtained by training the sentence vector module are stored, and when the query text is input, the Faiss tool is used to calculate the most similar in-library text vector based on the distance calculation method of cosine similarity, and the text corresponding to the most similar in-library text vector is used as the output text result.

[0091] To verify the effect of text retrieval of the present invention, the present invention uses a text library of 10,000 texts for retrieval experiments. Using the text retrieval method of the present invention, it only takes 5 seconds to obtain the text vector in the library through the sentence vector model, and it only takes 0.01 seconds to calculate the similarity, which greatly shortens the time for short text retrieval in a massive text library. At the same time, its accuracy can reach 88%, which is 4% higher than the original BERT model. The computer configuration used in the experiment: Intel i7-5820K CPU@3.30GHz, Nvidia Tesla V100 GPU.

[0092] The experimental results are shown in Table 1 below: (the higher the value, the faster the rate of calculating the number of sentences per second).

[0093] Table 1 Experimental data corresponding to different models

[0094] Model CPU GPU Unified Sentence Encoding Model 67 1318 BERT Model 44 1378 Sentence vector model 83 2024

[0095] It should be noted that although the above describes the various steps in a specific order, it does not mean that the various steps must be executed in the above specific order. In fact, some of these steps can be executed concurrently or even in a different order as long as the required functions can be achieved.

[0096] The present invention may be a system, a method and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for causing a processor to implement various aspects of the present invention.

[0097] Computer readable storage medium can be a tangible device that holds and stores instructions used by an instruction execution device. Computer readable storage medium can include, for example, but is not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination thereof. More specific examples (non-exhaustive list) of computer readable storage medium include: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a static random access memory (SRAM), a portable compact disk read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, for example, a punch card or a protruding structure in a groove on which instructions are stored, and any suitable combination thereof.

[0098] The embodiments of the present invention have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The terms used herein are selected to best explain the principles of the embodiments, practical applications, or technical improvements in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.

Claims

1. A sentence vector model training method, characterized in that: include: S1. Perform data enhancement processing on multiple original sentences to obtain multiple similar sentences of each original sentence. Copy all sentences multiple times to construct multiple sentence pairs and their similarity labels. S2. Pre-training the BERT model based on the multiple sentence pairs to obtain a first BERT model, wherein the pre-training includes pre-training a masked language model using sentence pairs and pre-training to determine whether two sentences are similar using sentence pairs and similarity labels; S3. Fine-tune the first BERT model using the classification data set corresponding to the task in the corresponding field to obtain a sentence vector model; The pre-training learning process includes: Randomly mask a predetermined proportion of words in the sentence pair, perform masked language model pre-training in the BERT model, obtain the prediction results of the masked words, and calculate the first sub-loss based on the prediction results and the masked original words; Input the sentence pair into the BERT model for binary classification pre-training to obtain a classification result indicating whether the two sentences in the sentence pair are similar, and calculate the second sub-loss based on the classification result and the similarity label; The total loss of the weighted sum of the first sub-loss and the second sub-loss is used to update the BERT model parameters through back propagation to obtain the first BERT model.

2. The method according to claim 1, characterized in that The process of constructing multiple sentence pairs and their similarity labels includes: Obtain multiple similar sentence pairs, and mark the similarity tags as similar, wherein each similar sentence pair consists of two sentences randomly selected from an original sentence and multiple similar sentences corresponding to the original sentence; A plurality of dissimilar sentence pairs are obtained, and similarity labels are marked as dissimilar, wherein each dissimilar sentence pair is composed of two sentences randomly selected from a number of original sentences, or from an original sentence and similar sentences corresponding to different original sentences, or from dissimilar sentences corresponding to different original sentences.

3. The method according to claim 1, characterized in that The fine-tuning of the first BERT model using a classification data set corresponding to a task in a corresponding field includes: Input the samples in the classification data set into the twin network structure based on the first BERT model to obtain the classification results of the samples; The first BERT model parameters are adjusted according to the classification results, and iterative training is performed. After the iterative training, the sentence vector model is obtained.

4. The method according to claim 3, characterized in that: The adjusting the first BERT model parameters according to the classification result includes: The loss is calculated using the cross entropy loss function based on the classification results and the true results, and the first BERT model parameters are updated by stochastic gradient descent. When the loss is less than or equal to the specified threshold, the update is stopped.

5. The method according to any one of claims 1 to 3, characterized in that: Before the sentence pair is input into the BERT model, the sentence pair is segmented using the custom vocabulary in the domain dictionary.

6. The method according to any one of claims 1 to 3, characterized in that: The data enhancement process includes synonym replacement, random insertion, random exchange, random deletion and a combination thereof.

7. A text retrieval method, characterized in that: include: Inputting the query text into the sentence vector model obtained by the method described in any one of claims 1 to 6 to obtain a corresponding query text vector; Obtaining the in-library text vectors extracted by the sentence vector model for each text in the text library; Calculate the similarity between the text vectors in each library and the query text vector, and output the text in the text library with the highest similarity to the query text.

8. The text retrieval method according to claim 7, characterized in that: Also includes: When the similarity is greater than a specified threshold, the text corresponding to the in-library text vector and the query text corresponding to the query text vector are taken as similar texts, wherein, among all similarities greater than the specified threshold, the text corresponding to the in-library text vector with the highest score is selected as the output text.

9. A computer-readable storage medium, characterized in that: A computer program is included thereon, and the computer program can be executed by a processor to implement the steps of the method according to any one of claims 1 to 7.

10. An electronic device, characterized in that: include: one or more processors; as well as A memory, wherein the memory is used to store one or more executable instructions; The one or more processors are configured to implement the steps of the method of any one of claims 1 to 7 by executing the one or more executable instructions.