Machine generated text detection model training method based on prefix enhancement and machine generated text detection method and device
By introducing prefix enhancement technology and feature weighted averaging module into the machine-generated text detection model, the problem of generalization of complex language models in the prior art is solved, and the detection accuracy and generalization ability of machine-generated text are improved.
Patent Information
- Application Number
- CN202411293515.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-14
- Publication Date
- 2025-01-10
- Estimated Expiration
- 2044-09-14
AI Technical Summary
The existing zero-sample machine-generated text detection method is difficult to generalize when dealing with complex language models, resulting in a decrease in the ability to accurately distinguish machine-generated text from human-written text, reducing the effectiveness of detection technology.
Using a machine-generated text detection model based on prefix enhancement, the prefix enhancement mapping module, word embedding module and feature weighted average module are used to enhance text feature expression capabilities, and integrate feature information through feature weighted average and attention masking mechanisms to improve the model's Generalization ability.
It improves the accuracy and generalization ability of machine-generated text detection, can more effectively distinguish machine-generated text from human-written text, and improves the performance of detection technology.
Smart Images

Figure CN119294381A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of machine-generated text detection, and in particular to a training method for a machine-generated text detection model based on prefix enhancement, and a machine-generated text detection method and device. Background Art
[0002] Today, large language models (LLMs) perform well in generating complex and coherent narratives, relying on their large parameter size and extensive training data. However, they sometimes introduce false information or fictitious details, making the generated texts have problems with accuracy and originality. Therefore, it has become increasingly urgent to develop effective detection techniques to evaluate the reliability of machine-generated texts.
[0003] In the existing field of machine-generated text detection, the performance of zero-shot methods is the most outstanding in identifying machine-generated text. This method uses the understanding of text generation patterns and language rules of general pre-trained language models, such as BERT and GPT, to identify machine-generated text by using predefined labels or prompts without specific task training. Since the text estimated by the detection model is similar enough to the machine-generated text in language structure and semantics, the zero-shot detection method can work effectively.
[0004] However, existing zero-shot methods have some non-negligible flaws:
[0005] The effectiveness of zero-shot methods fundamentally relies on the assumption that the language structure and semantic knowledge learned by the model during the general pre-training process is sufficiently broad and deep. This assumption holds true in many previous studies, especially when dealing with early generative models, whose text generation process typically relies on a small number of initial tokens. However, zero-shot methods face generalization difficulties when dealing with scenarios involving more advanced language models. These complex models usually undergo additional training stages to make their outputs consistent with human preferences and task requirements, and can generate text with unique stylistic elements that deviate significantly from typical text generation patterns. This makes it difficult for detection models to accurately distinguish machine-generated text from human-written text, thereby reducing the effectiveness of current zero-shot detection techniques. Summary of the invention
[0006] In view of the above defects or improvement needs of the prior art, the present invention provides a training method for a machine-generated text detection model based on prefix enhancement, a machine-generated text detection method and a device, which aims to solve the problem of poor accuracy in machine-generated text detection due to lack of in-depth understanding of the text generation process in the prior art.
[0007] To achieve the above object, according to one aspect of the present invention, there is provided: a training method for a machine-generated text detection model, wherein the machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the method includes:
[0008] Inputting the original text into the prefix enhancement mapping module to obtain a first prefix enhancement prompt feature vector after linear adaptation;
[0009] Input the original text into the word embedding module to obtain the original text feature vector;
[0010] Inputting the prefix enhancement prompt feature vector and the original text feature vector into the feature weighted average module to obtain the first fused feature vector;
[0011] Inputting the first fused feature vector into the output module to obtain a prediction result of the original text; and
[0012] The machine-generated text detection model is trained based on the first fused feature vector, the prediction result of the original text, and the degree of difference between the current prediction result of the model and the true label.
[0013] In one embodiment, the prefix enhancement mapping module includes a prefix enhancement marker, a prefix enhancement mapping model and a prefix enhancement prompt word adapter. The original text is input into the prefix enhancement mapping module to obtain a first prefix enhancement prompt feature vector after linear adaptation, including:
[0014] The original text is input into the prefix enhancement mapping module, and the original text is segmented by the prefix enhancement tagger to obtain the tag ID sequence of the prefix enhancement text;
[0015] The tag ID sequence of the prefix enhanced text is processed by the prefix enhancement mapping model to obtain the prefix enhancement prompt feature vector;
[0016] The prefix enhancement prompt feature vector is processed by a prefix enhancement prompt word adapter to obtain a prefix enhancement prompt feature vector after linear adaptation.
[0017] In one embodiment, the word embedding module includes a tagger and a word embedding matrix. The original text is input into the word embedding module to obtain the original text feature vector, including:
[0018] The original text is input into the word embedding module and marked by the marker to obtain a text tag ID sequence;
[0019] The text tag ID sequence is processed through the word embedding matrix to obtain the original text feature vector.
[0020] In one implementation, the prefix enhancement prompt feature vector and the original text feature vector are input into a feature weighted average module to obtain a fused feature vector, including:
[0021] The feature weighted averaging module performs weighted averaging on the prefix enhancement hint feature vector and the original text feature vector along the feature dimension to obtain a weighted feature vector as the first fused feature vector.
[0022] In one embodiment, the machine-generated text detection model is trained based on the fused feature vector, the prediction result of the original text, and the degree of difference between the current prediction result of the model and the true label, including:
[0023] By creating an attention mask to indicate the area that the model should focus on when processing the fused features, ignoring padding or other irrelevant parts, the first fused feature vector and the attention mask are used as input, forward propagation is performed in the model, and a data dictionary is output, where the data dictionary contains the predicted value calculated by the output module, the weighted feature vector calculated by the feature weighted average module, and the cross entropy loss calculated according to the degree of difference between the current prediction result of the model and the true label.
[0024] Based on the same inventive concept, the second aspect of the present invention provides a method for detecting machine-generated text, wherein the machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the method includes:
[0025] Input the text to be detected into the prefix enhancement mapping module to obtain a second prefix enhancement prompt feature vector after linear adaptation;
[0026] Input the text to be detected into the word embedding module to obtain the feature vector of the text to be detected;
[0027] Inputting the prefix enhancement prompt feature vector and the feature vector of the text to be detected into the feature weighted average module to obtain a second fused feature vector;
[0028] The second fused feature vector is input into the output module to obtain the prediction result of the text to be detected.
[0029] The machine-generated text detection model is a model obtained by using the training method for the machine-generated text detection model described in any one of claims 1 to 5.
[0030] Based on the same inventive concept, the third aspect of the present invention provides a training device for a machine-generated text detection model, wherein the machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the device includes:
[0031] A first prefix enhancement prompt feature vector acquisition module, used for inputting the original text into the prefix enhancement mapping module to obtain a first prefix enhancement prompt feature vector after linear adaptation;
[0032] The original text feature vector acquisition module is used to input the original text into the word embedding module to obtain the original text feature vector;
[0033] A first fused feature vector obtaining module, used for inputting the prefix enhancement prompt feature vector and the original text feature vector into a feature weighted average module to obtain a first fused feature vector;
[0034] A first prediction result output module, used for inputting the first fused feature vector into the output module to obtain a prediction result of the original text; and
[0035] A forward propagation module is used to train the machine-generated text detection model based on the first fused feature vector, the prediction result of the original text, and the degree of difference between the current prediction result of the model and the true label.
[0036] Based on the same inventive concept, the fourth aspect of the present invention provides a machine-generated text detection device, wherein the machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the device includes:
[0037] A second prefix enhancement prompt feature vector acquisition module, used for inputting the text to be detected into the prefix enhancement mapping module to obtain a second prefix enhancement prompt feature vector after linear adaptation;
[0038] A module for obtaining a feature vector of the text to be detected is used to input the text to be detected into a word embedding module to obtain a feature vector of the text to be detected;
[0039] A second fused feature vector obtaining module, used for inputting the prefix enhancement prompt feature vector and the feature vector of the text to be detected into a feature weighted average module to obtain a second fused feature vector;
[0040] The second prediction result output module is used to input the second fused feature vector into the output module to obtain the prediction result of the text to be detected.
[0041] Wherein, the machine-generated text detection model is a model obtained by using the training device of the machine-generated text detection model described in claim 7.
[0042] Based on the same inventive concept, the fifth aspect of the present invention provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, it implements the training method of the machine-generated text detection model described in the first aspect or the machine-generated text detection method described in the second aspect.
[0043] Based on the same inventive concept, the sixth aspect of the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the training method for the machine-generated text detection model described in the first aspect or the machine-generated text detection method described in the second aspect is implemented.
[0044] Compared with the prior art, the advantages and beneficial technical effects of the present invention are as follows:
[0045] The present invention provides a training method for a machine-generated text detection model. A prefix enhancement mapping module is used to perform prefix enhancement processing on the input original text to enhance the expressiveness of text features. By repeating these embedding vectors to match the batch size of the input text, the original text is marked and embedded using a secondary embedding module to obtain the original text feature vector, and the feature weighted average module is used to perform weighted average, thereby integrating feature information from different sources. In training mode, the output of the feature weighted average module will be used for the forward propagation of the model to generate a prediction result. At the same time, if a true label is provided, the loss function will be calculated, and back propagation will be performed to update the model parameters. Therefore, a machine-generated text detection model with better generalization ability can be obtained. The accuracy of the prediction can be improved by using the trained machine-generated text detection model to detect machine-generated text. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0047] Figure 1 A flowchart of a training method for a machine-generated text detection model in an embodiment of the present invention;
[0048] Figure 2 A flowchart of a machine-generated text detection method in an embodiment of the present invention;
[0049] Figure 3 A structural block diagram of a training device for a machine-generated text detection model in an embodiment of the present invention;
[0050] Figure 4 It is a structural block diagram of a machine-generated text detection device in an embodiment of the present invention.
[0051] Figure 5 A structural diagram of a machine-generated text detection model based on prefix enhancement in an embodiment of the present invention;
[0052] Figure 6 It is a detailed flow chart of the training method of the machine-generated text detection model in an embodiment of the present invention. DETAILED DESCRIPTION
[0053] The present invention discloses a machine-generated text detection model based on prefix enhancement and a training method thereof. In a specific implementation process, the GPT-J model is first obtained, and a scoring model and a reference model and respective word segmenters are initialized based on the model to obtain a set of preprocessing models, and then a data set is obtained, and the data set is divided into a training set and a validation set. The preprocessing model, the validation set, and the data set are used to fine-tune and evaluate the preprocessing model. The present invention can solve the problem of poor accuracy in detecting machine-generated texts due to the lack of in-depth understanding of the text generation process in the prior art.
[0054] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0055] Embodiment 1
[0056] The present invention discloses a training method for a machine-generated text detection model based on prefix enhancement, wherein the machine-generated text detection model comprises a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the method comprises:
[0057] S110: inputting the original text into a prefix enhancement mapping module to obtain a first prefix enhancement prompt feature vector after linear adaptation;
[0058] S120: inputting the original text into a word embedding module to obtain an original text feature vector;
[0059] S130: inputting the prefix enhancement prompt feature vector and the original text feature vector into a feature weighted average module to obtain a first fused feature vector;
[0060] S140: inputting the first fused feature vector into an output module to obtain a prediction result of the original text; and
[0061] The machine-generated text detection model is trained based on the first fused feature vector, the prediction result of the original text, and the degree of difference between the current prediction result of the model and the true label.
[0062] See also Figure 1 and Figure 5 ,in, Figure 1 is a flowchart of a training method for a machine-generated text detection model in an embodiment of the present invention, Figure 5 This is a structural diagram of a machine-generated text detection model based on prefix enhancement in an embodiment of the present invention.
[0063] The prefix enhancement mapping module includes a prefix enhancement marker, a prefix enhancement mapping model and a prefix enhancement prompt word adapter;
[0064] In the specific implementation process, the prefix enhancement tagger uses BertTokenizer to perform word segmentation on the original text to convert the text into a tag sequence that the model can understand. The input of the prefix enhancement tagger is the original text sequence, and the output is the tag ID sequence of the prefix enhancement text.
[0065] The prefix augmentation mapping model uses the Bert model to process the tag sequence of prefix augmented text and generate feature representation of prefix augmented text. The input of the prefix augmentation mapping model is the tag ID sequence of prefix augmented text, and the output is the prefix augmentation prompt feature vector.
[0066] The prefix enhancement cue word adapter is a custom linear layer that adjusts the dimension of the feature through linear transformation. The input is the feature vector of the prefix enhancement cue word, and the output is the prefix enhancement cue feature vector after linear adaptation.
[0067] The word embedding module includes a tagger and a word embedding matrix, which processes the original text data into a sequence of text tags and then converts it into a continuous vector representation. The input is the original text and the output is the original text feature vector.
[0068] The feature weighted average module is used to weighted average the prefix enhancement prompt feature vector and the original text feature vector along the feature dimension. The input of the feature weighted average module is the prefix enhancement prompt feature vector and the original text feature vector, and the output is the weighted feature vector, that is, the feature vector after the first fusion.
[0069] In one embodiment, the forward propagation module is used to perform the forward propagation process of the prompt prefix enhancement. By creating an attention mask, the model is instructed to pay attention to the area when processing the fused features, ignoring the padding or other irrelevant parts. First, the first fused feature representation is obtained by weighted averaging or connecting the prefix enhancement prompt feature vector with the original text feature vector. At the same time, when processing the input data, the length of the input sequence is analyzed, and a binary matrix is generated as an attention mask in combination with the actual effective tag information. 1 in the matrix represents the valid input part that the model should pay attention to, and 0 represents the padding part or irrelevant part that should be ignored, which is used to indicate the part that the model should pay attention to during the forward propagation process. The module takes the fused feature representation and the attention mask as input, performs forward propagation in the model, and outputs a data dictionary. The data dictionary contains the predicted value calculated by the word segmenter, the weighted feature vector calculated by the feature weighted average module, and the cross entropy loss calculated by the cross entropy loss function. The specific form of the cross entropy loss function is as follows:
[0070] L(y,p)=-[ylog(p)+(1-y)log(1-p)]
[0071] Among them, L(y,p) is the cross entropy loss obtained, y is the true label, p is the probability that the model predicts it to be a positive class (label 1), and L(y,p) is the loss value.
[0072] The adaptive weight decay mechanism is adopted in the training process. The adaptive weight decay optimizer specifically adopts the Adam optimization algorithm and weight decay to efficiently optimize the model parameters and control the generalization ability of the model. The input of the adaptive weight decay optimizer is the model weights and biases to be optimized, the gradient value calculated according to the loss function during the forward propagation of the model, the initial learning rate, and the hyperparameters that control the strength of weight decay. The output is the weight, learning rate, gradient, and loss value of the model after the gradient and learning rate are adjusted.
[0073] The cross entropy loss evaluator is a module of the forward propagation module, which is a module used to calculate the cross entropy loss in classification problems. The input includes the model's prediction results for the input data and the label of the true category of each sample, and the output is the cross entropy loss that represents the degree of difference between the model's current prediction results and the true label.
[0074] Preferably, the prefix enhancement mapping module first performs word segmentation on the input text and converts it into a tag sequence that the model can understand using T5Tokenizer. Next, the T5Model model is used to process the reverse text sequence to obtain an initial reverse feature representation. Through a custom linear layer, the reverse features are mapped to the target dimension to adapt to the input requirements of the main model. Then, the static prompt embedding vector is initialized to enhance the expressiveness of the text features. These embedding vectors are repeated to match the batch size of the input text.
[0075] The feature weighted average module first calculates the weighted average of the prefix enhancement hint feature vector and the original text feature vector. This step first calculates the weighting coefficient and implements the weighted average of the two feature vectors through element-wise multiplication and addition, thereby integrating feature information from different sources. Subsequently, the model uses these weighted feature vectors for subsequent processing, and no longer needs to construct an attention mask. Specifically, in training mode, the output of the feature weighted average module will be used for the forward propagation of the model to generate prediction results. At the same time, if the true label is provided, the loss function will be calculated and backpropagation will be performed to update the model parameters. In this process, the effect of the feature weighted average module directly affects the learning efficiency and final performance of the model. In non-training mode, such as the validation or testing phase, the feature weighted average module still processes the input text according to the same steps, but no parameter updates are performed. At this point, the purpose of the technology is to stably provide weighted features in order to evaluate the model's prediction accuracy and generalization ability for new data.
[0076] In training mode, the output of the feature weighted average module will be used for the forward propagation of the model to generate prediction results. At the same time, if the true label is provided, the loss function will be calculated and backpropagation will be performed to update the model parameters. In this process, the effect of the feature weighted average module directly affects the learning efficiency and final performance of the model. In non-training mode, such as the validation or testing phase, the feature weighted average module still processes the input text according to the same steps, but no parameter updates are performed. At this point, the purpose of the technology is to provide weighted features stably in order to evaluate the model's prediction accuracy and generalization ability for new data.
[0077] The forward propagation module consists of the following sub-steps:
[0078] (S1) Tokenize the text using the prefix augmentation tokenizer to convert it into a sequence of token IDs for reverse text that the model can handle, and add padding and truncation with a maximum length of 20. Move the resulting sequence of token IDs for reverse text to the device as input IDs.
[0079] (S2) Move the decoder input embeddings to the device and repeat to match the batch size of the inverse mapping input ID (the token ID of the reversed text), ensuring that each input text has a corresponding embedding representation.
[0080] (S3) Using the input ID obtained in step (S1) and the decoder input feature vector obtained in step (S2) as input, forward pass through the prefix enhancement mapping model to obtain the final hidden state of the prefix enhancement prompt word, which is then converted into a floating point number and adapted through the prefix enhancement prompt adapter. Output the adapted prefix enhancement prompt word feature vector and the length of the prefix enhancement prompt word feature vector.
[0081] (S4) Use the tokenizer to convert the tokenized text in step (S1) into a tag ID sequence of the original text that can be processed by the model, add padding, do not return the tag type ID, and move it to the device. The result of the original text tokenization and the input ID after removing the first token are obtained.
[0082] (S5) Input the adapted prompt word feature vector of step (S3) and the tokenized result of step (S4), and obtain the original text feature vector through the word embedding conversion matrix. Secondly, connect the adapted prefix enhanced prompt word feature vector obtained by the prefix enhancement mapping model in step (S3) with the original text feature vector obtained after tokenizing the original text in step (S4), and convert them into an appropriate data type to obtain a fused feature vector.
[0083] (S6) Input the adapted prefix-enhanced cue word feature vector of step (S3) and the tokenized result of step (S4), create a tensor with all 1s, whose shape is consistent with the batch and length of the prefix-enhanced cue word feature vector, and concatenate it with the tokenized attention mask to obtain a fused attention mask.
[0084] (S7) Input the fused word embedding vector of step (S5) and the fused attention mask of step (S6), use the scoring model for forward propagation, calculate the logits score, and output the logits score corresponding to each category of the scoring model.
[0085] (S8) Input the logits score obtained in step (S7) and the input ID after removing the first token in step (S4), and determine whether the scoring model used to calculate the logits score is the same as the reference model used to calculate the reference logits, so as to ensure the consistency and comparability of the calculation results. Specifically, input the logits score obtained in step (S7) and the input ID after removing the first token in step (S4), and determine whether the scoring model used to calculate the logits score is the same as the reference model used to calculate the reference logits, so as to ensure the consistency and comparability of the calculation results. If a different reference model is used, the original text of the input model is tokenized using a reference tokenizer, and forward passed through the reference model to calculate the reference logits. The original standard is then calculated using the discriminant criterion function. The logits and original standard of the reference model are output.
[0086] (S9) Input the result of tokenization of the original text in step (S4), the logits of the reference model in step (S8), the logits score obtained in (S7), and the input ID after removing the first token in step (S4). Use the scoring model to obtain the last tagged logits, make predictions through the classifier, and obtain a probability distribution indicating the possibility that the text belongs to a machine or a human. The probability distribution and the original standard are used as output. If the true label gts is provided, the loss is calculated, and the prediction result and loss are finally output.
[0087] The method of the present invention is described below by means of specific examples.
[0088] According to another aspect of the present invention, a method for training a machine-generated text detection model based on prefix enhancement is provided. Figure 6 , including the following steps:
[0089] (1) Obtain the GPT-J model and download the model file to a local directory.
[0090] (2) Use the pre-trained model obtained in step (1) as the scoring model and reference model, initialize its word segmenter and model respectively, and define the loss function and forward propagation function to obtain a complete model ready for training and verification.
[0091] (3) Obtain a data set, divide 80% of the data in the data set into a training set, and divide 20% of the data in the data set into a validation set.
[0092] (4) Using the training set obtained in step (3), randomly select 1,000 data points to obtain a training subset.
[0093] (5) Using the preprocessing model obtained in step (2) (the complete model prepared for training and validation), the validation set obtained in step (3), and the training subset obtained in step (4), the preprocessing model is fine-tuned and evaluated to obtain evaluation data.
[0094] It should be noted that the forward propagation function is a broader term that refers to the process by which the model calculates the output after receiving the input through various layers and modules (forward propagation process). It involves the extraction, fusion and final generation of prediction results of features. The main task of the forward propagation function is to pass the input data through the network structure of the model to obtain the output of the model.
[0095] The forward loss function is a specific term that refers to the part of the forward propagation process used to calculate the difference (loss) between the model output and the true label. In this invention, it refers to the cross entropy loss function, whose main task is to evaluate the error between the model's prediction results and the true label so as to perform backpropagation and parameter update during the training process.
[0096] Therefore, although both involve the forward propagation process of the model, the "forward propagation function" is a broader concept, while the "forward loss function" focuses on the specific implementation of calculating the loss.
[0097] Preferably, step (2) comprises the following sub-steps:
[0098] (2-1) Load the basic components required to initialize the prefix enhancement mapping task.
[0099] (2-2) Use the model obtained in step (1) as the scoring model, set the data set to 'xsum', and load the tokenizer loading module to obtain a scoring model tokenizer loaded into the specified device.
[0100] (2-3) The model obtained in step (1) is used as a scoring model and loaded into a model loading module to obtain a pre-trained scoring model loaded into a specified device.
[0101] (2-4) Determine whether the reference model is loaded, or whether the scoring model is different from the reference model. If so, proceed to step (2-5); if not, proceed to step (2-10).
[0102] (2-5) Use the model obtained in step (1) as the reference model, set the data set to 'xsum', and load the tokenizer loading module to obtain a reference model tokenizer loaded into the specified device.
[0103] (2-6) The model obtained in step (1) is used as a reference model and loaded into the model loading module to obtain a pre-trained reference model loaded into the specified device.
[0104] (2-7) Register model components that do not participate in gradient updates.
[0105] (2-8) Using the components and models in steps (2-1) to (2-6), complete initialization and prepare for training.
[0106] (2-9) Initialize the sampling bias and sampling bias analysis modules for evaluating the sampling bias between two sets of logits. Define a linear classifier and move it to the specified device (CPU or GPU). This linear layer has 50257 input features and 2 output features for binary classification tasks.
[0107] (2-10) Initialize the forward propagation module used to execute the prefix enhanced cue word generation algorithm.
[0108] Preferably, step (2-1) comprises the following sub-steps:
[0109] (2-1-1) Load the pre-trained T5 tokenizer to convert text into a token sequence that the model can process. Load the pre-trained T5 model and run it with 8-bit precision. Initialize the basic components required for the prefix enhancement mapping task.
[0110] (2-1-2) Create a custom linear layer as an adapter layer to map the output of the prefix enhancement mapping model from a 512-dimensional feature space to a 2560-dimensional feature space.
[0111] (2-1-3) Set the maximum length of the prefix enhancement prompt to 20 and the length of the static prompt to 0.
[0112] (2-1-4) Initialize a learnable embedding vector of shape (20,512) for the decoder input, filled with normal distribution random numbers.
[0113] Preferably, step (5) comprises the following sub-steps:
[0114] (5-1) This step is the model evaluation module, which uses the validation set obtained in step (3) to evaluate the preprocessing model obtained in step (2) to verify the effectiveness and accuracy of the model.
[0115] (5-2) The training subset obtained in step (4) will be used for model training, and the validation set obtained in step (3) will be used for model evaluation and hyperparameter adjustment.
[0116] (5-3) Get the initialized recorder, data loader, total number of epochs, AdamW optimizer, cosine annealing learning rate scheduler, and gradient scaler.
[0117] (5-4) Move the model obtained in step (2) to the specified device.
[0118] (5-5) Set the cumulative value of the gradient steps to 1.
[0119] (5-6) Initialize the epoch loss value list to store the loss value of each epoch.
[0120] (5-7) Initialize iteration counter i to 0.
[0121] (5-8) Initialize the loss tensor and move it to the specified device.
[0122] (5-9) Clear the gradient of the Adam optimizer obtained in step (5-3).
[0123] (5-10) Using the data loader obtained in step (5-3), obtain the original text and sampled text in batches, merge them into mixed text, and create corresponding labels.
[0124] (5-11) Update the learning rate scheduler obtained in step (5-3).
[0125] (5-12) Start automatic mixed precision training, use the text data obtained in step (5-10) to forward propagate through the model, obtain the output of the model, use the loss function to calculate the loss of the current batch, and add the loss of the current batch to the total loss.
[0126] (5-13) According to the length of the original text and the sampled text, a corresponding label list is generated, where the label of the original text is 0 and the label of the sampled text is 1.
[0127] (5-14) Convert the logits output by the model into probabilities, and use a threshold of 0.5 to classify the probabilities into two categories to obtain the prediction results. Calculate the sigmoid probability output by the model and add it to the probability list. Then, add the true label of the current batch to the true label list.
[0128] (5-15) Determine whether the iteration counter i+1 has reached the cumulative value of the gradient steps in step (5-5). If so, proceed to step (5-16); if not, proceed to step (5-17).
[0129] (5-16) Perform backpropagation, update model parameters and gradient scaler, and clear the optimizer gradient.
[0130] (5-17) Record the loss of the current batch into the epoch loss value list.
[0131] (5-18) Increase the iteration counter obtained in step (5-7) by 1.
[0132] (5-19) Using the area calculation module under the ROC curve of step (5-1-11), input the probability list and true label list obtained in step (5-14) to obtain the area under the ROC curve (ROC AUC) of the current model.
[0133] (5-20) Using the area under the precision-recall curve calculation module of step (5-1-12), input the probability list and true label list obtained in step (5-14) to obtain the area under the precision-recall curve (PRAUC) of the current model.
[0134] (5-21) Convert the prediction result list and the true label list of step (5-14) into tensors, and calculate their matching degree and average accuracy. Use the epoch loss value list updated in step (5-17) to get the average loss value of the current epoch.
[0135] (5-22) Using the recorder obtained in step (5-3), write the epoch average loss value obtained in the current step (5-21), the average accuracy obtained in step (5-21), the ROC AUC obtained in step (5-19), and the PR AUC obtained in step (5-20) into TensorBoard.
[0136] (5-23) Determine whether the current training cycle is a multiple of 4. If so, proceed to step (5-24); if not, proceed to step (5-25).
[0137] (5-24) Call the evaluation module of the training model of step (5-1) to obtain the evaluation results of the current model's performance on the verification data set.
[0138] (5-25) Save the model parameters fine-tuned in the current training cycle to the specified directory and print the save path.
[0139] (5-26) Close the recorder in step (5-3).
[0140] (5-27) Returns the fine-tuned model.
[0141] Preferably, step (5-1) is a model evaluation module, which includes the following sub-steps:
[0142] (5-1-1) Create an evaluation loader to load the dataset in batches.
[0143] (5-1-2) Move the preprocessed model obtained in step (2) to the specified device and set the model to evaluation mode.
[0144] (5-1-3) Initialize the loss value to 0.
[0145] (5-1-4) Create a data loader to load the validation set data obtained in step (3) in batches.
[0146] (5-1-5) Initialize the accuracy list to store the accuracy of each batch; initialize the probability list to store the predicted probability of the model output; initialize the true label list to store the true label.
[0147] (5-1-6) Use a context manager so that gradients are not calculated within the context.
[0148] (5-1-7) Determine whether the lengths of the original text and the sampled text are equal. If they are equal, proceed to step (5-1-8); if they are not equal, report an error.
[0149] (5-1-8) The original text and the sampled text are merged into a mixed text, and a corresponding label list is generated based on the original text and the sampled text.
[0150] (5-1-9) Pass the mixed text into the model for forward propagation to obtain the output.
[0151] (5-1-10) Add the loss value of the current batch to the cumulative loss. Add the true label and predicted probability of the batch to the true label list and probability list respectively, calculate the accuracy and accumulate them.
[0152] (5-1-11) Calculate PR AUC. Input the true labels and probability list of all samples updated in step (5-1-10) and calculate the precision and recall. Then use the obtained precision and recall to calculate the area under the precision-recall curve to get PR AUC.
[0153] (5-1-12) ROCAUC is calculated using the true labels and probability lists of all samples updated in step (5-1-10).
[0154] (5-1-13) Divide the accumulated loss during the entire epoch by the total number of samples to get the average loss.
[0155] (5-1-14) Returns a dictionary containing average accuracy, average loss, ROC AUC, and PR AUC.
[0156] Preferably, step (5-3) comprises the following sub-steps:
[0157] (5-3-1) Initialize the recorder. Get the current time and format it into a string.
[0158] (5-3-2) Initialize a TensorBoard logger object and set the log path according to the obtained time string.
[0159] (5-3-3) Create a data loader, set the batch size to 1, use the training subset obtained in step (4) to load a batch of data, and perform a random shuffle operation.
[0160] (5-3-4) Set the total number of training epochs.
[0161] (5-3-5) Use the Adam optimizer to initialize the model parameters and set the learning rate. The default value is 5e-5.
[0162] (5-3-6) Initialize the cosine annealing learning rate scheduler, which is used to dynamically adjust the learning rate. The period is set to the product of the length of the data loader obtained in step (5-3-3) and the total number of epochs obtained in step (5-3-4).
[0163] (5-3-7) Define the cross entropy loss function, which is used for loss calculation in model training.
[0164] (5-3-8) Initialize the GradScaler gradient scaler for mixed precision training.
[0165] Preferably, step (5-16) comprises the following sub-steps:
[0166] (5-16-1) Use the gradient scaler in step (5-3) and the loss tensor updated in step (5-12), calculate the gradient of the loss and perform backpropagation.
[0167] (5-16-2) Use the gradient scaler in step (5-3) and the Adam optimizer in step (5-3) to update the parameters of the model and execute the optimizer steps.
[0168] (5-16-3) Clear the gradient of the Adam optimizer in step (5-3).
[0169] (5-16-4) Update the scaling factor of the gradient scaler in step (5-3).
[0170] (5-16-5) Use the recorder in step (5-3) to write the loss updated in step (5-12) to TensorBoard for visualizing the loss during training.
[0171] (5-16-6) Add the loss tensor updated in step (5-12) to the epoch loss value list in step (5-6).
[0172] (5-16-7) Reset the loss tensor and move it to the specified device.
[0173] Compared with the general training method, the training method of the machine-generated text detection model based on prefix enhancement proposed in this scheme is mainly different in the following aspects:
[0174] 1. Prefix enhancement technology: This method introduces prefix enhancement technology, which enhances the model's ability to understand the input text through a specific prompt word generation algorithm. This technology can help the model better capture contextual information, thereby improving the quality and accuracy of text generation.
[0175] 2. Model initialization and fine-tuning: The method clearly distinguishes between the scoring model and the reference model, and trains and evaluates the model by fine-tuning the preprocessing model. This method enables the model to better utilize existing knowledge during training and improves the performance of the model.
[0176] Embodiment 2
[0177] Based on the same inventive concept, this embodiment discloses a machine-generated text detection method, wherein the machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, see Figure 2 , the method comprising:
[0178] S210: inputting the text to be detected into a prefix enhancement mapping module to obtain a second prefix enhancement prompt feature vector after linear adaptation;
[0179] S220: Input the text to be detected into a word embedding module to obtain a feature vector of the text to be detected;
[0180] S230: inputting the prefix enhancement prompt feature vector and the feature vector of the text to be detected into a feature weighted average module to obtain a second fused feature vector;
[0181] S240: Input the second fused feature vector into the output module to obtain a prediction result of the text to be detected.
[0182] The machine-generated text detection model is a model obtained by using the training method of the machine-generated text detection model of the first embodiment.
[0183] Since the method introduced in the second embodiment of the present invention is a method implemented by the model obtained by the training method of the machine-generated text detection model in the first embodiment of the present invention, those skilled in the art can understand the specific implementation and application of the method based on the method introduced in the first embodiment of the present invention, so it is not repeated here. All implementations and applications based on the model obtained by the method in the first embodiment of the present invention belong to the scope of protection of the present invention.
[0184] Embodiment 3
[0185] Based on the same inventive concept, the present invention provides a training device for a machine-generated text detection model based on prefix enhancement, wherein the machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, see Figure 3 , the device comprises:
[0186] A first prefix enhancement prompt feature vector obtaining module 301 is used to input the original text into the prefix enhancement mapping module to obtain a first prefix enhancement prompt feature vector after linear adaptation;
[0187] The original text feature vector obtaining module 302 is used to input the original text into the word embedding module to obtain the original text feature vector;
[0188] A first fused feature vector obtaining module 303, used for inputting the prefix enhancement prompt feature vector and the original text feature vector into a feature weighted average module to obtain a first fused feature vector;
[0189] A first prediction result output module 304, used to input the first fused feature vector into an output module to obtain a prediction result of the original text; and
[0190] The forward propagation module 305 is used to train the machine-generated text detection model based on the first fused feature vector, the prediction result of the original text, and the degree of difference between the current prediction result of the model and the true label.
[0191] Since the device introduced in the third embodiment of the present invention is a device used to implement the training method of the machine-generated text detection model in the first embodiment of the present invention, based on the method introduced in the first embodiment of the present invention, the person skilled in the art can understand the specific structure and deformation of the device, so it is not repeated here. All devices used in the method in the first embodiment of the present invention belong to the scope of protection of the present invention.
[0192] Embodiment 4
[0193] Based on the same inventive concept, the present invention provides a machine-generated text detection device, wherein the machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, see Figure 4 , the device comprises:
[0194] A second prefix enhancement prompt feature vector obtaining module 401 is used to input the text to be detected into the prefix enhancement mapping module to obtain a second prefix enhancement prompt feature vector after linear adaptation;
[0195] The module 402 for obtaining the feature vector of the text to be detected is used to input the text to be detected into the word embedding module to obtain the feature vector of the text to be detected;
[0196] A second fused feature vector obtaining module 403, for inputting the prefix enhancement prompt feature vector and the feature vector of the text to be detected into a feature weighted average module to obtain a second fused feature vector;
[0197] The second prediction result output module 404 is used to input the second fused feature vector into the output module to obtain the prediction result of the text to be detected.
[0198] Wherein, the machine-generated text detection model is a model obtained by using the training device of the machine-generated text detection model described in claim 7.
[0199] Since the device introduced in the fourth embodiment of the present invention is a device used to implement the machine-generated text detection method in the second embodiment of the present invention, based on the method introduced in the first embodiment of the present invention, the person skilled in the art can understand the specific structure and deformation of the device, so it is not repeated here. All devices used in the method in the first embodiment of the present invention belong to the scope of protection of the present invention.
[0200] Embodiment 5
[0201] Based on the same inventive concept, the present invention further provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the method described in the first embodiment is implemented.
[0202] Since the computer-readable storage medium introduced in the fifth embodiment of the present invention is a computer-readable storage medium used to implement the training method of the machine-generated text detection model in the first embodiment of the present invention or the machine-generated text detection method in the second embodiment, based on the method introduced in the first embodiment of the present invention, those skilled in the art can understand the specific structure and deformation of the computer-readable storage medium, so it is not repeated here. All computer-readable storage media used in the methods of the first or second embodiments of the present invention belong to the scope of protection of the present invention.
[0203] Embodiment 6
[0204] The present invention also 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 implements the method described in Embodiment 1 when executing the program.
[0205] Since the computer device introduced in the fourth embodiment of the present invention is a computer device used to implement the training method of the machine-generated text detection model in the first embodiment of the present invention or the machine-generated text detection method in the second embodiment, based on the method introduced in the first embodiment of the present invention, the person skilled in the art can understand the specific structure and deformation of the computer device, so it is not repeated here. All computer devices used in the methods of the first or second embodiments of the present invention belong to the scope of protection of the present invention.
[0206] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0207] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0208] Although preferred embodiments of the present invention have been described, additional changes and modifications may be made to these embodiments by those skilled in the art once the basic creative concepts are known. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention. Obviously, those skilled in the art may make various changes and modifications to the embodiments of the present invention without departing from the spirit and scope of the embodiments of the present invention. Thus, if these modifications and variations of the embodiments of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A training method for a machine-generated text detection model based on prefix enhancement, characterized in that: in, The machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the method includes: Inputting the original text into the prefix enhancement mapping module to obtain a first prefix enhancement prompt feature vector after linear adaptation; Input the original text into the word embedding module to obtain the original text feature vector; Inputting the prefix enhancement prompt feature vector and the original text feature vector into the feature weighted average module to obtain the first fused feature vector; Inputting the first fused feature vector into the output module to obtain a prediction result of the original text; and The machine-generated text detection model is trained based on the first fused feature vector, the prediction result of the original text, and the degree of difference between the current prediction result of the model and the true label.
2. The method for training a machine-generated text detection model as claimed in claim 1, characterized in that: The prefix enhancement mapping module includes a prefix enhancement marker, a prefix enhancement mapping model and a prefix enhancement prompt word adapter. The original text is input into the prefix enhancement mapping module to obtain a first prefix enhancement prompt feature vector after linear adaptation, including: The original text is input into the prefix enhancement mapping module, and the original text is segmented by the prefix enhancement tagger to obtain the tag ID sequence of the prefix enhancement text; The tag ID sequence of the prefix enhanced text is processed by the prefix enhancement mapping model to obtain the prefix enhancement prompt feature vector; The prefix enhancement prompt feature vector is processed by a prefix enhancement prompt word adapter to obtain a prefix enhancement prompt feature vector after linear adaptation.
3. The training method of a machine-generated text detection model as claimed in claim 1, characterized in that: The word embedding module includes a tagger and a word embedding matrix. The original text is input into the word embedding module to obtain the original text feature vector, including: The original text is input into the word embedding module and marked by the marker to obtain a text tag ID sequence; The text tag ID sequence is processed through the word embedding matrix to obtain the original text feature vector.
4. The method for training a machine-generated text detection model as claimed in claim 1, wherein: The prefix enhancement hint feature vector and the original text feature vector are input into the feature weighted average module to obtain the fused feature vector, including: The feature weighted averaging module performs weighted averaging on the prefix enhancement hint feature vector and the original text feature vector along the feature dimension to obtain a weighted feature vector as the first fused feature vector.
5. The method for training a machine-generated text detection model as claimed in claim 1, wherein: Based on the fused feature vector, the prediction result of the original text, and the degree of difference between the current prediction result of the model and the true label, the machine-generated text detection model is trained, including: By creating an attention mask to indicate the area that the model should focus on when processing the fused features, ignoring padding or other irrelevant parts, the first fused feature vector and the attention mask are used as input, forward propagation is performed in the model, and a data dictionary is output, where the data dictionary contains the predicted value calculated by the output module, the weighted feature vector calculated by the feature weighted average module, and the cross entropy loss calculated according to the degree of difference between the current prediction result of the model and the true label.
6. A method for detecting machine-generated text, characterized in that: in, The machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the method includes: Input the text to be detected into the prefix enhancement mapping module to obtain a second prefix enhancement prompt feature vector after linear adaptation; Input the text to be detected into the word embedding module to obtain the feature vector of the text to be detected; Inputting the prefix enhancement prompt feature vector and the feature vector of the text to be detected into the feature weighted average module to obtain a second fused feature vector; The second fused feature vector is input into the output module to obtain the prediction result of the text to be detected. The machine-generated text detection model is a model obtained by using the training method for the machine-generated text detection model described in any one of claims 1 to 5.
7. A training device for a machine-generated text detection model, characterized in that: in, The machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the device includes: A first prefix enhancement prompt feature vector acquisition module, used for inputting the original text into the prefix enhancement mapping module to obtain a first prefix enhancement prompt feature vector after linear adaptation; The original text feature vector acquisition module is used to input the original text into the word embedding module to obtain the original text feature vector; A first fused feature vector obtaining module, used for inputting the prefix enhancement prompt feature vector and the original text feature vector into a feature weighted average module to obtain a first fused feature vector; A first prediction result output module, used for inputting the first fused feature vector into the output module to obtain a prediction result of the original text; and A forward propagation module is used to train the machine-generated text detection model based on the first fused feature vector, the prediction result of the original text, and the degree of difference between the current prediction result of the model and the true label.
8. A machine-generated text detection device, characterized in that: in, The machine-generated text detection model includes a prefix enhancement mapping module, a word embedding module, a feature weighted average module and an output module, and the device includes: A second prefix enhancement prompt feature vector acquisition module, used for inputting the text to be detected into the prefix enhancement mapping module to obtain a second prefix enhancement prompt feature vector after linear adaptation; A module for obtaining a feature vector of the text to be detected is used to input the text to be detected into a word embedding module to obtain a feature vector of the text to be detected; A second fused feature vector obtaining module, used for inputting the prefix enhancement prompt feature vector and the feature vector of the text to be detected into a feature weighted average module to obtain a second fused feature vector; The second prediction result output module is used to input the second fused feature vector into the output module to obtain the prediction result of the text to be detected. Wherein, the machine-generated text detection model is a model obtained by using the training device of the machine-generated text detection model described in claim 7.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, it implements the training method of the machine-generated text detection model as described in any one of claims 1 to 5 or the machine-generated text detection method as described in claim 6.
10. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, it implements the training method of the machine-generated text detection model as described in any one of claims 1 to 5 or the machine-generated text detection method as described in claim 6.
Citation Information
Patent Citations
Natural language processing method and device based on knowledge guidance prefix fine tuning, computing equipment, and storage medium
CN113987209A
GPT generation language recognition and detection system
CN116757164A
Model training method and device, text detection method and device, storage medium and equipment
CN117744837A
Generated text source detection method based on writing style
CN118446202A
Cited By
Text training method of forward prediction algorithm
CN122451466A
A text training method of a forward prediction algorithm
CN122451466B