Skip to content

Instantly share code, notes, and snippets.

@jerheff
Created January 9, 2016 01:08
Show Gist options
  • Save jerheff/8cf06fe1df0695806456 to your computer and use it in GitHub Desktop.
Save jerheff/8cf06fe1df0695806456 to your computer and use it in GitHub Desktop.
Experimental binary cross entropy with ranking loss function
def binary_crossentropy_with_ranking(y_true, y_pred):
""" Trying to combine ranking loss with numeric precision"""
# first get the log loss like normal
logloss = K.mean(K.binary_crossentropy(y_pred, y_true), axis=-1)
# next, build a rank loss
# clip the probabilities to keep stability
y_pred_clipped = K.clip(y_pred, K.epsilon(), 1-K.epsilon())
# translate into the raw scores before the logit
y_pred_score = K.log(y_pred_clipped / (1 - y_pred_clipped))
# determine what the maximum score for a zero outcome is
y_pred_score_zerooutcome_max = K.max(y_pred_score * (y_true <1))
# determine how much each score is above or below it
rankloss = y_pred_score - y_pred_score_zerooutcome_max
# only keep losses for positive outcomes
rankloss = rankloss * y_true
# only keep losses where the score is below the max
rankloss = K.square(K.clip(rankloss, -100, 0))
# average the loss for just the positive outcomes
rankloss = K.sum(rankloss, axis=-1) / (K.sum(y_true > 0) + 1)
# return (rankloss + 1) * logloss - an alternative to try
return rankloss + logloss
@mpearmain
Copy link

What is K in this example?

@RmDr
Copy link

RmDr commented Oct 30, 2016

Think K is a python library working with tensors like tensorflow or theano.

@tomcwalker
Copy link

tomcwalker commented Oct 30, 2017

K is Keras, It's a Neural Network framework which gives very fast prototyping, and sits on top of Keras or Theano.

@YaronBlinder
Copy link

K is the backend (could be tensorflow, theano, or cntk at the time of writing).
It is common when writing functions for keras to import the backend as K:
from keras import backend as K

@Anirudhsekar96
Copy link

import keras.backend as K

^ That's where K comes from.

@rahimentezari
Copy link

This is based on which paper? I am going to cite the original paper.

@jerheff
Copy link
Author

jerheff commented Aug 27, 2019

I can't remember for sure, but I don't think I based this on a paper.

@SamuelMarks
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment