-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathearly_stopping.py
More file actions
142 lines (124 loc) · 5.21 KB
/
early_stopping.py
File metadata and controls
142 lines (124 loc) · 5.21 KB
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
import os
import numpy as np
import mxnet as mx
from gluonts.model.estimator import Estimator
from gluonts.dataset.common import Dataset
from gluonts.mx.trainer.callback import Callback
from gluonts.evaluation import Evaluator
### EARLY STOPPING adapted from https://gist.github.com/pbruneau/04c0dce4bdfb66ffac3f554f1b98c706
class MetricInferenceEarlyStopping(Callback):
"""
Early Stopping mechanism based on the prediction network.
Can be used to base the Early Stopping directly on a metric of interest, instead of on the training/validation loss.
In the same way as test datasets are used during model evaluation,
the time series of the validation_dataset can overlap with the train dataset time series,
except for a prediction_length part at the end of each time series.
Parameters
----------
validation_dataset
An out-of-sample dataset which is used to monitor metrics
predictor
A gluon predictor, with a prediction network that matches the training network
evaluator
The Evaluator used to calculate the validation metrics.
metric
The metric on which to base the early stopping on.
patience
Number of epochs to train on given the metric did not improve more than min_delta.
min_delta
Minimum change in the monitored metric counting as an improvement
verbose
Controls, if the validation metric is printed after each epoch.
minimize_metric
The metric objective.
restore_best_network
Controls, if the best model, as assessed by the validation metrics is restored after training.
num_samples
The amount of samples drawn to calculate the inference metrics.
"""
def __init__(
self,
validation_dataset: Dataset,
estimator: Estimator,
evaluator: Evaluator = Evaluator(num_workers=None, allow_nan_forecast=True), # nan can happen for tempfus
metric: str = "MSE",
patience: int = 10,
min_delta: float = 0.0,
verbose: bool = True,
minimize_metric: bool = True,
restore_best_network: bool = True,
num_samples: int = 100,
):
assert (
patience >= 0
), "EarlyStopping Callback patience needs to be >= 0"
assert (
min_delta >= 0
), "EarlyStopping Callback min_delta needs to be >= 0.0"
assert (
num_samples >= 1
), "EarlyStopping Callback num_samples needs to be >= 1"
self.validation_dataset = list(validation_dataset)
self.estimator = estimator
self.evaluator = evaluator
self.metric = metric
self.patience = patience
self.min_delta = min_delta
self.verbose = verbose
self.restore_best_network = restore_best_network
self.num_samples = num_samples
self.written_first = False
if minimize_metric:
self.best_metric_value = np.inf
self.is_better = np.less
else:
self.best_metric_value = -np.inf
self.is_better = np.greater
self.validation_metric_history = []
self.best_network = None
self.n_stale_epochs = 0
def on_epoch_end(
self,
epoch_no: int,
epoch_loss: float,
training_network: mx.gluon.nn.HybridBlock,
trainer: mx.gluon.Trainer,
best_epoch_info: dict,
ctx: mx.Context
) -> bool:
should_continue = True
transformation = self.estimator.create_transformation()
with self.estimator.trainer.ctx:
predictor = self.estimator.create_predictor(transformation=transformation, trained_network=training_network)
from gluonts.evaluation.backtest import make_evaluation_predictions
forecast_it, ts_it = make_evaluation_predictions(
dataset=self.validation_dataset,
predictor=predictor,
num_samples=self.num_samples,
)
agg_metrics, item_metrics = self.evaluator(ts_it, forecast_it)
current_metric_value = agg_metrics[self.metric]
self.validation_metric_history.append(current_metric_value)
if self.verbose:
print(
f"Validation metric {self.metric}: {current_metric_value}, best: {self.best_metric_value}"
)
if self.is_better(current_metric_value, self.best_metric_value) or not self.written_first:
self.written_first = True
self.best_metric_value = current_metric_value
if self.restore_best_network:
training_network.save_parameters(os.path.join(os.getenv('TMPDIR'), "best_network.params"))
self.n_stale_epochs = 0
else:
self.n_stale_epochs += 1
if self.n_stale_epochs == self.patience:
should_continue = False
print(
f"EarlyStopping callback initiated stop of training at epoch {epoch_no}."
)
if self.restore_best_network:
print(
f"Restoring best network from epoch {epoch_no - self.patience}."
)
training_network.load_parameters(os.path.join(os.getenv('TMPDIR'), "best_network.params"))
return should_continue