def w_categorical_crossentropy(y_true, y_pred, weights): nb_cl = len(weights) final_mask = K.zeros_like(y_pred[:,:, 0]) y_pred_max = K.max(y_pred, axis=-1) y_pred_max = K.expand_dims(y_pred_max, axis=-1) y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx()) for c_p, c_t in itertools.product(range(nb_cl), range(nb_cl)): final_mask += (weights[c_t, c_p] * y_pred_max_mat[:,:, c_p] * y_true[:,:, c_t]) #return K.mean( K.categorical_crossentropy(y_pred, y_true) * final_mask ) return K.categorical_crossentropy(y_pred, y_true) * final_mask
def accuracy(y_true, y_predicted): y = tf.argmax(y_true, axis=-1) y_ = tf.argmax(y_predicted, axis=-1) mask = tf.greater(y, 0) return K.cast(K.equal(tf.boolean_mask(y, mask), tf.boolean_mask(y_, mask)), K.floatx())
def weighted_accuracy(y_true, y_pred): return K.sum( K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)) * K.sum(y_true, axis=-1)) / K.sum(y_true)