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

By calculating the dual loss between the code generation model and the code verification model and updating the model parameters, the problem of low accuracy of code generation and verification models in the existing technology is solved, and more efficient code generation and verification is achieved.

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

Patent Information

Application Number
CN202311669214.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-12-06
Publication Date
2025-06-13

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, while training samples are scarce.

Method used

By calculating the dual loss between the code generation model and the code verification model, the model loss of the code generation model is obtained, and the parameters of the code generation model are updated based on this until the model loss meets a certain condition. Similarly, the code verification model is processed similarly to improve its accuracy.

Benefits of technology

Through this method, the knowledge in the training samples can be more fully explored, thereby improving the accuracy of the code generation model and code verification model and improving the quality of the generated code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120144098A_ABST
    Figure CN120144098A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation and verification model training method, generation method, device and equipment. The method comprises the following steps: acquiring a training sample pair; training the code generation model by using the obtained training sample pair; calculating code generation task loss in the current training operation and dual loss between the code generation model and the code verification model to obtain model loss of the code generation model; the code verification model converts a code into a model of demand information described by a corresponding natural language, and has duality with the code generation model; when the model loss of the code generation model does not meet the first model loss condition, parameters of the code generation model are updated based on the model loss of the code generation model, and the code generation model after parameter updating is trained again. 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 candidate code is simple and effective and does not restrict the internal structure of the code generation model, which can improve the quality of the generated code.

[0005] However, the current accuracy of both the code generation model and the code verification model is relatively low. 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 a model that converts requirement information described in natural language into corresponding code; the method includes: obtaining training sample pairs, where the training sample pairs include: requirement information described in natural language, and the code corresponding to the requirement information described in natural language; using the obtained training sample pairs to train the code generation model; calculating the code generation task loss in the current training operation, and the dual loss between the code generation model and the code verification model, to obtain the model loss of the code generation model; the code verification model is a model that converts code into corresponding requirement information described in natural language, and has duality with the code generation model; when the model loss of the code generation model does not meet the first model loss condition, updating the parameters of the code generation model based on the model loss of the code generation model, and retraining the code generation model with the updated parameters until the model loss of the code generation model meets the first model loss condition, and taking the code generation model that meets the first model loss 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 a model that converts requirement information described in natural language into corresponding code; the method includes: obtaining training sample pairs, where the training sample pairs include: requirement information described in natural language, and the code corresponding to the requirement information described in natural language; using the obtained training sample pairs to train the code verification model; calculating the code verification task loss in the current training operation, and the dual loss between the code generation model and the code verification model, to obtain the model loss of the code verification model; the code generation model is a model that converts requirement information described in natural language into corresponding code, and has duality with the code verification model; when the model loss of the code verification model does not meet the second model loss condition, updating the parameters of the code verification model based on the model loss of the code verification model, and retraining the code verification model with the updated parameters until the model loss of the code verification model meets the second model loss condition, and taking the code verification model that meets the second model loss condition as the finally generated code verification model.

[0010] An embodiment of the present invention also provides a code generation method, the method includes: inputting 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 requirement information described in natural language into the corresponding candidate codes; the code generation model is trained by using any one of the above code generation model training methods; determining the code corresponding to the requirement information described in natural language based on the first probability value.

[0011] An embodiment of the present invention further provides a training device for a code generation model, where the code generation model is a model that converts requirement information described in natural language into corresponding code; the device includes: a first acquisition unit, adapted to acquire a plurality of training sample pairs, where each training sample pair includes: requirement information described in natural language, and the code corresponding to the requirement information described in natural language; a first training unit, adapted to use the acquired training sample pairs to train the code generation model; a first calculation unit, adapted to calculate the code generation task loss in the current training operation, and the duality loss between the code generation model and the code verification model, to obtain the model loss of the code generation model; the code verification model is a model that converts code into corresponding requirement information described in natural language, and there is a duality with the code generation model; a first parameter update unit, adapted to update the parameters of the code generation model based on the model loss of the code generation model; and a first control unit, adapted to determine whether the model loss of the code generation model satisfies a first model loss condition, and when the model loss of the code generation model does not satisfy the first model loss condition, control the first training unit, the first calculation unit, and the first parameter update unit to repeatedly execute corresponding operations until the model loss of the code generation model satisfies the first model loss condition, and use the code generation model that satisfies the first model loss 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 a model that converts requirement information described in natural language into corresponding code; the device includes: a second acquisition unit, adapted to acquire a plurality of training sample pairs, where each training sample pair includes: requirement information described in natural language, and the code corresponding to the requirement information described in natural language; a second training unit, adapted to use the acquired training sample pairs to train the code verification model; a second calculation unit, adapted to calculate the code verification task loss in the current training operation, and the duality loss between the code generation model and the code verification model, to obtain the model loss of the code verification model; the code generation model is a model that converts requirement information described in natural language into corresponding code; a second parameter update unit, adapted to update the parameters of the code verification model based on the model loss of the code verification model; and a second control unit, adapted to determine whether the model loss of the code verification model satisfies a second model loss condition; and when the model loss of the code verification model does not satisfy the second model loss condition, control the second training unit, the second calculation unit, and the second parameter update unit to repeatedly execute corresponding operations until the model loss of the code verification model satisfies the second model loss condition, and use the code verification model that satisfies the second model loss condition as the finally generated code verification model.

[0013] An embodiment of the present invention further provides a code generation device, which includes: a code generation unit, adapted to obtain a trained code generation model, input requirement information described in natural language into the obtained code generation model to obtain two or more candidate codes, and convert the requirement information described in natural language into a first probability value corresponding to the candidate code; the code generation model is trained by using the training device of the above-mentioned code generation model; and a code determination unit, adapted to determine the code corresponding to the requirement information described in natural language based on the first 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, and a computer program capable of running on the processor is stored on the memory. It is 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, since the code verification model is a model that converts a code into corresponding requirement information described in natural language and has duality with the code generation model, therefore, the model loss of the code generation model is obtained through the duality loss between the code generation model and the code verification model, and the finally generated code generation model is obtained based on the model loss of the code generation model, so that the knowledge in the training samples can be more fully mined, thereby improving the accuracy of the finally obtained code generation model.

[0018] Since the code generation model is a model that converts requirement information described in natural language into corresponding codes and has duality with the code verification model, therefore, by calculating the duality loss between the code generation model and the code verification model and obtaining the finally generated code verification model based on the model loss of the code verification model, the knowledge in the training samples can be more fully mined, thereby improving the accuracy of the finally obtained 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 It is a flowchart of a method for training a code generation model in an embodiment of the present invention;

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

[0023] Figure 5 It is a flowchart of a code generation method in an embodiment of the present invention;

[0024] Figure 6 It is a schematic diagram of a code generation process in an embodiment of the present invention;

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

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

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

[0028] 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.

[0029] Existing code generation models based on deep learning 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.

[0030] 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 a higher probability, and these shorter or more general candidate codes may miss rare functions in the requirements, or contain common code segments not mentioned in the requirements, affecting the accuracy of the code generation model.

[0031] In order to improve the accuracy of the code generation model, a scheme of using a code verification model to verify the candidate codes generated by the code generation model is proposed. Specifically, refer to Figure 2, the first requirement is input into the code generation model to obtain K candidate codes (i.e., candidate code 1 to candidate code K, where K≥2 and K is an integer), and the first probability value for generating each candidate code is calculated. Then, for each candidate code, it is separately input into the code verification model, and the code verification model calculates the second probability value of converting each candidate code into the first requirement information. Next, the product of the first probability value and the second probability value corresponding to the same candidate code is used as the score of the candidate code, and the candidate code with the highest score is selected as the final code.

[0032] The code verification model verifies whether the candidate code and the first requirement information are semantically consistent and filters out the candidate codes that are semantically inconsistent with the first requirement. This method of using the code verification model to verify candidate codes 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.

[0033] 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 difficult and the training samples are scarce, the accuracies of the current code generation model and the code verification model are both low.

[0034] To address this problem, the present invention provides a method for training a code generation model. By applying this method, since the code verification model is a model that converts code into the corresponding requirement information described in natural language and there is a duality between the code generation model and the code verification model, the model loss of the code generation model is obtained by calculating the duality loss between the code generation model and the code verification model. Therefore, the accuracies of the finally obtained code generation model and the code verification model can be improved.

[0035] The embodiment of the present invention also provides a method for training a code verification model. By applying this method, since the code generation model is a model that converts the requirement information described in natural language into the corresponding code and there is a duality between the code generation model and the code verification model, the model loss of the code verification model is obtained by calculating the duality loss between the code generation model and the code verification model, which can improve the accuracies of the finally obtained code generation model and the code verification model.

[0036] To make the above objects, features, and advantages of the present invention more obvious and understandable, the specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings.

[0037] Refer to Figure 3, embodiments of the present invention provide a method for training a code generation model, which is a model that converts requirement information described in natural language into corresponding code. The method may include the following steps:

[0038] Step 31, obtain training sample pairs, where the training sample pairs include: requirement information described in natural language, and the code corresponding to the requirement information described in natural language.

[0039] In a specific implementation, the number of obtained 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 may include: requirement information x described in natural language, and the code y corresponding to the requirement information described in natural language.

[0040] Step 32, use the obtained training sample pairs to train the code generation model.

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

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

[0043] Specifically, referring to Figure 6 , 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 vectors corresponding to 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.

[0044] 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.

[0045] During the training process, the decoder is used to calculate the probability value of converting the requirement information x described in natural language into code y. Specifically, the decoder first sends the vector c output by the encoding layer into the decoding layer to obtain the vector output at each time step. Then, the reading layer calculates the probability value of converting the requirement information x described in natural language into code y based on the vectors output at each time step and outputs it.

[0046] During the actual prediction process, the decoder sends the vector c output by the encoding layer into the decoding layer to obtain the vector output at each time step. Then, the reading layer, based on the vectors output at each time step, 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, it outputs the first probability value that converts the requirement information x described in natural language into each candidate code.

[0047] Among them, the beam search algorithm is a commonly used algorithm in the field of natural language processing for generating 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 a 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.

[0048] Step 33, calculate the model loss of the code generation model.

[0049] Specifically, calculate the code generation task loss in the current training operation and the duality loss between the code generation model and the code verification model to obtain the model loss of the code generation model.

[0050] Among them, the code verification model is a model that converts code into the corresponding requirement information described in natural language and has duality with the code generation model.

[0051] In a specific implementation, the duality between the two models 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 the 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.

[0052] There is a certain probability correlation between the code generation model and the code verification model with duality, specifically as shown in formula (1):

[0053] p(x,y) = p(y|x) * p(x) = p(x|y) * p(y) (1)

[0054] Among them, p(x) and p(y) are the occurrence probabilities of the requirement information x described in natural language and the code y in the dataset.

[0055] When calculating the duality loss between the code generation model and the code verification model, the duality loss between the code generation model and the code verification model can be calculated based on the probability correlation between the requirement information described in natural language and the corresponding code in the obtained training sample pairs.

[0056] Specifically, the dual loss l between the code generation model and the code verification model dual can be expressed as:

[0057] l dual = (lg(p(y|x)p(x)) - lg(p(x|y)p(y))) 2 (2)

[0058] In an embodiment of the present invention, the dual loss between the code generation model and the code verification model can be obtained based on the code generation task loss in the current training operation, the model loss of the code verification model in the current training operation, the model loss of the natural language model, and the model loss of the code language model;

[0059] wherein, the model of the natural language model is a language model trained only with information requirements described in natural language; the code language model is a language model trained only with code.

[0060] Specifically, assuming the parameters of the code generation model are θ CG , and the parameters of the code verification model are θ CS , the parameters of are θ NL , and the parameters of the code language model are θ CODE , then the dual loss l between the code generation model and the code verification model dual can be further expressed as:

[0061] l dual = (lg(p(y|x; θ CG )) + lg(p(x; θ NL )) - lg(p(x|y; θ CS )) - lg(p(y; θ CODE ))) 2 (3)

[0062] In a specific implementation, the code generation task loss in the current training operation refers to the loss of the code generation model converting the requirement information x described in natural language into the corresponding code y in the current training operation, and can be specifically expressed as: l CG = lg(p(y|x; θ CG )), where l represents the cross-entropy loss, and l CG is the loss of the code generation task.

[0063] The code verification task loss in the current training operation refers to the loss of the code verification model converting the code y into the requirement information x described in natural language in the current training operation, and can be specifically expressed as: l CS = lg(p(x|y; θ CS )), lCS The loss for the code verification task.

[0064] The model loss of the natural language model refers to the loss when training the language model using a monolingual corpus that only contains requirement information described in natural language, and can be specifically expressed as: l NL = lg(p(x; θ NL ))), where l NL is the model loss of the natural language model during the process of training the language model using the requirement information described in natural language. When the input requirement information described in natural language is the same, the model loss l NL of the natural language model is also the same. Therefore, in practical applications, to accelerate the training speed, the model loss l NL of the natural language model corresponding to the obtained requirement information described in natural language can be pre-computed and saved.

[0065] The model loss of the code language model refers to the loss when training the speech model using a monolingual corpus that only contains code, and can be specifically expressed as: l CODE = lg(p(y; θ CODE ))), l CODE is the model loss of the code language model during the process of training the language model using code. Since the same code corresponds to the same model loss l CODE of the code language model, in practical applications, to accelerate the training speed, the model loss l CODE of the code language model corresponding to the obtained code can be pre-computed and saved.

[0066] Therefore, formula (3) can be further expressed as:

[0067] l dual = (l CG + l NL - l CS - l CODE ) 2 (4)

[0068] In practical applications, formula (4) can be used to calculate the dual loss between the code generation model and the code verification model. Among them, the model loss l NL of the natural language model and the model loss l CODE of the code language model can be pre-computed, and the code generation task loss l CG and the code verification task loss l CS can be calculated using existing technologies. When the input of the model is different, the code generation task loss l CG and the code verification task loss l CS will change accordingly.

[0069] In a specific implementation, after calculating the dual loss l between the code generation model and the code verification model dual the first preset constant K1 dual can be multiplied by the dual loss l between the code generation model and the code verification model dual and then added to the code generation task loss l in the current training operation CG to obtain the model loss l' of the code generation model, that is CG :

[0070] l' CG = K1 dual ×l dual +l CG (5)

[0071] wherein, the first preset constant K1 dual can be obtained through experimental tests

[0072] Step 34, determine whether the model loss of the code generation model meets the first model loss condition

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

[0074] When the model loss of the code generation model is less than or equal to the first preset model loss threshold, it indicates that the model loss of the code generation model meets the first model loss condition, and then step 35 is executed; otherwise, step 36 is executed

[0075] Step 35, use the code generation model that meets the first model loss condition as the finally generated code generation model

[0076] Step 36, update the parameters of the code generation model based on the model loss of the code generation model

[0077] In a specific implementation, update the parameters θ of the code generation model based on the model loss l' of the code generation model CG The specific update method is not limited here. Update the parameters θ of the code generation model CG CG ​After that, steps 32 to 36 are executed again, that is, the code generation model with updated parameters is trained using the obtained training sample pairs until the model loss of the code generation model 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.

[0078] Using the dual loss between the code generation model and the code verification model to obtain the model loss of the code generation model, thereby updating the parameters of the code generation model, can improve the accuracy of the finally generated code generation model in the case of fewer training sample pairs.

[0079] 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, 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.

[0080] Refer to Figure 4 Moreover, an embodiment of the present invention also provides a training method for a code verification model. The code verification model is a model that converts requirement information described in natural language into corresponding code. The method may include the following steps:

[0081] Step 41, obtain training sample pairs.

[0082] Among them, the training sample pairs include: requirement information described in natural language, and the code corresponding to the requirement information described in natural language.

[0083] In specific implementation, the number of obtained 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 may include: requirement information x described in natural language, and the code y corresponding to the requirement information described in natural language.

[0084] Step 42, use the obtained training sample pairs to train the code verification model.

[0085] Refer to Figure 6 Moreover, the code verification model can be any seq2seq model. The seq2seq model is used to map one sequence to another sequence.

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

[0087] Specifically, similar to the code generation model, the code verification model may 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 is used to convert the code y into a vector e, and the encoding layer converts the vector e into a context-fused vector c'.

[0088] The decoder is used to calculate the probability value of converting the code y into the required information x in natural language description. Specifically, the decoder first sends the vector c' into the decoding layer to obtain the vectors output at each time step, and 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 required information x in natural language description.

[0089] Step 43, calculate the model loss of the code verification model.

[0090] Specifically, calculate the code verification task loss in the current training operation, as well as the dual loss between the code generation model and the code verification model, to obtain the model loss of the code verification model; the code generation model is a model that converts the required information in natural language description into the corresponding code, and has duality with the code verification model.

[0091] In a specific implementation, the input of the code generation model is exactly the output of the code verification model, and the input of the code verification model is exactly the input of the code generation model. Therefore, the code generation model and the code verification model have duality.

[0092] In a specific implementation, the dual loss between the code generation model and the code verification model can be calculated based on the probability correlation between the required information in natural language description and the corresponding code in the obtained training sample pair.

[0093] In an embodiment of the present invention, calculating the dual loss between the code generation model and the code verification model may include: obtaining the dual loss between the code generation model and the code verification model based on the model loss of the code generation task in the current training operation, the model loss of the code verification task in the current training operation, the model loss of the natural language model, and the model loss of the code language model, where the natural language model is a language model trained only using the information requirements described in natural language; the code language model is a language model trained only using codes.

[0094] In a specific implementation, formula (4) can be used to calculate the dual loss l between the code generation model and the code verification model dual . The code verification task loss in the current training operation, l CS can be expressed as: l CS= lg(p(x|y; θ CS ))。

[0095] In one embodiment, the second preset constant K2 dual and the dual loss l dual between the code generation model and the code verification model CS is multiplied, and then added to the code verification task loss l CS in the current training operation, as the model loss l'

[0096] of the code verification model, that is: CS l' dual = K2 dual × l CS (6)

[0097] wherein, the second preset constant K2 dual can be obtained through experimental tests. The second preset constant K2 dual can be equal to or not equal to the first preset constant K1 dual , and there is no limitation here.

[0098] Step 44, determine whether the model loss of the code verification model satisfies the second model loss condition.

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

[0100] When the model loss of the code verification model is less than or equal to the second preset model loss threshold, it indicates that the model loss of the code verification model satisfies the second model loss condition. At this time, step 45 can be continued, otherwise step 46 is executed.

[0101] Step 45, use the code verification model that satisfies the second model loss condition as the finally generated code verification model.

[0102] Step 46, update the parameters of the code verification model based on the model loss of the code verification model.

[0103] In a specific implementation, update the parameters θ CS of the code verification model based on the model loss l' CS, there is no limitation on how to update it specifically. The parameters θ of the updated code verification model are CS After that, steps 42 to 46 are executed again, that is, using the obtained training sample pairs, the code verification model with updated parameters is trained again until the model loss of the code verification model meets the second model loss condition, so that the accuracy of the finally generated code verification model can be improved in the case of fewer training sample pairs.

[0104] Using the dual loss between the code generation model and the code verification model to obtain the model loss of the code verification model, thereby updating the parameters of the code verification model, can improve the accuracy of the finally generated code verification model in the case of fewer training sample pairs.

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

[0106] Due to the certain similarity between 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.

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

[0108] It should be noted that when a single training sample pair is obtained, the code generation task loss l CG , the code verification task loss l CS , the model loss l of the natural language model NL and the model loss l of the code language model CODE , and the model loss l' of the code generation model CG all correspond to this single training sample pair. When multiple training sample pairs are obtained, the code generation task loss l CG should be a vector composed of the code generation task losses corresponding to each of the multiple training samples, and the code verification task loss l CS should be a vector composed of the code verification task losses corresponding to each of the multiple training samples, the model loss l of the natural language model NL should be a vector composed of the model losses of the natural language model corresponding to each of the multiple training samples, and the model loss l of the code language model CODE should be a vector composed of the model losses of the code language model corresponding to each of the multiple training samples, and the model loss l' of the code generation model CGIt should be a vector composed of the model losses of the code generation models corresponding to the multiple training samples respectively.

[0109] It should be noted that for the code generation model, specific reference can be made to Figure 3 the description of the code generation model training method in for implementation. 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.

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

[0111] Step 51, 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 requirement information described in natural language into the corresponding candidate codes.

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

[0113] For example, referring to Figure 6 , 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, namely candidate code y1, candidate code y2,..., candidate code yK. Among them, candidate code y1 is: int Price = 200. Candidate code y2 is: int price = 200. Candidate code yK is: int price.

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

[0115] Step 52, based on the first probability value, determine the code corresponding to the requirement information described in natural language.

[0116] In a specific implementation, based on the first probability value, multiple methods can be adopted to determine the code corresponding to the requirement information described in natural language. For example, the code corresponding to the highest value among all the first probability values can be selected as the final code for the requirement information described in natural language.

[0117] In an embodiment of the present invention, determining the code corresponding to the requirement information described in natural language based on the first probability value may include: inputting the candidate codes output by the code generation model into the trained code verification model to obtain the second probability values of converting each of the candidate codes into the requirement information described in natural language; 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.

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

[0119] Specifically, referring to Figure 6 , after inputting the candidate codes y1 to yK into the code verification model, the second probability values of converting the candidate codes y1 to yK into the requirement information x described in natural language can be obtained. For example, the second probability value of converting the candidate code y1 into the requirement information x described in 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 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 natural language can be expressed as p(x|yK).

[0120] Taking the product of the first probability value and the corresponding second probability value as the score of the candidate code, and thus selecting the candidate code corresponding to 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). Assuming 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.

[0121] In other embodiments, other methods can also be adopted to generate the code, which will not be exemplified one by one here.

[0122] By using the code generation method in the embodiment 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 accuracy of the solution for generating code by using the code generation model and the code verification model is naturally higher.

[0123] 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.

[0124] Refer to Figure 7 , the embodiment of the present invention further provides a training apparatus 70 for a code generation model. The apparatus 70 may include: a first acquisition unit 71, a first training unit 72, a first calculation unit 73, a first parameter update unit 74, and a first control unit 75. Among them:

[0125] The first acquisition unit 71 is adapted to acquire a plurality of training sample pairs, and the training sample pairs include: requirement information described in natural language, and the code corresponding to the requirement information described in natural language;

[0126] The first training unit 72 is adapted to use the acquired training sample pairs to train the code generation model;

[0127] The first calculation unit 73 is adapted to calculate the code generation task loss of the code generation model in the current training operation, and the dual loss between the code generation model and the code verification model, so as to obtain the model loss of the code generation model; the code verification model is a model that converts code into corresponding requirement information described in natural language, and there is a duality with the code generation model;

[0128] The first parameter update unit 74 is adapted to update the parameters of the code generation model based on the model loss of the code generation model;

[0129] The first control unit 75 is adapted to determine whether the model loss of the code generation model satisfies the first model loss condition. When the model loss of the code generation model does not satisfy the first model loss condition, control the first training unit 72, the first calculation unit 73, and the first parameter update unit 74 to repeat the corresponding operations until the model loss of the code generation model satisfies the first model loss condition, and use the code generation model that satisfies the first model loss condition as the finally generated code generation model.

[0130] Regarding the first acquisition unit 71, the first training unit 72, the first calculation unit 73, the first parameter update unit 74, and the first control unit 75, specific implementation can be referred to the description of the code generation model training method, which will not be elaborated here.

[0131] Refer to Figure 8 , an embodiment of the present invention further provides a training device 80 for a code verification model. The device 80 may include: a second acquisition unit 81, a second training unit 82, a second calculation unit 83, a second parameter update unit 84, and a second control unit 85. Among them:

[0132] The second acquisition unit 81 is adapted to acquire a number of training sample pairs. The training sample pairs include: requirement information described in natural language, and the code corresponding to the requirement information described in natural language;

[0133] The second training unit 82 is adapted to use the acquired training sample pairs to train the code verification model;

[0134] The second calculation unit 83 is adapted to calculate the code verification task loss in the current training operation, as well as the dual loss between the code generation model and the code verification model, to obtain the model loss of the code verification model; the code generation model is a model that converts requirement information described in natural language into corresponding code

[0135] The second parameter update unit 84 is adapted to update the parameters of the code verification model based on the model loss of the code verification model;

[0136] The second control unit 85 is adapted to determine whether the model loss of the code verification model satisfies the second model loss condition; and when the model loss of the code verification model does not satisfy the second model loss condition, control the second training unit 82, the second calculation unit 83, and the second parameter update unit 84 to repeat the corresponding operations until the model loss of the code verification model satisfies the second model loss condition, and use the code verification model that satisfies the second model loss condition as the finally generated code verification model.

[0137] Regarding the second acquisition unit 81, the second training unit 82, the second calculation unit 83, the second parameter update unit 84, and the second control unit 85, specific implementation can be referred to the description of the code verification model training method, which will not be elaborated here.

[0138] Refer to Figure 9 , an embodiment of the present invention further provides a code generation device 90. The device 90 may include: a code generation unit 91 and a code determination unit 92. Among them:

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

[0140] The code determination unit 92 is adapted to determine the code corresponding to the requirement information described in natural language based on the first probability value.

[0141] In an embodiment of the present invention, the code determination unit 92 may include: a code verification subunit 921 and a determination subunit 922. Wherein:

[0142] The code verification subunit 921 is adapted to obtain a trained code verification model, input the candidate codes output by the code generation model into the trained code verification model, and 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 80 of the above code verification model;

[0143] The determination subunit 922 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.

[0144] Regarding each functional unit of the device 90, reference may specifically be made to the above description of the code generation method, which will not be elaborated here.

[0145] 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 implement the steps of any one of the above code verification model training methods, or implement the steps of any one of the above code generation methods.

[0146] In a specific implementation, the computer-readable storage medium may include: ROM, RAM, a magnetic disk, an optical disk, etc.

[0147] 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 code generation model training methods, or executes the steps of any one of the above code verification model training methods, or executes the steps of any one of the above code generation methods..

[0148] Regarding each device and product described in the above embodiments, each module / unit included therein can be a software module / unit, a hardware module / unit, or can 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 the form of hardware such as circuits, or at least some of the modules / units can be implemented in the form of software programs that run on a processor integrated inside the chip, and the remaining (if any) part of the modules / units can be implemented in the form of hardware such as circuits; for each device and product applied to or integrated into a chip module, each module / unit included therein can be implemented in the form of hardware such as circuits, and 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 software programs that run on a processor integrated inside the chip module, and the remaining (if any) part of the modules / units can be implemented in the form of hardware such as circuits; for each device and product applied to or integrated into a terminal, each module / unit included therein can be implemented in the form of hardware such as circuits, and 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 software programs that run on a processor integrated inside the terminal, and the remaining (if any) part of the modules / units can be implemented in the form of hardware such as circuits.

[0149] 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 determined by the scope defined by the claims.

Claims

1. A training method for a code generation model, characterized in that, the code generation model is a model that converts requirement information described in natural language into corresponding code; the method includes: obtaining training sample pairs, where each training sample pair includes: requirement information described in natural language, and the code corresponding to the requirement information described in natural language; using the obtained training sample pairs to train the code generation model; calculating the code generation task loss in the current training operation and the dual loss between the code generation model and the code verification model to obtain the model loss of the code generation model; the code verification model is a model that converts code into corresponding requirement information described in natural language and has duality with the code generation model; when the model loss of the code generation model does not meet the first model loss condition, updating the parameters of the code generation model based on the model loss of the code generation model and retraining the code generation model with the updated parameters until the model loss of the code generation model meets the first model loss condition, and taking the code generation model that meets the first model loss condition as the finally generated code generation model.

2. The training method for the code generation model according to claim 1, characterized in that, calculating the dual loss between the code generation model and the code verification model includes: calculating the dual loss between the code generation model and the code verification model based on the probability correlation between the requirement information described in natural language and the corresponding code in the obtained training sample pairs.

3. The training method for the code generation model according to claim 2, characterized in that, calculating the dual loss between the code generation model and the code verification model includes: obtaining the dual loss between the code generation model and the code verification model based on the code generation task loss in the current training operation, the code verification task loss in the current training operation, the model loss of the natural language model, and the model loss of the code language model; wherein, the natural language model is a language model trained only using information requirements described in natural language; the code language model is a language model trained only using code.

4. The training method for the code generation model according to claim 3, characterized in that, using the following formula to calculate the dual loss between the code generation model and the code verification model: l dual =(l CG +l NL -l CS -l CODE ) 2 ; where, l CG is the loss of the code generation task in the current training operation, l CS is the loss of the code verification task in the current training operation, l NL is the model loss of the natural language model, l CODE is the model loss of the code language model.

5. The training method for the code generation model according to claim 1, characterized in that, calculating the code generation task loss in the current training operation and the dual loss between the code generation model and the code verification model to obtain the model loss of the code generation model includes: taking the sum of the product of a first preset constant and the dual loss between the code generation model and the code verification model and the code generation task loss in the current training operation as the model loss of the code generation model.

6. The training method for 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.

7. A training method for a code verification model, characterized in that the code verification model is a model that converts requirement information described in natural language into corresponding code; the method includes: obtaining training sample pairs, where the training sample pairs include: requirement information described in natural language, and the code corresponding to the requirement information described in natural language; using the obtained training sample pairs to train the code verification model; calculating the code verification task loss in the current training operation, and the duality loss between the code generation model and the code verification model, to obtain the model loss of the code verification model; the code generation model is a model that converts requirement information described in natural language into corresponding code, and has duality with the code verification model; when the model loss of the code verification model does not meet the second model loss condition, updating the parameters of the code verification model based on the model loss of the code verification model, and retraining the code verification model with the updated parameters until the model loss of the code verification model meets the second model loss condition, and taking the code verification model that meets the second model loss condition as the finally generated code verification model.

8. The training method for a code verification model according to claim 7, characterized in that calculating the duality loss between the code generation model and the code verification model includes: calculating the duality loss between the code generation model and the code verification model based on the probability correlation between the requirement information described in natural language and the corresponding code in the obtained training sample pairs.

9. The training method for a code verification model according to claim 8, characterized in that calculating the duality loss between the code generation model and the code verification model includes: obtaining the duality loss between the code generation model and the code verification model based on the model loss of the code generation task in the current training operation, the model loss of the code verification task in the current training operation, the model loss of the natural language model, and the model loss of the code language model; wherein, the model of the natural language model is a language model trained only using the information requirements described in natural language; the code language model is a language model trained only using code.

10. The training method for a code verification model according to claim 9, characterized in that using the following formula to calculate the duality loss between the code generation model and the code verification model: l dual =(l CG +l NL -l CS -l CODE ) 2 ; where, l CG is the loss of the code generation task model in the current training operation, l CS is the loss of the code verification task model in the current training operation, l NL is the loss of the natural language model, l CODE is the loss of the code language model.

11. The training method for a code verification model according to claim 7, characterized in that calculating the model loss of the code verification model by calculating the code generation task loss in the current training operation and the duality loss between the code generation model and the code verification model includes: taking the sum of the product of a second preset constant and the duality loss between the code generation model and the code verification model and the code verification task loss in the current training operation as the model loss of the code verification model.

12. The training method of the code verification model according to claim 7, 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.

13. A code generation method, characterized in that, comprising: inputting 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 requirement information described in natural language into the corresponding candidate codes; the code generation model is trained by using the training method of the code generation model according to any one of claims 1 to 6; determining the code corresponding to the requirement information described in natural language based on the first probability value.

14. The code generation method according to claim 13, characterized in that, the determining the code corresponding to the requirement information described in natural language based on the first probability value includes: inputting 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 method of the code verification model according to any one of claims 7 to 12; 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.

15. A training device for a code generation model, characterized in that, the code generation model is a model for converting requirement information described in natural language into corresponding codes; the device includes: a first obtaining unit, adapted to obtain a plurality of training sample pairs, the training sample pairs including: requirement information described in natural language, and the codes corresponding to the requirement information described in natural language; a first training unit, adapted to use the obtained training sample pairs to train the code generation model; a first calculation unit, adapted to calculate the code generation task loss in the current training operation and the dual loss between the code generation model and the code verification model to obtain the model loss of the code generation model; the code verification model is a model for converting codes into corresponding requirement information described in natural language, and there is a duality with the code generation model; a first parameter updating unit, adapted to update the parameters of the code generation model based on the model loss of the code generation model; and a first control unit, adapted to judge whether the model loss of the code generation model meets the first model loss condition, and when the model loss of the code generation model does not meet the first model loss condition, control the first training unit, the first calculation unit and the first parameter updating unit to repeatedly execute the corresponding operations until the model loss of the code generation model meets the first model loss condition, and take the code generation model that meets the first model loss condition as the finally generated code generation model.

16. A training device for a code verification model, characterized in that, The code verification model is a model that converts requirement information described in natural language into corresponding code; the apparatus includes: A second acquisition unit, adapted to acquire a plurality of training sample pairs, where each training sample pair includes: requirement information described in natural language, and the code corresponding to the requirement information described in natural language; A second training unit, adapted to use the acquired training sample pairs to train the code verification model; A second calculation unit, adapted to calculate the loss of the code verification task in the current training operation, as well as the dual loss between the code generation model and the code verification model, to obtain the model loss of the code verification model; the code generation model is a model that converts requirement information described in natural language into corresponding code; A second parameter update unit, adapted to update the parameters of the code verification model based on the model loss of the code verification model; And a second control unit, adapted to determine whether the model loss of the code verification model meets the second model loss condition; and when the model loss of the code verification model does not meet the second model loss condition, control the second training unit, the second calculation unit, and the second parameter update unit to repeatedly execute corresponding operations until the corrected model loss of the code verification model meets the second model loss condition, and use the code verification model that meets the second model loss condition as the finally generated code verification model.

17. A code generation apparatus, characterized in that, it includes: A code generation unit, adapted to acquire a trained code generation model, and input requirement information described in natural language into the acquired code generation model to obtain two or more candidate codes, and a first probability value for converting the requirement information described in natural language into the corresponding candidate codes; the code generation model is trained by using the training apparatus of the code generation model described in claim 15; And a code determination unit, adapted to determine the code corresponding to the requirement information described in natural language based on the first probability value.

18. The code generation apparatus according to claim 17, characterized in that, the code determination unit includes: A code verification subunit, adapted to acquire a trained code verification model, and 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 apparatus of the code verification model described in claim 16; A determination subunit, 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.

19. A computer-readable storage medium, on which a computer program is stored, 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 14.

20. An electronic device, including a memory and a processor, and 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 performs the steps of the method according to any one of claims 1 to 14.