smooth = 1. intersection = tf.reduce_sum(flat_logits * flat_labels) dice_score = (2 * intersection + smooth) / ( tf.reduce_sum(flat_labels) + tf.reduce_sum(flat_logits) + smooth) dice_loss = 1 - dice_score cross_entropy_loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=flat_logits, labels=flat_labels)) loss = dice_loss + cross_entropy_loss