Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/lightning/pytorch/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,22 @@ def validation_step(self, batch, batch_idx):
# CASE 2: multiple validation dataloaders
def validation_step(self, batch, batch_idx, dataloader_idx=0):
# dataloader_idx tells you which dataset this is.
...
x, y = batch

# implement your own
out = self(x)

if dataloader_idx == 0:
loss = self.loss0(out, y)
else:
loss = self.loss1(out, y)

# calculate acc
labels_hat = torch.argmax(out, dim=1)
acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

# log the outputs separately for each dataloader
self.log_dict({f"val_loss_{dataloader_idx}": loss, f"val_acc_{dataloader_idx}": acc})

Note:
If you don't need to validate you don't need to implement this method.
Expand Down Expand Up @@ -875,7 +890,22 @@ def test_step(self, batch, batch_idx):
# CASE 2: multiple test dataloaders
def test_step(self, batch, batch_idx, dataloader_idx=0):
# dataloader_idx tells you which dataset this is.
...
x, y = batch

# implement your own
out = self(x)

if dataloader_idx == 0:
loss = self.loss0(out, y)
else:
loss = self.loss1(out, y)

# calculate acc
labels_hat = torch.argmax(out, dim=1)
acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

# log the outputs separately for each dataloader
self.log_dict({f"test_loss_{dataloader_idx}": loss, f"test_acc_{dataloader_idx}": acc})

Note:
If you don't need to test you don't need to implement this method.
Expand Down
Loading