A code comment generation method based on active learning

By selecting high-uncertainty code segments for manual annotation using an active learning method, and designing appropriate sampling strategies and incremental training, the problem of generating high-quality code comments was solved, achieving efficient annotation generation under limited budget.

CN115480821BActive Publication Date: 2026-02-06NANTONG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211129739.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-16
Publication Date
2026-02-06
Estimated Expiration
2042-09-16

AI Technical Summary

Technical Problem

Existing technologies struggle to generate high-quality code comments within limited budgets, deep neural network models perform poorly in the absence of training data, and building parallel corpora is prohibitively expensive.

Method used

An active learning approach is adopted, an appropriate sampling strategy is designed to select a representative subset of data, the model is improved through incremental training, and high-uncertainty code segments are selected for manual annotation using attention dispersion and clustering strategies to gradually optimize the model.

Benefits of technology

Generate higher quality code comments within a limited annotation budget, reduce manpower input, improve code understanding efficiency, and promote software development and maintenance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115480821B_ABST
    Figure CN115480821B_ABST
Patent Text Reader

Abstract

The application provides a code annotation generation method based on active learning, and belongs to the technical field of computers, and solves the problem that under a limited manual annotation budget, model training data is insufficient, and it is difficult to generate high-quality code annotations.The technical scheme comprises the following steps: (1) collecting domain data sets; (2) pre-training a general model; (3) designing an active learning sampling strategy based on clustering and attention dispersion; (4) continuously iterating in the two steps of data sampling, labeling and model fine-tuning until the budget is used up or the predefined termination condition is reached; (5) under the limited manual annotation budget, the fine-tuned model can generate higher quality code annotations for code segments.The beneficial effects of the application are that the model trained based on active learning can generate higher quality annotations for code segments using less training data under a limited annotation budget, thereby balancing the manpower required for a certain annotation quality.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer technology, and particularly relates to a code annotation generation method based on active learning. BACKGROUND

[0002] Generally, code annotations can help developers who are not familiar with field knowledge to understand the purpose and function of code snippets, thereby helping them to understand the program. However, due to limited project development budget or insufficient attention to the writing of code annotations, it is difficult to guarantee high-quality code annotations.

[0003] In recent years, researchers usually model the source code summarization task as a neural machine translation task. However, although some progress has been made in this field, the machine translation model is not perfect. It is well known that deep neural networks require a large amount of training data, and a large amount of high-quality parallel corpus is generally required for the training process to fully exploit the potential of deep models. The lack of training corpus is a common problem for many programming languages, and these methods will encounter various challenges in the case of data scarcity, which may eventually lead to poor performance of the neural machine translation model.

[0004] Therefore, there are more and more works to improve the performance of low-resource language models, such as dual learning and unsupervised learning, but they are often not as good as supervised models. However, it is a slow and laborious process to build a parallel corpus, and it is often expensive to hire professionals with specific domain knowledge. Therefore, how to design an effective method to automatically generate high-quality code annotations with limited manpower has become an urgent problem to be solved.

[0005] The application of active learning can effectively solve the above problems, and its goal is to train a well-performing model under a limited human translation budget. Specifically, by selecting a smaller representative subset of data to reduce the labeling cost, the selected data will be used to improve the machine translation system through incremental training or retraining, hoping that the model trained on this subset can approach the model trained on a larger labeled set.

[0006] How to solve the above technical problems has become a problem faced by the present application. SUMMARY

[0007] The application aims to provide a code annotation generation method based on active learning, which applies the active learning method to the deep code annotation generation task, and can train a model with good performance under a limited manual annotation budget. Specifically, the application designs a suitable sampling strategy to select a smaller representative subset of data to reduce the annotation cost. The selected data is used to improve the model performance through incremental learning or retraining, so that the model trained on this subset can approach the model trained on a larger annotated set.

[0008] The application proposes a code annotation generation method based on active learning. First, a deep learning model is pre-trained on an out-of-domain labeled dataset to initialize the model parameters. Second, the uncertainty score of each in-domain code snippet is obtained according to the acquisition function of the sampling strategy, and the code snippets are ranked in descending order according to the score. High-score code snippets are selected until the budget is used up or a predefined termination condition is reached. Human annotators write code annotations for these code snippets, add these sampled code snippets to the parallel corpus, and retrain the model parameters. The active learning method iterates between data selection / annotation and model updating. Finally, the model trained by active learning can generate higher-quality annotations for code snippets using less training data under a limited annotation budget. The application designs two active learning sampling strategies, which determine which code snippets are worth being supervised and annotated. The sampling strategy proposed in the application achieves better performance than random sampling in the same corpus.

[0009] The application is achieved by the following measures: a code annotation generation method based on active learning, comprising the following steps:

[0010] (1) Collecting in-domain dataset: mining high-quality open source projects from GitHub and Etherscan.io, collecting functions and corresponding annotations in open source code, forming an annotation generation dataset D, and further dividing it into a training set and a validation set. The training set is the initial unlabeled annotation generation dataset U.

[0011] (2) Pre-training a general model: pre-training a general deep learning model CodeBERT on an out-of-domain labeled dataset to initialize the model parameters. The model considers 6 programming languages, uses natural language and programming language as its input, captures the semantic connection between natural language and programming language, and outputs a general representation that can support natural language-programming language tasks. Specifically, the following steps are included:

[0012] (2-1) Training CodeBERT using mask language modeling: randomly select a set of positions to mask out for natural language and programming language, and then replace the selected positions with a special [MASK] token, the goal of which is to predict the original token that is masked out;

[0013] (2-2) Training CodeBERT using replacement token detection: train a generator model, which is a probability model similar to n-gram, to generate the masked word, and then train a discriminator model to determine whether a word is the original word.

[0014] (3) Design an active learning sampling strategy: one of the key elements of active learning is to design a reasonable sampling strategy, and the present application designs two different sampling strategies to adapt to the code annotation generation task:

[0015] (3-1) Model-related strategy based on attention dispersion: use the attention score of the deep learning model to obtain information, and select those code segments with higher attention dispersion. When generating target words one by one, the model will pay more attention to the part of the source code segment that is meaningful to the current task. If the system is translating an uncertain sample, its attention mechanism will be dispersed in the entire source sequence. A sample with higher attention dispersion, its attention probability distribution is often more uniform. Therefore, for the attention dispersion sampling strategy, the selected code segment will be those with higher attention dispersion. Specifically, the following steps are included:

[0016] (3-1-1) First, extract the weight that the attention model gives to the jth source word when decoding the ith target word, i.e. the attention distribution probability ω i,j ;

[0017] (3-1-2) Second, calculate the kurtosis of the weight for each target word y i , is the average value of the attention weight:

[0018]

[0019] (3-1-3) Finally, take the average of the negative kurtosis values of all words in the target sentence to get the attention dispersion score of the sentence:

[0020]

[0021] (3-1-4) Select the code segment with higher attention dispersion score, i.e. higher attention dispersion.

[0022] (3-2) Cluster-based model-independent strategy: using the idea of clustering, from the perspective of sample diversity, the samples with relatively distant semantic embedding vectors of source code are selected. Specifically, the following steps are included:

[0023] (3-2-1) First, the semantic embedding vector of the source code in the domain is calculated;

[0024] (3-2-2) Then, clustering is performed, and k points in the feature space are randomly set as the initial cluster centers. The distance from each point to the k centers is calculated, and then the labeled cluster centers are recalculated to obtain the new center point of each cluster;

[0025] (3-2-3) After clustering, the L2 distance between the semantic vectors of the code segments in each category is calculated to determine the semantic similarity between the code segments. The longer the distance between the semantic vectors, the lower the semantic similarity, and the higher the uncertainty score;

[0026] (3-2-4) Then, select several code segments with high uncertainty scores in each category as the sampled code segments.

[0027] (4) In the validation set, the quality of the generated annotations is evaluated using the automatic evaluation indicators of neural machine translation. If the indicator value is greater than the set threshold or the labeled budget is used up, the sampling process is terminated. If the indicator value is less than or equal to the set threshold, step (5) is continued;

[0028] (5) Data sampling and labeling: using the active learning sampling strategies based on clustering and attention dispersion in step (3) above, select a specified number of code segments from the unlabeled annotation generation dataset U for manual labeling. Add the sampled data to the labeled annotation generation dataset L, and remove these data from U. The remaining forms a new unlabeled annotation generation dataset U (U = U-L). Specifically, the following steps are included:

[0029] (5-1) According to the acquisition function of the sampling strategy, score each code snippet in the unlabeled dataset U for uncertainty;

[0030] (5-2) Sort the code snippets in descending order according to the score;

[0031] (5-3) Select a specified number of high-score code snippets according to the preset conditions;

[0032] (5-4) Manually label these code snippets, i.e. manually verify the crawled code annotations by experts. If the annotation quality is low, modify the annotations, otherwise use the crawled code annotations directly;

[0033] (5-5) Add these sampled code snippets to the labeled dataset L to obtain a supervised training subset.

[0034] (6) Model fine-tuning: based on the labeled annotation generation dataset L, fine-tune the code annotation generation model, retrain the model parameters, and perform step (4);

[0035] (7) Annotation generation stage: based on the active learning method, the fine-tuned model can generate higher quality code annotations for a new target code segment under a limited manual annotation budget.

[0036] Compared with the prior art, the beneficial effects of the present application are: the present application proposes a code annotation generation method based on active learning, and discusses the application of active learning in the code annotation generation task. Specifically, first, two active learning sampling strategies suitable for this task are designed, then the supervised sample data is used to gradually improve the annotation generation model through incremental training, the sampling strategy related to the model uses the attention score of the deep learning model to obtain information, and the code segment with higher attention dispersion is selected, and the sampling strategy unrelated to the model uses the clustering idea, and selects the sample with a farther distance between the source code embedding vectors from the sample diversity angle. After verification, the data sampled by the above sampling strategy is useful for adapting the code annotation generation model. Compared with the method of using random strategy for sampling and the benchmark method of training all data, the code annotation generation method using active learning can reduce the cost required in the model training process, and generate higher quality code annotations, thereby balancing the manpower investment required to achieve a certain generation quality. Meanwhile, high-quality code annotations can also help to improve the code understanding efficiency of developers, and facilitate the development and maintenance of software. BRIEF DESCRIPTION OF DRAWINGS

[0037] The accompanying drawings are used to provide a further understanding of the present application, and constitute a part of the specification, together with the embodiments of the present application, to explain the present application, and do not constitute a limitation of the present application.

[0038] Figure 1 A system framework diagram of the code annotation generation method based on active learning provided by the present application. DETAILED DESCRIPTION

[0039] In order to make the purpose, technical scheme and advantages of the present application more clear, the present application is further described in detail below in combination with the drawings and embodiments. Of course, the specific embodiments described herein are only used to explain the present application, and do not limit the present application.

[0040] Embodiment 1

[0041] Reference Figure 1As shown, the embodiment provides a code annotation generation method based on active learning, which specifically includes the following contents:

[0042] (1) Collecting domain data sets: Collecting smart contract corpus from Etherscan.io; and mining two large-scale corpora PCSD (Python code annotation data set) and JCSD (Java code annotation data set) from GitHub, and further dividing the corpora into training set, validation set and test set, and the specific statistical information is shown in Table 1.

[0043] Table 1

[0044] corpus training set validation set test set smart contract 50000 6636 1159 PCSD 65236 21745 21745 JCSD 69708 8714 6489

[0045] (2) Pre-training general model: Pre-training a general deep learning model CodeBERT on out-of-domain labeled data sets, initializing model parameters, the model considering 6 programming languages, using natural language and programming language as its input, capturing the semantic connection between natural language and programming language, and outputting a general representation that can widely support natural language-programming language tasks, specifically including the following steps:

[0046] (2-1) Formal input sequence is: [CLS], w1, w2,..., w n , [SEP], c1, c2,..., c m , [EOS], wherein w1, w2,..., w n represents an n-word sequence in natural language, c1, c2,..., c m represents an m-token sequence in code segment.

[0047] (2-2) Training CodeBERT using mask language modeling: randomly mask 15% of the tokens in the input sequence, and then replace the selected positions with a special [MASK] token, let the model predict the original token that is masked;

[0048] (2-3) Training CodeBERT using replacement token detection: training a generator model, which is a probability model similar to n-gram to generate masked words, and then training a discriminator model to determine whether a word is an original word.

[0049] (3) Designing an active learning sampling strategy: One of the key elements of active learning is to design a reasonable sampling strategy, and the embodiment designs two different sampling strategies to adapt to the code annotation generation task:

[0050] (3-1) Attention-Dispersion-Based Model-Related Strategies: This strategy utilizes the attention scores of deep learning models to acquire information, selecting code segments with high attention dispersion. When generating target words one by one, deep models tend to focus more on the parts of the source code segment that are meaningful for the current task. If the system is translating an uncertain sample, its attention mechanism will be dispersed throughout the entire source sequence. A sample with high attention dispersion often has a more uniform attention probability distribution. Therefore, for the attention dispersion sampling strategy, the selected code segments will be those with high attention dispersion. Specifically, the following steps are included:

[0051] (3-1-1) First, extract the weight that the attention model assigns to the j-th source word when decoding the i-th target word, i.e., the attention distribution probability ω. i,j ;

[0052] (3-1-2) Secondly, according to the following formula, for each target word y i Calculate the kurtosis of the weights. It is the average of the attention weights:

[0053]

[0054] (3-1-3) Finally, the average of the negative kurtosis values ​​of all words in the target sentence is taken to obtain the attention distraction score of the sentence:

[0055]

[0056] (3-1-4) Select code segments with higher attention distraction scores, i.e., higher degree of attention distraction.

[0057] (3-2) Cluster-based model-independent strategy: Utilizing the idea of ​​clustering, and considering sample diversity, samples with large distances between source code semantic embedding vectors are selected. The specific steps include the following:

[0058] (3-2-1) First, calculate the semantic embedding vector of the source code within the domain;

[0059] (3-2-2) Next, clustering is performed. K points in the feature space are randomly set as the initial cluster centers. For each other point, the distance to the k centers is calculated. Then, based on the marked cluster centers, the new center point of each cluster is recalculated.

[0060] (3-2-3) After clustering, calculate the L2 distance between the semantic vectors of code segments in each category to determine the semantic similarity between code segments. The longer the distance between semantic vectors, the lower the semantic similarity and the higher the uncertainty score.

[0061] (3-2-4) Then select several code snippets with higher uncertainty scores in each category as sampled code snippets.

[0062] (4) Evaluate the quality of generated annotations using the neural machine translation automatic evaluation metric in the validation set, if the metric value is greater than the set threshold or the label budget is used up, terminate the sampling process, if the metric value is less than or equal to the set threshold, continue to perform step (5);

[0063] (5) Data sampling and labeling: use the active learning sampling strategy based on clustering and attention dispersion in step (3) above to select a specified number of code snippets from the unlabeled annotation generation dataset U for manual labeling, add the sampled data to the labeled annotation generation dataset L, and remove these data from U, the remaining forms a new unlabeled annotation generation dataset U (U = U-L), which includes the following steps:

[0064] (5-1) Uncertainty score each code snippet in the unlabeled dataset U according to the acquisition function of the sampling strategy;

[0065] (5-2) Sort the code snippets in descending order according to the score;

[0066] (5-3) Select the top 20% of high-score code snippets;

[0067] (5-4) Manually label these code snippets, i.e. manually verify the crawled code annotations by experts, if the annotation quality is low, modify the annotations, otherwise use the crawled code annotations directly;

[0068] (5-5) Add these sampled code snippets to the labeled dataset L to obtain a supervised training subset.

[0069] Each round collects 20% of the samples in the training set in the domain for incremental training until all data is sampled

[0070] (6) Model fine-tuning: fine-tune the code annotation generation model based on the labeled annotation generation dataset L, retrain the model parameters, and perform step (4);

[0071] (7) Annotation generation phase: the fine-tuned model based on active learning can generate higher quality code annotations for a new target code snippet under a limited manual annotation budget.

[0072] (8) The parameter settings of the active learning-based code annotation generation method are shown in Table 2:

[0073] Table 2

[0074] hyperparameters value decoder_layers 6 max_source_length 256 max_target_length 64 batch_size 64 epoch 30

[0075] (9) Take the random sampling strategy as the baseline method, and evaluate the proposed method and the baseline method on the same corpus. Four evaluation metrics from the field of neural machine translation (i.e., BLEU, METEOR, ROUGE-L, and CIDER) are used to automatically evaluate the quality of the generated annotations. Taking the smart contract corpus as an example, Table 3 shows the comparison results of the proposed active learning sampling method and the baseline method.

[0076] Table 3 Comparison results of the proposed method and the baseline method

[0077]

[0078]

[0079] Experiments show that the proposed sampling method based on clustering and attention dispersion outperforms the random sampling baseline method in all indicators in each round, that is, it can generate higher quality code annotations. Specifically, using the clustering-based method to sample 60% of the data for model training can achieve performance comparable to that of randomly sampling 80% of the data, indicating the reasonableness and effectiveness of the proposed sampling strategy. In addition, by comparing the active learning-based model with the baseline model trained using all data, it is found that applying active learning techniques to the code annotation generation task can reduce the workload required in the annotation process while improving the quality of the generated annotations, thereby balancing the manpower required to achieve a certain quality.

[0080] The above only describes the preferred embodiments of the present application and is not intended to limit the present application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. A code annotation generation method based on active learning, characterized in that, Includes the following steps: (1) Collect datasets within the domain: Discover high-quality open-source projects from GitHub and Etherscan.io, collect functions and corresponding comments in the open-source code, form an annotation-generated dataset D, and further divide it into training set and validation set. The training set is the initial unlabeled annotation-generated dataset U. (2) Pre-trained general model: Based on the out-of-domain labeled dataset, a general deep learning model CodeBERT is pre-trained and the model parameters are initialized. The model considers 6 programming languages, uses natural language and programming language as its input, captures the semantic connection between natural language and programming language, and outputs a general representation that widely supports natural language-programming language tasks. (3) Design active learning sampling strategies: sampling strategies based on attention dispersion and sampling strategies based on clustering; (4) Use neural machine translation to automatically evaluate the quality of the generated annotations in the validation set. If the value of the indicator is greater than the set threshold or the label budget is exhausted, the sampling process is terminated. If the value of the indicator is less than or equal to the set threshold, step (5) is continued. (5) Data sampling and labeling: Using the clustering-based and attention-based active learning sampling strategy in step (3) above, select a specified number of code segments from the unlabeled annotation-generated dataset U for manual labeling, add the sampled data to the labeled annotation-generated dataset L, and remove these data from U. The remaining data form a new unlabeled annotation-generated dataset U (U = UL). (6) Model fine-tuning: Based on the labeled annotation-generated dataset L, fine-tune the code annotation generation model, retrain the model parameters, and execute step (4); (7) Annotation generation stage: The model finely tuned based on the active learning method can generate higher quality code annotations for a new target code segment under a limited manual annotation budget.

2. The code annotation generation method based on active learning according to claim 1, characterized in that, Step (2) involves pre-training a general model on an out-of-domain labeled dataset, which includes the following steps: (2-1) Modeling and training CodeBERT using masked language: Randomly select a set of positions for natural language and programming language to mask, and then replace the selected positions with special [MASK] tags. The goal is to predict the original masked tags. (2-2) Using replacement token detection to train CodeBERT: Train a generator model, which is a probabilistic model similar to an n-gram, to generate masked words, and then train a discriminator model to determine whether a word is an original word.

3. The code annotation generation method based on active learning according to claim 1, characterized in that, The design of the clustering-based and attention-dispersion-based active learning sampling strategy in step (3) includes the following steps: (3-1) Attention-Dispersion-Based Model-Related Strategies: This strategy utilizes the attention scores of deep learning models to acquire information, selecting code segments with high attention dispersion. When generating target words one by one, the deep model focuses on the parts of the source code segment that are meaningful for the current task. If the system is translating an uncertain sample, its attention mechanism will be dispersed throughout the entire source sequence. A sample with high attention dispersion has a uniform attention probability distribution. For the attention dispersion sampling strategy, the selected code segments will be those with high attention dispersion, including the following steps: (3-1-1) First, extract the weight that the attention model assigns to the j-th source word when decoding the i-th target word, i.e., the attention distribution probability ω. i,j ; (3-1-2) Secondly, according to the following formula, for each target word y i Calculate the kurtosis of the weights. It is the average of the attention weights: (3-1-3) Finally, the average of the negative kurtosis values ​​of all words in the target sentence is taken to obtain the attention distraction score of the sentence: (3-1-4) Select code segments with higher attention distraction scores, i.e., higher degree of attention distraction; (3-2) Cluster-based model-independent strategy: Utilizing the idea of ​​clustering, and considering sample diversity, samples with large distances between source code semantic embedding vectors are selected. This includes the following steps: (3-2-1) First, calculate the semantic embedding vector of the source code within the domain; (3-2-2) Next, clustering is performed. K points in the feature space are randomly set as the initial cluster centers. For each other point, the distance to the k centers is calculated. Then, based on the marked cluster centers, the new center point of each cluster is recalculated. (3-2-3) After clustering, calculate the L2 distance between the semantic vectors of code segments in each category to determine the semantic similarity between code segments. The longer the distance between semantic vectors, the lower the semantic similarity and the higher the uncertainty score. (3-2-4) Then, select several code segments with higher uncertainty scores in each category as the sampled code segments.

4. The code annotation generation method based on active learning according to claim 1, characterized in that, The data sampling and labeling stage in step (5) includes the following steps: (4-1) Perform uncertainty scoring on each code snippet in the unlabeled dataset U according to the acquisition function of the sampling strategy; (4-2) Sort the code snippets in descending order based on the scores; (4-3) Select a specified number of high-scoring code snippets according to preset conditions; (4-4) Manually mark these code snippets, that is, manually verify the comments of the crawled code by experts. If the comment quality is low, modify the comment; otherwise, use the crawled code comments directly. (4-5) Add these sampled code snippets to the labeled dataset L to obtain a training subset worthy of supervision.

Citation Information

Patent Citations

  • Decision-based code annotation generation method fusing information retrieval and deep learning

    CN113743064A

  • Bash code annotation generation method based on dual information retrieval

    CN113961237A