Issue
I have two probability distributions. How should I find the KL-divergence between them in PyTorch? The regular cross entropy only accepts integer labels.
Solution
Yes, PyTorch has a method named kl_div
under torch.nn.functional
to directly compute KL-devergence between tensors. Suppose you have tensor a
and b
of same shape. You can use the following code:
import torch.nn.functional as F
out = F.kl_div(a, b)
For more details, see the above method documentation.
Answered By - jdhao
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.