/
EarlyStoppingConfiguration.java
175 lines (148 loc) · 7.87 KB
/
EarlyStoppingConfiguration.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
package org.deeplearning4j.earlystopping;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.deeplearning4j.earlystopping.saver.InMemoryModelSaver;
import org.deeplearning4j.earlystopping.scorecalc.ScoreCalculator;
import org.deeplearning4j.earlystopping.termination.EpochTerminationCondition;
import org.deeplearning4j.earlystopping.termination.IterationTerminationCondition;
import org.deeplearning4j.exception.DL4JInvalidConfigException;
import org.deeplearning4j.nn.api.Model;
import org.nd4j.linalg.function.Supplier;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** Early stopping configuration: Specifies the various configuration options for running training with early stopping.<br>
* Users need to specify the following:<br>
* (a) EarlyStoppingModelSaver: How models will be saved (to disk, to memory, etc) (Default: in memory)<br>
* (b) Termination conditions: at least one termination condition must be specified<br>
* (i) Iteration termination conditions: calculated once for each minibatch. For example, maxTime or invalid (NaN/infinite) scores<br>
* (ii) Epoch termination conditions: calculated once per epoch. For example, maxEpochs or no improvement for N epochs<br>
* (c) Score calculator: what score should be calculated at every epoch? (For example: test set loss or test set accuracy)<br>
* (d) How frequently (ever N epochs) should scores be calculated? (Default: every epoch)<br>
* @param <T> Type of model. For example, {@link org.deeplearning4j.nn.multilayer.MultiLayerNetwork} or {@link org.deeplearning4j.nn.graph.ComputationGraph}
* @author Alex Black
*/
@Data
@NoArgsConstructor
public class EarlyStoppingConfiguration<T extends Model> implements Serializable {
private EarlyStoppingModelSaver<T> modelSaver;
private List<EpochTerminationCondition> epochTerminationConditions;
private List<IterationTerminationCondition> iterationTerminationConditions;
private boolean saveLastModel;
private int evaluateEveryNEpochs;
private ScoreCalculator<T> scoreCalculator;
private Supplier<ScoreCalculator> scoreCalculatorSupplier;
private EarlyStoppingConfiguration(Builder<T> builder) {
this.modelSaver = builder.modelSaver;
this.epochTerminationConditions = builder.epochTerminationConditions;
this.iterationTerminationConditions = builder.iterationTerminationConditions;
this.saveLastModel = builder.saveLastModel;
this.evaluateEveryNEpochs = builder.evaluateEveryNEpochs;
this.scoreCalculator = builder.scoreCalculator;
this.scoreCalculatorSupplier = builder.scoreCalculatorSupplier;
}
public ScoreCalculator<T> getScoreCalculator(){
if(scoreCalculatorSupplier != null){
return scoreCalculatorSupplier.get();
}
return scoreCalculator;
}
public void validate() {
if(scoreCalculator == null && scoreCalculatorSupplier == null) {
throw new DL4JInvalidConfigException("A score calculator or score calculator supplier must be defined.");
}
if(modelSaver == null) {
throw new DL4JInvalidConfigException("A model saver must be defined");
}
boolean hasTermination = false;
if(iterationTerminationConditions != null && !iterationTerminationConditions.isEmpty()) {
hasTermination = true;
}
else if(epochTerminationConditions != null && !epochTerminationConditions.isEmpty()) {
hasTermination = true;
}
if(!hasTermination) {
throw new DL4JInvalidConfigException("No termination conditions defined.");
}
}
public static class Builder<T extends Model> {
private EarlyStoppingModelSaver<T> modelSaver = new InMemoryModelSaver<>();
private List<EpochTerminationCondition> epochTerminationConditions = new ArrayList<>();
private List<IterationTerminationCondition> iterationTerminationConditions = new ArrayList<>();
private boolean saveLastModel = false;
private int evaluateEveryNEpochs = 1;
private ScoreCalculator<T> scoreCalculator;
private Supplier<ScoreCalculator> scoreCalculatorSupplier;
/** How should models be saved? (Default: in memory)*/
public Builder<T> modelSaver(EarlyStoppingModelSaver<T> modelSaver) {
this.modelSaver = modelSaver;
return this;
}
/** Termination conditions to be evaluated every N epochs, with N set by evaluateEveryNEpochs option */
public Builder<T> epochTerminationConditions(EpochTerminationCondition... terminationConditions) {
epochTerminationConditions.clear();
Collections.addAll(epochTerminationConditions, terminationConditions);
return this;
}
/** Termination conditions to be evaluated every N epochs, with N set by evaluateEveryNEpochs option */
public Builder<T> epochTerminationConditions(List<EpochTerminationCondition> terminationConditions) {
this.epochTerminationConditions = terminationConditions;
return this;
}
/** Termination conditions to be evaluated every iteration (minibatch)*/
public Builder<T> iterationTerminationConditions(IterationTerminationCondition... terminationConditions) {
iterationTerminationConditions.clear();
Collections.addAll(iterationTerminationConditions, terminationConditions);
return this;
}
/** Save the last model? If true: save the most recent model at each epoch, in addition to the best
* model (whenever the best model improves). If false: only save the best model. Default: false
* Useful for example if you might want to continue training after a max-time terminatino condition
* occurs.
*/
public Builder<T> saveLastModel(boolean saveLastModel) {
this.saveLastModel = saveLastModel;
return this;
}
/** How frequently should evaluations be conducted (in terms of epochs)? Defaults to every (1) epochs. */
public Builder<T> evaluateEveryNEpochs(int everyNEpochs) {
this.evaluateEveryNEpochs = everyNEpochs;
return this;
}
/** Score calculator. Used to calculate a score (such as loss function on a test set), every N epochs,
* where N is set by {@link #evaluateEveryNEpochs}
*/
public Builder<T> scoreCalculator(ScoreCalculator scoreCalculator) {
this.scoreCalculator = scoreCalculator;
return this;
}
/** Score calculator. Used to calculate a score (such as loss function on a test set), every N epochs,
* where N is set by {@link #evaluateEveryNEpochs}
*/
public Builder<T> scoreCalculator(Supplier<ScoreCalculator> scoreCalculatorSupplier){
this.scoreCalculatorSupplier = scoreCalculatorSupplier;
return this;
}
/** Create the early stopping configuration */
public EarlyStoppingConfiguration<T> build() {
return new EarlyStoppingConfiguration<>(this);
}
}
}