EIGHT_UNBALANCE = 37 def weighted_binary_crossentropy(y_true, y_pred): # Calculate the binary crossentropy b_ce = K.binary_crossentropy(y_true, y_pred) # Apply the weights weight_vector = (y_true * WEIGHT_UNBALANCE + (1.-y_true)) / 2. weighted_b_ce = weight_vector * b_ce return K.mean(weighted_b_ce,axis=-1)