def poisson_loss(y_true, y_pred):
y_pred = tf.cast(y_pred, tf.float32)
y_true = tf.cast(y_true, tf.float32)
# we can use the Possion PMF from TensorFlow as well
# dist = tf.contrib.distributions
# return -tf.reduce_mean(dist.Poisson(y_pred).log_pmf(y_true))
nelem = _nelem(y_true)
y_true = _nan2zero(y_true)
# last term can be avoided since it doesn't depend on y_pred
# however keeping it gives a nice lower bound to zero
ret = y_pred - y_true*tf.log(y_pred+1e-10) + tf.lgamma(y_true+1.0)
return tf.divide(tf.reduce_sum(ret), nelem)
# We need a class (or closure) here,
# because it's not possible to
# pass extra arguments to Keras loss functions
# See https://github.com/fchollet/keras/issues/2121
# dispersion (theta) parameter is a scalar by default.
# scale_factor scales the nbinom mean before the
# calculation of the loss to balance the
# learning rates of theta and network weights
评论列表
文章目录