Code generation method and system for introducing learnable algorithm knowledge

Through the tag generator, algorithm tags are generated and word embedding vectors are combined to assist in code generation, which solves the problem of algorithm programming questions reliance on rote memorization in the existing technology, and achieves efficient and accurate code generation and explanatory improvement.

CN120540656APending Publication Date: 2025-08-26INST OF SOFTWARE - CHINESE ACAD OF SCI
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510393036.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-31
Publication Date
2025-08-26

AI Technical Summary

Technical Problem

When faced with complex algorithm programming problems, existing code generation technologies rely on rote memorization rather than understanding, and the computing resources and time cost are high, so the effectiveness of manual construction of prompt words is difficult to guarantee.

Method used

The tag generator analyzes the problem to generate algorithm tags, uses algorithm tags and word embedding to select related algorithm knowledge, assists in code generation, reduces computing resources and time costs, and uses gradient backhaul to learn algorithm knowledge.

Benefits of technology

It improves the accuracy and interpretability of code generation, significantly improves the model's ability to solve algorithm programming problems, reduces computing resources and time consumption, and the generated algorithm tags can also help human programmers understand the problems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120540656A_ABST
    Figure CN120540656A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation method and system introducing learnable algorithm knowledge, and belongs to the technical field of code generation. In order to solve the problem of mechanical memory and high computing resource consumption existing in an existing method, a tag generator is mainly adopted to analyze a question to generate an algorithm tag, related algorithm knowledge is selected by utilizing the algorithm tag and word embedding, and the algorithm knowledge is input into a trained large language model after being embedded and spliced with the question to assist in code generation. According to the method, code generation based on algorithm knowledge can be realized, the accuracy and interpretability of code generation are improved, and meanwhile, training computing resources and time cost are reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of code generation, and in particular relates to a code generation method and system that introduces learnable algorithm knowledge. Background Art

[0002] Code generation refers to the automatic generation of program code from natural language descriptions. This technology has made significant progress with the emergence and application of large language models (LLMs). Code generation techniques implemented using LLMs first receive a problem description and then output code that satisfies the requirements. While LLMs have achieved impressive results on many code generation tasks, such as function-level code block completion and module-level functionality automatic generation, significantly improving development efficiency, performance bottlenecks persist when dealing with complex problems such as algorithmic programming problems. Algorithmic programming problems typically consist of a natural language problem description, test cases, and corresponding auxiliary code blocks. The goal of code generation techniques for such problems is to generate code that satisfies the problem description, thereby helping developers learn and train coding skills. For simple tasks like bubble sort, models can easily map them to corresponding code. However, as problem complexity increases, directly generating accurate code becomes increasingly difficult, especially for problems involving multi-step reasoning.

[0003] To improve the performance of LLMs on algorithmic programming problems, a common approach is to fine-tune them on a large number of natural language-code pairs (<problem description, code>). While this approach is simple and convenient, it ignores the algorithmic analysis required to solve such problems, causing the model to rely more on rote memorization than comprehension when generating code. To address the memorization issue between the problem and the code, some methods have introduced intermediate steps based on natural language cues. These studies attempt to decompose the problem into easily solvable subproblems and generate step-by-step guidance plans to assist in code generation. While natural language cues provide strong interpretability and address the memorization issue associated with fine-tuning, accurately decomposing the problem into step-by-step guidance plans is as difficult as solving the problem itself. Some researchers have found that integrating algorithmic knowledge into cues often improves the model's problem-solving and planning capabilities. However, the effectiveness of cues depends heavily on the distribution of the model's training data, which is often unknown. One approach that does not require training data is to use additional training data to fine-tune the learnable word embeddings. This approach can be used to obtain effective prompt words, but directly applying this approach can only produce an effective word embedding, which cannot meet the requirements of specific problem-specific analysis of algorithmic programming problems.

[0004] The current code generation technology mainly relies on LLM, among which the use of algorithmic programming question data to fine-tune the pre-training model is the current mainstream method. Its characteristic is that the code is directly output after the question is input, and there is no intermediate step in this process. Another method is to introduce the prompt word technology of the intermediate step. The intermediate steps include problem-solving steps, problem-solving tips, knowledge required for problem-solving, etc. These steps are added to the code generation step in the form of natural language prompt words. One of the technologies that introduces algorithmic knowledge to assist code generation is closest to the present invention. Specifically, this technology artificially constructs prompt words of algorithmic knowledge (dynamic programming, etc.), and prompts LLM to judge the algorithmic knowledge needed to solve the problem when generating code, and then adds the corresponding algorithmic knowledge as a prompt to the input of the model.

[0005] Currently, the mainstream approach to code generation based on LLM is to fine-tune a pre-trained model using data from algorithmic programming problems. Fine-tuning data is typically collected in the form of problem-code pairs (problem description, corresponding code). Therefore, code generation using this approach directly outputs code upon receiving the problem input, eliminating any intermediate steps. While convenient, this approach ignores the algorithmic analysis required to solve algorithmic programming problems, resulting in code generation relying more on rote memorization than comprehension. Furthermore, fine-tuning typically requires updating all model parameters, placing high demands on computing resources and time. Furthermore, while introducing intermediate prompts addresses the issue of rote memorization, the effectiveness of manually constructed prompts is questionable, as it requires prior knowledge of the training data distribution, and some overly detailed intermediate steps are as difficult to generate and construct as solving the problem itself. Summary of the Invention

[0006] The purpose of the present invention is to propose a code generation method and system that introduces learnable algorithm knowledge. The method uses a label generator to analyze questions and generate algorithm labels. The algorithm labels and word embeddings are used to select relevant algorithm knowledge to assist in code generation, thereby solving the mechanical memory problem in existing methods and reducing training computing resources and time costs.

[0007] To achieve the above object, the technical solution adopted by the present invention is as follows:

[0008] A code generation method that introduces learnable algorithm knowledge includes the following steps:

[0009] Use the algorithm to label the question set to obtain a training dataset and train the label generator and large language model;

[0010] Check whether the question contains an algorithm label. For questions that do not contain an algorithm label, use the trained label generator to generate the corresponding algorithm label.

[0011] Generate the corresponding algorithm knowledge embedding vector based on the algorithm label, use the large language model word embedding layer to generate the question embedding vector, and concatenate these two embedding vectors to obtain a joint representation vector;

[0012] Input the joint representation vector into the trained large language model to generate code that meets the requirements of the question.

[0013] Furthermore, the algorithm tag contains 30 items, including four categories: algorithms, programming techniques, data structure operations, and other programming topics.

[0014] Furthermore, the method of using algorithm labels to label the question set is: generate an algorithm label matrix for each question, each row in the matrix corresponds to a question, each column corresponds to an algorithm label, and the matrix elements are used to represent the correspondence between the question and the label.

[0015] Furthermore, the label generator consists of a Transformer encoder model and multiple algorithmic binary classification neural networks (MLPs). The Transformer encoder model consists of a series of encoding layers, each of which includes a multi-head attention layer and a feedforward neural network.

[0016] Furthermore, the steps of generating the algorithm label by the label generator include:

[0017] The Transformer encoder model converts the title into a word embedding vector through the word embedding layer;

[0018] The Transformer encoder model processes word embedding vectors, uses a multi-head attention mechanism to capture the global dependencies of questions, and performs nonlinear transformations on the attention outputs through a feedforward neural network to generate a question representation vector that contains the semantic information of the question.

[0019] The question representation vector is input into multiple algorithm binary classification neural networks (MLPs), which perform binary classification tasks respectively and generate prediction results for each algorithm label.

[0020] Furthermore, when training the label generator, the loss function of the label generator is calculated based on the true labels of the training dataset and the predicted labels generated by the label generator.

[0021] Furthermore, the step of generating a corresponding algorithm knowledge embedding vector according to the algorithm label includes:

[0022] Extract the average code length corresponding to each algorithm label and the total code length corresponding to all algorithm labels. Based on the ratio of these two lengths and combined with hyperparameters, calculate the algorithm knowledge embedding length of each algorithm label.

[0023] Generate an algorithm knowledge embedding matrix corresponding to each algorithm label. The dimension of the matrix is ​​determined by the calculated algorithm knowledge embedding length and the preset embedding dimension.

[0024] Based on the algorithm labels contained in the question, the corresponding algorithm knowledge embedding matrix is ​​selected and updated; for questions containing multiple algorithm labels, the corresponding algorithm knowledge embedding matrices are spliced ​​in a predetermined order to form a complete algorithm knowledge embedding vector.

[0025] Furthermore, the step of generating code in the large language model includes: inputting the question embedding vector, the algorithm knowledge embedding vector and the generated code token embedding vector into the large language model to generate the current code token; repeating this step until the code generation is completed.

[0026] A code generation system that introduces learnable algorithm knowledge, comprising:

[0027] The label generator, which consists of a Transformer encoder model and multiple algorithm binary classification neural networks (MLPs), is used to parse questions and generate corresponding algorithm labels;

[0028] Algorithm knowledge acquisition module, including algorithm embedding layer, used to generate corresponding algorithm knowledge embedding vector according to algorithm label;

[0029] The large language model generates a question embedding vector through the word embedding layer, and generates code that meets the requirements of the question based on the concatenation vector of the algorithm knowledge embedding vector and the question embedding vector.

[0030] The beneficial effects achieved by the present invention are as follows:

[0031] 1. This invention utilizes the algorithmic labels of algorithmic programming problems and the learnable word embedding coding algorithm knowledge to directly learn effective prompt words through gradient backpropagation, avoiding the time-consuming and labor-intensive manual construction of prompt words and the uncertainty of their effectiveness.

[0032] 2. This invention analyzes questions and generates algorithmic tags by introducing a tag generator, thereby performing algorithmic analysis on the questions and using algorithmic knowledge to guide code generation. Compared with the fine-tuning method of directly generating code, this method has stronger interpretability and is more in line with the programmer's thinking process when solving algorithmic questions.

[0033] 3. The present invention adopts a method of dynamically selecting and updating algorithm knowledge embedding, optimizing the algorithm knowledge vector according to the algorithm label during the model training process, so that the model has more targeted code generation capabilities for programming questions of different algorithm categories.

[0034] 4. The present invention provides coding guidance for code generation by combining the learned algorithm knowledge with the problem description, which reduces the consumption of computing resources and time compared with the fine-tuning method.

[0035] 5. The algorithmic knowledge learned through gradient backpropagation is more effective than manually constructed prompt words and can significantly improve the accuracy of code generation. Experiments on test datasets show that the relative improvement of the model is as high as 180%.

[0036] 6. The algorithm tags generated by the present invention are not only used for code generation, but also can provide algorithm hints for human programmers to assist programmers in understanding and solving algorithm programming problems. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] Figure 1 It is a flow chart of the code generation method of the present invention.

[0038] Figure 2 This is the structure diagram of the label generator.

[0039] Figure 3 It is a schematic diagram of algorithmic knowledge learning.

[0040] Figure 4 is an example diagram of the generated code. DETAILED DESCRIPTION

[0041] In order to make the various technical features and advantages or technical effects of the above technical solutions of the present invention more obvious and easy to understand, they are described in detail below through embodiments.

[0042] The embodiment of the present invention specifically provides a code generation method that introduces learnable algorithm knowledge, and its process is as follows: Figure 1 As shown. In the reasoning phase, this method first checks whether the problem to be solved has an available algorithm label, which is used to select algorithmic knowledge related to the problem; then the selected algorithmic knowledge is spliced ​​with the problem, and the code generation process is assisted by providing algorithmic guidance. If there is no available algorithm label, the label generator is called to analyze the problem and generate the corresponding algorithm label. Then, the relevant algorithmic knowledge in the algorithm embedding layer is selected based on the generated label. For the training phase, the algorithm label is also used to select relevant algorithmic knowledge and splice it with the problem. During the gradient calculation process, the parameters of the relevant algorithmic knowledge will be updated, while the parameters of the algorithmic knowledge not related to the problem will be ignored.

[0043] The specific processing steps of this method are as follows:

[0044] 1. Algorithmic label generation

[0045] First, the algorithm programming problem is divided into 30 algorithm tags, and the algorithm tag set T = {t1, t2, ..., t K}, where K = 30 represents the total number of algorithm tags. These algorithm tags are further divided into four categories, namely algorithms, programming techniques, data structure operations, and other programming topics, as shown in Table 1.

[0046] Table 1 Algorithm labels

[0047]

[0048] Check if the question has an available algorithm tag. If not, analyze the question and generate an algorithm tag. This paper builds a tag generator based on the Transformer encoder model and the binary classification algorithm. Figure 2 , used to generate algorithm labels. The encoder model is good at capturing text semantics and providing text representation vectors that can be used for classification.

[0049] The Transformer encoder model consists of a series of encoding layers, each of which includes a multi-head attention layer and a feedforward neural network. The multi-head self-attention mechanism captures global dependencies by calculating the relationship between each element in the input sequence and other elements. The feedforward neural network performs nonlinear transformations on the self-attention output to enhance the model's expressive power.

[0050] The label generator is pre-trained using a training dataset, which is a set of questions, represented by P = {p1, p2, ..., p N}, containing N questions. These questions are transformed by the word embedding layer of the Transformer encoder model to obtain word embedding vector representation Among them, Embed enc (.) represents the word embedding layer of the Transformer encoder model. The Transformer encoder model represents the word embedding vector P e Processing, using the multi-head attention mechanism to capture the global dependency of the question text, and performing nonlinear transformation on the attention output through the feedforward neural network to obtain the question representation vector P sr , which contains the semantic information of the topic. The encoding process is expressed as

[0051] The algorithm label corresponding to each question in the training data set can be expressed as a matrix L = {l1,l2,…,l N} T , where l∈R k×1 . Each element in the matrix Ln,k ∈{0,1} indicates whether the nth question has the kth algorithm label. The matrix L is used to store the algorithm label information of each question in the question set P. Each row of the matrix L corresponds to a question in the question set P, and each column corresponds to an algorithm label. For example, L 1,3 Indicates whether the first question in the set P has the third algorithm label. Since a question usually corresponds to multiple algorithm labels, this method uses a one-to-many binary classification method to generate algorithm labels. Each algorithm label corresponds to a neural network for binary classification.

[0052] The loss function for training the label generator is expressed as follows:

[0053]

[0054] Among them, MLP k (.) represents the binary classification neural network used for the k-th algorithm label, and δ(.) represents the Sigmoid function.

[0055] This step embeds and represents the question text using an encoder model, effectively capturing the question's semantic information. A multi-head attention mechanism and feedforward neural network are used to enhance the model's ability to extract text features. Algorithmic labels are generated through a one-to-many binary classification approach, and independent neural networks are used to predict each label, ensuring accurate and robust label generation.

[0056] 2. Algorithm knowledge learning

[0057] Algorithm knowledge is learned using learnable question embeddings and algorithm labels. For each algorithm label, the corresponding algorithm knowledge is selected through an algorithm embedding layer (e.g., a linear mapping layer or an embedding lookup table). The question embedding is obtained by transforming the question through the word embedding layer of a large language model.

[0058] Specifically, each algorithm label k corresponds to an algorithm knowledge embedding matrix Where D is the embedding dimension, C k is the embedding length, which is used to reflect the complexity of algorithm knowledge. k It is calculated based on the ratio of the average code length of the corresponding label to the total code length in the dataset. The calculation formula is:

[0059]

[0060] Among them, base is a hyperparameter, which represents the sum of the embedding lengths of all algorithm knowledge; AvgCodeLen(.) represents the average length of the code corresponding to the algorithm label in the dataset; round(.) is the rounding function.

[0061] During the training process, the algorithm label in the question is used to select and update the corresponding algorithm knowledge embedding parameters, thereby realizing the learning of algorithm knowledge. If a question involves multiple algorithm labels, the corresponding multiple algorithm knowledge embedding matrices are spliced ​​in a predetermined order. Finally, the algorithm knowledge embedding matrix is ​​spliced ​​with the embedding vector of the question to form a joint representation vector (see Figure 3 ), and then input into the large language model for training.

[0062] This step achieves refined learning for different algorithm labels by dynamically generating and optimizing the algorithm knowledge embedding matrix. Combined with the training method of the question embedding vector, it helps the model fully utilize the algorithm knowledge related to the label, improving the model's performance in algorithm classification and knowledge learning.

[0063] 3. Code Generation

[0064] The concatenated joint representation vector is input into the large language model to generate the code for the question. Specifically, for each token of the question code generated by the large language model, the i-th token generated by the n-th question is represented as And all tokens before the i-th token are represented as The entire code generation process can be expressed as follows:

[0065]

[0066] Among them, LLM (.) represents the large language model, AEL (.) represents the learned algorithm knowledge embedding vector, and Embed dec (.) represents the word embedding layer of the large language model, represents the generated algorithm label, Represents the topic embedding vector.

[0067] This step uses algorithm labels to select appropriate algorithm knowledge embedding vectors during the code generation process, thereby generating code more accurately and ensuring the accuracy of code generation.

[0068] Figure 4 This is an example of using this method for code generation.

[0069] Experimental test:

[0070] The method of the present invention is compared with the code generation method of the prior art. The code generation effect is shown in Table 2. It can be seen that the method of the present invention has a significant improvement in performance.

[0071] Table 2 Code generation effect

[0072]

[0073] Although the present invention has been disclosed as above by way of embodiments, they are not intended to limit the present invention. Any appropriate modification or equivalent substitution of the technical solution of the present invention by a person skilled in the art should be included in the protection scope of the present invention. The protection scope of the present invention shall be based on that defined in the claims.

Claims

1. A code generation method that introduces learnable algorithm knowledge, characterized in that: The following steps are involved: Use the algorithm to label the question set to obtain a training dataset and train the label generator and large language model; Check whether the question contains an algorithm label. For questions that do not contain an algorithm label, use the trained label generator to generate the corresponding algorithm label. Generate the corresponding algorithm knowledge embedding vector based on the algorithm label, use the large language model word embedding layer to generate the question embedding vector, and concatenate these two embedding vectors to obtain a joint representation vector; Input the joint representation vector into the trained large language model to generate code that meets the requirements of the question.

2. The method according to claim 1, wherein There are 30 algorithm tags, including four categories: algorithms, programming techniques, data structure operations, and other programming topics.

3. The method according to claim 1, wherein The method of using algorithm labels to label a set of questions is to generate an algorithm label matrix for each question, where each row in the matrix corresponds to a question, and each column corresponds to an algorithm label. The matrix elements are used to represent the correspondence between the question and the label.

4. The method according to claim 1, wherein The label generator consists of a Transformer encoder model and multiple algorithmic binary classification neural networks (MLPs). The Transformer encoder model consists of a series of encoding layers, each of which includes a multi-head attention layer and a feedforward neural network.

5. The method according to claim 4, wherein The steps of the label generator to generate algorithm labels include: The Transformer encoder model converts the title into a word embedding vector through the word embedding layer; The Transformer encoder model processes word embedding vectors, uses a multi-head attention mechanism to capture the global dependencies of questions, and performs nonlinear transformations on the attention outputs through a feedforward neural network to generate a question representation vector that contains the semantic information of the question. The question representation vector is input into multiple algorithm binary classification neural networks (MLPs), which perform binary classification tasks respectively and generate prediction results for each algorithm label.

6. The method according to claim 1, wherein When training the label generator, the loss function of the label generator is calculated based on the true labels of the training dataset and the predicted labels generated by the label generator.

7. The method according to claim 6, wherein The loss function of the label generator is expressed as follows: Among them, MLP k (.) represents the binary classification neural network for the kth algorithm label, δ(.) represents the Sigmoid function, represents the question representation vector, L n,k Indicates whether the nth question has the kth algorithm tag, where N is the number of questions and K is the number of algorithm tags.

8. The method according to claim 1, wherein The steps of generating the corresponding algorithm knowledge embedding vector according to the algorithm label include: Extract the average code length corresponding to each algorithm label and the total code length corresponding to all algorithm labels. Based on the ratio of these two lengths and combined with hyperparameters, calculate the algorithm knowledge embedding length of each algorithm label. Generate an algorithm knowledge embedding matrix corresponding to each algorithm label. The dimension of the matrix is ​​determined by the calculated algorithm knowledge embedding length and the preset embedding dimension. Based on the algorithm labels contained in the question, the corresponding algorithm knowledge embedding matrix is ​​selected and updated; for questions containing multiple algorithm labels, the corresponding algorithm knowledge embedding matrices are spliced ​​in a predetermined order to form a complete algorithm knowledge embedding vector.

9. The method according to claim 1, wherein The steps of generating code in the large language model include: inputting the question embedding vector, algorithm knowledge embedding vector and the generated code token embedding vector into the large language model to generate the current code token; repeating this step until the code generation is completed.

10. A code generation system that introduces learnable algorithm knowledge, used to implement the method according to any one of claims 1 to 9, characterized in that: include: The label generator, which consists of a Transformer encoder model and multiple algorithm binary classification neural networks (MLPs), is used to parse questions and generate corresponding algorithm labels; Algorithm knowledge acquisition module, including algorithm embedding layer, used to generate corresponding algorithm knowledge embedding vector according to algorithm label; The large language model generates a question embedding vector through the word embedding layer, and generates code that meets the requirements of the question based on the concatenation vector of the algorithm knowledge embedding vector and the question embedding vector.