Data processing method and device, computer equipment, readable storage medium and program product
By calculating adaptive thresholds and dimensionality reduction to process attention score sequences, and filtering value vectors with higher correlations, the problem of poor target word vector accuracy and high computational overhead in traditional technology is solved, and more efficient natural language processing is achieved.
Patent Information
- Application Number
- CN202410705037.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-31
- Publication Date
- 2024-09-17
AI Technical Summary
In traditional technology, the word vectors corresponding to all words in the input text are weighted and summed by attention scores, resulting in poor accuracy of the target word vector and increasing the computational overhead of the encoding process.
By calculating the adaptive threshold of each initial word vector, the attention score sequence is dimensionally reduced, and the value vector sequence with higher correlation is screened out, and a weighted sum is performed based on these sequences to obtain the target word vector.
It improves the correlation accuracy of target word vectors, reduces computational overhead, and enhances the influence analysis capability of the attention mechanism.
Smart Images

Figure CN118657145A
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a data processing method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Art
[0002] With the development of artificial intelligence technology, the Transformer (a model architecture based on the attention mechanism) architecture has emerged, and the self-attention mechanism has been applied to various natural language processing tasks. By introducing the attention mechanism, the neural network can automatically learn and selectively focus on important information in the input language text.
[0003] In traditional technology, the input text is vectorized to obtain the word vector of each word. For the word vector of each word in the input text, an attention score matrix containing the attention score of each word vector is calculated. Then, the word vector at each position is multiplied by the corresponding attention score and then summed to obtain the target word vector.
[0004] However, in the current traditional technology, the word vectors corresponding to all words in the input text are weightedly summed up by the attention scores, resulting in poor accuracy of the target word vectors. In addition, the word vectors corresponding to all words are multiplied by their corresponding attention scores and then weighted summed up, which increases the computational overhead of the encoding process. Summary of the invention
[0005] Based on this, it is necessary to provide a data processing method, apparatus, computer device, computer-readable storage medium and computer program product to address the above technical issues.
[0006] In a first aspect, the present application provides a data processing method, comprising:
[0007] Get the first value vector sequence and the first attention score sequence corresponding to the initial word vector;
[0008] For each of the initial word vectors, calculating an adaptive threshold corresponding to the initial word vector based on the first attention score sequence;
[0009] Performing dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence;
[0010] Based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors, a target word vector corresponding to each of the initial word vectors is obtained.
[0011] In one embodiment, for each of the initial word vectors, calculating the adaptive threshold corresponding to the initial word vector based on the first attention score sequence includes:
[0012] According to the first attention score sequence corresponding to each of the initial word vectors, calculate and obtain the attention variance corresponding to the initial word vector;
[0013] Based on the attention variance, the sequence dimension of the first attention score sequence, and an adaptive calculation formula based on the attention score variance, an adaptive threshold corresponding to the initial word vector is determined.
[0014] In one embodiment, the performing dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence, comprises:
[0015] For each of the first attention score sequences corresponding to the initial word vectors, sorting the first attention scores in the first attention score sequence to obtain a sorting result;
[0016] Accumulate and calculate the first attention scores according to the sorting result to obtain an initial cumulative sum result, and determine a target cumulative sum result according to the adaptive threshold and the initial cumulative sum result;
[0017] Filtering the first attention scores in the first attention score sequence according to the target cumulative sum result to obtain a second attention score sequence;
[0018] The first value vector sequence is screened based on the second attention score sequence to obtain a second value vector sequence.
[0019] In one embodiment, the first attention scores are cumulatively calculated according to the sorting results to obtain a plurality of initial cumulative sum results, and a target cumulative sum result is determined from the initial cumulative sum results according to the adaptive threshold, including:
[0020] Accumulate and calculate the first attention scores in the first attention score sequence according to the sorting result to obtain an initial cumulative sum result;
[0021] In the case where the initial cumulative sum result is greater than the adaptive threshold, determining each of the initial cumulative sum results as a target cumulative sum result;
[0022] When the initial cumulative sum result is less than or equal to the adaptive threshold, continue to execute the step of accumulating the attention scores in the first attention score sequence according to the sorting result to obtain the initial cumulative sum result.
[0023] In one embodiment, the step of screening the first attention scores in the first attention score sequence according to the target cumulative sum result to obtain the second attention score sequence includes:
[0024] Determining an initial second attention score in the first attention score sequence according to the target cumulative sum result;
[0025] The initial second attention score that is ranked last in the first attention score sequence and other first attention scores are eliminated to obtain a second attention score sequence.
[0026] In one embodiment, the filtering of the first value vector sequence based on the second attention score sequence to obtain the second value vector sequence includes:
[0027] Determine a second value vector in the first value vector sequence based on a correspondence between attention scores and value vectors and each second attention score in the second attention score sequence;
[0028] A second value vector sequence is obtained according to each of the second value vectors.
[0029] In one embodiment, the step of obtaining a target word vector corresponding to each of the initial word vectors by weighted summing the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors comprises:
[0030] Normalizing the second attention score sequence corresponding to each of the initial word vectors to obtain a target attention score sequence;
[0031] The target attention score sequence and the second value vector sequence are weightedly summed to obtain a target word vector corresponding to each of the initial word vectors.
[0032] In a second aspect, the present application further provides a data processing device, comprising:
[0033] An acquisition module, used to acquire a first value vector sequence and a first attention score sequence corresponding to the initial word vector;
[0034] A calculation module, configured to calculate, for each of the initial word vectors, an adaptive threshold corresponding to the initial word vector based on the first attention score sequence;
[0035] a dimensionality reduction module, configured to perform dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and to select a second value vector sequence from the first value vector sequence based on the second attention score sequence;
[0036] A weighting module is used to obtain a target word vector corresponding to each of the initial word vectors based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors.
[0037] In one embodiment, the calculation module is specifically used to calculate and obtain the attention variance corresponding to the initial word vector according to the first attention score sequence corresponding to each of the initial word vectors;
[0038] Based on the attention variance, the sequence dimension of the first attention score sequence, and an adaptive calculation formula based on the attention score variance, an adaptive threshold corresponding to the initial word vector is determined.
[0039] In one embodiment, the dimension reduction module is specifically used to sort the first attention scores in the first attention score sequence corresponding to each of the initial word vectors to obtain a sorting result;
[0040] Accumulate and calculate the first attention scores according to the sorting result to obtain an initial cumulative sum result, and determine a target cumulative sum result according to the adaptive threshold and the initial cumulative sum result;
[0041] Filtering the first attention scores in the first attention score sequence according to the target cumulative sum result to obtain a second attention score sequence;
[0042] The first value vector sequence is screened based on the second attention score sequence to obtain a second value vector sequence.
[0043] In one embodiment, the dimension reduction module is specifically used to accumulate the first attention scores in the first attention score sequence according to the sorting result to obtain an initial cumulative sum result;
[0044] In the case where the initial cumulative sum result is greater than the adaptive threshold, determining each of the initial cumulative sum results as a target cumulative sum result;
[0045] When the initial cumulative sum result is less than or equal to the adaptive threshold, continue to execute the step of accumulating the attention scores in the first attention score sequence according to the sorting result to obtain the initial cumulative sum result.
[0046] In one of the embodiments, the dimension reduction module is specifically used to determine an initial second attention score in the first attention score sequence according to the target cumulative sum result;
[0047] The initial second attention score that is ranked last in the first attention score sequence and other first attention scores are eliminated to obtain a second attention score sequence.
[0048] In one embodiment, the dimension reduction module is specifically used to determine the second value vector in the first value vector sequence based on the corresponding relationship between the attention score and the value vector, and each second attention score in the second attention score sequence;
[0049] A second value vector sequence is obtained according to each of the second value vectors.
[0050] In one embodiment, the weighting module is specifically used to normalize the second attention score sequence corresponding to each of the initial word vectors to obtain a target attention score sequence;
[0051] The target attention score sequence and the second value vector sequence are weightedly summed to obtain a target word vector corresponding to each of the initial word vectors.
[0052] In a third aspect, the present application further provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0053] Get the first value vector sequence and the first attention score sequence corresponding to the initial word vector;
[0054] For each of the initial word vectors, calculating an adaptive threshold corresponding to the initial word vector based on the first attention score sequence;
[0055] Performing dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence;
[0056] Based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors, a target word vector corresponding to each of the initial word vectors is obtained.
[0057] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:
[0058] Get the first value vector sequence and the first attention score sequence corresponding to the initial word vector;
[0059] For each of the initial word vectors, calculating an adaptive threshold corresponding to the initial word vector based on the first attention score sequence;
[0060] Performing dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence;
[0061] Based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors, a target word vector corresponding to each of the initial word vectors is obtained.
[0062] In a fifth aspect, the present application further provides a computer program product, including a computer program, which implements the following steps when executed by a processor:
[0063] Get the first value vector sequence and the first attention score sequence corresponding to the initial word vector;
[0064] For each of the initial word vectors, calculating an adaptive threshold corresponding to the initial word vector based on the first attention score sequence;
[0065] Performing dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence;
[0066] Based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors, a target word vector corresponding to each of the initial word vectors is obtained.
[0067] The above data processing method, device, computer equipment, computer-readable storage medium and computer program product obtain the first value vector sequence and the first attention score sequence corresponding to the initial word vector; for each initial word vector, the adaptive threshold corresponding to the initial word vector is calculated based on the first attention score sequence; the first attention score sequence is reduced in dimension according to the adaptive threshold to obtain the second attention score sequence, and the second value vector sequence is selected in the first value vector sequence based on the second attention score sequence; based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each initial word vector, the target word vector corresponding to each initial word vector is obtained. By adopting this method, by constructing the adaptive threshold corresponding to the initial word vector, a targeted analysis of the influence of each initial word vector in the attention mechanism is realized. Since the adaptive threshold can reflect the correlation influence of the first attention score on the attention mechanism calculation, the first attention score sequence and the first value vector sequence are reduced in dimension according to the adaptive threshold, which can eliminate the influence of the initial word vector with low correlation in the input text on the attention calculation, improve the accuracy of the target word vector in reflecting the correlation, and reduce the calculation overhead. BRIEF DESCRIPTION OF THE DRAWINGS
[0068] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the drawings required for use in the embodiments of the present application or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0069] Figure 1 is a flow chart of a data processing method in one embodiment;
[0070] Figure 2 A schematic diagram of a flow chart of calculating an adaptive threshold in one embodiment;
[0071] Figure 3 Schematic diagram of a process flow of a first attention score sequence dimensionality reduction processing step in one embodiment;
[0072] Figure 4 A schematic diagram of a flow chart of a step of determining a target accumulation and a result in one embodiment;
[0073] Figure 5 It is a flowchart of a first attention score sequence dimensionality reduction processing step in a specific embodiment;
[0074] Figure 6 A schematic diagram of a flow chart of a step of determining a second value vector sequence in one embodiment;
[0075] Figure 7A schematic diagram of a flow chart of the steps of calculating a target word vector in one embodiment;
[0076] Figure 8 is a schematic diagram of an example of a data processing method in one embodiment;
[0077] Fig. 9 is a structural block diagram of a data processing device in one embodiment;
[0078] Fig.10 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0079] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0080] In one embodiment, Figure 1 As shown, a data processing method is provided. This embodiment takes the method applied to a terminal as an example. It can be understood that the method can also be applied to a server, and can also be applied to a system including a terminal and a server, and is implemented through the interaction between the terminal and the server. In this embodiment, the method includes the following steps:
[0081] Step 102, obtain a first value vector sequence and a first attention score sequence corresponding to the initial word vector.
[0082] In the embodiment of the present application, the specific process of the terminal generating the initial word vector according to the input original text and generating the first value vector sequence and the first attention score sequence according to the initial word vector is described in detail in the following embodiment. After the terminal completes the generation of the first value vector sequence and the first attention score sequence, it passes them downward and further processes the first value vector sequence and the first attention score sequence.
[0083] Step 104: For each initial word vector, an adaptive threshold corresponding to the initial word vector is calculated based on the first attention score sequence.
[0084] Among them, the adaptive threshold is used to reflect the relevance of the first attention score in the attention mechanism calculation.
[0085] In an embodiment of the present application, the calculation of the adaptive threshold and the process of dimensionality reduction processing of the first attention score sequence in the subsequent steps can be built into the encoder or encoding structure of the natural language processing model. For each initial word vector, the terminal can analyze the concentration of the first attention score sequence according to a variety of methods to determine the adaptive threshold that matches the current initial word vector. In this embodiment, the terminal calculates the attention variance based on the first attention score in the first attention sequence, and obtains the adaptive threshold based on the attention variance by applying the adaptive calculation formula based on the attention score variance. The application of the adaptive calculation formula based on the attention score variance is described in detail in the following embodiments, and will not be repeated in this example. Optionally, the terminal can also calculate according to the distribution of each first attention score in the first attention score sequence, such as an average value or a weighted average value as an adaptive threshold, or the terminal can analyze and learn the first attention score sequence through an additional neural network layer to obtain the adaptive threshold corresponding to each first initial word vector.
[0086] Step 106, performing dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence.
[0087] In an embodiment of the present application, the terminal can directly use the comparison result of each first attention score in the first attention score sequence with the adaptive threshold as the basis for dimensionality reduction processing, screen the elements in the first attention score sequence, and obtain a second attention score sequence. In order to match the first value vector sequence of the initial word vector with the second attention score sequence obtained after the dimensionality reduction processing, the terminal also needs to synchronously perform dimensionality reduction operations on the first value vector sequence. The terminal can perform the same screening on the first value vector sequence with the same mark or in the same position according to the mark or position of the first attention score and the screening result of the first attention score sequence, and obtain a second value vector sequence corresponding to the second attention score sequence. Since the adaptive threshold can reflect the correlation between the initial word vectors, and then the first attention score is screened based on the adaptive threshold, the first attention score with low correlation in the input text can be eliminated so that it does not participate in the calculation of the attention mechanism, so as to reduce the influence of the initial word vector with low correlation on the calculation of the attention mechanism.
[0088] Step 108, based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each initial word vector, obtain the target word vector corresponding to each initial word vector.
[0089] In an embodiment of the present application, the terminal uses the second attention score sequence corresponding to each initial word vector as a weight, and performs weighted summation on the second value vector sequence, and the result of the weighted summation is the target word vector corresponding to the first initial word vector. By calculating the attention score of the initial word vector and performing weighted summation to obtain the target word vector, the dynamic contextual relationship of each initial word vector in the original text sequence can be captured, and the importance of each word in the original text sequence can be calculated, and the contextual information can be better utilized for specific tasks.
[0090] In the above data processing method, by constructing an adaptive threshold corresponding to the initial word vector, a targeted analysis of the influence of each initial word vector in the attention mechanism is achieved. Since the adaptive threshold can reflect the correlation influence of the first attention score on the attention mechanism calculation, and then the first attention score sequence and the first value vector sequence are reduced in dimension according to the adaptive threshold, the influence of the initial word vector with low correlation in the input text on the attention calculation can be eliminated, the accuracy of the target word vector in reflecting the correlation can be improved, and the computational overhead can be reduced.
[0091] In an exemplary embodiment, step 102 further includes steps A01 to A03, wherein:
[0092] A01, each word in the original text is vectorized through the word embedding model to obtain the initial word vector corresponding to each word.
[0093] In the embodiment of the present application, for the application of natural language processing tasks, such as machine translation, abstract extraction or sentiment analysis tasks, the terminal first obtains the original text to be processed, and then preprocesses the text, including word segmentation and text cleaning, to obtain multiple words contained in the original text, and vectorizes each word, that is, the terminal can input it into the word embedding model, which can be CBOW (Continuous Bag Of Words, a neural network model for generating word vectors) or NNLM (Neural Network Language Model, Neural Network Language Model), etc. Each word in the original text is vectorized by the word embedding model to obtain the initial word vector corresponding to each word.
[0094] A02, based on the parameter matrix of the attention mechanism and the initial word vector, generates the query vector, key vector and value vector corresponding to the initial word vector.
[0095] In the embodiment of the present application, the first value vector sequence and the first attention score sequence are obtained by using the parameter matrix in the attention mechanism. , as well as The initial word vector is obtained by the terminal, wherein the initialization and training update method of the parameter matrix is the same as the traditional attention mechanism, and will not be described in detail in the present embodiment. , and Multiply them to get the query vector , key vector and the value vector .
[0096] A03, generate a first attention score sequence according to the query vector and key vector corresponding to the initial word vector, and construct a first value vector sequence according to the value vector corresponding to the initial word vector.
[0097] In the embodiment of the present application, the value vectors corresponding to all the initial word vectors constitute a first value vector sequence and a key vector sequence, and the query vector Perform vector dot product operations on the key vector sequences corresponding to all the initial word vectors of the input text including itself, and normalize the vector dot product results to obtain The first attention score sequence.
[0098] In an exemplary embodiment, a method for calculating an adaptive threshold is provided, such as Figure 2 As shown, step 104 includes steps 202 to 204. Among them:
[0099] Step 202, based on the first attention score sequence corresponding to each initial word vector, calculate and obtain the attention variance corresponding to the initial word vector.
[0100] In an embodiment of the present application, first, for the sequence of initial word vectors input, the terminal uses a standard attention mechanism to calculate the first attention score sequence corresponding to each word vector, including calculating the similarity or correlation score (first attention score) between the word vector and the query vector, and forming the first attention score sequence through the similarity or correlation score. For the first attention score sequence corresponding to each initial word vector, the terminal first calculates the average value (mean) of the first attention score, and calculates the variance of each first attention score sequence based on the average value, and obtains the attention variance corresponding to the first attention score sequence of each initial word vector.
[0101] Step 204, based on the attention variance, the sequence dimension of the first attention score sequence, and the adaptive calculation formula based on the attention score variance, determine the adaptive threshold corresponding to the initial word vector.
[0102] In the embodiment of the present application, after obtaining the attention variance, the terminal applies an adaptive calculation formula based on the attention score variance to each word vector to determine its corresponding adaptive threshold, wherein the adaptive calculation formula based on the attention score variance is as follows:
[0103]
[0104] in, is the variance function, is the first attention score sequence, which is used to calculate the first attention score sequence The variance of is the dimension of the input sequence. This calculation is done by The data distribution and fluctuation of the system determine the value of the adaptive threshold P, achieving the purpose of self-adaptation. In addition, As a dimensionality compensation, it can effectively handle similar This extreme attention score distribution leads to a small variance.
[0105] In this embodiment, through an adaptive calculation formula based on the attention score variance, an adaptive threshold that matches the initial word vector can be determined according to the data distribution and fluctuation degree in the first attention score sequence, and then subsequent processing is performed based on the adaptive threshold. This can more accurately reflect the characteristics of the input initial word vector and improve the accuracy of the dimensionality reduction processing of the first attention score sequence.
[0106] In an exemplary embodiment, the method of performing dimensionality reduction processing on the first attention score sequence is not unique, and the terminal can also perform dimensionality reduction processing based on sorting and accumulation, such as Figure 3 As shown, step 106 includes steps 302 to 308. Among them:
[0107] Step 302, for each first attention score sequence corresponding to the initial word vector, the first attention scores in the first attention score sequence are sorted to obtain a sorting result.
[0108] In an embodiment of the present application, the terminal sorts the first attention scores in the first attention score sequence in descending order according to the size of the first attention scores for each first initial word vector, and stores the sorted results for subsequent analysis and processing.
[0109] Step 304, the first attention scores are cumulatively calculated according to the sorting result to obtain an initial cumulative sum result, and a target cumulative sum result is determined according to the adaptive threshold and the initial cumulative sum result.
[0110] In an embodiment of the present application, for each sorted sequence, the terminal first initializes a cumulative sum variable S to 0, and adds the first attention score to S one by one in the order of the sorting results to obtain an initial cumulative sum result. In the process of accumulation, each time the initial cumulative sum result is obtained, the initial cumulative sum result is compared with the adaptive threshold, and the initial cumulative sum result that meets the adaptive threshold condition is determined as the target cumulative sum result based on the comparison result between the initial cumulative sum result and the adaptive threshold after each accumulation. For example, when the initial cumulative sum result is greater than the adaptive threshold corresponding to the current initial word vector, the current initial cumulative sum result is used as the target cumulative sum result.
[0111] Optionally, for multiple initial cumulative sum results, the terminal can determine a target cumulative sum result from the initial cumulative sum results based on different cumulative sum evaluation criteria. For example, the terminal can directly compare the adaptive threshold with each initial cumulative sum result, and take the initial cumulative sum result closest to the adaptive threshold as the target cumulative sum result.
[0112] Alternatively, the terminal sorts the first attention scores from large to small, and performs accumulation processing to obtain multiple initial cumulative sum results with gradually decreasing increment values. The initial cumulative sum result can illustrate the contribution of each first attention score to the overall cumulative sum, and how the cumulative sum increases as the first attention scores at different positions in the sorting result are accumulated.
[0113] The evaluation criteria of the cumulative sum can also be evaluated based on the comparison results of multiple initial cumulative sum results in the accumulation process with the adaptive threshold. Specifically, since the sorting result is the sorting result of the first attention score from large to small, the initial cumulative sum result is also an increasing sequence with gradually decreasing increments. The terminal compares each initial cumulative sum result with the adaptive threshold. If the comparison result obtained is that the initial cumulative sum result at a certain position is greater than the adaptive threshold, the terminal can determine the first initial cumulative sum result that exceeds the adaptive threshold as the target cumulative sum result, indicating that the first attention scores corresponding to the initial cumulative sum results that are less than or equal to the adaptive threshold are all key information with a high correlation with the target task.
[0114] Step 306, screening the first attention scores in the first attention score sequence according to the target cumulative sum result to obtain a second attention score sequence.
[0115] In an embodiment of the present application, the terminal traces back the sorted first attention score sequence according to the target cumulative sum result, and filters the first attention scores according to the target cumulative sum result. For example, the first attention scores involved in the cumulative calculation in the target cumulative sum result are retained, or based on other filtering rules, the first attention scores are filtered according to the target cumulative sum result to obtain a second attention score sequence.
[0116] Step 308, screening the first value vector sequence based on the second attention score sequence to obtain a second value vector sequence.
[0117] In an embodiment of the present application, since the first attention score sequence has a corresponding relationship with the first value vector sequence, the terminal can determine the first value vector in the first value vector sequence that can be used as a retained value vector or the first value vector that needs to be screened and eliminated according to the identification or position of the first attention score used as a retained attention score in the screening process in the first attention score sequence, and screen the first value vector sequence based on the same principle to obtain a second value vector sequence.
[0118] In this embodiment, by sorting the first attention score sequence and determining the target cumulative sum result, and then screening and reducing the dimension of the first attention score sequence according to the target cumulative sum result, the information selection for the target task can be optimized, and the part with less relevance to the target task can be screened and reduced in dimension, so that the content with stronger relevance to the current task can be selected more accurately, reducing the computational overhead and improving the accuracy of attention calculation.
[0119] In an exemplary embodiment, the method of determining the target cumulative sum result according to the adaptive threshold is not unique. This embodiment provides an implementation method for determining the target cumulative sum result, such as Figure 4 As shown, step 304 includes steps 402 to 406. Among them:
[0120] Step 402, performing cumulative calculation on the first attention scores in the first attention score sequence according to the sorting result to obtain an initial cumulative sum result.
[0121] In the embodiment of the present application, the terminal performs cumulative calculation according to the sorting results of the first attention scores arranged from large to small to obtain an initial cumulative sum result. The initial cumulative sum result can illustrate the contribution of each first attention score to the overall cumulative sum, and how the cumulative sum increases as the first attention scores at different positions in the sorting results are accumulated.
[0122] Step 404: when the initial cumulative sum result is greater than the adaptive threshold, each initial cumulative sum result is determined as a target cumulative sum result.
[0123] In the embodiment of the present application, in the process of accumulating and calculating the first attention score according to the sorting result, the terminal compares the current initial cumulative sum result with the adaptive threshold. Since the first attention scores corresponding to the initial cumulative sum results that are less than or equal to the adaptive threshold are all key information with a high correlation with the target task, when the initial cumulative sum result is greater than the adaptive threshold, the terminal determines the current initial cumulative sum result that exceeds the adaptive threshold as the target cumulative sum result.
[0124] Step 406, when the initial cumulative sum result is less than or equal to the adaptive threshold, continue to perform cumulative calculations on the attention scores in the first attention score sequence according to the sorting results to obtain the initial cumulative sum result.
[0125] In an embodiment of the present application, during the cumulative calculation of the first attention score, if the initial cumulative sum result is less than or equal to the adaptive threshold, it means that the current first attention score can be retained to participate in the subsequent attention weighted sum calculation, and the terminal continues to execute step 402.
[0126] In this embodiment, the first initial cumulative sum result that is greater than the adaptive threshold is determined as the target cumulative sum result, which can accurately determine the relevance distribution score boundary of each first attention score for the target task. Using the target cumulative sum result as the basis for dimensionality reduction can improve the accuracy of dimensionality reduction of the first attention score sequence and ensure the model's attention to key information.
[0127] In an exemplary embodiment, Figure 5 As shown, step 306 includes steps 502 to 504. Among them:
[0128] Step 502, determining an initial second attention score in the first attention score sequence according to the target cumulative sum result.
[0129] In the embodiment of the present application, the terminal divides all first attention scores involved in the target cumulative sum result determined in step 404 into initial second attention scores. Specifically, the terminal can classify and divide the first attention scores based on the position marks corresponding to the first attention scores involved in the target cumulative sum result in the first attention score sequence.
[0130] Step 504: Eliminate the initial second attention score that is ranked last in the first attention score sequence and other first attention scores to obtain a second attention score sequence.
[0131] In the embodiment of the present application, the initial second attention score that is the last in the sorting result of the first attention score sequence is the standard point for correlation division, that is, the first attention score that is newly added to the cumulative calculation in the calculation process of the target cumulative result, and the attention scores that are subsequently cumulatively calculated are all less relevant to the target task. Therefore, the terminal determines the last initial second attention score based on the position of each first attention score in the target cumulative result in the first attention score sequence, and then excludes the last initial second attention score and other first attention scores, that is, the second attention score is obtained after dimensionality reduction screening, which means that the model can focus on the information that contributes most to the overall context understanding by selecting all the first attention scores before the cumulative sum reaches the target threshold.
[0132] Optionally, the terminal can identify other first attention scores that have not been selected based on the second attention score and the position mark, and the terminal eliminates them. The elimination process may be to set their attention scores to 0 or remove them from the first attention score sequence.
[0133] In this embodiment, the first attention score sequence is screened for dimensionality reduction based on the cumulative sum result and the adaptive threshold, which can reduce the computational overhead of the attention mechanism and eliminate the influence of less relevant information on the target word vector calculation, so that the attention mechanism can more accurately capture and utilize contextual information, thereby improving the accuracy of the target word vector results in related tasks of natural language processing.
[0134] In an exemplary embodiment, Figure 6 As shown, step 308 includes steps 602 to 604. Among them:
[0135] Step 602, based on the correspondence between the attention score and the value vector, and each second attention score in the second attention score sequence, determine the second value vector in the first value vector sequence.
[0136] In an embodiment of the present application, the correspondence between the attention score and the value vector can be obtained through the correspondence between the position mark of the attention score in the attention score sequence and the position mark of the value vector in the value vector sequence, or the correspondence can also be other identifiers. Specifically, the terminal collects the position mark of each retained second attention score from the second attention score sequence, and searches for the second value vector as the retained value vector in the first value vector sequence based on the correspondence between the position mark and the position mark of the attention score in the attention score sequence and the position mark of the value vector in the value vector sequence, wherein the second value vector is selected to be retained based on its importance in the second attention score sequence.
[0137] Step 604: Obtain a second value vector sequence according to each second value vector.
[0138] In the embodiment of the present application, the terminal ensures that the screened second value vectors are sorted according to their order in the original first value vector sequence, and the sorted second value vectors are sequentially combined into a second value vector sequence.
[0139] In this embodiment, the first value vector is selected based on the importance of the second attention score, thereby forming a more refined and effective information representation, eliminating information with low relevance, reducing the overhead of computing resources, and improving the accuracy of the target word vector results in related tasks of natural language processing.
[0140] In an exemplary embodiment, Figure 7 As shown, step 108 includes steps 702 to 704. Among them:
[0141] Step 702, normalize the second attention score sequence corresponding to each initial word vector to obtain a target attention score sequence.
[0142] In an embodiment of the present application, for the second attention score sequence corresponding to each initial word vector, the terminal performs secondary normalization processing on the second attention score sequence obtained after dimensionality reduction through a Softmax function to obtain a target attention score sequence, thereby ensuring that the sum of the elements of the new attention score sequence (i.e., the target attention score sequence) is always 1.
[0143] Step 704, performing weighted sum processing on the target attention score sequence and the second value vector sequence to obtain the target word vector corresponding to each initial word vector.
[0144] In the embodiment of the present application, for each initial word vector, the terminal performs a weighted summation of the target attention score sequence corresponding to the initial word vector and the second value vector sequence obtained after screening to obtain the target word vector corresponding to the current initial word vector. The target word vector The calculation formula is as follows:
[0145]
[0146] in, is the attention score sequence, is the second value vector sequence.
[0147] In this embodiment, through normalization and weighted summation, refined optimization and information integration of the initial word vectors are achieved, and a target word vector is generated for each initial word vector. The target word vector integrates the contribution of all relevant information in the context and more accurately captures the semantics of each initial word vector in a specific context.
[0148] In a specific embodiment, an example of a data processing method is provided, such as Figure 8 As shown, where:
[0149] Initial word vector Get the query vector based on the pre-trained parameter matrix , key vector and the value vector , value vector Construct the first value vector sequence and based on the query vector and key vector get The corresponding first attention score sequence is Figure 8 As shown, =0.7, =0.2, =0.1. , and Sort by size, and the result is: , , According to the first attention score sequence, the adaptive calculation formula of the attention score variance is applied to obtain The corresponding adaptive threshold, such as Figure 8 As shown, the adaptive threshold P=0.96.
[0150] The first attention scores in the first attention score sequence are screened by an adaptive threshold, such as Figure 8 As shown, each first attention score is accumulated and calculated, that is, + =0.9, at this time, the initial cumulative sum is less than the adaptive threshold P, then the cumulative calculation continues, that is, + + =1, at this time the initial cumulative sum is greater than the adaptive threshold P, then the current initial cumulative sum is determined as the target cumulative sum.
[0151] The first attention score included in the target accumulation and result , and Determine as the initial second attention score, and sort the initial second attention score that is the last in the first attention score sequence (i.e. ) and other first attention scores are eliminated to obtain the second attention score involved in the attention mechanism calculation, and according to the corresponding relationship between the attention score and the value vector and the second attention score existing in the second attention score sequence, the first value vector in the first value vector sequence is correspondingly selected to obtain the second value vector ( and ) is a second value vector sequence composed of .
[0152] Normalize the second attention score twice (based on the Softmax function) to get the target attention score and , and finally according to the target attention score , and the second value vector , Perform weighted summation to obtain the initial word vector The corresponding target word vector .
[0153] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0154] Based on the same inventive concept, the embodiment of the present application also provides a data processing device for implementing the data processing method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in the one or more data processing device embodiments provided below can refer to the limitations on the data processing method above, and will not be repeated here.
[0155] In an exemplary embodiment, Fig. 9 As shown, a data processing device 900 is provided, comprising: an acquisition module 901, a calculation module 902, a dimension reduction module 903 and a weighting module 904, wherein:
[0156] An acquisition module 901 is used to acquire a first value vector sequence and a first attention score sequence corresponding to an initial word vector;
[0157] A calculation module 902 is used to calculate, for each initial word vector, an adaptive threshold corresponding to the initial word vector based on the first attention score sequence;
[0158] A dimension reduction module 903 is used to perform dimension reduction processing on the first attention score sequence according to an adaptive threshold to obtain a second attention score sequence, and to select a second value vector sequence from the first value vector sequence based on the second attention score sequence;
[0159] The weighting module 904 is used to obtain the target word vector corresponding to each initial word vector by weighted summation of the second attention score sequence and the second value vector sequence corresponding to each initial word vector.
[0160] In one embodiment, the calculation module 902 is specifically used to calculate and obtain the attention variance corresponding to the initial word vector according to the first attention score sequence corresponding to each initial word vector;
[0161] Based on the attention variance, the sequence dimension of the first attention score sequence, and the adaptive calculation formula based on the attention score variance, the adaptive threshold corresponding to the initial word vector is determined.
[0162] In one embodiment, the dimension reduction module 903 is specifically used to sort the first attention scores in the first attention score sequence corresponding to each initial word vector to obtain a sorting result;
[0163] The first attention scores are accumulated and calculated according to the sorting results to obtain an initial accumulated sum result, and a target accumulated sum result is determined according to the adaptive threshold and the initial accumulated sum result;
[0164] The first attention score in the first attention score sequence is screened according to the target cumulative sum result to obtain a second attention score sequence;
[0165] The first value vector sequence is screened based on the second attention score sequence to obtain a second value vector sequence.
[0166] In one embodiment, the dimension reduction module 903 is specifically used to perform cumulative calculation on the first attention scores in the first attention score sequence according to the sorting result to obtain an initial cumulative sum result;
[0167] When the initial cumulative sum result is greater than the adaptive threshold, each initial cumulative sum result is determined as a target cumulative sum result;
[0168] When the initial cumulative sum result is less than or equal to the adaptive threshold, continue to perform the step of accumulating the attention scores in the first attention score sequence according to the sorting result to obtain the initial cumulative sum result.
[0169] In one embodiment, the dimension reduction module 903 is specifically used to determine an initial second attention score in the first attention score sequence according to the target cumulative sum result;
[0170] The initial second attention score that is ranked last in the first attention score sequence and other first attention scores are eliminated to obtain a second attention score sequence.
[0171] In one embodiment, the dimension reduction module 903 is specifically used to determine the second value vector in the first value vector sequence based on the correspondence between the attention score and the value vector, and each second attention score in the second attention score sequence;
[0172] A second value vector sequence is obtained according to each second value vector.
[0173] In one embodiment, the weighting module 904 is specifically used to normalize the second attention score sequence corresponding to each initial word vector to obtain a target attention score sequence;
[0174] The target attention score sequence and the second value vector sequence are weightedly summed to obtain the target word vector corresponding to each initial word vector.
[0175] Each module in the above data processing device can be implemented in whole or in part by software, hardware or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.
[0176] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as shown in FIG. Fig.10 As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. Among them, the processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store an initial word vector, a first attention score sequence, a first value vector sequence, a second attention score sequence, a second value vector and a target word vector. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a data processing method is implemented.
[0177] Those skilled in the art will understand that Fig.10The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0178] In an exemplary embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the following steps are implemented:
[0179] Get the first value vector sequence and the first attention score sequence corresponding to the initial word vector;
[0180] For each initial word vector, calculate the adaptive threshold corresponding to the initial word vector based on the first attention score sequence;
[0181] Performing dimensionality reduction processing on the first attention score sequence according to an adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence;
[0182] Based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each initial word vector, the target word vector corresponding to each initial word vector is obtained.
[0183] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0184] According to the first attention score sequence corresponding to each initial word vector, the attention variance corresponding to the initial word vector is calculated and obtained;
[0185] Based on the attention variance, the sequence dimension of the first attention score sequence, and the adaptive calculation formula based on the attention score variance, the adaptive threshold corresponding to the initial word vector is determined.
[0186] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0187] For the first attention score sequence corresponding to each initial word vector, the first attention scores in the first attention score sequence are sorted to obtain a sorting result;
[0188] The first attention scores are accumulated and calculated according to the sorting results to obtain an initial accumulated sum result, and a target accumulated sum result is determined according to the adaptive threshold and the initial accumulated sum result;
[0189] The first attention score in the first attention score sequence is screened according to the target cumulative sum result to obtain a second attention score sequence;
[0190] The first value vector sequence is screened based on the second attention score sequence to obtain a second value vector sequence.
[0191] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0192] Accumulate the first attention scores in the first attention score sequence according to the sorting result to obtain an initial cumulative sum result;
[0193] When the initial cumulative sum result is greater than the adaptive threshold, each initial cumulative sum result is determined as a target cumulative sum result;
[0194] When the initial cumulative sum result is less than or equal to the adaptive threshold, continue to perform the step of accumulating the attention scores in the first attention score sequence according to the sorting result to obtain the initial cumulative sum result.
[0195] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0196] determining an initial second attention score in the first attention score sequence according to the target cumulative sum result;
[0197] The initial second attention score that is ranked last in the first attention score sequence and other first attention scores are eliminated to obtain a second attention score sequence.
[0198] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0199] Determine a second value vector in the first value vector sequence based on a correspondence between the attention scores and the value vectors and each second attention score in the second attention score sequence;
[0200] A second value vector sequence is obtained according to each second value vector.
[0201] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0202] Normalize the second attention score sequence corresponding to each initial word vector to obtain the target attention score sequence;
[0203] The target attention score sequence and the second value vector sequence are weightedly summed to obtain the target word vector corresponding to each initial word vector.
[0204] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above method embodiments are implemented.
[0205] In one embodiment, a computer program product is provided, including a computer program, which implements the steps in the above method embodiments when executed by a processor.
[0206] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant regulations.
[0207] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic unit, a data processing logic unit based on quantum computing, an artificial intelligence (AI) processor, etc., but are not limited to this.
[0208] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0209] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A data processing method, characterized in that: The method comprises: Get the first value vector sequence and the first attention score sequence corresponding to the initial word vector; For each of the initial word vectors, calculating an adaptive threshold corresponding to the initial word vector based on the first attention score sequence; Performing dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence; Based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors, a target word vector corresponding to each of the initial word vectors is obtained.
2. The method according to claim 1, characterized in that The step of calculating, for each of the initial word vectors, an adaptive threshold corresponding to the initial word vector based on the first attention score sequence includes: According to the first attention score sequence corresponding to each of the initial word vectors, calculate and obtain the attention variance corresponding to the initial word vector; Based on the attention variance, the sequence dimension of the first attention score sequence, and an adaptive calculation formula based on the attention score variance, an adaptive threshold corresponding to the initial word vector is determined.
3. The method according to claim 1, characterized in that: The step of performing dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and screening a second value vector sequence in the first value vector sequence based on the second attention score sequence includes: For each of the first attention score sequences corresponding to the initial word vectors, sorting the first attention scores in the first attention score sequence to obtain a sorting result; Accumulate and calculate the first attention scores according to the sorting result to obtain an initial cumulative sum result, and determine a target cumulative sum result according to the adaptive threshold and the initial cumulative sum result; Filtering the first attention scores in the first attention score sequence according to the target cumulative sum result to obtain a second attention score sequence; The first value vector sequence is screened based on the second attention score sequence to obtain a second value vector sequence.
4. The method according to claim 3, characterized in that The step of accumulating the first attention scores according to the sorting result to obtain an initial cumulative sum result, and determining a target cumulative sum result according to the adaptive threshold and the initial cumulative sum result includes: Accumulate and calculate the first attention scores in the first attention score sequence according to the sorting result to obtain an initial cumulative sum result; In the case where the initial cumulative sum result is greater than the adaptive threshold, determining each of the initial cumulative sum results as a target cumulative sum result; When the initial cumulative sum result is less than or equal to the adaptive threshold, continue to execute the step of accumulating the attention scores in the first attention score sequence according to the sorting result to obtain the initial cumulative sum result.
5. The method according to claim 3, characterized in that: The step of screening the first attention scores in the first attention score sequence according to the target cumulative sum result to obtain a second attention score sequence includes: Determining an initial second attention score in the first attention score sequence according to the target cumulative sum result; The initial second attention score that is ranked last in the first attention score sequence and other first attention scores are eliminated to obtain a second attention score sequence.
6. The method according to claim 3, characterized in that The step of screening the first value vector sequence based on the second attention score sequence to obtain a second value vector sequence includes: Determine a second value vector in the first value vector sequence based on a correspondence between attention scores and value vectors and each second attention score in the second attention score sequence; A second value vector sequence is obtained according to each of the second value vectors.
7. The method according to claim 1, characterized in that The step of obtaining a target word vector corresponding to each of the initial word vectors by weighted summing the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors comprises: Normalizing the second attention score sequence corresponding to each of the initial word vectors to obtain a target attention score sequence; The target attention score sequence and the second value vector sequence are weightedly summed to obtain a target word vector corresponding to each of the initial word vectors.
8. A data processing device, characterized in that: The device comprises: An acquisition module, used to acquire a first value vector sequence and a first attention score sequence corresponding to the initial word vector; A calculation module, configured to calculate, for each of the initial word vectors, an adaptive threshold corresponding to the initial word vector based on the first attention score sequence; a dimensionality reduction module, configured to perform dimensionality reduction processing on the first attention score sequence according to the adaptive threshold to obtain a second attention score sequence, and to select a second value vector sequence from the first value vector sequence based on the second attention score sequence; A weighting module is used to obtain a target word vector corresponding to each of the initial word vectors based on the weighted sum of the second attention score sequence and the second value vector sequence corresponding to each of the initial word vectors.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
11. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
