Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative Binomial Loss in Neural Network using Tensorflow / Keras

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.

like image 802
JCMRE Avatar asked Nov 08 '22 02:11

JCMRE


1 Answers

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')
like image 75
Mete Han Kahraman Avatar answered Nov 14 '22 23:11

Mete Han Kahraman