Lightning-AI / Lightning-AI/pytorch-lightning
PyTorch Lightning module doesn't save logged val loss? ModelCheckpoint error when using trainer.tune
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 31.4k
- Forks
- 3.8k
- Avg merge
- 6d 7h
- Merged PRs (30d)
- 6
Description
### Bug description
I'm running an LSTM-based model training on Kaggle. I use Pytorch Lightning and wandb logger for that.
That's my model's class:
```
class Model(pl.LightningModule):
def __init__(
self,
input_size: int,
hidden_size: int,
bidirectional: bool = False,
lstm_layers: int = 1,
lstm_dropout: float = 0.4,
fc_dropout: float = 0.4,
lr: float = 0.01,
lr_scheduler_patience: int = 2,
):
super().__init__()
self.lr = lr
self.save_hyperparameters()
# LSTM
self.encoder_lstm = nn.LSTM(
input_size=input_size,
hidden_size=hidden_size,
num_layers=lstm_layers,
bidirectional=bidirectional,
dropout=lstm_dropout if lstm_layers > 1 else 0,
batch_first=True,
)
# Fully-connected
num_directions = 2 if bidirectional else 1
self.fc = nn.Sequential(
nn.Linear(
hidden_size * num_directions, hidden_size * num_directions * 2
),
nn.ReLU(),
nn.Dropout(fc_dropout),
nn.Linear(hidden_size * num_directions * 2, input_size),
)
self.loss_function = nn.MSELoss()
def configure_optimizers(self):
optimizer = torch.optim.AdamW(self.parameters(), lr=self.lr)
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, patience=self.hparams.lr_scheduler_patience
),
"monitor": "val_loss",
},
}
def forward(self, x, prev_state):
...
def training_step(self, batch, batch_idx):
loss, _ = self._step(batch)
self.log("train_loss", loss)
return loss
def validation_step(self, batch, batch_idx):
loss, embeddings = self._step(batch)
self.log("val_loss", loss)
return {
'val_loss': loss,
'preds': embeddings # this is consumed by my custom callback
}
def test_step(self, batch, batch_idx):
loss, _ = self._step(batch)
self.log("test_loss", loss)
```
And that's how I use it:
```
model = Model(
bidirectional=False,
lstm_layers=1,
lstm_dropout=0.4,
fc_dropout=0.4,
lr=0.01,
lr_scheduler_patience=2
)
...
checkpoint_callback = ModelCheckpoint(
monitor="val_loss",
every_n_train_steps=100,
verbose=True
)
trainer = pl.Trainer(
accelerator='gpu',
precision=16,
max_epochs=100,
callbacks=[early_stopping, checkpoint_callback, lr_monitor, custom_callback],
log_every_n_steps=50,
logger=wandb_logger,
auto_lr_find=True,
)
trainer.tune(model, train_dataloaders=train_dataloader, val_dataloaders=val_dataloader)
trainer.fit(model, train_dataloader, val_dataloader)
```
When I don't run `trainer.tune(model, train_dataloaders=train_dataloader, val_dataloaders=val_dataloader)` `trainer.fit` works perfectly but when I run 'trainer.tune' I get such ModelCheckpoint error (when running `fit`)
```
MisconfigurationException: `ModelCheckpoint(monitor='val_loss')` could not find the monitored key in the returned metrics: ['train_loss', 'epoch', 'step']. HINT: Did you call `log('val_loss', value)` in the `LightningModule`?
```
So even though I log `val_loss` it doesn't get saved. On the Trainer object I set `log_every_n_steps=50` and on the ModelCheckpoint I set `every_n_train_steps=100` so it seems that it should have 'val_loss' logged by the moment ModelCheckpoint gets going.
I printed val loss in `validation_step` and it gets computed before ModelCheckpoint is run. I also defined an `on_train_batch_end` function in my custom callback to see saved trainer metrics. It turns out that val loss is in fact missing.
### How to reproduce the bug
_No response_
### Error messages and logs
```
MisconfigurationException: `ModelCheckpoint(monitor='val_loss')` could not find the monitored key in the returned metrics: ['train_loss', 'epoch', 'step']. HINT: Did you call `log('val_loss', value)` in the `LightningModule`?
```
### Environment
Current environment
```
WandB version: 0.13.9
OS: Linux-5.15.65+-x86_64-with-debian-bullseye-sid
Python version: 3.7.12
Versions of relevant libraries:
Pytorch-Lightning: 1.8.6
```
### More info
_No response_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided trainer.tune and trainer.fit calls, then inspect how ModelCheckpoint receives metrics after validation_step logs val_loss. Reproduce the missing-key error and determine why val_loss is absent from the checkpoint metrics; the issue is done when the documented setup exposes val_loss to ModelCheckpoint consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100