Skip to content

Commit 144fb35

Browse files
authored
[FEAT] Choose monitor for validation (#1488)
1 parent 936a3cf commit 144fb35

39 files changed

Lines changed: 148 additions & 1 deletion

neuralforecast/common/_base_model.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def __init__(
118118
step_size: int = 1,
119119
num_lr_decays: int = 0,
120120
early_stop_patience_steps: int = -1,
121+
val_monitor: str = "ptl/val_loss",
121122
scaler_type: str = "identity",
122123
futr_exog_list: Union[List, None] = None,
123124
hist_exog_list: Union[List, None] = None,
@@ -295,11 +296,17 @@ def __init__(
295296

296297
# Callbacks
297298
if early_stop_patience_steps > 0:
299+
valid_monitors = ["ptl/val_loss", "valid_loss", "train_loss"]
300+
if val_monitor not in valid_monitors:
301+
raise ValueError(
302+
f"val_monitor='{val_monitor}' is not supported. "
303+
f"Valid options are: {valid_monitors}."
304+
)
298305
if "callbacks" not in trainer_kwargs:
299306
trainer_kwargs["callbacks"] = []
300307
trainer_kwargs["callbacks"].append(
301308
EarlyStopping(
302-
monitor="ptl/val_loss", patience=early_stop_patience_steps
309+
monitor=val_monitor, patience=early_stop_patience_steps
303310
)
304311
)
305312

@@ -398,6 +405,7 @@ def __init__(
398405
max(max_steps // self.num_lr_decays, 1) if self.num_lr_decays > 0 else 10e7
399406
)
400407
self.early_stop_patience_steps = early_stop_patience_steps
408+
self.val_monitor = val_monitor
401409
self.val_check_steps = val_check_steps
402410
self.windows_batch_size = windows_batch_size
403411
self.step_size = step_size

neuralforecast/models/autoformer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ class Autoformer(BaseModel):
431431
learning_rate (float): Learning rate between (0, 1).
432432
num_lr_decays (int): Number of learning rate decays, evenly distributed across max_steps.
433433
early_stop_patience_steps (int): Number of validation iterations before early stopping.
434+
val_monitor (str): metric to monitor for early stopping. Valid options: "ptl/val_loss", "valid_loss", "train_loss". Default: "ptl/val_loss".
434435
val_check_steps (int): Number of training steps between every validation loss check.
435436
batch_size (int): number of different series in each batch.
436437
valid_batch_size (int): number of different series in each validation and test batch, if None uses batch_size.
@@ -487,6 +488,7 @@ def __init__(
487488
learning_rate: float = 1e-4,
488489
num_lr_decays: int = -1,
489490
early_stop_patience_steps: int = -1,
491+
val_monitor: str = "ptl/val_loss",
490492
val_check_steps: int = 100,
491493
batch_size: int = 32,
492494
valid_batch_size: Optional[int] = None,
@@ -519,6 +521,7 @@ def __init__(
519521
learning_rate=learning_rate,
520522
num_lr_decays=num_lr_decays,
521523
early_stop_patience_steps=early_stop_patience_steps,
524+
val_monitor=val_monitor,
522525
val_check_steps=val_check_steps,
523526
batch_size=batch_size,
524527
valid_batch_size=valid_batch_size,

neuralforecast/models/bitcn.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class BiTCN(BaseModel):
104104
learning_rate (float): Learning rate between (0, 1). Default: 1e-3.
105105
num_lr_decays (int): Number of learning rate decays, evenly distributed across max_steps. Default: -1.
106106
early_stop_patience_steps (int): Number of validation iterations before early stopping. Default: -1.
107+
val_monitor (str): metric to monitor for early stopping. Valid options: "ptl/val_loss", "valid_loss", "train_loss". Default: "ptl/val_loss".
107108
val_check_steps (int): Number of training steps between every validation loss check. Default: 100.
108109
batch_size (int): number of different series in each batch. Default: 32.
109110
valid_batch_size (int): number of different series in each validation and test batch, if None uses batch_size. Default: None.
@@ -153,6 +154,7 @@ def __init__(
153154
learning_rate: float = 1e-3,
154155
num_lr_decays: int = -1,
155156
early_stop_patience_steps: int = -1,
157+
val_monitor: str = "ptl/val_loss",
156158
val_check_steps: int = 100,
157159
batch_size: int = 32,
158160
valid_batch_size: Optional[int] = None,
@@ -185,6 +187,7 @@ def __init__(
185187
learning_rate=learning_rate,
186188
num_lr_decays=num_lr_decays,
187189
early_stop_patience_steps=early_stop_patience_steps,
190+
val_monitor=val_monitor,
188191
val_check_steps=val_check_steps,
189192
batch_size=batch_size,
190193
valid_batch_size=valid_batch_size,

neuralforecast/models/deepar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class DeepAR(BaseModel):
3737
learning_rate (float): Learning rate between (0, 1).
3838
num_lr_decays (int): Number of learning rate decays, evenly distributed across max_steps.
3939
early_stop_patience_steps (int): Number of validation iterations before early stopping.
40+
val_monitor (str): metric to monitor for early stopping. Valid options: "ptl/val_loss", "valid_loss", "train_loss". Default: "ptl/val_loss".
4041
val_check_steps (int): Number of training steps between every validation loss check.
4142
batch_size (int): number of different series in each batch.
4243
valid_batch_size (int): number of different series in each validation and test batch, if None uses batch_size.
@@ -92,6 +93,7 @@ def __init__(
9293
learning_rate: float = 1e-3,
9394
num_lr_decays: int = 3,
9495
early_stop_patience_steps: int = -1,
96+
val_monitor: str = "ptl/val_loss",
9597
val_check_steps: int = 100,
9698
batch_size: int = 32,
9799
valid_batch_size: Optional[int] = None,
@@ -130,6 +132,7 @@ def __init__(
130132
learning_rate=learning_rate,
131133
num_lr_decays=num_lr_decays,
132134
early_stop_patience_steps=early_stop_patience_steps,
135+
val_monitor=val_monitor,
133136
val_check_steps=val_check_steps,
134137
batch_size=batch_size,
135138
valid_batch_size=valid_batch_size,

neuralforecast/models/deepnpts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DeepNPTS(BaseModel):
3838
learning_rate (float): Learning rate between (0, 1).
3939
num_lr_decays (int): Number of learning rate decays, evenly distributed across max_steps.
4040
early_stop_patience_steps (int): Number of validation iterations before early stopping.
41+
val_monitor (str): metric to monitor for early stopping. Valid options: "ptl/val_loss", "valid_loss", "train_loss". Default: "ptl/val_loss".
4142
val_check_steps (int): Number of training steps between every validation loss check.
4243
batch_size (int): number of different series in each batch.
4344
valid_batch_size (int): number of different series in each validation and test batch, if None uses batch_size.
@@ -89,6 +90,7 @@ def __init__(
8990
learning_rate: float = 1e-3,
9091
num_lr_decays: int = 3,
9192
early_stop_patience_steps: int = -1,
93+
val_monitor: str = "ptl/val_loss",
9294
val_check_steps: int = 100,
9395
batch_size: int = 32,
9496
valid_batch_size: Optional[int] = None,
@@ -136,6 +138,7 @@ def __init__(
136138
learning_rate=learning_rate,
137139
num_lr_decays=num_lr_decays,
138140
early_stop_patience_steps=early_stop_patience_steps,
141+
val_monitor=val_monitor,
139142
val_check_steps=val_check_steps,
140143
batch_size=batch_size,
141144
valid_batch_size=valid_batch_size,

neuralforecast/models/dilated_rnn.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ class DilatedRNN(BaseModel):
309309
learning_rate (float): Learning rate between (0, 1).
310310
num_lr_decays (int): Number of learning rate decays, evenly distributed across max_steps.
311311
early_stop_patience_steps (int): Number of validation iterations before early stopping.
312+
val_monitor (str): metric to monitor for early stopping. Valid options: "ptl/val_loss", "valid_loss", "train_loss". Default: "ptl/val_loss".
312313
val_check_steps (int): Number of training steps between every validation loss check.
313314
batch_size (int): number of different series in each batch.
314315
valid_batch_size (int): number of different series in each validation and test batch.
@@ -362,6 +363,7 @@ def __init__(
362363
learning_rate: float = 1e-3,
363364
num_lr_decays: int = 3,
364365
early_stop_patience_steps: int = -1,
366+
val_monitor: str = "ptl/val_loss",
365367
val_check_steps: int = 100,
366368
batch_size=32,
367369
valid_batch_size: Optional[int] = None,
@@ -395,6 +397,7 @@ def __init__(
395397
learning_rate=learning_rate,
396398
num_lr_decays=num_lr_decays,
397399
early_stop_patience_steps=early_stop_patience_steps,
400+
val_monitor=val_monitor,
398401
val_check_steps=val_check_steps,
399402
batch_size=batch_size,
400403
valid_batch_size=valid_batch_size,

neuralforecast/models/dlinear.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class DLinear(BaseModel):
6464
learning_rate (float): Learning rate between (0, 1).
6565
num_lr_decays (int): Number of learning rate decays, evenly distributed across max_steps.
6666
early_stop_patience_steps (int): Number of validation iterations before early stopping.
67+
val_monitor (str): metric to monitor for early stopping. Valid options: "ptl/val_loss", "valid_loss", "train_loss". Default: "ptl/val_loss".
6768
val_check_steps (int): Number of training steps between every validation loss check.
6869
batch_size (int): number of different series in each batch.
6970
valid_batch_size (int): number of different series in each validation and test batch, if None uses batch_size.
@@ -111,6 +112,7 @@ def __init__(
111112
learning_rate: float = 1e-4,
112113
num_lr_decays: int = -1,
113114
early_stop_patience_steps: int = -1,
115+
val_monitor: str = "ptl/val_loss",
114116
val_check_steps: int = 100,
115117
batch_size: int = 32,
116118
valid_batch_size: Optional[int] = None,
@@ -143,6 +145,7 @@ def __init__(
143145
learning_rate=learning_rate,
144146
num_lr_decays=num_lr_decays,
145147
early_stop_patience_steps=early_stop_patience_steps,
148+
val_monitor=val_monitor,
146149
val_check_steps=val_check_steps,
147150
batch_size=batch_size,
148151
windows_batch_size=windows_batch_size,

neuralforecast/models/fedformer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ class FEDformer(BaseModel):
427427
learning_rate (float): Learning rate between (0, 1).
428428
num_lr_decays (int): Number of learning rate decays, evenly distributed across max_steps.
429429
early_stop_patience_steps (int): Number of validation iterations before early stopping.
430+
val_monitor (str): metric to monitor for early stopping. Valid options: "ptl/val_loss", "valid_loss", "train_loss". Default: "ptl/val_loss".
430431
val_check_steps (int): Number of training steps between every validation loss check.
431432
batch_size (int): number of different series in each batch.
432433
valid_batch_size (int): number of different series in each validation and test batch, if None uses batch_size.
@@ -485,6 +486,7 @@ def __init__(
485486
learning_rate: float = 1e-4,
486487
num_lr_decays: int = -1,
487488
early_stop_patience_steps: int = -1,
489+
val_monitor: str = "ptl/val_loss",
488490
val_check_steps: int = 100,
489491
batch_size: int = 32,
490492
valid_batch_size: Optional[int] = None,
@@ -516,6 +518,7 @@ def __init__(
516518
learning_rate=learning_rate,
517519
num_lr_decays=num_lr_decays,
518520
early_stop_patience_steps=early_stop_patience_steps,
521+
val_monitor=val_monitor,
519522
val_check_steps=val_check_steps,
520523
batch_size=batch_size,
521524
valid_batch_size=valid_batch_size,

neuralforecast/models/gru.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class GRU(BaseModel):
4747
learning_rate (float): Learning rate between (0, 1).
4848
num_lr_decays (int): Number of learning rate decays, evenly distributed across max_steps.
4949
early_stop_patience_steps (int): Number of validation iterations before early stopping.
50+
val_monitor (str): metric to monitor for early stopping. Valid options: "ptl/val_loss", "valid_loss", "train_loss". Default: "ptl/val_loss".
5051
val_check_steps (int): Number of training steps between every validation loss check.
5152
batch_size (int): number of different series in each batch.
5253
valid_batch_size (int): number of different series in each validation and test batch.
@@ -105,6 +106,7 @@ def __init__(
105106
learning_rate: float = 1e-3,
106107
num_lr_decays: int = -1,
107108
early_stop_patience_steps: int = -1,
109+
val_monitor: str = "ptl/val_loss",
108110
val_check_steps: int = 100,
109111
batch_size=32,
110112
valid_batch_size: Optional[int] = None,
@@ -142,6 +144,7 @@ def __init__(
142144
learning_rate=learning_rate,
143145
num_lr_decays=num_lr_decays,
144146
early_stop_patience_steps=early_stop_patience_steps,
147+
val_monitor=val_monitor,
145148
val_check_steps=val_check_steps,
146149
batch_size=batch_size,
147150
valid_batch_size=valid_batch_size,

neuralforecast/models/hint.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def __init__(
160160
self.h = h
161161
self.model = model
162162
self.early_stop_patience_steps = model.early_stop_patience_steps
163+
self.val_monitor = model.val_monitor
163164
self.S = S
164165
self.reconciliation = reconciliation
165166
self.loss = model.loss

0 commit comments

Comments
 (0)