如何使用TensorFlow后端计算Keras中的KL散度? [英] How do I compute the KL divergence in Keras with TensorFlow backend?

查看:1401
本文介绍了如何使用TensorFlow后端计算Keras中的KL散度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用tensorflow作为后端时,如何计算喀拉拉邦的kl消散?我计算L1损耗如下:

How can I compute kl diveregence in keras while using tensorflow as backend? I compute L1 loss as follows:

def l1_loss(y_true, y_pred):
    return K.sum(K.abs(y_pred - y_true), axis=-1)

推荐答案

Keras已经实施了KL分叉,可以看到

Keras already has the KL-divergence implemented, as it can be seen here, the code is just:

def kullback_leibler_divergence(y_true, y_pred):
    y_true = K.clip(y_true, K.epsilon(), 1)
    y_pred = K.clip(y_pred, K.epsilon(), 1)
    return K.sum(y_true * K.log(y_true / y_pred), axis=-1)

因此,只需使用kld,KLD或kullback_leibler_divergence作为损失即可.

So just use kld, KLD or kullback_leibler_divergence as loss.

这篇关于如何使用TensorFlow后端计算Keras中的KL散度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆