A text-to-text system and method based on local-global attention

Through the local-global attention mechanism and sparse operation, the problems of high computational complexity and information loss of the traditional attention mechanism are solved, and efficient long text processing and performance improvement are achieved.

CN118333054BActive Publication Date: 2024-08-23ZHEJIANG LAB
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410748618.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-06-12
Publication Date
2024-08-23
Estimated Expiration
2044-06-12

AI Technical Summary

Technical Problem

The traditional attention mechanism has excessive computational complexity when processing long text sequences. The local attention mechanism may lose character information outside the window. The random selection of key characters may miss important characters and add unnecessary characters, resulting in a decline in model performance.

Method used

A text-to-text system based on local-global attention is used to filter out task-related characters through multiple rounds of routing characters for local attention processing, combined with sparse operations to reduce computational complexity and memory usage.

Benefits of technology

Effectively capture long-distance information, maintain linear computational complexity, while reducing the amount of parameters and memory usage, and improving model efficiency and performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118333054B
    Figure CN118333054B
Patent Text Reader

Abstract

The present invention discloses a text-to-text system and method based on local-global attention, the system comprising at least one electronic data storage, a data preprocessor and a model generator. At least one electronic data storage is used to store input data. The data preprocessor cleans, segments and generates word vectors for the input text; the model generator is a variant of transformer, in each attention block, the attention layer is a sparse local-global attention, and the feedforward layer is a sparse feedforward layer; sparsity can greatly reduce the parameters of the network, thereby reducing the calculation and storage space, and the local-global attention is the aggregation of multiple local attentions, thereby reducing the calculation complexity from the square level of the text length to the linear level. By constructing the model, the present invention can be used for text translation, knowledge question and answer, summary generation, etc., and its advantages are that the calculation complexity is greatly reduced and the storage space is saved, and the training and reasoning time of the model are shortened.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of natural language technology, and in particular relates to a system and method suitable for fast text generation of long texts. Background Art

[0002] Text-to-text tasks play an increasingly important role in our daily lives and work, such as machine translation, dialogue question answering, text summarization, etc. These tasks usually require processing long text sequences and in-depth understanding and expression of the text content. However, the traditional attention mechanism faces the problem of excessive computational complexity when processing long text sequences, and its computational complexity grows quadratically with the length of the input text. This poses a huge challenge to large-scale text processing tasks.

[0003] To solve this problem, researchers proposed a local attention mechanism. In the local attention mechanism, each character only pays attention to the neighboring characters within a fixed window, thereby reducing the computational complexity to be linear with the input length. Although this method improves efficiency, it may cause the loss of information about characters outside the window, thereby hindering the model from capturing long-distance dependencies and fully representing the input text. To overcome this limitation, researchers introduced a method that randomly selects a few key characters and allows all other characters to pay attention to these key characters (Manzil Zaheer, GuruGuruganesh, Avinava Dubey,Joshua Ainslie, Chris Alberti, Santiago Ontanon,Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. Big Bird:Transformers for Longer Sequences. Advances in neural information processingsystems 33 (2020): 17283-17297.). This method aims to maintain the information of key characters while maintaining computational efficiency. However, random selection may cause important key characters to be missed and unnecessary characters to be introduced, thus affecting the performance of the model.

[0004] To address these issues, researchers have proposed learnable key character selection methods (Joshua Ainslie, Tao Lei, Michiel de Jong, Santiago Ontañón, Siddhartha Brahma, Yury Zemlyanskiy, David Uthus, Mandy Guo, James Lee-Thorp, Yi Tay, Yun-Hsuan Sung,Sumit Sanghai. CoLT5: Faster Long-Range Transformers with Conditional Computation. arXiv preprint arXiv:2303.09752.). These methods use machine learning techniques to identify and select key characters based on their importance and relevance to the task. Although this method can effectively select key characters, it faces two major drawbacks:

[0005] Computational complexity: The selected key characters need to pay attention to each other, which leads to the computational complexity proportional to the square of the number of key characters.

[0006] Flexibility: Different characters may focus on different sets of characters. In order to select appropriate key characters that meet the needs of all characters, a large number of key characters may be required, which may reduce overall efficiency. Summary of the invention

[0007] The present invention aims to overcome the above-mentioned deficiencies in the prior art and provide a text-to-text system and method based on local-global attention. The present invention uses multiple rounds of routing characters to filter out task-related characters, and performs local attention on the characters filtered out by each routing, effectively capturing long-distance information while maintaining linear computational complexity. In addition, the present invention reduces the number of parameters and memory usage by performing sparse operations on each linear layer.

[0008] The first aspect of the present invention relates to a text-to-text system based on local-global attention, which comprises a data storage, a data preprocessor and a model generator. The data preprocessor processes the data stored in the data storage into a word vector sequence, and passes the sequence to the model generator to generate text.

[0009] A data storage device is used to store text information. The text information includes multiple types of text information, including texts in translation, classification, regression and summary. Each text includes a source text and a target text. The source text includes a task text and an object text. The task text is used at the beginning of the source text to indicate the task of the object text. The target text is a label text. For example, the task text is "English translation into Chinese:", the object text is English text, and the target text is the Chinese translation of the English text.

[0010] The data preprocessor is configured to generate a feature vector sequence that can be understood by the machine learning model from the data stored in the data storage device, specifically, discard low-quality data, segment words, build a dictionary, perform one-hot encoding on each word, convert text data into matrix data, and divide the matrix data into a training data set and a validation data set. The low-quality data includes text with a length of less than 20 and repeated text. The row vector of the matrix data corresponds to the one-hot encoding of the word.

[0011] The model generator uses the sequence generated by the data preprocessor to generate a specified type of text. The model generator consists of an input module, an output module, a local-global encoding module, a local-global decoding module, a linear module and a softmax module. Among them,

[0012] The input module is composed of a position encoding block and an input embedding block, and is configured to convert the source text matrix generated by the data preprocessor into a matrix with position information. The input embedding block embeds the source text matrix into the Euclidean space to obtain a dense matrix, and the position encoding block adds the position matrix to the dense matrix to obtain a matrix with position information.

[0013] The local-global encoding module generates a compressed information matrix using the matrix generated by the input module.

[0014] The output module concatenates the start symbol and the target text matrix and converts it into a matrix with position information, which consists of a fusion position block and an output embedding block. The input embedding block concatenates the start symbol and the target text matrix and embeds them into the Euclidean space to obtain a dense matrix, and the fusion position block adds the position matrix to the dense matrix to obtain a matrix with order information. If the target text matrix is ​​missing, the output of the model generator is used instead.

[0015] The local-global decoding module generates a representation using the compressed information matrix generated by the local-global encoding module and the matrix with order generated by the output module.

[0016] The linear module embeds the representation into the word vector space, and outputs a probability matrix through the sofmax module, where each row vector represents the probability of each word in the dictionary being selected.

[0017] Furthermore, the local-global encoding module is composed of a plurality of local-global encoding blocks stacked together, and its information flow is: the input of the first local-global encoding block is the output of the input module, and the input of the remaining local-global encoding blocks is the output of the previous local-global encoding block. The local-global encoding block is composed of two normalization layers, a sparse local-global self-attention layer, two addition layers and a sparse feedforward layer, which are used to compress information. The specific process is:

[0018] The output of the input module (or the output of the previous local-global coding block) passes through the normalization layer and the sparse local-global self-attention layer to become a representation matrix containing contextual relationships. The output of the input module (or the output of the previous local-global coding block) and the representation matrix are added in an addition layer to obtain a residual matrix. The residual matrix passes through another normalization layer and the sparse feedforward layer and is then added to itself to obtain a compression matrix.

[0019] Furthermore, the over-sparse local-global self-attention layer is composed of multiple routing sublayers, multiple sparse local self-attention sublayers, an addition sublayer and a linear sublayer. Its workflow is as follows: each of the routing sublayers selects multiple row vectors with top scores in the output matrix of the normalization layer, and passes the multiple row vectors to the corresponding sparse local self-attention sublayer. The addition sublayer aggregates the matrices output by the sparse local self-attention sublayers according to the principle of adding vectors corresponding to the text at the same position, and sends the aggregated matrices to the linear sublayer to obtain a representation matrix containing contextual relationships. Among them,

[0020] The number of the routing sublayers is the same as the number of the sparse local self-attention sublayers, and they form a paired relationship. The routing sublayer is a learnable vector, the vector dimension is consistent with the number of columns of the matrix output by the normalization layer, and it is configured to screen the row vectors of the matrix. Specifically, each row vector of the matrix is ​​scored by taking the inner product of the learnable vector and each row vector of the matrix, and some vectors with high scores are screened out to obtain a new matrix. The sparse local self-attention sublayer consists of three sparse linear elements, a local attention element and a weighting element, and is configured to encode local contextual relationships. The specific process is: the three sparse linear elements use the output matrix of the paired routing sublayer to generate three matrices Q, K and V respectively. The local attention element uses Q and K to generate the local attention matrix, and the weighting element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing local contextual relationships.

[0021] Wherein, the local attention element is configured to generate a local attention matrix. The local attention matrix is ​​obtained by performing a softmax operation on each row of a sparse matrix. The element of the i-th row and j-th column of the sparse matrix is ​​obtained by performing an inner product operation of the i-th row of Q and the j-th row of K, wherein the j-th row is no more than w rows away from the i-th row, and the remaining elements of the sparse matrix are set to negative infinity.

[0022] Furthermore, the over-sparse local-global self-attention layer can also be composed of multiple routing sublayers, multiple sparse local self-attention sublayers, an addition sublayer and a linear sublayer. Its workflow is as follows: the first routing sublayer selects multiple row vectors with top scores in the output matrix of the normalization layer to form a new matrix in sequence, and passes the new matrix to one of the sparse local self-attention sublayers. The remaining routing sublayers use the new matrix produced by the previous routing sublayer as input, regenerate a new matrix, and pass the regenerated new matrix to the corresponding sparse local self-attention sublayer. The addition sublayer adds up the matrices output by each sparse local self-attention sublayer according to the principle of adding vectors corresponding to the text in the same position, and sends the added matrix to the linear sublayer to obtain a representation matrix containing contextual relationships.

[0023] The number of the routing sublayers is the same as the number of the sparse local self-attention sublayers, and they form a pairing relationship.

[0024] Furthermore, the multiple routes include a trivial route, that is, selecting all row vectors in the input matrix.

[0025] Furthermore, the local-global decoding module is composed of a plurality of local-global decoding blocks stacked together. Its information flow is as follows: the input of the first local-global decoding block is the output of the output module and the output of the local-global encoding block, and the input of the remaining local-global decoding blocks is the output of the previous local-global decoding block and the output of the local-global encoding module. The local-global decoding block is composed of three normalization layers, a masked sparse local-global self-attention layer, a sparse local-global attention layer, three addition layers and a sparse feedforward layer, which are used to decode information.

[0026] The specific process is as follows: the output of the output module (or the output of the previous local-global decoding module) passes through the normalization layer and the masked sparse local-global self-attention layer to become a representation matrix containing the previous context relationship. The output of the output module (or the output of the previous local-global decoding block) and the representation matrix are added in an addition layer to obtain a residual matrix. The residual matrix and the output of the local-global encoding module pass through the second normalization layer and the sparse local-global attention layer to generate an output, and the output is added to the output of the output module (or the output of the previous local-global decoding block) in the second addition layer to obtain a decoding residual matrix. The output of the decoding residual matrix after passing through the third normalization layer and the feedforward layer is added to the decoding residual matrix in the third addition layer to obtain a decoding matrix.

[0027] Furthermore, the masked sparse local-global self-attention layer is composed of multiple routing sublayers, multiple masked sparse local self-attention sublayers, an addition sublayer and a linear sublayer. Its workflow is as follows: each of the routing sublayers screens out multiple row vectors with top scores in the output matrix of the normalization layer, and passes the multiple row vectors to the corresponding masked sparse local self-attention sublayer to reduce the dimension of the vector. The addition sublayer aggregates the matrices output by each masked sparse local self-attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and sends the aggregated matrix to the linear sublayer to obtain a representation matrix containing the previous text relationship. Among them, the number of the routing sublayers is the same as the number of the masked sparse local self-attention sublayers, and forms a pairing relationship.

[0028] The masked sparse local self-attention sublayer is composed of three sparse linear elements, a masked local attention element and a weighting element, and is configured to encode local previous context relationships. The specific process is: the three sparse linear elements use the output matrix of the paired routing sublayer to generate three matrices Q, K and V. The masked local attention element uses Q and K to generate the local attention matrix, and the weighting element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing the local previous context relationship. Where

[0029] The masked local attention element is configured to generate a local attention matrix. The local attention matrix is ​​obtained by performing a softmax operation on each row of a sparse matrix. The i-th row and j-th column element of the sparse matrix is ​​obtained by performing an inner product operation on the i-th row of Q and the j-th row of K, where j does not exceed i and is no more than w rows away from i. The remaining elements of the sparse matrix are set to negative infinity.

[0030] Furthermore, the masked sparse local-global self-attention layer can also be composed of multiple routing sublayers, multiple masked sparse local self-attention sublayers, an addition sublayer and a linear sublayer. Its workflow is as follows: the first routing sublayer selects multiple row vectors with top scores in the output matrix of the normalization layer to form a new matrix in sequence, and passes the new matrix to one of the masked sparse local self-attention sublayers. The remaining routing sublayers use the new matrix produced by the previous routing sublayer as input, regenerate a new matrix, and pass the regenerated new matrix to the corresponding masked sparse local self-attention sublayer. The addition sublayer splices the matrices output by each masked sparse local self-attention sublayer, and sends the spliced ​​matrix to the linear sublayer to obtain a representation matrix containing the previous context relationship. Among them, the number of the routing sublayers is the same as the number of the masked sparse local self-attention sublayers, and forms a pairing relationship.

[0031] Furthermore, the sparse local-global attention layer is composed of multiple routing sublayers, multiple sparse local attention sublayers, a splicing sublayer and a linear sublayer. Its workflow is as follows: each of the routing sublayers uses the output of the local-global encoding module through the output matrix of the normalization layer and the output matrix of the residual matrix through the normalization layer to select multiple row vectors with the top scores to form a new matrix A and a new matrix B, and pass the two new matrices to the corresponding sparse local attention sublayer. The splicing layer aggregates the matrices output by each sparse local attention sublayer according to the principle of adding vectors corresponding to the text at the same position. The linear sublayer uses the aggregated matrix to obtain the representation matrix.

[0032] Among them, the two new matrices have the same number of row vectors, and the number of the routing sublayers is the same as the number of the sparse local attention sublayers, and they form a paired relationship. The sparse local attention sublayer is composed of three sparse linear elements, one local attention element and one weighting element, and is configured to capture local textual relationships. The specific process is: one of the sparse linear elements uses the output matrix of the paired routing sublayer to generate the Q matrix; the other two sparse linear elements use the output matrix of the paired routing sublayer to generate the K and V matrices respectively. The local attention element uses Q and K to generate the local attention matrix. The weighting element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing local textual relationships.

[0033] Furthermore, the sparse local-global attention layer can also be composed of multiple routing sublayers, multiple sparse local attention sublayers, an addition sublayer and a linear sublayer. Its workflow is as follows: the first routing sublayer uses the output of the local-global encoding module through the output matrix of the normalization layer and the output matrix of the residual matrix through the normalization layer to select multiple row vectors with the top scores to form a new matrix A and a new matrix B, and pass the two new matrices to the corresponding sparse local attention sublayer. The remaining routing sublayers use the output of the previous routing sublayer as input, and the output new matrix is ​​passed to the corresponding sparse local attention sublayer. The addition sublayer aggregates the matrices output by each sparse local attention sublayer according to the principle of adding vectors corresponding to the text in the same position, and the linear sublayer uses the aggregated matrix to obtain a representation matrix. Among them, the two new matrices have the same number of row vectors, the number of routing sublayers is the same as the number of sparse local attention sublayers, and a pairing relationship is formed.

[0034] A second aspect of the present invention relates to a text-to-text method based on local-global attention, comprising:

[0035] Step 1: Transfer text data into a data storage device, wherein the text data includes translation text, dialogue text, article abstract, etc. Each text includes a source text and a target text, wherein the source text includes an operation text and an operation object text, wherein the operation text is at the beginning of the source text, and the target text is a label text, for example, the operation text is "English translated into Chinese:", the operation object text is an English text, and the target text is the Chinese translation corresponding to the English text.

[0036] Step 2: Preprocess the data, preprocess the data in the data storage, including discarding low-quality data, segmenting words, building a dictionary, one-hot encoding each word, converting the text data into matrix data, and dividing the matrix data into a training data set and a validation data set. The low-quality data includes text with a length of less than 20 and repeated text. The row vector of the matrix data corresponds to the one-hot encoding of the word.

[0037] Step 3: Construct a local-whole based generative model.

[0038] Step 4: training the model, using the data in a data storage device as the input of the model after step 2, using back propagation and Adam optimizer to train the model, and obtaining the model by minimizing the cross entropy loss function.

[0039] Step 5: Input the text data of interest into the trained prediction model after steps 1 and 2 to obtain the target text.

[0040] Furthermore, the generation model described in step 3 is composed of an input module, an output module, a local-global encoding module, a local-global decoding module, a linear module and a softmax module, and is configured to use the sequence generated by the data preprocessor to generate a specified type of text, and the process is as follows:

[0041] (3.1) The input module embeds the source text matrix generated by the data preprocessor into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the matrix to obtain a matrix that integrates the position information. , the size is .

[0042] (3.2) The local-global encoding module uses the matrix to generate the encoding matrix. Specifically,

[0043] (3.2a) Select multiple routes in Select the first several vectors from the matrix to form several matrices and regularize them separately. for A learnable route. Each row vector of In routing The score below is . Select the route with the highest score The vector matrix ,in ,if If you are selected, for No. Vectors. For matrices Do regularization.

[0044] (3.2b) Use sparse local self-attention mechanism on the routing matrix. A sparse local self-attention mechanism. Do sparse linear mapping and get the query, key, and value matrices respectively:

[0045] (1)

[0046] The parameter matrix They are all sparse matrices. Therefore, the local attention is:

[0047] (2)

[0048] in An integer whose absolute value is less than a positive number ,right Perform softmax operation to get the weight vector , so we get the vector of the above relationship :

[0049] (3)

[0050] Aggregate the vectors generated by all sparse local attention mechanisms into:

[0051] (4)

[0052] in yes The regularization of exist The numerical order is The following are set to 0, The previous softmax operation.

[0053] (3.2c) Make the residual of the output and input of the attention mechanism: , and then update the After regularization and single hidden layer perceptron, the residual is added to itself to further update , where the parameter matrix of the perceptron is a sparse matrix.

[0054] (3.2d) For updated Iterate steps (3.2a)--(3.2c) 12 times to get the encoding matrix ,

[0055] (3.3) The output module splices the start symbol encoding vector onto the target text matrix to form a new matrix, then embeds the new matrix into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the dense matrix to obtain a matrix that integrates the position information. , the size is When the model is used, the target text will be missing and the output of the model can be used instead of the target text.

[0056] (3.4) The local-global decoding block uses the encoding matrix and the output of the output module as input, and the output matrix is ​​represented. Specifically,

[0057] (3.4a) Select multiple routes in Select the first several vectors to form several matrices and regularize them separately.

[0058] (3.4b) Use the masked sparse self-attention mechanism for the routed matrix. Compared with the attention in (3.2b), Add a natural number limit, that is, each word cannot pay attention to the word after it.

[0059] (3.4c) The output and input of the masked sparse self-attention mechanism are made into residuals, and after the update, it is still recorded as ,

[0060] (3.4d) Select the route in (3.2d) using multiple routes and (3.4c) Select the first several vectors to form several matrices and regularize them respectively, among which the effect is and The routes on the , and the number of selected ones are also the same.

[0061] (3.4e) Use a sparse attention mechanism on the matrix selected by the route, The sparse attention mechanism is as follows. and Do sparse linear mapping and get the query, key, and value matrices respectively:

[0062] (5)

[0063] The parameter matrix They are all sparse matrices, the same as (3.2b), from which we can derive local attention and then the vector of contextual relationship. We aggregate the vectors of all sparse attention mechanisms into .

[0064] (3.4f) Make the residual of the output and input of the attention mechanism: , and then update the After regularization, the single hidden layer perceptron is used to make a residual with itself and further update , where the parameter matrix of the perceptron is a sparse matrix.

[0065] (3.4g) Iterate steps (1.4b)-(1.4f) 12 times and output the representation matrix.

[0066] (3.5) The linear module projects the representation matrix representation into the dictionary vector space and converts it into a word probability matrix through the softmax module.

[0067] Furthermore, the sparsity of the sparse feedforward layer and the sparse linear element is 90%, and the parameters are randomly initialized. During training, a pruning operation and an adding connection operation are performed every 1000 iterations. The criterion of the pruning operation is that the last 5% of the absolute values ​​of the parameters are reset to 0, and the adding connection operation adds connections to the unconnected neurons through the competitive Hebbian criterion, and the number of added connections is the number of parameters pruned by the operation. Specifically, the transpose of the parameter matrix is ​​multiplied by the output and then subtracted from the input to obtain a difference matrix, the second norm of the difference matrix is ​​taken, and then the second norm is derived with respect to the parameter matrix, and the parameters with zero parameter values ​​and the first absolute values ​​of the derivatives are re-randomly initialized, that is, the connection is added.

[0068] The working principle of the present invention is: multiple routers are used to extract several sub-texts from the original text; for each sub-text, a local attention mechanism is used to obtain the representation of each character in the text; then, the representations of the same characters in each sub-text are summarized to form a comprehensive representation containing contextual relationships; in this process, a sparse parameter matrix is ​​introduced to replace the traditional dense parameter matrix, which effectively improves the processing efficiency and reduces the computational complexity.

[0069] The innovation of the present invention is that the computational complexity is reduced to a linear level while acquiring long-distance information through the combination of routers and local attention.

[0070] The advantages of the present invention are: capturing long-distance information while reducing computational complexity and storage volume, and accelerating model training and reasoning time. BRIEF DESCRIPTION OF THE DRAWINGS

[0071] Figure 1 It is an overall framework diagram of the system and method of the present invention.

[0072] Figure 2 Flowchart for generating target text for the present invention.

[0073] Figure 3 This is a framework diagram of the generation module proposed in the present invention.

[0074] Figure 4 and Figure 5 This is the architecture diagram of the local-global self-attention mechanism proposed in the present invention.

[0075] Figure 6 Diagram of the sparse local self-attention sublayer architecture used in the present invention.

[0076] Figure 7 This is the architecture diagram of the sparse masked local self-attention layer used in the present invention.

[0077] Figure 8 This is a diagram of the sparse local attention layer architecture used in the present invention. DETAILED DESCRIPTION

[0078] The present invention will be further described in conjunction with the accompanying drawings.

[0079] Embodiment 1:

[0080] This embodiment provides a language translation system based on a local-global attention text-to-text system of the present invention, wherein the system is as follows: Figure 1 , including a data storage device, a data preprocessor and a model generator. The data storage device includes at least one translation database. Each data in the database consists of a text, and each text includes a source text and a target text. The source text consists of a task text and an object text. The object text is the text to be translated. An object text can only be in one language, such as English, German, Italian, etc. The task text is at the beginning of the source text and is used to specify the text of the object task. It has a fixed format: "translate" + object text language + "translate to" + target language + ":". For example, assuming that the object text is English and the target is Chinese, the task text is set to "translate English into Chinese:". The target text is the expert translation of the object text.

[0081] The data preprocessor is connected to the data storage device. The data preprocessor receives the input of the data storage device, generates a word vector and a text matrix after cleaning, and passes the text matrix to the model generator to generate the target text. The word vector is the one-hot encoding of the word in the data storage device, and the text matrix is ​​the matrix of the text under the one-hot encoding, and the row vector of the matrix corresponds to the one-hot encoding of the text in the text. The cleaning method is to delete the text whose length of the object text is less than 5, delete the repeated text, and delete the special symbols in the text, such as continuous non-text parts such as "+++++++++" or "-------".

[0082] The model generator is Figure 3 As shown, it is composed of an input module, an output module, a local-global encoding module, a local-global decoding module, a linear module, a softmax module, and a text printing module. The relationship between the modules and the internal structure of the modules are described as follows.

[0083] The input module consists of an input embedding block and a fused position block. The input embedding block embeds the source text matrix into the Euclidean space to obtain a dense matrix. The fused position block adds a position matrix to the dense matrix to obtain a matrix with position information.

[0084] The local-global encoding module encodes the output matrix of the input module into a compressed matrix. It is composed of a plurality of local-global encoding blocks stacked together, specifically: the input of the first local-global encoding block is the output of the input module, and the input of the remaining local-global encoding blocks is the output of the previous local-global encoding block. Among them, the local-global encoding block is composed of two normalization layers, a sparse local-global self-attention layer, two addition layers and a sparse feedforward layer, which are used to compress information. The relationship between each layer is as follows: the input of the local-global encoding block is converted into a representation matrix containing contextual relationships after passing through the normalization layer and the sparse local-global self-attention layer. The representation matrix and the input of the local-global encoding block are added in an addition layer to obtain a residual matrix. The residual matrix is ​​added to itself after passing through the output of another normalization layer and the sparse feedforward layer to obtain a compressed matrix.

[0085] The output module concatenates the start symbol and the target text matrix and converts them into a matrix with position information. It consists of a fusion position block and an output embedding block. The input embedding block concatenates the start symbol and the target text matrix and embeds them into the Euclidean space to obtain a dense matrix. The fusion position block adds the position matrix to the dense matrix to obtain a matrix with sequence information. If the target text matrix is ​​missing, the output of the softmax module in the model generator is used instead.

[0086] The local-global decoding module is composed of a plurality of local-global decoding blocks stacked together, and the output of the output module and the output of the local-global encoding module are fused into a representation matrix of the text. Specifically, the input of the first local-global decoding block is the output of the output module and the output of the local-global encoding block, and the input of the remaining local-global encoding blocks is the output of the previous local-global decoding block and the output of the local-global encoding module. Furthermore,

[0087] The local-global decoding block consists of three normalization layers, a masked sparse local-global self-attention layer, a sparse local-global attention layer, three addition layers, and a sparse feed-forward layer.

[0088] The input of the local-global decoding block passes through the normalization layer and the masked sparse local-global self-attention layer to become a representation matrix containing the previous context relationship. The input of the local-global decoding block and the representation matrix are added in an addition layer to obtain a residual matrix. The residual matrix and the output of the local-global encoding module pass through the second normalization layer and the sparse local-global attention layer to generate an output, and the output is added to the residual matrix in the second addition layer to obtain a decoding residual matrix. The output of the decoding residual matrix after passing through the third normalization layer and the feedforward layer is added to the decoding residual matrix in the third addition layer to obtain a representation matrix.

[0089] The linear module passes the output of the local-global decoding module to the softmax module to output the word vector matrix, and the text printing module converts the word vector matrix into text and prints it out.

[0090] The sparse local-global self-attention layer is as follows Figure 4 As shown, it is composed of multiple routing sublayers, multiple sparse local self-attention sublayers, an addition sublayer and a linear sublayer. Its workflow is as follows: the first routing sublayer selects multiple row vectors with the highest scores in the output matrix of the normalization layer to form a new matrix in order, and passes the new matrix to one of the sparse local self-attention sublayers. The remaining routing sublayers use the new matrix produced by the previous routing sublayer as input, regenerate a new matrix, and pass the regenerated new matrix to the corresponding sparse local self-attention sublayer. The addition sublayer adds up the matrices output by each sparse local self-attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and sends the added matrix to the linear sublayer to obtain a representation matrix containing contextual relationships. The number of routing sublayers is the same as the number of sparse local self-attention sublayers, and forms a pairing relationship. The routing sublayer is a learnable vector whose dimension is consistent with the number of columns of the matrix output by the normalization layer. It is configured to screen the row vectors of the matrix. Specifically, each row vector of the matrix is ​​scored by taking the inner product of the learnable vector and each row vector of the matrix, and some vectors with high scores are screened out to obtain a new matrix.

[0091] The sparse local self-attention sublayer is as follows Figure 6 As shown, it is composed of three sparse linear elements, a local attention element and a weighting element, and is configured to encode local contextual relationships. The specific process is: the three sparse linear elements use the output matrix of the paired routing sublayer to generate three matrices Q, K and V respectively, the local attention element uses Q and K to generate the local attention matrix, and the weighting element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing local contextual relationships. Among them,

[0092] The local attention element is configured to generate a local attention matrix, which is obtained by performing a softmax operation on each row of a sparse matrix, and the elements in the i-th row and j-th column of the sparse matrix are obtained by performing an inner product operation on the i-th row of Q and the j-th row of K, where the j-th row is no more than w rows away from the i-th row, and the remaining elements of the sparse matrix are set to negative infinity.

[0093] The masked sparse local-global self-attention layer is as follows Figure 7 As shown, it is composed of multiple routing sublayers, multiple masked sparse local self-attention sublayers, an addition sublayer and a linear sublayer. Its workflow is as follows: each of the routing sublayers screens out multiple row vectors with top scores in the output matrix of the normalization layer, and passes the multiple row vectors to the corresponding masked sparse local self-attention sublayer. The addition sublayer adds up the matrices output by each masked sparse local self-attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and sends the added matrix to the linear sublayer to obtain a representation matrix containing the previous text relationship. Among them, the number of the routing sublayers is the same as the number of the masked sparse local self-attention sublayers, and forms a pairing relationship.

[0094] The masked sparse local self-attention sublayer is composed of three sparse linear elements, a masked local attention element and a weighted element, and is configured to encode local previous context relationships. The specific process is: the three sparse linear elements use the output matrix of the paired routing sublayer to generate three matrices Q, K and V, the masked local attention element uses Q and K to generate a local attention matrix, and the weighted element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing the local previous context relationship.

[0095] The masked local attention element is configured to generate a local attention matrix, which is obtained by performing a softmax operation on each row of a sparse matrix, and the i-th row and j-th column element of the sparse matrix is ​​obtained by performing an inner product operation on the i-th row of Q and the j-th row of K, where j does not exceed i and is no more than w rows away from i, and the remaining elements of the sparse matrix are set to negative infinity.

[0096] The sparse local-global attention layer is as follows Figure 8As shown, it is composed of multiple routing sublayers, multiple sparse local attention sublayers, an addition sublayer and a linear sublayer. Its workflow is as follows: each of the routing sublayers selects multiple row vectors with the highest scores from the output matrix of the local-global encoding module after the normalization layer and the output matrix of the residual matrix after the normalization layer to form a new matrix A and a new matrix B, and passes the two new matrices to the corresponding sparse local attention sublayer. The addition sublayer adds up the matrices output by each sparse local attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and the linear sublayer uses the added matrices to obtain a representation matrix. Among them, the two new matrices have the same number of row vectors. The number of the routing sublayers is the same as the number of the sparse local attention sublayers, and a pairing relationship is formed.

[0097] The sparse local attention sublayer is composed of three sparse linear elements, one local attention element and one weighting element, and is configured to capture local previous context relationships. The specific process is: the one sparse linear element uses the output matrix of the paired routing sublayer to generate a Q matrix, the other two sparse linear elements use the output matrix of the paired routing sublayer to generate two matrices K and V respectively, the local attention element uses Q and K to generate a local attention matrix, and the weighting element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing the local previous context relationship.

[0098] The sparse feedforward layer and sparse linear element are feedforward layers and linear mappings that restrict parameter matrices, and the proportion of zero elements in the parameter matrix is ​​90%. The parameter matrix randomly initializes parameters, and during training, a pruning operation and an adding connection operation are performed every 1000 iterations. The criterion of the pruning operation is that the last 5% of the absolute values ​​of the parameters are reset to 0, and the adding connection operation adds connections to neurons without connections through the competitive Hebbian criterion, and the number of added connections is the number of parameters pruned by the operation. Specifically, the transpose of the parameter matrix is ​​multiplied by the output and then subtracted from the input to obtain a difference matrix, and the difference matrix is ​​taken as the second norm, and then the second norm is derived with respect to the parameter matrix. The parameters with zero parameter values ​​and the first absolute value of the derivative are reinitialized, that is, the connection is added.

[0099] The parameters in the model generator are obtained by training with back propagation and the Adam optimizer to minimize the cross entropy loss function.

[0100] The user prepares source text and inputs it into the data preprocessor. The data preprocessor uses the data input into the memory by the user to obtain a matrix of the text. The model generator uses the matrix to generate the target text.

[0101] Example 2

[0102] The present invention provides a language translation method using a local-global attention based text-to-text method of the present invention. Figure 2 A more detailed workflow of the language translation method is provided. The workflow includes the following steps: storing data, preprocessing data, building a generative model, generating target text, and training the generative model. Each step is described in detail below.

[0103] Step 1: Store data. Crawl translation text data from the website in an electronic data storage medium. Each text contains a source text and a target text. The source text consists of a task text and an object text. The object text is the text to be translated. An object text can only be in one language, such as English, German, Italian, etc. The task text is at the beginning of the source text and is used to specify the text of the object task. It has a fixed format: "translate" + object text language + "translate to" + target language + ":". For example, assuming the object text is English and the target is Chinese, the task text is set to "Translate English into Chinese:". The target text is the expert translation of the object text.

[0104] Step 2: Clean the text data, one-hot encode the words of the cleaned text data, and convert the text into a text matrix. The cleaning method is to delete the text with a length of less than 5 and delete the repeated text.

[0105] Step 3: Construct a generative model. The generative model is as follows: Figure 3 As shown in Figure 1, it consists of an input module, an output module, a local-global encoding module, a local-global decoding module, a linear module, and a softmax module. The process is as follows:

[0106] (3.1) The input module embeds the source text matrix generated by the data preprocessor into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the matrix to obtain a matrix that integrates the position information. , the size is .

[0107] (3.2) The local-global encoding module uses the matrix to generate the encoding matrix. Specifically,

[0108] (3.2a) Select multiple routes in Select the first several vectors from the matrix to form several matrices and regularize them separately. for A learnable route. Each row vector of In routing The score below is . Select the route with the highest score The vector matrix ,in ,if If you are selected, for No. Vectors. For matrices Do regularization.

[0109] (3.2b) Use a sparse local self-attention mechanism on the routing matrix, such as Figure 5 As shown. A sparse local self-attention mechanism such as Figure 6 As shown. Do sparse linear mapping and get the query, key, and value matrices respectively:

[0110] (1)

[0111] The parameter matrix They are all sparse matrices. Therefore, the local attention is:

[0112] (2)

[0113] in An integer whose absolute value is less than a positive number ,right Perform softmax operation to get the weight vector , so we get the vector of the above relationship :

[0114] (3)

[0115] Aggregate the vectors generated by all sparse local attention mechanisms into:

[0116] (4)

[0117] in yes The regularization of exist The numerical order is The following are set to 0, The previous softmax operation.

[0118] (3.2c) Make the residual of the output and input of the attention mechanism: , and then update the After regularization and a single hidden layer perceptron, the residual is added to itself to further update X, where the parameter matrix of the perceptron is a sparse matrix.

[0119] (3.2d) For updated Iterate steps (3.2a)--(11.2c) 12 times to get the encoding matrix ,

[0120] (3.3) The output module splices the start symbol encoding vector onto the target text matrix to form a new matrix, then embeds the new matrix into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the dense matrix to obtain a matrix that integrates the position information. , the size is When the model is used, the target text will be missing and the output of the model can be used instead of the target text.

[0121] (3.4) The local-global decoding block uses the encoding matrix and the output of the output module as input, and the output matrix is ​​represented. Specifically,

[0122] (3.4a) Select multiple routes in Select the first several vectors to form several matrices and regularize them separately.

[0123] (3.4b) Apply masked sparse self-attention mechanism to the routing matrix, such as Figure 7 As shown. The attention is different from the attention in (11.2b). Add a natural number limit, that is, each word cannot pay attention to the word after it.

[0124] (3.4c) The output and input of the masked sparse self-attention mechanism are made into residuals, and after the update, it is still recorded as .

[0125] (3.4d) Select the route in (3.2d) using multiple routes and (3.4c) Select the first several vectors to form several matrices and regularize them respectively, among which the effect is and The routes on the , and the number of selected ones are also the same.

[0126] (3.4e) The sparse attention mechanism is applied to the matrix selected by the route. The i-th sparse attention mechanism is as follows Figure 8 As shown. and Do sparse linear mapping and get the query, key, and value matrices respectively:

[0127] (5)

[0128] The parameter matrix They are all sparse matrices, the same as (3.2b), from which we can derive local attention and then the vector of contextual relationship. We aggregate the vectors of all sparse attention mechanisms into .

[0129] (3.4f) Make the residual of the output and input of the attention mechanism: , and then update the After regularization, the single hidden layer perceptron is used to make a residual with itself and further update , where the parameter matrix of the perceptron is a sparse matrix.

[0130] (3.4g) Iterate steps (3.4b)-(3.4f) 12 times and output the representation matrix.

[0131] (3.5) The linear module projects the representation matrix representation into the dictionary vector space and converts it into a word vector matrix through the softmax module.

[0132] Step 4: Training process: The data in a data storage device is used as the input of the model through step 2, and the model is trained using back propagation and Adam optimizer to obtain the model by minimizing the cross entropy loss function.

[0133] Step 5: The user inputs the source text into the data preprocessor, obtains the word vector matrix of the target text through steps 2 and 3, and converts the matrix into the target text, wherein the characters corresponding to the word vectors of the matrix satisfy the word vectors of the characters and are closest to the word vectors.

[0134] Implementation Case 3

[0135] The present invention provides a summary generation method using a text-to-text method based on local-global attention of the present invention. Figure 2 A more detailed workflow of the summary generation method is provided. The workflow includes the following steps: storing data, preprocessing data, building a generative model, generating target text, and training the generative model. Each step is described in detail below.

[0136] Step 1: Store data. Crawl text data with summaries from the website and store them in an electronic data storage medium. Each text contains a source text and a target text. The source text consists of a task text and an object text, and the object text is the text from which the summary is extracted. The task text is at the beginning of the source text and is used to specify the text of the object task. It has a fixed format: "Summary:" The target text is the actual summary part of the object text.

[0137] Step 2: Clean the text data, one-hot encode the words of the cleaned text data, and convert the text into a text matrix. The cleaning method is to delete the text with a length of less than 5 and delete the repeated text.

[0138] Step 3: Construct a generative model. The generative model is as follows: Figure 3 As shown in Figure 1, it consists of an input module, an output module, a local-global encoding module, a local-global decoding module, a linear module, and a softmax module. The process is as follows:

[0139] (3.1) The input module embeds the source text matrix generated by the data preprocessor into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the matrix to obtain a matrix that integrates the position information. , the size is .

[0140] (3.2) The local-global encoding module uses the matrix to generate the encoding matrix. Specifically,

[0141] (3.2a) Select multiple routes in Select the first several vectors from the matrix to form several matrices and regularize them separately. for A learnable route. Each row vector of In routing The score below is . Select the route with the highest score The vector matrix ,in ,if If you are selected, for No. Vectors. For matrices Do regularization.

[0142] (3.2b) Use a sparse local self-attention mechanism on the routing matrix, such as Figure 5 As shown. A sparse local self-attention mechanism such as Figure 6 As shown. Do sparse linear mapping and get the query, key, and value matrices respectively:

[0143] (1)

[0144] The parameter matrix They are all sparse matrices. Therefore, the local attention is:

[0145] (2)

[0146] in An integer whose absolute value is less than a positive number ,right Perform softmax operation to get the weight vector , so we get the vector of the above relationship :

[0147] (3)

[0148] Aggregate the vectors generated by all sparse local attention mechanisms into:

[0149] (4)

[0150] in yes The regularization of exist The numerical order is The following are set to 0, The previous softmax operation.

[0151] (3.2c) Make the residual of the output and input of the attention mechanism: , and then update the After regularization and a single hidden layer perceptron, the residual is added to itself to further update X, where the parameter matrix of the perceptron is a sparse matrix.

[0152] (3.2d) For updated Iterate steps (3.2a)--(11.2c) 12 times to get the encoding matrix ,

[0153] (3.3) The output module splices the start symbol encoding vector onto the target text matrix to form a new matrix, then embeds the new matrix into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the dense matrix to obtain a matrix that integrates the position information. , the size is When the model is used, the target text will be missing and the output of the model can be used instead of the target text.

[0154] (3.4) The local-global decoding block uses the encoding matrix and the output of the output module as input, and the output matrix is ​​represented. Specifically,

[0155] (3.4a) Select multiple routes in Select the first several vectors to form several matrices and regularize them separately.

[0156] (3.4b) Apply masked sparse self-attention mechanism to the routing matrix, such as Figure 7 As shown. The attention is different from the attention in (11.2b). Add a natural number limit, that is, each word cannot pay attention to the word after it.

[0157] (3.4c) The output and input of the masked sparse self-attention mechanism are made into residuals, and after the update, it is still recorded as .

[0158] (3.4d) Select the route in (3.2d) using multiple routes and (3.4c) Select the first several vectors to form several matrices and regularize them respectively, among which the effect is and The routes on the , and the number of selected ones are also the same.

[0159] (3.4e) The sparse attention mechanism is applied to the matrix selected by the route. The i-th sparse attention mechanism is as follows Figure 8 As shown. and Do sparse linear mapping and get the query, key, and value matrices respectively:

[0160] (5)

[0161] The parameter matrix They are all sparse matrices, the same as (3.2b), from which we can derive local attention and then the vector of contextual relationship. We aggregate the vectors of all sparse attention mechanisms into .

[0162] (3.4f) Make the residual of the output and input of the attention mechanism: , and then update the After regularization, the single hidden layer perceptron is used to make a residual with itself and further update , where the parameter matrix of the perceptron is a sparse matrix.

[0163] (3.4g) Iterate steps (3.4b)-(3.4f) 12 times and output the representation matrix.

[0164] (3.5) The linear module projects the representation matrix representation into the dictionary vector space and converts it into a word vector matrix through the softmax module.

[0165] Step 4: Training process: The data in a data storage device is used as the input of the model through step 2, and the model is trained using back propagation and Adam optimizer to obtain the model by minimizing the cross entropy loss function.

[0166] Step 5: The user inputs the source text into the data preprocessor, obtains the word vector matrix of the target text through steps 2 and 3, and converts the matrix into summary text, in which the words corresponding to the word vectors of the matrix satisfy the word vectors of the words that are closest to the word vectors.

[0167] Example 4

[0168] The present invention provides a method for integrating translation, summarization and question answering by applying a text-to-text method based on local-global attention of the present invention. Figure 2 A more detailed workflow of the summary generation method is provided. The workflow includes the following steps: storing data, preprocessing data, building a generative model, generating target text, and training the generative model. Each step is described in detail below.

[0169] Step 1: Store data. Crawl translation, summary and question-and-answer text data from the website and store them in an electronic data storage medium. Each text contains a source text and a target text. The source text consists of a task text and an object text. The object text is the translated text in the translation data, the text from which the summary is extracted in the summary, and the reading text in the question-and-answer. The task text is the text that marks the object text task. For the translation text, the task text is fixed as "will" + the language corresponding to the object text + "translate to" + the target language + ":". For the summary text, the task text is "summary:". For the question-and-answer text, the task text is the question text + ":". The target text is the task result of the object text.

[0170] Step 2: Clean the text data, one-hot encode the words of the cleaned text data, and convert the text into a text matrix. The cleaning method is to delete the text with a length of less than 5 and delete the repeated text.

[0171] Step 3: Construct a generative model. The generative model is as follows: Figure 3 As shown in Figure 1, it consists of an input module, an output module, a local-global encoding module, a local-global decoding module, a linear module, and a softmax module. The process is as follows:

[0172] (3.1) The input module embeds the source text matrix generated by the data preprocessor into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the matrix to obtain a matrix that integrates the position information. , the size is .

[0173] (3.2) The local-global encoding module uses the matrix to generate the encoding matrix. Specifically,

[0174] (3.2a) Select multiple routes in Select the first several vectors from the matrix to form several matrices and regularize them separately. for A learnable route. Each row vector of In routing The score below is . Select the route with the highest score The vector matrix ,in ,if If you are selected, for No. Vectors. For matrices Do regularization.

[0175] (3.2b) Use a sparse local self-attention mechanism on the routing matrix, such as Figure 5 As shown. A sparse local self-attention mechanism such as Figure 6 As shown. Do sparse linear mapping and get the query, key, and value matrices respectively:

[0176] (1)

[0177] The parameter matrix They are all sparse matrices. Therefore, the local attention is:

[0178] (2)

[0179] in An integer whose absolute value is less than a positive number ,right Perform softmax operation to get the weight vector , so we get the vector of the above relationship :

[0180] (3)

[0181] Aggregate the vectors generated by all sparse local attention mechanisms into:

[0182] (4)

[0183] in yes The regularization of exist The numerical order is The following are set to 0, The previous softmax operation.

[0184] (3.2c) Make the residual of the output and input of the attention mechanism: , and then update the After regularization and a single hidden layer perceptron, the residual is added to itself to further update X, where the parameter matrix of the perceptron is a sparse matrix.

[0185] (3.2d) For updated Iterate steps (3.2a)--(11.2c) 12 times to get the encoding matrix ,

[0186] (3.3) The output module splices the start symbol encoding vector onto the target text matrix to form a new matrix, then embeds the new matrix into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the dense matrix to obtain a matrix that integrates the position information. , the size is When the model is used, the target text will be missing and the output of the model can be used instead of the target text.

[0187] (3.4) The local-global decoding block uses the encoding matrix and the output of the output module as input, and the output matrix is ​​represented. Specifically,

[0188] (3.4a) Select multiple routes in Select the first several vectors to form several matrices and regularize them separately.

[0189] (3.4b) Apply masked sparse self-attention mechanism to the routing matrix, such as Figure 7 As shown. The attention is different from the attention in (11.2b). Add a natural number limit, that is, each word cannot pay attention to the word after it.

[0190] (3.4c) The output and input of the masked sparse self-attention mechanism are made into residuals, and after the update, it is still recorded as .

[0191] (3.4d) Select the route in (3.2d) using multiple routes and (3.4c) Select the first several vectors to form several matrices and regularize them respectively, among which the effect is and The routes on the , and the number of selected ones are also the same.

[0192] (3.4e) The sparse attention mechanism is applied to the matrix selected by the route. The i-th sparse attention mechanism is as follows Figure 8 As shown. and Do sparse linear mapping and get the query, key, and value matrices respectively:

[0193] (5)

[0194] The parameter matrix They are all sparse matrices, the same as (3.2b), from which we can derive local attention and then the vector of contextual relationship. We aggregate the vectors of all sparse attention mechanisms into .

[0195] (3.4f) Make the residual of the output and input of the attention mechanism: , and then update the After regularization, the single hidden layer perceptron is used to make a residual with itself and further update , where the parameter matrix of the perceptron is a sparse matrix.

[0196] (3.4g) Iterate steps (3.4b)-(3.4f) 12 times and output the representation matrix.

[0197] (3.5) The linear module projects the representation matrix representation into the dictionary vector space and converts it into a word vector matrix through the softmax module.

[0198] Step 4: Training process: The data in a data storage device is used as the input of the model through step 2, and the model is trained using back propagation and Adam optimizer to obtain the model by minimizing the cross entropy loss function.

[0199] Step 5: The user inputs the source text into the data preprocessor, obtains the word vector matrix of the target text through steps 2 and 3, and converts the matrix into the target text, wherein the characters corresponding to the word vectors of the matrix satisfy the word vectors of the characters and are closest to the word vectors.

[0200] The contents described in the embodiments of this specification are merely an enumeration of the implementation forms of the inventive concept. The protection scope of the present invention should not be regarded as limited to the specific forms described in the embodiments. The protection scope of the present invention also extends to equivalent technical means that can be conceived by those skilled in the art based on the inventive concept.

Claims

1. A text-to-text system based on local-global attention, It is characterized in that The invention comprises a data storage device, a data preprocessor and a model generator, wherein the data preprocessor processes the data stored in the data storage device into a word vector sequence, and passes the sequence to the model generator to generate text, wherein the data storage device stores text information, and the text information includes multiple types of text information, including texts in translation, classification, regression and summary, each text includes a source text and a target text, the source text includes a task text and an object text, wherein the task text is used at the beginning of the source text to indicate the task of the object text, and the target text is a label text of the object text task; A data preprocessor is configured to clean the data stored in the data storage device and generate a text matrix for each cleaned text, wherein each row of the text matrix is ​​a one-hot encoding of a corresponding character in the text; The model generator includes an input module, an output module, a local-global encoding module, a local-global decoding module, a linear module and a softmax module, and is configured to use the sequence generated by the data preprocessor to generate a specified type of text, and the process is as follows: the input module converts the source text matrix generated by the data preprocessor into a matrix with position information, the local-global encoding module uses the matrix with position information to generate a compression matrix, the local-global decoding module uses the compression matrix and the output of the output module as input, and outputs a matrix representation, the linear module projects the matrix representation into a word vector space, and converts it into a word probability matrix through the softmax module; The input module is composed of an input embedding block and a fused position block, wherein the input embedding block embeds the source text matrix into the Euclidean space to obtain a dense matrix, and the fused position block adds a position matrix to the dense matrix to obtain a matrix with position information, and The local-global coding module is composed of a plurality of local-global coding blocks stacked together, and its information flow is: the input of the first local-global coding block is the output of the input module, and the input of the remaining local-global coding blocks is the output of the previous local-global coding block, wherein, The local-global encoding block consists of two normalization layers, a sparse local-global self-attention layer, two addition layers and a sparse feed-forward layer to compress information, where The input of the local-global encoding block is transformed into a representation matrix containing contextual relationships after passing through a normalization layer and the sparse local-global self-attention layer, and The representation matrix and the input of the local-global coding block are added in an addition layer to obtain a residual matrix, and The residual matrix is ​​added to itself after passing through another normalization layer and the output of the sparse feedforward layer to obtain a compressed matrix, and The output module concatenates the start symbol and the target text matrix and converts them into a matrix with position information, which consists of a fused position block and an output embedding block. The input embedding block concatenates the start symbol and the target text matrix and embeds them into the Euclidean space to obtain a dense matrix. The fused position block adds the position matrix to the dense matrix to obtain a matrix with order information. If the target text matrix is ​​missing, the output of the model generator is used instead. The local-global decoding module is composed of a plurality of local-global decoding blocks stacked together, and its information flow is as follows: the input of the first local-global decoding block is the output of the output module and the output of the local-global encoding block, and the input of the remaining local-global encoding blocks is the output of the previous local-global decoding block and the output of the local-global encoding module, wherein: The local-global decoding block consists of three normalization layers, a masked sparse local-global self-attention layer, a sparse local-global attention layer, three addition layers, and a sparse feed-forward layer, where The input of the local-global decoding block is transformed into a representation matrix containing the previous context relationship after passing through the normalization layer and the masked sparse local-global self-attention layer. The input of the local-global decoding block and the representation matrix are added in an addition layer to obtain a residual matrix, and The residual matrix and the output of the local-global encoding module are successively passed through the second normalization layer and the sparse local-global attention layer to generate an output, and the output is added to the residual matrix in the second addition layer to obtain a decoding residual matrix, and The output of the decoding residual matrix after passing through the third normalization layer and the feedforward layer is added to the decoding residual matrix in the third addition layer to obtain a representation matrix; The sparse feedforward layer is a 2-layer perceptron, the number of output neurons is the same as the number of output neurons, the number of neurons in the hidden layer is one-fourth of the number of input neurons, the proportion of 0 in each layer parameter matrix is ​​90%, and the parameters are randomly initialized. During training, a pruning operation and an adding connection operation are performed every 1000 iterations. The criterion of the pruning operation is that the last 5% of the absolute values ​​of the parameters are reset to 0. The adding connection operation adds connections to neurons without connections through the competitive Hebbian criterion, and the number of added is the number of parameters pruned by the operation. Specifically, the transpose of the parameter matrix is ​​multiplied by the output and the difference is obtained from the input to obtain a difference matrix, the second norm is performed on the difference matrix, and then the second norm is derived with respect to the parameter matrix, and the parameters with zero parameter values ​​and the first absolute values ​​of the derivatives are reinitialized, that is, the connection is added; The sparse local-global self-attention layer is composed of multiple routing sublayers, multiple sparse local self-attention sublayers, an addition sublayer and a linear sublayer. Its working process is as follows: each of the routing sublayers selects multiple row vectors with top scores in the output matrix of the normalization layer, and passes the multiple row vectors to the corresponding sparse local self-attention sublayer. The addition sublayer aggregates the vectors corresponding to the text at the same position output by each sparse local self-attention sublayer, and sends the aggregated matrix to the linear sublayer to obtain a representation matrix containing the contextual relationship, wherein, The number of the routing sublayers is the same as the number of the sparse local self-attention sublayers, and they form a paired relationship, and The routing sublayer is a learnable vector whose dimension is consistent with the number of columns of the matrix output by the normalization layer, and is configured to screen the row vectors of the matrix. Specifically, each row vector of the matrix is ​​scored by performing an inner product between the learnable vector and each row vector of the matrix, and some vectors with high scores are screened out to obtain a new matrix, and The sparse local self-attention sublayer is composed of three sparse linear elements, a local attention element and a weighting element, and is configured to encode local contextual relationships. The specific process is: the three sparse linear elements use the output matrix of the paired routing sublayer to generate three matrices Q, K and V respectively, the local attention element uses Q and K to generate a local attention matrix, and the weighting element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing local contextual relationships, wherein, The local attention element is configured to generate a local attention matrix, wherein the local attention matrix is ​​obtained by performing a softmax operation on each row of a sparse matrix, and the element in the i-th row and j-th column of the sparse matrix is ​​obtained by performing an inner product operation on the i-th row of Q and the j-th row of K, wherein the j-th row is no more than w rows away from the i-th row, and the remaining elements of the sparse matrix are set to negative infinity; The multiple routes include a trivial route, that is, selecting all row vectors in the input matrix; The sparse linear element is a single-layer perceptron, and the content of zero elements in the parameter matrix is ​​90%. The parameters are randomly initialized, and during training, a pruning operation and an adding connection operation are performed every 1000 iterations. The criterion of the pruning operation is that the last 5% of the absolute values ​​of the parameters are reset to 0. The adding connection operation adds connections to neurons without connections through the competitive Hebbian criterion, and the number of added connections is the number of parameters pruned by the operation. Specifically, the transpose of the parameter matrix is ​​multiplied by the output and then the difference is obtained from the input to obtain a difference matrix, the second norm is performed on the difference matrix, and then the second norm is derived with respect to the parameter matrix, and the parameters with zero parameter values ​​and the first absolute values ​​of the derivatives are reinitialized, that is, the connection is added; The sparse local-global self-attention layer is composed of multiple routing sublayers, multiple sparse local self-attention sublayers, an addition sublayer and a linear sublayer, and its workflow is as follows: the first routing sublayer selects multiple row vectors with top scores in the output matrix of the normalization layer to form a new matrix in order, and passes the new matrix to one of the sparse local self-attention sublayers. The remaining routing sublayers use the new matrix produced by the previous routing sublayer as input to regenerate a new matrix, and pass the regenerated new matrix to the corresponding sparse local self-attention sublayer. The addition sublayer aggregates the matrices output by each sparse local self-attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and sends the aggregated matrix to the linear sublayer to obtain a representation matrix containing contextual relationships, wherein, The number of the routing sublayers is the same as the number of the sparse local self-attention sublayers, and they form a paired relationship; The masked sparse local-global self-attention layer is composed of multiple routing sublayers, multiple masked sparse local self-attention sublayers, an addition sublayer and a linear sublayer. The workflow is as follows: each of the routing sublayers selects multiple row vectors with top scores in the output matrix of the normalization layer, and passes the multiple row vectors to the corresponding masked sparse local self-attention sublayer. The addition sublayer adds up the matrices output by each masked sparse local self-attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and sends the added matrix to the linear sublayer to obtain a representation matrix containing the previous text relationship, wherein, The number of the routing sublayers is the same as the number of the masked sparse local self-attention sublayers, and they form a paired relationship, and The masked sparse local self-attention sublayer is composed of three sparse linear elements, a masked local attention element and a weighting element, and is configured to encode local previous context relations. The specific process is: the three sparse linear elements use the output matrix of the paired routing sublayer to generate three matrices Q, K and V, the masked local attention element uses Q and K to generate a local attention matrix, and the weighting element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing the local previous context relation, where The masked local attention element is configured to generate a local attention matrix, wherein the local attention matrix is ​​obtained by performing a softmax operation on each row of a sparse matrix, and the i-th row and j-th column element of the sparse matrix is ​​obtained by performing an inner product operation on the i-th row of Q and the j-th row of K, where j does not exceed i and is no more than w rows away from i, and the remaining elements of the sparse matrix are set to negative infinity; Alternatively, the masked sparse local-global self-attention layer is composed of multiple routing sublayers, multiple masked sparse local self-attention sublayers, an addition sublayer and a linear sublayer, and its workflow is as follows: the first routing sublayer selects multiple row vectors with top scores in the output matrix of the normalization layer to form a new matrix in sequence, and passes the new matrix to one of the masked sparse local self-attention sublayers, the remaining routing sublayers use the new matrix produced by the previous routing sublayer as input, regenerate a new matrix, and pass the regenerated new matrix to the corresponding masked sparse local self-attention sublayer, the addition sublayer adds up the matrices output by each masked sparse local self-attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and sends the added matrix to the linear sublayer to obtain a representation matrix containing contextual relationships, wherein, The number of the routing sublayers is the same as the number of the masked sparse local self-attention sublayers, and they form a paired relationship; Alternatively, the sparse local-global attention layer is composed of multiple routing sublayers, multiple sparse local attention sublayers, an addition sublayer and a linear sublayer, and its workflow is as follows: each of the routing sublayers selects multiple row vectors with the highest scores from the output matrix of the local-global encoding module after the normalization layer and the output matrix of the residual matrix after the normalization layer to form a new matrix A and a new matrix B, and passes the two new matrices to the corresponding sparse local attention sublayer, the addition sublayer adds up the matrices output by each sparse local attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and the linear sublayer uses the added matrices to obtain a representation matrix, wherein, The two new matrices have the same number of row vectors, and The number of the routing sublayers is the same as the number of the sparse local attention sublayers, and they form a paired relationship, and The sparse local attention sublayer is composed of three sparse linear elements, one local attention element and one weighting element, and is configured to capture local previous context relations. The specific process is as follows: one sparse linear element uses the output matrix of the paired routing sublayer to generate a Q matrix, and the other two sparse linear elements use the output matrix of the paired routing sublayer to generate two matrices K and V respectively. The local attention element uses Q and K to generate a local attention matrix, and the weighting element uses the local attention matrix and the V matrix to perform matrix product to obtain a representation matrix containing the local previous context relations; Alternatively, the sparse local-global attention layer is composed of multiple routing sublayers, multiple sparse local attention sublayers, an addition sublayer and a linear sublayer, and its workflow is as follows: the first routing sublayer selects multiple row vectors with the highest scores from the output matrix of the local-global encoding module after the normalization layer and the output matrix of the residual matrix after the normalization layer to form a new matrix A and a new matrix B, and passes the two new matrices to the corresponding sparse local attention sublayer, and the remaining routing sublayers use the output of the previous routing sublayer as input, and pass the output new matrix to the corresponding sparse local attention sublayer, and the addition sublayer adds up the matrices output by each sparse local attention sublayer according to the principle of adding vectors corresponding to the text at the same position, and the linear sublayer uses the added matrix to obtain a representation matrix, wherein, The two new matrices have different numbers of row vectors, and The number of the routing sublayers is the same as the number of the sparse local attention sublayers, and they form a paired relationship.

2. A text-to-text approach based on local-global attention, include: Step 1: Transfer text data into a data storage device, wherein the text data includes translation text, dialogue text, and article abstract, wherein: Each text contains a source text and a target text, and the source text begins with the category it belongs to; Step 2: Preprocess the data. Preprocess the data in the data storage, including discarding low-quality data, segmenting words, building a dictionary, uniquely encoding each word, converting text data into matrix data, and dividing the matrix data into a training data set and a validation data set. The low-quality data includes texts with a length of less than 20 characters, repeated texts, and The row vectors of the matrix data correspond to the one-hot encoding of the words; Step 3: Construct a local-global generative model; Step 4: training the model, taking the data in a data storage device as the input of the model after step 2, using back propagation and Adam optimizer to train the model, and obtaining the model by minimizing the cross entropy loss function; Step 5: Input the text data of interest into the trained prediction model after steps 1 and 2 to obtain the target text; The generation model described in step 3 consists of an input module, an output module, a local-global encoding module, a local-global decoding module, a linear module and a softmax module, and is configured to use the sequence generated by the data preprocessor to generate a specified type of text. The process is as follows: (3.1) The input module embeds the source text matrix generated by the data preprocessor into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the matrix to obtain a matrix that integrates the position information , the size is ; (3.2) The local-global encoding module uses the matrix to generate the encoding matrix. Specifically, (3.2a) Select multiple routes in Select the first several vectors from the matrix to form several matrices and regularize them separately. for A learnable route, Each row vector of In routing The score below is Select the route with the highest score The vector matrix ,in ,if If you are selected, for No. vector, matrix Do regularization; (3.2b) Use sparse local self-attention mechanism on the matrix selected by routing, A sparse local self-attention mechanism Do sparse linear mapping and get the query, key, and value matrices respectively: , (1) The parameter matrix They are all sparse matrices, from which the local attention is: (2) in An integer whose absolute value is less than a positive number ,right Perform softmax operation to get the weight vector , so we get the vector of the above relationship : (3) Aggregate the vectors generated by all sparse local attention mechanisms into: (4) in yes The regularization of exist The numerical order is The following are set to 0, The previous softmax operation; (3.2c) Make the residual of the output and input of the attention mechanism: , and then update the After regularization and single hidden layer perceptron, the residual is added to itself and updated , where the parameter matrix of the perceptron is a sparse matrix; (3.2d) For updated Iterate steps (3.2a)--(3.2c) 12 times to get the encoding matrix ; (3.3) The output module splices the start symbol encoding vector onto the target text matrix to form a new matrix, then embeds the new matrix into the Euclidean space to obtain a dense matrix, and adds the position encoding matrix to the dense matrix to obtain a matrix that integrates the position information. , the size is , the target text will be missing when using the model, and the output of the model will be used instead of the target text; (3.4) The local-global decoding block uses the encoding matrix and the output of the output module as input, and the output matrix represents, specifically, (3.4a) Select multiple routes in Select the first several vectors to form several matrices and regularize them respectively; (3.4b) A masked sparse self-attention mechanism is used for the routed matrix, where the attention is different from the attention in (3.2b) and requires is a natural number, that is, each word cannot pay attention to the word after it; (3.4c) The output and input of the masked sparse self-attention mechanism are made into residuals, and after the update, it is still recorded as ; (3.4d) Select the route in (3.2d) using multiple routes and (3.4c) Select the first several vectors to form several matrices and regularize them respectively, among which the effect is and The routes on the , and the number of selected ones are also the same; (3.4e) Use a sparse attention mechanism on the matrix selected by the route, The sparse attention mechanism is as follows: and Do sparse linear mapping and get the query, key, and value matrices respectively: (5) The parameter matrix They are all sparse matrices, the same as (3.2b), from which we can derive local attention and then the vector of contextual relationship. We aggregate the vectors of all sparse attention mechanisms into ; (3.4f) Make the residual of the output and input of the attention mechanism: , and then update the After regularization, the single hidden layer perceptron is used to make a residual with itself and update , where the parameter matrix of the perceptron is a sparse matrix; (3.4g) Iterate steps (3.4b)-(3.4f) 12 times, and output the representation matrix; (3.5) The linear module projects the representation matrix representation into the dictionary vector space and converts it into a word vector matrix through the softmax module.