-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathcallbacks_test.ts
294 lines (261 loc) · 11 KB
/
callbacks_test.ts
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* @license
* Copyright 2018 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
import {tensor2d} from '@tensorflow/tfjs-core';
import * as tfl from './index';
import {describeMathCPUAndGPU} from './utils/test_utils';
describe('EarlyStopping', () => {
function createDummyModel(): tfl.LayersModel {
const model = tfl.sequential();
model.add(tfl.layers.dense({units: 1, inputShape: [1]}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
return model;
}
it('Default monitor, default mode, increasing val_loss', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping();
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
await callback.onEpochEnd(0, {val_loss: 10});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {val_loss: 9});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(2);
await callback.onEpochEnd(2, {val_loss: 9.5});
expect(model.stopTraining).toEqual(true);
});
it('Default monitor, default mode, holding val_loss', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping();
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
await callback.onEpochEnd(0, {val_loss: 10});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {val_loss: 9});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(2);
await callback.onEpochEnd(2, {val_loss: 9});
expect(model.stopTraining).toEqual(true);
});
it('Default monitor, default mode, custom minDelta', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping({minDelta: 1});
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
await callback.onEpochEnd(0, {val_loss: 10});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {val_loss: 8});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(2);
// An decrease of 0.5 is < minDelta (1) and should trigger stop.
await callback.onEpochEnd(2, {val_loss: 7.5});
expect(model.stopTraining).toEqual(true);
});
it('Default monitor, default mode, custom baseline, stopping', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping({baseline: 5});
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(1);
// Failure to go below the baseline will stop the training immediately.
await callback.onEpochEnd(1, {val_loss: 6});
expect(model.stopTraining).toEqual(true);
});
it('Default monitor, default mode, custom baseline, not stopping',
async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping({baseline: 5});
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(1);
// If the loss value goes below the baseline, training should continue.
await callback.onEpochEnd(1, {val_loss: 4});
expect(model.stopTraining).toBeUndefined();
// If the loss value increases, training should stop;
await callback.onEpochEnd(1, {val_loss: 4.5});
expect(model.stopTraining).toEqual(true);
});
it('Custom monitor, default model, increasing', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping({monitor: 'aux_loss'});
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
await callback.onEpochEnd(0, {val_loss: 10, aux_loss: 100});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {val_loss: 9, aux_loss: 120});
expect(model.stopTraining).toEqual(true);
});
it('Custom monitor, max, increasing', async () => {
const model = createDummyModel();
const callback =
tfl.callbacks.earlyStopping({monitor: 'aux_metric', mode: 'max'});
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
await callback.onEpochEnd(0, {val_loss: 10, aux_metric: 100});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {val_loss: 9, aux_metric: 120});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(2);
await callback.onEpochEnd(2, {val_loss: 9, aux_metric: 110});
expect(model.stopTraining).toEqual(true);
});
it('Custom monitor, max, custom minDelta', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping(
{monitor: 'aux_metric', mode: 'max', minDelta: 5});
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
await callback.onEpochEnd(0, {val_loss: 10, aux_metric: 100});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {val_loss: 9, aux_metric: 120});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(2);
// An increase of 2 is < minDelta (5) and should cause stopping.
await callback.onEpochEnd(2, {val_loss: 9, aux_metric: 122});
expect(model.stopTraining).toEqual(true);
});
it('Custom monitor, max, custom negative minDelta', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping(
{monitor: 'aux_metric', mode: 'max', minDelta: -5});
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
await callback.onEpochEnd(0, {val_loss: 10, aux_metric: 100});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {val_loss: 9, aux_metric: 120});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(2);
// An increase of 2 is < minDelta (5) and should cause stopping.
await callback.onEpochEnd(2, {val_loss: 9, aux_metric: 122});
expect(model.stopTraining).toEqual(true);
});
it('Patience = 2', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping({patience: 2});
callback.setModel(model);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
await callback.onEpochEnd(0, {val_loss: 10});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {val_loss: 9});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(2);
await callback.onEpochEnd(2, {val_loss: 9.5});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(3);
await callback.onEpochEnd(3, {val_loss: 9.6});
expect(model.stopTraining).toEqual(true);
});
it('Missing monitor leads to warning', async () => {
const model = createDummyModel();
const callback = tfl.callbacks.earlyStopping();
callback.setModel(model);
const warnMessages: string[] = [];
function fakeWarn(message: string) {
warnMessages.push(message);
}
spyOn(console, 'warn').and.callFake(fakeWarn);
await callback.onTrainBegin();
await callback.onEpochBegin(0);
// Note that the default monitor (val_loss) is missing.
await callback.onEpochEnd(0, {loss: 100});
expect(model.stopTraining).toBeUndefined();
await callback.onEpochBegin(1);
await callback.onEpochEnd(1, {loss: 100});
expect(model.stopTraining).toBeUndefined();
expect(warnMessages.length).toEqual(2);
expect(warnMessages[0]).toMatch(/val_loss is not available/);
expect(warnMessages[1]).toMatch(/val_loss is not available/);
});
});
describeMathCPUAndGPU('EarlyStopping LayersModel.fit() integration', () => {
it('Functional model, monitor loss, With minDelta', async () => {
const input = tfl.input({shape: [1]});
const output =
tfl.layers.dense({units: 1, kernelInitializer: 'ones'}).apply(input) as
tfl.SymbolicTensor;
const model = tfl.model({inputs: input, outputs: output});
const xs = tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tensor2d([2, 3, 4, 5], [4, 1]);
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Without the EarlyStopping callback, the loss value would be:
// 1, 0.734, 0.549, 0.421, 0.332, ...
// With loss being monitored and minDelta set to 0.25, the training should
// stop after the 3rd epoch.
const history = await model.fit(xs, ys, {
epochs: 10,
callbacks: tfl.callbacks.earlyStopping({monitor: 'loss', minDelta: 0.25})
});
expect(history.history.loss.length).toEqual(3);
});
it('Sequential model, monitor val_acc', async () => {
const model = tfl.sequential();
model.add(tfl.layers.dense({
units: 3,
activation: 'softmax',
kernelInitializer: 'ones',
inputShape: [2]
}));
const xs = tensor2d([1, 2, 3, 4], [2, 2]);
const ys = tensor2d([[1, 0, 0], [0, 1, 0]], [2, 3]);
const xsVal = tensor2d([4, 3, 2, 1], [2, 2]);
const ysVal = tensor2d([[0, 0, 1], [0, 1, 0]], [2, 3]);
model.compile(
{loss: 'categoricalCrossentropy', optimizer: 'sgd', metrics: ['acc']});
// Without the EarlyStopping callback, the val_acc value would be:
// 0.5, 0.5, 0.5, 0.5, ...
// With val_acc being monitored, training should stop after the 2nd epoch.
const history = await model.fit(xs, ys, {
epochs: 10,
validationData: [xsVal, ysVal],
callbacks: tfl.callbacks.earlyStopping({monitor: 'val_acc'})
});
expect(history.history.loss.length).toEqual(2);
});
it('Sequential model, monitor val_acc, custom patience', async () => {
const model = tfl.sequential();
model.add(tfl.layers.dense({
units: 3,
activation: 'softmax',
kernelInitializer: 'ones',
inputShape: [2]
}));
const xs = tensor2d([1, 2, 3, 4], [2, 2]);
const ys = tensor2d([[1, 0, 0], [0, 1, 0]], [2, 3]);
const xsVal = tensor2d([4, 3, 2, 1], [2, 2]);
const ysVal = tensor2d([[0, 0, 1], [0, 1, 0]], [2, 3]);
model.compile(
{loss: 'categoricalCrossentropy', optimizer: 'sgd', metrics: ['acc']});
// Without the EarlyStopping callback, the val_acc value would be:
// 0.5, 0.5, 0.5, 0.5, ...
// With val_acc being monitored and patience set to 4, training should stop
// after the 5th epoch.
const history = await model.fit(xs, ys, {
epochs: 10,
validationData: [xsVal, ysVal],
callbacks: tfl.callbacks.earlyStopping({monitor: 'val_acc', patience: 4})
});
expect(history.history.loss.length).toEqual(5);
});
});