Personalized code generation system and method based on residual learning
Through the personalized code generation system based on residual learning, it extracts and imitates the coding style characteristics of developers, solves the problem that traditional methods are difficult to understand and adapt to personalized coding styles, achieves efficient and customized code generation, and reduces code Maintenance costs.
Patent Information
- Application Number
- CN202410722876.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-05
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2044-06-05
AI Technical Summary
Traditional code generation methods are difficult to capture developers' individual coding styles and preferences, resulting in generated code that requires developers to review and modify, increasing the cost of code development and maintenance.
A personalized code generation system based on residual learning is used to build a residual learning framework for a large language model by performing style detection on user historical codes, extract and imitate the developer's code style characteristics, and generate personalized code.
It improves the degree of customization of automated code generation and reduces code maintenance costs. The generated code is consistent with the developer's style and improves development efficiency.
Smart Images

Figure CN118733045A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of natural language processing, and specifically refers to a personalized code generation system and method based on residual learning. Background Art
[0002] As the complexity of software development increases, automatic code generation technology has become one of the key means to improve development efficiency and code quality. Traditional code generation methods, such as rule-based methods and simple machine learning models, often have difficulty capturing the nuances and personalized requirements of coding.
[0003] In recent years, deep learning technology, especially large pre-trained language models (such as GPT-3, Codex, etc.), has shown great potential in the field of code generation. These models can learn the syntax and structure of programming languages based on massive code bases, and implement functions such as automatic code completion, bug fixes, and code refactoring. However, although these models perform well in understanding and generating code, they generally lack the ability to understand and adapt to developers' personalized coding styles and preferences. Code that has not been personalized often requires developers to review and modify it, and it is very easy to miss, which greatly increases the cost of code development and maintenance.
[0004] Although large language models have powerful language understanding and generation capabilities in the field of code generation, how to generate personalized code remains a challenge. Traditional personalization methods cannot fully understand the code style habits of developers and meet the needs of personalized generation. Therefore, a learning method that can capture the code syntax style of developers is needed to meet the code personalization needs of different developers.
[0005] Given the excellent code generation effect and performance of deep pre-training models, we introduced them into personalized generation tasks. In order to better achieve personalized code generation, we need to find a personalized input and output method that fits the characteristics of large language models, effectively captures and imitates the code style and preferences of developers, and effectively establishes fine-grained code style features for different developers. Summary of the invention
[0006] In order to solve the problems in the background technology, the present invention proposes a novel residual learning framework, based on which a personalized code generation method based on residual learning is provided. The purpose of the present invention is to design a method for representing the developer's code style, decompose the user's code style into different code features, combine the code features learned by residual learning to form the developer's code style, and guide the large language model to generate corresponding personalized code through the code style features.
[0007] The technical solution adopted by the present invention is as follows, which specifically comprises the following steps:
[0008] 1. A personalized code generation system based on residual learning, comprising:
[0009] The pre-processing module performs style detection on the user's historical code to obtain the style sequence of the corresponding code snippet;
[0010] The information extraction module builds a residual learning framework based on the large language model (LLM) based on the user's historical code and style sequence, and extracts the user's style information through the residual learning framework; the style information is a vector representation of each code style specification learned during training;
[0011] The code generation module integrates the input natural language and style information and completes the code generation task with the help of the large language model LLM.
[0012] 2. A personalized code generation method based on residual learning
[0013] Applied to the above-mentioned personalized code generation system, the personalized code generation method specifically includes the following steps:
[0014] 1) Define several code style specifications from the three perspectives of naming, format, and structure, obtain a data set of code snippets of programming languages, and use code detection tools to collect style sequences representing corresponding code snippets;
[0015] 2) Setting the residual term based on the code in step 1) and its corresponding style sequence to generate training data that can be used for residual learning;
[0016] 3) Set a learnable vector representation for each code style specification;
[0017] 4) Based on the learnable vector representation in step 3), a residual learning framework is constructed through a large language model LLM;
[0018] 5) Based on the training data in step 2), residual learning is performed through the residual learning framework in step 4), and the vector representation of each code style specification is obtained through training and learning;
[0019] 6) For a developer's historical code, obtain its style sequence through the code detection tool, and use the vector representation of the code style specification trained in step 5) to generate personalized code consistent with the developer's historical code style.
[0020] The step 1) is specifically as follows:
[0021] Each code snippet C in the collected programming language code snippet dataset is collected from the three perspectives of naming, format, and structure through a code detection tool to obtain the style sequence S of the corresponding code snippet = {S1, S2, ..., S n};
[0022] The style sequence is a sequence consisting of code style specifications as sequence elements; n is the number of collected code style specifications, and the element S in the style sequence is n The value is 0 or 1, indicating whether the code style specification of the current location is followed. 1 means it is followed, and 0 means it is not followed.
[0023] Coding style specifications include: naming specifications such as using camel case for variable names; format specifications such as limiting line length to no more than 50 characters and using spaces on both sides of operators; and structural specifications such as the order of methods in a class following the order of public methods first and private methods later.
[0024] The step 2) is specifically as follows:
[0025] Traverse the code snippets in step 1) and their corresponding style sequences to construct a residual learning dataset D that satisfies the following conditions:
[0026] Each residual learning data d in the residual learning dataset D = {C A , S A , C B , S B , r, t, T};
[0027] Among them, S A , S B For code snippet C A With code snippet C B The corresponding two sequences; extract S B The elements with median value 1 form the sequence S B ′; Extract S A The elements with median value 1 form the sequence S A ′; Code snippet C A With code snippet C B Need to satisfy the corresponding two sequences S A ′,S B The number of different elements in ′ is 1;
[0028] Among them, r represents two sequences S A ′,S B The code style specification names of the different elements in ′; t is the textual detailed explanation of the code style specification of r.
[0029] For each residual learning data d, the training text T is expressed as: "Two pieces of code and their corresponding style sequences are known; please identify and explain the code style specification name that appears in the second piece of code but not in the first piece of code; the first piece of code: C A , the style sequence of the first code segment: S A ; The second code: C B, the style sequence of the second code segment: S B ; A: r is present in the second code block but not in the first; its textual interpretation is t. ”
[0030] The step 3) specifically comprises the following steps:
[0031] Based on the set n code style specifications, a learnable vector representation is set for each code style specification, and finally a set of learnable vector representations A = {A1, A2, ..., A n}.
[0032] The step 4) of constructing the residual learning framework model specifically includes the following steps:
[0033] 4.1) For each training text T corresponding to the residual learning data: insert the corresponding learnable vector representation at the position where the style sequence value is 1, delete the position where the value is 0 from the text, replace r with the corresponding learnable vector representation (find the element position corresponding to r in the style sequence, and replace r with the corresponding learnable vector representation according to the element position), and transform the remaining positions into text embeddings through the LLM Tokenizer, and finally obtain the training embedding Q;
[0034] 4.2) Freeze all parameter weights of LLM, input the training embedding Q into LLM, and retain the weight learning of the learnable vector representation.
[0035] The step 5) specifically comprises the following steps:
[0036] The training data of step 2) is trained through the residual learning framework of step 4) to learn the vector representation of each code style specification; the negative log-likehood function is selected as the final loss function:
[0037]
[0038] Error back propagation is used to adjust the parameters represented by the learnable vector; the parameters represented by the learnable vector are selected according to the loss function of the model during the training process, and a set of parameters corresponding to the minimum loss function is used as the parameters represented by the learnable vector.
[0039] The step 6) specifically comprises the following steps:
[0040] 6.1) For a developer’s historical code, use code detection tools to collect the style sequence S = {S1, S2, ..., S n};
[0041] 6.2) The vectors corresponding to the style specifications with a value of 1 in the style sequence are represented to form a vector group A = {A1, A2, ..., A m}; where m is the number of elements with a value of 1 in the style sequence;
[0042] 6.3) The question text passes through the LLM Tokenizer to obtain the embedded sequence E = {E1, E2, ..., E n}, where n is the number of tokenized vector representations; the question text is a user programming question or requirement described in natural language;
[0043] Connect the vector group A of style features with the embedding sequence E to obtain the user's input embedding Q:
[0044] Q={A1,A2,…,A m , E1, E2, …, E n};
[0045] 6.4) The embedded Q is input into LLM through autoregressive decoding to obtain a personalized code consistent with the developer’s historical style.
[0046] 3. A terminal
[0047] The terminal includes a memory, a processor, and a personalized code generation program stored in the memory and executable on the processor. When the processor executes the personalized code generation program, the above-mentioned personalized code generation method based on residual learning is implemented.
[0048] 4. A computer-readable storage medium
[0049] The computer-readable storage medium stores a personalized code generation program, and when the personalized code generation program is executed by the processor, the above-mentioned personalized code generation method based on residual learning is implemented.
[0050] Beneficial effects of the present invention:
[0051] The present invention provides a personalized code generation system and method based on residual learning, which extracts style specifications from data and designs residual learning to train different style specification representations. With the help of style specification representation, flexible and diverse code styles can be configured to help developers generate personalized code consistent with style requirements, thereby improving the customization degree of automatic code generation, improving development efficiency, and reducing code maintenance costs. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] Figure 1 is a flow chart of the present invention;
[0053] Figure 2Generate a personalized code example diagram for the present invention;
[0054] Figure 3 It is a basic structural diagram of the personalized code generation method based on residual learning of the present invention. DETAILED DESCRIPTION
[0055] The present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0056] like Figure 1 As shown, a personalized code generation method based on residual learning of the present invention comprises the following steps:
[0057] The technical solution adopted by the present invention is as follows, which specifically comprises the following steps:
[0058] Step 1): Collect a Java code snippet dataset, and use a code detection tool to collect several code style specifications from the three perspectives of naming, format, and structure to obtain the style sequence of the corresponding code snippet:
[0059] For each code snippet C in the collected Java code snippet dataset, a code detection tool is used to collect several code style specifications from the three perspectives of naming, format, and structure, and the style sequence S of the corresponding code snippet is obtained. n}, n is the number of collected code style specifications, and the elements in the style sequence are 0 or 1, representing whether a specific code style specification is followed, 1 represents "yes" and 0 represents "no".
[0060] like Figure 1 As shown in the "Style sequence corresponding to the code", the style sequence corresponding to code A is {1, 1, 0........, 0}; the style specification at the first position means "whether ( and { are on the same line", and the style specification at the second position means "whether to use camel case naming". The element value of the style sequence is 1, which means the corresponding style specification is "yes", and the value is 0, which means the corresponding style specification is "no".
[0061] Step 2: Based on the code in step 1) and its corresponding style sequence, generate training data that can be used for residual learning by setting the residual term:
[0062] For the code in step 1) and its corresponding style sequence, construct a residual learning dataset D that satisfies the following conditions. For each residual learning data d = {C A , S A , C B , S B , r, t, T}:
[0063] Extract S BThe elements with median value 1 form the sequence S B ′; Extract S A The elements with median value 1 form the sequence S A ′, where code fragment C A With code snippet C B The two corresponding sequences S′ must be satisfied A , S B The number of different elements in ′ is 1;
[0064] Among them, r represents two sequences S′ A , S B The code style specification names of the different elements in ′, and t is the detailed textual explanation of the code style specification of r. For each residual learning data d, we construct the following training text T: "Two pieces of code and their corresponding style sequences are known. Please identify and explain the code style specification names that appear in the second piece of code but not in the first piece of code. The first piece of code: C A , the style sequence of the first code segment: S A ; The second code: C B , the style sequence of the second code segment: S B ; A: r is present in the second code block but not in the first; its textual interpretation is t. ”
[0065] like Figure 3 As shown, two similar codes and corresponding style sequences, as well as text prompt words are organized to form training texts.
[0066] Step 3): Set a learnable vector representation for each code style specification;
[0067] Based on the collected n code style specifications, a learnable vector representation is set for each code style specification, and finally a set of learnable vector representations A = {A1, A2, ..., A n};
[0068] like Figure 3 As shown, the learnable vector after the style sequence of the first code is converted is represented as {a1, a2, a3}; the learnable vector after the style sequence of the second code is converted is represented as {a1, a2, a3, a4}.
[0069] Step 4): Based on the learnable vector representation of step 3), a learning framework for residual learning is constructed through a large language model LLM;
[0070] The step (4) is specifically as follows:
[0071] 4.1) For a training text T of residual learning data, insert the corresponding learnable vector representation for the position where the style sequence value is 1, delete the position where the value is 0 from the text, and replace r with the corresponding learnable vector representation. The remaining positions are converted into text embeddings through the LLM Tokenizer, and finally the training embedding Q is obtained;
[0072] 4.2) Freeze all parameter weights of LLM, input the training embedding Q into LLM, and retain the weight learning of the learnable vector representation;
[0073] like Figure 3 As shown in the figure, the frozen CodeLlama model is used as the LLM used in the residual learning framework, and the text is converted into text embedding through the CodeLlama Tokenizer. At the same time, the style sequence is converted into the corresponding learnable vector group {a1, a2, a3} and {a1, a2, a3, a4}.
[0074] Step 5): Based on the training data from step 2), the residual learning framework of step 4) is used for training to learn the vector representation of each code style specification. The negative log-likehood function is selected as the final loss function:
[0075]
[0076] Error back propagation is used to adjust the parameters represented by the learnable vector; the parameters represented by the learnable vector are selected according to the loss function of the model during the training process, and a set of parameters corresponding to the minimum loss function is used as the parameters represented by the learnable vector.
[0077] like Figure 1 As shown in the “Training Process” in Figure 2, we train the model based on the residual learning framework by training the training data.
[0078] Step 6): Based on a developer's historical code, obtain its style sequence through a code detection tool, and use the vector representation of the code style specification trained in step 5) to generate personalized code consistent with the developer's historical code style;
[0079] The step (6) is specifically as follows:
[0080] 6.1) For a developer's historical code, we use code detection tools to collect several code style specifications from the three perspectives of naming, format, and structure to represent the style sequence specifications of the corresponding code snippet, and obtain the style sequence S = {S1, S2, ..., S n}, n is the number of collected code style specifications, and the elements in the style sequence are 0 or 1, representing whether a specific code style specification is followed, 1 represents "yes" and 0 represents "no".
[0081] like Figure 1 As shown in the "Inference Process" in , if the element of the summarized user style sequence has a value of 1 in the style sequence corresponding to the historical code, then it is assigned a value of 1; for the style specification that is always assigned a value of 0 in the style sequence of the historical code, its value is 0.
[0082] 6.2) The vectors corresponding to the style specifications with a value of 1 in the style sequence are represented to form a vector group A = {A1, A2, ..., A m}; where m is equal to the number of elements with a value of 1 in the developer's style sequence.
[0083] 6.3) The vector group A of the style features and the question text are passed through the LLM Tokenizer and the vector sequence E = {E1, E2, ..., E n} to obtain the user's input embedding Q = {A1, A2, ..., A m , E1, E2, …, E n}, where n is the number of Tokenized vector representations.
[0084] The embedded Q is input into LLM and decoded through autoregression to obtain personalized code consistent with the developer's historical style.
[0085] like Figure 1 As shown in the "Inference Process" in the example, the input question is "Given an integer array, write a function to calculate the prefix sum of the array", and the user style sequence is {1, 1, 0......., 0}. The question and the user style sequence are input into the trained model based on the residual learning framework, and finally the code that conforms to the user's coding style is obtained. The code example is as follows Figure 2 As shown, you can see that the generated code "{" and "(" are on the same line, and the class name "PrefixSum", variable name "prefixSums" and "calculatePrefixSums" are all camelCase names, which conforms to the user's historical style habits.
[0086] The effect of this method on the organized code personalized dataset is as follows:
[0087] Personalized approach BLEU-4 Rouge-1 Rouge-2 DAPT 0.3078 0.4001 0.2877 Prefix-Tuning 0.4835 0.3623 0.2511 Model of the present invention 0.5573 0.4277 0.2917
[0088] Note:
[0089] (1) DAPT and Prefix-Tuning are commonly used personalized algorithm models;
[0090] (2) BLEU-4: BLEU-4∈[0,1], the larger the value, the better the personalization effect;
[0091] (3) Rouge-1: NMI∈[0,1], the larger the value, the better the personalization effect;
[0092] (4) Rouge-2: FMI∈[0,1], the larger the value, the better the personalization effect;
[0093] (5) Compared with DAPT, the BLEU-4, Rouge-1, and Rouge-2 indicators of the present invention on the same dataset are improved by 24.95%, 2.66%, and 0.4%, respectively;
[0094] (6) Compared with Prefix-Tuning, the BLEU-4, Rouge-1, and Rouge-2 indicators of the present invention on the same dataset are improved by 7.38%, 6.54%, and 4.06%, respectively.
Claims
1. A personalized code generation system based on residual learning, characterized in that: include: The pre-processing module performs style detection on the user's historical code to obtain the style sequence of the corresponding code snippet; The information extraction module builds a residual learning framework based on the large language model (LLM) based on the user's historical code and style sequence, and extracts the user's style information through the residual learning framework; the style information is a vector representation of each code style specification learned during training; The code generation module integrates the input natural language and style information and completes the code generation task with the help of the large language model LLM.
2. A personalized code generation method based on residual learning, characterized in that: Applied to the personalized code generation system of claim 1, the personalized code generation method specifically comprises the following steps: 1) Define several code style specifications from the three perspectives of naming, format, and structure, obtain a data set of code snippets of programming languages, and use code detection tools to collect style sequences representing corresponding code snippets; 2) Setting the residual term based on the code in step 1) and its corresponding style sequence to generate training data that can be used for residual learning; 3) Set a learnable vector representation for each code style specification; 4) Based on the learnable vector representation in step 3), a residual learning framework is constructed through a large language model LLM; 5) Based on the training data in step 2), residual learning is performed through the residual learning framework in step 4), and the vector representation of each code style specification is obtained through training and learning; 6) For a developer's historical code, obtain its style sequence through the code detection tool, and use the vector representation of the code style specification trained in step 5) to generate personalized code consistent with the developer's historical code style.
3. The method for generating personalized code based on residual learning according to claim 2, characterized in that: The step 1) is specifically as follows: Each code snippet C in the collected programming language code snippet dataset is collected from the three perspectives of naming, format, and structure through a code detection tool to obtain the style sequence S of the corresponding code snippet = {S1, S2, ..., S n }; The style sequence is a sequence consisting of code style specifications as sequence elements; n is the number of collected code style specifications, and the element S in the style sequence is n The value is 0 or 1, indicating whether the code style specification of the current location is followed. 1 means it is followed, and 0 means it is not followed.
4. The method for generating personalized code based on residual learning according to claim 2, characterized in that: The step 2) is specifically as follows: Traverse the code snippets in step 1) and their corresponding style sequences to construct a residual learning dataset D that satisfies the following conditions: Each residual learning data d in the residual learning dataset D = {C A , S A , C B , S B , r, t, T}; Among them, S A , S B For code snippet C A With code snippet C B The corresponding two sequences; extract S B The elements with median value 1 form the sequence S B ′; Extract S A The elements with median value 1 form the sequence S A ′; Code snippet C A With code snippet C B Need to satisfy the corresponding two sequences S A ′,S B The number of different elements in ′ is 1; Among them, r represents two sequences S A ′,S B The code style specification names of the different elements in ′; t is the textual detailed explanation of the code style specification of r. For each residual learning data d, the training text T is expressed as: "Two pieces of code and their corresponding style sequences are known; please identify and explain the code style specification name that appears in the second piece of code but not in the first piece of code; the first piece of code: C A , the style sequence of the first code segment: S A ; The second code: C B , the style sequence of the second code segment: S B ; A: r exists in the second code block but not in the first; its textual interpretation is t. ".
5. The method for generating personalized code based on residual learning according to claim 2, characterized in that: The step 3) specifically comprises the following steps: Based on the set n code style specifications, a learnable vector representation is set for each code style specification, and finally a set of learnable vector representations A = {A1, A2, ..., A n }.
6. The method for generating personalized code based on residual learning according to claim 2, characterized in that: The step 4) of constructing the residual learning framework model specifically includes the following steps: 4.1) For each training text T corresponding to the residual learning data: insert the corresponding learnable vector representation at the position where the style sequence value is 1, delete the position where the value is 0 from the text, replace r with the corresponding learnable vector representation, and transform the remaining positions into text embeddings through the LLM Tokenizer, and finally obtain the training embedding Q; 4.2) Freeze all parameter weights of LLM, input the training embedding Q into LLM, and retain the weight learning of the learnable vector representation.
7. The method for generating personalized code based on residual learning according to claim 2, characterized in that: The step 5) specifically comprises the following steps: The training data of step 2) is trained through the residual learning framework of step 4) to learn the vector representation of each code style specification; the negative log-likehood function is selected as the final loss function: Error back propagation is used to adjust the parameters represented by the learnable vector; the parameters represented by the learnable vector are selected according to the loss function of the model during the training process, and a set of parameters corresponding to the minimum loss function is used as the parameters represented by the learnable vector.
8. The method for generating personalized code based on residual learning according to claim 2, characterized in that: The step 6) specifically comprises the following steps: 6.1) For a developer’s historical code, use code detection tools to collect the style sequence S = {S1, S2, ..., S n }; 6.2) The vectors corresponding to the style specifications with a value of 1 in the style sequence are represented to form a vector group A = {A1, A2, ..., A m }; where m is the number of elements with a value of 1 in the style sequence; 6.3) The question text passes through the LLM Tokenizer to obtain the embedded sequence E = {E1, E2, ..., E n }, where n is the number of tokenized vector representations; the question text is a user programming question or requirement described in natural language; Connect the vector group A of style features with the embedding sequence E to obtain the user's input embedding Q: <h2 style=";text-align:left;direction:ltr">Q={A1,A2,…,A<h2 style=";text-align:left;direction:ltr"> m <h2 style=";text-align:left;direction:ltr"> E1, E2,…, E<h2 style=";text-align:left;direction:ltr"> n <h2 style=";text-align:left;direction:ltr">}; 6.4) The embedded Q is input into LLM through autoregressive decoding to obtain a personalized code consistent with the developer’s historical style.
9. A terminal, characterized in that: The terminal includes a memory, a processor, and a personalized code generation program stored in the memory and executable on the processor. When the processor executes the personalized code generation program, the personalized code generation method based on residual learning as described in any one of claims 2 to 8 is implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a personalized code generation program, and when the personalized code generation program is executed by the processor, the personalized code generation method based on residual learning according to any one of claims 2 to 8 is implemented.
Citation Information
Patent Citations
Meta-learning-based code self-adaptive generation method
CN112114791A
Test code generation method and device, equipment and storage medium
CN117632710A
Method and device for automatically correcting code style
CN117931150A
System and method for utilizing production insights in generative AI models
US11941378B1
Learning and using programming styles
US20210004210A1
