Energy model training method, data security detection method and system
By constructing cross-entropy loss function and contrastive loss function to adjust the parameters of the energy model, the problem of inaccurate prediction results of pre-trained models in data security detection is solved, and higher prediction accuracy and adaptability are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-24
- Publication Date
- 2026-03-31
AI Technical Summary
Pre-trained natural language models make inaccurate predictions in data security detection. The probability values and confidence levels output by the models differ greatly, making it difficult to adapt to changes in online data distribution. This results in low timeliness of risk control capabilities and difficulty in identifying abnormal online samples.
By acquiring training sample data, sample labels, and historical probability output results, we construct cross-entropy loss function and contrastive loss function, adjust the model parameters of the energy model, and improve the prediction accuracy of the model.
It improves the accuracy of natural language models in data security detection, enabling them to more accurately identify data with security risks and adapt to changes in online data distribution.
Smart Images

Figure CN115169445B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer science, and more specifically, to an energy model training method, a data security detection method, and a system. Background Technology
[0002] Pre-trained language models have made significant progress in Natural Language Understanding (NLU), playing a crucial role in applications such as text content security, image content security, and intelligent writing. However, pre-trained models often suffer from inaccurate predictions in downstream applications, such as a large discrepancy between the model's output probability value and its confidence level.
[0003] Taking content security detection as an example, the threshold of the content security model is set at 0.7, but the accuracy of the selected risk samples is often lower than 0.7. Sometimes, the model output probabilities for both risk and normal samples are close to 0.9, making it difficult for operations to adjust the online model's threshold based on the model output. Furthermore, as the online model runs, the distribution of online data often changes drastically. Traditionally trained models struggle to detect these changes, making it difficult for the content security model to identify abnormal samples in real time. This can lead to the model outputting extremely high risk probabilities for normal samples, reducing the efficiency of the review process. Simultaneously, model updates lag significantly behind changes in online distribution, greatly reducing the timeliness of risk control capabilities and making it difficult to address new online risks.
[0004] There is currently no effective solution to the above problems. Summary of the Invention
[0005] This application provides an energy model training method, a data security detection method, and a system to at least address the technical problem of low prediction accuracy of pre-trained natural language models used for data security detection.
[0006] According to one aspect of the embodiments of this application, an energy model training method is provided, comprising: acquiring training sample data, sample labels, and historical probability output results corresponding to historical data; inputting the training sample data into the energy model to be trained to obtain a first probability output result corresponding to the training sample data; determining a target loss function based on the first probability output result, sample labels, and historical probability output results, wherein the target loss function includes at least: a cross-entropy loss function and a contrastive loss function; and adjusting the model parameters of the energy model using the target loss function to obtain a target energy model.
[0007] According to another aspect of the embodiments of this application, a data security detection method is also provided, comprising: acquiring data to be detected; inputting the data to be detected into a target energy model, predicting the confidence level of the data to be detected as having a security risk through the target energy model, wherein the target energy model is trained using the energy model training method described above; determining a security detection result based on the confidence level, wherein when the confidence level is higher than a preset threshold, the security detection result is determined to be that the data to be detected has a security risk.
[0008] According to another aspect of the embodiments of this application, a data security detection method is also provided, comprising: calling a first interface to send data to be detected to a cloud server, wherein the cloud server is used to call a target energy model to predict the confidence level of the security risk of the data to be detected, and to determine the security detection result based on the confidence level, wherein the target energy model is trained using the energy model training method described above; and calling a second interface to receive the security detection result fed back by the cloud server.
[0009] According to another aspect of the embodiments of this application, a data security detection method is also provided, comprising: calling a first interface to receive data to be detected sent by a client; calling a target energy model to predict the confidence level of the security risk of the data to be detected, and determining the security detection result based on the confidence level, wherein the target energy model is trained using the energy model training method described above; and calling a second interface to feed back the security detection result to the client.
[0010] According to another aspect of the embodiments of this application, a data security detection system is also provided, comprising: a receiving device for receiving data to be detected input by a target object; a detection device for inputting the data to be detected into a built-in target energy model, predicting the confidence level of the data to be detected as having a security risk through the target energy model, and determining the security detection result based on the confidence level, wherein the target energy model is trained using the energy model training method described above; and an alarm device for issuing an alarm message when the confidence level is higher than a preset threshold, the alarm message being used to indicate that the data to be detected has a security risk.
[0011] According to another aspect of the embodiments of this application, a computer-readable storage medium is also provided, the computer-readable storage medium including a stored program, wherein, when the program is run by a processor, it controls the device where the computer-readable storage medium is located to execute the above-described energy model training method or data security detection method.
[0012] According to another aspect of the embodiments of this application, an electronic device is also provided, the electronic device including: a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the above-described energy model training method or data security detection method through the computer program.
[0013] In this embodiment, when training the target energy model, firstly, training sample data, sample labels, and historical probability output results corresponding to historical data are obtained; then, the training sample data is input into the energy model to be trained to obtain a first probability output result corresponding to the training sample data; next, a target loss function is determined based on the first probability output result, sample labels, and historical probability output results. This target loss function includes at least a cross-entropy loss function and a contrastive loss function; finally, the model parameters of the energy model are adjusted using the target loss function to obtain the target energy model. The contrastive loss function, constructed with reference to historical probability output results, plays a crucial regularization role in detecting uneven distribution and calibrating expected errors in the target energy model, effectively improving the accuracy of the target energy model's prediction results. This solves the technical problem of low prediction accuracy in pre-trained natural language models used for data security detection. Attached Figure Description
[0014] 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:
[0015] Figure 1 This is a hardware structure block diagram of an optional computer terminal according to an embodiment of this application;
[0016] Figure 2 This is a flowchart illustrating an optional energy model training method according to an embodiment of this application;
[0017] Figure 3 This is a schematic diagram of an optional model training process according to an embodiment of this application;
[0018] Figure 4 This is a flowchart illustrating an optional data security detection method according to an embodiment of this application;
[0019] Figure 5 This is a schematic diagram of an optional data security detection front-end display interface according to an embodiment of this application;
[0020] Figure 6 This is a flowchart illustrating another optional data security detection method according to an embodiment of this application;
[0021] Figure 7 This is a flowchart illustrating another optional data security detection method according to an embodiment of this application;
[0022] Figure 8 This is a schematic diagram of an optional energy model training device according to an embodiment of this application;
[0023] Figure 9 This is a schematic diagram of the structure of an optional data security detection system according to an embodiment of this application;
[0024] Figure 10 This is a schematic diagram of the structure of an optional electronic device according to an embodiment of this application. Detailed Implementation
[0025] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.
[0026] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0027] First, some nouns or terms that appear in the description of the embodiments of this application shall be interpreted as follows:
[0028] Energy Based Model (EBM): A probabilistic model, its basic form is a power function. It captures the dependencies between variables by applying a range-bound energy to each configuration of the variables. The energy model has two main tasks: inference, which primarily involves finding the configurations of latent variables that minimize the energy value given the observed variables; and learning, which primarily involves finding an appropriate energy function such that the energy of correct inputs and outputs in the sample is lower than the energy of incorrect inputs and outputs.
[0029] Out-of-Distribution Detection (OOD Detection): Using model outputs such as the maximum probability of the label and the log-likelihood function, it detects whether the current sample is consistent with the distribution of the training set samples, which is crucial to ensuring the reliability and security of machine learning systems.
[0030] Expected Calibration Error (ECE): Model calibration aims to ensure that the predicted probabilities of the model align with the actual empirical probabilities, and the accuracy of the calibration should correspondingly match the confidence level. When a deep model exhibits a mismatch between accuracy and confidence during the learning process, a reasonable evaluation metric, namely the expected calibration error, is needed to describe the degree of matching between the model's learning and its actual confidence level.
[0031] Cross Entropy Loss (CEL): Cross entropy is used to evaluate the distance between two sample distributions. It can be used to evaluate the difference between the probability distribution obtained from training and the true distribution. In other words, the cross entropy loss function is used to identify the distance between the actual output (probability) and the expected output (probability). The smaller the cross entropy value, the closer the two probability distributions are.
[0032] Contrastive Loss (CL) is primarily used in dimensionality reduction. It determines whether two originally similar samples remain similar in the feature space after dimensionality reduction (feature extraction), or whether originally dissimilar samples remain dissimilar. This loss function effectively expresses the degree of matching between samples. The contrastive loss functions in this application include unconditional contrastive loss and conditional contrastive loss.
[0033] Software as a Service (SaaS): This refers to providing software services over the internet. In the industry, SaaS is also known as software operation or simply software operation. SaaS platform providers deploy application software on their own servers. Customers can order the required application software services from the provider via the internet based on their actual work needs, paying according to the amount and duration of the service ordered, and receiving the services provided by the SaaS platform provider through the internet. SaaS application software has three models: free, paid, and value-added. Paid services are usually "all-inclusive," covering the usual application software license fee, software maintenance fee, and technical support fee, all bundled into a monthly rental fee for each user.
[0034] Example 1
[0035] According to an embodiment of this application, an energy model training method is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, 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.
[0036] The methods and embodiments provided in this application can be executed on a mobile terminal, a computer terminal, or a similar computing device. Taking running on a computer terminal as an example, Figure 1 This is a hardware structure block diagram of a computer terminal used to implement an energy model training method according to an embodiment of this application. Figure 1 As shown, the computer terminal 10 may include one or more (only one is shown in the figure) processors 102 (processors 102 may include, but are not limited to, microprocessors MCUs or programmable logic devices FPGAs), a memory 104 for storing data, and a transmission module 106 for communication functions. Those skilled in the art will understand that... Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the aforementioned electronic device. For example, computer terminal 10 may also include... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.
[0037] It should be noted that the aforementioned one or more processors 102 and / or other data processing circuits are generally referred to herein as "data processing circuits". These data processing circuits may be embodied, in whole or in part, in software, hardware, firmware, or any other combination thereof. Furthermore, the data processing circuits may be a single, independent processing module, or may be integrated, in whole or in part, into any other element within the computer terminal 10. As involved in the embodiments of this application, the data processing circuits serve as processor control (e.g., selection of a variable resistor termination path connected to an interface).
[0038] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / modules corresponding to the energy model training method in this embodiment. The processor 102 executes various functional applications and data processing by running the software programs and modules stored in the memory 104, thereby implementing the above-mentioned vulnerability detection method for the application. The memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the computer terminal 10 via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0039] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by the communication provider of the computer terminal 10. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be a Radio Frequency (RF) module, used for wireless communication with the Internet.
[0040] The display may be, for example, a touchscreen liquid crystal display (LCD) that allows the user to interact with the user interface of the computer terminal 10.
[0041] It should be noted here that, in some optional embodiments, the above... Figure 1 The computer device (or mobile device) shown may include hardware elements (including circuitry), software elements (including computer code stored on a computer-readable medium), or a combination of both hardware and software elements. It should be noted that... Figure 1 This is only one instance of a particular specific instance, and is intended to illustrate the types of components that may exist in the aforementioned computer devices.
[0042] Under the above operating environment, this application provides an energy model training method. Figure 2 This is a flowchart illustrating the energy model training method according to an embodiment of this application, as shown below. Figure 2 As shown, the method may include steps S202-S208, wherein:
[0043] Step S202: Obtain training sample data, sample labels, and historical probability output results corresponding to historical data.
[0044] Depending on the application scenario of the energy model, the training sample data can be corresponding text data, image data, etc., and these training sample data usually satisfy a joint distribution; the sample labels can be used to identify whether there are security risks in the training sample data.
[0045] Historical data is typically stored in a target queue on a local or cloud server. This target queue can be a data buffer with a FIFO (First In First Out) structure. The target queue also stores historical probability output results corresponding to the historical data.
[0046] Step S204: Input the training sample data into the energy model to be trained to obtain the first probability output result corresponding to the training sample data.
[0047] Optionally, the energy model to be trained includes at least a pre-trained encoder and a classifier. The pre-trained encoder can be BERT (Bidirectional Encoder Representation from Transformers), a pre-trained language representation model that uses MLM (Masked Language Model) to pre-train bidirectional Transformers to generate deep bidirectional language representations. After pre-training, only an additional output layer needs to be added for fine-tuning to achieve excellent performance in various downstream tasks. The classifier can use the commonly used softmax function for classification output.
[0048] As an optional implementation, when inputting training sample data into the energy model to be trained and obtaining the first probability output result corresponding to the training sample data, it can be divided into the following two steps: First, input the training sample data into the pre-trained encoder to obtain the embedding feature vector corresponding to the training sample data; then, input the embedding feature vector into the classifier to obtain the first probability output result (logits) corresponding to the training sample data.
[0049] Optionally, after obtaining the first probability output result, the first probability output result can be stored in the target queue.
[0050] Step S206: Determine the target loss function based on the first probability output result, sample label and historical probability output result, wherein the target loss function includes at least: cross-entropy loss function and contrastive loss function.
[0051] The contrastive loss function includes unconditional contrastive loss function and conditional contrastive loss function, which can play an important role in the regularization of OOD detection and ECE calibration of energy models, thereby improving the accuracy of energy model prediction results.
[0052] As an optional implementation, the following process can be used to determine the target loss function: determine the cross-entropy loss function based on the first probability output and the sample labels; determine the unconditional contrast loss function based on the first probability output and the historical probability output; determine the conditional contrast loss function based on the first probability output, the sample labels, and the historical probability output; and combine the cross-entropy loss function, the unconditional contrast loss function, and the conditional contrast loss function to obtain the target loss function.
[0053] Optionally, when combining the cross-entropy loss function, the unconditional contrast loss function, and the conditional contrast loss function to obtain the target loss function, the first weight coefficient of the cross-entropy loss function, the second weight coefficient of the unconditional contrast loss function, and the third weight coefficient of the conditional contrast loss function can be determined first; then, the cross-entropy loss function, the unconditional contrast loss function, and the conditional contrast loss function can be summed according to the first weight coefficient, the second weight coefficient, and the third weight coefficient to obtain the target loss function.
[0054] Assuming the training sample data and sample labels satisfy a two-dimensional joint distribution p(x, y), then the log-likelihood function value reflecting the first probability output of the energy model is determined as follows:
[0055] logp θ (x, y) = α[logp] θ (x)+logp θ [y|x)]+(1-α)[logp θ (y)+logp θ (x|y)]
[0056] =α[logp θ (x)+logp θ [y|x)]+(1-α)logp θ (x|y)+const
[0057] Determine the cross-entropy loss function:
[0058]
[0059] Among them, E pdata(x,y) f represents the expectation of training sample data and sample labels p(x, y). θ(x) represents the probability output result for the input sample data x, and [y] represents the sample label corresponding to the input sample data x. In the denominator, y represents the set of sample labels.
[0060] Since parameter estimation of energy models is difficult for high-dimensional discrete data, this application proposes a contrastive learning scheme based on historical data to approximate the log-likelihood function value of the energy model, resulting in an unconditional contrastive loss function:
[0061]
[0062] Where N represents the total number of training samples, h θ (x)=∑ y exp(f θ (x)[y]) represents the energy function of the unconditional likelihood function.
[0063] Determine the conditional contrastive loss function:
[0064]
[0065] Assuming the first and second weighting coefficients are the same, both being α, and the third weighting coefficient is 1-α, then the final target loss function is determined as follows:
[0066]
[0067] Step S208: Adjust the model parameters of the energy model using the target loss function to obtain the target energy model.
[0068] As an alternative implementation, training sample data can be input into the energy model for iterative training. During the iterative training process, the model parameters of the energy model are adjusted by minimizing the target loss function to obtain the target energy model.
[0069] Optionally, during iterative training, after adjusting the model parameters of the energy model using the target loss function, uneven distribution detection and expected error calibration can be performed on the energy model; when the energy model passes uneven distribution detection and expected error calibration, the energy model corresponding to the current model parameters is determined to be the target energy model.
[0070] Figure 3This diagram illustrates an optional model training process. First, training sample data is input into a pre-trained encoder to obtain corresponding embedded feature vectors. Then, these embedded feature vectors are input into a classifier to obtain the corresponding first probability output, i.e., 1-K in the diagram. Next, a cross-entropy loss function is determined based on the first probability output and the sample labels. Then, an unconditional contrastive loss function is determined based on the first probability output and the historical probability outputs in the queue. Finally, a conditional contrastive loss function is determined based on the first probability output, the sample labels, and the historical probability outputs in the queue. The cross-entropy loss function, the unconditional contrastive loss function, and the conditional contrastive loss function are combined to obtain the target loss function. Finally, the model parameters of the energy model are adjusted using the target loss function to obtain the target energy model.
[0071] In this embodiment, when training the target energy model, firstly, training sample data, sample labels, and historical probability output results corresponding to historical data are obtained; then, the training sample data is input into the energy model to be trained to obtain a first probability output result corresponding to the training sample data; next, a target loss function is determined based on the first probability output result, sample labels, and historical probability output results. This target loss function includes at least a cross-entropy loss function and a contrastive loss function; finally, the model parameters of the energy model are adjusted using the target loss function to obtain the target energy model. The contrastive loss function, constructed with reference to historical probability output results, plays a crucial regularization role in detecting uneven distribution and calibrating expected errors in the target energy model, effectively improving the accuracy of the target energy model's prediction results. This solves the technical problem of low prediction accuracy in pre-trained natural language models used for data security detection.
[0072] Example 2
[0073] Based on the target energy model obtained in Example 1, this application also provides a data security detection method. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than that shown here.
[0074] The data security detection method proposed in this application embodiment can also run in the operating environment of the computer terminal 10 provided in Embodiment 1, such as... Figure 4 As shown, the data security detection method includes steps S402-S406, wherein:
[0075] Step S402: Obtain the data to be detected.
[0076] Optionally, the terminal can obtain the data to be detected input by the user from the front-end interactive interface. The type of the data to be detected can be text data or image data, etc.
[0077] Step S404: Input the data to be detected into the target energy model, and predict the confidence level of the data to be detected as having a security risk through the target energy model. The target energy model is trained using the energy model training method in Example 1.
[0078] Specifically, the training process of the target energy model includes: acquiring training sample data, sample labels, and historical probability output results corresponding to historical data; inputting the training sample data into the energy model to be trained to obtain the first probability output result corresponding to the training sample data; determining the target loss function based on the first probability output result, sample labels, and historical probability output results, wherein the target loss function includes at least: a cross-entropy loss function and a contrastive loss function; and adjusting the model parameters of the energy model using the target loss function to obtain the target energy model.
[0079] The contrastive loss function includes an unconditional contrastive loss function and a conditional contrastive loss function. When determining the target loss function, the cross-entropy loss function can be determined based on the first probability output result and the sample label; the unconditional contrastive loss function can be determined based on the first probability output result and the historical probability output result; the conditional contrastive loss function can be determined based on the first probability output result, the sample label, and the historical probability output result; and the target loss function is obtained by combining the cross-entropy loss function, the unconditional contrastive loss function, and the conditional contrastive loss function.
[0080] By constructing a contrastive loss function by referencing historical probability outputs, it can play an important regularization role in the OOD detection and ECE calibration of the energy model, improve the accuracy of the energy model prediction results, and thus output a more reliable confidence level that the data to be detected has a safety risk.
[0081] Step S406: Determine the security detection result based on the confidence level, wherein when the confidence level is higher than a preset threshold, the security detection result is determined to indicate that the data to be detected has a security risk.
[0082] Figure 5 A schematic diagram of the terminal's front-end display interface during an optional data security detection process is shown, such as... Figure 5As shown, the input area is used for users to input the content to be detected, such as a piece of text information. After obtaining the content to be detected, the terminal responds to the user's click of the "Detect" button, calls the trained target energy model to predict the confidence level of the security risk of the data to be detected, and displays the detection result in the result display area. Result display area 1 can display the final security detection result, such as "Data has security risk" or "Data does not have security risk," while result display area 2 can also display the predicted confidence level of the security risk of the data to be detected or intermediate probability results for user reference.
[0083] In this embodiment, the data to be detected is first acquired, then input into a target energy model. The target energy model predicts the confidence level of the data's security risk, and the security detection result is determined based on the confidence level. Specifically, if the confidence level is higher than a preset threshold, the security detection result is determined as the data posing a security risk. Because the trained target energy model has high prediction accuracy, it can effectively identify data with security risks.
[0084] Example 3
[0085] Based on the target energy model obtained in Example 1, this application also provides another data security detection method. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than that shown here.
[0086] The data security detection method proposed in this application can be applied to SaaS services and is mainly implemented by the client, such as... Figure 6 As shown, the data security detection method includes steps S602-S604, wherein:
[0087] Step S602: Call the first interface to send the data to be detected to the cloud server. The cloud server is used to call the target energy model to predict the confidence level of the security risk of the data to be detected, and determine the security detection result based on the confidence level. The target energy model is trained using the energy model training method in Example 1.
[0088] Specifically, the training process of the target energy model includes: acquiring training sample data, sample labels, and historical probability output results corresponding to historical data; inputting the training sample data into the energy model to be trained to obtain the first probability output result corresponding to the training sample data; determining the target loss function based on the first probability output result, sample labels, and historical probability output results, wherein the target loss function includes at least: a cross-entropy loss function and a contrastive loss function; and adjusting the model parameters of the energy model using the target loss function to obtain the target energy model.
[0089] The contrastive loss function includes an unconditional contrastive loss function and a conditional contrastive loss function. When determining the target loss function, the cross-entropy loss function can be determined based on the first probability output result and the sample label; the unconditional contrastive loss function can be determined based on the first probability output result and the historical probability output result; the conditional contrastive loss function can be determined based on the first probability output result, the sample label, and the historical probability output result; and the target loss function is obtained by combining the cross-entropy loss function, the unconditional contrastive loss function, and the conditional contrastive loss function.
[0090] By constructing a contrastive loss function by referencing historical probability output results, it can play an important regularization role in the OOD detection and ECE calibration of the energy model, improve the accuracy of the energy model prediction results, and thus obtain more reliable data security detection results.
[0091] Step S604: Call the second interface to receive the security detection results fed back by the cloud server.
[0092] In this embodiment, the client calls a first interface to send the data to be detected to a cloud server. The cloud server then uses a target energy model to predict the confidence level of the security risk in the data to be detected and determines the security detection result based on the confidence level. The client then calls a second interface to receive the security detection result fed back by the cloud server. Because the prediction results of the trained target energy model have high accuracy, it can effectively identify data with security risks.
[0093] Example 4
[0094] Based on the target energy model obtained in Example 1, this application also provides another data security detection method. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than that shown here.
[0095] The data security detection method proposed in this application can also be applied to SaaS services, mainly implemented by cloud servers, such as... Figure 7As shown, the data security detection method includes steps S702-S706, wherein:
[0096] Step S702: Call the first interface to receive the data to be detected sent by the client.
[0097] Step S704: Call the target energy model to predict the confidence level of the security risk of the data to be detected, and determine the security detection result based on the confidence level. The target energy model is trained using the energy model training method in Example 1.
[0098] Step S706: Call the second interface to send the security detection results back to the client.
[0099] Specifically, the training process of the target energy model includes: acquiring training sample data, sample labels, and historical probability output results corresponding to historical data; inputting the training sample data into the energy model to be trained to obtain the first probability output result corresponding to the training sample data; determining the target loss function based on the first probability output result, sample labels, and historical probability output results, wherein the target loss function includes at least: a cross-entropy loss function and a contrastive loss function; and adjusting the model parameters of the energy model using the target loss function to obtain the target energy model.
[0100] The contrastive loss function includes an unconditional contrastive loss function and a conditional contrastive loss function. When determining the target loss function, the cross-entropy loss function can be determined based on the first probability output result and the sample label; the unconditional contrastive loss function can be determined based on the first probability output result and the historical probability output result; the conditional contrastive loss function can be determined based on the first probability output result, the sample label, and the historical probability output result; and the target loss function is obtained by combining the cross-entropy loss function, the unconditional contrastive loss function, and the conditional contrastive loss function.
[0101] By constructing a contrastive loss function by referencing historical probability output results, it can play an important regularization role in the OOD detection and ECE calibration of the energy model, improve the accuracy of the energy model prediction results, and thus obtain more reliable data security detection results.
[0102] In this embodiment, the cloud server calls a first interface to receive the data to be detected sent by the client, calls a target energy model to predict the confidence level of the security risk of the data to be detected, and determines the security detection result based on the confidence level. The target energy model is trained using the energy model training method in Embodiment 1. Then, a second interface is called to feed the security detection result back to the client. Because the prediction accuracy of the trained target energy model is high, it can effectively identify data with security risks.
[0103] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions and modules involved are not necessarily essential to this application.
[0104] Through the above description of the embodiments, those skilled in the art can clearly understand that the energy model training method or data security detection method according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platform. Of course, it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods of the various embodiments of this application.
[0105] Example 5
[0106] According to embodiments of this application, an energy model training apparatus for implementing the energy model training method in Embodiment 1 is also provided, such as... Figure 8 As shown, the device includes at least: an acquisition module 80, an input / output module 82, a determination module 84, and an adjustment module 86, wherein:
[0107] The acquisition module 80 is used to acquire training sample data, sample labels, and historical probability output results corresponding to historical data.
[0108] Depending on the application scenario of the energy model, the training sample data can be corresponding text data, image data, etc., and these training sample data usually satisfy a joint distribution; the sample labels can be used to identify whether the training sample data has security risks. Historical data is usually stored in a target queue on a local or cloud server. This target queue can be a data buffer with a FIFO structure, and it also stores the historical probability output results corresponding to the historical data.
[0109] The input / output module 82 is used to input training sample data into the energy model to be trained and obtain the first probability output result corresponding to the training sample data.
[0110] Optionally, the energy model to be trained includes at least: a pre-trained encoder and a classifier. The pre-trained encoder can be BERT, a pre-trained language representation model that uses MLM to pre-train bidirectional Transformers to generate deep bidirectional language representations. After pre-training, only an additional output layer needs to be added for fine-tuning to achieve excellent performance in various downstream tasks. The classifier can use the commonly used softmax function for classification output.
[0111] As an optional implementation, the input / output module can perform the following two steps when inputting training sample data into the energy model to be trained and obtaining the first probability output result corresponding to the training sample data: First, input the training sample data into the pre-trained encoder to obtain the embedding feature vector corresponding to the training sample data; then, input the embedding feature vector into the classifier to obtain the first probability output result (logits) corresponding to the training sample data.
[0112] Optionally, after obtaining the first probability output result, the input / output module can store the first probability output result in the target queue.
[0113] The determination module 84 is used to determine the target loss function based on the first probability output result, sample label and historical probability output result, wherein the target loss function includes at least: cross-entropy loss function and contrastive loss function.
[0114] The contrastive loss function includes unconditional contrastive loss function and conditional contrastive loss function, which can play an important role in the regularization of OOD detection and ECE calibration of energy models, thereby improving the accuracy of energy model prediction results.
[0115] As an optional implementation, the determination module may refer to the following process when determining the target loss function: determine the cross-entropy loss function based on the first probability output result and the sample label; determine the unconditional contrast loss function based on the first probability output result and the historical probability output result; determine the conditional contrast loss function based on the first probability output result, the sample label, and the historical probability output result; and combine the cross-entropy loss function, the unconditional contrast loss function, and the conditional contrast loss function to obtain the target loss function.
[0116] Optionally, when determining the target loss function by combining the cross-entropy loss function, the unconditional contrast loss function, and the conditional contrast loss function, the module can first determine the first weight coefficient of the cross-entropy loss function, the second weight coefficient of the unconditional contrast loss function, and the third weight coefficient of the conditional contrast loss function; then, according to the first weight coefficient, the second weight coefficient, and the third weight coefficient, the cross-entropy loss function, the unconditional contrast loss function, and the conditional contrast loss function are summed to obtain the target loss function.
[0117] Adjustment module 86 is used to adjust the model parameters of the energy model using the target loss function to obtain the target energy model.
[0118] As an optional implementation, the adjustment module can input training sample data into the energy model for iterative training. During the iterative training process, the model parameters of the energy model are adjusted by minimizing the target loss function to obtain the target energy model.
[0119] Optionally, during iterative training, after adjusting the model parameters of the energy model using the target loss function, uneven distribution detection and expected error calibration can be performed on the energy model; when the energy model passes uneven distribution detection and expected error calibration, the energy model corresponding to the current model parameters is determined to be the target energy model.
[0120] It should be noted that each module in the energy model training device in this embodiment corresponds one-to-one with each implementation step of the energy model training method in Embodiment 1. Since Embodiment 1 has been described in detail, some details not shown in this embodiment can be referred to Embodiment 1, and will not be elaborated further here.
[0121] Example 6
[0122] According to embodiments of this application, a data security detection system is also provided, such as... Figure 9 As shown, the system includes at least: a receiving device 90, a detection device 92, and an alarm device 94, wherein:
[0123] The receiving device 90 is used to receive the data to be detected input by the target object.
[0124] The receiving device can be an interactive device such as a touch screen, which can receive the data to be detected input by the user. The type of data to be detected can be text data or image data, etc.
[0125] The detection device 92 is used to input the data to be detected into the built-in target energy model, predict the confidence level of the safety risk of the data to be detected through the target energy model, and determine the safety detection result based on the confidence level. The target energy model is trained using the energy model training method in Example 1.
[0126] Specifically, the training process of the target energy model includes: acquiring training sample data, sample labels, and historical probability output results corresponding to historical data; inputting the training sample data into the energy model to be trained to obtain the first probability output result corresponding to the training sample data; determining the target loss function based on the first probability output result, sample labels, and historical probability output results, wherein the target loss function includes at least: a cross-entropy loss function and a contrastive loss function; and adjusting the model parameters of the energy model using the target loss function to obtain the target energy model.
[0127] The contrastive loss function includes an unconditional contrastive loss function and a conditional contrastive loss function. When determining the target loss function, the cross-entropy loss function can be determined based on the first probability output result and the sample label; the unconditional contrastive loss function can be determined based on the first probability output result and the historical probability output result; the conditional contrastive loss function can be determined based on the first probability output result, the sample label, and the historical probability output result; and the target loss function is obtained by combining the cross-entropy loss function, the unconditional contrastive loss function, and the conditional contrastive loss function.
[0128] By constructing a contrastive loss function by referencing historical probability output results, it can play an important regularization role in the OOD detection and ECE calibration of the energy model, improve the accuracy of the energy model prediction results, and thus obtain more reliable data security detection results.
[0129] Alarm device 94 is used to issue an alarm message when the confidence level is higher than a preset threshold. The alarm message is used to indicate that there is a security risk in the data to be tested.
[0130] In this embodiment of the application, the receiving device in the data security detection system receives the data to be detected input by the target object and sends it to the detection device. The detection device inputs the data to be detected into a built-in target energy model, predicts the confidence level of the security risk of the data to be detected through the target energy model, and determines the security detection result based on the confidence level. When the confidence level is higher than a preset threshold, the alarm device issues an alarm message to indicate that the data to be detected has a security risk. Since the prediction results of the trained target energy model have a high accuracy, it can effectively identify data with security risks.
[0131] Example 7
[0132] According to an embodiment of this application, a computer-readable storage medium is also provided, which includes a stored program, wherein when the program is run by a processor, it controls the device where the computer-readable storage medium is located to execute the above-described energy model training method or data security detection method.
[0133] Optionally, when the program is executed by the processor, the device containing the computer-readable storage medium executes the following steps: acquiring training sample data, sample labels, and historical probability output results corresponding to historical data; inputting the training sample data into the energy model to be trained to obtain a first probability output result corresponding to the training sample data; determining a target loss function based on the first probability output result, sample labels, and historical probability output results, wherein the target loss function includes at least: a cross-entropy loss function and a contrastive loss function; and adjusting the model parameters of the energy model using the target loss function to obtain the target energy model.
[0134] Optionally, when the program is run by the processor, it controls the device containing the computer-readable storage medium to perform the following steps: acquiring data to be detected; inputting the data to be detected into a target energy model, predicting the confidence level of the data to be detected as having a security risk through the target energy model, wherein the target energy model is trained using the energy model training method in Example 1; determining the security detection result based on the confidence level, wherein when the confidence level is higher than a preset threshold, the security detection result is determined to be that the data to be detected has a security risk.
[0135] Optionally, when the program is run by the processor, the device containing the computer-readable storage medium executes the following steps: calling a first interface to send the data to be detected to a cloud server, wherein the cloud server is used to call a target energy model to predict the confidence level of the security risk of the data to be detected, and to determine the security detection result based on the confidence level, wherein the target energy model is trained using the energy model training method in Example 1; calling a second interface to receive the security detection result fed back by the cloud server.
[0136] Optionally, when the program is run by the processor, the device containing the computer-readable storage medium executes the following steps: calling the first interface to receive the data to be detected sent by the client; calling the target energy model to predict the confidence level of the security risk of the data to be detected, and determining the security detection result based on the confidence level, wherein the target energy model is trained using the energy model training method in Example 1; and calling the second interface to feed back the security detection result to the client.
[0137] Example 9
[0138] According to embodiments of this application, an electronic device is also provided, such as... Figure 10 As shown, the electronic device includes a processor 100 and a memory 102, wherein the memory 102 is connected to the processor 100 and stores a computer program. The processor is configured to execute the energy model training method or data security detection method described above through the computer program.
[0139] Optionally, the processor can execute the following steps via a computer program: acquiring training sample data, sample labels, and historical probability output results corresponding to historical data; inputting the training sample data into the energy model to be trained to obtain a first probability output result corresponding to the training sample data; determining a target loss function based on the first probability output result, sample labels, and historical probability output results, wherein the target loss function includes at least: a cross-entropy loss function and a contrastive loss function; and adjusting the model parameters of the energy model using the target loss function to obtain the target energy model.
[0140] Optionally, the processor can execute the following steps through a computer program: acquiring the data to be detected; inputting the data to be detected into a target energy model, predicting the confidence level of the data to be detected as having a security risk through the target energy model, wherein the target energy model is trained using the energy model training method in Example 1; determining the security detection result based on the confidence level, wherein when the confidence level is higher than a preset threshold, the security detection result is determined to be that the data to be detected has a security risk.
[0141] Optionally, the processor can execute the following steps through a computer program: calling a first interface to send the data to be detected to a cloud server, wherein the cloud server is used to call a target energy model to predict the confidence level of the security risk of the data to be detected, and to determine the security detection result based on the confidence level, wherein the target energy model is trained using the energy model training method in Example 1; calling a second interface to receive the security detection result fed back by the cloud server.
[0142] Optionally, the processor can execute the following steps via a computer program: when the program is run by the processor, it controls the device containing the computer-readable storage medium to execute the following steps: calling a first interface to send the data to be detected to a cloud server, wherein the cloud server is used to call a target energy model to predict the confidence level of the security risk of the data to be detected, and to determine the security detection result based on the confidence level, wherein the target energy model is trained using the energy model training method in Example 1; calling a second interface to receive the security detection result fed back by the cloud server.
[0143] The sequence numbers of the embodiments in this application are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0144] In the above embodiments of this application, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0145] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection of units or modules may be electrical or other forms.
[0146] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0147] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0148] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.
[0149] The above are merely preferred embodiments of this application. It should be noted that those skilled in the art can make various improvements and modifications without departing from the principles of this application, and these improvements and modifications should also be considered within the scope of protection of this application.
Claims
1. An energy model training method, characterized by, The method comprises: obtaining training sample data, sample labels, and historical probability output results corresponding to historical data; inputting the training sample data into an energy model to be trained to obtain first probability output results corresponding to the training sample data; determining a target loss function based on the first probability output results, the sample labels, and the historical probability output results, wherein the target loss function at least includes a cross-entropy loss function and a contrastive loss function; adjusting model parameters of the energy model using the target loss function to obtain a target energy model; obtaining to-be-detected data, wherein the to-be-detected data includes at least one of text data and image data; inputting the to-be-detected data into the target energy model to predict a security detection result of the to-be-detected data by the target energy model, wherein the security detection result is used to indicate whether the to-be-detected data has a security risk. The method further comprises:
2. The method of claim 1, wherein, determining an unconditional contrastive loss function based on the first probability output results and the historical probability output results, determining a conditional contrastive loss function based on the first probability output results, the sample labels, and the historical probability output results, and determining the target loss function by combining the cross-entropy loss function. The method further comprises: determining the cross-entropy loss function based on the first probability output results and the sample labels; 3. The method of claim 2, wherein, combining the cross-entropy loss function, the unconditional contrastive loss function, and the conditional contrastive loss function to obtain the target loss function. The method further comprises: determining a first weight coefficient of the cross-entropy loss function, a second weight coefficient of the unconditional contrastive loss function, and a third weight coefficient of the conditional contrastive loss function; 4. The method of claim 1, wherein, summing the cross-entropy loss function, the unconditional contrastive loss function, and the conditional contrastive loss function according to the first weight coefficient, the second weight coefficient, and the third weight coefficient to obtain the target loss function. The energy model at least includes a pre-trained encoder and a classifier, and inputting the training sample data into the energy model to be trained to obtain the first probability output results corresponding to the training sample data comprises: inputting the training sample data into the pre-trained encoder to obtain an embedding feature vector corresponding to the training sample data; inputting the embedding feature vector into the classifier to obtain the first probability output results corresponding to the training sample data.
5. The method of claim 4, wherein, After obtaining the first probability output result corresponding to the training sample data, the method further comprises: storing the first probability output result into a target queue, wherein the historical probability output result is also stored in the target queue.
6. The method of claim 1, wherein, adjusting the model parameters of the energy model by using the target loss function to obtain a target energy model, comprising: inputting the training sample data into the energy model for iterative training, and adjusting the model parameters of the energy model by minimizing the target loss function during the iterative training to obtain the target energy model.
7. The method of claim 1, wherein, After adjusting the model parameters of the energy model by using the target loss function, the method further comprises: performing allocation unevenness detection and expected error calibration on the energy model; determining that the energy model is the target energy model when the energy model passes the allocation unevenness detection and the expected error calibration.
8. A data security detection method, characterized by, comprising: obtaining to-be-detected data, wherein the to-be-detected data comprises at least one of text data and image data; inputting the to-be-detected data into a target energy model to predict a confidence degree of a security risk existing in the to-be-detected data by using the target energy model, wherein the target energy model is trained by using the energy model training method in any one of claims 1 to 7; determining a security detection result based on the confidence degree, wherein when the confidence degree is higher than a preset threshold, it is determined that the security detection result is that a security risk exists in the to-be-detected data.
9. A data security detection method, characterized by, comprising: calling a first interface to send to-be-detected data to a cloud server, wherein the to-be-detected data comprises at least one of text data and image data, and the cloud server is configured to call a target energy model to predict a confidence degree of a security risk existing in the to-be-detected data, and determine a security detection result based on the confidence degree, wherein the target energy model is trained by using the energy model training method in any one of claims 1 to 7; calling a second interface to receive the security detection result fed back by the cloud server.
10. A data security detection method, characterized by, comprising: calling a first interface to receive to-be-detected data sent by a client, wherein the to-be-detected data comprises at least one of text data and image data; calling a target energy model to predict a confidence degree of a security risk existing in the to-be-detected data, and determining a security detection result based on the confidence degree, wherein the target energy model is trained by using the energy model training method in any one of claims 1 to 7; calling a second interface to feed back the security detection result to the client.
11. A data security detection system, characterized by, comprising: a receiving device configured to receive to-be-detected data input by a target object, wherein the to-be-detected data comprises at least one of text data and image data; a detection device configured to input the to-be-detected data into a built-in target energy model to predict a confidence degree of a security risk existing in the to-be-detected data by using the target energy model, and determine a security detection result based on the confidence degree, wherein the target energy model is trained by using the energy model training method in any one of claims 1 to 7. An alarm device is configured to send an alarm information when the confidence is higher than a preset threshold, and the alarm information is used to prompt that the to-be-detected data has a security risk.
12. A computer-readable storage medium, characterized in that, The computer readable storage medium comprises a stored program, wherein the program, when executed by a processor, controls a device where the computer readable storage medium is located to perform the energy model training method in any one of claims 1 to 7 or the data security detection method in any one of claims 8 to 10.
13. An electronic device, comprising: Comprise: A memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the energy model training method in any one of claims 1 to 7 or the data security detection method in any one of claims 8 to 10 through the computer program.
Citation Information
Patent Citations
Abnormality detection system and abnormality detection method
CN114328639A