How to accumulate metrics for multiple validation dataloaders #5793
-
❓ Questions and HelpWhat is your question?How to accumulate metrics for multiple validation dataloaders separately? Currently the metrics are accumulated for all dataloaders simultaneously. CodeThe validation step accepts def validation_step(self, batch, batch_idx, dataset_idx: Optional[int] = None): However I'm not sure how to update the metrics separately for each dataloader. Would I have to create separate metrics, one for dataset A and second for B? Or maybe my metric could accept the This however wouldn't work with pl factory metrics like average precision, since they are dataset agnostic? def update(self, preds: torch.Tensor, target: torch.Tensor): Not sure how to approach this. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 2 replies
-
You would have to create seperate metrics per validation dataloader (similar to how you need seperate metrics for train/val/test). Something like this could maybe work for you def __init__(self, ...)
...
self.val_metrics = nn.ModuleList([pl.metrics.Accuracy() for _ in range(n_val_dataloaders)])
def validation_step(self, batch, batch_idx, dataset_idx):
...
self.val_metrics[dataset_idx].update(preds, target) |
Beta Was this translation helpful? Give feedback.
-
Maybe it would be a good idea to add it to the documentation or as an example? |
Beta Was this translation helpful? Give feedback.
-
So there already is this note in the documentation: |
Beta Was this translation helpful? Give feedback.
-
I was trying to find some information about this in the docs, but phrases "multiple dataloaders" or "metrics for multiple dataloaders" didn't get me what I was looking for.
def __init__(self, ...)
...
self.val_metrics = nn.ModuleList([pl.metrics.Accuracy() for _ in range(n_val_dataloaders)])
def validation_step(self, batch, batch_idx, dataset_idx):
...
self.val_metrics[dataset_idx].update(preds, target) I have not yet tested your example but this is just something that I was hoping to find in the documentation :). |
Beta Was this translation helpful? Give feedback.
-
I think it is a great idea to add a second note. Would you be up for submitting a PR @potipot ? |
Beta Was this translation helpful? Give feedback.
-
I'm still not sure though if aggregation is the default behavior with the new metrics API. I'm trying to verify this or maybe you can confirm? |
Beta Was this translation helpful? Give feedback.
-
If you have a metric object for each individual dataloader, then the metric should accumulate separately. |
Beta Was this translation helpful? Give feedback.
-
But if we just do def __init__(self, ...)
...
self.val_metrics = pl.metrics.Accuracy()
def validation_step(self, batch, batch_idx, dataset_idx):
...
self.val_metrics.update(preds, target) they would be evaluated across all dataloaders and computed only at the end? |
Beta Was this translation helpful? Give feedback.
-
Yes exactly :] |
Beta Was this translation helpful? Give feedback.
You would have to create seperate metrics per validation dataloader (similar to how you need seperate metrics for train/val/test). Something like this could maybe work for you