'pytorch_lightning.utilities.exceptions.MisconfigurationException: The closure hasn't been executed

I got an error message when I used pytorch-lightning Trainer module:

File "run.py", line 105, in <module>
    runner.fit(experiment)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 769, in fit
    self._fit_impl, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 721, in _call_and_handle_interrupt
    return trainer_fn(*args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 809, in _fit_impl
    results = self._run(model, ckpt_path=self.ckpt_path)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 1234, in _run
    results = self._run_stage()
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 1321, in _run_stage
    return self._run_train()
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 1351, in _run_train
    self.fit_loop.run()
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/base.py", line 204, in run
    self.advance(*args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/fit_loop.py", line 269, in advance
    self._outputs = self.epoch_loop.run(self._data_fetcher)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/base.py", line 204, in run
    self.advance(*args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/epoch/training_epoch_loop.py", line 208, in advance
    batch_output = self.batch_loop.run(batch, batch_idx)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/base.py", line 204, in run
    self.advance(*args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/batch/training_batch_loop.py", line 88, in advance
    outputs = self.optimizer_loop.run(split_batch, optimizers, batch_idx)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/base.py", line 204, in run
    self.advance(*args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/optimization/optimizer_loop.py", line 207, in advance
    self.optimizer_idx,
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/optimization/optimizer_loop.py", line 258, in _run_optimization
    result = closure.consume_result()
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/loops/optimization/closure.py", line 52, in consume_result
    "The closure hasn't been executed."
pytorch_lightning.utilities.exceptions.MisconfigurationException: The closure hasn't been executed. HINT: did you call `optimizer_closure()` in your `optimizer_step` hook? It could also happen because the `optimizer.step(optimizer_closure)` call did not execute it internally.

I read the Pytorch Lightning 1.6.1 documentation. It seems that under the hood, the Lightning Trainer handles the training loop details for me. I don't know how to handle the "optimizer.step()" by myself...

The following is my code of calling Trainer:

runner = Trainer(checkpoint_callback=checkpoint_callback,
                 resume_from_checkpoint=model_path,
                 logger=tt_logger,
                 log_every_n_steps=100,
                 weights_summary='full',
                 # early_stop_callback = False,
                 **config['trainer_params'])

And I defined the following function in my model:

def forward(self, input: Tensor, **kwargs) -> Tensor
def training_step(self, batch, batch_idx, optimizer_idx)
def optimizer_step(self, epoch, batch_idx, optimizer, optimizer_idx, def optimizer_closure, on_tpu, using_native_amp, using_lbfgs)
on_load_checkpoint(self, checkpoint)
def training_epoch_end(self, outputs)
def configure_optimizers(self)
def train_dataloader(self)
def data_transforms(self)

This is my first time asking a question here. If there is something wrong, please forgive me. Thank you all for your help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source