- KL divergence用于度量两个分布P和Q的差异,这种度量【不具有】对称性
- P是实际分布(pred probs)
- Q是建模分布(gt)
- $D_{KL}(P||Q)=\sum_i P(i)ln\frac{P(i)}{Q(i)}$
- 散度定义为分布P和分布Q之间的对数差异的加权和,用P的概率去加权
- 当Q是one-hot label的时候,要先clip再log
方法
- torch.nn.functional.kl_div(input, target, size_average=None, reduce=None, reduction=’mean’)
- input:对数概率
- target:概率
- tf.distributions.kl_divergence(distribution_a, distribution_b, allow_nan_stats=True, name=None)
- distribution_a&b 来自tf.distributions.Categorical(logits=None, prob=None, …)
- 传入logits/probs,先转换成distribution,再计算kl divergence
- torch.nn.KLDivLoss
- tf.keras.losses.KLDivergence
- tf.keras.losses.kullback_leibler_divergence
- torch.nn.functional.kl_div(input, target, size_average=None, reduce=None, reduction=’mean’)
code
1 | # torch version |