code: def test_batch_normalization_train_grad_grad():
      utt.seed_rng()
  
      for axes in ("per-activation", "spatial", (1, 2, 3, 4)):
          for vartype in (
              T.tensor5,
              T.tensor4,
              T.tensor3,
              T.matrix,
              T.vector,
          ):
              # run these experiments with float64 for sufficient numerical stability
              x, dy, scale, x_mean, x_invstd = (
                  vartype(n, dtype="float64")
                  for n in ("x", "dy", "scale", "x_mean", "x_invstd")
              )
              ndim = x.ndim
  
              # reference forward pass
              if axes == "per-activation":
                  axes = (0,)
              elif axes == "spatial":
                  axes = (0,) + tuple(range(2, ndim))
              else:
                  # remove non-existing axes
                  axes = tuple(i for i in axes if i < ndim)
              if len(axes) == 0:
                  continue
  
              def bn_grad_wrt_inputs_f(x, dy, scale, x_mean, x_invstd):
                  (
                      g_inputs,
                      g_scale,
                      g_bias,
                  ) = bn.AbstractBatchNormTrainGrad(axes)(
                      x, dy, scale, x_mean, x_invstd
                  )
                  return g_inputs
  
              def bn_grad_wrt_scale_f(x, dy, scale, x_mean, x_invstd):
                  (
                      g_inputs,
                      g_scale,
                      g_bias,
                  ) = bn.AbstractBatchNormTrainGrad(axes)(
                      x, dy, scale, x_mean, x_invstd
                  )
                  return g_scale
  
              def bn_grad_wrt_bias_f(x, dy, scale, x_mean, x_invstd):
                  (
                      g_inputs,
                      g_scale,
                      g_bias,
                  ) = bn.AbstractBatchNormTrainGrad(axes)(
                      x, dy, scale, x_mean, x_invstd
                  )
                  return g_bias
  
              # run
              for data_shape in (
                  (4, 3, 3, 3, 3),
                  (4, 3, 1, 1, 1),
                  (2, 3, 5, 3, 2),
              ):
                  data_shape = data_shape[:ndim]
                  param_shape = tuple(
                      1 if d in axes else s
                      for d, s in enumerate(data_shape)
                  )
                  # force float64 for sufficient numerical stability
                  x_val = 4 + 3 * np.random.randn(*data_shape).astype(
                      "float64"
                  )
                  dy_val = -1 + 2 * np.random.randn(*data_shape).astype(
                      "float64"
                  )
                  scale_val = np.random.randn(*param_shape).astype(
                      "float64"
                  )
                  x_mean_val = np.random.randn(*param_shape).astype(
                      "float64"
                  )
                  x_invstd_val = np.random.randn(*param_shape).astype(
                      "float64"
                  )
  
                  utt.verify_grad(
                      bn_grad_wrt_inputs_f,
                      [
                          x_val,
                          dy_val,
                          scale_val,
                          x_mean_val,
                          x_invstd_val,
                      ],
                      abs_tol=5e-4,
                      rel_tol=5e-4,
                  )
                  utt.verify_grad(
                      bn_grad_wrt_scale_f,
                      [
                          x_val,
                          dy_val,
                          scale_val,
                          x_mean_val,
                          x_invstd_val,
                      ],
                  )
                  utt.verify_grad(
                      bn_grad_wrt_bias_f,
                      [
                          x_val,
                          dy_val,
                          scale_val,
                          x_mean_val,
                          x_invstd_val,
                      ],
                  )
  
file_path: Theano/theano/tensor/nnet/tests/test_bn.py
imports: from __future__ import absolute_import, print_function, division
  import theano
  import theano.tensor
  from theano.tests import unittest_tools
  import numpy
  from collections import OrderedDict
  from theano.tensor.nnet import bn
label_desc: Use image node_id 5 for calling a global function with example usage: test_batch_normalization_train_grad_grad() and returns: g_inputs, g_scale, g_bias