-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathstopIfAccuracyNotImproving.m
36 lines (28 loc) · 1.05 KB
/
stopIfAccuracyNotImproving.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function stop = stopIfAccuracyNotImproving(info,N)
stop = false;
% Keep track of the best validation accuracy and the number of validations for which
% there has not been an improvement of the accuracy.
persistent bestValAccuracy
persistent valLag
% Clear the variables when training starts.
if info.State == "start"
bestValAccuracy = 0;
valLag = 0;
elseif ~isempty(info.ValidationLoss)
% Compare the current validation accuracy to the best accuracy so far,
% and either set the best accuracy to the current accuracy, or increase
% the number of validations for which there has not been an improvement.
if info.ValidationAccuracy > bestValAccuracy
valLag = 0;
bestValAccuracy = info.ValidationAccuracy;
else
valLag = valLag + 1;
end
% If the validation lag is at least N, that is, the validation accuracy
% has not improved for at least N validations, then return true and
% stop training.
if valLag >= N
stop = true;
end
end
end