Created
August 29, 2019 09:32
-
-
Save ali-mosavian/ea24a6ccc2763ac16a6a4504bf845f24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import keras.backend as K | |
import keras.losses as losses | |
import keras.metrics as metrics | |
def zero_loss(targets, outputs): | |
return losses.categorical_crossentropy(targets, outputs)*0 | |
def categorical_crossentropy_(target_mask_value=None): | |
def _no_mask(y_true, y_pred): | |
return K.categorical_crossentropy(y_true, y_pred) | |
def _with_mask(y_true, y_pred): | |
mask = K.cast(K.not_equal(y_true, target_mask_value), K.floatx()) | |
eps = (1.0-mask)*K.epsilon() | |
return K.categorical_crossentropy(y_true*mask+eps, y_pred*mask+eps) | |
func = _no_mask if target_mask_value is None else _with_mask | |
func.__name__ = 'categorical_crossentropy' | |
return func | |
def categorical_accuracy_(target_mask_value=None): | |
def _no_mask(y_true, y_pred): | |
return metrics.categorical_accuracy(y_true, y_pred) | |
def _with_mask(y_true, y_pred): | |
mask = K.cast(K.not_equal(y_true, -1), K.floatx()) | |
return metrics.categorical_accuracy(y_true * mask, y_pred * mask) | |
func = _no_mask if target_mask_value is None else _with_mask | |
func.__name__ = 'categorical_accuracy' | |
return func | |
def precision_(target_mask_value=None): | |
def no_mask(y_true, y_pred): | |
tp = K.sum(K.cast(y_true*y_pred, K.floatx()), axis=0) | |
fp = K.sum(K.cast((1-y_true)*y_pred, K.floatx()), axis=0) | |
return tp / (tp + fp + K.epsilon()) | |
def with_mask(y_true, y_pred): | |
mask = K.cast(K.not_equal(y_true, target_mask_value), K.floatx()) | |
tp = K.sum(K.cast(y_true*y_pred, K.floatx())*mask, axis=0) | |
fp = K.sum(K.cast((1-y_true)*y_pred, K.floatx())*mask, axis=0) | |
return tp / (tp + fp + K.epsilon()) | |
func = no_mask if mask is None else with_mask | |
func.__name__ = 'precision' | |
return func | |
def recall_(target_mask_value=None): | |
def no_mask(y_true, y_pred): | |
tp = K.sum(K.cast(y_true*y_pred, K.floatx()), axis=0) | |
fn = K.sum(K.cast(y_true*(1-y_pred), K.floatx()), axis=0) | |
return tp / (tp + fn + K.epsilon()) | |
def with_mask(y_true, y_pred): | |
mask = K.cast(K.not_equal(y_true, target_mask_value), K.floatx()) | |
tp = K.sum(K.cast(y_true*y_pred, K.floatx())*mask, axis=0) | |
fn = K.sum(K.cast(y_true*(1-y_pred), K.floatx())*mask, axis=0) | |
return tp / (tp + fn + K.epsilon()) | |
func = no_mask if mask is None else with_mask | |
func.__name__ = 'recall' | |
return func | |
def f1_(target_mask_value=None): | |
calc_precision = precision_(target_mask_value) | |
calc_recall = recall_(target_mask_value) | |
def _calculate(y_true, y_pred): | |
p = calc_precision(y_true, y_pred) | |
r = calc_recall(y_true, y_pred) | |
f1 = 2*p*r / (p+r+K.epsilon()) | |
f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1) | |
return K.mean(f1) | |
_calculate.__name__ = 'f1' | |
return _calculate | |
def f1_loss_(target_mask_value=None): | |
calc_precision = precision_(target_mask_value) | |
calc_recall = recall_(target_mask_value) | |
def _calculate(y_true, y_pred): | |
p = calc_precision(y_true, y_pred) | |
r = calc_recall(y_true, y_pred) | |
f1 = 2*p*r / (p+r+K.epsilon()) | |
f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1) | |
return K.mean(f1) | |
_calculate.__name__ = 'f1_loss' | |
return _calculate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment