Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Minor] Make tests deterministic #1600

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions neuralprophet/forecaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@
checkpointing: bool = False,
continue_training: bool = False,
num_workers: int = 0,
deterministic: bool = False,
):
"""Train, and potentially evaluate model.

Expand Down Expand Up @@ -1069,6 +1070,7 @@
checkpointing_enabled=checkpointing,
continue_training=continue_training,
num_workers=num_workers,
deterministic=deterministic,
)
else:
df_val, _, _, _ = df_utils.prep_or_copy_df(validation_df)
Expand All @@ -1093,6 +1095,7 @@
checkpointing_enabled=checkpointing,
continue_training=continue_training,
num_workers=num_workers,
deterministic=deterministic,
)

# Show training plot
Expand All @@ -1105,7 +1108,7 @@
# Only display the plot if the session is interactive, eg. do not show in github actions since it
# causes an error in the Windows and MacOS environment
if matplotlib.is_interactive():
fig

Check warning on line 1111 in neuralprophet/forecaster.py

View workflow job for this annotation

GitHub Actions / pyright

Expression value is unused (reportUnusedExpression)

self.fitted = True
return metrics_df
Expand Down Expand Up @@ -2714,6 +2717,7 @@
checkpointing_enabled: bool = False,
continue_training=False,
num_workers=0,
deterministic: bool = False,
):
"""
Execute model training procedure for a configured number of epochs.
Expand Down Expand Up @@ -2771,6 +2775,7 @@
metrics_enabled=metrics_enabled,
checkpointing_enabled=checkpointing_enabled,
num_batches_per_epoch=len(train_loader),
deterministic=deterministic,
)

# Tune hyperparams and train
Expand Down
5 changes: 5 additions & 0 deletions neuralprophet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pandas as pd
import pytorch_lightning as pl
import torch
from lightning_fabric.utilities.seed import seed_everything

from neuralprophet import utils_torch
from neuralprophet.logger import ProgressBar
Expand Down Expand Up @@ -710,6 +711,7 @@ def set_random_seed(seed: int = 0):
"""
np.random.seed(seed)
torch.manual_seed(seed)
seed_everything(seed, workers=True)


def set_logger_level(logger, log_level, include_handlers=False):
Expand Down Expand Up @@ -818,6 +820,7 @@ def configure_trainer(
metrics_enabled: bool = False,
checkpointing_enabled: bool = False,
num_batches_per_epoch: int = 100,
deterministic: bool = False,
):
"""
Configures the PyTorch Lightning trainer.
Expand Down Expand Up @@ -888,6 +891,8 @@ def configure_trainer(
else:
config["logger"] = False

config["deterministic"] = deterministic

# Configure callbacks
callbacks = []
has_custom_callbacks = True if "callbacks" in config else False
Expand Down
Loading
Loading