Skip to content

Commit 5f103d8

Browse files
committed
add tests and adapt docstring
1 parent 00f2e25 commit 5f103d8

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

neuralprophet/configure.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def set_optimizer(self):
190190

191191
def set_scheduler(self):
192192
"""
193-
Set the scheduler and scheduler args.
193+
Set the scheduler and scheduler arg depending on the user selection.
194194
The scheduler is not initialized yet as this is done in configure_optimizers in TimeNet.
195195
"""
196196
self.scheduler_args.clear()
@@ -221,15 +221,6 @@ def set_scheduler(self):
221221
"gamma": 0.95,
222222
}
223223
)
224-
elif self.scheduler.lower() == "reducelronplateau":
225-
self.scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau
226-
self.scheduler_args.update(
227-
{
228-
"mode": "min",
229-
"factor": 0.1,
230-
"patience": 10,
231-
}
232-
)
233224
elif self.scheduler.lower() == "cosineannealinglr":
234225
self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR
235226
self.scheduler_args.update(

neuralprophet/forecaster.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@ class NeuralProphet:
301301
>>> m = NeuralProphet(collect_metrics=["MSE", "MAE", "RMSE"])
302302
>>> # use custorm torchmetrics names
303303
>>> m = NeuralProphet(collect_metrics={"MAPE": "MeanAbsolutePercentageError", "MSLE": "MeanSquaredLogError",
304+
scheduler : str, torch.optim.lr_scheduler._LRScheduler
305+
Type of learning rate scheduler to use.
306+
307+
Options
308+
* (default) ``OneCycleLR``: One Cycle Learning Rate scheduler
309+
* ``StepLR``: Step Learning Rate scheduler
310+
* ``ExponentialLR``: Exponential Learning Rate scheduler
311+
* ``CosineAnnealingLR``: Cosine Annealing Learning Rate scheduler
312+
313+
Examples
314+
--------
315+
>>> from neuralprophet import NeuralProphet
316+
>>> # Step Learning Rate scheduler
317+
>>> m = NeuralProphet(scheduler="StepLR")
304318
305319
COMMENT
306320
Uncertainty Estimation
@@ -975,6 +989,13 @@ def fit(
975989
Note: using multiple workers and therefore distributed training might significantly increase
976990
the training time since each batch needs to be copied to each worker for each epoch. Keeping
977991
all data on the main process might be faster for most datasets.
992+
scheduler : str
993+
Type of learning rate scheduler to use for continued training. If None, uses ExponentialLR as
994+
default as specified in the model config.
995+
Options
996+
* ``StepLR``: Step Learning Rate scheduler
997+
* ``ExponentialLR``: Exponential Learning Rate scheduler
998+
* ``CosineAnnealingLR``: Cosine Annealing Learning Rate scheduler
978999
9791000
Returns
9801001
-------
@@ -2796,7 +2817,8 @@ def _train(
27962817
checkpoint_path = self.metrics_logger.checkpoint_path
27972818
checkpoint = torch.load(checkpoint_path)
27982819

2799-
previous_epoch = self.model.current_epoch
2820+
checkpoint_epoch = checkpoint["epoch"] if "epoch" in checkpoint else 0
2821+
previous_epoch = max(self.model.current_epoch, checkpoint_epoch)
28002822

28012823
# Set continue_training flag in model to update scheduler correctly
28022824
self.model.continue_training = True

tests/test_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,34 @@ def test_continue_training():
115115
metrics = m.fit(df, checkpointing=True, freq="D")
116116
metrics2 = m.fit(df, freq="D", continue_training=True, epochs=ADDITIONAL_EPOCHS)
117117
assert metrics["Loss"].min() >= metrics2["Loss"].min()
118+
119+
120+
def test_continue_training_with_scheduler_selection():
121+
df = pd.read_csv(PEYTON_FILE, nrows=NROWS)
122+
m = NeuralProphet(
123+
epochs=EPOCHS,
124+
batch_size=BATCH_SIZE,
125+
learning_rate=LR,
126+
n_lags=6,
127+
n_forecasts=3,
128+
n_changepoints=0,
129+
)
130+
metrics = m.fit(df, checkpointing=True, freq="D")
131+
# Continue training with StepLR
132+
metrics2 = m.fit(df, freq="D", continue_training=True, epochs=ADDITIONAL_EPOCHS, scheduler="StepLR")
133+
assert metrics["Loss"].min() >= metrics2["Loss"].min()
134+
135+
136+
def test_save_load_continue_training():
137+
df = pd.read_csv(PEYTON_FILE, nrows=NROWS)
138+
m = NeuralProphet(
139+
epochs=EPOCHS,
140+
n_lags=6,
141+
n_forecasts=3,
142+
n_changepoints=0,
143+
)
144+
metrics = m.fit(df, checkpointing=True, freq="D")
145+
save(m, "test_model.pt")
146+
m2 = load("test_model.pt")
147+
metrics2 = m2.fit(df, continue_training=True, epochs=ADDITIONAL_EPOCHS, scheduler="StepLR")
148+
assert metrics["Loss"].min() >= metrics2["Loss"].min()

0 commit comments

Comments
 (0)