Deep submission message generation method based on context-aware retrieval
By using a context-aware retrieval-based deep commit message generation method, and leveraging an encoder-decoder model with attention and decay sampling mechanisms, the problem of high-frequency words ignoring low-frequency words in existing technologies is solved, resulting in more accurate and reliable code commit logs.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-11
- Publication Date
- 2026-04-14
AI Technical Summary
Existing technologies tend to favor high-frequency words in the corpus when generating code commit logs, ignoring low-frequency words and exhibiting exposure bias.
A deep commit message generation method based on context-aware retrieval is adopted. By training an encoder-decoder model based on an attention mechanism, combined with a global attention mechanism and a decay sampling mechanism, code commit logs are generated, and the historical retrieval module is used to improve the processing capability of low-frequency words.
It generates more accurate and relevant code commit logs, improving the quality and reliability of commit logs, especially in handling low-frequency words.
Smart Images

Figure CN121858142A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the generation of code change commit logs in version control systems in the field of software engineering, specifically a deep code commit log generation method based on context-aware retrieval. Background Technology
[0002] Version control systems are widely used to manage changes between versions. When committing code changes, developers can attach a commit log describing why the change was made. However, as code structures become more complex, writing high-quality commit logs becomes time-consuming and difficult for developers, who often neglect to do so.
[0003] Several methods for automatically generating code commit logs have been proposed by scholars. These methods can be broadly categorized into rule-based, retrieval-based, and learning-based methods.
[0004] Non-patent document 1 (Qin Liu, Zihe Liu, Hongming Zhu, Hongfei Fan, Bowen Du, and YuQian. 2019. Generating commit messages from diffs using pointer-generator network. In 2019IEEE / ACM 16th International Conference on Mining Software Repositories (MSR). IEEE, 299–309.)
[0005] PtrGNCMsg is an improved attention-based encoder-decoder model that utilizes a pointer generator network to transform different code changes into code commit logs. By searching for the minimum set of identifiers with the highest probabilities, PtrGNCMsg can predict out-of-vocabulary words. In the field of code commit log generation, PtrGNCMsg has been shown to outperform neural machine translation-based methods.
[0006] Non-patent document 2(Zhongxin Liu,
[0007] NNGen is a state-of-the-art method that uses the nearest neighbor algorithm to retrieve the top k commits from the training set. These top k commits are then compared, and the nearest neighbors are identified by leveraging BLEU scores, resulting in the target code commit log with the smallest distance.
[0008] While learning-based methods have achieved good results, they tend to favor high-frequency words in the corpus, ignoring low-frequency words. They also suffer from exposure bias. Summary of the Invention
[0009] This invention addresses the shortcomings of existing technologies in code commit log generation by proposing a deep commit message generation method based on context-aware retrieval, comprising the following steps:
[0010] Step S1: Train an attention-based context-aware encoder-decoder model. During the encoding phase of the training process, the encoder converts the input words of each source sequence into fixed-length vector representations, uses a bidirectional long short-term memory network to calculate the contextual dependencies of the source sequence, and extracts feature information related to semantic changes in the code through a global attention mechanism to generate source context vectors for the decoder part.
[0011] Step S2: During the decoding phase of the training process, the context-aware decoder uses a long short-term memory network to expand the submission information sequence and updates the hidden state of the decoder at each decoding time step. The decoder selects the context input through a decay sampling mechanism, where the context includes the output generated by the decoder in the previous step or the source context vector generated by the encoder. The decoder optimizes the model parameters through the maximum likelihood estimation method to minimize the difference between the generated code submission log and the target code submission log.
[0012] Step S3: After the context-aware encoder-decoder model is trained, the input verification code change is sent to the encoder. First, the trained encoder is run to obtain the hidden state vector corresponding to the last layer of the bidirectional long short-term memory network for the verification code change. Then, the hidden state vector is compressed into a feature vector through global max pooling. Based on the feature vector, the history of code change is retrieved and matched. The model predicts the generation conditions of the code submission message based on the similarity in the history and gradually generates the code submission message until the predetermined length is reached. Finally, the code submission message is output.
[0013] Furthermore, step S1 includes the following steps:
[0014] Step S1.1: The encoder sets the code change sequence in the training set as the source sequence for input, represented as x = {x1, ..., x...} |x|}, where each input word x i Transformed into a fixed-length vector representation through an embedding layer. i is the encoding time step in the source sequence, i∈[1,|x|];
[0015] Step S1.2: Use the encoder's bidirectional long short-term memory network to compute the hidden state h of each word in the input source sequence at time step i. i The hidden state h i Including forward hidden state and backward hidden state The specific calculations are as follows:
[0016]
[0017] LSTM stands for Long Short-Term Memory Network;
[0018] Step S1.3: Use a global attention mechanism to compute the source context vector c j The source context vector c of the attention mechanism j Calculate using the following formula:
[0019]
[0020] Where a ij For the hidden state h i The attention weights are given by j, where j is the decoding time step in the output sequence, and j∈[1,|y|].
[0021] Furthermore, step S2 includes the following steps:
[0022] Step S2.1: The decoder sets the output of the submitted code as the output sequence, represented as y = {y1, ..., y2}. |y|At each decoding time step j, the target word y generated by the decoder is calculated. j With the i-th word x in the source sequence i The correlation is calculated by normalizing the source sequences using the following formula:
[0023]
[0024] Among them, v a s is a parameter vector used to map the states of the decoder and encoder to attention scores. j-1 W represents the hidden state of the decoder in the previous time step. a and U a These are the model parameters learned during the training process;
[0025] Step S2.2: The decoder uses a sampling mechanism with probability attenuation to select the source context vector generated by the encoder according to the attenuation probability p, or selects the output of the decoder at the previous time step j-1 as the context for the next step with probability 1-p. The decay probability p is calculated based on the batch index k using the following formula:
[0026]
[0027] Where B is the training batch size, N is the number of submission pairs in the training data, and μ is the hyperparameter controlling the rate of decrease of p;
[0028] Step S2.3: At each decoding time step j, the decoder updates its hidden state s through the Long Short-Term Memory network. j The formula is as follows:
[0029]
[0030] in This is the output of the attenuation sampling mechanism;
[0031] Step S2.4: At each decoding time step j, the decoder performs a linear transformation on the output of the attenuation sampling mechanism and the source context vector c. j With the decoder's hidden state s j Combined, predict the probability distribution P of the next generated word. j Calculate using the following formula:
[0032] P j =softmax(t) j )
[0033]
[0034] Where g is a linear transformation that maps each target word to its corresponding dimension;
[0035] Step S2.5: Optimize the decoder parameters using maximum likelihood estimation to minimize the difference between the generated sequence and the true target sequence. The loss function is as follows:
[0036]
[0037] Where θ represents the trainable parameters of the model, N is the total size of the training data, and |y n | represents the length of the actual code commit log. This represents the j-th word of the n-th sample in the real target sequence. Let J represent the probability distribution of the words generated by the decoder at time step j. It is for the reference target words The difference predicts the probability of the correct word.
[0038] Furthermore, step S3 includes the following steps:
[0039] Step S3.1: Input the verification code change into the trained context-aware encoder-decoder model. The model obtains the hidden state vector corresponding to the verification code change in the last layer through a bidirectional long short-term memory network. Where n is the length of the code change sequence, k is the size of the hidden dimension, and cosine similarity is used to calculate the similarity between the verification code change sequence and each code change in the training set code change sequence. The specific formula is as follows:
[0040]
[0041] Where N is the number of code change sequences in the training set;
[0042] Step S3.2: Compress the hidden state vector into a feature vector using a global max pooling mechanism. The specific calculation formula is as follows:
[0043]
[0044] Based on the similarity between the feature vector and the similarity calculated by cosine similarity, the difference retrieval module is used to retrieve the code change most similar to the verification code change from the training set.
[0045] Step S3.3: Input the verified code change and the retrieved code change in parallel into the trained context-aware decoder model, and use H to represent the hidden states of the last encoder layer. test and H retrievalThis means that in each decoding time step j, the context vector is calculated using the formula given in step S1.3, and the conditional probability of predicting the next word is calculated according to the formula given in step S2.4, and P is used respectively. test (y j |x) and P retrieval (y j |x) represents;
[0046] Step S3.4: Calculate the final generation probability P used to generate the final code commit message by combining the conditional probabilities corresponding to the verified code changes and the retrieved code changes. final The specific calculation formula is as follows:
[0047] P final (y j |x)=P test (y j ∣x)+λBLEU(x retrieval ,x test )P retrieval (y j |x)
[0048] Where λ is a hyperparameter adjusted based on the verification code change, and BLEU is used to measure the similarity between the retrieved code change and the verification code change;
[0049] Step S3.5: Use the beam search algorithm to select the top k candidate sequences with the highest probability of satisfying the generation conditions, where k is the beam size. The decoder generates code submission messages step by step until the predetermined length is met or the termination condition is reached. Finally, the top candidate sequence is selected as the target code submission message output.
[0050] Furthermore, in step S3.4, the model generates a code submission message by calculating the conditional probability of each word, and processes the noise information that may be contained in the retrieved code change sequence based on the weight adjustment mechanism of the conditional probability.
[0051] Further, in step S3.5, the beam search algorithm starts from the start mark, calculates the probability distribution of all possible first output words, and selects the top k candidate words with the highest probabilities as initial candidate sequences. For each candidate sequence, the algorithm adds all possible next words to the end of the sequence to form new candidate sequences and calculates the probabilities of these new sequences. Then, from all newly generated candidate sequences, the algorithm selects the top k sequences with the highest probabilities to retain. This process is repeated until the predetermined maximum sequence length is reached or all candidate sequences reach the end mark. Finally, among the retained candidate sequences, the sequence with the highest probability is taken as the final output.
[0052] This invention provides a deep code commit log generation model based on context-aware retrieval. By combining contextual information and a retrieval module during training, the model can generate more accurate and relevant code commit logs, improving the quality and reliability of the logs. Furthermore, the introduction of the retrieval module allows the model to utilize contextual information from similar code commits when generating logs, thereby enhancing its ability to handle low-frequency words. Attached Figure Description
[0053] Figure 1 This is a flowchart of the method of the present invention.
[0054] Figure 2 This is a schematic diagram of the training model of the present invention.
[0055] Figure 3 This diagram illustrates the differences between the present invention and other methods. Detailed Implementation
[0056] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, further description will be provided below in conjunction with the accompanying drawings of the embodiments of the present invention.
[0057] Taking a dataset for generating code commit logs as an example, this dataset collects the top 10,000 repositories from GitHub, using a combination of keywords and patterns to filter out as much noise as possible. After preprocessing and filtering, a training set, a validation set, and a test set are obtained, containing 96,704, 5,372, and 5,372 commit pairs, respectively. The embedding size and the dimension of the hidden state in the LSTM are set to 512. The number of encoder and decoder layers is set to 2 each. The training batch size is set to 32, and an exit mechanism is used to avoid overfitting with an exit rate of 0.1. We use the Adam optimizer algorithm with an initial learning rate of 0.001; for this dataset, the maximum number of training steps is set to 400k. Accordingly, λ in Equation 14 is set to 0.5 based on the validation set. The maximum and minimum lengths of the target sentences are set to 30 and 2, respectively. By default, the bundle size in the bundle search algorithm is set to 5, and the maximum length of the input difference is set to 100.
[0058] like Figure 1 As shown, a deep submission message generation method based on context-aware retrieval includes the following steps:
[0059] Step S1: Train an attention-based context-aware encoder-decoder model. During the encoding phase of the training process, the encoder converts the input words of each source sequence into fixed-length vector representations, uses a bidirectional long short-term memory network to calculate the contextual dependencies of the source sequence, and extracts feature information related to semantic changes in the code through a global attention mechanism to generate source context vectors for the decoder part.
[0060] Step S1.1: The encoder sets the code change sequence in the training set as the source sequence for input, represented as x = {x1, ..., x...} |x|}, where each input word x i Transformed into a fixed-length vector representation through an embedding layer. i is the encoding time step in the source sequence, i∈[1,|x|];
[0061] Step S1.2: The encoder's input is a variable-length source sequence, i.e., the code-changing part. First, each input word is represented as a fixed-length vector through an embedding layer. The encoder processes one vector at a time until the end of the sequence. We use a bidirectional long short-term memory network to capture information before and after the current position. The hidden state of the bidirectional long short-term memory network at time step i is calculated as follows:
[0062]
[0063] in, It is the word x i The embedding vector;
[0064] Step S1.3: An attention mechanism is used to selectively focus on parts of the source sentence during translation. A global attention mechanism is used to extract the source context vector. The source context vector is calculated according to the following formula:
[0065]
[0066] Where a ij It is in a hidden state h i Attention weights;
[0067] Step S2: During the decoding phase of the training process, the context-aware decoder uses a long short-term memory network to expand the submission information sequence and updates the hidden state of the decoder at each decoding time step. The decoder selects the context input through a decay sampling mechanism, where the context includes the output generated by the decoder in the previous step or the source context vector generated by the encoder. The decoder optimizes the model parameters through the maximum likelihood estimation method to minimize the difference between the generated code submission log and the target code submission log.
[0068] Step S2.1: The decoder sets the output of the submitted code as the output sequence, represented as y = {y1, ..., y2}. |y| At each decoding time step j, the target word y generated by the decoder is calculated. j With the i-th word x in the source sequence i The correlation is calculated by normalizing the source sequences using the following formula:
[0069]
[0070] Among them, v a s is a parameter vector used to map the states of the decoder and encoder to attention scores. j-1 W represents the hidden state of the decoder in the previous time step. a and U a These are the model parameters learned during the training process;
[0071] Step S2.2: The decoder uses a sampling mechanism with probability attenuation to select the source context vector generated by the encoder according to the attenuation probability p, or selects the output of the decoder at the previous time step j-1 as the context for the next step with probability 1-p. The decay probability p is calculated based on the batch index k using the following formula:
[0072]
[0073] Where B is the training batch size, N is the number of submission pairs in the training data, and μ is the hyperparameter controlling the rate of decrease of p;
[0074] Step S2.3: The context-aware decoder uses LSTM to expand the code change information. At time step j, the decoder's hidden state is updated as follows:
[0075]
[0076] in It is the output of the attenuation sampling mechanism.
[0077] Step S2.4: Once the hidden state of the decoder is obtained, predict the probability distribution P of the next word. j The calculation is as follows: After the decoder obtains the hidden state, it predicts the probability distribution P of the next word. j Calculate using the following formula:
[0078] P j =softmax(t) j )
[0079]
[0080] Where g is a linear transformation that maps each target word to its corresponding dimension.
[0081] Step S2.5: The goal of the network is to maximize the probability of the true sequence based on maximum likelihood estimation. Therefore, the loss function is as follows: Minimize the following loss function to optimize the model parameters:
[0082]
[0083] The trainable parameters of the θ model, where N is the total size of the training data, |y n | refers to the length of the actual code commit log, and j represents the time step. It is for the reference target words The difference predicts the probability of the correct word.
[0084] Step S3: After the context-aware encoder-decoder model is trained, as shown below... Figure 2 As shown, the verification code change is input into the encoder. First, the trained encoder is run to obtain the hidden state vector corresponding to the verification code change in the last layer of the bidirectional long short-term memory network. Then, the hidden state vector is compressed into a feature vector through global max pooling. Based on this feature vector, the history of code changes is retrieved and matched. The model predicts the generation conditions of the code submission message based on the similarity in the history and generates the code submission message step by step until the predetermined length is reached. Finally, the code submission message is output.
[0085] Step S3.1: Encode the given verification code change to obtain the hidden state vector of the last Bi-LSTM layer. Use global max pooling to compress the hidden state vector into a fixed-length vector. Calculate the cosine similarity between the retrieved code change and the given code change. Select the most similar code change based on the similarity.
[0086]
[0087] Step S3.2: Select the difference with the highest similarity, and generate the final code commit log by combining the conditional probabilities of the given code change and the retrieved code changes, calculated according to the following formula:
[0088] P final (y j |x)=P test (y j ∣x)+λBLEU(x retrieval ,x test )P retrieval (y j |x)
[0089] Where λ is a hyperparameter adjusted based on the validation set, and BLEU is used to measure the similarity between the retrieved code changes and a given code change.
[0090] Step S3.3: The beam search algorithm is used to retain the top k candidate sequences with the highest conditional probabilities among all combinations, where k is the beam size. The first sentence is committed as the final target code in the log output before encountering a terminator or reaching the maximum sentence length.
[0091] The performance of the code commit log generation task is evaluated based on three widely used metrics: BLEU, METEOR, and ROUGE-L.
[0092] BLEU is widely used to evaluate the quality of machine translation systems. It is defined as the geometric mean of the accuracy score of an n-gram (for BLEU, n = 1, 2, 3, 4) match multiplied by a length conciseness penalty factor (BP). For each pair (pred, ref), the length conciseness penalty factor is introduced to prevent accuracy bias caused by the generated short sentences. The BLEU score is calculated as follows: and
[0093]
[0094] Among them, len pred To predict the length of a sentence, len ref For reference sentence length, N=4, each n grams w n The weight is set to
[0095] METEOR combines unary segmentation matching precision with a recall score utilizing the harmonic mean. METEOR further employs synonym matching, calculated using the formula: P is unit precision, R is unit recall. c is the segment matching penalty factor, which is calculated using the following formula: Where |chunks| is the number of matched chunks, and |unigrams| is the number of matched unigrams.
[0096] ROUGE-L calculates the length of the longest common subsequence between the generated sentence and the reference sentence. It does not attempt to assess the fluency of the summary, but rather focuses on the recall score.
[0097] These metrics are all used to measure the quality of the generated code commit logs.
[0098] We used state-of-the-art NNGen and PtrGNCMsg as baselines. Results are as follows: Figure 3The results show that, on average, the context-aware code commit log generation method significantly outperforms NNGen and PtrGNCMsg by 19% and 64% respectively in BLEU (a widely used method for evaluating the accuracy of machine translation systems).
[0099] The above embodiments are used to explain and illustrate the present invention, but not to limit the present invention. Any modifications and changes made to the present invention within the spirit and scope of the claims shall fall within the protection scope of the present invention.
Claims
1. A method for generating deep submission messages based on context-aware retrieval, characterized in that, Includes the following steps: Step S1: Train an attention-based context-aware encoder-decoder model. During the encoding phase of the training process, the encoder converts the input words of each source sequence into fixed-length vector representations, uses a bidirectional long short-term memory network to calculate the contextual dependencies of the source sequence, and extracts feature information related to semantic changes in the code through a global attention mechanism to generate source context vectors for the decoder part. Step S2: During the decoding phase of the training process, the context-aware decoder uses a long short-term memory network to expand the submission information sequence and updates the hidden state of the decoder at each decoding time step. The decoder selects the context input through a decay sampling mechanism, where the context includes the output generated by the decoder in the previous step or the source context vector generated by the encoder. The decoder optimizes the model parameters through the maximum likelihood estimation method to minimize the difference between the generated code submission log and the target code submission log. Step S3: After the context-aware encoder-decoder model is trained, the input verification code change is sent to the encoder. First, the trained encoder is run to obtain the hidden state vector corresponding to the last layer of the bidirectional long short-term memory network for the verification code change. Then, the hidden state vector is compressed into a feature vector through global max pooling. Based on the feature vector, the history of code change is retrieved and matched. The model predicts the generation conditions of the code submission message based on the similarity in the history and gradually generates the code submission message until the predetermined length is reached. Finally, the code submission message is output.
2. The deep submission message generation method based on context-aware retrieval according to claim 1, characterized in that, Step S1 includes the following steps: Step S1.1: The encoder sets the code change sequence in the training set as the source sequence for input, represented as x = {x1, ..., x...} |x| }, where each input word x i Transformed into a fixed-length vector representation through an embedding layer. i is the encoding time step in the source sequence, i∈[1,|x|]; Step S1.2: Use the encoder's bidirectional long short-term memory network to compute the hidden state h of each word in the input source sequence at time step i. i The hidden state h i Including forward hidden state and backward hidden state The specific calculations are as follows: LSTM stands for Long Short-Term Memory Network; Step S1.3: Use a global attention mechanism to compute the source context vector c j The source context vector c of the attention mechanism j Calculate using the following formula: Where a ij For the hidden state h i The attention weights are given by j, where j is the decoding time step in the output sequence, and j∈[1,|y|].
3. The deep submission message generation method based on context-aware retrieval according to claim 1, characterized in that, Step S2 includes the following steps: Step S2.1: The decoder sets the output of the submitted code as the output sequence, represented as y = {y1, ..., y2}. |y| At each decoding time step j, the target word y generated by the decoder is calculated. j With the i-th word x in the source sequence i The correlation is calculated by normalizing the source sequences using the following formula: Among them, v a s is a parameter vector used to map the states of the decoder and encoder to attention scores. j-1 W represents the hidden state of the decoder in the previous time step. a and U a These are the model parameters learned during the training process; Step S2.2: The decoder uses a sampling mechanism with probability attenuation to select the source context vector generated by the encoder according to the attenuation probability p, or selects the output of the decoder at the previous time step j-1 as the context for the next step with probability 1-p. The decay probability p is calculated based on the batch index k using the following formula: Where B is the training batch size, N is the number of submission pairs in the training data, and μ is the hyperparameter controlling the rate of decrease of p; Step S2.3: At each decoding time step j, the decoder updates its hidden state s through the Long Short-Term Memory network. j The formula is as follows: in This is the output of the attenuation sampling mechanism; Step S2.4: At each decoding time step j, the decoder performs a linear transformation on the output of the attenuation sampling mechanism and the source context vector c. j With the decoder's hidden state s j Combined, predict the probability distribution P of the next generated word. j Calculate using the following formula: P j =softmax(t j ) Where g is a linear transformation that maps each target word to its corresponding dimension; Step S2.5: Optimize the decoder parameters using maximum likelihood estimation to minimize the difference between the generated sequence and the true target sequence. The loss function is as follows: Where θ represents the trainable parameters of the model, N is the total size of the training data, and |y n | represents the length of the actual code commit log. This represents the j-th word of the n-th sample in the real target sequence. Let J represent the probability distribution of the words generated by the decoder at time step j. It is for the reference target words The difference predicts the probability of the correct word.
4. The deep submission message generation method based on context-aware retrieval according to claim 1, characterized in that, Step S3 includes the following steps: Step S3.1: Input the verification code change into the trained context-aware encoder-decoder model. The model obtains the hidden state vector corresponding to the verification code change in the last layer through a bidirectional long short-term memory network. Where n is the length of the code change sequence, k is the size of the hidden dimension, and cosine similarity is used to calculate the similarity between the verification code change sequence and each code change in the training set code change sequence. The specific formula is as follows: Where N is the number of code change sequences in the training set; Step S3.2: Compress the hidden state vector into a feature vector using a global max pooling mechanism. The specific calculation formula is as follows: Based on the similarity between the feature vector and the similarity calculated by cosine similarity, the difference retrieval module is used to retrieve the code change most similar to the verification code change from the training set. Step S3.3: Input the verified code change and the retrieved code change in parallel into the trained context-aware decoder model, and use H to represent the hidden states of the last encoder layer. test and H retrieval This means that in each decoding time step j, the context vector is calculated using the formula given in step S1.3, and the conditional probability of predicting the next word is calculated according to the formula given in step S2.4, and P is used to calculate the probability of predicting the next word. test (y j |x) and P retrieval (y j |x) represents; Step S3.4: Calculate the final generation probability P used to generate the final code commit message by combining the conditional probabilities corresponding to the verified code changes and the retrieved code changes. final The specific calculation formula is as follows: P final (y j |x)=P test (y j |x)+λBLUE(x retrieval ,x test )P retrieval (y j |x) Where λ is a hyperparameter adjusted based on the verification code change, and BLEU is used to measure the similarity between the retrieved code change and the verification code change; Step S3.5: Use the beam search algorithm to select the top k candidate sequences with the highest probability of satisfying the generation conditions, where k is the beam size. The decoder generates code submission messages step by step until the predetermined length is met or the termination condition is reached. Finally, the top candidate sequence is selected as the target code submission message output.
5. The method for generating deep submission messages based on context-aware retrieval according to claim 5, characterized in that, In step S3.4, the model generates a code submission message by calculating the conditional probability of each word, and processes the noise information that may be contained in the retrieved code change sequence based on the weight adjustment mechanism of the conditional probability.
6. The method for generating deep submission messages based on context-aware retrieval according to claim 1, characterized in that, In step S3.5, the beam search algorithm starts from the start mark, calculates the probability distribution of all possible first output words, and selects the top k candidate words with the highest probabilities as the initial candidate sequences. For each candidate sequence, the algorithm adds all possible next words to the end of the sequence to form new candidate sequences and calculates the probabilities of these new sequences. Then, from all the newly generated candidate sequences, the algorithm selects the top k sequences with the highest probabilities to retain. This process is repeated until the predetermined maximum sequence length is reached or all candidate sequences reach the end mark. Finally, among the retained candidate sequences, the sequence with the highest probability is taken as the final output.