code: def loss_variability(ground_truth, pred_multi, weight_map=None):
      one_hot_gt, output_shape = labels_to_one_hot(
          tf.cast(ground_truth, tf.int64), tf.shape(pred_multi)[-1]
      )
      dense_gt = tf.sparse_tensor_to_dense(one_hot_gt)
      pred_hard = tf.argmax(pred_multi, -1)
  
      one_hot_pred, _ = labels_to_one_hot(
          tf.cast(pred_hard, tf.int64), tf.shape(pred_multi)[-1]
      )
      dense_pred = tf.sparse_tensor_to_dense(one_hot_pred)
      freq_pred = tf.divide(
          tf.reduce_sum(dense_pred, 1),
          tf.cast(tf.shape(pred_multi)[1], tf.float32),
      )
      variability_pred = tf.reduce_sum(tf.square(freq_pred), -1)
      freq_gt = tf.divide(
          tf.reduce_sum(dense_gt, 1),
          tf.cast(tf.shape(pred_multi)[1], tf.float32),
      )
      variability_gt = tf.reduce_sum(tf.square(freq_gt), -1)
  
      diff_square = tf.square(variability_gt - variability_pred)
      if weight_map is not None:
          diff_square = weight_map * diff_square
      loss = tf.sqrt(tf.reduce_mean(diff_square))
      return loss
  
file_path: NiftyNet/niftynet/layer/loss_classification_multi.py
imports: from __future__ import absolute_import, print_function, division
  import numpy
  import tensorflow
  from niftynet.engine.application_factory import LossClassificationMultiFactory
  from niftynet.layer.base_layer import Layer
label_desc: Use image node_id 4 for calling a global function with example usage: loss_variability(ground_truth, pred_multi, weight_map) and returns: loss