5

I'm working with a highly skewed count dataset that's typically fit with a negative binomial. I want to use the negative binomial as a loss functions in Keras or Tensorflow on a feed forward neural network.

To my knowledge, after looking through available loss functions, such a function doesn't exist for keras or tensorflow (although I'm hoping I'm wrong and I just missed something).

I've looked around and I've seen posts suggest just swapping out the loss to include the negative binomial, but it appears there's more to creating a custom loss in tensorflow than just swapping it out with tf.contrib.distributions.NegativeBinomial

There are several posts that talk about creating custom loss functions for Tensorflow / keras, e.g.: https://datascience.stackexchange.com/questions/25029/custom-loss-function-with-additional-parameter-in-keras After looking at this, I'm struggling to figure out how to code this.

I'm wondering if 1) anyone has already created a loss function using negative binomial and is willing to share how it was coded or 2) tips on how to make this work.

I'm working exclusively in python on this project.

Appreciate all the help in advance.

1
  • I am trying to solve same thing. Have you got any luck to solve this? How to define such a loss and metric?
    – gugatr0n1c
    Apr 19, 2019 at 15:18

1 Answer 1

1

I don't know about negative binomial loss, but I do know how to implement a custom loss function

def custom_loss(y_true, y_pred):
    ...
    loss = ...
    return loss

Treat y_true and y_pred as tensors. (this means instead of using functions such as np.pow(y_true,2) you need to use functions from tensorflow or keras.backend)

pass your custom loss function as compile argument

model.compile(loss=custom_loss, optimizer='your favorite optimizer')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.