code: def dice_plus_xent_loss(prediction, ground_truth, weight_map=None):
      """
      Function to calculate the loss used in https://arxiv.org/pdf/1809.10486.pdf,
      no-new net, Isenseee et al (used to win the Medical Imaging Decathlon).
  
      It is the sum of the cross-entropy and the Dice-loss.
  
      :param prediction: the logits
      :param ground_truth: the segmentation ground truth
      :param weight_map:
      :return: the loss (cross_entropy + Dice)
  
      """
      num_classes = tf.shape(prediction)[-1]
  
      prediction = tf.cast(prediction, tf.float32)
      loss_xent = cross_entropy(
          prediction, ground_truth, weight_map=weight_map
      )
  
      # Dice as according to the paper:
      one_hot = labels_to_one_hot(ground_truth, num_classes=num_classes)
      softmax_of_logits = tf.nn.softmax(prediction)
  
      if weight_map is not None:
          weight_map_nclasses = tf.tile(
              tf.reshape(weight_map, [-1, 1]), [1, num_classes]
          )
          dice_numerator = 2.0 * tf.sparse_reduce_sum(
              weight_map_nclasses * one_hot * softmax_of_logits,
              reduction_axes=[0],
          )
          dice_denominator = tf.reduce_sum(
              weight_map_nclasses * softmax_of_logits,
              reduction_indices=[0],
          ) + tf.sparse_reduce_sum(
              one_hot * weight_map_nclasses, reduction_axes=[0]
          )
      else:
          dice_numerator = 2.0 * tf.sparse_reduce_sum(
              one_hot * softmax_of_logits, reduction_axes=[0]
          )
          dice_denominator = tf.reduce_sum(
              softmax_of_logits, reduction_indices=[0]
          ) + tf.sparse_reduce_sum(one_hot, reduction_axes=[0])
  
      epsilon = 0.00001
      loss_dice = -(dice_numerator + epsilon) / (
          dice_denominator + epsilon
      )
      dice_numerator = tf.Print(
          dice_denominator,
          [dice_numerator, dice_denominator, loss_dice],
      )
  
      return loss_dice + loss_xent
  
imports: from __future__ import absolute_import, print_function, division
  import numpy
  import tensorflow
  from niftynet.engine.application_factory import LossSegmentationFactory
  from niftynet.layer.base_layer import Layer
label_desc: Use image node_id 6 for calling a global function with example usage: dice_plus_xent_loss(prediction, ground_truth, weight_map) and returns: unknown