-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing kullback-leilbler divergence as custom Keras objective function #2473
Comments
You'll need to add fuzz factors to avoid dividing by zero. |
Hi @fchollet what exactly do you mean when you say fuzz factors? Hi @rsmichael Did you find a workaround for this yet? |
Yes, here's my function:
You have to add fuzz factors (the "eps" variable in my function) to prevent kullback leibler divergence from taking extreme (or infinite values) when the predicted probability is very low. |
Thanks @rsmichael .. Thats's exactly what I thought. |
Thanks. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed. |
Hi,
I want to train models where the gold standard data are discrete probability distributions, which come from many observations of different discrete outcomes with the same input. For my model, I want to minimize, kullback-leibler divergence, which measures the information gain from a predicted distribution, q, to a true distribution, p:
KL_div = \sum{i}p_i*log(p_i/q_i)
Where i in the index for all of the categorical possibilities.
I implemented a function that takes in vectors and computes the kullback-leibler divergence:
Here's an example of the function computing kullback-leibler divergence of two discrete distributions with length 2:
When I try to compile my model with this objective, I get the following error:
model.compile(loss=kullback_leibler, optimizer='sgd')
This suggests that instead of making a function that directly computes the kullback-leibler divergence, I instead need it to output a Theano variable. I tried to do this and I get no error in compiling my model, but I get nan as the result of my model.
Here's the function:
The call to fit my model:
And the output:
I'm not quite sure what's going wrong, but would greatly appreciate any help. I am also unsure: do keras objective functions also need to be able to pass in 2D tensors? I am not 100% sure how to do this from reading the Theano documentation.
Thanks for your help!
The text was updated successfully, but these errors were encountered: