code: def _computing_gradient_norm(displacement, flag_L1=False):
      """
  
      :param displacement: tensor, displacement field
      :param flag_L1: boolean, True if L1 norm shoudl be used
      :return: L2 (or L1) norm of gradients
      """
      norms = []
      for spatial_ind in range(infer_spatial_rank(displacement)):
          dTdt = ImgGrad(spatial_axis=spatial_ind)(displacement)
          if flag_L1:
              norms.append(tf.abs(dTdt))
          else:
              norms.append(dTdt * dTdt)
      return tf.reduce_mean(norms)
  
imports: from __future__ import absolute_import, print_function
  import numpy
  import tensorflow
  from tensorflow.contrib.layers.python.layers import regularizers
  from niftynet.engine.application_initializer import GlorotUniform
  from niftynet.layer.convolution import ConvolutionalLayer
  from niftynet.layer.downsample_res_block import DownBlock
  from niftynet.layer.grid_warper import _create_affine_features
  from niftynet.layer.layer_util import infer_spatial_rank, check_spatial_dims
  from niftynet.layer.linear_resize import LinearResizeLayer
  from niftynet.layer.spatial_gradient import SpatialGradientLayer
  from niftynet.layer.upsample_res_block import UpBlock
  from niftynet.network.base_net import BaseNet
label_desc: Use image node_id 6 for calling a global function with example usage: _computing_gradient_norm(displacement, flag_L1) and returns: tf