Machine translation method and device, electronic equipment and storage medium
By obtaining the coverage vector and estimating the length of the target word sequence in the machine translation model, and optimizing the position encoding, the problem of insufficient control of the output sequence length of the Transformer model is solved, the accuracy and fluency of translation are improved, and missed translations and over-translations are avoided. translation phenomenon.
Patent Information
- Application Number
- CN202410732769.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-06
- Publication Date
- 2024-09-03
AI Technical Summary
Existing machine translation models based on neural networks, especially the Transformer model, lack a mechanism to control the length of the output sequence, resulting in missed translations and over-translations, affecting the accuracy and fluency of translation.
By obtaining the coverage vector of the machine translation model at the current time step, the length of the target end word sequence is estimated based on the length of the input source end word sequence and the target end word sequence, and the length is added to the position coding to optimize the position coding for Translate to avoid missed translations and over-translations.
It effectively avoids missing translations and over-translations, improves the accuracy and performance of machine translation, and ensures the quality of translation results.
Smart Images

Figure CN118586407A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a machine translation method, device, electronic device, and computer-readable storage medium. Background Art
[0002] With the development of neural network technology, the accuracy of machine translation has gradually improved, and the application of machine translation has become more and more extensive. For example, the transformer model has been widely used in the field of machine translation. In addition to the accuracy and fluency of translation, the length of machine translation based on neural network positioning is also an important evaluation indicator for measuring the performance of machine translation. Some applications will optimize the output length. Especially in some important documents, missed translation and over-translation will have a serious impact on the translation results. Therefore, how to reduce the missed translation and over-translation phenomena of machine translation based on neural networks and improve the performance of machine translation is crucial. However, due to the huge differences in the output sequences generated by the transformer model, the model has no mechanism to control the length of the output sequence. That is, the transformer model does not explicitly model the sentence length of the input and output, and the decoding method does not allow the specification of the expected length of the translation to be generated. When the transformer model is translating, missed translation or over-translation may occur.
[0003] It can be seen that the machine translation method in the prior art still needs to be improved. Summary of the invention
[0004] The embodiments of the present application provide a machine translation method and device, which are helpful to improve the accuracy of machine translation.
[0005] In a first aspect, an embodiment of the present application provides a machine translation method, comprising:
[0006] Obtaining a coverage vector of the machine translation model at the current time step, wherein the coverage vector is used to identify the source end words that have completed translation in the input source end word sequence of the machine translation model;
[0007] According to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence, the length of the target end word sequence corresponding to the current time step is estimated;
[0008] Adding the length of the target end word sequence to the position code of the machine translation model to obtain an optimized position code;
[0009] Based on the optimized position encoding, the source end word sequence corresponding to the current time step is translated to obtain a translation result of the source end word sequence corresponding to the current time step.
[0010] In a second aspect, an embodiment of the present application provides a machine translation device, including:
[0011] A coverage vector acquisition module, used to acquire a coverage vector of the machine translation model at a current time step, wherein the coverage vector is used to identify the source end word that has completed translation in the input source end word sequence of the machine translation model;
[0012] A word sequence length estimation module, used to estimate the length of the target end word sequence corresponding to the current time step according to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence;
[0013] A position encoding optimization module, used for adding the length of the target end word sequence to the position encoding of the machine translation model to obtain an optimized position encoding;
[0014] The translation module is used to translate the source end word sequence corresponding to the current time step based on the optimized position encoding to obtain a translation result of the source end word sequence corresponding to the current time step.
[0015] In a third aspect, an embodiment of the present application further discloses an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the machine translation method described in the embodiment of the present application when executing the computer program.
[0016] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium on which a computer program is stored, and when the program is executed by a processor, the steps of the machine translation method disclosed in the embodiment of the present application are implemented.
[0017] The machine translation method disclosed in the embodiment of the present application obtains the coverage vector of the machine translation model at the current time step, wherein the coverage vector is used to identify the source end words that have been translated in the input source end word sequence of the machine translation model; estimates the length of the target end word sequence corresponding to the current time step according to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence; adds the length of the target end word sequence to the position coding of the machine translation model to obtain an optimized position coding; and translates the source end word sequence corresponding to the current time step based on the optimized position coding to obtain a translation result of the source end word sequence corresponding to the current time step, which helps to avoid the occurrence of missed translation and over-translation, thereby improving the accuracy of machine translation.
[0018] The above description is only an overview of the technical solution of the present application. In order to more clearly understand the technical means of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0020] Figure 1 is a flow chart of the machine translation method disclosed in the embodiment of the present application;
[0021] Figure 2 It is a schematic diagram of the attention matrix content of the source end to the target end in the machine translation method disclosed in the embodiment of the present application;
[0022] Figure 3 is a schematic diagram of the structure of a machine translation device disclosed in an embodiment of the present application;
[0023] Figure 4 A block diagram schematically shows an electronic device for executing the method according to the present application; and
[0024] Figure 5 A storage unit for holding or carrying a program code for implementing the method according to the present application is schematically shown. DETAILED DESCRIPTION
[0025] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0026] Reference Figure 1 , a machine translation method disclosed in an embodiment of the present application includes: steps 110 to 140.
[0027] Step 110, obtaining a coverage vector of the machine translation model at the current time step, wherein the coverage vector is used to identify source end words that have completed translation in an input source end word sequence of the machine translation model.
[0028] In machine translation technology, coverage vectors are used to track the source words that have been translated. Taking the source sentence x = {x1, x2, x3, x4} as an example, the initial coverage vector is C = {0, 0, 0, 0}, indicating that the source sentence has not been translated yet. When translating, first translate x1, the coverage vector can become {1, 0, 0, 0}, then translate x2, the coverage vector can become {1, 1, 0, 0}, and there is no need to repeat the coverage until the coverage vector becomes {1, 1, 1, 1}, indicating that all words in the source sentence have been translated. Using coverage vectors can avoid missing translations and over-translations, and each source word is guaranteed to be translated.
[0029] In attention-based machine translation models, there is usually a lack of a mechanism to indicate whether a source word has been translated, so it is prone to "coverage" errors. For example, some parts of the source sentence are translated more than once or not translated. For machine translation models, directly modeling coverage is complex, but this problem can be significantly alleviated by tracking the attention signal during decoding. For example, a coverage vector is attached to each source word (i.e., h j ), which is initialized as a zero vector but updated after each perusal of the corresponding annotation. The coverage vector is fed to the attention model to help adjust future attention, which enables the machine translation model to consider untranslated words more.
[0030] For the transformer-based translation model, since the coverage vector covers the attention history of hj, the words around the j-th source word will receive a lot of attention, which will lead to over-translation and further attention should be prevented. In the prior art, the coverage vector of the transformer-based translation model is calculated by the following formula:
[0031] C i,j =gupdate(C i-1,j , α i,j ,φ(h j ), T);
[0032] where gupdate(·) is the decoding process at time step i through the new attention α i,j Update C i,j Function; C i,j is a d-dimensional cover vector summarizing the hidden layer output h j The attention history up to time step i; φ(h j ) is a word-specific feature with its own parameters; τ is an auxiliary input used in different kinds of coverage models. Among them, gupdate(·) and φ(·) can take different functional forms, as well as different auxiliary inputs τ.
[0033] In the prior art, a scalar (d=1) is used to represent the coverage vector of each source word, and an accumulation operation is used for gupdate. That is, the initial value of the coverage vector is zero, indicating that the corresponding source word has not been translated. The coverage vector is iteratively constructed by accumulating the alignment probabilities generated by the attention model, and each alignment probability is normalized by different context-related weights. At time step i, the source word x j The coverage vector is calculated by the following formula:
[0034]
[0035] Among them, φ j is a predefined weight, indicating the expected j The number of target terminology to be generated; α k,j represents the attention weight of the jth source word at time step k. In the prior art, there is a method that sets the predefined weights of all source words to a fixed value φ=1, which means that the alignment method is used directly without normalization. However, in neural machine translation, different types of source words may contribute differently to the generation of the target sentence. The methods in the prior art will increase the translation error and reduce the translation quality.
[0036] In an embodiment of the present application, obtaining the coverage vector of the machine translation model at the current time step includes: based on the attention mechanism, obtaining the coverage vector of the machine translation model at the current time step according to the attention value of the source end word that has completed the translation in the input source end word sequence of the machine translation model.
[0037] Among the important properties of the known multi-head attention mechanism, in the last layer of the transformer decoder, each encoder-decoder attention head is scattered and aligned with a specific source word, which is likely to be translated next. The multi-head attention mechanism of the last layer of the decoder for the encoder represents multiple word alignment. The characters on the source side corresponding to the attention of the last layer of the decoder are the characters that are very likely to be generated next, that is, the first few characters with the largest weights in the attention matrix of the source side to the target side in the last layer of the decoder, which most likely determine the characters to be translated by the neural network next. Therefore, the first few characters with the largest weights in the attention matrix generated by each translation can be used to approximate the characters on the source side to be translated. In the embodiment of the present application, there is no need to adjust the attention mechanism, and only the translated source words are counted by the attention mechanism to calculate the coverage vector.
[0038] In some optional embodiments, the attention mechanism is based on obtaining the coverage vector of the machine translation model at the current time step according to the attention value of the source end word that has completed the translation in the input source end word sequence of the machine translation model, including: sub-steps S1 to S3.
[0039] Sub-step S1, obtaining the attention matrix of the source end to the target end according to the attention value of each head in the terminal layer of the multi-head attention mechanism decoder of the machine translation model.
[0040] The dimension of the attention matrix is: second length × first length, wherein the first length is the length of the input source word sequence, and the second length is the length of the target word sequence translated up to the current time step. Each element value in the attention matrix represents the influence of the corresponding source word on the target word.
[0041] The maximum value of the attention of each head in the last layer of the decoder of the translation model based on the transformer structure is most likely the next word to be translated. In the embodiment of the present application, taking the translation model including N heads as an example, the multi-head attention mechanism selects the attention matrix of the source end to the target end with N heads. Therefore, there will be N alternative results during translation, among which there may be overlaps, and the elements in each vector in each head range from 0 to 1. Add the values of the N heads and divide by N to get the mean of all heads, which also ranges from 0 to 1. The attention matrix of the source end to the target end can be calculated by the following formula:
[0042]
[0043] Among them, head i represents the attention value of the ith head, N represents the number of heads in the multi-head attention, and A represents the average of the attention matrix of the source end to the target end. The dimension of the attention matrix A is the number of target end words × the number of source end words. Taking N equal to 8 as an example, the content of the attention matrix can be as follows Figure 2 shown.
[0044] Sub-step S2, according to the preset judgment condition, obtains the word sequence positions of at most a preset number of source end words that have completed the translation according to the attention matrix.
[0045] The preset judgment condition includes: the size condition of the attention value and the difference condition of the attention value corresponding to the source end word. The preset judgment condition is set according to the actual application scenario and the test result. The preset number is determined according to the specific application scenario. Preferably, the preset number is 3.
[0046] In some optional embodiments, the word sequence positions of at most a preset number of source end words that have completed translation are obtained according to the attention matrix according to the preset judgment condition, including: for each target end word, the word sequence positions of at most a preset number of source end words that have completed translation are obtained according to the comparison result of the attention value of each source end word to the target end word in the attention matrix and the preset weight threshold, and / or, the word sequence positions of at most a preset number of source end words that have completed translation are obtained according to the comparison result of the difference between two adjacent attention values and the preset weight difference threshold. The value of the preset weight threshold is determined according to experimental results, for example, it can be greater than or equal to 0.3 or less than or equal to 0.4.
[0047] Optionally, the word sequence positions of up to a preset number of source words that have completed the translation are obtained based on the comparison result of the attention value of each source word to the target word in the attention matrix with a preset weight threshold, and / or based on the comparison result of the difference between two adjacent attention values with a preset weight difference threshold, including: for each target word, in the order of the attention values of each source word to the target word from large to small, judging in turn whether the current attention value meets the corresponding judgment condition, until the attention value does not meet the corresponding judgment condition or the number of attention values that meet the corresponding judgment condition is greater than or equal to the preset number, and the corresponding judgment condition includes: the maximum attention value is greater than the preset weight threshold, and the difference between the non-maximum attention value and the previous attention value that meets the corresponding judgment condition is less than the preset weight difference threshold; the word sequence position of the source word corresponding to the attention value that meets the corresponding judgment condition is used as the word sequence position of the source word that has completed the translation.
[0048] Among them, obtaining the word sequence positions of at most a preset number of source-end words that have completed the translation means: taking the preset number equal to 3 as an example, a maximum of three steps of judgment are required to determine the source-end words that have completed the translation. After that, the word sequence positions of these three source-end words can be further determined. The three judgment steps are introduced below. That is, the at most preset number refers to less than or equal to the preset number. For example, when the preset number is equal to 3, the word sequence positions of 1 or 2 source-end words that have completed the translation are obtained.
[0049] The first step is to obtain the maximum attention value in the attention matrix, and then determine whether the maximum attention value is greater than a preset weight threshold. If so, determine that the source end word corresponding to the maximum attention value is the first source end word corresponding to the target end word.
[0050] Among them, the judgment condition of the first step can be expressed as:
[0051] Among them, Canidate1 represents the first source word currently determined, ratio represents the preset weight threshold, and A iIt represents the attention value of the i-th source word in the attention matrix to the current target word, and argmax represents the maximum value operation.
[0052] The second step is to determine whether the difference between the second largest attention value and the maximum attention value in the attention matrix is less than a preset weight difference threshold. If so, the source end word corresponding to the second largest attention value is used as the second source end word corresponding to the target end word.
[0053] Among them, the judgment condition of the second step can be expressed as:
[0054]
[0055] Among them, Canidate1 represents the first source end word that has been determined, Canidate2 represents the second source end word, TH represents the preset weight difference threshold, A i It represents the attention value of the i-th source word in the attention matrix to the current target word, and argmax represents the maximum value operation.
[0056] The third step is to determine whether the difference between the third largest attention value and the second largest attention value in the attention matrix is less than a preset weight difference threshold. If so, the source end word corresponding to the third largest attention value is used as the word sequence position of the third source end word corresponding to the target end word.
[0057] Among them, the judgment condition of the third step can be expressed as:
[0058]
[0059] Among them, Canidate1 represents the first source end word that has been determined, Canidate2 represents the second source end word, Canidate2 represents, TH represents the preset weight difference threshold, A i represents the attention value of the i-th source end word in the attention matrix to the target end word, and argmax represents the maximum value operation. For example, the value of TH can be 0.1.
[0060] At this point, the three source end words corresponding to the target end words are determined.
[0061] In the three-step judgment executed in the above sequence, if any step does not meet the judgment condition, the subsequent steps will not be executed. For example, when only the maximum attention value in the attention matrix in the first step is greater than the preset weight threshold, and the difference between the second largest attention value in the attention matrix and the maximum attention value is greater than or equal to the preset weight difference threshold, the third step will not be executed.
[0062] In some possible embodiments, if the preset number is 1, it is only necessary to perform the aforementioned first step operation based on the comparison result of the attention value of each source end word to the target end word in the attention matrix and the preset weight threshold to obtain the word sequence position of a source end word that has completed the translation.
[0063] In an embodiment of the present application, for each target end word, the attention value of the first source end word to the target end word must be greater than a preset weight threshold, otherwise it cannot be proved that the source end word has a corresponding translation relationship with the target end word. The attention value of the second source end word to the target end word needs to satisfy that the attention value is less than 0.1 of the first source end word. If the gap is too large, it can be considered that the target end word is only translated from the first source end word. Similarly, the attention value of the third source end word to the target end word needs to satisfy that the attention value is less than 0.1 of the second source end word. If the gap is too large, it can be considered that the target end word is only translated from the first source end word and the second source end word. And so on, until the number of determined source end words is greater than or equal to the preset number.
[0064] Sub-step S3, generating a coverage vector of the machine translation model at the current time step according to the word sequence position.
[0065] Among them, the coverage vector is a one-dimensional vector with a length equal to the length of the input source end word sequence. The initial value of the coverage vector is set to a zero vector with a dimension of 1×the length, such as C={0, 0, 0, 0}, where the number of 0s is equal to the length of the input source end word sequence, and each time the 0s of up to three j positions are converted to 1s, indicating that the source end word has been translated. If the j position obtained is already 1, it means that a source end word is related to the translation of multiple target end words. There is no need to repeat the coverage, and the vector remains unchanged. In the end, a one-dimensional vector with a length equal to the length of the input source end word sequence will be obtained, in which the position with a value of 1 represents that the source end word at that position has participated in the translation, and the position with a value of 0 represents that the source end word at that position has not participated in the translation, or the degree of participation is very low and can be ignored.
[0066] Step 120, based on the length of the translated source word, the length of the translated target word sequence, and the length of the input source word sequence, estimate the length of the target word sequence corresponding to the current time step.
[0067] In the embodiments of the present application, the length of the target-side word sequence can be a relative length or an absolute length. The relative length is the percentage of the length of the translated characters in the total character length of the target-side word sequence, and the absolute length is the total character length of the completely translated target-side word sequence. Since the same source-side word may be translated into target-side words of different lengths, the length of the target-side word sequence cannot be directly obtained. In some embodiments of the present application, an approximate method is used to obtain the proportion of the source-side words that have been translated (i.e., the translated source-side words), obtain the relative length information, and then calculate the absolute length based on the relative length.
[0068] In some embodiments of the present application, estimating the length of the target-side word sequence corresponding to the current time step according to the length of the source-side word that has been translated, the length of the translated target-side word, and the length of the input source-side word sequence includes: obtaining the relative length according to the proportion of the source-side word that has been translated relative to the length of the input source-side word sequence, and using the relative length as the length of the target-side word sequence corresponding to the current time step; calculating the absolute length corresponding to the target-side word sequence according to the relative length and the length of the translated target-side word, and using the absolute length as the length of the target-side word sequence corresponding to the current time step.
[0069] During the translation process, it can be considered that there is a dynamic balance in how many source-side words each generated target-side word is translated from, and the longer the translation, the more stable it is. Therefore, the relationship between the translation lengths of the source-side word and the target-side word can be expressed by the following formula:
[0070]
[0071] In the above formula 1, len 源翻 represents the number of characters of the source-side that have been translated (i.e., the length of the source-side word), len 源 represents the length of the input source-side word sequence, len 目标翻 represents the number of characters of the target-side that have been translated (i.e., the length of the target-side word), len 目标 represents the length of the target-side word sequence.
[0072] Taking the input source-side word sequence "I want to go to school" and the translated target-side word sequence "i go to school" as an example, len 源 = 5, representing the character length of the word sequence "I want to go to school", and len 目标 = 4, representing the character length of the translated target-side word sequence "i go to school". When starting the translation, for the translated target-side word len 目标翻 "i", its corresponding source-side word len 源翻It is "I", that is, the source end word "I" corresponds to the target end word "i".
[0073] Specifically, the accurate relative length should be the length of the source end word that has been translated compared to the length of the input source end word sequence, or the length of the target end word that has been translated by the target end compared to the length obtained by the overall translation of the target end, and the two will change dynamically in numerical value and become closer and closer. Among them, the length of the source end word that has been translated, the length of the input source end word sequence and the length of the target end word that has been translated by the target end are determined, and the length of the overall translation of the known target end cannot be directly obtained. Therefore, in the embodiment of the present application, the length of the source end word that has been translated by the source end can be solved by first indirectly obtaining the length of the source end word that the source end has translated, thereby increasing the accuracy of the input encoding.
[0074] In the embodiment of the present application, the coverage vector is used to approximate the length of the source end that has been translated. The following introduces the methods for obtaining the relative length and the absolute length respectively.
[0075] (I) Relative length
[0076] Optionally, the ratio of the source-end words that have been translated by the source end to the length of the input source-end word sequence is calculated based on the coverage vector. If the position with a value of 1 in the coverage vector is a source-end word that has participated in the translation, and the source-end length is known, the relative length can be calculated by formula 2:
[0077]
[0078] In the above formula 2, pos r Indicates the calculated relative length, x i =1 means covering the value in vector C, len 源 Indicates the length of the input source word sequence.
[0079] (ii) Absolute length
[0080] According to formula 1, we can get formula 3:
[0081]
[0082] Substituting the relative length into Formula 3, we get Formula 4:
[0083]
[0084] In the above formula 4, len 目标 Indicates the absolute length to be obtained, pos r is the relative length obtained, len 目标翻 It indicates the length of the target word that has been translated. From the above formula 4, we can see that when pos r When it is 0, it does not meet the mathematical definition.r When it is 1, the length of the target word that has been translated by the target end is len 目标翻 and len 目标 Equal, will not provide any additional valuable information. In the experiment, most cases will not produce pos r is 1, so according to observation, we can choose pos r The absolute length information is obtained in the range of 0.4 to 0.6.
[0085] In other optional embodiments, the length of the target word sequence corresponding to the current time step is estimated based on the length of the source word that has been translated, the length of the target word obtained by translation, and the length of the input source word sequence, including: based on Hinchin's law of large numbers, the absolute length of the target word sequence is calculated based on the length of the input source word sequence and the arithmetic mean of the length of the source word corresponding to each translated target word, as the length of the target word sequence corresponding to the current time step.
[0086] Hinchin's law of large numbers theoretically states that it is reasonable to use the arithmetic mean to approximate the actual true value. Under the premise that the lengths of the source and target words of machine translation satisfy Hinchin's law of large numbers, let a1, a2, ...a n is a sequence of independent and identically distributed random variables. If a finite mathematical expectation exists μ, then it obeys the law of large numbers, that is, for any ε>0, there is formula 5:
[0087]
[0088] Among them, a k It can be understood that in the process of machine translation, the target end word generated at time step k is translated from several source end words, and μ represents the average number of source end words translated from the target end word in machine translation, so μ = len 源 / len 目标 , n represents the length of the target end word that has been translated. The above translation process shows that in machine translation, the average corresponding relationship between the translation length of the target end word and the source end word is: (1 / 4)×(1+2+0+2)=5 / 4. That is, len 源 / len 目标 =5 / 4. According to this method, the absolute length of the target end word sequence can be estimated based on the known length of the input source end word sequence.
[0089] In the embodiments of the present application, estimating the length of the target word sequence based on the coverage vector helps improve the accuracy of translation compared to the prior art of taking the length of the input source word sequence as the length of the target word sequence.
[0090] Step 130, adding the length of the target end word sequence to the position code of the machine translation model to obtain an optimized position code.
[0091] As mentioned above, the length of the target-side word sequence includes: absolute length and relative length. In the embodiments of the present application, according to the lengths of different types of target-side word sequences, different methods are used to add the length of the target-side word sequence to the position encoding of the machine translation model.
[0092] In some optional embodiments, the length of the target-side word sequence is added to the position encoding of the machine translation model to obtain an optimized position encoding, including: in response to the length of the target-side word sequence being an absolute length, subtracting the mark position of the current time step from the absolute length to obtain a difference, updating the mark position of the machine translation model, and performing position encoding according to the updated mark position to obtain an optimized position encoding.
[0093] The length of the target end word sequence may be an absolute length calculated by any of the methods described above. When the length of the target end word sequence is an absolute length, the position encoding of the machine translation model may be updated by the following formulas 6 and 7:
[0094]
[0095] Wherein, i represents the time step, i=1,…,d / 2, d represents the encoding dimension, pos represents the tag position of the machine translation model, and len represents the estimated absolute length of the target end word sequence.
[0096] The estimated absolute length of the target word sequence minus the current position marker is used as a new position marker, so that the position encoding of the machine translation model includes both the length information of the target word sequence to be translated and the position information, which can improve the encoding accuracy.
[0097] In some other optional embodiments, the length of the target-side word sequence is added to the position encoding of the machine translation model to obtain an optimized position encoding, including: in response to the length of the target-side word sequence being a relative length, updating the marker position of the machine translation model with the sum of the relative length and the marker position of the current time step, and performing position encoding according to the updated marker position to obtain an optimized position encoding.
[0098] The length of the target end word sequence may be a relative length calculated by the method described above. In the case where the length of the target end word sequence is a relative length, the position encoding of the machine translation model may be updated by the following formulas 8 and 9:
[0099]
[0100] Where i represents the time step, i=1,…,d / 2, d represents the encoding dimension, pos represents the marker position of the machine translation model, pos r Indicates the estimated relative length of the target word sequence.
[0101] The relative length pos of the target end word sequence r The positional encoding added to the machine translation model is used to explicitly model the input and output sentence lengths, and the positional encoding used by the machine translation model is enriched with information representing the position of a word relative to the end of the target string, so that the positional encoding of the machine translation model includes both the length information of the target end word sequence and the position information, thereby improving the encoding accuracy.
[0102] In an embodiment of the present application, a machine translation model is first trained based on a transformer based on training data as a first machine translation model; then, the position encoding of the transformer is improved, and the improved transformer is trained to obtain a second machine translation model. In the process of training the second machine translation model, for the current source end word sequence sample, the current source end word sequence sample is first input into the first machine translation model, the attention matrix of each head of the last layer decoder of the first machine translation model is obtained, and the coverage vector corresponding to each time step is calculated according to the attention matrix according to the aforementioned method, and the length of the source end word completed in the input source end word sequence identified by the coverage vector, the length of the target end word obtained by translation, and the length of the input source end word sequence, the length of the target end word sequence corresponding to the corresponding time step is estimated. On the other hand, the current source end word sequence sample is input into the second machine translation model, and at the same time, the length of the target end word sequence calculated according to the attention matrix of the first machine translation model is used as an input of the position encoding of the corresponding time step, and the encoding position is enhanced so that the second machine translation model can better translate the target end word of the ideal length.
[0103] That is, the length of the target end word sequence is calculated by the attention matrix obtained when the pre-trained first machine translation model translates the source end word sequence. In this step, the length of the target end word sequence is added to the position encoding of the second machine translation model to obtain an optimized position encoding.
[0104] In some other embodiments of the present application, only one machine translation model may be trained. When obtaining the coverage vector of the machine translation model at the current time step, the coverage vector is calculated based on the attention matrix of the source end to the target end in the last layer of the decoder when the machine translation model translates the mth source end word, and is used to estimate the length of the target end word sequence corresponding to the m+1th target end word. Afterwards, the length of the target end word sequence is used as an input for position encoding of the machine translation model, and position encoding is performed.
[0105] Step 140: Based on the optimized position code, the source end word sequence corresponding to the current time step is translated to obtain a translation result of the source end word sequence corresponding to the current time step.
[0106] Next, the pre-trained machine translation model (such as the second machine translation model) translates the source end word sequence corresponding to the current time step based on the optimized position encoding to obtain the translation result of the source end word sequence corresponding to the current time step (ie, the target end word sequence).
[0107] For a specific implementation method of translating the source-end word sequence corresponding to the current time step based on the optimized position coding to obtain the translation result of the source-end word sequence corresponding to the current time step, refer to the specific implementation method of translating the source-end word sequence corresponding to the current time step based on position coding in the prior art, which will not be repeated here.
[0108] The translation model based on the Transformer model solves the problem of non-parallelism in models such as long short-term memory networks through positional encoding, but the positional encoding only contains positional information, including the absolute position of the current time step and the correlation information between different time steps, and there is no explicit modeling of the length encoding. Therefore, when predicting, the transformer model only knows how many words have been translated, and it is not clear how many words are left to be translated, and the percentage of the translated words in the target translation length, which is prone to over-translation and omission. In the embodiment of the present application, by introducing the length parameter of the target end word sequence in the positional encoding, the occurrence of over-translation and omission can be effectively avoided.
[0109] In summary, the machine translation method disclosed in the embodiment of the present application obtains the coverage vector of the machine translation model at the current time step, wherein the coverage vector is used to identify the source end words that have been translated in the input source end word sequence of the machine translation model; estimates the length of the target end word sequence corresponding to the current time step according to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence; adds the length of the target end word sequence to the position coding of the machine translation model to obtain an optimized position coding; based on the optimized position coding, translates the source end word sequence corresponding to the current time step to obtain the translation result of the source end word sequence corresponding to the current time step, which helps to avoid the occurrence of missed translation and over-translation, thereby improving the accuracy of machine translation.
[0110] Reference Figure 3 The present application also discloses a machine translation device, which includes:
[0111] A coverage vector acquisition module 310 is used to acquire a coverage vector of the machine translation model at the current time step, wherein the coverage vector is used to identify the source end words that have completed translation in the input source end word sequence of the machine translation model;
[0112] A word sequence length estimation module 320 is used to estimate the length of the target end word sequence corresponding to the current time step according to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence;
[0113] A position encoding optimization module 330, configured to add the length of the target end word sequence to the position encoding of the machine translation model to obtain an optimized position encoding;
[0114] The translation module 340 is used to translate the source-end word sequence corresponding to the current time step based on the optimized position code to obtain a translation result of the source-end word sequence corresponding to the current time step.
[0115] Optionally, the coverage vector acquisition module 310 is further used to:
[0116] Based on the attention mechanism, the coverage vector of the machine translation model at the current time step is obtained according to the attention value of the source end word that has completed the translation in the input source end word sequence of the machine translation model.
[0117] Optionally, the attention mechanism is based on the attention value of the source end word that has completed the translation in the input source end word sequence of the machine translation model to obtain the coverage vector of the machine translation model at the current time step, including:
[0118] Obtaining an attention matrix of a source end to a target end according to the attention value of each head in the terminal layer of a multi-head attention mechanism decoder of the machine translation model;
[0119] According to the preset judgment condition, obtaining the word sequence positions of at most a preset number of source end words that have completed the translation according to the attention matrix;
[0120] According to the word sequence position, a coverage vector of the machine translation model at the current time step is generated.
[0121] Optionally, the step of obtaining, according to a preset judgment condition, word sequence positions of at most a preset number of source-end words that have completed translation according to the attention matrix includes:
[0122] For each target word, based on the comparison result of the attention value of each source word in the attention matrix to the target word with a preset weight threshold, and / or based on the comparison result of the difference between two adjacent attention values with a preset weight difference threshold, the word sequence positions of up to a preset number of source words that have completed the translation are obtained.
[0123] Optionally, the word sequence length estimation module 320 is further configured to:
[0124] Obtaining a relative length according to the length ratio of the source end word that has completed the translation relative to the input source end word sequence, and using the relative length as the length of the target end word sequence corresponding to the current time step;
[0125] According to the relative length and the length of the target end word obtained after the translation, the absolute length corresponding to the target end word sequence is calculated, and the absolute length is used as the length of the target end word sequence corresponding to the current time step.
[0126] Optionally, the word sequence length estimation module 320 is further configured to:
[0127] Based on Hinchin's law of large numbers, the absolute length of the target word sequence is calculated according to the length of the input source word sequence and the arithmetic mean of the length of the source word corresponding to each translated target word, as the length of the target word sequence corresponding to the current time step.
[0128] Optionally, the adding the length of the target end word sequence to the position encoding of the machine translation model to obtain an optimized position encoding includes:
[0129] In response to the length of the target end word sequence being an absolute length, the tag position of the machine translation model is updated by subtracting the tag position of the current time step from the absolute length, and position encoding is performed according to the updated tag position to obtain an optimized position encoding;
[0130] In response to the length of the target end word sequence being a relative length, the marker position of the machine translation model is updated with the sum of the relative length and the marker position of the current time step, and position encoding is performed according to the updated marker position to obtain an optimized position encoding.
[0131] The machine translation device disclosed in the embodiment of the present application is used to implement the machine translation method described in the embodiment of the present application. The specific implementation methods of each module of the device will not be repeated here. Please refer to the specific implementation methods of the corresponding steps in the method embodiment.
[0132] In summary, the machine translation device disclosed in the embodiment of the present application obtains the coverage vector of the machine translation model at the current time step, wherein the coverage vector is used to identify the source end word that has been translated in the input source end word sequence of the machine translation model; estimates the length of the target end word sequence corresponding to the current time step according to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence; adds the length of the target end word sequence to the position coding of the machine translation model to obtain an optimized position coding; based on the optimized position coding, translates the source end word sequence corresponding to the current time step to obtain the translation result of the source end word sequence corresponding to the current time step, which helps to avoid the occurrence of missed translation and over-translation, thereby improving the accuracy of machine translation.
[0133] Each embodiment in this specification is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0134] The above is a detailed introduction to a machine translation method and device, and a machine translation system provided by the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and a core idea. At the same time, for a general technician in this field, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
[0135] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0136] The various component embodiments of the present application can be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. It should be understood by those skilled in the art that a microprocessor or digital signal processor (DSP) can be used in practice to implement some or all functions of some or all components in the electronic device according to the embodiment of the present application. The present application can also be implemented as a device or apparatus program (e.g., computer program and computer program product) for executing part or all of the methods described herein. Such a program implementing the present application can be stored on a computer-readable medium, or can have the form of one or more signals. Such a signal can be downloaded from an Internet website, or provided on a carrier signal, or provided in any other form.
[0137] For example, Figure 4 An electronic device that can implement the method according to the present application is shown. The electronic device can be a PC, a mobile terminal, a personal digital assistant, a tablet computer, etc. The electronic device traditionally includes a processor 410 and a memory 420 and a program code 430 stored on the memory 420 and can be run on the processor 410, and the processor 410 implements the method described in the above embodiment when executing the program code 430. The memory 420 can be a computer program product or a computer-readable medium. The memory 420 can be an electronic memory such as a flash memory, an EEPROM (electrically erasable programmable read-only memory), an EPROM, a hard disk or a ROM. The memory 420 has a storage space 4201 for the program code 430 of the computer program for executing any method step in the above method. For example, the storage space 4201 for the program code 430 can include various computer programs for implementing the various steps in the above method respectively. The program code 430 is a computer-readable code. These computer programs can be read from one or more computer program products or written into the one or more computer program products. These computer program products include program code carriers such as hard disks, compact disks (CDs), memory cards or floppy disks. The computer program includes a computer readable code, and when the computer readable code is executed on an electronic device, the electronic device is caused to execute the method according to the above embodiment.
[0138] The embodiment of the present application further discloses a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the steps of the machine translation method described in the embodiment of the present application are implemented.
[0139] Such a computer program product may be a computer-readable storage medium, which may have a computer-readable storage medium having a Figure 4 The memory 420 in the electronic device shown in the figure is similar to the storage segment, storage space, etc. The program code can be compressed and stored in the computer-readable storage medium in an appropriate form. The computer-readable storage medium is generally as shown in the reference Figure 5 The portable or fixed storage unit. Generally, the storage unit includes computer readable code 430', which is a code read by a processor. When the code is executed by the processor, each step in the method described above is implemented.
[0140] The term "one embodiment", "embodiment" or "one or more embodiments" herein means that a particular feature, structure or characteristic described in conjunction with the embodiment is included in at least one embodiment of the present application. In addition, please note that the examples of the term "in one embodiment" here do not necessarily all refer to the same embodiment.
[0141] In the description provided herein, a large number of specific details are described. However, it is understood that the embodiments of the present application can be practiced without these specific details. In some instances, well-known methods, structures and techniques are not shown in detail so as not to obscure the understanding of this description.
[0142] In the claims, any reference signs placed between brackets shall not be construed as limiting the claims. The word "comprising" does not exclude the presence of elements or steps not listed in the claims. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The present application may be implemented by means of hardware comprising several different elements and by means of a suitably programmed computer. In a unit claim enumerating several means, several of these means may be embodied by the same item of hardware. The use of the words first, second, and third etc. does not indicate any order. These words may be interpreted as names.
[0143] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A machine translation method, characterized in that: The method comprises: Obtaining a coverage vector of the machine translation model at the current time step, wherein the coverage vector is used to identify the source end words that have completed translation in the input source end word sequence of the machine translation model; According to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence, the length of the target end word sequence corresponding to the current time step is estimated; Adding the length of the target end word sequence to the position code of the machine translation model to obtain an optimized position code; Based on the optimized position encoding, the source end word sequence corresponding to the current time step is translated to obtain a translation result of the source end word sequence corresponding to the current time step.
2. The method according to claim 1, characterized in that The obtaining of the coverage vector of the machine translation model at the current time step includes: Based on the attention mechanism, the coverage vector of the machine translation model at the current time step is obtained according to the attention value of the source end word that has completed the translation in the input source end word sequence of the machine translation model.
3. The method according to claim 2, characterized in that The method based on the attention mechanism, according to the attention value of the source end word that has completed the translation in the input source end word sequence of the machine translation model, obtains the coverage vector of the machine translation model at the current time step, including: Obtaining an attention matrix of a source end to a target end according to the attention value of each head in the terminal layer of a multi-head attention mechanism decoder of the machine translation model; According to the preset judgment condition, obtaining the word sequence positions of at most a preset number of source end words that have completed the translation according to the attention matrix; According to the word sequence position, a coverage vector of the machine translation model at the current time step is generated.
4. The method according to claim 3, characterized in that: The step of obtaining, according to the attention matrix, at most a preset number of word sequence positions of source end words that have completed the translation according to the preset judgment condition comprises: For each target word, based on the comparison result of the attention value of each source word in the attention matrix to the target word with a preset weight threshold, and / or based on the comparison result of the difference between two adjacent attention values with a preset weight difference threshold, the word sequence positions of up to a preset number of source words that have completed the translation are obtained.
5. The method according to claim 1, characterized in that The estimating the length of the target end word sequence corresponding to the current time step according to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence includes: Obtaining a relative length according to the length ratio of the source end word that has completed the translation relative to the input source end word sequence, and using the relative length as the length of the target end word sequence corresponding to the current time step; According to the relative length and the length of the target end word obtained after the translation, the absolute length corresponding to the target end word sequence is calculated, and the absolute length is used as the length of the target end word sequence corresponding to the current time step.
6. The method according to claim 1, characterized in that The estimating the length of the target end word sequence corresponding to the current time step according to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence includes: Based on Hinchin's law of large numbers, the absolute length of the target word sequence is calculated according to the length of the input source word sequence and the arithmetic mean of the length of the source word corresponding to each translated target word, as the length of the target word sequence corresponding to the current time step.
7. The method according to claim 5 or 6, characterized in that: The step of adding the length of the target end word sequence to the position code of the machine translation model to obtain an optimized position code comprises: In response to the length of the target end word sequence being an absolute length, the tag position of the machine translation model is updated by subtracting the tag position of the current time step from the absolute length, and position encoding is performed according to the updated tag position to obtain an optimized position encoding; In response to the length of the target end word sequence being a relative length, the marker position of the machine translation model is updated with the sum of the relative length and the marker position of the current time step, and position encoding is performed according to the updated marker position to obtain an optimized position encoding.
8. A machine translation device, characterized in that: The device comprises: A coverage vector acquisition module, used to acquire a coverage vector of the machine translation model at a current time step, wherein the coverage vector is used to identify the source end word that has completed translation in the input source end word sequence of the machine translation model; A word sequence length estimation module, used to estimate the length of the target end word sequence corresponding to the current time step according to the length of the source end word that has been translated, the length of the target end word obtained by translation, and the length of the input source end word sequence; A position encoding optimization module, used for adding the length of the target end word sequence to the position encoding of the machine translation model to obtain an optimized position encoding; The translation module is used to translate the source end word sequence corresponding to the current time step based on the optimized position encoding to obtain a translation result of the source end word sequence corresponding to the current time step.
9. An electronic device comprising a memory, a processor, and a program code stored in the memory and executable on the processor, wherein: When the processor executes the program code, the method according to any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium having program code stored thereon, characterized in that: When the program code is executed by a processor, the steps of the method described in any one of claims 1 to 7 are implemented.
