Automatically stop training of neural network if certain accuracy in training is reached
2 views (last 30 days)
Show older comments
Julius Jordan
on 12 Apr 2022
Answered: Milan Bansal
on 14 Sep 2023
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);
0 Comments
Accepted Answer
Milan Bansal
on 14 Sep 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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!