Text prediction method and apparatus, computer-readable storage medium, and electronic device
By incorporating sequence length into the beam search algorithm to control the target score, and utilizing an improved beam search algorithm such as the transformer model for text prediction, the problem of low accuracy of the beam search algorithm in sequence prediction tasks is solved, achieving more efficient and accurate text prediction.
Patent Information
- Application Number
- CN202310249616.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-10
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2043-03-10
AI Technical Summary
The beam search algorithm suffers from low prediction accuracy in sequence prediction tasks, especially when the candidate space is large, the computational cost is huge and it is difficult to obtain the global optimal solution.
By incorporating sequence length into the beam search algorithm to control the target score of the initial identifier sequence, and by utilizing the positive correlation between the target score and sequence length, the algorithm suppresses excessively high scores for short sequences and avoids premature termination of prediction. Improved beam search algorithms, such as transformer models or RNN models, are used for text prediction.
It improves the accuracy and efficiency of text prediction, effectively suppresses the phenomenon of excessively high scores for short sequences, ensures that the prediction process does not terminate prematurely, and obtains better prediction results.
Smart Images

Figure CN116311277B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence, and more specifically, to a text prediction method, apparatus, computer-readable storage medium, and electronic device. Background Technology
[0002] Beam search is widely used in sequence prediction deep learning models, such as transformer machine translation, text generation (including writing essays, copywriting, summarizing, composing couplets, free-response questions, writing novels, and completing full texts), and optical character recognition (OCR) and other sequence prediction tasks.
[0003] When processing the sequence probabilities (an L×N probability matrix, where L represents the sequence length and N represents the candidate space size, also known as the dictionary size) output by the model, a greedy algorithm that selects the maximum probability at each step can quickly yield results, but this result may not be globally optimal. Exhaustive searching of all sequences to select the one with the maximum probability sum can achieve global optimality, but when the candidate space is large, its computational cost is extremely high, reaching N to the power of L. The beam search algorithm is a compromise heuristic algorithm that selects the optimal solution up to the current step within a beam space at each step, thus balancing computational speed and prediction accuracy. However, even with this approach, its prediction accuracy still needs improvement.
[0004] There is currently no effective solution to the above problems. Summary of the Invention
[0005] This invention provides a text prediction method, apparatus, computer-readable storage medium, and electronic device to at least solve the technical problem of low prediction accuracy in text prediction using the beam search algorithm in related technologies.
[0006] According to one aspect of the present invention, a text prediction method is provided, comprising: acquiring a target image, wherein the target image includes multiple characters; inputting the target image into a target prediction model, predicting the multiple characters in the target image through the target prediction model to obtain a target text sequence, wherein the target prediction model is used to predict multiple characters step by step, in each step of prediction there are multiple initial identifier sequences, determining a target score for the initial identifier sequence based on the sequence length and target probability of the initial identifier sequence, and determining a target text sequence from the multiple initial identifier sequences based on the target score, wherein the target score is positively correlated with the sequence length, and the target probability characterizes the probability that the text sequence matched by the initial identifier sequence is a text sequence composed of multiple characters.
[0007] Furthermore, the text prediction method also includes: in each prediction step, determining multiple initial identifier sequences for the current step; for each initial identifier sequence in the current step, determining a target score for the initial identifier sequence based on its sequence length and target probability; selecting multiple candidate identifier sequences from the multiple initial identifier sequences in the current step based on the target score, wherein the candidate identifier sequences are used to determine the initial identifier sequences in the next prediction step; and determining the target text sequence based on the candidate identifier sequences in the current step and their target scores if the candidate identifier sequences in the current step meet preset conditions.
[0008] Furthermore, the text prediction method also includes: when the current step is the i-th step, determining the logarithm of the target probability of the initial identifier sequence based on the target scores of the candidate identifier sequences that make up the initial identifier sequence in the (i-1)-th step, where i is a positive integer greater than 1; taking the logarithm of the sequence length of the initial identifier sequence to obtain a first value, and determining the target coefficient based on the first value, where the target coefficient is negatively correlated with the first value; and calculating the product of the logarithm of the target probability of the initial identifier sequence and the target coefficient to obtain the target score of the initial identifier sequence.
[0009] Furthermore, the text prediction method also includes: when the current step is the i-th step, sorting the multiple initial identifier sequences of the i-th step according to the target score from high to low, to obtain multiple sorted initial identifier sequences, where i is a positive integer greater than 1; selecting the first M initial identifier sequences from the sorted multiple initial identifier sequences to obtain M undetermined candidate identifier sequences, where M is a positive integer greater than N, and N represents the size of the beam search space; determining whether there is a target initial identifier sequence among the M undetermined candidate identifier sequences, where the target initial identifier sequence is an initial identifier sequence containing a sequence termination identifier, and the sequence termination identifier indicates that the prediction of the target initial identifier sequence has ended; if there is a target initial identifier sequence among the M undetermined candidate identifier sequences, updating the target score of the target initial identifier sequence to a preset score; selecting N undetermined candidate identifier sequences from the M undetermined candidate identifier sequences according to the updated target score, to obtain N candidate identifier sequences.
[0010] Furthermore, the text prediction method also includes: saving the target score of the initial target identifier sequence to the target storage area before updating the target score of the initial target identifier sequence to a preset score; wherein, determining the target text sequence based on the candidate identifier sequences and the target scores of the candidate identifier sequences in the current step includes: selecting the largest target score from the target scores of each candidate identifier sequence in the current step and the target scores in the target storage area, and determining the candidate identifier sequence corresponding to the largest target score as the target identifier sequence; and deserializing the target identifier sequence according to the preset mapping relationship between characters and identifiers to obtain the target text sequence.
[0011] Furthermore, the preset conditions are one of the following: there exists a candidate identifier sequence in the current step with a sequence length greater than the preset sequence length; the target score of each candidate identifier sequence in the current step is less than the target score already stored in the target storage area in the previous step.
[0012] According to another aspect of the present invention, a text prediction method is also provided, comprising: obtaining a target sentence to be translated; inputting the target sentence into a target prediction model, predicting the translation sentence corresponding to the target sentence through the target prediction model, and obtaining a target text sequence, wherein the target prediction model is used to predict the translation sentence step by step, in each step of prediction there are multiple initial identifier sequences, determining the target score of the initial identifier sequence based on the sequence length and target probability of the initial identifier sequence, and determining the target text sequence from the multiple initial identifier sequences based on the target score, wherein the target score is positively correlated with the sequence length, and the target probability characterizes the probability that the text sequence matched by the initial identifier sequence is the translation sentence.
[0013] According to another aspect of the present invention, a text prediction apparatus is also provided, comprising: a first acquisition module for acquiring a target image, wherein the target image includes multiple characters; and a first prediction module for inputting the target image into a target prediction model, predicting multiple characters in the target image through the target prediction model to obtain a target text sequence, wherein the target prediction model is used to predict multiple characters step by step, and in each step of prediction there are multiple initial identifier sequences, a target score of the initial identifier sequence is determined according to the sequence length and target probability of the initial identifier sequence, and a target text sequence is determined from the multiple initial identifier sequences according to the target score, wherein the target score is positively correlated with the sequence length, and the target probability characterizes the probability that the text sequence matched by the initial identifier sequence is a text sequence composed of multiple characters.
[0014] According to another aspect of the present invention, a text prediction apparatus is also provided, comprising: a second acquisition module for acquiring a target sentence to be translated; and a second prediction module for inputting the target sentence into a target prediction model, predicting the translation sentence corresponding to the target sentence through the target prediction model to obtain a target text sequence, wherein the target prediction model is used to predict the translation sentence step by step, in each step of prediction there are multiple initial identifier sequences, the target score of the initial identifier sequence is determined according to the sequence length and target probability of the initial identifier sequence, and the target text sequence is determined from the multiple initial identifier sequences according to the target score, wherein the target score is positively correlated with the sequence length, and the target probability characterizes the probability that the text sequence matched by the initial identifier sequence is the translation sentence.
[0015] According to another aspect of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored in the computer-readable storage medium, and the computer program is configured to execute the above-described text prediction method at runtime.
[0016] According to another aspect of the present invention, an electronic device is also provided, the electronic device including one or more processors; a memory for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors are configured to run the programs, wherein the programs are configured to execute the above-described text prediction method at runtime.
[0017] In this embodiment of the invention, a method is adopted to control the target score of the initial identifier sequence by combining the sequence length during the prediction process. The target image is acquired and then input into the target prediction model. The target prediction model predicts multiple characters in the target image to obtain the target text sequence. The target image includes multiple characters. The target prediction model is used to predict multiple characters step by step. There are multiple initial identifier sequences in each prediction step. The target score of the initial identifier sequence is determined according to the sequence length and target probability of the initial identifier sequence. The target text sequence is determined from the multiple initial identifier sequences according to the target score. The target score is positively correlated with the sequence length. The target probability represents the probability that the text sequence matched by the initial identifier sequence is a text sequence composed of multiple characters.
[0018] It is noteworthy that in the above process, by determining the target score of the initial label sequence based on the sequence length and target probability of the initial label sequence, and making the target score positively correlated with the sequence length, it is ensured that the shorter the sequence length, the greater the penalty on the target score. This can effectively suppress the phenomenon of short sequence scores being too high during the prediction process, thereby avoiding premature termination of the prediction process and effectively improving the accuracy of the prediction.
[0019] Therefore, the solution provided in this application achieves the goal of controlling the target score of the initial identifier sequence by combining the sequence length during the prediction process, thereby improving the technical effect of prediction accuracy and solving the technical problem of low prediction accuracy when using the beam search algorithm for text prediction in related technologies. Attached Figure Description
[0020] The accompanying drawings, which are included to provide a further understanding of the invention and form part of this application, illustrate exemplary embodiments of the invention and, together with their description, serve to explain the invention and do not constitute an undue limitation thereof. In the drawings:
[0021] Figure 1 This is a schematic diagram of an optional text prediction method according to an embodiment of the present invention;
[0022] Figure 2 This is a schematic diagram of an optional candidate identifier sequence according to an embodiment of the present invention;
[0023] Figure 3 This is a schematic diagram of the operation of an optional decoder according to an embodiment of the present invention;
[0024] Figure 4 This is a schematic diagram of an optional text prediction method according to an embodiment of the present invention;
[0025] Figure 5 This is a schematic diagram of an optional text prediction device according to an embodiment of the present invention;
[0026] Figure 6 This is a schematic diagram of an optional text prediction device according to an embodiment of the present invention;
[0027] Figure 7 This is a schematic diagram of an optional electronic device according to an embodiment of the present invention. Detailed Implementation
[0028] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0029] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0030] For ease of description, the following explains some of the nouns or terms used in the embodiments of this application:
[0031] Beam Search: A probabilistic heuristic search algorithm used in the inference phase of sequence prediction models. Compared to greedy algorithms (which take the maximum probability at each prediction step), it considers a larger probability space at each step, resulting in a higher overall confidence level for the generated sequences.
[0032] A dictionary is a (k, v) storage structure that maps IDs to characters, where the key is an integer and the value is a character. It can be used to look up characters based on input integer IDs using a hash function. In Chinese sequence prediction scenarios, all Chinese characters and punctuation marks are typically converted into integer IDs in a specific order, and a dictionary is built based on these IDs and the corresponding Chinese characters. The length (size) of the dictionary is the sum of the number of Chinese characters and the number of punctuation marks.
[0033] 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, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use and processing of the relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and corresponding operation portals are provided for users to choose to authorize or refuse.
[0034] Example 1
[0035] According to an embodiment of the present invention, an embodiment of a text prediction method is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0036] The text prediction method provided in this application improves the traditional beam search algorithm to enhance prediction accuracy. In this embodiment, the text prediction method is applied to an OCR scenario. Figure 1 This is a schematic diagram of an optional text prediction method according to an embodiment of the present invention, such as... Figure 1 As shown, the method includes the following steps:
[0037] Step S101: Obtain the target image, wherein the target image includes multiple characters.
[0038] Optionally, electronic devices, application systems, servers, or other similar devices can be used as the execution subject. In this embodiment, the target prediction system is used as the execution subject. The target image is the image to be OCR recognized, and the aforementioned characters include, but are not limited to, text, letters, and punctuation marks.
[0039] Step S102: Input the target image into the target prediction model, and use the target prediction model to predict multiple characters in the target image to obtain the target text sequence. The target prediction model is used to predict multiple characters in steps. In each step of prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined according to the sequence length and target probability. The target text sequence is determined from multiple initial identifier sequences according to the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is a text sequence composed of multiple characters.
[0040] The target prediction model is a prediction model using an improved beam search algorithm. This prediction model can be a transformer model or a large model of a transformer variant, or it can be a sequence prediction model based on a recurrent neural network (RNN), a text generation model based on a transformer decoder, or a model that generates text from an image (such as transformer OCR), etc. In this embodiment, a prediction model containing both an encoder and a decoder is used as an example to illustrate the text prediction method. Optionally, in other embodiments, the target prediction model may only include a decoder; for example, the target prediction model may be a text generation model using an improved beam search algorithm, etc.
[0041] In step S102, the target prediction model is a pre-trained model. The target prediction system inputs the target image into the target prediction model. The encoder in the target prediction model acquires the aforementioned target image and performs encoding operations on the target image to obtain a target tensor E. The shape of the target tensor E is (batch_size, seq_len, dim), where batch_size is the batch size for prediction, i.e., batch_size sequences are predicted at once, seq_len is the length of the sequence input to the encoder, and dim is the feature dimension. Then, the aforementioned target tensor E and initial values are input to the decoder so that the decoder predicts multiple characters in the target image based on the aforementioned target tensor E and initial values to obtain the target text sequence. The aforementioned initial values are integer tensors with shape (batch_size, beam_size, 1) and value bos_id, used to represent the start of the sequence. beam_size is the size of the beam search space, i.e., each sequence buffers beam_size channels at each step, and the sequence scores of these beam_size channels are the largest of the top beam_size channels up to the current step.
[0042] In related technologies, the target prediction model converts all the characters it has recognized into integer IDs in a certain order and constructs a dictionary based on these integer IDs. These integer IDs are also known as identifiers. During the prediction process, the target prediction model processes data based on the identifiers, and at the end of the prediction, it restores the identifiers to characters based on the dictionary. Specifically, in related technologies, during the process of the decoder predicting multiple characters in the target image based on the aforementioned target tensor E and initial values to obtain the target text sequence, the decoder predicts multiple characters step-by-step, and these predictions are not synchronized for characters at different positions. For example, in the first step, the character at the first position is predicted; in the second step, the character at the second position is predicted; and in the i-th step, the character at the i-th position is predicted. Each prediction step contains multiple initial identifier sequences. The initial identifier sequence consists of the identifiers matched by characters predicted in all previous steps and the identifiers matched by characters predicted in the current step. That is, as the number of steps increases, the length of the initial identifier sequence also increases. For example, in the first prediction step, the sequence length is 1; in the second prediction step, the sequence length is 2; and in the i-th prediction step, the sequence length is i. The decoder predicts the target probability for each initial identifier sequence and uses this target probability directly as the target score for the initial identifier sequence to determine the target text sequence from multiple initial identifier sequences. The target probability is obtained by multiplying the probability corresponding to each identifier in the initial identifier sequence. However, since the actual probability is less than 1 in the calculation process, multiplying multiple values less than 1 can cause data underflow in the target probability. That is, the shorter the initial identifier sequence, the higher its target score, which can lead to the prediction process terminating prematurely and potentially failing to reach a better solution.
[0043] In this embodiment, in each prediction step, for each initial identifier sequence, after determining the target probability of the initial identifier sequence, the target prediction model can multiply the sequence length of the initial identifier sequence by the target probability to obtain the target score of the initial identifier sequence, or it can add the sequence length of the initial identifier sequence to the target probability to obtain the target score of the initial identifier sequence. Optionally, the target prediction model can also determine a penalty coefficient based on the sequence length of the initial identifier sequence, and make this penalty coefficient gradually and smoothly decrease as the sequence grows (i.e., as the number of steps increases), thereby multiplying the penalty coefficient by the logarithm of the target probability to determine the target score of the initial identifier sequence.
[0044] It should be noted that by determining the target score of the initial label sequence based on its sequence length and target probability, and by ensuring that the target score is positively correlated with the sequence length, the penalty for the target score is greater when the sequence length is shorter. This effectively suppresses the phenomenon of short sequences scoring too high during the prediction process, thereby preventing the prediction process from terminating prematurely and thus effectively improving the accuracy of the prediction.
[0045] Based on the scheme defined in steps S101 to S102 above, it can be understood that in this embodiment of the invention, the target score of the initial identifier sequence is controlled by combining the sequence length during the prediction process. By acquiring a target image and then inputting the target image into a target prediction model, the target prediction model predicts multiple characters in the target image to obtain a target text sequence. The target image includes multiple characters, and the target prediction model is used to predict multiple characters step by step. In each step of the prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined according to the sequence length and target probability. The target text sequence is determined from the multiple initial identifier sequences according to the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is a text sequence composed of multiple characters.
[0046] It is noteworthy that in the above process, by determining the target score of the initial label sequence based on the sequence length and target probability of the initial label sequence, and making the target score positively correlated with the sequence length, it is ensured that the shorter the sequence length, the greater the penalty on the target score. This can effectively suppress the phenomenon of short sequence scores being too high during the prediction process, thereby avoiding premature termination of the prediction process and effectively improving the accuracy of the prediction.
[0047] Therefore, the solution provided in this application achieves the goal of controlling the target score of the initial identifier sequence by combining the sequence length during the prediction process, thereby improving the technical effect of prediction accuracy and solving the technical problem of low prediction accuracy when using the beam search algorithm for text prediction in related technologies.
[0048] In an optional embodiment, during the process of predicting multiple characters in a target image using a target prediction model to obtain a target text sequence, the target prediction model can determine multiple initial identifier sequences in each prediction step. For each initial identifier sequence in the current step, a target score is determined based on the sequence length and target probability of the initial identifier sequence. Then, multiple candidate identifier sequences are selected from the multiple initial identifier sequences in the current step based on the target score. Thus, if the candidate identifier sequences in the current step meet preset conditions, the target text sequence is determined based on the candidate identifier sequences in the current step and their target scores. The candidate identifier sequences are used to determine the initial identifier sequences for the next prediction step.
[0049] Optionally, when the current step is the first step, the object detection model takes one identifier from the dictionary as a sequence to obtain multiple initial identifier sequences for the first step. For example, if the dictionary contains "I", "you", "he", and "good", then the first step has four initial identifier sequences, and the corresponding Chinese sequences are "Sequence 1: I", "Sequence 2: you", "Sequence 3: he", and "Sequence 4: is".
[0050] Furthermore, the target prediction model can determine the target score of each initial identifier sequence based on its sequence length and target probability in the first step. Then, the target prediction model can determine the number of candidate identifier sequences to be selected based on the aforementioned beam_size, and select the initial identifier sequences corresponding to the first beam_size target scores as candidate identifier sequences for the first step.
[0051] Furthermore, given that the current step is step i, the target prediction model obtains multiple candidate identifier sequences for step (i-1). For each candidate identifier sequence in step (i-1), the candidate identifier sequence of step i-1 is combined with each identifier in the dictionary to obtain multiple initial identifier sequences for step i, where i is a positive integer greater than 1. For example, if i is 2, and the Chinese sequences corresponding to the candidate identifier sequences selected in step 1 are "Sequence 1: I", "Sequence 2: You", and "Sequence 3: He", then in step 2, "Sequence 1: I" from step 1 is combined with the identifiers corresponding to "I", "You", "He", and "Good" respectively to obtain "Sequence 1: I I", "Sequence 2: I You", "Sequence 3: I He", and "Sequence 4: I Good" in step 2. Similarly, in step 2, "Sequence 2: You" and "Sequence 3: He" from step 1 are also combined with "I", "You", "He", and "Good" respectively, so that a total of 12 initial identifier sequences can be obtained in step 2.
[0052] Furthermore, the target prediction model determines the target score for each initial identifier sequence in step 2, and selects candidate identifier sequences in step 2 based on the target scores.
[0053] For example, Figure 2 This is a schematic diagram of an optional method for determining a candidate identifier sequence according to an embodiment of the present invention, such as... Figure 2 As shown, the dictionary includes "A", "B", "C", "D", and "E", with a beam size of 2. The two candidate identifier sequences selected in the first step correspond to "A" and "C" respectively. Then, as... Figure 2 As shown, in the second step, the candidate identifier sequences from the first step are combined with each identifier in the dictionary, and two candidate identifier sequences are obtained in the second step. The two candidate identifier sequences in the second step correspond to "AB" and "CE" respectively.
[0054] Optionally, the target prediction model continues to perform step-by-step predictions until the candidate identifier sequence in the current step meets the preset conditions. Then, the target text sequence is determined based on the candidate identifier sequence in the current step and the target score of the candidate identifier sequence. The target prediction model can determine the target text sequence based solely on the candidate identifier sequence and the target score of the candidate identifier sequence in the current step, or it can combine the target scores of the candidate identifier sequences from previous steps to determine the target text sequence.
[0055] It should be noted that by filtering the initial identifier sequence identifiers through the target score in each prediction step, the computational load is effectively reduced, thereby improving prediction efficiency.
[0056] In an optional embodiment, during the process of determining the target score of the initial identifier sequence based on its sequence length and target probability, the target prediction model, when the current step is step i, determines the logarithm of the target probability of the initial identifier sequence based on the target scores of the candidate identifier sequences that make up the initial identifier sequence in step (i-1). Then, it takes the logarithm of the sequence length of the initial identifier sequence to obtain a first value, and determines the target coefficient based on the first value. Finally, it calculates the product of the logarithm of the target probability of the initial identifier sequence and the target coefficient to obtain the target score of the initial identifier sequence. Here, i is a positive integer greater than 1, and the target coefficient is negatively correlated with the first value.
[0057] Optionally, the process of determining the target score of the initial identifier sequence is first explained when the current step is the first step. In the first step of prediction, the encoder in the target prediction model obtains the target tensor E and the sequence start identifier, where the sequence start identifier is the aforementioned bos_id. The target tensor E is multiplied by two different weight matrices to obtain the static key and static value. The bos_id is multiplied by two different weight matrices to obtain the dynamic key and dynamic value in the first step. The static key, static value, dynamic key, and dynamic value are intermediate variables obtained by the decoder during the calculation process, and the decoder can cache these intermediate variables. The static key and static value are used by the decoder to implement the cross-attention mechanism, while the dynamic key and dynamic value are used by the decoder to implement the self-attention mechanism. The static key and static value do not change in subsequent calculations, while the dynamic key and dynamic value are adjusted as the sequence is adjusted and gradually expanded as the sequence grows.
[0058] Furthermore, the decoder can determine the probability that the character corresponding to each identifier in the first step is the character at the first character position in the target image based on the static variables and the dynamic variables from the first step, thereby obtaining the target probability of each initial identifier sequence, and determining the target score based on the target probability. Since each initial identifier sequence in the first step consists of one identifier, the identifier probability of the identifier contained in each initial identifier sequence is the target probability of that initial identifier sequence.
[0059] Optionally, the process of determining the target score of the initial identifier sequence when the current step is step i is described. First, for each initial identifier sequence in step i, the dynamic variable corresponding to the initial identifier sequence in step i can be determined according to the following formula:
[0060] key i =concat([key i-1 Δkey i ], axis=1)
[0061] First, a target candidate identifier sequence is defined, which is the candidate identifier sequence of the (i-1)th step that makes up the initial identifier sequence of the i-th step. i This represents the dynamic key that matches the initial identifier sequence in step i. `concat()` indicates the concatenation process. i-1 Δkey represents the dynamic key for matching the target candidate identifier sequence. i This represents the predicted value y at step i-1 based on the target candidate identifier sequence.i-1 The calculated key value of the Self-Attention part, axis=1 indicates that the key... i-1 and Δkey i Generate key by concatenating according to the second axis. i Wherein, the predicted value y of the target candidate identifier sequence at step i-1. i-1 That is, the (i-1)th identifier in the target candidate identifier sequence.
[0062] value i =concat([value]) i-1 Δvalue i ], axis=1)
[0063] Where, value i This represents the dynamic value matched in step i of the initial identifier sequence. `concat()` indicates the concatenation process. i-1 The dynamic value, Δvalue, represents the matching of the target candidate identifier sequence. i This represents the predicted value y at step i-1 based on the target candidate identifier sequence. i-1 The calculated value of the Self-Attention part, axis=1 indicates that the value is... i-1 and Δvalue i Generate value by concatenating along the second axis. i .
[0064] It should be noted that Δkey i and Δvalue i The shape is (batch_size*beam_size, 1, dim), key i and value i The shape is (batch_size * beam_size, i, dim). For different initial identifier sequences in step i, there are different keys. i and value i Furthermore, in the first step, since the previous step does not exist, therefore, the key... i =Δkey i value i =Δvalue i .
[0065] Furthermore, after determining the dynamic variables in step i, the decoder can determine the probability that the character corresponding to each target identifier in step i is the character at the i-th character position in the target image based on the static variables and the dynamic variables in step i. Here, the target identifier is the identifier that is combined with the aforementioned target candidate identifier sequence to form the initial identifier sequence in step i. The aforementioned identifier probability can be determined based on the static variables and the dynamic variables in step i according to the methods provided in related technologies, so it will not be elaborated here.
[0066] Furthermore, for each initial identifier sequence in step i, the encoder can determine the target score of the initial identifier sequence based on the following formula:
[0067]
[0068] Among them, P i p represents the target score of the initial identifier sequence in step i. i Let P represent the identification probability of the target identifier in step i. i-1 Let ln(i) represent the target score of the candidate identifier sequence in the (i-1)th step, which constitutes the initial identifier sequence in the i-th step. In other words, ln(i) represents the target score of the target candidate identifier sequence. Let (ln(i-1)+1) represent the target coefficient, and (ln(p) represent the reciprocal of the target coefficient at step (i-1). i )+P i-1 (ln(i-1)+1)) is equivalent to the logarithm of the target probability of the initial identifier sequence in step i. It should be noted that in the first step, P... i-1 (ln(i-1)+1)=0.
[0069] It should be noted that since the target probability is a value greater than or equal to 0 and less than or equal to 1, the logarithm of the target probability is negative. This means that as the sequence length increases, the first value increases, the target coefficient decreases, and the target score increases. This effectively penalizes the target score based on the sequence length, thus improving prediction accuracy.
[0070] In an optional embodiment, during the process of selecting multiple candidate identifier sequences from multiple initial identifier sequences in the current step based on the target score, the target prediction model can, when the current step is step i, sort the multiple initial identifier sequences in step i according to the target score from high to low, obtaining multiple sorted initial identifier sequences. Then, it selects the first M initial identifier sequences from the sorted initial identifier sequences to obtain M pending candidate identifier sequences. Next, it determines whether there is a target initial identifier sequence among the M pending candidate identifier sequences. If there is a target initial identifier sequence among the M pending candidate identifier sequences, it updates the target score of the target initial identifier sequence to a preset score, thereby selecting N pending candidate identifier sequences from the M pending candidate identifier sequences according to the updated target score, obtaining N candidate identifier sequences. Here, i is a positive integer greater than 1, M is a positive integer greater than N, N represents the size of the beam search space, and the target initial identifier sequence is an initial identifier sequence containing a sequence termination identifier, which indicates the end of the prediction of the target initial identifier sequence.
[0071] The aforementioned sequence termination identifier also exists in the dictionary. Optionally, in step i, the decoder can select the top M initial identifier sequences with the highest scores from the multiple initial identifier sequences in step i based on the target score, resulting in M candidate identifier sequences. Here, N is the aforementioned beam_size, and M is a positive integer greater than N. In this embodiment, M is beam_size+1. Afterwards, since there are dictionary-size-multiplied initial identifier sequences in step i (meaning each value in the dictionary is repeated beam_size times), when selecting the top beam_size initial identifier sequences according to the target score, it is possible that the i-th identifier of these beam_size initial identifier sequences is the sequence termination identifier. In this case, all beam_size channels will terminate and prediction will stop. Therefore, in this embodiment, the top beam_size+1 initial identifier sequences are selected first, ensuring that the i-th identifier of at least one initial identifier sequence is not the sequence termination identifier, thus ensuring that the sequence continues to predict to try to obtain a better solution. The aforementioned sequence termination identifier is eos_id.
[0072] Optionally, after obtaining M candidate identifier sequences, the decoder can determine whether the target initial identifier sequence exists among all candidate identifier sequences. If it does, it means that the prediction of the target initial identifier sequence has ended, and its corresponding target score can be updated to a preset score. The preset score is a score that is less than the lower limit of the target score that can be calculated. In this embodiment, the preset score can be negative infinity.
[0073] Furthermore, the decoder can select the top N candidate identifier sequences with the highest scores from the M candidate identifier sequences based on the updated target score, thus obtaining N candidate identifier sequences.
[0074] Optionally, if none of the candidate identifier sequences contain a sequence termination identifier, the top N candidate identifier sequences with the highest scores can be directly selected based on the target score, resulting in N candidate identifier sequences. The identifier newly added to a candidate identifier sequence in the current step is the predicted value y of that candidate identifier sequence in the current step. i .
[0075] Optionally, if the current step is the first step, since there are only dictionary-sized initial identifier sequences, the first N initial identifier sequences can be directly selected from the sorted initial identifier sequences to obtain N candidate identifier sequences, and the target score of the target initial identifier sequence in the N candidate identifier sequences can be updated to the preset score.
[0076] It should be noted that by reserving MN channels during the search in each step of the bundle space, the sequence is guaranteed not to terminate in all channels during the subsequent prediction process, thereby further improving the prediction accuracy and efficiency.
[0077] In an optional embodiment, before updating the target score of the initial target identifier sequence to a preset score, the target prediction model can save the target score of the initial target identifier sequence to the target storage area. In the process of determining the target text sequence based on the candidate identifier sequences and their target scores in the current step, the target prediction model can select the largest target score from the target scores of each candidate identifier sequence in the current step and the target scores in the target storage area, and determine the candidate identifier sequence corresponding to the largest target score as the target identifier sequence. Then, based on the preset mapping relationship between characters and identifiers, the target identifier sequence is deserialized to obtain the target text sequence.
[0078] Optionally, for each prediction step, before updating the target score of the current step's initial target identifier sequence to the preset score, the decoder can temporarily store the target score of the initial target identifier sequence in the target storage area P. end Furthermore, in each prediction step, the target storage region P is... end The target scores are filtered, and only the top beam_size target scores with the highest scores are retained, along with their corresponding initial target identifier sequences.
[0079] Furthermore, if the candidate identifier sequence in the current step meets the preset conditions, the decoder can assign the target score and target storage area P to each candidate identifier sequence in the current step.end The target scores are compared, and the candidate identifier sequence corresponding to the highest target score is determined as the target identifier sequence.
[0080] Furthermore, the decoder can deserialize the target identifier sequence based on the mapping relationship between characters and identifiers in the dictionary to restore the target text sequence composed of characters, where the target text sequence is the prediction result of the target prediction model.
[0081] It should be noted that by selecting the target identifier sequence based on the target score in the target storage area and the target score in the current step, the target identifier sequence can be determined more accurately, thereby further improving the prediction accuracy.
[0082] In one optional embodiment, the preset condition is one of the following: there exists a candidate identifier sequence in the current step with a sequence length greater than a preset sequence length; the target score of each candidate identifier sequence in the current step is less than the target score already stored in the target storage area in the previous step.
[0083] That is, prediction stops when the length i of the sequence predicted in the current step exceeds a pre-set threshold, or when, up to the current step, the target scores of all batch_size*beam_size sequences are less than the target storage area P. end The target score has already been stored in the previous prediction.
[0084] It should be noted that, since the target text sequence depends on the target score, enriching the conditions for stopping prediction in the target prediction model by incorporating the target score avoids invalid prediction steps and thus improves prediction efficiency.
[0085] Optionally, in this embodiment, during the training process, an initial prediction model can be constructed. Then, the training image is used as input to the encoder in the initial prediction model, and the tensor `e` and `eos_id` output by the encoder after processing the training image are used as input to the decoder in the initial prediction model to obtain the prediction result output by the decoder. Specifically, the real text sequence and `eos_id` in the training image are used as labels to train the initial prediction model, thereby obtaining the target prediction model.
[0086] In one optional embodiment, taking the Transformer OCR model in an OCR scenario as an example, an application process of this application will be described. Figure 3 This is a schematic diagram of the operation of an optional decoder according to an embodiment of the present invention, such as... Figure 3As shown, during the prediction process of the target prediction model, the encoder obtains the picture data (i.e., the aforementioned target image), and the decoder obtains the feature vector output by the encoder. In the first-step prediction, based on this feature vector and the sequence start identifier bos_id, an N-dimensional (dictionary size) vector is output. The N-dimensional vector is used to represent the identification probabilities of each identifier in the dictionary at the current step. Then, the beam search algorithm selects the first beam_size identifiers from the N-dimensional vector as the candidate identifier sequences in the first step. For example, beam_size is 3, and assume the candidate identifier sequences in the first step are {"中", "申", "甲"}, and the logarithms of the corresponding target probabilities are {-0.1, -0.12, -0.13}. Then, the decoder performs a secondary screening on the candidate identifier sequences according to whether there is a sequence termination identifier. If there is no candidate identifier sequence containing the sequence termination identifier, the next step of prediction is carried out. If there is a candidate identifier sequence containing the sequence termination identifier, the sequence and the corresponding target score are temporarily stored and the probability is set to negative infinity. In the next step, according to the feature vector output by the encoder, bos_id, and the beam_size candidate identifier sequences predicted in the first step, the beam_size + 1 pending candidate identifier sequences in the second step are predicted. For example, the pending candidate identifier sequences predicted in the second step are respectively:
[0087] {"中EOS", "申EOS", "甲EOS", "中国"}
[0088] Then the logarithms of the corresponding target probabilities are:
[0089] {-0.115, -0.116, -0.119, -0.125}
[0090] The logarithms of the corresponding target probabilities with penalty (divided by log(2)+1) are:
[0091] {-0.0682, -0.685, -0.070, -0.074}
[0092] Since the termination character "EOS" is encountered in the first three channels, therefore, if only the first beam_size pending candidate identifier sequences are taken, then the finally selected prediction sequence is "中" with the maximum probability of -0.115. As Figure 3As shown, but if the first beam_size + 1 undetermined candidate identification sequences are taken, and the logarithm of the penalized target probability is used as the score, that is, the target score is used as the judgment criterion, then after the undetermined candidate identification sequences are secondarily screened according to whether there is a sequence termination identifier, {"中EOS", "申EOS", "甲EOS"}, these three sequences will be temporarily stored, and the corresponding probability sums {-0.0682, -0.685, -0.070} will also be temporarily stored, while the sequence "中国" will continue to be predicted downward. And so on, finally the decoder will predict the candidate identification sequence of the current step based on the feature vector output by the encoder and the candidate identification sequence ("中国") of the previous step. Suppose the first beam_size + 1 candidate identification sequences predicted at the i-th step are:
[0093] {"中国人", "中国入", "中国八", "中国大"}
[0094] Then the logarithm of the corresponding target probability is:
[0095] {-0.136, -0.231, -0.348, -0.482}
[0096] The logarithm of the corresponding penalized (divided by (log(3)+1)) target probability is:
[0097] {-0.0648, -0.1100, -0.1658, -0.2297}
[0098] It can be found at this time that if the logarithm of the penalized target probability is selected as the scoring criterion, then "中国人" will be selected as the current best sequence, because its probability sum of -0.0648 is greater than the probability sum of -0.0682 of "中EOS" that has stopped in the previous step. If the logarithm of the target probability is used as the scoring criterion, then the model will output the stopped sequence "中", and its score of -0.115 is greater than the current highest score of -0.136. <00,00261>In related technologies, the earlier the prediction ends, the greater the logarithm of the target probability (because it is a value that decreases as the sequence grows), and the model will only select the one with the highest score as the final predicted output, resulting in the beam search algorithm predicting the end prematurely. Therefore, it can be found that selecting the logarithm of the target probability with a penalty of 1 / (log(i)+1) as the sequence scoring criterion, and selecting the first beam_size + 1 undetermined candidate identification sequences at each intermediate step can effectively prevent the original beam search algorithm from predicting the end prematurely. Further, as Figure 3As shown, prediction can continue until the termination prediction condition is met. The decoder will output the beam_size candidate identifier sequences of the current step, and compare its first candidate identifier sequence with the target score of the stored sequences that have terminated early. The sequence with the highest score is then used as the target identifier sequence, and the target identifier sequence is restored to the target text sequence according to the dictionary.
[0100] In an alternative embodiment, if the target prediction model does not have an encoder, such as when the target prediction model is used for text generation tasks, the aforementioned bos_id can be directly input into the decoder, or a sequence of identifiers and bos_id can be input.
[0101] Therefore, the solution provided in this application achieves the goal of controlling the target score of the initial identifier sequence by combining the sequence length during the prediction process, thereby improving the technical effect of prediction accuracy and solving the technical problem of low prediction accuracy when using the beam search algorithm for text prediction in related technologies.
[0102] Example 2
[0103] According to an embodiment of the present invention, an embodiment of a text prediction method is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0104] In this embodiment, the text prediction method is applied to a text translation scenario. Figure 4 This is a schematic diagram of an optional text prediction method according to an embodiment of the present invention, such as... Figure 4 As shown, the method includes the following steps:
[0105] Step S401: Obtain the target sentence to be translated.
[0106] Optionally, electronic devices, application systems, servers, or other similar devices can be used as the execution subject. In this embodiment, the target prediction system is used as the execution subject. The target statement consists of characters, including but not limited to letters, punctuation marks, and text.
[0107] Step S402: Input the target statement into the target prediction model, and predict the corresponding translation statement through the target prediction model to obtain the target text sequence. The target prediction model is used to predict the translation statement in steps. In each step of the prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined based on the sequence length and target probability. The target text sequence is determined from the multiple initial identifier sequences based on the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is the translation statement.
[0108] In this embodiment, a text prediction method is described using a target prediction model that includes an encoder and a decoder as an example. The encoder in the target prediction model acquires the target statement, encodes it to obtain a target tensor E, and then inputs the target tensor E and initial values into the decoder. The decoder then predicts the corresponding translation statement based on the target tensor E and the initial values, thus obtaining the target text sequence. The decoder's sequence prediction process is the same as that in Embodiment 1, and therefore will not be repeated here.
[0109] Based on the scheme defined in steps S401 to S402 above, it can be understood that in this embodiment of the invention, the target score of the initial identifier sequence is controlled by combining the sequence length during the prediction process. The target sentence to be translated is obtained, and then the target sentence is input into the target prediction model. The target prediction model predicts the translation sentence corresponding to the target sentence to obtain the target text sequence. The target prediction model is used to predict the translation sentence step by step. In each step of the prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined according to the sequence length and target probability. The target text sequence is determined from multiple initial identifier sequences according to the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is the translation sentence.
[0110] It is noteworthy that in the above process, by determining the target score of the initial label sequence based on the sequence length and target probability of the initial label sequence, and making the target score positively correlated with the sequence length, it is ensured that the shorter the sequence length, the greater the penalty on the target score. This can effectively suppress the phenomenon of short sequence scores being too high during the prediction process, thereby avoiding premature termination of the prediction process and effectively improving the accuracy of the prediction.
[0111] Therefore, the solution provided in this application achieves the goal of controlling the target score of the initial identifier sequence by combining the sequence length during the prediction process, thereby improving the technical effect of prediction accuracy and solving the technical problem of low prediction accuracy when using the beam search algorithm for text prediction in related technologies.
[0112] Example 3
[0113] According to an embodiment of the present invention, a text prediction device is provided, wherein, Figure 5 This is a schematic diagram of an optional text prediction device according to an embodiment of the present invention, such as... Figure 5 As shown, the device includes:
[0114] The first acquisition module 501 is used to acquire a target image, wherein the target image includes multiple characters;
[0115] The first prediction module 502 is used to input the target image into the target prediction model, and predict multiple characters in the target image through the target prediction model to obtain the target text sequence. The target prediction model is used to predict multiple characters step by step. In each step of prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined according to the sequence length and target probability. The target text sequence is determined from multiple initial identifier sequences according to the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is a text sequence composed of multiple characters.
[0116] It should be noted that the first acquisition module 501 and the first prediction module 502 mentioned above correspond to steps S101 to S102 in the above embodiments. The two modules and the corresponding steps implement the same examples and application scenarios, but are not limited to the content disclosed in the above embodiment 1.
[0117] Optionally, the first prediction module 502 further includes: a first determining submodule, used to determine multiple initial identifier sequences for the current step in each prediction step, and for each initial identifier sequence in the current step, determine a target score for the initial identifier sequence based on the sequence length and target probability of the initial identifier sequence; a filtering submodule, used to filter multiple candidate identifier sequences from the multiple initial identifier sequences in the current step based on the target score, wherein the candidate identifier sequences are used to determine the initial identifier sequences in the next prediction step; and a second determining submodule, used to determine a target text sequence based on the candidate identifier sequences in the current step and the target scores of the candidate identifier sequences, provided that the candidate identifier sequences in the current step meet preset conditions.
[0118] Optionally, the first determining submodule further includes: a first determining unit, configured to determine the logarithm of the target probability of the initial identifier sequence based on the target scores of the candidate identifier sequences that make up the initial identifier sequence in the case that the current step is the i-th step, where i is a positive integer greater than 1; a second determining unit, configured to take the logarithm of the sequence length of the initial identifier sequence to obtain a first value, and determine the target coefficient based on the first value, where the target coefficient is negatively correlated with the first value; and a calculation unit, configured to calculate the product of the logarithm of the target probability of the initial identifier sequence and the target coefficient to obtain the target score of the initial identifier sequence.
[0119] Optionally, the filtering submodule further includes: a sorting unit, used to sort the multiple initial identifier sequences of the i-th step according to the target score from high to low when the current step is the i-th step, to obtain multiple sorted initial identifier sequences, where i is a positive integer greater than 1; a first filtering unit, used to filter out the first M initial identifier sequences from the sorted multiple initial identifier sequences to obtain M undetermined candidate identifier sequences, where M is a positive integer greater than N, and N represents the size of the beam search space; a third determining unit, used to determine whether there is a target initial identifier sequence among the M undetermined candidate identifier sequences, where the target initial identifier sequence is an initial identifier sequence containing a sequence termination identifier, and the sequence termination identifier indicates that the prediction of the target initial identifier sequence has ended; an updating unit, used to update the target score of the target initial identifier sequence to a preset score when there is a target initial identifier sequence among the M undetermined candidate identifier sequences; and a second filtering unit, used to filter out N undetermined candidate identifier sequences from the M undetermined candidate identifier sequences according to the updated target score, to obtain N candidate identifier sequences.
[0120] Optionally, the text prediction device further includes: a saving module for saving the target score of the initial target identifier sequence to the target storage area; wherein, the second determining submodule further includes: a third filtering unit for filtering the largest target score from the target scores of each candidate identifier sequence in the current step and the target scores in the target storage area, and determining the candidate identifier sequence corresponding to the largest target score as the target identifier sequence; and a processing unit for deserializing the target identifier sequence according to the preset mapping relationship between characters and identifiers to obtain the target text sequence.
[0121] Optionally, the preset conditions are one of the following: there is a candidate identifier sequence in the current step with a sequence length greater than the preset sequence length; the target score of each candidate identifier sequence in the current step is less than the target score already stored in the target storage area in the previous step.
[0122] Example 4
[0123] According to an embodiment of the present invention, a text prediction device is provided, wherein, Figure 6 This is a schematic diagram of an optional text prediction device according to an embodiment of the present invention, such as... Figure 6 As shown, the device includes:
[0124] The second acquisition module 601 is used to acquire the target sentence to be translated;
[0125] The second prediction module 602 is used to input the target statement into the target prediction model, predict the corresponding translation statement through the target prediction model, and obtain the target text sequence. The target prediction model is used to predict the translation statement step by step. In each step of the prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined based on the sequence length and target probability. The target text sequence is determined from the multiple initial identifier sequences based on the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is the translation statement.
[0126] It should be noted that the second acquisition module 601 and the second prediction module 602 mentioned above correspond to steps S401 to S402 in the above embodiments. The examples and application scenarios implemented by the two modules and the corresponding steps are the same, but are not limited to the content disclosed in the above embodiment 1.
[0127] Example 5
[0128] According to another aspect of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored in the computer-readable storage medium, wherein the computer program is configured to execute the above-described text prediction method at runtime.
[0129] Example 6
[0130] According to another aspect of the present invention, an electronic device is also provided, wherein, Figure 7 This is a schematic diagram of an optional electronic device according to an embodiment of the present invention, such as... Figure 7 As shown, the electronic device includes one or more processors; and a memory for storing one or more programs, which, when executed by one or more processors, enable the one or more processors to run the programs, wherein the programs are configured to execute the text prediction method described above during runtime.
[0131] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0132] In the above embodiments of the present invention, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0133] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units can be a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some interfaces; indirect couplings or communication connections between units or modules may be electrical or other forms.
[0134] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0135] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0136] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0137] The above are merely preferred embodiments of the present invention. It should be noted that those skilled in the art can make various improvements and modifications without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A text prediction method, characterized in that, include: Acquire a target image, wherein the target image includes multiple characters; The target image is input into a target prediction model, which predicts multiple characters in the target image to obtain a target text sequence. The target prediction model is used to predict the multiple characters step by step. In each step of the prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined based on the sequence length and target probability. The target text sequence is determined from the multiple initial identifier sequences based on the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is a text sequence composed of the multiple characters. The determination of the target score of the initial identifier sequence based on its sequence length and target probability includes: when the current step is the i-th step, determining the logarithm of the target probability of the initial identifier sequence based on the target scores of the candidate identifier sequences that make up the (i-1)-th step of the initial identifier sequence, where i is a positive integer greater than 1; taking the logarithm of the sequence length of the initial identifier sequence to obtain a first value, and determining a target coefficient based on the first value, where the target coefficient is negatively correlated with the first value; and calculating the product of the logarithm of the target probability of the initial identifier sequence and the target coefficient to obtain the target score of the initial identifier sequence.
2. The method according to claim 1, characterized in that, The target prediction model predicts multiple characters in the target image to obtain a target text sequence, including: In each prediction step, multiple initial identifier sequences are determined for the current step. For each initial identifier sequence in the current step, the target score of the initial identifier sequence is determined based on the sequence length of the initial identifier sequence and the target probability. Multiple candidate identifier sequences are selected from multiple initial identifier sequences in the current step based on the target score, wherein the candidate identifier sequences are used to determine the initial identifier sequences in the next prediction step; If the candidate identifier sequence in the current step meets the preset conditions, the target text sequence is determined based on the candidate identifier sequence in the current step and the target score of the candidate identifier sequence.
3. The method according to claim 2, characterized in that, Based on the target score, multiple candidate identifier sequences are selected from the multiple initial identifier sequences of the current step, including: When the current step is the i-th step, the multiple initial identifier sequences of the i-th step are sorted in descending order of the target score to obtain multiple sorted initial identifier sequences, where i is a positive integer greater than 1; The first M initial identifier sequences are selected from the sorted initial identifier sequences to obtain M candidate identifier sequences to be determined, where M is a positive integer greater than N, and N represents the size of the beam search space; Determine whether a target initial identifier sequence exists among the M undetermined candidate identifier sequences, wherein the target initial identifier sequence is an initial identifier sequence containing a sequence termination identifier, and the sequence termination identifier indicates that the prediction of the target initial identifier sequence has ended; If the target initial identifier sequence exists among the M candidate identifier sequences, the target score of the target initial identifier sequence is updated to the preset score; Based on the updated target score, N candidate identifier sequences are selected from the M candidate identifier sequences to obtain N candidate identifier sequences.
4. The method according to claim 3, characterized in that, Before updating the target score of the initial target identifier sequence to a preset score, the method further includes: Save the target score of the initial target identifier sequence to the target storage area; The determination of the target text sequence based on the candidate identifier sequence in the current step and the target score of the candidate identifier sequence includes: The maximum target score is selected from the target scores of each candidate identifier sequence in the current step and the target scores in the target storage area, and the candidate identifier sequence corresponding to the maximum target score is determined as the target identifier sequence; Based on the preset mapping relationship between characters and identifiers, the target identifier sequence is deserialized to obtain the target text sequence.
5. The method according to claim 4, characterized in that, The preset condition is one of the following: In the current step, there exists a candidate identifier sequence with a sequence length greater than a preset sequence length; The target score of each candidate identifier sequence in the current step is less than the target score that the target storage area has already stored in the previous step.
6. A text prediction method, characterized in that, include: Obtain the target sentence to be translated; The target statement is input into a target prediction model, which predicts the corresponding translation statement to obtain a target text sequence. The target prediction model is used to predict the translation statement in steps. In each step of the prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined based on the sequence length and target probability. The target text sequence is then determined from the multiple initial identifier sequences based on the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is the translation statement. The determination of the target score of the initial identifier sequence based on its sequence length and target probability includes: when the current step is the i-th step, determining the logarithm of the target probability of the initial identifier sequence based on the target scores of the candidate identifier sequences that make up the (i-1)-th step of the initial identifier sequence, where i is a positive integer greater than 1; taking the logarithm of the sequence length of the initial identifier sequence to obtain a first value, and determining a target coefficient based on the first value, where the target coefficient is negatively correlated with the first value; and calculating the product of the logarithm of the target probability of the initial identifier sequence and the target coefficient to obtain the target score of the initial identifier sequence.
7. A text prediction device, characterized in that, include: The first acquisition module is used to acquire a target image, wherein the target image includes multiple characters; The first prediction module is used to input the target image into a target prediction model, and predict multiple characters in the target image through the target prediction model to obtain a target text sequence. The target prediction model is used to predict the multiple characters step by step. In each step of prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined according to the sequence length and target probability. The target text sequence is determined from the multiple initial identifier sequences according to the target score. The target score is positively correlated with the sequence length. The target probability represents the probability that the text sequence matched by the initial identifier sequence is a text sequence composed of the multiple characters. The first prediction module includes: a first determining unit, configured to determine the logarithm of the target probability of the initial identifier sequence based on the target scores of the candidate identifier sequences constituting the initial identifier sequence at the current step of step i, wherein i is a positive integer greater than 1; a second determining unit, configured to take the logarithm of the sequence length of the initial identifier sequence to obtain a first value, and determine a target coefficient based on the first value, wherein the target coefficient is negatively correlated with the first value; and a calculation unit, configured to calculate the product of the logarithm of the target probability of the initial identifier sequence and the target coefficient to obtain the target score of the initial identifier sequence.
8. A text prediction device, characterized in that, include: The second acquisition module is used to acquire the target sentence to be translated; The second prediction module is used to input the target statement into the target prediction model, predict the corresponding translation statement through the target prediction model, and obtain the target text sequence. The target prediction model is used to predict the translation statement step by step. In each step of the prediction, there are multiple initial identifier sequences. The target score of the initial identifier sequence is determined according to the sequence length and target probability of the initial identifier sequence. The target text sequence is determined from the multiple initial identifier sequences according to the target score. The target score is positively correlated with the sequence length, and the target probability represents the probability that the text sequence matched by the initial identifier sequence is the translation statement. The determination of the target score of the initial identifier sequence based on its sequence length and target probability includes: when the current step is the i-th step, determining the logarithm of the target probability of the initial identifier sequence based on the target scores of the candidate identifier sequences that make up the (i-1)-th step of the initial identifier sequence, where i is a positive integer greater than 1; taking the logarithm of the sequence length of the initial identifier sequence to obtain a first value, and determining a target coefficient based on the first value, where the target coefficient is negatively correlated with the first value; and calculating the product of the logarithm of the target probability of the initial identifier sequence and the target coefficient to obtain the target score of the initial identifier sequence.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program is configured to execute the text prediction method according to any one of claims 1 to 6 when it is run.
10. An electronic device, characterized in that, The electronic device includes one or more processors; A memory for storing one or more programs, which, when executed by one or more processors, cause the one or more processors to be configured to run the programs, wherein the programs are configured to execute the text prediction method of any one of claims 1 to 6 at runtime.
Citation Information
Patent Citations
Text translation method and apparatus, storage medium and computer device
WO2020048195A1
Information processing method and apparatus, electronic device, and storage medium
WO2022111347A1