Progress Bar Variables from Validation Step #6688
-
Greetings, I can only show metrics of variables calculated on training step but can't show validation step metrics on the progress bar. How can show a metric in the validation step ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi It works fine for me. Have a look at this running example (latest PL version 1.2.6: import os
import torch
from torch.utils.data import Dataset
from pytorch_lightning import LightningModule, Trainer
class RandomDataset(Dataset):
def __init__(self, size, length):
self.len = length
self.data = torch.randn(length, size)
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return self.len
class BoringModel(LightningModule):
def __init__(self):
super().__init__()
self.layer = torch.nn.Linear(32, 2)
def forward(self, x):
return self.layer(x)
def loss(self, batch, prediction):
# An arbitrary loss to have a loss that updates the model weights during `Trainer.fit` calls
return torch.nn.functional.mse_loss(prediction, torch.ones_like(prediction))
def step(self, x):
x = self.layer(x)
out = torch.nn.functional.mse_loss(x, torch.ones_like(x))
return out
def training_step(self, batch, batch_idx):
output = self.layer(batch)
loss = self.loss(batch, output)
return {"loss": loss}
def training_step_end(self, training_step_outputs):
return training_step_outputs
def training_epoch_end(self, outputs) -> None:
torch.stack([x["loss"] for x in outputs]).mean()
def validation_step(self, batch, batch_idx):
output = self.layer(batch)
loss = self.loss(batch, output)
self.log("VALIDATION_STEP", loss, prog_bar=True)
return {"x": loss}
def validation_epoch_end(self, outputs) -> None:
torch.stack([x['x'] for x in outputs]).mean()
def test_step(self, batch, batch_idx):
output = self.layer(batch)
loss = self.loss(batch, output)
return {"y": loss}
def test_epoch_end(self, outputs) -> None:
torch.stack([x["y"] for x in outputs]).mean()
def configure_optimizers(self):
optimizer = torch.optim.SGD(self.layer.parameters(), lr=0.1)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1)
return [optimizer], [lr_scheduler]
def test_run():
train_data = torch.utils.data.DataLoader(RandomDataset(32, 64))
val_data = torch.utils.data.DataLoader(RandomDataset(32, 64))
# model
model = BoringModel()
trainer = Trainer(
default_root_dir=os.getcwd(),
limit_train_batches=1,
limit_val_batches=1,
max_epochs=3,
weights_summary=None,
)
trainer.fit(model, train_data, val_data)
if __name__ == '__main__':
test_run() |
Beta Was this translation helpful? Give feedback.
-
Hi. It seems that you run the program in pycharm. I would also encounter that problem as below. Epoch 0: 50%|███████▌ | 1/2 [00:00<00:00, 793.10it/s, loss=1.28, v_num=0]
Validating: 0it [00:00, ?it/s]
Epoch 0: 100%|█| 2/2 [00:00<00:00, 547.39it/s, loss=1.28, v_num=0, VALIDATION_ST
Epoch 1: 50%|▌| 1/2 [00:00<00:00, 1027.76it/s, loss=4.07, v_num=0, VALIDATION_S
Validating: 0it [00:00, ?it/s]
Epoch 1: 100%|█| 2/2 [00:00<00:00, 621.22it/s, loss=4.07, v_num=0, VALIDATION_ST
Epoch 2: 50%|▌| 1/2 [00:00<00:00, 1037.94it/s, loss=3.73, v_num=0, VALIDATION_S
Validating: 0it [00:00, ?it/s]
Epoch 2: 100%|█| 2/2 [00:00<00:00, 634.89it/s, loss=3.73, v_num=0, VALIDATION_ST
Epoch 2: 100%|█| 2/2 [00:00<00:00, 475.49it/s, loss=3.73, v_num=0, VALIDATION_ST
Process finished with exit code 0 I found this solution, but didn't try yet. |
Beta Was this translation helpful? Give feedback.
Hi
It works fine for me. Have a look at this running example (latest PL version 1.2.6: