code: def gradient_check(
      seq_len, input_size, hidden_size, batch_size, threshold=1.0e-3
  ):
      # 'threshold' is the max fractional difference
      #             between gradient estimate and
      #             bprop deltas (def is 5%)
      # for a given set of layer parameters calculate
      # the gradients and compare to the derivatives
      # obtained with the bprop function.  repeat this
      # for a range of perturbations and use the
      # perturbation size with the best results.
      # This is necessary for 32 bit computations
  
      min_max_err = -1.0  # minimum max error
      neon_logger.display("Perturb mag, max grad diff")
      for pert_exp in range(-5, 0):
          # need to generate the scaling and input outside
          # having an issue with the random number generator
          # when these are generated inside the gradient_calc
          # function
          input_shape = (input_size, seq_len * batch_size)
          output_shape = (hidden_size, seq_len * batch_size)
          rand_scale = np.random.random(output_shape) * 2.0 - 1.0
  
          inp = np.random.randn(*input_shape)
  
          pert_mag = 10.0**pert_exp
          (grad_est, deltas) = gradient_calc(
              seq_len,
              input_size,
              hidden_size,
              batch_size,
              epsilon=pert_mag,
              rand_scale=rand_scale,
              inp_bl=inp,
          )
          dd = np.max(np.abs(grad_est - deltas))
          neon_logger.display("%e, %e" % (pert_mag, dd))
          if min_max_err < 0.0 or dd < min_max_err:
              min_max_err = dd
          # reset the seed so models are same in each run
          # allclose_with_out(grad_est,deltas, rtol=0.0, atol=0.0)
          NervanaObject.be.rng_reset()
  
      # check that best value of worst case error is less than threshold
      neon_logger.display(
          "Worst case error %e with perturbation %e"
          % (min_max_err, pert_mag)
      )
      neon_logger.display("Threshold %e" % (threshold))
      assert min_max_err < threshold
  
file_path: neon/tests/test_lstm.py
imports: import itertools
  import numpy
  from neon import NervanaObject, logger
  from neon.initializers.initializer import Constant, Gaussian
  from neon.layers.recurrent import LSTM
  from neon.layers.container import DeltasTree
  from neon.transforms import Logistic, Tanh
  from lstm_ref import LSTM
  from utils import sparse_rand, allclose_with_out
label_desc: Use image node_id 9 for calling a global function with example usage: gradient_check(seq_len, input_size, hidden_size, batch_size, threshold) without return types