如何使用Keras / Tensorflow编写损失函数进行负二项式分配?

时间:2019-04-21 12:21:40

标签: python tensorflow keras deep-learning log-likelihood

我正在建模具有负二项式分布的变量。 除了预测预期平均值,我还想对分布的两个参数建模。所以我的神经网络的输出层是两个神经元。 为此,我需要编写自定义损失函数。但是下面的代码不起作用-遍历张量似乎是有问题的。 谁能帮忙,如何重写它以与tensorflow / keras一起使用?

非常感谢

我只需要重写此代码,并使用适合tensorflows张量的代码。根据我得到的错误,也许tensorflow.map_fn可能会导致解决方案,但是我对此没有运气。

总体上来说效果很好,但与Keras / Tensorflow配合使用

from scipy.stats import nbinom
from keras import backend as K
import tensorflow as tf

def loss_neg_bin(y_pred, y_true):

    result = 0.0
    for p, t in zip(y_pred, y_true):
        result += -nbinom.pmf(t, p[0], min(0.99, p[1]))

    return result

我得到的错误:

  

TypeError:仅在渴望执行时,张量对象才可迭代   已启用。要遍历此张量,请使用tf.map_fn。

1 个答案:

答案 0 :(得分:1)

您需要tf.map_fn来实现循环,并且需要tf.py_func来完成nbinom.pmf的包装。例如:

from scipy.stats import nbinom
import tensorflow as tf

def loss_neg_bin(y_pred, y_true):
    result = 0.0
    for p, t in zip(y_pred, y_true):
        result += -nbinom.pmf(t, p[0], min(0.99, p[1]))
    return result

y_pred= [[0.4, 0.4],[0.5, 0.5]]
y_true= [[1, 2],[1, 2]]
print('your version:\n',loss_neg_bin(y_pred, y_true))

def loss_neg_bin_tf(y_pred, y_true):
    result = tf.map_fn(lambda x:tf.py_func(lambda p,t:-nbinom.pmf(t, p[0], min(0.99,p[1]))
                                           ,x
                                           ,tf.float64)
                       ,(y_pred,y_true)
                       ,dtype=tf.float64)
    result = tf.reduce_sum(result,axis=0)
    return result

y_pred_tf = tf.placeholder(shape=(None,2),dtype=tf.float64)
y_true_tf = tf.placeholder(shape=(None,2),dtype=tf.float64)
loss = loss_neg_bin_tf(y_pred_tf, y_true_tf)

with tf.Session() as sess:
    print('tensorflow version:\n',sess.run(loss,feed_dict={y_pred_tf:y_pred,y_true_tf:y_true}))

# print
your version:
 [-0.34313146 -0.13616026]
tensorflow version:
 [-0.34313146 -0.13616026]

此外,如果您使用tf.py_func计算负二项式的概率质量函数作为损失反馈模型,则需要自己定义梯度函数。

更新-添加可区分的负二项式损失

nbinom的概率质量函数为:

nbinom.pmf(k) = choose(k+n-1, n-1) * p**n * (1-p)**k

根据scipy.stats.nbinom用于k >= 0

所以我添加了可微分的负二项式损失版本。

import tensorflow as tf

def nbinom_pmf_tf(x,n,p):
    coeff = tf.lgamma(n + x) - tf.lgamma(x + 1) - tf.lgamma(n)
    return tf.cast(tf.exp(coeff + n * tf.log(p) + x * tf.log(1 - p)),dtype=tf.float64)

def loss_neg_bin_tf_differentiable(y_pred, y_true):
    result = tf.map_fn(lambda x: -nbinom_pmf_tf(x[1]
                                                , x[0][0]
                                                , tf.minimum(tf.constant(0.99,dtype=tf.float64),x[0][1]))
                       ,(y_pred,y_true)
                       ,dtype=tf.float64)
    result = tf.reduce_sum(result,axis=0)
    return result

y_pred_tf = tf.placeholder(shape=(None,2),dtype=tf.float64)
y_true_tf = tf.placeholder(shape=(None,2),dtype=tf.float64)
loss = loss_neg_bin_tf_differentiable(y_pred_tf, y_true_tf)
grads = tf.gradients(loss,y_pred_tf)

y_pred= [[0.4, 0.4],[0.5, 0.5]]
y_true= [[1, 2],[1, 2]]
with tf.Session() as sess:
    print('tensorflow differentiable version:')
    loss_val,grads_val = sess.run([loss,grads],feed_dict={y_pred_tf:y_pred,y_true_tf:y_true})
    print(loss_val)
    print(grads_val)

# print
tensorflow differentiable version:
[-0.34313146 -0.13616026]
[array([[-0.42401619,  0.27393084],
       [-0.36184822,  0.37565048]])]