6

I need to minimize KL loss in tensorflow.

I tried this function tf.contrib.distributions.kl(dist_a, dist_b, allow_nan=False, name=None), but I failed.

I tried to implement it manually:

def kl_divergence(p,q):
    return p* tf.log(p/q)+(1-p)*tf.log((1-p)/(1-q))

Is it correct?

nbro
  • 15,395
  • 32
  • 113
  • 196
Alberto Merciai
  • 474
  • 1
  • 5
  • 17
  • 1
    Possible duplicate of [KL Divergence in TensorFlow](http://stackoverflow.com/questions/41863814/kl-divergence-in-tensorflow) – Transcendental Apr 09 '17 at 00:11

1 Answers1

10

What you have there is the cross entropy, KL divergence should be something like:

def kl_divergence(p, q): 
    return tf.reduce_sum(p * tf.log(p/q))

This assumes that p and q are both 1-D tensors of float, of the same shape and for each, their values sum to 1.

It should also work if p and q are equally sized mini-batches of 1-D tensors that obey the above constraints.

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
Daniel Slater
  • 4,123
  • 4
  • 28
  • 39