From f08dcf814034527577abe21ad0bdf28e27d4eb66 Mon Sep 17 00:00:00 2001 From: Oskar Triebe Date: Thu, 19 Oct 2023 16:18:04 -0700 Subject: [PATCH] [Minor] SmoothL1Loss correctly mentioned instead of Huber (#1458) * replace false mentions of Huber loss * set SmoothL1Loss beta to 0.1 * increase beta to 0.3 * reset beta to 1.0 --- .../feature-guides/hyperparameter-selection.md | 2 +- ...205\345\217\202\346\225\260\351\200\211\345\217\226.md" | 4 ++-- neuralprophet/configure.py | 7 ++++--- neuralprophet/forecaster.py | 4 ++-- tests/test_configure.py | 2 +- tests/test_uncertainty.py | 2 +- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/source/how-to-guides/feature-guides/hyperparameter-selection.md b/docs/source/how-to-guides/feature-guides/hyperparameter-selection.md index 785fb4338..0e532d5f5 100644 --- a/docs/source/how-to-guides/feature-guides/hyperparameter-selection.md +++ b/docs/source/how-to-guides/feature-guides/hyperparameter-selection.md @@ -27,7 +27,7 @@ If it looks like the model is overfitting to the training data (the live loss pl you can reduce `epochs` and `learning_rate`, and potentially increase the `batch_size`. If it is underfitting, the number of `epochs` and `learning_rate` can be increased and the `batch_size` potentially decreased. -The default loss function is the 'Huber' loss, which is considered to be robust to outliers. +The default loss function is the 'SmoothL1Loss' loss, which is considered to be robust to outliers. However, you are free to choose the standard `MSE` or any other PyTorch `torch.nn.modules.loss` loss function. ## Increasing Depth of the Model diff --git "a/docs/zh/\350\266\205\345\217\202\346\225\260\351\200\211\345\217\226.md" "b/docs/zh/\350\266\205\345\217\202\346\225\260\351\200\211\345\217\226.md" index af553e5f0..f101d7e50 100644 --- "a/docs/zh/\350\266\205\345\217\202\346\225\260\351\200\211\345\217\226.md" +++ "b/docs/zh/\350\266\205\345\217\202\346\225\260\351\200\211\345\217\226.md" @@ -22,7 +22,7 @@ NeuralProphet有一些超参数需要用户指定。如果没有指定,将使 | `learning_rate` | None | | `epochs` | None | | `batch_size` | None | -| `loss_func` | Huber | +| `loss_func` | SmoothL1Loss | | `train_speed` | None | | `normalize_y` | auto | | `impute_missing` | True | @@ -43,7 +43,7 @@ NeuralProphet采用随机梯度下降法进行拟合--更准确地说,是采 如果看起来模型对训练数据过度拟合(实时损失图在此很有用),可以减少 `epochs` 和 `learning_rate`,并有可能增加 `batch_size`。如果是低拟合,可以增加`epochs` 和`learning_rate` 的数量,并有可能减少`batch_size` 。 -默认的损失函数是 "Huber "损失,该函数被认为对离群值具有鲁棒性。但是,您可以自由选择标准的 "MSE "或任何其他PyTorch `torch.nn.modules.loss`损失函数。 +默认的损失函数是 "SmoothL1Loss "损失,该函数被认为对离群值具有鲁棒性。但是,您可以自由选择标准的 "MSE "或任何其他PyTorch `torch.nn.modules.loss`损失函数。 ## 增加模型的深度 diff --git a/neuralprophet/configure.py b/neuralprophet/configure.py index 9b658854e..fe4686811 100644 --- a/neuralprophet/configure.py +++ b/neuralprophet/configure.py @@ -117,9 +117,10 @@ def __post_init__(self): def set_loss_func(self): if isinstance(self.loss_func, str): - if self.loss_func.lower() in ["huber", "smoothl1", "smoothl1loss"]: - self.loss_func = torch.nn.SmoothL1Loss(reduction="none") - elif self.loss_func.lower() in ["mae", "l1", "l1loss"]: + if self.loss_func.lower() in ["smoothl1", "smoothl1loss", "huber"]: + # keeping 'huber' for backwards compatiblility, though not identical + self.loss_func = torch.nn.SmoothL1Loss(reduction="none", beta=1.0) + elif self.loss_func.lower() in ["mae", "maeloss", "l1", "l1loss"]: self.loss_func = torch.nn.L1Loss(reduction="none") elif self.loss_func.lower() in ["mse", "mseloss", "l2", "l2loss"]: self.loss_func = torch.nn.MSELoss(reduction="none") diff --git a/neuralprophet/forecaster.py b/neuralprophet/forecaster.py index 383aa18c1..852fc297b 100644 --- a/neuralprophet/forecaster.py +++ b/neuralprophet/forecaster.py @@ -223,7 +223,7 @@ class NeuralProphet: Type of loss to use: Options - * (default) ``Huber``: Huber loss function + * (default) ``SmoothL1Loss``: SmoothL1 loss function * ``MSE``: Mean Squared Error loss function * ``MAE``: Mean Absolute Error loss function * ``torch.nn.functional.loss.``: loss or callable for custom loss, eg. L1-Loss @@ -360,7 +360,7 @@ def __init__( learning_rate: Optional[float] = None, epochs: Optional[int] = None, batch_size: Optional[int] = None, - loss_func: Union[str, torch.nn.modules.loss._Loss, Callable] = "Huber", + loss_func: Union[str, torch.nn.modules.loss._Loss, Callable] = "SmoothL1Loss", optimizer: Union[str, Type[torch.optim.Optimizer]] = "AdamW", newer_samples_weight: float = 2, newer_samples_start: float = 0.0, diff --git a/tests/test_configure.py b/tests/test_configure.py index 8bcae981d..e5c5e9800 100644 --- a/tests/test_configure.py +++ b/tests/test_configure.py @@ -9,7 +9,7 @@ def generate_config_train_params(overrides={}): "learning_rate": None, "epochs": None, "batch_size": None, - "loss_func": "Huber", + "loss_func": "SmoothL1Loss", "optimizer": "AdamW", } for key, value in overrides.items(): diff --git a/tests/test_uncertainty.py b/tests/test_uncertainty.py index e7ba16118..039128cb1 100644 --- a/tests/test_uncertainty.py +++ b/tests/test_uncertainty.py @@ -60,7 +60,7 @@ def test_uncertainty_estimation_peyton_manning(): m = NeuralProphet( n_forecasts=1, - loss_func="Huber", + loss_func="SmoothL1Loss", quantiles=[0.01, 0.99], epochs=EPOCHS, batch_size=BATCH_SIZE,