Repository: jdb78/pytorch-forecasting

Issue:
AttributeError: module 'torch' has no attribute 'logit'
- PyTorch-Forecasting version: 0.8.0
- PyTorch version:
- Python version: 3.7
- Operating System: WIndows 10 Entreprise

### Expected behavior



### Actual behavior

However, result was .... I think it has to do with ... because of ...

### Code to reproduce the problem

```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-271-a127b35d9f4a> in <module>
----> 1 import pytorch_forecasting

C:\ProgramData\Anaconda3\lib\site-packages\pytorch_forecasting\__init__.py in <module>
      2 PyTorch Forecasting package for timeseries forecasting with PyTorch.
      3 """
----> 4 from pytorch_forecasting.data import EncoderNormalizer, GroupNormalizer, TimeSeriesDataSet
      5 from pytorch_forecasting.models import Baseline, DeepAR, NBeats, TemporalFusionTransformer
      6 

C:\ProgramData\Anaconda3\lib\site-packages\pytorch_forecasting\data\__init__.py in <module>
      5 to abstracts the necessary work.
      6 """
----> 7 from pytorch_forecasting.data.encoders import EncoderNormalizer, GroupNormalizer, NaNLabelEncoder, TorchNormalizer
      8 from pytorch_forecasting.data.timeseries import TimeSeriesDataSet, TimeSynchronizedBatchSampler
      9 

C:\ProgramData\Anaconda3\lib\site-packages\pytorch_forecasting\data\encoders.py in <module>
    182 
    183 
--> 184 class TorchNormalizer(BaseEstimator, TransformerMixin):
    185     """
    186     Basic target transformer that can be fit also on torch tensors.

C:\ProgramData\Anaconda3\lib\site-packages\pytorch_forecasting\data\encoders.py in TorchNormalizer()
    191         "log": (torch.log, torch.exp),
    192         "log1p": (torch.log1p, torch.exp),
--> 193         "logit": (torch.logit, torch.sigmoid),
    194         "softplus": (_plus_one, F.softplus),
    195         "relu": (_identity, _clamp_zero),

AttributeError: module 'torch' has no attribute 'logit'
```

Paste the command(s) you ran and the output. Including a link to a colab notebook will speed up issue resolution.
If there was a crash, please include the traceback here.
The code used to initialize the TimeSeriesDataSet and model should be also included.



Discussion:
What was your PyTorch version? You might have to update.
Confirmed, updating from torch==1.5.0 to 1.7.1 fixed this.  
Also confirming, after torch update from 1.4.0 to 1.7.0 issue is no more.

Resolving patch:
diff --git a/examples/ar.py b/examples/ar.py
--- a/examples/ar.py
+++ b/examples/ar.py
@@ -113,7 +113,7 @@
 )
 
 # calcualte mean absolute error on validation set
-actuals = torch.cat([y for x, y in iter(val_dataloader)])
+actuals = torch.cat([y for x, (y, weight) in iter(val_dataloader)])
 predictions = deepar.predict(val_dataloader)
 print(f"Mean absolute error of model: {(actuals - predictions).abs().mean()}")
 
diff --git a/pytorch_forecasting/models/temporal_fusion_transformer/tuning.py b/pytorch_forecasting/models/temporal_fusion_transformer/tuning.py
--- a/pytorch_forecasting/models/temporal_fusion_transformer/tuning.py
+++ b/pytorch_forecasting/models/temporal_fusion_transformer/tuning.py
@@ -119,7 +119,7 @@ def optimize_hyperparameters(
     def objective(trial: optuna.Trial) -> float:
         # Filenames for each trial must be made unique in order to access each checkpoint.
         checkpoint_callback = pl.callbacks.ModelCheckpoint(
-            os.path.join(model_path, "trial_{}".format(trial.number), "{epoch}"), monitor="val_loss"
+            dirpath=os.path.join(model_path, "trial_{}".format(trial.number)), filename="{epoch}", monitor="val_loss"
         )
 
         # The default logger in PyTorch Lightning writes to event files to be consumed by
@@ -131,12 +131,12 @@ def objective(trial: optuna.Trial) -> float:
         gradient_clip_val = trial.suggest_loguniform("gradient_clip_val", *gradient_clip_val_range)
         trainer_kwargs.setdefault("gpus", [0] if torch.cuda.is_available() else None)
         trainer = pl.Trainer(
-            checkpoint_callback=checkpoint_callback,
             max_epochs=max_epochs,
             gradient_clip_val=gradient_clip_val,
             callbacks=[
                 metrics_callback,
                 learning_rate_callback,
+                checkpoint_callback,
                 PyTorchLightningPruningCallback(trial, monitor="val_loss"),
             ],
             logger=logger,
diff --git a/pytorch_forecasting/utils.py b/pytorch_forecasting/utils.py
--- a/pytorch_forecasting/utils.py
+++ b/pytorch_forecasting/utils.py
@@ -6,33 +6,10 @@
 from typing import Any, Callable, List, Tuple, Union
 
 import torch
+from torch.fft import irfft, rfft
 import torch.nn.functional as F
 from torch.nn.utils import rnn
 
-try:
-    # This works in PyTorch 1.7+
-    # taken from https://github.com/pyro-ppl/pyro/blob/dev/pyro/ops/fft.py
-    from torch.fft import irfft, rfft
-except ModuleNotFoundError:
-    # This works in PyTorch 1.6
-    def rfft(input, n=None):
-        if n is not None:
-            m = input.size(-1)
-            if n > m:
-                input = torch.nn.functional.pad(input, (0, n - m))
-            elif n < m:
-                input = input[..., :n]
-        return torch.view_as_complex(torch.rfft(input, 1))
-
-    def irfft(input, n=None):
-        if torch.is_complex(input):
-            input = torch.view_as_real(input)
-        else:
-            input = torch.nn.functional.pad(input[..., None], (0, 1))
-        if n is None:
-            n = 2 * (input.size(-1) - 1)
-        return torch.irfft(input, 1, signal_sizes=(n,))
-
 
 def integer_histogram(
     data: torch.LongTensor, min: Union[None, int] = None, max: Union[None, int] = None


Test patch:
diff --git a/tests/test_models/test_deepar.py b/tests/test_models/test_deepar.py
--- a/tests/test_models/test_deepar.py
+++ b/tests/test_models/test_deepar.py
@@ -38,15 +38,16 @@ def _integration(
     early_stop_callback = EarlyStopping(monitor="val_loss", min_delta=1e-4, patience=1, verbose=False, mode="min")
 
     logger = TensorBoardLogger(tmp_path)
-    checkpoint = ModelCheckpoint(filepath=tmp_path)
     trainer = pl.Trainer(
-        checkpoint_callback=checkpoint,
         max_epochs=3,
         gpus=gpus,
         weights_summary="top",
         gradient_clip_val=0.1,
         callbacks=[early_stop_callback],
-        fast_dev_run=True,
+        checkpoint_callback=True,
+        default_root_dir=tmp_path,
+        limit_train_batches=2,
+        limit_val_batches=2,
         logger=logger,
     )
 
@@ -67,7 +68,7 @@ def _integration(
             val_dataloaders=val_dataloader,
         )
         # check loading
-        net = DeepAR.load_from_checkpoint(checkpoint.best_model_path)
+        net = DeepAR.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)
 
         # check prediction
         net.predict(val_dataloader, fast_dev_run=True, return_index=True, return_decoder_lengths=True)
diff --git a/tests/test_models/test_nbeats.py b/tests/test_models/test_nbeats.py
--- a/tests/test_models/test_nbeats.py
+++ b/tests/test_models/test_nbeats.py
@@ -15,15 +15,16 @@ def test_integration(dataloaders_fixed_window_without_covariates, tmp_path, gpus
     early_stop_callback = EarlyStopping(monitor="val_loss", min_delta=1e-4, patience=1, verbose=False, mode="min")
 
     logger = TensorBoardLogger(tmp_path)
-    checkpoint = ModelCheckpoint(filepath=tmp_path)
     trainer = pl.Trainer(
-        checkpoint_callback=checkpoint,
-        max_epochs=3,
+        max_epochs=2,
         gpus=gpus,
         weights_summary="top",
         gradient_clip_val=0.1,
         callbacks=[early_stop_callback],
-        fast_dev_run=True,
+        checkpoint_callback=True,
+        default_root_dir=tmp_path,
+        limit_train_batches=2,
+        limit_val_batches=2,
         logger=logger,
     )
 
@@ -43,7 +44,7 @@ def test_integration(dataloaders_fixed_window_without_covariates, tmp_path, gpus
             val_dataloaders=val_dataloader,
         )
         # check loading
-        net = NBeats.load_from_checkpoint(checkpoint.best_model_path)
+        net = NBeats.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)
 
         # check prediction
         net.predict(val_dataloader, fast_dev_run=True, return_index=True, return_decoder_lengths=True)
diff --git a/tests/test_models/test_temporal_fusion_transformer.py b/tests/test_models/test_temporal_fusion_transformer.py
--- a/tests/test_models/test_temporal_fusion_transformer.py
+++ b/tests/test_models/test_temporal_fusion_transformer.py
@@ -34,15 +34,16 @@ def test_integration(multiple_dataloaders_with_covariates, tmp_path, gpus):
 
     # check training
     logger = TensorBoardLogger(tmp_path)
-    checkpoint = ModelCheckpoint(filepath=tmp_path)
     trainer = pl.Trainer(
-        checkpoint_callback=checkpoint,
         max_epochs=3,
         gpus=gpus,
         weights_summary="top",
         gradient_clip_val=0.1,
         callbacks=[early_stop_callback],
-        fast_dev_run=True,
+        checkpoint_callback=True,
+        default_root_dir=tmp_path,
+        limit_train_batches=2,
+        limit_val_batches=2,
         logger=logger,
     )
     # test monotone constraints automatically
@@ -87,7 +88,7 @@ def test_integration(multiple_dataloaders_with_covariates, tmp_path, gpus):
             )
 
             # check loading
-            net = TemporalFusionTransformer.load_from_checkpoint(checkpoint.best_model_path)
+            net = TemporalFusionTransformer.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)
 
             # check prediction
             net.predict(val_dataloader, fast_dev_run=True, return_index=True, return_decoder_lengths=True)