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

조회 수: 3 (최근 30일)
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);

채택된 답변

Milan Bansal
Milan Bansal 2023년 9월 14일
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개)

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by