`model` must be a `LightningModule` or `torch._dynamo.OptimizedModule`, got `TemporalFusionTransformer`

4.1k Views Asked by At

im trying to temporal fusion transformer from pytorch_forecasting module but im getting the error in trainer.fit method:model must be a LightningModule or torch._dynamo.OptimizedModule, got TemporalFusionTransformer.I'm just replicating this paper from 'towardsdatascience'.reference:https://towardsdatascience.com/temporal-fusion-transformer-time-series-forecasting-with-deep-learning-complete-tutorial-d32c1e51cd91#:~:text=T%20emporal%20F%20usion%20T,dynamics%20of%20multiple%20time%20sequences.

3

There are 3 best solutions below

0
Soofies On

There has been an update to pytorch-forecasting requirements and pytorch lightning no longer imports as lightning.pytorch, but pytorch_lightning.

Changing this in pytorch-forecasting basemodel.py solved the issue for me.

1
Xiaofeifei On

Soffies' answer is basically correct. The fundamental reason is that import/from lightning.pytorch is incompatible with import/from pytorch_lightning. In pytorch_forecasting, the author uses import lightning.pytorch extensively. Therefore, all you need to do is to replace 'pytorch_lightning' with 'lightning.pytorch' in the your code. Additionally, I encountered this issue after updating to torch2

0
gndps On

replace your imports

from lightning.pytorch import Trainer #❌
from pytorch_lightning import Trainer #✅