code: def cross_entropy(prediction, ground_truth, weight_map=None):
      """
      Function to calculate the cross-entropy loss function
  
      :param prediction: the logits (before softmax)
      :param ground_truth: the segmentation ground truth
      :param weight_map:
      :return: the cross-entropy loss
      """
      if len(ground_truth.shape) == len(prediction.shape):
          ground_truth = ground_truth[..., -1]
  
      # TODO trace this back:
      ground_truth = tf.cast(ground_truth, tf.int32)
  
      entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
          logits=prediction, labels=ground_truth
      )
  
      if weight_map is None:
          return tf.reduce_mean(entropy)
  
      weight_sum = tf.maximum(tf.reduce_sum(weight_map), 1e-6)
      return tf.reduce_sum(entropy * weight_map / weight_sum)
  
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 8 for calling a global function with example usage: cross_entropy(prediction, ground_truth, weight_map) and returns: tf, tf