How to implement a custom Metric based on sklearn's functions #5810
Replies: 3 comments
-
Calling |
Beta Was this translation helpful? Give feedback.
-
Thanks for your suggestions. It really helped me a lot. By the way, is the |
Beta Was this translation helpful? Give feedback.
-
Yes, first sync states from all processes and then compute the value. |
Beta Was this translation helpful? Give feedback.
-
❓ Questions and Help
What is your question?
How to implement a custom Metric based on sklearn's functions?
Code
I tried to implement a ROCAUC Metric with help of sklearn's roc_auc_score, because it supports multilabel classification.
In the training procedure, I log the ROCAUC score along with other metrics at the end of validation epoch.
It worked fine with single GPU training (without ddp).
However, when using
ddp
accelerator and settingmove_metrics_to_cpu = True
. It threw the following error:It seemed to be caused by
sync_dist=True
in thelog
function. When I modified the last line inROCAUC
'scompute
function:the error is solved.
When we wrap the sklearn's metric function into pytorch-lightning's Metric, we have to first convert
Tensor
tonumpy.ndarray
, then we have to convertnumpy.ndarray
toTensor
after computing the metric value.So my question is: Is there a better way than manually moving the
Tensor
to GPU like this?What's your environment?
Beta Was this translation helpful? Give feedback.
All reactions