Code generation and verification model training method, generation method, device and equipment

By calculating gradients and updating parameters in the code generation model and code verification model, sharing knowledge to improve accuracy, the problem of low accuracy of code generation model and code verification model in the existing technology is solved, and higher code generation quality and model generalization capabilities are achieved.

CN120215893APending Publication Date: 2025-06-27CHINA UNIONPAY MERCHANT SERVICES CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311769886.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-12-20
Publication Date
2025-06-27

AI Technical Summary

Technical Problem

Existing code generation models and code verification models have low accuracy, making it difficult to search and generate code in huge spaces that meet syntax rules and can solve specific tasks, and training samples are scarce.

Method used

By obtaining the initial training sample pair, inputting it to the code generation model and code verification model, computing gradients and updating parameters until preset conditions are met, sharing knowledge to improve accuracy.

Benefits of technology

The accuracy of the code generation model and code verification model is improved, and the generalization ability of the model and the quality of the generated code are enhanced by sharing knowledge points.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120215893A_ABST
    Figure CN120215893A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation and verification model training method, generation method, device and equipment. The code generation model training method comprises the steps of obtaining an initial training sample pair; inputting the first demand information into the code generation model to obtain a fourth vector set; inputting the fourth vector set into a code verification model, and obtaining feedback information of the code verification model about a third training sample pair; calculating a gradient of the code generation model based on feedback information of the code verification model about a third training sample pair; and when the gradient of the code generation model does not meet a first preset condition, updating parameters of the code generation model until the gradient of the code generation model meets the first preset condition, and taking the code generation model meeting the first preset condition as a finally generated code generation model. By adopting the scheme, the accuracy of the code generation model can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of machine learning, and particularly relates to a training method, a generation method, a device and a device for a code generation and verification model. Background Art

[0002] By using a code generation model, the requirements described by a user in natural language can be automatically converted into executable code, which can lower the threshold of software development and improve the software development efficiency.

[0003] However, in practical applications, on the one hand, it is difficult for the code generation model to search and generate code that meets the syntax rules and can solve specific tasks in a huge space; on the other hand, compared with general classification models, the training samples of the code generation model are scarce. As a result, the accuracy of existing code generation models is relatively low.

[0004] In order to improve the accuracy of the code generation model, a solution of using a code verification model to verify the candidate code generated by the code generation model is proposed. The code verification model verifies whether the candidate code is semantically consistent with the first requirement and filters out the candidate code that is semantically inconsistent with the first requirement. This method of using a code verification model to verify the candidate code is simple and effective and does not restrict the internal structure of the code generation model, and can improve the quality of the generated code.

[0005] However, the current code generation model and code verification model both have relatively low accuracy. Summary of the Invention

[0006] One of the problems to be solved by the present invention is: to improve the accuracy of the code generation model.

[0007] Another problem to be solved by the present invention is: to improve the accuracy of the code verification model.

[0008] To solve the above problems, an embodiment of the present invention provides a method for training a code generation model, where the code generation model is used to convert requirement information described in natural language into corresponding code; the method includes: obtaining an initial training sample pair, where the initial training sample pair includes: first requirement information, and the first code corresponding to the first requirement information; inputting the first requirement information into the code generation model to obtain a fourth vector set; the fourth vector set is a set composed of time-step vectors output by the decoding layer of the code generation model; inputting the fourth vector set into a code verification model, and obtaining feedback information of the code verification model about a third training sample pair; the third training sample pair includes: first requirement information, and the fourth vector set; the code verification model is used to convert code into corresponding requirement information described in natural language, and has duality with the code generation model; calculating the gradient of the code generation model based on the feedback information of the code verification model about the third training sample pair; when the gradient of the code generation model does not meet a first preset condition, updating the parameters of the code generation model until the gradient of the code generation model meets the first preset condition, and taking the code generation model that meets the first preset condition as the finally generated code generation model.

[0009] An embodiment of the present invention also provides a method for training a code verification model, where the code verification model is used to convert requirement information described in natural language into corresponding code; the method includes: obtaining an initial training sample pair, where the initial training sample pair includes: first requirement information, and the first code corresponding to the first requirement information; inputting the first code into the code verification model to obtain a first vector set; the first vector set is a set of time-step vectors output by the decoding layer of the code verification model after inputting the first code into the code verification model; inputting the first vector set into the code generation model, and obtaining feedback information of the code generation model about a first training sample pair; the first training sample pair includes: the first vector set, and the corresponding first code; the code generation model is used to convert requirement information described in natural language into corresponding code, and has duality with the code generation model; calculating the gradient of the code verification model based on the feedback information of the code generation model about the first training sample pair; when the gradient of the code verification model does not meet a third preset condition, updating the parameters of the code verification model until the gradient of the code verification model meets the third preset condition, and taking the code verification model that meets the third preset condition as the finally generated code verification model.

[0010] An embodiment of the present invention further provides a code generation method, the method comprising: inputting requirement information described in natural language into a trained code generation model to obtain two or more candidate codes, and a first probability value for converting the first requirement information into the corresponding candidate code; the code generation model is trained by using any one of the above code generation model training methods; inputting the candidate codes output by the code generation model into a trained code verification model to obtain a second probability value for converting each of the candidate codes into the requirement information described in natural language; the code verification model is trained by using any one of the above code verification model training methods; and determining the code corresponding to the requirement information described in natural language based on the product of the first probability value and the corresponding second probability value.

[0011] An embodiment of the present invention further provides a training device for a code generation model, the code generation model being used for converting requirement information described in natural language into corresponding codes; the device comprising: a first acquisition unit, adapted to acquire a first training sample pair, the first training sample pair comprising: a first vector set, and a first code corresponding to the first vector set; the code verification model is used for converting a code into the corresponding requirement information described in natural language and has duality with the code generation model; the first vector set is a set of vectors at each time step output by a decoding layer of the code verification model after inputting the first code into the code verification model; a first training unit, adapted to train the code generation model by using the first training sample pair and obtain a code generation task loss in the current training process; a first parameter update unit, adapted to update parameters of the code generation model; and a first control unit, adapted to determine whether the code generation task loss satisfies a first preset condition, and when the code generation task loss does not satisfy the first preset condition, control the first training unit and the first parameter update unit to repeatedly execute corresponding operations until the code generation task loss satisfies the first preset condition, and use the code generation model that satisfies the first preset condition as the finally generated code generation model.

[0012] An embodiment of the present invention further provides a training device for a code verification model, where the code verification model is used to convert requirement information described in natural language into corresponding code; the device includes: a second acquisition unit, adapted to acquire a third training sample pair, where the third training sample pair includes: first requirement information, and a fourth vector set; the first requirement information is the requirement information described in natural language corresponding to the first code; the fourth vector set is a set of vectors at each time step output by the decoding layer of the code generation model after the first requirement information is input into the code generation model; the code generation model is used to convert requirement information described in natural language into corresponding code, and has duality with the code generation model; a second training unit, adapted to use the third training sample pair to train the code verification model and obtain the code verification task loss during the current training process; a second parameter update unit, adapted to update the parameters of the code verification model; and a second control unit, adapted to determine whether the code verification task loss meets a second preset condition, and when the code verification task loss meets the second preset condition, control the second training unit and the second parameter update unit to repeatedly execute corresponding operations until the code verification task loss meets the second preset condition, and use the code verification model that meets the second preset condition as the finally generated code verification model.

[0013] An embodiment of the present invention further provides a code generation device, where the code generation device includes: a code generation unit, adapted to input requirement information described in natural language into a trained code generation model to obtain two or more candidate codes, and a first probability value for converting the first requirement information into the corresponding candidate code; the code generation model is trained by using the training device for the code generation model described above; a code verification unit, adapted to input the candidate codes output by the code generation model into a trained code verification model to obtain a second probability value for converting each of the candidate codes into the requirement information described in natural language; the code verification model is trained by using the training device for the code verification model described above; a code determination unit, adapted to determine the code corresponding to the requirement information described in natural language based on the product of the first probability value and the corresponding second probability value.

[0014] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored, and the computer program is executed by a processor to implement the steps of any one of the above methods.

[0015] An embodiment of the present invention further provides an electronic device, including a memory and a processor, where a computer program capable of running on the processor is stored on the memory, and characterized in that when the processor runs the computer program, it executes the steps of any one of the above methods.

[0016] Compared with the prior art, the technical solution of the embodiment of the present invention has the following advantages:

[0017] Applying the solution of the present invention, by obtaining the fourth vector set and inputting the fourth vector set into the code verification model to obtain the feedback information of the code verification model about the third training sample pair, and then transmitting the feedback information of the code verification model about the third training sample pair back to the code generation model, the knowledge of the code verification model is shared with the code generation model, thereby improving the accuracy of the code generation model.

[0018] By obtaining the first vector set and inputting the first vector set into the code generation model to obtain the feedback information of the code generation model about the first training sample pair, and then calculating the gradient of the code verification model based on the feedback information of the code generation model about the first training sample pair, the knowledge of the code generation model is shared with the code verification model, thereby improving the accuracy of the code verification model. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 is a schematic diagram of a code generation process;

[0020] Figure 2 is a schematic diagram of using a code verification model to verify candidate codes generated by a code generation model;

[0021] Figure 3 is a flowchart of a method for training a code generation model in an embodiment of the present invention;

[0022] Figure 4 is a schematic diagram of the training principle of a code generation model in an embodiment of the present invention;

[0023] Figure 5 is a schematic diagram of the feedback result of a code;

[0024] Figure 6 is a schematic diagram of the feedback result of a code in an embodiment of the present invention;

[0025] Figure 7 is a flowchart of a method for training a code verification model in an embodiment of the present invention;

[0026] Figure 8 is a schematic diagram of the training principle of a code verification model in an embodiment of the present invention;

[0027] Figure 9 is a flowchart of a code generation method in an embodiment of the present invention;

[0028] Figure 10 is a schematic diagram of a code generation process in an embodiment of the present invention;

[0029] Figure 11 It is a schematic structural diagram of a training device for a code generation model in an embodiment of the present invention;

[0030] Figure 12 It is a schematic structural diagram of a training device for a code verification model in an embodiment of the present invention;

[0031] Figure 13 It is a schematic structural diagram of a code generation device in an embodiment of the present invention. Detailed implementation manners

[0032] Figure 1 It is a schematic diagram of a code generation process. Refer to Figure 1 , the task of the code generation model is to convert the user requirement information described in natural language into executable code. For example, the user requirement information described in natural language can be: The price of the ticket is 200 yuan. After being converted by the code generation model, the obtained code can be: int price = 200.

[0033] Existing deep learning-based code generation models usually use beam search technology to generate multiple candidate codes, and rank the candidate codes according to the probability of generating each candidate code, and finally select the candidate code with the highest probability as the finally generated code.

[0034] However, since the code generation model often tends to assign higher scores to shorter or more general candidate codes, making these shorter or more general candidate codes have higher probabilities, and these shorter or more general candidate codes may miss relatively rare functions in the requirements, or contain common code segments not mentioned in the requirements, affecting the accuracy of the code generation model.

[0035] In order to improve the accuracy of the code generation model, a solution is proposed to use a code verification model to verify the candidate codes generated by the code generation model. Specifically, refer to Figure 2 , input the first requirement into the code generation model to obtain K candidate codes (i.e., candidate code 1 to candidate code K, K≥2 and K is an integer), and calculate the first probability value of generating each candidate code. Then, for each candidate code, input it into the code verification model respectively, and use the code verification model to calculate the second probability value of converting each candidate code into the first requirement information. Then, take the product of the first probability value and the second probability value corresponding to the same candidate code as the score of the candidate code, and select the candidate code with the highest score as the final code.

[0036] The code verification model verifies whether the candidate code is semantically consistent with the first requirement information and filters out the candidate codes that are semantically inconsistent with the first requirement. This method of verifying candidate codes using the code verification model is simple and effective and does not impose constraints on the internal structure of the code generation model, which can improve the quality of the generated code.

[0037] However, the training processes of the current code generation model and the code verification model are independent of each other, that is, different training samples are used to independently train the code generation model and the code verification model respectively. Since the training of the code generation model and the code verification model themselves is relatively difficult and the training samples are scarce, the accuracies of the current code generation model and the code verification model are both relatively low.

[0038] To address this problem, the present invention provides a method for training a code generation model. By applying this method, a fourth vector set is obtained and input into the code verification model to obtain the feedback information of the code verification model regarding the third training sample pair. Subsequently, the feedback information of the code verification model regarding the third training sample pair is passed back to the code generation model, enabling the knowledge of the code verification model to be shared with the code generation model, thereby improving the accuracy of the code generation model.

[0039] The embodiment of the present invention also provides a method for training a code verification model. By applying this method, a first vector set is obtained and input into the code generation model to obtain the feedback information of the code generation model regarding the first training sample pair. Subsequently, the feedback information of the code generation model regarding the first training sample pair is used to calculate the gradient of the code verification model, enabling the knowledge of the code generation model to be shared with the code verification model, thereby improving the accuracy of the code verification model.

[0040] To make the above objects, features, and advantages of the present invention more obvious and understandable, the following detailed description of the specific embodiments of the present invention will be provided with reference to the accompanying drawings.

[0041] Before describing the training methods of the code generation model and the code verification model in the embodiments of the present invention, a detailed description of the code generation model and the code verification model will be provided in combination with Figure 10 , as follows:

[0042] The code generation model is used to convert the requirement information described in natural language into corresponding code. The code verification model is used to convert the code into corresponding requirement information described in natural language. The code generation model and the code generation model have duality.

[0043] In a specific implementation, there is a duality between two models, which means that the input data and output data of the two models have duality. Specifically, in the embodiments of the present invention, the input of the code generation model and the output of the code verification model are both requirement information described in natural language, and the input of the code verification model and the output of the code generation model are both codes.

[0044] In a specific implementation, referring to Figure 10 , the code generation model can be any sequence-to-sequence (seq2seq) model. The seq2seq model is used to map one sequence to another sequence.

[0045] In one embodiment, the seq2seq model can use the beam search algorithm to convert the requirement information described in natural language into candidate codes.

[0046] Among them, the beam search algorithm is a commonly used algorithm in the field of natural language processing, which is used to generate a most likely output sequence for a given input sequence. By using the beam search algorithm, multiple candidate answers can be maintained simultaneously during the search process, and the candidate answers to be retained are selected according to the pre-set width (i.e., the beam width). Compared with the greedy algorithm, it can avoid prematurely abandoning potential answers, thereby improving the accuracy of the model.

[0047] Specifically, referring to Figure 10 , the seq2seq model consists of two main parts: an encoder and a decoder. Among them, the encoder is adapted to convert the requirement information x described in natural language into a vector c. Specifically, the encoder includes an embedding layer and an encoding layer. The embedding layer of the encoder can map each token in the requirement information x described in natural language to a corresponding vector according to a dictionary, and the corresponding vectors of each token form a vector set h. Then, the encoding layer converts the vector set h into a vector c that integrates context. In the requirement information x described in natural language, each token is a Chinese character or a phrase.

[0048] The decoder includes: a decoding layer and a reading layer. The functions of the decoder during the training process and the actual prediction process are slightly different.

[0049] During the training process, the decoder is used to calculate the probability value of converting the requirement information x described in natural language into a code y. Among them, the decoder first sends the vector c output by the encoding layer into the decoding layer to obtain a vector set g output at each time step. Then, the reading layer calculates the probability value output of converting the requirement information x described in natural language into a code y based on the vector set g output at each time step.

[0050] In the actual prediction process, the decoder sends the vector c output by the encoding layer into the decoding layer to obtain a set of vectors g output at each time step. Then, based on the vectors g output at each time step, the reading layer uses the beam search algorithm to obtain the candidate code corresponding to the requirement information x described in natural language and outputs it. At the same time, the first probability value for converting the requirement information x described in natural language into each candidate code is output.

[0051] Referring to Figure 10 , the code verification model can also be any seq2seq model. The seq2seq model is used to map one sequence to another sequence.

[0052] In one embodiment, the seq2seq model can use the beam search algorithm to convert the code into the requirement information described in natural language.

[0053] Specifically, similar to the code generation model, the code verification model can also include an encoder and a decoder. The encoder of the code verification model is adapted to convert the code into a vector c'. The encoder includes an embedding layer and an encoding layer. The embedding layer obtains a set of vectors e composed of the vectors corresponding to each token in the code y, and the encoding layer converts the set of vectors e into a context-fused vector c'.

[0054] The decoder is used to calculate the probability value of converting the code y into the requirement information x described in natural language. Specifically, the decoder first sends the vector c' into the decoding layer to obtain the vectors output at each time step. The set of vectors at each time step is d. Then the set of vectors d is sent to the reading layer to obtain the probability value of converting the code y into the requirement information x described in natural language.

[0055] In one embodiment of the present invention, the encoder parameters of the code generation model are the same as those of the code verification model.

[0056] In other embodiments, the encoder parameters of the code generation model and the code verification model can also be different.

[0057] Since there is a certain similarity between the code and natural language, setting the encoder parameters of the code verification module and the code generation module to be the same can share knowledge, thereby enhancing the generalization ability of the code generation model and the code verification model, and further improving the accuracy of the code generation model and the code verification model.

[0058] Referring to Figure 3 , an embodiment of the present invention provides a training method for a code generation model. The method may include the following steps:

[0059] Step 301, obtain the initial training sample pairs,

[0060] The initial training sample pair includes: first requirement information, and a first code corresponding to the first requirement information.

[0061] In an embodiment of the present invention, the code corresponding to the first requirement information x' described in natural language is the first code y'. The first requirement information x' and the first code y' form an initial training sample pair (x', y').

[0062] The first code y' is input into a code verification model. The embedding layer of the code verification model can output a third vector set e', and the decoding layer of the code verification model can output a first vector set d'. The first requirement information x' is input into a code generation model. The encoding layer of the code generation model can output a second vector set h', and the vectors at each time step output by the decoding layer of the code generation model form a fourth vector set g'.

[0063] Step 302: Input the first requirement information into the code generation model to obtain a fourth vector set.

[0064] Wherein, the fourth vector set is a set g' composed of the vectors at each time step output by the decoding layer of the code generation model.

[0065] Step 303: Input the fourth vector set into the code verification model, and obtain feedback information of the code verification model regarding a third training sample pair.

[0066] Wherein, the third training sample pair includes: first requirement information, and the fourth vector set.

[0067] That is to say, the training sample pair (x', g') composed of the first requirement information x' and the fourth vector set g' is used as the training sample of the code verification model. The number of the third training sample pairs is not limited, and it can be a single training sample pair or a batch of training sample pairs. Each training sample pair can include: the first requirement information x', and the fourth vector set g'.

[0068] Since the final output of the code generation model is a code, which belongs to a non - continuous variable rather than a vector, directly inputting the final output of the code generation model into the code verification model will cause gradient break.

[0069] In the prior art, a reinforcement learning method is generally used to solve the gradient break problem. However, the reinforcement learning method has problems such as high requirements for data volume, low learning efficiency, and sensitivity to the initial state.

[0070] In the solution of the present invention, the reinforcement learning method is not adopted. Instead, the fourth vector set is directly input into the encoding layer of the code verification model to avoid gradient break and can overcome the defects of the reinforcement learning method.

[0071] Step 304: Calculate the gradient of the code generation model based on the feedback information of the code verification model regarding the third training sample pair.

[0072] In a specific implementation, after obtaining the feedback information of the code verification model regarding the third training sample pair, the gradient of the code generation model can be calculated using the feedback information of the code verification model regarding the third training sample pair.

[0073] In a specific implementation, the code verification model can evaluate the fourth vector set g' and generate feedback information regarding the third training sample pair, so that the knowledge of the code verification model is shared with the code generation model, thereby improving the accuracy of the code generation model.

[0074] In a specific implementation, the feedback information of the code verification model regarding the third training sample pair can reflect whether the code verification model accurately converts the fourth vector set g' into the first requirement information x'. For each fourth vector set g' in the third training sample pair, a feedback information regarding the conversion result of the fourth vector set g' is generated. This feedback information can be gradient information calculated based on the probability value of the code verification model converting the fourth vector set g' into the first requirement information x'. Subsequently, this gradient information is passed back to the code generation model, so that the knowledge of the code verification model is shared with the code generation model, thereby improving the accuracy of the code generation model.

[0075] Step 305: Determine whether the gradient of the code generation model meets the first preset condition.

[0076] In a specific implementation, the first preset condition can be various and is not limited here. In one embodiment, the first preset condition can be: the gradient of the code generation model is less than or equal to the first preset gradient threshold. The first preset gradient threshold can be set according to the actual situation. The gradient of the code generation model being less than or equal to the first preset gradient threshold indicates that the gradient of the code generation model during the current training process is within the allowable range.

[0077] When the gradient of the code generation model meets the first preset condition, execute Step 306; otherwise, execute Step 307.

[0078] Step 306: Use the code generation model that meets the first preset condition as the finally generated code generation model.

[0079] Step 307: Update the parameters of the code generation model.

[0080] In a specific implementation, the parameters of the code generation model are updated based on the gradient of the code generation model. There is no limitation on how to update specifically. After updating the parameters of the code generation model, steps 301 to 307 are executed again, that is, the initial training sample pairs are obtained again, and the code generation model with updated parameters is trained using the newly obtained initial training sample pairs until the gradient of the code generation model meets the first preset condition. Thus, the accuracy of the finally generated code generation model can be improved in the case of fewer training sample pairs.

[0081] In the actual application process, the embedding layer of the code generation model encodes the first requirement information to obtain a second vector set h'. Other parts of the code generation model may directly copy the second vector set h' to obtain a fourth vector set. Then, except for the reading layer of the code verification model, other parts directly copy the fourth vector set to obtain a first vector set. Finally, the reading layer of the code verification model converts the first vector set into the first requirement information, so that the probability p(x'|g') of converting the fourth vector set into the first requirement information is maximized. At this time, only calculating the gradient of the code generation model based on p(x'|g') will cause the code verification model and the code generation model to collapse into simple copy models.

[0082] To avoid the above problems, in one embodiment, referring to Figure 4 , before obtaining the first training sample pair, it can be determined with probability Pc1 whether to input the first requirement information into the code generation model.

[0083] Specifically, before executing step 302, the method may further include:

[0084] Step 308, obtaining a first random number R1.

[0085] Step 309, comparing the value of the first random number R1 with a first preset random number threshold Pc1 to determine whether to train the code generation model using the first training sample pair.

[0086] Specifically, when R1 < Pc1, step 302 can be executed, that is, inputting the first requirement information into the code generation model. When R1 ≥ Pc1, step 310 is executed.

[0087] Step 310, inputting the initial training sample into the code generation model and calculating the gradient of the code generation model.

[0088] In a specific implementation, an initial training sample pair composed of the first requirement information and the first code, that is, (x', y'), is input into the code generation model for training, and the gradient of the code generation model is calculated.

[0089] After performing step 310 to calculate the gradient of the code generation model, step 305 can be executed, that is, to determine whether the gradient of the code generation model meets the first preset condition. When the first preset condition is met, the finally generated code generation model is obtained; otherwise, the parameters of the code generation model are updated.

[0090] During the training process, with a constant probability Pc1, the code verification model is made to reconstruct the original input of the code generation model. At the same time, with a probability of 1 - Pc1, the first demand information x' and the first code y' are used to train the code generation model, so as to prevent the fourth vector set from degenerating into a simple copy of the second vector set.

[0091] In another embodiment of the present invention, referring to Figure 3 , before performing step 308, that is, before obtaining the first random number R1, the following steps may further be included:

[0092] Step 311, obtaining a second random number R2.

[0093] Step 312, comparing the value of the second random number R2 with a second preset random number threshold Pc2 to determine whether to obtain the first random number R1.

[0094] In a specific implementation, specifically, when R2 < Pc2, step 308 can be executed, that is, the first random number R1 is obtained. When R2 ≥ Pc2, step 313 is executed.

[0095] Step 313, inputting the first code into the code verification model to obtain a first vector set.

[0096] The first vector set is a set of vectors at each time step output by the decoding layer of the code verification model after the first code is input into the code verification model.

[0097] Step 314, inputting the first training sample pair into the code generation model for training and obtaining the corresponding code generation task loss.

[0098] The first training sample pair includes: the first vector set and the corresponding first code.

[0099] The first training sample pair (d', y') composed of the first vector set d' and the first code y' is input into the code generation model. The number of the first training sample pairs is not limited and can be a single training sample pair or a batch of training sample pairs. Each first training sample pair can include: the first vector set d' and the first code y' corresponding to the first vector set d'.

[0100] In a specific implementation, calculate the loss of the corresponding code generation task, that is, calculate the loss of the code generation model in the process of converting the first vector set into the code. The loss of the corresponding code generation task refers to the cross-entropy loss l of the code generation model when converting the first vector set d' into the first code y' with parameters θ CG when converting the first vector set d' into the first code y' CG , which can be expressed as:

[0101] l CG = -lg(p(y'|d'; θ CG )) (1)

[0102] After obtaining the probability p(y|d') of converting the first vector set d' into the first code y', the loss l of the corresponding code generation task can be calculated using formula (1). CG .

[0103] Step 315, determine whether the loss of the corresponding code generation task meets the second preset condition.

[0104] In a specific implementation, the second preset condition can be various and is not limited here. In one embodiment, the second preset condition can be: the loss of the code generation task is less than or equal to the first preset loss threshold. The first preset loss threshold can be set according to the actual situation. The loss of the code generation task being less than or equal to the first preset loss threshold indicates that the loss of the code generation task in the current training process is within the allowable range.

[0105] If the loss of the code generation task in the current training process is less than or equal to the first preset loss threshold, then execute step 316, otherwise execute step 317.

[0106] Step 316, use the code generation model that meets the second preset condition as the finally generated code generation model.

[0107] Step 317, update the parameters of the code generation model.

[0108] In a specific implementation, update the parameters θ of the code generation model based on the loss of the code generation task CG , and there is no limit on how to update specifically. After updating the parameters θ of the code generation model CG , re-execute steps 32 to 35, that is, use the obtained training sample pairs to retrain the code generation model with updated parameters until the loss of the code generation task meets the first model loss condition, thereby improving the accuracy of the finally generated code generation model in the case of fewer training sample pairs.

[0109] After performing step 317, the initial training sample pairs can be obtained again, and the code generation model can be trained using the newly obtained initial training sample pairs.

[0110] To further prevent the code verification model and the code generation model from collapsing into simple copy models. In an embodiment of the present invention, referring to Figure 3 , before performing step 313, that is, before inputting the first code into the code verification model, the method may further include:

[0111] Step 318, obtain a third random number R3.

[0112] Step 319, compare the value R3 of the third random number with a third preset random number threshold Pc3 to determine whether to input the first training sample pair into the code generation model.

[0113] Specifically, when R3 < Pc3, perform step 313; otherwise, step 320 can be executed.

[0114] Step 320, input the initial training sample into the code generation model and calculate the code generation task loss.

[0115] In a specific implementation, when it is determined not to input the first training sample pair into the code generation model, the code generation model is trained using the initial training sample pair, and the code generation task loss can be calculated with reference to formula (1).

[0116] After obtaining the code generation task loss through step 320, step 315 can be continued, that is, determine whether the obtained code generation task loss meets the second preset condition. When it does not meet the second preset condition, perform step 317; otherwise, perform step 316.

[0117] Figure 5 It is a schematic diagram of the feedback result of an existing piece of code. Figure 6 It is a schematic diagram of the feedback result of a piece of code in an embodiment of the present invention.

[0118] Referring to Figure 5 , during the process of independently training the code verification model and the code generation model, assume that the content of the first code y' is: i = 5. The first requirement information x' corresponding to the first code y' is: assign 5 to i. And the output of the code verification model for the first code y' is the second requirement information x" as: assign i to 5. At this time, although the second requirement information x" is correct, due to the large difference between the second requirement information x" and the first requirement information x', the code verification model will receive negative feedback, that is, consider the second requirement information x" as the wrong result.

[0119] Referring to Figure 6During the process of training the code generation model using the solution of the embodiment of the present invention, after the vector corresponding to the second requirement information x" is input into the code generation model, the code generation model can restore the vector corresponding to the second requirement information x" to the first code y'. At this time, although there are significant differences between the second requirement information x" and the first requirement information x', if the code generation model can accurately restore the second requirement information x" to the first code y', the code verification model can still receive positive feedback.

[0120] It can be seen from this that the accuracy of the code verification model can be improved by generating feedback information about the first training sample pair.

[0121] It should be noted that for the code verification model, specific implementation can refer to the description of the code verification model training method below. In specific implementation, the code generation model can be trained first, and then the code verification model can be trained. It is also possible to train the code verification model first and then the code generation model, or to train the code generation model and the code verification model simultaneously. There is no limitation here.

[0122] Refer to Figure 7 An embodiment of the present invention also provides a method for training a code verification model, and the method may include the following steps:

[0123] Step 701, obtain an initial training sample pair.

[0124] Wherein, the initial training sample pair includes: first requirement information, and a first code corresponding to the first requirement information.

[0125] Regarding the code generation model and the code verification model, specific reference can be made to the above description of Figure 10 the code generation model and the code verification model, which will not be elaborated here.

[0126] In one embodiment, the code generation model and the code verification model are sequence-to-sequence models, and the encoder parameters of the code generation model are the same as those of the code verification model.

[0127] In other embodiments, the encoder parameters of the code generation model and the code verification model may also be different.

[0128] In an embodiment of the present invention, the code corresponding to the first demand information x' is the first code y'. The first code y' is input into the code verification model. The embedding layer of the code verification model outputs a third vector set e', and the decoding layer of the code verification model outputs a first vector set d'. The first demand information x' is input into the code generation model. The encoding layer of the code generation model outputs a second vector set h', and the vectors at each time step output by the decoding layer of the code generation model form a fourth vector set g'.

[0129] Step 702: Input the first code into the code verification model to obtain a first vector set.

[0130] The first vector set d' is a set of vectors at each time step output by the decoding layer of the code verification model after the first code is input into the code verification model.

[0131] Step 703: Input the first vector set into the code generation model, and obtain the feedback information of the code generation model regarding the first training sample pair.

[0132] The first training sample pair includes: the first vector set and the corresponding first code; the code generation model is used to convert the demand information described in natural language into the corresponding code, and has duality with the code generation model.

[0133] That is to say, the first vector set d' and the corresponding first code y' are combined to form a first training sample pair (d', y') as the training sample of the code generation model. The number of the first training sample pairs is not limited, and can be a single training sample pair or a batch of training sample pairs. Each training sample pair can include: the first vector set d' and the corresponding first code y'.

[0134] Since the final output of the code verification model is the first demand information, which belongs to a non - continuous variable rather than a vector, directly inputting the final output of the code verification model into the code generation model will cause gradient breakage.

[0135] In the prior art, the reinforcement learning method is generally used to solve the gradient breakage problem. However, the reinforcement learning method has problems such as high requirements for the amount of data, low learning efficiency, and sensitivity to the initial state.

[0136] In the solution of the present invention, the reinforcement learning method is not adopted. Instead, the first vector set d' is directly input into the encoding layer of the code generation model to avoid gradient breakage and can overcome the defects of the reinforcement learning method.

[0137] Step 704: Calculate the gradient of the code verification model based on the feedback information of the code generation model regarding the first training sample pair.

[0138] In a specific implementation, the code generation model can evaluate the first vector set d' and generate feedback information about the first training sample pair, so that the knowledge of the code generation model can be shared with the code verification model, thereby improving the accuracy of the code verification model.

[0139] In a specific implementation, the feedback information of the code generation model about the first training sample pair can reflect whether the code generation model accurately converts the first vector set d' into the first code y'. For each first vector set d' in the first training sample pair, a feedback information about the conversion result of the first vector set d' is generated. This feedback information can be gradient information calculated based on the probability value of the code generation model converting the first vector set d' into the first code y'. Subsequently, this gradient information is passed back to the code verification model, so that the knowledge of the code generation model is shared with the code verification model, thereby improving the accuracy of the code verification model.

[0140] Step 705, determine whether the gradient of the code verification model meets the third preset condition.

[0141] In a specific implementation, there can be various third preset conditions, which are not limited here. In one embodiment, the third preset condition can be: the gradient of the code verification model is less than or equal to the second preset gradient threshold. The second preset gradient threshold can be set according to the actual situation. The gradient of the code verification model being less than or equal to the second preset gradient threshold indicates that the gradient of the code verification model is within the allowable range during the current training process.

[0142] When the gradient of the code verification model meets the third preset condition, execute step 706; otherwise, execute step 707.

[0143] Step 706, use the code verification model that meets the third preset condition as the finally generated code verification model.

[0144] Step 707, update the parameters of the code verification model.

[0145] In a specific implementation, there is no limitation on how to update the parameters of the code verification model based on the gradient of the code verification model. After updating the parameters of the code verification model, re-obtain the initial training sample pair, and use the re-obtained initial training sample pair to re-train the code verification model with updated parameters until the gradient of the code verification model meets the third preset condition, thereby improving the accuracy of the finally generated code verification model in the case of fewer training sample pairs.

[0146] In order to prevent the code verification model and the code generation model from collapsing into a simple replication model, in an embodiment of the present invention, before inputting the first code into the code verification model, that is, before executing step 702, the following steps may further be included:

[0147] Step 708: Obtain a fourth random number R4.

[0148] Step 709: Compare the value of the fourth random number R4 with a fourth preset random number threshold Pc4 to determine whether to input the first code into the code verification model.

[0149] Specifically, when R4 < Pc4, step 702 may be executed, that is, input the first code into the code verification model. When R4 ≥ Pc4, execute step 710.

[0150] Step 710: Input the initial training sample into the code verification model and calculate the gradient of the code verification model.

[0151] In a specific implementation, an initial training sample pair composed of the first requirement information and the first code, that is, (x', y'), is input into the code verification model, and the gradient of the code verification model is calculated.

[0152] After calculating the gradient of the code generation model by executing step 710, step 705 may be executed, that is, determine whether the gradient of the code verification model meets a third preset condition. When the third preset condition is met, obtain the finally generated code verification model; otherwise, update the parameters of the code verification model.

[0153] In another embodiment of the present invention, referring to Figure 7 , before executing step 708, that is, before obtaining the fourth random number R4, the following steps may further be included:

[0154] Step 711: Obtain a fifth random number R5.

[0155] Step 712: Compare the value of the fifth random number R5 with a fifth preset random number threshold Pc5 to determine whether to obtain the fourth random number.

[0156] Specifically, when R5 < Pc5, step 708 may be executed, that is, obtain the fourth random number. When R1 ≥ Pc1, execute step 713.

[0157] Step 713: Input the first requirement information into the code generation model to obtain a fourth vector set.

[0158] The fourth vector set is a set of vectors at each time step output by the decoding layer of the code generation model after inputting the first requirement information into the code generation model.

[0159] Step 714: Input the third training sample pair into the code verification model and obtain the corresponding code verification task loss.

[0160] The third training sample pair includes: first requirement information and a fourth vector set.

[0161] Use the training sample pair (x’, g’) composed of the first requirement information x’ and the fourth vector set g’ as the training sample of the code verification model. The number of the third training sample pairs is not limited and can be a single training sample pair or a batch of training sample pairs. Each training sample pair can include: the first requirement information x’ and the fourth vector set g’.

[0162] Refer to Figure 8 , input the fourth vector set g’ into the encoding layer of the code verification model. After being encoded by the encoding layer, a vector integrating context can be obtained, and then after passing through the decoding layer, the probability p(x’|g’) of converting the fourth vector set g’ into the first requirement information x’ can be obtained.

[0163] In a specific implementation, calculate the corresponding code verification task loss, that is, calculate the loss of the code verification model in the process of converting the fourth vector set into the first requirement information. The code verification task loss in the current training process refers to the cross-entropy loss l of the code verification model in the process of converting the fourth vector set into the first requirement information when the parameter is θ CS and can be expressed as: CS

[0164] l CS = -lg(p(x|y; θ CS )) (2)

[0165] After obtaining the probability p(x’|g’) of converting the fourth vector set g’ into the first requirement information x’, the corresponding code verification task loss l can be calculated using formula (2). CS .

[0166] Step 715: Determine whether the code verification task loss meets the fourth preset condition.

[0167] In a specific implementation, the fourth preset condition can be various and is not limited here. In one embodiment, the fourth preset condition can be: the code verification task loss is less than or equal to the second preset loss threshold. The second preset loss threshold can be set according to the actual situation. The code verification task loss being less than or equal to the second preset loss threshold indicates that the code verification task loss is within the allowable range. Among them, the second preset loss threshold can be equal to or not equal to the first preset model loss threshold, which is not limited here.

[0168] When the loss of the code verification task is less than or equal to the second preset loss threshold, it indicates that the loss of the code verification task in the current training process meets the fourth preset condition. At this time, step 716 can be continued; otherwise, step 717 is executed.

[0169] Step 716: Use the code verification model that meets the fourth preset condition as the finally generated code verification model.

[0170] Step 717: Update the parameters of the code verification model.

[0171] In a specific implementation, based on the loss of the code verification task in the current training process, update the parameters θ of the code verification model CS , and there is no limitation on how to update specifically. After updating the parameters θ of the code verification model CS , re-execute steps 713 to 717, that is, use the obtained third training sample pair to retrain the code verification model with updated parameters until the loss of the code verification task of the code verification model meets the fourth preset condition. Thus, the accuracy of the finally generated code verification model can be improved in the case of fewer training sample pairs.

[0172] In an embodiment of the present invention, in order to further prevent the code verification model and the code generation model from collapsing into simple copy models, before inputting the first requirement information into the code generation model, that is, before executing step 713, it may further include:

[0173] Step 718: Obtain the sixth random number R6;

[0174] Step 719: Compare the value R6 of the sixth random number with the sixth preset random number threshold Pc6 to determine whether to use the third training sample pair to train the code verification model.

[0175] Specifically, when R6 < Pc6, step 713 can be executed, that is, input the first requirement information into the code generation model. When R6 ≥ Pc6, step 720 is executed.

[0176] Step 720: Input the initial training sample into the code verification model and calculate the loss of the code verification task.

[0177] In a specific implementation, when it is determined not to input the third training sample pair into the code verification model, use the initial training sample pair to train the code verification model, and the loss of the code verification task can be calculated with reference to formula (2).

[0178] After obtaining the code verification task loss through step 720, step 715 can be continued, that is, to determine whether the obtained code verification task loss meets the fourth preset condition. When the fourth preset condition is not met, step 717 is executed; otherwise, step 716 is executed.

[0179] In specific implementation, regarding the feedback information of the third training sample pair, it can be fed back whether the code verification model accurately converts the fourth vector set g' into the first requirement information x'. For each fourth vector set g' in the third training sample pair, a feedback information about the conversion result of the fourth vector set g' is generated. The feedback information can be gradient information calculated based on the probability value of the code verification model converting the fourth vector set g' into the first requirement information x'. Subsequently, the gradient information is passed back to the code generation model, so that the knowledge of the code verification model can be shared with the code generation model, thereby improving the accuracy of the code verification model.

[0180] It should be noted that regarding the code generation model, reference can be specifically made to Figure 3 the description of the training method of the code generation model in for implementation. In specific implementation, the code generation model can be trained first, and then the code verification model can be trained; or the code verification model can be trained first, and then the code generation model can be trained; or the code generation model and the code verification model can be trained simultaneously, which is not limited here.

[0181] Referring to Figure 9 , an embodiment of the present invention further provides a code generation method, and the method may include the following steps:

[0182] Step 91, input the requirement information described in natural language into the trained code generation model to obtain two or more candidate codes, and the first probability value of converting the first requirement information into the corresponding candidate codes.

[0183] Among them, the code generation model can be trained by using the training method of the code generation model in the embodiment of the present invention.

[0184] For example, referring to Figure 10 , assume that the requirement information described in natural language is: The original price of the ticket is 200. After passing through the code generation model, K candidate codes can be obtained, which are candidate code y1, candidate code y2,..., candidate code yK respectively. Among them, candidate code y1 is: int Price = 200. Candidate code y2 is: int price = 200. Candidate code yK is: int price.

[0185] The decoder of the code generation model can output a first probability value that converts the requirement information described in the natural language into corresponding candidate codes. Among them, the first probability value of converting the requirement information x described in the natural language into the candidate code y1 can be expressed as p(y1|x). The first probability value of converting the requirement information x described in the natural language into the candidate code y2 can be expressed as p(y2|x). …… The first probability value of converting the requirement information x described in the natural language into the candidate code yK can be expressed as p(yK|x).

[0186] Step 92: Input the candidate codes output by the code generation model into the trained code verification model to obtain a second probability value that converts each of the candidate codes into the requirement information described in the natural language.

[0187] Among them, the code verification model is trained by using the training method of the code verification model in the embodiments of the present invention.

[0188] Specifically, referring to Figure 10 , after inputting the candidate codes y1 to yK into the code verification model, the codes corresponding to the candidate codes y1 to yK that are respectively converted into the requirement information x described in the natural language can be obtained. For example, the second probability value of converting the candidate code y1 into the requirement information x described in the natural language can be expressed as p(x|y1). The second probability value of converting the candidate code y2 into the requirement information x described in the natural language can be expressed as p(x|y2). …… The second probability value of converting the candidate code yK into the requirement information x described in the natural language can be expressed as p(x|yK).

[0189] Step 93: Determine the code corresponding to the requirement information described in the natural language based on the product of the first probability value and the corresponding second probability value.

[0190] Take the product of the first probability value and the corresponding second probability value as the score of the candidate code, and thus select the candidate code with the highest score as the finally generated code. For example, the score of the candidate code y1 can be expressed as p(y1|x)*p(x|y1). The score of the candidate code y2 can be expressed as p(y2|x)*p(x|y2). …… The score of the candidate code yK can be expressed as p(yK|x)*p(x|yK). Assume that the score of the candidate code y2 is the highest, then the candidate code y2 is the finally generated code, that is: int price = 200.

[0191] In other embodiments, other methods can also be used to generate codes, which will not be exemplified one by one here.

[0192] By using the code generation method in the embodiments of the present invention, since the accuracy of at least one of the code generation model and the code verification model is improved, the accuracy of the finally generated code can be improved. It can be understood that when the accuracies of both the code generation model and the code verification model are improved, the scheme of generating code using the code generation model and the code verification model naturally has a higher accuracy.

[0193] To enable those skilled in the art to better understand and implement the present invention, the corresponding apparatus, electronic device, and computer-readable storage medium of the above method will be described in detail below.

[0194] Referring to Figure 11 , the embodiments of the present invention further provide a training apparatus 100 for a code generation model. The apparatus 100 may include: a first acquisition unit 101, a fourth vector acquisition unit 102, a first training unit 103, a first gradient calculation unit 104, a first parameter update unit 105, and a first control unit 106. Among them:

[0195] The first acquisition unit 101 is adapted to acquire an initial training sample pair, and the initial training sample pair includes: first requirement information, and a first code corresponding to the first requirement information;

[0196] The fourth vector acquisition unit 102 is adapted to input the first requirement information into the code generation model to obtain a fourth vector set; the fourth vector set is a set composed of vectors at each time step output by the decoding layer of the code generation model;

[0197] The first training unit 103 is adapted to input the fourth vector set into the code verification model and obtain feedback information of the code verification model on a third training sample pair; the third training sample pair includes: first requirement information, and the fourth vector set; the code verification model is used to convert a code into corresponding natural language description requirement information and has duality with the code generation model;

[0198] The first gradient calculation unit 104 is adapted to calculate the gradient of the code generation model based on the feedback information of the code verification model on the third training sample pair;

[0199] The first parameter update unit is adapted to update the parameters of the code generation model;

[0200] The first control unit is adapted to determine whether the gradient of the code generation model meets a first preset condition. When the gradient of the code generation model does not meet the first preset condition, the first training unit 103, the first gradient calculation unit 104, and the first parameter update unit 105 are controlled to repeatedly execute corresponding operations until the gradient of the code generation model meets the first preset condition. The code generation model that meets the first preset condition is used as the finally generated code generation model.

[0201] For each of the above functional units, specific implementation can refer to the description of the code generation model training method, which will not be elaborated here.

[0202] Refer to Figure 12 In addition, an embodiment of the present invention further provides a training device 110 for a code verification model. The device 110 may include: a second acquisition unit 111, a first vector acquisition unit 112, a second training unit 113, a second gradient calculation unit 114, a second parameter update unit 115, and a second control unit 116. Among them:

[0203] The second acquisition unit 111 is adapted to acquire an initial training sample pair, where the initial training sample pair includes: first requirement information, and a first code corresponding to the first requirement information;

[0204] The first vector acquisition unit 112 is adapted to input the first code into the code verification model to obtain a first vector set; the first vector set is a set of vectors at each time step output by the decoding layer of the code verification model after the first code is input into the code verification model;

[0205] The second training unit 113 is adapted to input the first vector set into the code generation model and obtain feedback information of the code generation model about the first training sample pair; the first training sample pair includes: the first vector set, and the corresponding first code; the code generation model is used to convert requirement information described in natural language into corresponding code, and has duality with the code generation model;

[0206] The second gradient calculation unit 114 is adapted to calculate the gradient of the code verification model based on the feedback information of the code generation model about the first training sample pair;

[0207] The second parameter update unit 115 is adapted to update the parameters of the code verification model;

[0208] The second control unit 116 is adapted to determine whether the gradient of the code verification model meets a second preset condition, and when the gradient of the code verification model meets the second preset condition, control the second training unit 113, the second gradient calculation unit 114, and the second parameter update unit 114 to repeatedly execute corresponding operations until the gradient of the code verification model meets the second preset condition, and use the code verification model that meets the second preset condition as the finally generated code verification model.

[0209] Regarding each of the above functional units, specific implementation can be referred to the description of the code verification model training method, which will not be elaborated here.

[0210] Refer to Figure 13 , an embodiment of the present invention further provides a code generation device 120, which may include: a code generation unit 121, a code verification unit 122, and a code determination unit 123. Among them:

[0211] The code generation unit 121 is adapted to input the requirement information described in natural language into the trained code generation model to obtain two or more candidate codes, and a first probability value for converting the first requirement information into the corresponding candidate codes; the code generation model is trained by the training device 100 of the above code generation model;

[0212] The code verification unit 122 is adapted to input the candidate codes output by the code generation model into the trained code verification model to obtain a second probability value for converting each of the candidate codes into the requirement information described in natural language; the code verification model is trained by the training device 110 of the above code verification model;

[0213] The code determination unit 123 is adapted to determine the code corresponding to the requirement information described in natural language based on the product of the first probability value and the corresponding second probability value.

[0214] Regarding each functional unit of the device 120, specific implementation can be referred to the above description of the code generation method, which will not be elaborated here.

[0215] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. The computer program is executed by a processor to implement the steps of any one of the above code generation model training methods, or to implement the steps of any one of the above code verification model training methods, or to implement the steps of any one of the above code generation methods.

[0216] In specific implementation, the computer-readable storage medium may include: ROM, RAM, disk, or optical disc, etc.

[0217] An embodiment of the present invention further provides an electronic device, including a memory and a processor. A computer program capable of running on the processor is stored on the memory. When the processor runs the computer program, it executes the steps of any one of the above-mentioned training methods of the code generation model, or executes the steps of any one of the above-mentioned training methods of the code verification model, or executes the steps of any one of the above-mentioned code generation methods.

[0218] Regarding each module / unit included in the various devices and products described in the above embodiments, it can be a software module / unit, a hardware module / unit, or it can also be partially a software module / unit and partially a hardware module / unit. For example, for each device and product applied to or integrated into a chip, each module / unit included therein can be implemented in a hardware manner such as a circuit. Or, at least some of the modules / units can be implemented in the form of a software program that runs on a processor integrated inside the chip, and the remaining (if any) part of the modules / units can be implemented in a hardware manner such as a circuit; for each device and product applied to or integrated into a chip module, each module / unit included therein can be implemented in a hardware manner such as a circuit. Different modules / units can be located in the same component (such as a chip, a circuit module, etc.) or different components of the chip module. Or, at least some of the modules / units can be implemented in the form of a software program that runs on a processor integrated inside the chip module, and the remaining (if any) part of the modules / units can be implemented in a hardware manner such as a circuit; for each device and product applied to or integrated into a terminal, each module / unit included therein can be implemented in a hardware manner such as a circuit. Different modules / units can be located in the same component (such as a chip, a circuit module, etc.) or different components inside the terminal. Or, at least some of the modules / units can be implemented in the form of a software program that runs on a processor integrated inside the terminal, and the remaining (if any) part of the modules / units can be implemented in a hardware manner such as a circuit.

[0219] Although the present invention is disclosed as above, the present invention is not limited thereto. Any person skilled in the art can make various changes and modifications without departing from the spirit and scope of the present invention. Therefore, the protection scope of the present invention should be subject to the scope defined by the claims.

Claims

1. A training method for a code generation model, characterized in that, The code generation model is used to convert requirement information described in natural language into corresponding code; The method includes: Obtaining an initial training sample pair, where the initial training sample pair includes: first requirement information, and a first code corresponding to the first requirement information; Inputting the first requirement information into the code generation model to obtain a fourth vector set; the fourth vector set is a set composed of vectors at each time step output by the decoding layer of the code generation model; Inputting the fourth vector set into a code verification model, and obtaining feedback information of the code verification model about a third training sample pair; the third training sample pair includes: first requirement information, and the fourth vector set; the code verification model is used to convert code into corresponding requirement information described in natural language, and has duality with the code generation model; Calculating the gradient of the code generation model based on the feedback information of the code verification model about the third training sample pair; When the gradient of the code generation model does not meet a first preset condition, updating the parameters of the code generation model until the gradient of the code generation model meets the first preset condition, and using the code generation model that meets the first preset condition as the finally generated code generation model.

2. The training method of the code generation model according to claim 1, wherein Before inputting the first requirement information into the code generation model, it further includes: Obtaining a first random number; Comparing the value of the first random number with a first preset random number threshold to determine whether to input the first requirement information into the code generation model.

3. The training method of the code generation model according to claim 2, wherein When it is determined not to input the first requirement information into the code generation model, it further includes: Inputting the initial training sample into the code generation model, and calculating the gradient of the code generation model.

4. The training method of the code generation model according to claim 2, wherein Before obtaining the first random number, it further includes: Obtaining a second random number; Comparing the value of the second random number with a second preset random number threshold to determine whether to obtain the first random number.

5. The training method of the code generation model according to claim 4, wherein When it is determined not to obtain the first random number, it further includes: Inputting the first code into the code verification model to obtain a first vector set; the first vector set is a set of vectors at each time step output by the decoding layer of the code verification model after inputting the first code into the code verification model; Inputting a first training sample pair into the code generation model, and obtaining a corresponding code generation task loss; the first training sample pair includes: the first vector set, and the corresponding first code; when the code generation task loss does not meet a second preset condition, updating the parameters of the code generation model until the code generation task loss meets the first preset condition, and using the code generation model that meets the first preset condition as the finally generated code generation model.

6. The training method of the code generation model according to claim 5, wherein Before training the code generation model using the first training sample pair, it further includes: Obtaining a third random number; Comparing the value of the third random number with a third preset random number threshold to determine whether to input the first training sample pair into the code generation model.

7. The training method of the code generation model according to claim 6, characterized in that, When it is determined not to input the first training sample pair into the code generation model, it further includes: Input the initial training samples into the code generation model, and calculate the code generation task loss.

8. The training method of the code generation model according to claim 1, characterized in that The code generation model and the code verification model are sequence-to-sequence models, and the encoder parameters of the code generation model are the same as those of the code verification model.

9. A training method for a code verification model, characterized in that, The code verification model is used to convert the requirement information described in natural language into corresponding code; the method includes: Obtain an initial training sample pair, where the initial training sample pair includes: first requirement information, and the first code corresponding to the first requirement information. Input the first code into the code verification model to obtain a first vector set; the first vector set is a set of vectors at each time step output by the decoding layer of the code verification model after the first code is input into the code verification model. Input the first vector set into the code generation model, and obtain the feedback information of the code generation model about the first training sample pair; the first training sample pair includes: the first vector set, and the corresponding first code; the code generation model is used to convert the requirement information described in natural language into corresponding code, and has duality with the code generation model. Calculate the gradient of the code verification model based on the feedback information of the code generation model about the first training sample pair. When the gradient of the code verification model does not meet the third preset condition, update the parameters of the code verification model until the gradient of the code verification model meets the third preset condition, and use the code verification model that meets the third preset condition as the finally generated code verification model.

10. The training method of the code verification model according to claim 9, wherein Before inputting the first code into the code verification model, it further includes: Obtain a fourth random number. Compare the value of the fourth random number with a fourth preset random number threshold to determine whether to input the first code into the code verification model.

11. The training method of the code verification model according to claim 10, characterized in that, When it is determined not to input the first code into the code verification model, it further includes: Input the initial training samples into the code verification model, and calculate the gradient of the code generation model.

12. The training method of the code verification model according to claim 10, wherein Before obtaining the fourth random number, it further includes: Obtain a fifth random number. Compare the value of the fifth random number with a fifth preset random number threshold to determine whether to obtain the fourth random number.

13. The training method of the code verification model according to claim 12, wherein, When it is determined not to obtain the fourth random number, it further includes: Input the first requirement information into the code generation model to obtain a fourth vector set; the fourth vector set is a set of vectors at each time step output by the decoding layer of the code generation model after the first requirement information is input into the code generation model. Input the third training sample pair into the code verification model, and obtain the corresponding code verification task loss; the third training sample pair includes: the first requirement information, and the fourth vector set. When the code verification task loss does not meet the fourth preset condition, update the parameters of the code verification model until the code verification task loss meets the fourth preset condition, and use the code verification model that meets the fourth preset condition as the finally generated code verification model.

14. The training method of the code verification model according to claim 13, characterized in that Before training the code verification model using the third training sample pair, the following steps are also included: Obtain a sixth random number; Compare the value of the sixth random number with a sixth preset random number threshold to determine whether to train the code verification model using the third training sample pair.

15. The training method of the code verification model according to claim 14, characterized in that, When it is determined not to train the code verification model using the third training sample pair, the following steps are also included: Input the initial training sample into the code verification model and calculate the code verification task loss.

16. The training method of the code verification model according to claim 9, wherein The code generation model and the code verification model are sequence-to-sequence models, and the encoder parameters of the code generation model are the same as those of the code verification model.

17. A code generation method, characterized in that, It includes: Input the requirement information described in natural language into the trained code generation model to obtain two or more candidate codes, and a first probability value for converting the first requirement information into the corresponding candidate codes; the code generation model is trained using the training method of the code generation model according to any one of claims 1 to 8; Input the candidate codes output by the code generation model into the trained code verification model to obtain a second probability value for converting each of the candidate codes into the requirement information described in natural language; the code verification model is trained using the training method of the code verification model according to any one of claims 9 to 19; Based on the product of the first probability value and the corresponding second probability value, determine the code corresponding to the requirement information described in natural language.

18. A training device for a code generation model, characterized in that, The code generation model is used to convert the requirement information described in natural language into the corresponding code; The device includes: A first acquisition unit, adapted to acquire an initial training sample pair, where the initial training sample pair includes: first requirement information, and a first code corresponding to the first requirement information; A fourth vector acquisition unit, adapted to input the first requirement information into the code generation model to obtain a fourth vector set; the fourth vector set is a set composed of the vectors at each time step output by the decoding layer of the code generation model; A first training unit, adapted to input the fourth vector set into the code verification model and obtain the feedback information of the code verification model regarding the third training sample pair; the third training sample pair includes: first requirement information, and the fourth vector set; the code verification model is used to convert the code into the corresponding requirement information described in natural language and has duality with the code generation model; A first gradient calculation unit, adapted to calculate the gradient of the code generation model based on the feedback information of the code verification model regarding the third training sample pair; A first parameter update unit, adapted to update the parameters of the code generation model; and a first control unit, adapted to determine whether the gradient of the code generation model meets a first preset condition, and when the gradient of the code generation model does not meet the first preset condition, control the first training unit, the first gradient calculation unit, and the first parameter update unit to repeatedly execute corresponding operations until the gradient of the code generation model meets the first preset condition, and use the code generation model that meets the first preset condition as the finally generated code generation model.

19. A training device for a code verification model, characterized in that, The code verification model is used to convert the requirement information described in natural language into corresponding code; The device includes: A second acquisition unit, adapted to acquire an initial training sample pair, where the initial training sample pair includes: first requirement information, and a first code corresponding to the first requirement information; A first vector acquisition unit, adapted to input the first code into the code verification model to obtain a first vector set; the first vector set is a set of vectors at each time step output by the decoding layer of the code verification model after the first code is input into the code verification model; A second training unit, adapted to input the first vector set into the code generation model and obtain feedback information of the code generation model about the first training sample pair; the first training sample pair includes: the first vector set, and the corresponding first code; the code generation model is used to convert the requirement information described in natural language into corresponding code, and has duality with the code generation model; A second gradient calculation unit, adapted to calculate the gradient of the code verification model based on the feedback information of the code generation model about the first training sample pair; A second parameter update unit, adapted to update the parameters of the code verification model; and a second control unit, adapted to determine whether the gradient of the code verification model meets a second preset condition, and when the gradient of the code verification model meets the second preset condition, control the second training unit, the second gradient calculation unit, and the second parameter update unit to repeatedly execute corresponding operations until the gradient of the code verification model meets the second preset condition, and use the code verification model that meets the second preset condition as the finally generated code verification model.

20. A code generation device, characterized in that, including: A code generation unit, adapted to input the requirement information described in natural language into the trained code generation model to obtain two or more candidate codes, and a first probability value for converting the first requirement information into the corresponding candidate code; the code generation model is trained by using the training device of the code generation model described in claim 18; A code verification unit, adapted to input the candidate codes output by the code generation model into the trained code verification model to obtain a second probability value for converting each of the candidate codes into the requirement information described in natural language; the code verification model is trained by using the training device of the code verification model described in claim 19; A code determination unit, adapted to determine the code corresponding to the requirement information described in natural language based on the product of the first probability value and the corresponding second probability value.

21. A computer-readable storage medium having a computer program stored thereon, characterized in that, The computer program is executed by a processor to implement the steps of the method according to any one of claims 1 to 17.

22. An electronic device, comprising a memory and a processor, wherein a computer program capable of running on the processor is stored on the memory, characterized in that When the processor runs the computer program, it executes the steps of the method according to any one of claims 1 to 17.