A method and device for automatically generating code based on specified functions

By establishing keyword sequences and using dual-channel GPT2 to improve the model and Logistic classifier, the problem of insufficient controllability and logic of code generation in the existing technology is solved, and high-quality and highly controllable automatic generation of production code is achieved, reducing development costs and improving efficiency.

CN114444442BActive Publication Date: 2025-03-07BEIJING MECHANICAL EQUIP INST
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202011196907.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-10-30
Publication Date
2025-03-07
Estimated Expiration
2040-10-30

AI Technical Summary

Technical Problem

The existing text generation technology based on GPT2 model is difficult to meet the needs of specified users in terms of text controllability and logic, and cannot be effectively applied to automatic production code generation in specific fields.

Method used

By obtaining the specified functions of user input, a keyword sequence is established, and the code is automatically generated using One-Hot encoding and dual-channel GPT2 improved model, and logical probability calculation is performed in combination with the Logistic classifier to ensure that the generated code has strong logical correlation and good controllability.

Benefits of technology

It realizes automatic generation of high-quality production code, meets specific functional needs, reduces product development costs, improves efficiency, and prevents quality risks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114444442B_ABST
    Figure CN114444442B_ABST
Patent Text Reader

Abstract

The present invention relates to a method for automatically generating codes based on a specified function, belongs to the field of artificial intelligence technology, and solves the problem that the controllability and logic of the existing text cannot really meet the needs of the specified user. The method includes: obtaining a keyword sequence corresponding to the specified function; encoding the keyword sequence and the code sequence input by the user respectively; splicing each element in the keyword sequence encoding result with the code template respectively, inputting each splicing result and the historical code obtained by the last code automatic generation into the dual-path GPT2 improved model, and obtaining the output probability of each group of data; identifying the keywords corresponding to the first K groups of data with the largest output probability as candidate keywords; inputting each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user into the Logistic classifier, and obtaining the logical probability of each group of data; selecting the candidate keyword output corresponding to the maximum logical probability, and completing the automatic code generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a method and device for automatically generating codes based on specified functions. Background Art

[0002] In actual production environments, automated code generation is of great significance in improving the work efficiency of ordinary technicians, reducing the risk of program errors, and saving development costs. At present, with the continuous development of artificial intelligence technology, existing technologies have made breakthrough progress in automated text generation. For example, the GPT2 model proposed by openAI has demonstrated powerful text generation capabilities in automated news writing.

[0003] However, the existing text generation technology based on the GPT2 model is completely randomly generated based on the training text. It cannot truly meet the needs of specified users in terms of text controllability and logic, and is difficult to apply to production applications in specific fields. It is in urgent need of improvement.

[0004] Currently, there is a lack of a code automatic generation method that can automatically generate high-quality production codes with good text controllability and strong logic according to the functional requirements of specific products. Summary of the invention

[0005] In view of the above analysis, the embodiments of the present invention aim to provide a method and device for automatically generating codes based on specified functions, so as to solve the problem that the controllability and logic of the text in the prior art cannot truly meet the needs of specified users.

[0006] On the one hand, an embodiment of the present invention provides a method for automatically generating code based on a specified function, comprising the following steps:

[0007] Obtain all typical processes of the specified function input by the user, set the main flow corresponding to each typical process as a keyword, and establish a keyword sequence corresponding to the specified function;

[0008] One-hot encoding is performed on the keyword sequence corresponding to the specified function and the code sequence input by the user to obtain respective encoding results;

[0009] Each element in the keyword sequence encoding result is spliced ​​with the code template selected by the user, and each splicing result and the historical code of the specified function obtained by the last code automatic generation are used as a group of data, which are input into the pre-trained two-way GPT2 improved model to obtain the output probability of each group of data; the keywords corresponding to the elements of the first K groups of data with the largest output probability are identified as candidate keywords;

[0010] Take each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a group of data, input it into the pre-trained Logistic classifier, and obtain the logical probability of each group of data;

[0011] Select the alternative keyword corresponding to the maximum logical probability as the output to complete the automatic code generation.

[0012] The beneficial effects of the above technical solution are as follows: the existing automatic code generation method has poor logical correlation between the generated code and the specified function due to the lack of control branches. The above technical solution, through model input control, secondary model classification, word embedding and encoding, overcomes the problem that the controllability and logic of code generation in the existing technology are difficult to meet the actual production application needs, and can automatically generate high-quality production code with good controllability, which is of great significance in reducing product development costs, improving efficiency, and preventing quality risks.

[0013] Based on the further improvement of the above method, the dual-path GPT2 improved model is: the splicing result and the historical code of the specified function are taken as two inputs, input into their respective GPT2 models, extract the 9th to 11th layer outputs of their respective GPT2 models, first perform dot multiplication and then sum the two outputs, normalize the sum result, and input the normalized result into the 12th layer of the GPT2 model to obtain the output probability.

[0014] The beneficial effect of the above further improvement scheme is that the historical code of the specified function is taken into consideration during the automatic code generation process, the output result of the GPT2 model code branch (12th layer) is controlled, and the control information of the control branch (the outputs of the 9th to 11th layers are first multiplied and then summed, and the sum result is normalized) is superimposed on the 12th layer of the GPT2 model as input, ensuring that the subsequent code generation meets the control requirements.

[0015] Further, all typical processes of the designated function input by the user are obtained, the main flow corresponding to each typical process is set as a keyword, and a keyword sequence corresponding to the designated function is established, further comprising the following steps:

[0016] According to user needs, the target system is functionally divided to obtain all sub-functions;

[0017] For each sub-function, all corresponding typical processes are obtained, where the typical processes include at least one of description, loop, comparison, sorting, and calculation;

[0018] Extract the main flow corresponding to each typical process as a keyword, and establish a set of all keywords of the sub-function as the keyword sequence of the sub-function;

[0019] Obtain keyword sequences of all sub-functions, and establish a database including each sub-function and the corresponding keyword sequence;

[0020] Get the specified function of user input;

[0021] The designated function is matched with the sub-functions in the above database, and a keyword sequence corresponding to the matching sub-function is extracted as the keyword sequence corresponding to the designated function.

[0022] The beneficial effect of the above further improvement scheme is: according to the characteristics of the target system, the keywords corresponding to the specified functions are obtained, and then the keywords corresponding to the specified functions are digitally represented to provide input for subsequent keyword screening, laying the foundation for accurate automatic code generation.

[0023] Further, the keyword sequence corresponding to the specified function and the code sequence input by the user are respectively One-Hot encoded to obtain respective encoding results, further comprising the following steps:

[0024] Extract all keywords from the database, perform deduplication processing on the extracted results, and establish a keyword table;

[0025] According to the established keyword table, the keyword sequence corresponding to the specified function is One-Hot encoded to obtain the keyword sequence encoding result corresponding to the specified function, and the embedding value of the encoding result is obtained; wherein, if the keyword in the keyword table exists in the keyword sequence corresponding to the specified function, it is set to 1, otherwise, it is set to 0, and each element of the keyword table is traversed in turn;

[0026] Get the code sequence entered by the user;

[0027] According to the keyword table, One-Hot encoding is performed on the code sequence input by the user to obtain the encoding result of the code sequence input by the user.

[0028] The beneficial effect of the above further improvement scheme is that the automatic code generation process incorporates the consideration of the code sequence input by the user. The code sequence input by the user and the keyword sequence are digitally encoded respectively, thereby ensuring the subsequent keyword screening is accurate.

[0029] Further, the two-way GPT2 improved model and Logistic classifier are trained through the following steps:

[0030] Acquire training data including all sub-functions of the target system; each set of training data includes a keyword sequence of a sub-function, a code sequence input by a user, a standard code template, and a historical code corresponding to the sub-function, and the output data is the expected keyword corresponding to the standard code template;

[0031] When using each set of training data for training, the keyword sequence corresponding to the sub-function and the code sequence input by the user are respectively One-Hot encoded to obtain respective encoding results;

[0032] Each element of the keyword sequence encoding result is spliced ​​with the standard code template respectively, and each splicing result and the historical code of the specified function obtained by the last code automatic generation are used as a group of data, which are input into the two-way GPT2 improved model to obtain the output probability of each group of data; the keywords corresponding to the elements of the first K groups of data with the largest output probability are identified as candidate keywords;

[0033] Take each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a group of data, input it into the pre-trained Logistic classifier, and obtain the logical probability of each group of data;

[0034] Identify the candidate keyword corresponding to the maximum logical probability, compare it with the expected keyword, and determine whether the two are consistent. If they are consistent, the training result y=1, otherwise, the training result y=0;

[0035] According to the above training results y, the loss function J is obtained by the following formula (i) (θ,ε)

[0036]

[0037] In the formula, θ is the improved model parameter of dual-path GPT2, ε is the Logistic classifier parameter, u is the constant coefficient, i is the current iteration cycle, m is the maximum number of iterations, and y is the maximum number of iterations. (i) is the training result, h (i) is the maximum logical probability, J (0) (θ,ε)=0;

[0038] Compare the obtained loss function with the loss function of the previous iteration cycle;

[0039] if The GPT2 improved model parameters θ and the Logistic classifier parameters ε are updated through the update strategy in the following formula to obtain the updated results θ', ε'

[0040] θ'=θ-α θ

[0041] ε'=ε-α ε

[0042] In the formula, α θ , α ε is the preset update step size;

[0043] if The GPT2 improved model parameter θ and the Logistic classifier parameter ε are updated through the update strategy 2 in the following formula to obtain the updated results θ', ε'

[0044]

[0045] ε'=ε-α ε

[0046] in

[0047] v (t) =β2v (t-1) +β1

[0048] m (t) =β4m (t-1) +β3

[0049] In the formula, β1, β2, β3, β4, η, τ are all constants, m (0) =v (0) =0;

[0050] Use the two-way GPT2 improved model and Logistic classifier with updated parameters to obtain the candidate keywords corresponding to the maximum logical probability again, until the candidate keywords corresponding to the maximum logical probability are consistent with the expected keywords, completing the training of the two-way GPT2 improved model and Logistic classifier using this set of training data;

[0051] The next set of training data is used to train the two-way GPT2 improved model and the Logistic classifier, and the training of all training data is completed in sequence to obtain the trained two-way GPT2 improved model and Logistic classifier.

[0052] The beneficial effect of the above further improvement scheme is: the dual-channel GPT2 improved model and Logistic classifier trained by the above scheme can automatically generate code in the actual production process, can manually control the various functions to be implemented by the code, and can automatically integrate the code specifications and code standards accumulated over a long period of time into the current code generation, ensuring the high quality of the code, greatly improving the coding efficiency of front-line personnel, and realizing the accumulation and inheritance of high-quality and high-quality coding.

[0053] On the other hand, an embodiment of the present invention provides a device for automatically generating code based on a specified function, characterized in that it includes:

[0054] The information collection module is used to receive the specified function input by the user, provide the user with a code template to select, and enter the code sequence input by the user and send it to the data processing module;

[0055] The data processing module is used to obtain all typical processes of the specified function, set the main flow corresponding to each typical process as a keyword, and establish the keyword sequence corresponding to the specified function; perform One-Hot encoding on the keyword sequence corresponding to the specified function and the code sequence input by the user to obtain their respective encoding results; splice each element in the keyword sequence encoding result with the code template selected by the user, and input each splicing result and the historical code of the specified function obtained by the last code automatic generation as a group of data into the pre-trained two-way GPT2 improved model to obtain the output probability of each group of data; identify the keywords corresponding to the elements of the first K groups of data with the largest output probability as candidate keywords; input each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a group of data into the pre-trained Logistic classifier to obtain the logical probability of each group of data, and send the logical probability of each group of data and the corresponding keywords to the code generation module;

[0056] The code generation module is used to select the alternative keyword corresponding to the maximum logical probability as the first code after the code sequence input by the user, and feed the output new code as the updated code sequence input by the user to the data processing module, output all the codes in sequence, and complete the automatic code generation.

[0057] The beneficial effects of the above technical solution are as follows: the existing automatic code generation device has poor logical correlation between the generated code and the specified function due to the lack of control branches. The above technical solution, through model input control, secondary model classification, word embedding and encoding, overcomes the problem that the controllability and logic of code generation in the existing technology are difficult to meet the actual production application needs, and can automatically generate high-quality production code with good controllability, which is of great significance in reducing product development costs, improving efficiency, and preventing quality risks.

[0058] Based on the further improvement of the above-mentioned device, the dual-path GPT2 improved model is: the splicing result and the historical code of the specified function are taken as two inputs, input into their respective GPT2 models, extract the 9th to 11th layer outputs of their respective GPT2 models, first perform dot multiplication and then sum the two outputs, normalize the sum result, and input the normalized result into the 12th layer of the GPT2 model to obtain the output probability.

[0059] The beneficial effect of adopting the above further improvement scheme is that the existing automatic code generation method has poor logical correlation between the generated code and the previous and next codes due to the lack of control branches, and cannot realize the automatic generation of codes for specified functions. The above technical scheme, through model input control, secondary model classification, word embedding and encoding, overcomes the problem that the controllability and logic of code generation in the existing technology are difficult to meet the actual production application needs, and can automatically generate high-quality production codes with good controllability, which is of great significance in reducing product development costs, improving efficiency, and preventing quality risks.

[0060] Furthermore, the data processing module further comprises:

[0061] A keyword generation module is used to obtain all typical processes of the specified function, set the main flow corresponding to each typical process as a keyword, establish a keyword sequence corresponding to the specified function, and send it to the encoding module;

[0062] The encoding module is used to perform one-hot encoding on the received keyword sequence corresponding to the designated function and the code sequence input by the user, obtain respective encoding results, send the keyword sequence encoding result to the candidate data generation module, and send the encoding result of the code sequence input by the user to the logic judgment module;

[0063] The candidate data generation module is used to splice each element in the received keyword sequence encoding result with the code template selected by the user, and input each splicing result and the historical code of the specified function obtained by the previous code automatic generation as a group of data into the pre-trained two-way GPT2 improved model to obtain the output probability of each group of data, identify the keywords corresponding to the elements of the first K groups of data with the largest output probability, and send them as candidate keywords to the logic judgment module;

[0064] The logic judgment module is used to obtain the Embedding value of the keyword sequence encoding result, and input each received candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence entered by the user as a group of data into the pre-trained Logistic classifier to obtain the logical probability of each group of data, and send the logical probability of each group of data and the corresponding keyword to the code generation module.

[0065] The beneficial effect of adopting the above further improvement scheme is that the automatic code generation process incorporates three considerations: the code sequence input by the user, the historical code of the specified function obtained by the previous automatic code generation, and the code template currently selected by the user.

[0066] Furthermore, the keyword generation module executes the following procedure to establish a keyword sequence corresponding to the specified function:

[0067] According to user needs, the target system is functionally divided to obtain all sub-functions;

[0068] For each sub-function, all corresponding typical processes are obtained, where the typical processes include at least one of description, loop, comparison, sorting, and calculation;

[0069] Extract the main flow corresponding to each typical process as a keyword, and establish a set of all keywords of the sub-function as the keyword sequence of the sub-function;

[0070] Obtain keyword sequences of all sub-functions, and establish a database including each sub-function and the corresponding keyword sequence;

[0071] Get the specified function of user input;

[0072] The designated function is matched with the sub-functions in the above database, and a keyword sequence corresponding to the matching sub-function is extracted as the keyword sequence corresponding to the designated function.

[0073] The beneficial effect of adopting the above-mentioned further improvement scheme is: according to the characteristics of the target system, the keywords corresponding to the specified functions are obtained, and then the keywords corresponding to the specified functions are digitally represented to provide input for subsequent keyword screening, laying the foundation for accurate automatic code generation.

[0074] Further, the encoding module executes the following procedure to perform one-hot encoding on the keyword sequence corresponding to the specified function and the code sequence input by the user respectively:

[0075] Extract all keywords from the database, perform deduplication processing on the extracted results, and establish a keyword table;

[0076] According to the established keyword table, the keyword sequence corresponding to the specified function is One-Hot encoded to obtain the keyword sequence encoding result corresponding to the specified function, and the embedding value of the encoding result is obtained; wherein, if the keyword in the keyword table exists in the keyword sequence corresponding to the specified function, it is set to 1, otherwise, it is set to 0, and each element of the keyword table is traversed in turn;

[0077] Get the code sequence entered by the user;

[0078] According to the keyword table, One-Hot encoding is performed on the code sequence input by the user to obtain the encoding result of the code sequence input by the user.

[0079] The beneficial effect of adopting the above-mentioned further improved solution is that the code sequence and keyword sequence input by the user are digitally encoded respectively, thereby ensuring the subsequent keyword screening to be carried out accurately.

[0080] In the present invention, the above-mentioned technical solutions can also be combined with each other to achieve more preferred combination solutions. Other features and advantages of the present invention will be described in the subsequent description, and some advantages can become obvious from the description, or can be understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained through the contents particularly pointed out in the description and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0081] The drawings are only for the purpose of illustrating specific embodiments and are not to be considered limiting of the present invention. Like reference symbols denote like components throughout the drawings.

[0082] Figure 1 This is a schematic diagram of the steps of a method for automatically generating codes based on a specified function in Embodiment 1 of the present invention;

[0083] Figure 2 This is a schematic diagram of the structure of a device for automatically generating codes based on a specified function according to Embodiment 3 of the present invention;

[0084] Figure 3 This is a schematic diagram of the structure of a device for automatically generating codes based on specified functions according to Embodiment 4 of the present invention. DETAILED DESCRIPTION

[0085] The preferred embodiments of the present invention are described in detail below in conjunction with the accompanying drawings, wherein the accompanying drawings constitute a part of this application and are used together with the embodiments of the present invention to illustrate the principles of the present invention, but are not used to limit the scope of the present invention.

[0086] Example 1

[0087] A specific embodiment of the present invention discloses a method for automatically generating code based on a specified function, such as Figure 1 As shown, the following steps are included:

[0088] S1. Obtain all typical processes of the specified function input by the user, set the main process corresponding to each typical process as a keyword, and establish a keyword sequence corresponding to the specified function;

[0089] S2. One-Hot encode the keyword sequence corresponding to the specified function and the code sequence entered by the user to obtain their respective encoding results;

[0090] S3. Each element in the keyword sequence encoding result is spliced ​​with the code template selected by the user, and each splicing result and the historical code of the specified function obtained by the last code automatic generation are input into the pre-trained two-way GPT2 improved model to obtain the output probability of each group of data; the keywords corresponding to the elements of the first K groups of data with the largest output probability are identified as candidate keywords; K can be set according to needs, such as 100;

[0091] S4. Take each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a set of data, input it into the pre-trained Logistic classifier, and obtain the logical probability of each set of data;

[0092] S5. Select the candidate keyword corresponding to the maximum logical probability as the first code after the code sequence input by the user, output all the codes in sequence, and complete the automatic code generation.

[0093] Compared with the prior art, the method provided in this embodiment can meet the requirements of automatic code generation for system-specified functions in the actual production process. Through model input control, secondary model classification, word embedding and encoding, the problem that the controllability and logic of the existing automatic code generation methods are difficult to meet the actual production application requirements is overcome. The ability to automatically generate high-quality production code with good controllability is of great significance in reducing product development costs, improving efficiency, and preventing quality risks.

[0094] Example 2

[0095] Based on the optimization of Example 1, the two-way GPT2 improved model is: the splicing result and the historical code of the specified function are used as two-way inputs, input into their respective GPT2 models, extract the 9th to 11th layer outputs of their respective GPT2 models, the output corresponding to the splicing result is (a1, a2, a3), the output corresponding to the historical code is (b1, b2, b3), the two-way output is first dot-multiplied and then summed to obtain (a1b1+a2b2+a3b3), the sum result is normalized by the softmax function, the normalized result is input into the 12th layer of the GPT2 model as input, and the 12th layer output of the GPT2 model is used as the obtained output probability. The above GPT2 model is a prior art model.

[0096] Preferably, step S1 is further refined as follows:

[0097] S11. According to user needs, divide the target system into functions and obtain all sub-functions;

[0098] S12. For each sub-function, obtain all corresponding typical processes, wherein the typical processes include at least one of description, loop, comparison, sorting, and calculation;

[0099] S13. Extract the main flow corresponding to each typical process as a keyword, and establish a set of all keywords of the sub-function as the keyword sequence of the sub-function; assuming f i Represents the i-th sub-function, extracting keywords for each sub-function ij , j=1,···,M i , M i is the number of keywords for the ith sub-function, then the following keyword matrix is ​​formed:

[0100]

[0101] S14. Obtain keyword sequences for all sub-functions and establish a database including each sub-function and the corresponding keyword sequence;

[0102] S15. Get the specified function input by the user;

[0103] S16. Match the designated function with the sub-functions in the database, and extract the keyword sequence corresponding to the matching sub-function as the keyword sequence corresponding to the designated function.

[0104] Preferably, step S2 is further refined as follows:

[0105] S21. Extract all keywords from the database, perform deduplication and merging processing on the extracted results, and establish a keyword table; assuming that after deduplication of all keywords, N keywords remain, the remaining keywords are rearranged in dictionary order to form a keyword table:

[0106] Vocabulary:[keyword1 keyword2 … keyword N ] (2)

[0107] S22. According to the keyword table, one-hot encode the keyword sequence corresponding to the specified function to obtain the keyword sequence encoding result, and obtain the embedding value of the encoding result; wherein, if the keyword in the keyword table exists in the keyword sequence corresponding to the specified function, it is set to 1, otherwise, it is set to 0; each sub-function is represented in the form of a one-hot encoding sequence;

[0108] S23. Get the code sequence entered by the user;

[0109] S24. According to the keyword table, One-Hot encoding is performed on the code sequence input by the user to obtain the encoding result of the code sequence input by the user.

[0110] Step S22 further obtains the embedding value of the keyword sequence encoding result through the following steps:

[0111] S221. Set the embedding dimension D=100 and calculate the sliding window window=5;

[0112] S222. Learn word embedding using the CBOW algorithm;

[0113] S223. The learning algorithm structure is a single hidden layer neural network structure, and the optimization goal is to minimize the cross entropy;

[0114] S224.After the word2vec algorithm is executed, the embedding value of each keyword can be output;

[0115] S225. For each keyword sequence, the average of the embedding values ​​of each keyword is calculated as the embedding result of the sequence.

[0116] Preferably, the two-way GPT2 improved model and the Logistic classifier are trained by the following steps:

[0117] S01. Acquire training data including all sub-functions of the target system; each set of training data includes a keyword sequence of a sub-function, a code sequence input by the user, a standard code template, and a historical code corresponding to the sub-function, and the output data is the expected keyword (or a code consisting of a series of keywords) corresponding to the standard code template;

[0118] S02. When each set of training data is used for training, the keyword sequence corresponding to the sub-function and the code sequence entered by the user are respectively One-Hot encoded to obtain their respective encoding results;

[0119] S03. Each element of the keyword sequence encoding result is spliced ​​with the standard code template respectively, and each splicing result and the historical code of the specified function obtained by the last code automatic generation are input into the two-way GPT2 improved model as a group of data to obtain the output probability of each group of data; the keywords corresponding to the elements of the first K groups of data with the largest output probability are identified as candidate keywords;

[0120] S04. Take each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a set of data, input it into the pre-trained Logistic classifier, and obtain the logical probability of each set of data;

[0121] S05. Identify the candidate keyword corresponding to the maximum logical probability, compare it with the expected keyword, and determine whether the two are consistent. If they are consistent, the training result y=1, otherwise, the training result y=0;

[0122] S06. According to the above training result y, the loss function J of this iteration cycle is obtained by the following formula: (i) (θ,ε)

[0123]

[0124] In the formula, θ is the improved model parameter of dual-path GPT2, ε is the Logistic classifier parameter, u is the constant coefficient, i is the current iteration cycle, m is the maximum number of iterations, which can be set to 1000, and y (i) is the training result, h (i) is the maximum logical probability, J (0) (θ,ε)=0;

[0125] S07. Compare the above loss function obtained with the loss function of the previous iteration cycle; if Execute step S08, otherwise Execute step S09;

[0126] S08. Update the GPT2 improved model parameter θ and the Logistic classifier parameter ε through the update strategy in the following formula to obtain the updated results θ', ε'

[0127]

[0128] In the formula, α θ , α ε is the preset update step size;

[0129] S09. Update the GPT2 improved model parameter θ and the Logistic classifier parameter ε through the update strategy 2 in the following formula to obtain the updated results θ', ε'

[0130]

[0131] in

[0132] v (t) =β2v (t-1) +β1

[0133] m (t)=β4m (t-1) +β3

[0134] In the formula, β1, β2, β3, β4, η, τ are all constants, which are set by programmers based on experience, and m (0) =v (0) =0.

[0135] S010.i automatically increases by 1, enters a new iteration cycle, repeats steps S02-S09, and uses the two-way GPT2 improved model and Logistic classifier with updated parameters to obtain the candidate keywords corresponding to the maximum logical probability again, until the candidate keywords corresponding to the maximum logical probability are consistent with the expected keywords, completing the training of the two-way GPT2 improved model and Logistic classifier with this set of training data;

[0136] S011. Train the two-way GPT2 improved model and the Logistic classifier with the next set of training data, complete the training of all training data in turn, and obtain the trained two-way GPT2 improved model and Logistic classifier.

[0137] Compared with Example 1, the method provided in this embodiment provides a specific two-way GPT2 improved model, as well as a training method for the two-way GPT2 improved model and the Logistic classifier. Through this method, automatic generation of codes that meet various specified functions can be obtained, overcoming the problem that the controllability and logic of automated code generation are difficult to meet the actual production application requirements. The present invention can automatically generate high-quality production codes with good controllability, which is of great significance for reducing product development costs, improving efficiency, and preventing quality risks.

[0138] Example 3

[0139] The present invention also discloses a device for automatically generating codes based on a specified function corresponding to the method of embodiment 1, comprising an information acquisition module, a data processing module, and a code generation module connected in sequence, such as Figure 2 shown.

[0140] The information collection module is used to receive the specified function input by the user, provide the user with a code template to select, and enter the code sequence input by the user and send it to the data processing module.

[0141] The data processing module is used to obtain all typical processes of the specified function, set the main flow corresponding to each typical process as a keyword, and establish a keyword sequence corresponding to the specified function; perform One-Hot encoding on the keyword sequence corresponding to the specified function and the code sequence input by the user to obtain their respective encoding results; splice each element in the keyword sequence encoding result with the code template selected by the user, and input each splicing result and the historical code of the specified function obtained by the previous code automatic generation as a group of data into a pre-trained two-way GPT2 improved model to obtain the output probability of each group of data; identify the keywords corresponding to the elements of the first K groups of data with the largest output probability as candidate keywords; input each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a group of data into a pre-trained Logistic classifier to obtain the logical probability of each group of data, and send the logical probability of each group of data and the corresponding keyword to the code generation module.

[0142] The code generation module is used to select the alternative keyword corresponding to the maximum logical probability as the first code after the code sequence input by the user, and feed the output new code as the updated code sequence input by the user to the data processing module, output all the codes in sequence, and complete the automatic code generation.

[0143] Compared with the prior art, the device provided in this embodiment can meet the requirements of automatic code generation for system-specified functions in the actual production process. Through model input control, secondary model classification, word embedding and encoding, etc., the problem that the controllability and logic of the existing automatic code generation methods are difficult to meet the actual production application requirements is overcome. The ability to automatically generate high-quality production code with good controllability is of great significance in reducing product development costs, improving efficiency, and preventing quality risks.

[0144] Example 4

[0145] Based on the improvement of Example 3, a code automatic generation device corresponding to the method described in Example 2 is also disclosed. In the device, the dual-path GPT2 improved model is: the splicing result and the historical code of the specified function are used as two-path inputs, input into the respective GPT2 models, extract the 9th to 11th layer outputs of the respective GPT2 models, first dot-multiply and then sum the two-path outputs, normalize the sum result, input the normalized result into the 12th layer of the GPT2 model, and obtain the output probability.

[0146] Preferably, the data processing module further comprises a keyword generation module, an encoding module, an alternative data generation module, and a logic judgment module which are connected in sequence. Moreover, the output end of the code generation module is connected to the feedback end of the encoding module, such as Figure 3 shown.

[0147] The keyword generation module is used to obtain all typical processes of the specified function, set the main flow corresponding to each typical process as a keyword, establish the keyword sequence corresponding to the specified function, and send it to the encoding module.

[0148] The encoding module is used to perform One-Hot encoding on the received keyword sequence corresponding to the specified function and the code sequence entered by the user, respectively, to obtain their respective encoding results, and to send the keyword sequence encoding result to the alternative data generation module, and to send the encoding result of the code sequence entered by the user to the logic judgment module.

[0149] The alternative data generation module is used to splice each element in the received keyword sequence encoding result with the code template selected by the user, and take each splicing result and the historical code of the specified function obtained by the previous code automatic generation as a group of data, input them into the pre-trained two-way GPT2 improved model, obtain the output probability of each group of data, identify the keywords corresponding to the elements of the first K groups of data with the largest output probability, and send them as alternative keywords to the logic judgment module.

[0150] The logic judgment module is used to obtain the Embedding value of the keyword sequence encoding result, and input each received candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence entered by the user as a group of data into the pre-trained Logistic classifier to obtain the logical probability of each group of data, and send the logical probability of each group of data and the corresponding keyword to the code generation module.

[0151] Preferably, the keyword generation module executes the following procedure to establish a keyword sequence corresponding to the specified function:

[0152] SS1. According to user needs, divide the target system into functional parts and obtain all sub-functions;

[0153] SS2. For each sub-function, obtain all corresponding typical processes, wherein the typical processes include at least one of description, loop, comparison, sorting, and calculation;

[0154] SS3. Extract the main flow corresponding to each typical process as a keyword, and establish a set of all keywords of the sub-function as the keyword sequence of the sub-function;

[0155] SS4. Obtain keyword sequences for all sub-functions and establish a database including each sub-function and the corresponding keyword sequence;

[0156] SS5. Get the specified function of user input;

[0157] SS6. Match the designated function with the sub-functions in the above database, and extract the keyword sequence corresponding to the matching sub-function as the keyword sequence corresponding to the designated function.

[0158] Preferably, the encoding module executes the following procedure to perform one-hot encoding on the keyword sequence corresponding to the specified function and the code sequence input by the user respectively:

[0159] SS7. Extract all keywords from the database, perform deduplication processing on the extracted results, and establish a keyword table;

[0160] SS8. According to the established keyword table, perform One-Hot encoding on the keyword sequence corresponding to the specified function, obtain the keyword sequence encoding result corresponding to the specified function, and obtain the embedding value of the encoding result; wherein, if the keyword in the keyword table exists in the keyword sequence corresponding to the specified function, it is set to 1, otherwise, it is set to 0, and each element of the keyword table is traversed in turn;

[0161] SS9. Get the code sequence entered by the user;

[0162] SS10. According to the keyword table, One-Hot encoding is performed on the code sequence input by the user to obtain the encoding result of the code sequence input by the user.

[0163] The method in which the data processing module executes the following program to train the dual-path GPT2 improved model and the Logistic classifier is shown in Example 2.

[0164] Compared with Example 3, the automatic code generation device provided in this embodiment provides a specific two-way GPT2 improved model, as well as a training method for the two-way GPT2 improved model and the Logistic classifier. Through the device, automatic code generation that meets various specified functions can be obtained, overcoming the problem that the controllability and logic of automatic code generation are difficult to meet the actual production application needs. The present invention can automatically generate high-quality production codes with good controllability, which is of great significance to reducing product development costs, improving efficiency, and preventing quality risks.

[0165] Those skilled in the art will appreciate that all or part of the processes of the above-mentioned embodiments can be implemented by instructing related hardware through a computer program, and the program can be stored in a computer-readable storage medium, wherein the computer-readable storage medium is a disk, an optical disk, a read-only storage memory, or a random access memory, etc.

[0166] The above description is only a preferred specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily conceived by any technician familiar with the technical field within the technical scope disclosed by the present invention should be covered within the protection scope of the present invention.

Claims

1. A method for automatically generating code based on a specified function, characterized in that: include: Obtain all typical processes of the specified function input by the user, set the main flow corresponding to each typical process as a keyword, and establish a keyword sequence corresponding to the specified function; One-hot encoding is performed on the keyword sequence corresponding to the specified function and the code sequence input by the user to obtain respective encoding results; Each element in the keyword sequence encoding result is spliced ​​with the code template selected by the user, and each splicing result and the historical code of the specified function obtained by the last code automatic generation are used as a group of data, and input into the pre-trained two-way GPT2 improved model to obtain the output probability of each group of data; wherein, the two-way GPT2 improved model is: the splicing result and the historical code of the specified function are used as two-way inputs, input into their respective GPT2 models, extract the 9th to 11th layer outputs of their respective GPT2 models, first dot-multiply the two-way outputs and then sum them, normalize the sum result, and input the normalized result into the 12th layer of the GPT2 model to obtain the output probability; Identify the keywords corresponding to the elements of the first K groups of data with the highest output probability as candidate keywords; Take each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a group of data, input it into the pre-trained Logistic classifier, and obtain the logical probability of each group of data; The candidate keyword corresponding to the maximum logical probability is selected as the first code after the code sequence input by the user, and all codes are output in sequence to complete the automatic code generation.

2. The method for automatic code generation according to claim 1, characterized in that: Acquire all typical processes of the designated function input by the user, set the main flow corresponding to each typical process as a keyword, and establish a keyword sequence corresponding to the designated function, further comprising the following steps: According to user needs, the target system is functionally divided to obtain all sub-functions; For each sub-function, all corresponding typical processes are obtained, where the typical processes include at least one of description, loop, comparison, sorting, and calculation; Extract the main flow corresponding to each typical process as a keyword, and establish a set of all keywords of the sub-function as the keyword sequence of the sub-function; Obtain keyword sequences of all sub-functions, and establish a database including each sub-function and the corresponding keyword sequence; Get the specified function of user input; The designated function is matched with the sub-functions in the above database, and a keyword sequence corresponding to the matching sub-function is extracted as the keyword sequence corresponding to the designated function.

3. The method for automatic code generation according to claim 2, characterized in that: One-hot encoding is performed on the keyword sequence corresponding to the specified function and the code sequence input by the user to obtain respective encoding results, further comprising the following steps: Extract all keywords from the database, perform deduplication processing on the extracted results, and establish a keyword table; According to the established keyword table, the keyword sequence corresponding to the specified function is One-Hot encoded to obtain the keyword sequence encoding result corresponding to the specified function, and the embedding value of the encoding result is obtained; wherein, if the keyword in the keyword table exists in the keyword sequence corresponding to the specified function, it is set to 1, otherwise, it is set to 0, and each element of the keyword table is traversed in turn; Get the code sequence entered by the user; According to the keyword table, One-Hot encoding is performed on the code sequence input by the user to obtain the encoding result of the code sequence input by the user.

4. The method for automatically generating code according to any one of claims 1 to 3, characterized in that: The two-way GPT2 improved model and Logistic classifier are trained through the following steps: Acquire training data including all sub-functions of the target system; each set of training data includes a keyword sequence of a sub-function, a code sequence input by a user, a standard code template, and a historical code corresponding to the sub-function, and the output data is the expected keyword corresponding to the standard code template; When using each set of training data for training, the keyword sequence corresponding to the sub-function and the code sequence input by the user are respectively One-Hot encoded to obtain respective encoding results; Each element of the keyword sequence encoding result is spliced ​​with the standard code template respectively, and each splicing result and the historical code of the specified function obtained by the last code automatic generation are used as a group of data, which are input into the two-way GPT2 improved model to obtain the output probability of each group of data; the keywords corresponding to the elements of the first K groups of data with the largest output probability are identified as candidate keywords; Take each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a group of data, input it into the pre-trained Logistic classifier, and obtain the logical probability of each group of data; Identify the candidate keyword corresponding to the maximum logical probability, compare it with the expected keyword, and determine whether the two are consistent. If they are consistent, the training result y=1, otherwise, the training result y=0; According to the above training results y, the loss function J is obtained by the following formula (i) (θ,ε) In the formula, θ is the improved model parameter of dual-path GPT2, ε is the Logistic classifier parameter, u is the constant coefficient, i is the current iteration cycle, m is the maximum number of iterations, and y is the maximum number of iterations. (i) is the training result, h (i) is the maximum logical probability, J (0) (θ,ε)=0; Compare the obtained loss function with the loss function of the previous iteration cycle; if The GPT2 improved model parameter θ and the Logistic classifier parameter ε are updated through the update strategy in the following formula to obtain the update results θ', ε'θ' = θ-α θ e'=e-a ε In the formula, α θ , α ε is the preset update step size; if The GPT2 improved model parameter θ and the Logistic classifier parameter ε are updated through the update strategy 2 in the following formula to obtain the updated results θ', ε' e'=e-a ε in v (t) =β2v (t-1) +β1 m (t) =β4m (t-1) +β3 In the formula, β1, β2, β3, β4, η, τ are all constants, m (0) =v (0) =0; Use the two-way GPT2 improved model and Logistic classifier with updated parameters to obtain the candidate keywords corresponding to the maximum logical probability again, until the candidate keywords corresponding to the maximum logical probability are consistent with the expected keywords, completing the training of the two-way GPT2 improved model and Logistic classifier using this set of training data; The next set of training data is used to train the two-way GPT2 improved model and the Logistic classifier, and the training of all training data is completed in sequence to obtain the trained two-way GPT2 improved model and Logistic classifier.

5. A device for automatically generating codes based on a specified function, characterized in that: include: The information collection module is used to receive the specified function input by the user, provide the user with a code template to select, and enter the code sequence input by the user and send it to the data processing module; The data processing module is used to obtain all typical processes of the specified function, set the main flow corresponding to each typical process as a keyword, and establish a keyword sequence corresponding to the specified function; perform One-Hot encoding on the keyword sequence corresponding to the specified function and the code sequence input by the user to obtain respective encoding results; splice each element in the keyword sequence encoding result with the code template selected by the user, and input each splicing result and the historical code of the specified function obtained by the last code automatic generation as a group of data into a pre-trained two-way GPT2 improved model to obtain the output probability of each group of data; wherein, the two-way GPT2 improved model is: the splicing result and the historical code of the specified function are used as two-way inputs, input into their respective GPT2 models, extract the 9th to 11th layer outputs of their respective GPT2 models, first dot-multiply the two-way outputs and then sum them, normalize the sum result, and input the normalized result into the 12th layer of the GPT2 model to obtain the output probability; Identify the keywords corresponding to the elements of the first K groups of data with the largest output probability as candidate keywords; take each candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence input by the user as a group of data, input them into the pre-trained Logistic classifier, obtain the logical probability of each group of data, and send the logical probability of each group of data and the corresponding keywords to the code generation module; The code generation module is used to select the alternative keyword corresponding to the maximum logical probability as the first code after the code sequence input by the user, and feed the output new code as the updated code sequence input by the user to the data processing module, output all the codes in sequence, and complete the automatic code generation.

6. The automatic code generation device according to claim 5, characterized in that: The data processing module further comprises: A keyword generation module is used to obtain all typical processes of the specified function, set the main flow corresponding to each typical process as a keyword, establish a keyword sequence corresponding to the specified function, and send it to the encoding module; The encoding module is used to perform one-hot encoding on the received keyword sequence corresponding to the designated function and the code sequence input by the user, obtain respective encoding results, send the keyword sequence encoding result to the candidate data generation module, and send the encoding result of the code sequence input by the user to the logic judgment module; The candidate data generation module is used to splice each element in the received keyword sequence encoding result with the code template selected by the user, and input each splicing result and the historical code of the specified function obtained by the previous code automatic generation as a group of data into the pre-trained two-way GPT2 improved model to obtain the output probability of each group of data, identify the keywords corresponding to the elements of the first K groups of data with the largest output probability, and send them as candidate keywords to the logic judgment module; The logic judgment module is used to obtain the Embedding value of the keyword sequence encoding result, and input each received candidate keyword, the Embedding value of the keyword sequence encoding result, and the encoding result of the code sequence entered by the user as a group of data into the pre-trained Logistic classifier to obtain the logical probability of each group of data, and send the logical probability of each group of data and the corresponding keyword to the code generation module.

7. The automatic code generation device according to claim 6, characterized in that: The keyword generation module executes the following procedure to establish a keyword sequence corresponding to the specified function: According to user needs, the target system is functionally divided to obtain all sub-functions; For each sub-function, all corresponding typical processes are obtained, where the typical processes include at least one of description, loop, comparison, sorting, and calculation; Extract the main flow corresponding to each typical process as a keyword, and establish a set of all keywords of the sub-function as the keyword sequence of the sub-function; Obtain keyword sequences of all sub-functions, and establish a database including each sub-function and the corresponding keyword sequence; Get the specified function of user input; The designated function is matched with the sub-functions in the above database, and a keyword sequence corresponding to the matching sub-function is extracted as the keyword sequence corresponding to the designated function.

8. The automatic code generation device according to claim 7, characterized in that: The encoding module executes the following procedure to perform one-hot encoding on the keyword sequence corresponding to the specified function and the code sequence input by the user respectively: Extract all keywords from the database, perform deduplication processing on the extracted results, and establish a keyword table; According to the established keyword table, the keyword sequence corresponding to the specified function is One-Hot encoded to obtain the keyword sequence encoding result corresponding to the specified function, and the embedding value of the encoding result is obtained; wherein, if the keyword in the keyword table exists in the keyword sequence corresponding to the specified function, it is set to 1, otherwise, it is set to 0, and each element of the keyword table is traversed in turn; Get the code sequence entered by the user; According to the keyword table, One-Hot encoding is performed on the code sequence input by the user to obtain the encoding result of the code sequence input by the user.

Citation Information

Patent Citations

  • Code submission annotation prediction method based on code change key class judgment

    CN110908709A

  • Intelligent text processing method and device, electronic equipment and storage medium

    CN111144093A