Skip to content
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

Adding binary sensitivity and specificity loss functions. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions CusFns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from keras import backend as K

def binary_sensitivity(y_true, y_pred):
import tensorflow as tf
y_true = tf.minimum(tf.maximum(y_true - 0.499, 0) * 10000, 1)
y_pred = tf.minimum(tf.maximum(y_pred - 0.499, 0) * 10000, 1)

cp = K.sum(y_true)
tp_tensor = y_true * y_pred
tp = K.sum(tp_tensor)
return tp/cp

def binary_sensitivity_loss(y_true, y_pred):
import tensorflow as tf
y_true = tf.minimum(tf.maximum(y_true - 0.499, 0) * 10000, 1)
y_pred = tf.minimum(tf.maximum(y_pred - 0.499, 0) * 10000, 1)
cp = K.sum(y_true)
tp_tensor = y_true * y_pred
tp = K.sum(tp_tensor)
return 1 - tp / cp

def binary_specificity(y_true, y_pred):
import tensorflow as tf
y_true = tf.minimum(tf.maximum(y_true - 0.499, 0) * 10000, 1)
y_pred = tf.minimum(tf.maximum(y_pred - 0.499, 0) * 10000, 1)
ones_tensor = tf.ones_like(y_true)
cn_tensor = ones_tensor - y_true
cn = K.sum(cn_tensor)
y_pred_n = ones_tensor - y_pred
tn = K.sum(cn_tensor * y_pred_n)
return tn/cn

def binary_specificity_loss(y_true, y_pred):
import tensorflow as tf
y_true = tf.minimum(tf.maximum(y_true - 0.499, 0) * 10000, 1)
y_pred = tf.minimum(tf.maximum(y_pred - 0.499, 0) * 10000, 1)
ones_tensor = tf.ones_like(y_true)
cn_tensor = ones_tensor - y_true
cn = K.sum(cn_tensor)
y_pred_n = ones_tensor - y_pred
tn = K.sum(cn_tensor * y_pred_n)
return 1 - tn/cn