Text embedding vector generation method and device based on pre-training model

By using the multi-layer hidden states of the pre-trained model and performing a weighted average in the generation of text embedding vectors, the problem of one-sided information caused by relying only on the output of the last layer in the existing technology is solved, and the quality and expressive ability of text embedding vectors are improved. , improving the performance of downstream tasks.

CN118586399APending Publication Date: 2024-09-03UNIV OF ELECTRONICS SCI & TECH OF CHINA
View PDF 0 Cites 5 Cited by

Patent Information

Application Number
CN202410641351.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-05-22
Publication Date
2024-09-03

AI Technical Summary

Technical Problem

Existing text embedding vector generation methods only rely on the output of the last layer of the model, which may ignore valuable information at other levels, resulting in the generated text embedding vector being one-sided in terms of feature attention, unable to comprehensively and accurately express the semantics of the text, and affecting downstream tasks. performance.

Method used

Multi-layer hidden states based on the pre-trained model are used to generate text embedding vectors, and a weighted average is performed according to the number of layers, and the feature information of different layers is combined to generate a more comprehensive and accurate text embedding vector.

Benefits of technology

By utilizing multi-layer hidden states and weighted average technology, the quality and expressiveness of text embedding vectors are improved, and the performance of downstream tasks is enhanced, especially in tasks such as text deduplication, sentiment discrimination, text clustering and text retrieval.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118586399A
    Figure CN118586399A
Patent Text Reader

Abstract

The invention provides a text embedding vector generation method and device based on a pre-training model, and the method comprises the steps: obtaining to-be-processed text data, and carrying out the preprocessing and cleaning of the to-be-processed text data; inputting the processed text data into the constructed language model module to generate each layer of hidden state of the model; wherein the language model module comprises a word embedding layer, a position coding layer and a Transform coding layer; inputting multiple layers of hidden input vectors output by the language model into a vector pooling module to obtain text embedding vectors; wherein the vector pooling module comprises a model reasoning module and a pooling module; according to the method, the multi-layer hidden state is generated through the model, then the text embedding is generated by using the multi-layer hidden state, and weighted averaging is carried out on the multi-layer hidden state according to the importance of the number of layers, so that the text embedding vector considering the characteristics of each layer is obtained.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of text embedding vector generation and artificial intelligence, and in particular to a method and device for generating high-quality text embedding vectors. Background Art

[0002] In natural language processing, the generation of text embedding vectors is one of the most important technologies. Generating high-quality text embedding vectors has always been a core goal in the field of artificial intelligence. Text embedding vectors are vectors used to represent the semantics of text, and their quality directly affects the results of downstream tasks. A good text embedding generation method should ensure that in the feature space, the embedding vectors corresponding to semantically similar texts are close, while the embedding vectors corresponding to semantically dissimilar texts are far apart.

[0003] At present, the mainstream method of generating text embedding vectors based on pre-trained models is to use the pre-trained model to generate vectors for each word, and then convert these word vectors into sentence vectors through pooling. Traditionally, it is believed that higher levels of pre-trained models can more comprehensively express the overall semantics of the text. Therefore, in practice, only the word embedding vectors output by the last layer of the model are processed to obtain the text embedding vector.

[0004] However, after further research, we found that different layers of the model actually focus on different information, and the output of the highest layer does not show the best performance in all evaluation indicators. Simply relying on the output of the last layer for pooling may ignore the valuable information contained in other layers, resulting in the generated text embedding vector being one-sided in feature focus.

[0005] Existing text embedding vector generation methods, such as CLS pooling, Last pooling, mean pooling, and max pooling, all operate based on the word vector output by the last layer of the model. Although these methods are simple and easy to implement in practice, they all have a common limitation: they are all based on the assumption that the word vector output by the last layer can provide the best results in all tasks. However, experimental evidence shows that in some tasks, the output of higher levels of the model can even outperform the last layer. This means that a solution that relies solely on the last layer for pooling may not be able to fully and accurately express the semantics of the text.

[0006] In addition, text embedding vector generation is the basis of many tasks in natural language processing, such as text deduplication, sentiment discrimination, text clustering, text retrieval, and anomaly detection. Therefore, the representation ability of text embedding vectors is directly related to the effect of these downstream tasks. In order to improve the performance of these tasks, it is necessary to seek a method that can generate more comprehensive and accurate text embedding vectors. Summary of the invention

[0007] In order to solve the shortcomings of the prior art, this patent proposes a method for generating text embedding vectors based on a pre-trained model. This method uses the multi-layer hidden states generated by the pre-trained model to generate text embeddings, and performs weighted averaging according to the importance of the number of layers, thereby obtaining a text embedding vector that takes into account the characteristics of each layer.

[0008] To achieve the above object, the present invention is implemented by adopting the following technical solutions:

[0009] In a first aspect, the present invention provides a method for generating a text embedding vector based on a pre-trained model, comprising:

[0010] Obtain the text data to be processed, and perform preprocessing and cleaning;

[0011] Input the processed text data into the constructed language model module to generate the hidden state of each layer of the model;

[0012] The vector pooling module constructed by the multi-layer hidden input output of the language model obtains the text embedding vector.

[0013] The language model module execution process includes:

[0014] Constructing a language model module, wherein the language model module includes a word embedding layer, a position encoding layer, and a Transformer encoding layer;

[0015] The processed data is input into the language model module for inference to obtain the hidden state of each layer of the model;

[0016] The Transformer encoding layer includes a multi-head self-attention layer and a feedforward neural network layer;

[0017] The multi-head self-attention layer refers to the multi-head attention mechanism module, which is the core module of the Transformer model. This module allows the model to jointly focus on information from different representation subspaces at different positions. The multi-head self-attention in the encoder layer uses a bidirectional encoding method because it has complete text;

[0018] The feedforward neural network layer scales and linearly transforms the results produced by the multi-head attention layer.

[0019] The vector pooling module execution process includes:

[0020] Constructing vector pooling module

[0021] The obtained multi-layer hidden layer states are input into the vector pooling module for training to obtain the text embedding vector;

[0022] The vector pooling module specifies the number of layers, selects hidden variables output by higher layers, performs mean pooling on them to obtain vectors of the corresponding number of layers, and then performs weighted averaging on these vectors to obtain the final text embedding vector.

[0023] In a second aspect, the present invention provides a device for generating a text embedding vector based on a pre-trained model, the device comprising:

[0024] A pre-training module, used to pre-process the text and pre-encode the processed text;

[0025] The language model module is responsible for generating the hidden state of each layer of the model based on the results of the preprocessing module;

[0026] The vector pooling module is responsible for pooling the multi-layer hidden layers output by the language model to obtain the text embedding vector;

[0027] Tool module, responsible for cleaning and filtering training data.

[0028] Compared with the prior art, the text embedding vector generation method based on the pre-training model provided by the present invention has the following beneficial effects:

[0029] 1) The input text is preprocessed with the help of encoding tools in natural language, and the BERT model with good results in the field of text embedding vector generation is introduced. Under the condition of the same text input, the quality of text embedding vectors in the current text embedding vector generation is improved.

[0030] 2) This paper proposes a new text embedding vector pooling method, which uses the characteristics of different layers in the pre-trained model focusing on different data, combines the text representation vectors of different layers, and gives more weight to higher layers to obtain text representation vectors. This method can obtain more types of information from the model and give appropriate attention to this information, thereby enhancing the text expression ability of the model. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] The present invention will be described in more detail below based on embodiments and with reference to the accompanying drawings, wherein:

[0032] Figure 1 A schematic diagram of a flow chart of a method for generating a text embedding vector based on a pre-trained model of the present invention is shown;

[0033] Figure 2 A schematic diagram of the execution flow of the language model module of the present invention is shown;

[0034] Figure 3 A schematic diagram of the execution flow of the vector pooling module of the present invention is shown. DETAILED DESCRIPTION

[0035] The present invention will be further described below in conjunction with the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solution of the present invention, and cannot be used to limit the protection scope of the present invention.

[0036] Embodiment 1:

[0037] like Figure 1 As shown, an embodiment of the present invention provides a method for generating a text embedding vector based on a pre-trained model, comprising the following steps:

[0038] Step S1, clean the text corpus, establish a vocabulary according to the subword encoding algorithm, and segment the input text according to the subword table;

[0039] Step S2: input the processed text data into the constructed language model module to generate the hidden state of each layer of the model;

[0040] The language model execution process includes:

[0041] Step S201, constructing a language model module, the language model module includes a word embedding layer, a position encoding layer, and a Transformer encoding layer;

[0042] Step S202: input the processed data into the language model module for training to obtain the hidden state of each layer of the model;

[0043] Step S3, the vector pooling module constructed by the multi-layer hidden input output by the language model obtains the text embedding vector;

[0044] Among them, the vector pooling execution process includes:

[0045] Step S301, constructing a vector pooling module, where the vector pooling module includes a model reasoning module and a pooling module;

[0046] Step S302: input the obtained multi-layer hidden layer state into the vector pooling module for training to obtain the text embedding vector;

[0047] like Figure 2 As shown in the figure, the language model module inputs the BERT language model according to the result of word segmentation and outputs N hidden states. The specific execution details of the language model include:

[0048] 1) Shuffle the sequences in the training set and input them into the model in small batches;

[0049] 2) The model performs a preprocessing operation on the input encoding, converting it into a word embedding, and then combines the position embedding information to obtain a context embedding;

[0050] 3) The upper and lower word embeddings obtained after preprocessing are input into the neural network, and the three weight matrices are respectively operated to obtain the three matrices Q, K, and V. Q, K, and V are respectively passed through the self-attention module to obtain the attention score matrix between each character and other characters. The operation method is as follows:

[0051]

[0052] 4) Z 1~n After concatenation, it is passed into the linear layer to obtain a final output Z with the same dimension as the input matrix X of the multi-head attention layer;

[0053] 5) The residual and regularization layer outputs the output X of the multi-head attention layer, that is, Z is residually connected to X, and then a linear regularization operation is performed to convert the input of each layer of neurons into a standard normal distribution with a mean and variance.

[0054] X = LayerNorm(X + Z);

[0055] 6) The multi-layer perceptron module uses two fully connected layers to process the results. The output has the same dimension as the input, followed by a residual regularization layer operation to obtain the hidden state h i , each hidden state contains m vectors represented by h i =(e i1 ,e i2 …e im );

[0056] 7) The output from 3) is used as the input of the next cycle. The training cycle ends after N times and n hidden states H = (h1, h2…h n );

[0057] like Figure 3 As shown in the figure, the vector pooling module generates text embedding vectors based on the N hidden states generated by the language model and the pooling method; the specific execution details of the vector pooling model include:

[0058] 1) Select the hidden states from the tth layer to the last layer, and perform average pooling on the vectors of each layer as the text representation vector e of that layer i , and finally get n-t+1 layer vectors (e t ,e t+1 …e n );

[0059] 2) Take a weighted average of these vectors. The weight of the tth layer is the lowest, and the weights increase as the layers go up, so that the overall semantics gains a higher weight. The weight formula is:

[0060]

[0061] 3) After weighted averaging, the final text embedding representation vector E can be obtained. The text embedding vector representation formula is:

[0062]

[0063] Embodiment 2:

[0064] An embodiment of the present invention may provide a device for generating a text embedding vector based on a pre-trained model, the device comprising:

[0065] A pre-training module, used to pre-process the text and pre-encode the processed text;

[0066] The language model module is responsible for generating the hidden state of each layer of the model based on the results of the preprocessing module;

[0067] The vector pooling module is responsible for pooling the multi-layer hidden layers output by the language model to obtain the text embedding vector;

[0068] Tool module, responsible for cleaning and filtering training data.

[0069] Although the present invention is described herein with reference to specific embodiments, it should be understood that these embodiments are merely examples of the principles and applications of the present invention. It should therefore be understood that many modifications may be made to the exemplary embodiments and that other arrangements may be devised without departing from the spirit and scope of the present invention as defined by the appended claims. It should be understood that the various dependent claims and features described herein may be combined in a manner different from that described in the original claims. It should also be understood that features described in conjunction with individual embodiments may be used in other described embodiments.

Claims

1. A method for generating text embedding vectors based on a pre-trained model, characterized in that: include: Obtain the text data to be processed, and perform preprocessing and cleaning; Input the processed text data into the constructed language model module to generate the hidden state of each layer of the model; The vector pooling module constructed by the multi-layer hidden input output of the language model obtains the text embedding vector.

2. The method according to claim 1, characterized in that The language model module execution process includes: Constructing a language model module, wherein the language model module includes a word embedding layer, a position encoding layer, and a Transformer encoding layer; The processed data is input into the language model module for prediction to obtain the hidden state of each layer of the model.

3. The method according to claim 1, characterized in that The vector pooling module execution process includes: constructing a vector pooling module, inputting the obtained multi-layer hidden state into the vector pooling module training, and obtaining a text embedding vector.

4. The method according to claim 2, characterized in that: The Transformer encoding layer includes a multi-head self-attention layer and a feedforward neural network layer; The multi-head self-attention layer refers to the multi-head attention mechanism module, which is the core module of the Transformer model. This module allows the model to jointly focus on information from different representation subspaces at different positions; the multi-head self-attention in the encoder layer has the complete text and uses a bidirectional encoding method; The feedforward neural network layer scales and linearly transforms the results produced by the multi-head attention layer; the segmented text is input into the model to obtain the hidden state of each layer output of the model.

5. The method according to claim 3, characterized in that: The vector pooling module specifies the number of layers, selects hidden variables output by the predetermined number of layers, performs mean pooling on them to obtain vectors of the corresponding number of layers, and then performs weighted averaging on these vectors to obtain the final text embedding vector.

6. A device for generating text embedding vectors based on a pre-trained model, characterized in that: The device includes a pre-training module, a language model module, a vector pooling module and a tool module; A pre-training module, used to pre-process the text and pre-encode the processed text; The language model module is responsible for generating the hidden state of each layer of the model based on the results of the preprocessing module; The vector pooling module is responsible for pooling the multi-layer hidden layers output by the language model to obtain the text embedding vector; Tool module, responsible for cleaning and filtering training data; The modules are based on the method described in claim 1 and work together to improve the performance of the model in generating text embedding vectors.

Citation Information

Cited By

  • Watermark generation method and system based on large language model

    CN119577707A

  • Time sequence anomaly detection method, device and equipment based on KAN network and medium

    CN120067999A

  • Time series anomaly detection method, device, equipment and medium based on KAN network

    CN120067999B

  • Text embedding method and system based on large model intermediate layer fusion

    CN121168473A

  • A text embedding method and system based on large model intermediate layer fusion

    CN121168473B