Training method and device of neural network model for classification, equipment and medium
Through the combination of adversarial samples and random forgetting mechanisms, adversarial training methods solve the problem of the accuracy of training sets and test sets in the existing technology, significantly improving the generalization and anti-interference ability of neural network models, and are suitable for images and text classification tasks.
Patent Information
- Application Number
- CN202411060813.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-02
- Publication Date
- 2024-12-20
AI Technical Summary
The existing adversarial training methods only optimize the training set during the training process, resulting in the unsynchronized improvement of the accuracy of the test set and there is overfitting.
A training method for neural network model used for classification is adopted. By generating adversarial samples and random forgetting mechanisms, the model is trained multiple times based on training samples and adversarial samples, random forgetting is performed when the cardinality is preset, and calculation is performed when the cardinality is not cardinality multiple. Cross the loss function value until the model converges.
It significantly improves the robust generalization of the model, synchronizes the accuracy of the training set and test set, enhances the anti-interference ability of the model, and is suitable for classification tasks of images and text.
Smart Images

Figure CN119168014A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of model training technology, and in particular to a training method, device, equipment and medium for a neural network model for classification. Background Art
[0002] At present, many classification models are trained by adversarial training, which is to add small perturbations to the training data to enhance the generalization of the model. There are many adversarial training schemes, such as PGD adversarial training method.
[0003] However, existing methods usually have the problem of only optimizing the training set during the training process, but not processing the test set, so that the accuracy of the training set is improved, but the accuracy of the test set is not improved synchronously, that is, there is an overfitting phenomenon. Summary of the invention
[0004] To solve the above problems, the present application provides a training method, apparatus, device and medium for a neural network model for classification, so as to overcome or at least partially overcome the shortcomings of the prior art.
[0005] This application adopts the following technical solutions:
[0006] In a first aspect, the present application provides a training method for a neural network model for classification, comprising:
[0007] Generate an adversarial sample based on a training sample, where the training sample is an image sample or a text sample;
[0008] Initializing a model to be trained, where the model to be trained is a neural network model for classification;
[0009] Based on the training samples and the adversarial samples, the initialized model to be trained is trained for multiple rounds, and when the current training round is an integer multiple of a preset base number, the current round model is reset based on a random forgetting coefficient;
[0010] When the current training round is not an integer multiple of the preset cardinality, the current round model is aligned with the initialized model to be trained to calculate the loss function value, and the neural network model updates the current round model according to the obtained loss function value until the training end condition is met to obtain a trained neural network model for classification.
[0011] Optionally, in the above-mentioned training method of the neural network model for classification, the initializing the model to be trained includes:
[0012] The pre-trained neural network model for classification is used as the initialized model to be trained.
[0013] Optionally, in the above-mentioned training method of the neural network model for classification, the initializing the model to be trained includes:
[0014] Determine the cross loss entropy of the current training round of the model to be trained based on the training sample and the adversarial sample;
[0015] Determine the convergence gradient based on the cross-loss entropy between the current training round and the previous training round;
[0016] The model to be trained in the current training round is updated according to the converged gradient until the end condition of the pre-training is met, thereby completing the initialization of the model to be trained.
[0017] Optionally, in the above-mentioned training method for a neural network model for classification, resetting the current round model based on a random forgetting coefficient includes:
[0018] Randomly select a random forgetting coefficient, where the random forgetting coefficient is any value between zero and one;
[0019] Reset the current round model according to the following formula:
[0020] f φ =αf φ +(1-α)f θ ;
[0021] Among them, f φ is the current round model, α is the random forgetting coefficient, f θ is the model to be trained after initialization.
[0022] Optionally, in the above-mentioned training method of the neural network model for classification, aligning the current round model with the initialized model to be trained to calculate the loss function value, and updating the current round model according to the obtained loss function value, includes:
[0023] The cross loss function value between the current round model and the initialized model to be trained is calculated according to the following formula:
[0024] L FOMO =λ 1 D KL (f θ (x)||f φ (x))+λ 2 D KL (f θ (x+δ)||f φ (x+δ))+L CR (f θ (x),y)+L CR (f θ (x+δ),y);
[0025] Among them, L FOMO is the cross loss function, λ 1 and λ 2 is a hyperparameter, D KL is the KL divergence, f φ is the current round model, f θ is the initialized model to be trained, L CR represents the cross loss entropy;
[0026] A convergence gradient is determined according to the cross loss entropy obtained in the current training round and the previous training round, and the current round model is updated according to the convergence gradient.
[0027] Optionally, in the above-mentioned training method of the neural network model for classification, the model to be trained is an image classification model constructed based on ResNet, or a text classification model constructed based on BERT.
[0028] In a second aspect, the present application also provides a classification method based on a neural network model, comprising:
[0029] Acquire information to be classified, where the information to be classified is image data or text data;
[0030] The information to be classified is input into a trained neural network model for classification for prediction to obtain a classification result, wherein the neural network model for classification is obtained according to any of the training methods for the neural network model for classification mentioned above.
[0031] In a third aspect, the present application further provides a training device for a neural network model for classification, the training device for a neural network model for classification comprising:
[0032] An adversarial sample generating unit, configured to generate an adversarial sample according to a training sample, wherein the training sample is an image sample or a text sample;
[0033] A model initialization unit, used to initialize a model to be trained, wherein the model to be trained is a neural network model for classification;
[0034] A first training unit is used to perform multiple rounds of training on the initialized model to be trained based on the training samples and the adversarial samples, and when the current training round is an integer multiple of a preset cardinality, the current round model is reset based on a random forgetting coefficient;
[0035] The second training unit is used to align the current round model with the initialized model to be trained to calculate the loss function value when the current training round is not an integer multiple of the preset cardinality, and the neural network model updates the current round model according to the obtained loss function value until the training end condition is met to obtain a trained neural network model for classification.
[0036] In a fourth aspect, the present application provides a computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements any of the above-mentioned training methods for a neural network model for classification, or steps of a classification method based on a neural network model.
[0037] In a fifth aspect, the present application provides a computer-readable storage medium storing a computer program, which, when instructed by a processor, implements any of the above-mentioned training methods for a neural network model for classification, or the steps of a classification method based on a neural network model.
[0038] At least one of the above technical solutions adopted in this application can achieve the following beneficial effects:
[0039] This application is aimed at the neural network model used for classification. The model to be trained is initialized so that it can be hot-started in an expected direction. Then, the initialized model to be trained is trained for multiple rounds based on training samples and adversarial samples. A base number e is preset in advance. r During the training process, whenever the current training round is a preset base number or an integer multiple of the preset base number, that is, each e r When the training round is an integer multiple of the preset base, the current round model is reset based on the random forgetting coefficient to achieve random forgetting of samples; when the current training round is not a preset base or an integer multiple of the preset base, the current round model is aligned with the initialized model to be trained to calculate the loss function value, and the current round model is back-propagated according to the obtained loss function value until the model converges. This application uses random forgetting to improve the robust generalization of the model, so that the accuracy of the training set and the accuracy of the test set are improved simultaneously, which significantly increases the generalization and anti-interference ability of the model, and is particularly suitable for classification neural network models. These models can be widely used in the classification of images and texts, such as text-based user intent classification and image-based contract classification. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0041] Figure 1 A schematic diagram showing a flow chart of a training method for a neural network model for classification according to an embodiment of the present application;
[0042] Figure 2 A schematic flow chart showing a method for training a neural network model for classification according to another embodiment of the present application;
[0043] Figure 3 A schematic diagram showing a flow chart of a classification method based on a neural network model according to an embodiment of the present application;
[0044] Figure 4 A schematic diagram showing the structure of a training device for a neural network model for classification according to an embodiment of the present application is shown;
[0045] Figure 5 A schematic diagram showing the structure of a classification device based on a neural network model according to an embodiment of the present application is shown;
[0046] Figure 6 A schematic structural diagram of a computer device according to an embodiment of the present application is shown. DETAILED DESCRIPTION
[0047] In order to make the purpose, technical solution and advantages of the present application clearer, the technical solution of the present application will be clearly and completely described below in combination with the specific embodiments of the present application and the corresponding drawings. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.
[0048] The idea of the present application is to use adversarial training method with adversarial samples, and set a cardinality in advance. Whenever the training round of the training model encounters an integer multiple of the preset cardinality, the current training model is reset by using a random forgetting coefficient; and when the training round of the training model is a round that is not divisible by the non-forgetting cardinality, the cross loss function of the current training model and the initialized training model is used for back propagation, thereby using the random forgetting mechanism to improve the robust generalization of the neural network model used for classification.
[0049] Figure 1 A flow chart of a training method for a neural network model for classification according to an embodiment of the present application is shown. Figure 1 It can be seen that this embodiment includes steps S110 to S140:
[0050] Step S110, generating adversarial samples based on training samples, wherein the training samples are image samples or text samples. The training method of the present application is applicable to any neural network model for classification, and is particularly applicable to image classification scenarios or text classification scenarios. For example, the neural network model for classification can be an image classification model built based on ResNet or a text classification model built based on BERT.
[0051] Taking the image classification model built based on ResNet as an example, the business application can be to use the ResNet image classification model to classify documents based on the layout of the file page. For example, contracts can be classified into engineering contracts, sales contracts, lease contracts, loan contracts, etc. based on the image of the contract, so as to facilitate the extraction of different key information in downstream tasks, such as extracting the construction period and scope of the project in the engineering contract; extracting the principal and interest rate in the loan contract. For another example, document images can be classified into types such as ID cards, passports, business licenses, etc. based on the page layout, so that key information such as ID card numbers can be extracted at specific locations using corresponding templates.
[0052] Taking the text classification model built based on BERT as an example, in business applications, after extracting text from documents using OCR technology, BERT can be used for text classification. Another example is that for products on an online shopping platform, their popularity can be classified based on user text reviews. Another example is that in online medical scenarios, the urgency of the patient's condition can be preliminarily classified based on the patient's text description of the condition.
[0053] The application does not impose any specific restrictions on the structure of the neural network model used for classification, which can be selected as needed; and there is no restriction on the scenarios in which the neural network model used for classification is applied.
[0054] For the sake of convenience, the following description takes the image classification model built based on ResNet as an example.
[0055] First, adversarial samples are generated based on training samples. The training samples can be freely selected based on the field in which the constructed image classification model is to be used, and sufficiently skilled samples are ready for use.
[0056] The training sample data is recorded as D, D = (x i ,y i ),i=1,...,n, where x i is the i-th feature, which can be in the form of a vector, y i is the i-th label.
[0057] Any adversarial sample algorithm in the prior art can be used to generate adversarial samples, such as PGD (Projected Gradient Descent), FGSM (Fast Gradient Sign Method), etc.
[0058] Specifically, batch data B is randomly selected from the training sample data D and denoted as (x, y). The number of batch data is the batch size, where x is the matrix of the number of features multiplied by the batch size, and the length of y is the batch size.
[0059] Use any of the above methods to generate adversarial samples B'=A(B,f θ ) is recorded as (x+δ,y), where A represents the generation algorithm of adversarial samples, and f θ Represents the model to be trained.
[0060] Step S120: Initialize the model to be trained, where the model to be trained is a neural network model for classification.
[0061] Initialize the model to be trained. The purpose of initialization is to make the model to be trained move in a roughly correct direction to achieve a hot start, rather than starting from a completely random direction. This helps the model converge quickly and improve its accuracy.
[0062] In some embodiments, the initialization of the model to be trained includes: using a pre-trained classification neural network model as the initialized model to be trained. There are many pre-trained neural network models in the prior art, and these models usually have a certain generalization ability, such as the BERT model for text classification. Using such a pre-trained model as the initialized model to be trained, and then training it with the training method of the present application, can further improve its accuracy and pertinence to the application scenario.
[0063] In other embodiments, the initialization of the model to be trained includes: determining the cross loss entropy of the current training round of the model to be trained based on the training samples and the adversarial samples; determining the convergence gradient according to the cross loss entropy between the current training round and the previous training round; updating the model to be trained in the current training round according to the convergence gradient until the end condition of the pre-training is met, thereby completing the initialization of the model to be trained.
[0064] That is, adversarial training can also be used to initialize the model to be trained. As mentioned above, the model to be trained is recorded as f θ For any training round, we can use training sample B and adversarial sample B' to calculate the model f θ The cross loss entropy value L 总 , where the cross loss entropy function refers to the following formula:
[0065] L 总 =L CR (f θ (x),y)+L CR (f θ (x+δ),y);
[0066] Among them, L 总 Represents the computational model f θ The cross loss entropy value, L CR represents the cross-loss entropy function.
[0067] Then calculate the convergence gradient of two adjacent training rounds, that is, the convergence gradient of the current training round and the previous training round, and back propagate according to the obtained convergence gradient to update the model to be trained. θ .
[0068] After several rounds of adversarial training, the end condition of pre-training is met, and the initialized model to be trained is obtained, which is still recorded as f θ This startup method can be understood as a hot start, and the number of rounds of adversarial training can be recorded as e warm-up , after e warm-up After 1 round, the initialized model to be trained is obtained. This hot start method can make a model move in a roughly correct direction instead of starting from a completely random direction, which is more conducive to rapid model training and accuracy improvement.
[0069] After initialization, let f φ =f θ , where f θ is the initialized model to be trained, f φ is the model of the current training round, and the model to be trained f is to be initialized here θ The model to be trained as the first round of subsequent model training f φ .
[0070] Step S130: Based on the training samples and the adversarial samples, the initialized model to be trained is trained for multiple rounds. When the current training round is an integer multiple of a preset base, the current round model is reset based on a random forgetting coefficient.
[0071] Step S140: When the current training round is not an integer multiple of the preset cardinality, the current round model is aligned with the initialized model to be trained to calculate the loss function value, and the neural network model updates the current round model according to the obtained loss function value until the training end condition is met, thereby obtaining a trained neural network model for classification.
[0072] After the model to be trained is initialized, the training samples and adversarial samples are used to train the initialized model for multiple rounds. In this training process, a random forgetting mechanism is added, and a base number e can be set in advance. r , this cardinality e r It has a default value, and users can also set it according to their needs. r It represents the frequency of forgetting. The smaller it is, the more frequently it is forgotten, and of course more rounds are needed to train well. r 3 or 5 and so on.
[0073] In e warm-up In the subsequent training rounds, whenever the current training round is an integer multiple of the preset base number, that is, each e r When the number of rounds is a multiple of , a random forgetting step is taken, that is, the current round model is reset based on the random forgetting coefficient.
[0074] Specifically, in some embodiments of the present application, resetting the current round model based on the random forgetting coefficient includes: randomly selecting a random forgetting coefficient, where the random forgetting coefficient is any value between zero and one; resetting the current round model according to the following formula:
[0075] f φ =αf φ +(1-α)f θ ;
[0076] Among them, f φ is the current round model, α is the random forgetting coefficient, which is any value between zero and one, and f θ is the model to be trained after initialization.
[0077] That is, we can randomly take a random forgetting coefficient with a ratio of α to reinitialize the current model, that is, θ Reset to αf φ +(1-α)f θ , where f θ is the current round model, α is the random forgetting coefficient, f θ is the model to be trained after initialization.
[0078] The random forgetting step is set to control the initialization of the training model f in the final trained model. φ The model adopting the forgetting mechanism θ The larger the ratio α is, the higher the degree of forgetting is, and more rounds are needed for training. α is a configurable parameter that can be specified by technical personnel. It is any value between zero and one, and can be selected by comprehensively considering accuracy and training cost.
[0079] Whenever the current training round is not an integer multiple of the preset cardinality, the current round model is aligned with the initialized model to be trained to calculate the loss function value, and the current round model is updated according to the obtained loss function value.
[0080] That is, not e r When the training rounds are divisible by integers, the aforementioned training samples and adversarial samples are still used to calculate the loss function and perform back propagation.
[0081] Specifically, in some embodiments, aligning the current round model with the initialized model to be trained to calculate the loss function value, and updating the current round model according to the obtained loss function value includes: calculating the cross loss function value between the current round model and the initialized model to be trained according to the following formula:
[0082] L FOMO =λ 1 D KL (f θ (x)||f φ (x))+λ 2 D KL (f θ (x+δ)||f φ (x+δ))+L CR (f θ (x), y)+L CR (f θ (x+δ), y);
[0083] Among them, L FOMO is the cross loss function, λ 1 and λ 2 is a hyperparameter, D KL is the KL divergence, f θ is the current round model, f θ is the initialized model to be trained, L CR represents the cross loss entropy; a convergence gradient is determined according to the cross loss entropy obtained in the current training round and the previous training round, and the current round model is updated according to the convergence gradient.
[0084] The initialized model to be trained f θ With the current round model f φ The alignment loss function value can be calculated as follows:
[0085] L FOMO =λ 1 D KL (f θ (x)||f φ (x))+λ 2 D KL (fθ (x+δ)||f φ (x+δ))+L CR (f θ (x), y)+L CR (f θ (x+δ), y)
[0086] Among them, L FOMO represents the loss function value, λ 1 and λ 2 is a hyperparameter, D KL is the KL divergence.
[0087] In the above formula, λ 1 D KL (f θ (x)||f φ (x))+λ 2 D KL (f θ (x+δ)||f φ (x+δ)) is the regularization term, which can ensure that the model f after adding the disturbance φ And the initialized model to be trained f θ There will not be a large deviation, which can effectively prevent the overfitting of the model and improve the accuracy of the model.
[0088] Furthermore, the convergence gradient of the current training round and the previous training round is calculated. The convergence gradient is defined as the ratio of the difference between the loss function values of the current training round and the previous training round to the loss function value of the previous training round, and the current round model f is updated. φ .
[0089] The training is terminated through multiple rounds of training until the convergence criterion is reached. The present application does not limit the convergence criterion, which may be reaching a certain round, or the convergence gradient being less than a certain threshold, etc. After the training is completed, a trained neural network model for classification can be obtained.
[0090] Depend on Figure 1 It can be seen from the method shown that the present application initializes the model to be trained for the neural network model used for classification, so that it can achieve hot start in an expected direction, and then performs multiple rounds of training on the initialized model to be trained based on the training samples and adversarial samples at the same time, and a base number e is preset in advance. r During the training process, whenever the current training round is a preset base number or an integer multiple of the preset base number, that is, each e rWhen the training round is an integer multiple of the preset base, the current round model is reset based on the random forgetting coefficient to achieve random forgetting of samples; when the current training round is not a preset base or an integer multiple of the preset base, the current round model is aligned with the initialized model to be trained to calculate the loss function value, and the current round model is back-propagated according to the obtained loss function value until the model converges. This application uses random forgetting to improve the robust generalization of the model, so that the accuracy of the training set and the accuracy of the test set are improved simultaneously, which significantly increases the generalization and anti-interference ability of the model, and is particularly suitable for classification neural network models. These models can be widely used in the classification of images and texts, such as text-based user intent classification and image-based contract classification.
[0091] Figure 2 A flow chart showing a training method for a neural network model for classification according to another embodiment of the present application is shown, Figure 2 It can be seen that this embodiment includes steps S210 to S240:
[0092] Step S210, generating adversarial samples based on training samples.
[0093] Step S220, initialize the model to be trained based on the adversarial training method, and then start the following random forgetting training process for the initialized model to be trained:
[0094] Step S230, determine whether the current round is an integer multiple of the preset cardinality. If so, proceed to step S241, reset the current round model based on the randomly selected random forgetting coefficient; and proceed to step S250 to determine whether the convergence standard is met; if so, proceed to step S260 to end training; if the convergence standard is not met, return to step S230, that is, the step of determining whether the current round is an integer multiple of the preset cardinality.
[0095] If it is determined in step S230 that the current round is not an integer multiple of the preset cardinality, then enter step S242, align the current round model with the initialized model to be trained to calculate the loss function value, and update the current round model according to the obtained loss function value; and enter step S250 to determine whether the convergence standard is met. If so, go to step S260 to end the training; if the convergence standard is not met, return to step S230, that is, the step of determining whether the current round is an integer multiple of the preset cardinality.
[0096] The present application also provides a classification method based on a neural network model, that is, the neural network model obtained by the above training method is used to classify image data or text data. Figure 3A schematic flow chart of a classification method based on a neural network model according to an embodiment of the present application is shown; this embodiment includes steps S310 to S320:
[0097] Step S310: Acquire information to be classified, where the information to be classified is image data or text data.
[0098] Step S320: inputting the information to be classified into a trained neural network model for classification for prediction to obtain a classification result, wherein the neural network model for classification is obtained by any of the aforementioned training methods for the neural network model for classification.
[0099] In this application, the information to be classified is image data or text data, such as the image of the engineering contract mentioned above, or the text entered by the user in the interactive interface, etc., which are not listed here. The business scenario is the same as before and will not be repeated.
[0100] Take some actual scenarios as examples, specifically, take the application of the neural network model for classification as the image classification model ResNet in image classification as an example. The input format of ResNet training samples is (x, y), where: x is 3*224*224, where 3 is the RGB value, and 224 is the length and width of the image (some ResNets accept 224*224 image input); y is a one hot vector with a length equal to the number of categories. Let the model to be trained be a ResNet model. The output f(x) of the model is a vector with a length equal to the number of categories, each element is non-negative, and their sum is 1, which is the probability value of these categories, thereby obtaining the classification result.
[0101] For example, in some practical scenarios, in a BERT-based text classification model, the input format of the training sample is (x, y), where x can be the sum of the token embedding, position embedding and segment embedding obtained after the text passes through the tokenizer, and the feature size is the sequence length * 768, where 768 is the length of a commonly used BERT feature dimension; y is a one hot vector with a length equal to the number of categories. Let the model to be trained be a BERT model. The model output f(x) is a vector with a length equal to the number of categories, each element is non-negative, and their sum is 1, which is the probability value of these categories, thereby obtaining the classification result.
[0102] The neural network model used for classification is obtained according to the training method described in the aforementioned steps S110 to S140.
[0103] Figure 4 A schematic diagram of a training device for a neural network model for classification according to an embodiment of the present application is shown. Figure 4 It can be seen that the training device 400 for a neural network model for classification includes: an adversarial sample generating unit 410, which is used to generate adversarial samples according to training samples, wherein the training samples are image samples or text samples;
[0104] A model initialization unit 420, used to initialize a model to be trained, wherein the model to be trained is a neural network model for classification;
[0105] A first training unit 430 is used to perform multiple rounds of training on the initialized model to be trained based on the training samples and the adversarial samples, and when the current training round is an integer multiple of a preset cardinality, the current round model is reset based on a random forgetting coefficient;
[0106] The second training unit 440 is used to align the current round model with the initialized model to be trained to calculate the loss function value when the current training round is not an integer multiple of the preset cardinality, and update the current round model according to the obtained loss function value until the training end condition is met to obtain a trained neural network model for classification.
[0107] In some embodiments of the present application, in the above-mentioned training device for the neural network model for classification, the model initialization unit 420 is used to use the pre-trained neural network model for classification as the initialized model to be trained.
[0108] In some embodiments of the present application, in the above-mentioned training device for the neural network model for classification, the model initialization unit 420 is used to determine the cross loss entropy of the current training round of the model to be trained based on the training samples and the adversarial samples; determine the convergence gradient according to the cross loss entropy of the current training round and the previous training round; update the model to be trained in the current training round according to the convergence gradient until the end condition of the pre-training is met, thereby completing the initialization of the model to be trained.
[0109] In some embodiments of the present application, in the above-mentioned training device for the neural network model for classification, the first training unit is used to randomly select a random forgetting coefficient, where the random forgetting coefficient is any value between zero and one; and reset the current round model according to the following formula:
[0110] f φ =αf φ +(1-α)f θ ;
[0111] Among them, f φ is the current round model, α is the random forgetting coefficient, f θ is the model to be trained after initialization.
[0112] In some embodiments of the present application, in the above-mentioned training device for the neural network model for classification, the first training unit is used to calculate the cross loss function value between the current round model and the initialized model to be trained according to the following formula:
[0113] L FOMO =λ 1 D KL (f θ (x)||f φ (x))+λ 2 D KL (f θ (x+δ)||f φ (x+δ))+L CR (f θ (x),y)+L CR (f θ (x+δ),y);
[0114] Among them, L FOMO is the cross loss function, λ 1 and λ 2 is a hyperparameter, D KL is the KL divergence, f φ is the current round model, f θ is the initialized model to be trained, L CR represents the cross loss entropy;
[0115] A convergence gradient is determined according to the cross loss entropy obtained in the current training round and the previous training round, and the current round model is updated according to the convergence gradient.
[0116] In some embodiments of the present application, in the above-mentioned training device for the neural network model for classification, the model to be trained is an image classification model constructed based on ResNet, or a text classification model constructed based on BERT.
[0117] It should be noted that the above-mentioned training device for the neural network model for classification can implement the above-mentioned training method for the neural network model for classification one by one, which will not be described in detail.
[0118] Figure 5 A schematic diagram of the structure of a classification device based on a neural network model according to an embodiment of the present application is shown. Figure 5 It can be seen that the classification device 500 based on the neural network model includes:
[0119] An acquisition unit 510 is used to acquire information to be classified, where the information to be classified is image data or text data;
[0120] The prediction unit 520 is used to input the information to be classified into a trained neural network model for classification for prediction to obtain a classification result, wherein the neural network model for classification is obtained according to any of the training methods for the neural network model for classification described above.
[0121] It should be noted that the above-mentioned classification device based on the neural network model can implement the above-mentioned classification method based on the neural network model one by one, which will not be repeated here.
[0122] Figure 6 A schematic diagram showing the structure of a computer device according to an embodiment of the present application is shown. Figure 6 As shown, the internal structure of the computer device may include a processor, a memory, a network interface and a database connected via a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile and / or volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external client via a network connection. When the computer program is executed by the processor, the training method of the neural network model for classification of the present application, or the functions or steps of the classification method based on the neural network model are implemented.
[0123] In one embodiment, the computer device provided by the present application includes a memory and a processor, the memory stores a database and a computer program that can be run on the processor, and when the processor executes the computer program, the training method of the neural network model for classification of the present application, or the functions and steps of the classification method based on the neural network model of the present application is implemented.
[0124] In one embodiment, a computer-readable storage medium is also provided, on which a computer program is stored. When the computer program is executed by a processor, it implements the training method of the neural network model for classification of the present application, or the functions and steps of the classification method based on the neural network model.
[0125] It should be noted that the above functions or steps that can be implemented by the computer device or computer-readable storage medium can refer to the relevant description in the aforementioned method embodiment. To avoid repetition, they will not be described one by one here.
[0126] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0127] Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example. In actual applications, the above-mentioned functions can be distributed and completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.
[0128] The embodiments described above are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be included in the protection scope of the present invention.
Claims
1. A training method for a neural network model for classification, characterized in that: include: Generate an adversarial sample based on a training sample, where the training sample is an image sample or a text sample; Initializing a model to be trained, where the model to be trained is a neural network model for classification; Based on the training samples and the adversarial samples, the initialized model to be trained is trained for multiple rounds, and when the current training round is an integer multiple of a preset base number, the current round model is reset based on a random forgetting coefficient; When the current training round is not an integer multiple of the preset cardinality, the current round model is aligned with the initialized model to be trained to calculate the loss function value, and the neural network model updates the current round model according to the obtained loss function value until the training end condition is met to obtain a trained neural network model for classification.
2. The training method of the neural network model for classification according to claim 1, characterized in that: The initializing the model to be trained includes: The pre-trained neural network model for classification is used as the initialized model to be trained.
3. The training method of the neural network model for classification according to claim 1, characterized in that: The initializing the model to be trained includes: Determine the cross loss entropy of the current training round of the model to be trained based on the training sample and the adversarial sample; Determine the convergence gradient based on the cross-loss entropy between the current training round and the previous training round; The model to be trained in the current training round is updated according to the converged gradient until the end condition of the pre-training is met, thereby completing the initialization of the model to be trained.
4. The training method of the neural network model for classification according to claim 1, characterized in that: The resetting of the current round model based on the random forgetting coefficient includes: Randomly select a random forgetting coefficient, where the random forgetting coefficient is any value between zero and one; Reset the current round model according to the following formula: f φ =αf φ +(1-a)f θ ; Among them, f φ is the current round model, α is the random forgetting coefficient, f θ is the model to be trained after initialization.
5. The training method of the neural network model for classification according to claim 1, characterized in that: The step of aligning the current round model with the initialized model to be trained to calculate the loss function value, and updating the current round model according to the obtained loss function value, includes: The cross loss function value between the current round model and the initialized model to be trained is calculated according to the following formula: L FOMO =λ1D KL (f θ (x)||f φ (x))+λ2D KL (f θ (x+δ)||f φ (x+δ))+L CR (f θ (x),y)+L CR (f θ (x+δ),y); Among them, L FOMO is the cross loss function, λ1 and λ2 are hyperparameters, and D KL is the KL divergence, f φ is the current round model, f θ is the initialized model to be trained, L CR represents the cross loss entropy; A convergence gradient is determined according to the cross loss entropy obtained in the current training round and the previous training round, and the current round model is updated according to the convergence gradient.
6. The training method of a neural network model for classification according to any one of claims 1 to 5, characterized in that: The model to be trained is an image classification model built based on ResNet, or a text classification model built based on BERT.
7. A classification method based on a neural network model, characterized in that: include: Acquire information to be classified, where the information to be classified is image data or text data; The information to be classified is input into a trained neural network model for classification for prediction to obtain a classification result, wherein the neural network model for classification is obtained according to the training method for a neural network model for classification according to any one of claims 1 to 6.
8. A training device for a neural network model for classification, characterized in that: The training device of the neural network model for classification includes: An adversarial sample generating unit, configured to generate an adversarial sample according to a training sample, wherein the training sample is an image sample or a text sample; A model initialization unit, used to initialize a model to be trained, wherein the model to be trained is a neural network model for classification; A first training unit is used to perform multiple rounds of training on the initialized model to be trained based on the training samples and the adversarial samples, and when the current training round is an integer multiple of a preset cardinality, the current round model is reset based on a random forgetting coefficient; The second training unit is used to align the current round model with the initialized model to be trained to calculate the loss function value when the current training round is not an integer multiple of the preset cardinality, and update the current round model according to the obtained loss function value until the training end condition is met to obtain a trained neural network model for classification.
9. 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 computer program, the processor implements the steps of the training method of the neural network model for classification as described in any one of claims 1 to 6 or the classification method based on the neural network model as described in claim 7.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is instructed by the processor, it implements the steps of the training method of the neural network model for classification as described in any one of claims 1 to 6 or the classification method based on the neural network model as described in claim 7.
Citation Information
Patent Citations
Image classification method and device based on adversarial training neural network, and storable medium
CN117115536A
Cited By
Neural network model training method and system used for adversarial defense, and related device
WO2026188744A1
