Reordering model training method and apparatus, computer device, and storage medium
Patent Information
- Application Number
- CN202611041095.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-13
- Publication Date
- 2026-10-23
AI Technical Summary
[0003]然而,现有重排序模型缺乏对候选代码本身质量的感知能力,导致排序结果难以有效区分不同质量的候选代码
[0038]与相关技术相比,在本实施例中提供的重排序模型训练方法、装置、计算机设备和存储介质,其中,该方法包括:获取每个预设的代码质量评估维度对应的指南集合;其中,指南集合包括多个预设指南,且多个预设指南用于表征不同质量代码的生成策略;针对每个预设查询语句,根据每个代码质量评估维度对应的指南集合中的每个预设指南、以及预设查询语句,生成每个代码质量评估维度下的每个预设指南对应的样本代码;根据各个代码质量评估维度下的各个预设指南对应的样本代码,构建预设查询语句对应的训练数据集,并根据各个预设查询语句对应的训练数据集对基础重排序模型进行训练,得到目标重排序模型;其中,目标重排序模型用于代码检索结果的重排序处理。
Smart Images

Figure CN122964585A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of code retrieval technology, and in particular to a reordering model training method, apparatus, computer device, and storage medium. Background Technology
[0002] Code retrieval refers to retrieving code snippets from a large-scale codebase that match a user's query intent. With the development of deep learning technology, re-ranking models are widely used in code retrieval systems to refine the ranking of candidate code in the initial recall results, thereby improving the accuracy of retrieval results and user experience.
[0003] However, existing re-ranking models lack the ability to perceive the quality of the candidate code itself, making it difficult for the ranking results to effectively distinguish candidate codes of different qualities.
[0004] There is currently no effective solution to the problem that re-ranking models in related technologies lack the ability to perceive the quality of candidate code itself, making it difficult to effectively distinguish candidate code of different quality in the ranking results. Summary of the Invention
[0005] This embodiment provides a re-ranking model training method, apparatus, computer device, and storage medium, which aims to enable the re-ranking model to perceive the quality of the candidate code itself, so that the ranking results can effectively distinguish candidate codes of different quality.
[0006] Firstly, this embodiment provides a reordering model training method, which includes:
[0007] Obtain a set of guidelines corresponding to each preset code quality assessment dimension; wherein, the set of guidelines includes multiple preset guidelines, and the multiple preset guidelines are used to characterize the generation strategies of different quality codes;
[0008] For each preset query statement, based on each preset guide in the guide set corresponding to each code quality assessment dimension and the preset query statement, sample code corresponding to each preset guide under each code quality assessment dimension is generated;
[0009] Based on the sample code corresponding to each preset guideline under each of the code quality assessment dimensions, a training dataset corresponding to the preset query statement is constructed, and the basic re-ranking model is trained based on the training dataset corresponding to each preset query statement to obtain the target re-ranking model; wherein, the target re-ranking model is used for re-ranking processing of code retrieval results.
[0010] In some embodiments, the plurality of preset guidelines include a first preset guideline for characterizing high-quality code generation strategies and a second preset guideline for characterizing low-quality code generation strategies; the sample code includes a first sample code corresponding to the first preset guideline and a second sample code corresponding to the second preset guideline;
[0011] The step of constructing a training dataset corresponding to the preset query statement based on sample code corresponding to each preset guideline under each of the aforementioned code quality assessment dimensions includes:
[0012] For each code quality assessment dimension, sample code corresponding to each preset guideline under the code quality assessment dimension is labeled to obtain a label for the first sample code and a label for the second sample code. The label for the first sample code characterizes its semantic relevance to the preset query statement and its first quality performance on the code quality assessment dimension. The label for the second sample code characterizes its semantic relevance to the query statement and its second quality performance on the code quality assessment dimension. The first quality performance is superior to the second quality performance.
[0013] Obtain a third sample code that is not related to the query semantics, and generate a label for the third sample code; wherein, the label of the third sample code is used to characterize that the third sample code is not related to the query semantics;
[0014] The priority of each sample code is determined based on the tags of the first sample code, the second sample code, and the third sample code; wherein the priority of the first sample code is higher than that of the second sample code, and the priority of the second sample code is higher than that of the third sample code.
[0015] According to the priority, ordered pairs are constructed for the first sample code, the second sample code, and the third sample code to obtain an ordered pair set. The training dataset is constructed according to the ordered pair set corresponding to each of the code quality evaluation dimensions. The ordered pair set includes multiple ordered pairs, and each ordered pair contains two sample codes with different priorities.
[0016] In some embodiments, training the basic re-ranking model based on the training dataset corresponding to each of the preset query statements to obtain the target re-ranking model includes:
[0017] Based on the model prediction score corresponding to each sample code in each ordered pair, a first ranking loss function corresponding to the basic re-ranking model is constructed; wherein, the model prediction score is the prediction score of the basic re-ranking model for each sample code in each ordered pair;
[0018] Based on the first ranking loss function, construct the target ranking loss function corresponding to the basic re-ranking model;
[0019] The basic re-ranking model is trained based on the training dataset corresponding to each preset query statement and the target ranking loss function to obtain the target re-ranking model.
[0020] In some embodiments, constructing the target ranking loss function corresponding to the basic re-ranking model based on the first ranking loss function includes:
[0021] A dynamic weight term is constructed based on a preset ranking evaluation index; wherein, the dynamic weight term is used to characterize the change in the preset ranking evaluation index after the two sample codes in the ordered pair are swapped in the current predicted ranking, and the current predicted ranking is obtained by sorting the predicted scores of each sample code from high to low based on the basic re-ranking model.
[0022] The target ranking loss function is constructed based on the dynamic weight term and the first ranking loss function.
[0023] In some embodiments, the preset ranking evaluation index is normalized cumulative gain; the step of determining dynamic weight terms based on the preset ranking evaluation index includes:
[0024] The first difference term is determined based on the label of each sample code in the ordered pair;
[0025] The second difference term is determined based on the sorting position of each sample code in the ordered pair in the current predicted sorting;
[0026] The dynamic weight term is determined based on the first difference term, the second difference term, and the maximum cumulative gain under ideal sorting.
[0027] In some embodiments, the preset ranking evaluation index is the average regression ranking; the step of determining the dynamic weight item based on the preset ranking evaluation index includes:
[0028] Based on the label of each sample code in the ordered pair, determine the indicator function value corresponding to the ordered pair;
[0029] The third difference term is determined based on the sorting position of each sample code in the ordered pair in the current predicted sorting;
[0030] The dynamic weight term is determined based on the indicator function value and the third difference term.
[0031] In some embodiments, the preset code quality assessment dimensions include at least one of code execution speed, code memory usage, and code maintainability.
[0032] Secondly, this embodiment provides a reordering model training device, which includes:
[0033] The acquisition module is used to acquire a set of guidelines corresponding to each preset code quality assessment dimension; wherein, the set of guidelines includes multiple preset guidelines, and the multiple preset guidelines are used to characterize the generation strategies of different quality codes;
[0034] The generation module is used to generate sample code corresponding to each preset guide under each preset guide in the guide set corresponding to each preset code quality assessment dimension, and the preset query statement for each preset query statement.
[0035] The training module is used to construct a training dataset corresponding to the preset query statement based on the sample code corresponding to each preset guideline under each of the code quality assessment dimensions, and to train the basic re-ranking model based on the training dataset corresponding to each preset query statement to obtain the target re-ranking model; wherein, the target re-ranking model is used for re-ranking processing of code retrieval results.
[0036] Thirdly, this embodiment provides a computer device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the reordering model training method described in the first aspect above.
[0037] Fourthly, this embodiment provides a storage medium storing a computer program that, when executed by a processor, implements the reordering model training method described in the first aspect above.
[0038] Compared with related technologies, the reordering model training method, apparatus, computer device, and storage medium provided in this embodiment include the following: The method includes: obtaining a set of guidelines corresponding to each preset code quality assessment dimension; wherein the set of guidelines includes multiple preset guidelines, and the multiple preset guidelines are used to characterize generation strategies for different quality codes; for each preset query statement, generating sample code corresponding to each preset guideline under each code quality assessment dimension based on each preset guideline in the set of guidelines corresponding to each code quality assessment dimension and the preset query statement; constructing a training dataset corresponding to each preset query statement based on the sample code corresponding to each preset guideline under each code quality assessment dimension, and training a basic reordering model based on the training dataset corresponding to each preset query statement to obtain a target reordering model; wherein the target reordering model is used for reordering code retrieval results.
[0039] In this way, by using the set of guidelines corresponding to the preset code quality assessment dimensions, sample code with code quality differences is generated, enabling the trained target re-ranking model to have the ability to perceive the quality of candidate code. Thus, when re-ranking the code retrieval results, it can effectively distinguish candidate code based on code quality and optimize the rationality of the ranking results.
[0040] Details of one or more embodiments of this application are set forth in the following drawings and description to make other features, objects and advantages of this application more readily apparent. Attached Figure Description
[0041] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0042] Figure 1 This is a flowchart of an embodiment of the reordering model training method provided in this application;
[0043] Figure 2 This is a flowchart of Embodiment 2 of the reordering model training method provided in this application;
[0044] Figure 3 This is a flowchart of Embodiment 3 of the reordering model training method provided in this application;
[0045] Figure 4 This is a flowchart of Embodiment 4 of the reordering model training method provided in this application;
[0046] Figure 5 This is a flowchart of Embodiment 5 of the reordering model training method provided in this application;
[0047] Figure 6This is a flowchart of Embodiment Six of the reordering model training method provided in this application;
[0048] Figure 7 This is a flowchart of Embodiment Seven of the Reordering Model Training Method provided in this application;
[0049] Figure 8 This is a structural block diagram of the reordering model training device provided in this application. Detailed Implementation
[0050] To better understand the purpose, technical solution, and advantages of this application, the application is described and illustrated below in conjunction with the accompanying drawings and embodiments.
[0051] Unless otherwise defined, the technical or scientific terms used in this application shall have the general meaning understood by one of ordinary skill in the art to which this application pertains. Words such as “a,” “an,” “an,” “the,” “the,” and “these” used in this application do not indicate quantitative limitation and may be singular or plural. The terms “comprising,” “including,” “having,” and any variations thereof used in this application are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or device that comprises a series of steps or modules (units) is not limited to the listed steps or modules (units) but may include steps or modules (units) not listed, or may include other steps or modules (units) inherent to these processes, methods, products, or devices. Words such as “connected,” “linked,” and “coupled” used in this application are not limited to physical or mechanical connections but may include electrical connections, whether direct or indirect. “Multiple” used in this application refers to two or more. “And / or” describes the relationship between related objects, indicating that three relationships may exist; for example, “A and / or B” can represent: A alone, A and B simultaneously, and B alone. Normally, the character " / " indicates that the objects before and after it are in an "or" relationship. The terms "first," "second," "third," etc., used in this application are merely to distinguish similar objects and do not represent a specific order of objects.
[0052] The following specific embodiments are given to illustrate the technical solution of this application in detail.
[0053] Figure 1 This is a flowchart of an embodiment of the reordering model training method provided in this application. Please refer to... Figure 1 The method provided in this embodiment may include the following steps:
[0054] Step S101: Obtain the guide set corresponding to each preset code quality assessment dimension; wherein, the guide set includes multiple preset guides, and the multiple preset guides are used to characterize the generation strategies of different quality codes.
[0055] It should be noted that the guide set is a pre-built set of text instructions for different code quality assessment dimensions. Each guide set contains multiple preset guides, which are used to guide the large language model to generate sample code related to a specific code quality assessment dimension.
[0056] In this embodiment, code quality assessment dimensions include, but are not limited to, code execution speed, code memory usage, and code maintainability. Specifically, code execution speed characterizes the code's running efficiency, code memory usage characterizes the memory consumption during code execution, and code maintainability characterizes the code's readability, modifiability, and extensibility.
[0057] For example, in one embodiment, for each code quality assessment dimension, the corresponding set of guidelines includes K first preset guidelines and K second preset guidelines. The first preset guidelines are used to guide the large language model to generate high-quality code, and their specific content may include optimization strategies for algorithm complexity, specifications for code structure clarity, etc. The second preset guidelines are used to guide the large language model to generate low-quality code, and their specific content may include frequent creation of temporary objects, use of complex nested structures, etc.
[0058] More specifically, regarding the dimension of code execution speed, the first preset guideline may include strategies such as avoiding repeated calculations in loops to guide large language models to generate faster-executing code, while the second preset guideline may include strategies such as using nested loops to implement algorithms to guide large language models to generate slower-executing code.
[0059] It is understood that the above-described method of distinguishing between high-quality and low-quality generation strategies through the first and second preset guidelines is merely an exemplary implementation. In other embodiments, more preset guidelines for quality levels can be set according to actual needs, or other methods can be used to control the generation of different quality codes. This application does not impose any specific limitations on this.
[0060] Step S102: For each preset query statement, generate sample code corresponding to each preset guide in the guide set corresponding to each code quality assessment dimension, and the preset query statement.
[0061] For example, for each preset query (e.g., a natural language query), preset guidelines are sampled from the guideline set corresponding to each code quality assessment dimension. The sampled preset guidelines are combined with the preset query to form structured prompts. These structured prompts are then input into a large language model to generate corresponding sample code. In this way, sample code corresponding to each preset guideline under each code quality assessment dimension is obtained.
[0062] It should be noted that the sampling method can be random sampling or ordered sampling based on preset rules; this embodiment does not specifically limit this. Furthermore, since the guide set corresponding to each code quality assessment dimension includes K first preset guidelines and K second preset guidelines, the total number of sample codes corresponding to each code quality assessment dimension does not exceed 2K.
[0063] In practice, when combining a preset query statement with a sampled first preset guideline, the large language model outputs the first sample code that performs well in this code quality assessment dimension. When combining a preset query statement with a sampled second preset guideline, the large language model outputs the second sample code that performs poorly in this code quality assessment dimension.
[0064] It should be understood that the above-described code generation method based on a large language model is merely an exemplary implementation. Other embodiments, such as generating corresponding sample code from a predefined code template according to an encoding strategy in a preset guide, are not specifically limited here.
[0065] Furthermore, after the large language model generates sample code, the generated sample code can be filtered. For example, invalid outputs (such as malformed code) and duplicate code variations from the large language model output can be removed, retaining valid sample code that can be compiled correctly and has diverse content. For each preset query statement, if the number of independent code variations among the remaining valid sample code after the above filtering process is less than the preset number (e.g., 3), then the query statement and all its corresponding sample code are deleted to ensure that each query statement retains a sufficient amount of diverse comparative data for subsequent model training.
[0066] Step S103: Based on the sample code corresponding to each preset guideline under each code quality assessment dimension, construct the training dataset corresponding to the preset query statement, and train the basic re-ranking model based on the training dataset corresponding to each preset query statement to obtain the target re-ranking model; wherein, the target re-ranking model is used for the re-ranking processing of code retrieval results.
[0067] In this step, for each preset query statement, training data pairs containing quality comparisons are constructed based on the first sample code generated using the first preset guideline and the sample code generated using the second preset guideline. Subsequently, the basic re-ranking model is trained using the training datasets corresponding to each preset query statement to optimize its parameters, resulting in a target re-ranking model. This target re-ranking model is used to re-rank the candidate code in the candidate code list recalled by the code retrieval system in the initial retrieval phase, prioritizing higher-quality candidate code and avoiding the inclusion of functionally correct but low-quality code snippets (e.g., low execution efficiency, high memory consumption, numerous code smells, etc.) in the final code retrieval results. Specifically, the trained target re-ranking model is deployed to the re-ranking phase to perform refined quality-aware ranking on the candidate code set initially returned by the dual encoder, outputting the final ranking results.
[0068] It should be noted that when there are multiple code quality assessment dimensions, the training data corresponding to each code quality assessment dimension can be used together to train the same re-ranking model, or different re-ranking models can be trained for different code quality assessment dimensions. The specific choice can be made according to the actual application scenario, and this embodiment does not make specific limitations on this.
[0069] The reordering model training method provided in this embodiment obtains a set of guidelines corresponding to each preset code quality assessment dimension. The guide set includes multiple preset guidelines, each representing a generation strategy for code of different quality. For each preset query, sample code corresponding to each preset guide under each code quality assessment dimension is generated based on each preset guide in the guide set corresponding to each code quality assessment dimension and the preset query. A training dataset corresponding to each preset query is constructed based on the sample code corresponding to each preset guide under each code quality assessment dimension. The basic reordering model is then trained using the training dataset corresponding to each preset query to obtain the target reordering model. The target reordering model is used for reordering the code retrieval results.
[0070] In this way, by using the set of guidelines corresponding to the preset code quality assessment dimensions, sample code with code quality differences is generated, enabling the trained target re-ranking model to have the ability to perceive the quality of candidate code. Thus, when re-ranking the code retrieval results, it can effectively distinguish candidate code based on code quality and optimize the rationality of the ranking results.
[0071] Figure 2 The flowchart for Embodiment 2 of the reordering model training method provided in this application is shown below. Please refer to... Figure 2Based on the above embodiments, multiple preset guidelines include a first preset guideline for characterizing high-quality code generation strategies and a second preset guideline for characterizing low-quality code generation strategies; the sample code includes a first sample code corresponding to the first preset guideline and a second sample code corresponding to the second preset guideline; based on the sample code corresponding to each preset guideline under each code quality assessment dimension, a training dataset corresponding to a preset query statement is constructed, including:
[0072] Step S201: For each code quality assessment dimension, the sample code corresponding to each preset guideline under the code quality assessment dimension is labeled to obtain the label of the first sample code and the label of the second sample code; wherein, the label of the first sample code is used to characterize the semantic relevance of the first sample code to the preset query statement and the first quality performance of the first sample code in the code quality assessment dimension; the label of the second sample code characterizes the semantic relevance of the second sample code to the query and the second quality performance of the second sample code in the code quality assessment dimension; the first quality performance is better than the second quality performance.
[0073] Specifically, for each code quality assessment dimension, the first sample code and the second sample code are labeled separately. The first sample code is the sample code generated using the first preset guideline, and it is a positive sample; the second sample code is the sample code generated using the second preset guideline, and the first sample code is a negative sample.
[0074] It should be noted that the label for the first sample code is used to characterize the semantic relevance of the first sample code to the preset query statement, and the first quality performance of the first sample code on this code quality evaluation dimension. The label for the second sample code is used to characterize the semantic relevance of the second sample code to the preset query statement, and the second quality performance of the second sample code on this code quality evaluation dimension. Furthermore, the first quality performance is superior to the second quality performance.
[0075] For example, the first quality performance corresponds to label 2, which indicates that the first sample code is semantically relevant to the query and that the first sample code is of excellent quality. The first sample code can be denoted as... The second quality performance corresponds to label 1, which indicates that the second sample code is semantically relevant to the query and of poor quality. The second sample code can be denoted as... .
[0076] Step S202: Obtain a third sample code that is not related to the query semantics and generate a label for the third sample code; wherein, the label of the third sample code is used to characterize that the third sample code is not related to the query semantics.
[0077] It should be noted that the third sample code is used to provide semantically irrelevant negative samples to the model during training, enabling the model to distinguish between semantically relevant and semantically irrelevant code. Furthermore, the third sample code can be obtained by randomly sampling code fragments from the code library, selecting code that is semantically unrelated to the preset query statement from sample codes corresponding to other preset query statements, or other code that is semantically unrelated to the preset query statement. This embodiment does not impose specific limitations on this.
[0078] For example, the third sample code corresponds to label 0, which indicates that the third sample code is not related to the query semantics of the preset query statement. The third sample code can be denoted as... .
[0079] Step S203: Determine the priority of each sample code based on the label of the first sample code, the label of the second sample code, and the label of the third sample code; wherein the priority of the first sample code is higher than that of the second sample code, and the priority of the second sample code is higher than that of the third sample code.
[0080] Specifically, based on the labels of each first sample code, each second sample code, and each third sample code, priority sorting is performed on each first sample code, each second sample code, and each third sample code to determine the priority order among them.
[0081] The first sample code has a higher priority than the second sample code, and the second sample code has a higher priority than the third sample code; that is, the priority order is as follows: (Label 2) > (Label 1) > (Label 0).
[0082] Step S204: Based on priority, construct ordered pairs for the first sample code, the second sample code, and the third sample code to obtain an ordered pair set. Then, construct the training dataset based on the ordered pair set corresponding to each code quality evaluation dimension. The ordered pair set includes multiple ordered pairs, and each ordered pair contains two sample codes with different priorities.
[0083] Specifically, based on priority order, ordered pairs are constructed for each sample code (including the first, second, and third sample codes) corresponding to each preset query statement, to build an ordered pair set covering all partial order relations. Where i and j represent two different sample codes under the same preset query statement, and Let i and j represent the labels of sample code i and j, respectively. Each ordered pair in this set represents a training sample pair, used to indicate to the reordering model that the predicted score of sample code i should be placed before that of sample code j during the learning process.
[0084] It should be noted that the ordered set constructed in this step covers the following three partial order relations: (1) the priority of the first sample code is higher than that of the second sample code, that is, high-quality related code is better than low-quality related code; (2) the priority of the first sample code is higher than that of the third sample code, that is, high-quality related code is better than irrelevant code; (3) the priority of the second sample code is higher than that of the third sample code, that is, low-quality related code is better than irrelevant code.
[0085] It is understandable that the above three partial order relations cover the relative ranking relationship between all sample codes of different priorities under the same query, ensuring that the model can learn the ranking strategy of prioritizing quality over low quality and relevance over irrelevance during the training process.
[0086] Subsequently, a training dataset is constructed based on the ordered pairs corresponding to each code quality assessment dimension. That is, the ordered pairs of each preset query statement under each code quality assessment dimension together constitute the training dataset corresponding to that query.
[0087] The method provided in this embodiment annotates sample codes by constructing a three-level labeling system and determines the priority order of sample codes based on the labels. On this basis, it constructs an ordered pair set covering all partial order relations, so that the trained reordering model can simultaneously learn the ranking strategy of prioritizing quality over low quality and relevance over irrelevance.
[0088] Figure 3 The flowchart for Embodiment 3 of the reordering model training method provided in this application is shown below. Please refer to... Figure 3 Based on the above embodiments, the basic re-ranking model is trained using the training dataset corresponding to each preset query statement to obtain the target re-ranking model, including:
[0089] Step S301: Construct the first ranking loss function corresponding to the basic re-ranking model based on the model prediction score corresponding to each sample code in each ordered pair; wherein, the model prediction score is the prediction score of the basic re-ranking model for each sample code in each ordered pair.
[0090] It should be noted that for any ordered pair (i, j), the base reordering model outputs a predicted score s for sample code i. i Output the predicted score s for sample code j. jThe prediction score measures the degree of match between the sample code and the preset query statement; a higher score indicates that the model believes the sample code should be ranked higher.
[0091] In this embodiment, a first ranking loss function is constructed based on the model's predicted score corresponding to each sample code in each ordered pair. The core idea of this first ranking loss function is that a loss is incurred if the order of scores predicted by the model is inconsistent with the priority order.
[0092] For example, the specific formula for the first ranking loss function is as follows:
[0093] (1)
[0094] In equation (1), Let S denote the first sorting loss function; S denotes the set of ordered pairs. , denoted as and respectively, representing the prediction scores of the basic reordering model for the two sample codes in the ordered pair (i, j).
[0095] Step S302: Based on the first ranking loss function, construct the target ranking loss function corresponding to the basic re-ranking model.
[0096] In this step, the first ranking loss function is used as the target ranking loss function for the base re-ranking model, and is then used for subsequent model training. It should be understood that the first ranking loss function reflects the difference between the model's predicted score and the true priority order; by minimizing this loss function, the model can learn the correct ranking ability.
[0097] Step S303: Train the basic re-ranking model based on the training dataset corresponding to each preset query statement and the target ranking loss function to obtain the target re-ranking model.
[0098] Specifically, with minimizing the target ranking loss function as the optimization objective, the basic re-ranking model is iteratively trained using training datasets corresponding to each preset query statement. In each iteration, sample codes from the training dataset are input into the basic re-ranking model for forward propagation to obtain the prediction score for each sample code. The loss value is calculated based on the target ranking loss function, and the parameters of the basic re-ranking model are updated using the backpropagation algorithm. When the preset training termination conditions are met (such as reaching the maximum number of iterations or the loss value tending to converge), the trained basic re-ranking model is used as the target re-ranking model.
[0099] It should be noted that this target re-ranking model is used for re-ranking code retrieval results. During the inference phase, the target re-ranking model receives a list of candidate codes recalled by the code retrieval system, calculates a prediction score for each candidate code in the list relative to a preset query statement, and then re-ranks the candidate code list according to the prediction scores from highest to lowest.
[0100] The method provided in this embodiment constructs a ranking loss function based on ordered pairs, enabling the basic re-ranking model to learn the relative ranking relationships between sample codes, thereby obtaining the target re-ranking model. Furthermore, the target re-ranking model trained in this embodiment possesses the ability to perceive the quality of candidate codes during the re-ranking process of code retrieval results, effectively improving the rationality of the ranking results.
[0101] Figure 4 The flowchart for Embodiment 4 of the reordering model training method provided in this application is shown below. Please refer to... Figure 4 Based on the above embodiments, according to the first ranking loss function, a target ranking loss function corresponding to the basic re-ranking model is constructed, including:
[0102] Step S401: Construct a dynamic weight term based on the preset ranking evaluation index; wherein, the dynamic weight term is used to characterize the change in the preset ranking evaluation index after the two sample codes in the ordered pair are swapped in the current predicted ranking, and the current predicted ranking is obtained by sorting the predicted scores of each sample code from high to low based on the basic re-ranking model.
[0103] In this embodiment, the current prediction ranking refers to the ranking list obtained by inputting sample codes into the basic re-ranking model, outputting the prediction score of each sample code, and sorting them from highest to lowest prediction score. In this prediction ranking, each sample code has a corresponding ranking position.
[0104] It should be noted that for any ordered pair (i, j), if sample code i has a higher priority than sample code j in the ordered pair, but in the current predicted ranking, sample code i may be ranked after sample code j (i.e., the model ranks it incorrectly), or sample code i may be ranked before sample code j (i.e., the model ranks it correctly). The dynamic weight term is used to characterize the change in the preset ranking evaluation index if the ranking positions of sample code i and sample code j are swapped in the current predicted ranking. The larger the change, the more serious the damage to the ranking quality caused by the error of the ordered pair in the ranking list, and the greater the penalty weight should be applied to the ordered pair during training. Conversely, if the change is small, a smaller weight is assigned.
[0105] The preset ranking evaluation index can be normalized loss cumulative gain (nDCG), mean reciprocal ranking (MRR), or other evaluation indexes that can measure the ranking quality. This embodiment does not make specific limitations on this.
[0106] Therefore, by introducing dynamic weights, the reordering model can adaptively adjust the weight coefficients of each ordered pair in the loss function according to the position of the sample code in the sorted list during training, thereby guiding the model to pay more attention to sorting errors that have a greater impact on sorting quality.
[0107] Step S402: Construct the target ranking loss function based on the dynamic weight term and the first ranking loss function.
[0108] Specifically, the target ranking loss function is obtained by accumulating the corresponding dynamic weights assigned to the loss terms of each ordered pair in the first ranking loss function. For ordered pairs with higher priority (i.e., ordered pairs whose ranking evaluation metrics change significantly after swapping positions), the corresponding loss terms in the target ranking loss function have larger weights, and the model will pay more attention to correcting the ranking errors of such ordered pairs during training; for ordered pairs with smaller changes, the corresponding loss terms have smaller weights.
[0109] The method provided in this embodiment introduces a dynamic weight term based on the ranking evaluation index, which enables the target ranking loss function to adaptively adjust the penalty weight of each ordered pair according to the position of the sample code in the predicted ranking. This guides the model to apply greater penalty weight to ranking errors that have a greater impact on the ranking evaluation index, thereby further optimizing the re-ranking quality of the code retrieval results.
[0110] Figure 5 The flowchart for Embodiment 5 of the reordering model training method provided in this application is shown below. Please refer to... Figure 5 Based on the above embodiments, the preset ranking evaluation index is the normalized cumulative gain; the dynamic weight term is determined according to the preset ranking evaluation index, including:
[0111] Step S501: Determine the first difference term based on the label of each sample code in the ordered pair.
[0112] Specifically, for any ordered pair (i, j), obtain the labels corresponding to the two sample codes in the ordered pair. The label for sample code i is rel. i The tag for sample code j is rel. j Then, the first difference term is calculated based on the two labels.
[0113] Step S502: Determine the second difference term based on the sorting position of each sample code in the ordered pair in the current predicted sorting.
[0114] Specifically, the ranking of two sample codes in the ordered pair is obtained in the current predicted ranking. The current predicted ranking is obtained by sorting the sample codes from highest to lowest based on the prediction scores of the base re-ranking model. The ranking position of sample code i is denoted as rank. i The sorting position of the sample code is denoted as rank. j Subsequently, a second difference term is calculated based on the ranking positions of the two sample codes in the current predicted ranking.
[0115] It should be noted that when two sample codes are far apart in the ranking list (e.g., in the 1st and 10th positions respectively), swapping their positions will have a significant impact on the ranking evaluation index; when two sample codes are adjacent (e.g., in the 5th and 6th positions respectively), the impact of swapping their positions is relatively small.
[0116] Step S503: Determine the dynamic weight term based on the first difference term, the second difference term, and the maximum cumulative gain under ideal sorting.
[0117] In this embodiment, the specific formula for the dynamic weight term is as follows:
[0118] (2)
[0119] In equation (2), Indicates a dynamic weight term; and represent the labels of sample code i and sample code j, respectively; IDCG represents the maximum cumulative gain under ideal sorting. and These represent the sorting positions of sample code i and sample code j, respectively.
[0120] At this point, the specific formula for the target ranking loss function is as follows:
[0121] (3)
[0122] In equation (3), Represents the target ranking loss function; S represents the dynamic weight term; S represents the ordered set of pairs. , denoted as and respectively, representing the prediction scores of the basic reordering model for the two sample codes in the ordered pair (i, j).
[0123] The method provided in this embodiment determines a dynamic weight term by separately determining a first difference term and a second difference term, and combining this with the maximum cumulative gain of the loss under ideal ranking, thus realizing dynamic weight calculation based on the nDCG index. In this way, by introducing the aforementioned dynamic weight term into the target ranking loss function, the re-ranking model can adaptively adjust the optimization intensity of each ordered pair according to the importance of its ranking position during training, applying greater penalty weights to ranking errors that have a significant impact on the ranking evaluation index; that is, imposing greater penalties on critical errors that rank higher during training.
[0124] Figure 6 The flowchart for Embodiment Six of the reordering model training method provided in this application is shown below. Please refer to... Figure 6 Based on the above embodiments, the preset ranking evaluation index is the average reciprocal ranking; dynamic weight items are determined according to the preset ranking evaluation index, including:
[0125] Step S601: Determine the indicator function value corresponding to the ordered pair based on the label of each sample code in the ordered pair.
[0126] Specifically, for any ordered pair (i, j), the labels corresponding to the two sample codes in the ordered pair are obtained, and the indicator function value is determined based on the comparison result of the two labels. Here, the label of sample code i is rel. i The tag for sample code j is rel. j Indicator functions This is used to determine whether the true priority of sample code i is higher than that of sample code j, that is, to determine whether the ordered pair satisfies the preset partial order relation. When When the value is 1, the indicator function takes the value 1; otherwise, it takes the value 0.
[0127] Step S602: Determine the third difference term based on the sorting position of each sample code in the ordered pair in the current predicted sorting.
[0128] Specifically, the ranking of two sample codes in the ordered pair is obtained in the current predicted ranking. The current predicted ranking is obtained by sorting the sample codes from highest to lowest based on the prediction scores of the base re-ranking model. The ranking position of sample code i is denoted as rank. i The sorting position of the sample code is denoted as rank. j Subsequently, a third difference term is calculated based on the ranking positions of the two sample codes in the current predicted ranking.
[0129] Step S603: Determine the dynamic weight term based on the indicator function value and the third difference term.
[0130] In this embodiment, the specific formula for the dynamic weight term is as follows:
[0131] (4)
[0132] In equation (4), Indicates a dynamic weight term; and These represent the labels for sample code i and sample code j, respectively. Indicates an indicator function; and These represent the sorting positions of sample code i and sample code j, respectively.
[0133] At this point, the specific formula for the target ranking loss function is as follows:
[0134] (5)
[0135] In equation (5), Represents the target ranking loss function; S represents the dynamic weight term; S represents the ordered set of pairs. , denoted as and respectively, representing the prediction scores of the basic reordering model for the two sample codes in the ordered pair (i, j).
[0136] The method provided in this embodiment determines dynamic weight terms based on the indicator function value and the third difference term, thereby realizing dynamic weight calculation based on the MRR index. This allows the dynamic weight terms to accurately reflect the influence of different ranking positions on the MRR index, thus helping to improve the ranking accuracy of the re-ranking model.
[0137] The present embodiment will now be described and illustrated through preferred embodiments.
[0138] Figure 7 The flowchart of Embodiment Seven of the reordering model training method provided in this application is as follows: Figure 7 As shown, the training method for this reordering model includes the following steps:
[0139] Step S701: Obtain the guide set corresponding to each preset code quality assessment dimension; wherein, the guide set includes multiple preset guides, and the multiple preset guides include a first preset guide for characterizing high-quality code generation strategies and a second preset guide for characterizing low-quality code generation strategies.
[0140] Step S702: For each preset query statement, generate sample code corresponding to each preset guide in the guide set corresponding to each code quality assessment dimension, and the preset query statement.
[0141] Step S703: For each code quality assessment dimension, the sample code corresponding to each preset guideline under the code quality assessment dimension is labeled to obtain the label of the first sample code and the label of the second sample code; wherein, the label of the first sample code is used to characterize the semantic relevance of the first sample code to the preset query statement and the first quality performance of the first sample code in the code quality assessment dimension; the label of the second sample code characterizes the semantic relevance of the second sample code to the query and the second quality performance of the second sample code in the code quality assessment dimension; the first quality performance is better than the second quality performance;
[0142] Step S704: Obtain a third sample code that is not related to the query semantics and generate a label for the third sample code; wherein, the label of the third sample code is used to characterize that the third sample code is not related to the query semantics.
[0143] Step S705: Determine the priority of each sample code based on the label of the first sample code, the label of the second sample code, and the label of the third sample code; wherein the priority of the first sample code is higher than that of the second sample code, and the priority of the second sample code is higher than that of the third sample code.
[0144] Step S706: Based on priority, construct ordered pairs for the first sample code, the second sample code, and the third sample code to obtain an ordered pair set. Construct a training dataset based on the ordered pair sets corresponding to each code quality evaluation dimension. The ordered pair set includes multiple ordered pairs, and each ordered pair contains two sample codes with different priorities.
[0145] Step S707: Train the basic reordering model based on the training dataset corresponding to each preset query statement to obtain the target reordering model; wherein, the target reordering model is used for reordering the code retrieval results.
[0146] It should be noted that the steps shown in the above process or in the flowchart of the accompanying figures can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases the steps shown or described may be executed in a different order than that shown here.
[0147] This embodiment also provides a reordering model training device for implementing the above embodiments and preferred embodiments; details already described will not be repeated. The terms "module," "unit," "subunit," etc., used below refer to combinations of software and / or hardware that perform a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0148] Figure 8 This is a structural block diagram of the reordering model training device in this embodiment, as shown below. Figure 8 As shown, the training device for the reordering model includes:
[0149] The acquisition module 10 is used to acquire the guide set corresponding to each preset code quality assessment dimension; wherein, the guide set includes multiple preset guides, and the multiple preset guides are used to characterize the generation strategies of different quality codes;
[0150] The generation module 20 is used to generate sample code corresponding to each preset guide in each code quality assessment dimension, based on each preset guide in the guide set corresponding to each code quality assessment dimension and the preset query statement.
[0151] Training module 30 is used to construct a training dataset corresponding to a preset query statement based on the sample code corresponding to each preset guideline under each code quality assessment dimension, and to train the basic re-ranking model based on the training dataset corresponding to each preset query statement to obtain the target re-ranking model; wherein, the target re-ranking model is used for re-ranking the code retrieval results.
[0152] Optionally, in some possible implementations, training module 30 is specifically used for:
[0153] For each code quality assessment dimension, the sample code corresponding to each preset guideline under the code quality assessment dimension is labeled to obtain the label of the first sample code and the label of the second sample code. The label of the first sample code is used to characterize the semantic relevance of the first sample code to the preset query statement and the first quality performance of the first sample code in the code quality assessment dimension. The label of the second sample code characterizes the semantic relevance of the second sample code to the query and the second quality performance of the second sample code in the code quality assessment dimension. The first quality performance is better than the second quality performance.
[0154] Obtain third sample codes that are not related to the query semantics and generate labels for the third sample codes; wherein, the labels of the third sample codes are used to characterize that the third sample codes are not related to the query semantics.
[0155] The priority of each sample code is determined based on the labels of the first sample code, the second sample code, and the third sample code; wherein the priority of the first sample code is higher than that of the second sample code, and the priority of the second sample code is higher than that of the third sample code.
[0156] Based on priority, ordered pairs are constructed for the first, second, and third sample codes to obtain an ordered pair set. The training dataset is then constructed based on the ordered pair sets corresponding to each code quality evaluation dimension. The ordered pair set includes multiple ordered pairs, and each ordered pair contains two sample codes with different priorities.
[0157] Optionally, in some possible implementations, training module 30 is specifically used for:
[0158] Based on the model prediction score corresponding to each sample code in each ordered pair, a first ranking loss function is constructed for the basic re-ranking model; where the model prediction score is the prediction score of the basic re-ranking model for each sample code in each ordered pair.
[0159] Based on the first ranking loss function, construct the target ranking loss function corresponding to the basic re-ranking model;
[0160] Based on the training dataset corresponding to each preset query statement and the target ranking loss function, the basic re-ranking model is trained to obtain the target re-ranking model.
[0161] Optionally, in some possible implementations, training module 30 is specifically used for:
[0162] A dynamic weight term is constructed based on a preset ranking evaluation index. The dynamic weight term is used to characterize the change in the preset ranking evaluation index after the two sample codes in the ordered pair are swapped in the current predicted ranking. The current predicted ranking is obtained by sorting the predicted scores of each sample code from high to low based on the basic re-ranking model.
[0163] Based on the dynamic weights and the first ranking loss function, construct the target ranking loss function.
[0164] Optionally, in some possible implementations, training module 30 is specifically used for:
[0165] Determine the first difference term based on the label of each sample code in the ordered pair;
[0166] The second difference term is determined based on the order position of each sample code in the ordered pair in the current predicted order;
[0167] The dynamic weight term is determined based on the first difference term, the second difference term, and the maximum cumulative gain of the loss under the ideal order.
[0168] Optionally, in some possible implementations, training module 30 is specifically used for:
[0169] Based on the label of each sample code in the ordered pair, determine the indicator function value corresponding to the ordered pair;
[0170] The third difference term is determined based on the order position of each sample code in the ordered pair in the current predicted order;
[0171] The dynamic weight term is determined based on the indicator function value and the third difference term.
[0172] It should be noted that the above modules can be functional modules or program modules, and can be implemented through software or hardware. For modules implemented through hardware, the above modules can reside in the same processor; or the above modules can be located in different processors in any combination.
[0173] This embodiment also provides a computer device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0174] Optionally, the computer device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0175] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementations, and will not be repeated in this embodiment.
[0176] Furthermore, in conjunction with the reordering model training method provided in the above embodiments, this embodiment can also provide a storage medium for implementation. This storage medium stores a computer program; when executed by a processor, this computer program implements any of the reordering model training methods in the above embodiments.
[0177] It should be understood that the specific embodiments described herein are merely illustrative of the application and not intended to limit it. All other embodiments derived by those skilled in the art based on the embodiments provided in this application without inventive effort are within the scope of protection of this application.
[0178] Obviously, the accompanying drawings are merely some examples or embodiments of this application. Those skilled in the art can apply this application to other similar situations based on these drawings without any creative effort. Furthermore, it is understood that although the work done in this development process may be complex and lengthy, for those skilled in the art, certain design, manufacturing, or production modifications made based on the technical content disclosed in this application are merely conventional technical means and should not be considered as insufficient disclosure of this application.
[0179] The term "embodiment" in this application refers to a specific feature, structure, or characteristic described in connection with an embodiment that may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily imply the same embodiment, nor does it imply that it is mutually exclusive with or independent of other embodiments. It will be clearly or implicitly understood by those skilled in the art that the embodiments described in this application may be combined with other embodiments without conflict.
[0180] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of patent protection. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the appended claims.
Claims
1. A method for training a reordering model, characterized in that, The reordering model training method includes: Obtain a set of guidelines corresponding to each preset code quality assessment dimension; wherein, the set of guidelines includes multiple preset guidelines, and the multiple preset guidelines are used to characterize the generation strategies of different quality codes; For each preset query statement, based on each preset guide in the guide set corresponding to each code quality assessment dimension and the preset query statement, sample code corresponding to each preset guide under each code quality assessment dimension is generated; Based on the sample code corresponding to each preset guideline under each of the code quality assessment dimensions, a training dataset corresponding to the preset query statement is constructed, and the basic re-ranking model is trained based on the training dataset corresponding to each preset query statement to obtain the target re-ranking model; wherein, the target re-ranking model is used for re-ranking processing of code retrieval results.
2. The reordering model training method according to claim 1, characterized in that, The plurality of preset guidelines include a first preset guideline for characterizing high-quality code generation strategies and a second preset guideline for characterizing low-quality code generation strategies; the sample code includes a first sample code corresponding to the first preset guideline and a second sample code corresponding to the second preset guideline; The step of constructing a training dataset corresponding to the preset query statement based on sample code corresponding to each preset guideline under each of the aforementioned code quality assessment dimensions includes: For each code quality assessment dimension, sample code corresponding to each preset guideline under the code quality assessment dimension is labeled to obtain a label for the first sample code and a label for the second sample code. The label for the first sample code characterizes its semantic relevance to the preset query statement and its first quality performance on the code quality assessment dimension. The label for the second sample code characterizes its semantic relevance to the query statement and its second quality performance on the code quality assessment dimension. The first quality performance is superior to the second quality performance. Obtain a third sample code that is not related to the query semantics, and generate a label for the third sample code; wherein, the label of the third sample code is used to characterize that the third sample code is not related to the query semantics; The priority of each sample code is determined based on the tags of the first sample code, the second sample code, and the third sample code; wherein the priority of the first sample code is higher than that of the second sample code, and the priority of the second sample code is higher than that of the third sample code. According to the priority, ordered pairs are constructed for the first sample code, the second sample code, and the third sample code to obtain an ordered pair set. The training dataset is constructed according to the ordered pair set corresponding to each of the code quality evaluation dimensions. The ordered pair set includes multiple ordered pairs, and each ordered pair contains two sample codes with different priorities.
3. The reordering model training method according to claim 2, characterized in that, The step of training the basic reordering model based on the training dataset corresponding to each of the preset query statements to obtain the target reordering model includes: Based on the model prediction score corresponding to each sample code in each ordered pair, a first ranking loss function corresponding to the basic re-ranking model is constructed; wherein, the model prediction score is the prediction score of the basic re-ranking model for each sample code in each ordered pair; Based on the first ranking loss function, construct the target ranking loss function corresponding to the basic re-ranking model; The basic re-ranking model is trained based on the training dataset corresponding to each preset query statement and the target ranking loss function to obtain the target re-ranking model.
4. The reordering model training method according to claim 3, characterized in that, The step of constructing the target ranking loss function corresponding to the basic re-ranking model based on the first ranking loss function includes: A dynamic weight term is constructed based on a preset ranking evaluation index; wherein, the dynamic weight term is used to characterize the change in the preset ranking evaluation index after the two sample codes in the ordered pair are swapped in the current predicted ranking, and the current predicted ranking is obtained by sorting the predicted scores of each sample code from high to low based on the basic re-ranking model. The target ranking loss function is constructed based on the dynamic weight term and the first ranking loss function.
5. The reordering model training method according to claim 4, characterized in that, The preset ranking evaluation index is the normalized cumulative gain; the determination of dynamic weight terms based on the preset ranking evaluation index includes: The first difference term is determined based on the label of each sample code in the ordered pair; The second difference term is determined based on the sorting position of each sample code in the ordered pair in the current predicted sorting; The dynamic weight term is determined based on the first difference term, the second difference term, and the maximum cumulative gain under ideal sorting.
6. The reordering model training method according to claim 4, characterized in that, The preset ranking evaluation index is the average reciprocal ranking; The step of determining dynamic weight items based on preset ranking evaluation indicators includes: Based on the label of each sample code in the ordered pair, determine the indicator function value corresponding to the ordered pair; The third difference term is determined based on the sorting position of each sample code in the ordered pair in the current predicted sorting; The dynamic weight term is determined based on the indicator function value and the third difference term.
7. The reordering model training method according to claim 1, characterized in that, The preset code quality assessment dimensions include at least one of code execution speed, code memory usage, and code maintainability.
8. A reordering model training device, characterized in that, The reordering model training device includes: The acquisition module is used to acquire a set of guidelines corresponding to each preset code quality assessment dimension; wherein, the set of guidelines includes multiple preset guidelines, and the multiple preset guidelines are used to characterize the generation strategies of different quality codes; The generation module is used to generate sample code corresponding to each preset guide under each preset guide in the guide set corresponding to each preset code quality assessment dimension, and the preset query statement for each preset query statement. The training module is used to construct a training dataset corresponding to the preset query statement based on the sample code corresponding to each preset guideline under each of the code quality assessment dimensions, and to train the basic re-ranking model based on the training dataset corresponding to each preset query statement to obtain the target re-ranking model; wherein, the target re-ranking model is used for re-ranking processing of code retrieval results.
9. A computer device, comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to perform the steps of the reordering model training method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the reordering model training method according to any one of claims 1 to 7.