Automatically stop training of neural network if certain accuracy in training is reached

3 visualizzazioni (ultimi 30 giorni)
I would like to do a sensitivity analysis to get the best parameters of my simulated training data. To make it automatic i would like the training to stop automatically when the network reaches an accuracy of x% on the training dataset. Is there an option or parameter for this?
Currently my code looks like this:
layers = [...
imageInputLayer([100,100,1])
convolution2dLayer(3,4,'Stride',1,'Padding',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2,'Padding',0)
convolution2dLayer(3,8,'Stride',1,'Padding',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2,'Padding',0)
fullyConnectedLayer(32)
fullyConnectedLayer(32)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm','InitialLearnRate',0.001,'MaxEpochs',20,'Plots','none','MiniBatchSize',500);
net = trainNetwork(trainingHeatmapComplete4d,trainingOutputsComplete,layers,options);

Risposta accettata

Milan Bansal
Milan Bansal il 14 Set 2023
Hi,
In my understanding, you want to interrupt the training of Neural Network automatically when a certain accuracy is reached.
It is possible to do so by defining a callback function and setting it as "OutputFcn" in "options" argument of the "trainNetwork" function. Add the following function at the bottom of the script.
function stop = accuracyCallback(info)
stop = false; % Initialize the stop flag as false
if info.TrainingAccuracy > 90 % Desired accuracy
stop = true; % Set the stop flag to true if desired accuracy is reached
end
end
This function will act as callback function to stop the training at 90% accuracy.
Set the 'OutputFcn' option to the callback function defined above in the "options" argument as shown below.
options = trainingOptions('sgdm','InitialLearnRate',0.001, ...
'MaxEpochs',20,'Plots','none','MiniBatchSize',500, ...
'OutputFcn', @accuracyCallback);
Refer to the documentation link to know more about "trainNetwork" function.

Più risposte (0)

Categorie

Scopri di più su Deep Learning Toolbox in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by