code: def check_backward(
      func,
      x_data,
      y_grad,
      params=(),
      eps=1e-3,
      atol=1e-5,
      rtol=1e-4,
      no_grads=None,
      dtype=None,
      detect_nondifferentiable=False,
  ):
      """Test backward procedure of a given function.
  
      This function automatically checks the backward-process of a given function
      to ensure that the computed gradients are approximately correct.
      For example, assuming you've defined a :class:`~chainer.FunctionNode` class
      ``MyFunc``, that takes two arguments and returns one value, you can wrap
      it in a ordinary function and check its gradient computations as follows:
  
      .. code-block:: python
  
          def func(xs):
              y, = MyFunc().apply(xs)
              return y
  
          x1_data = xp.array(...)
          x2_data = xp.array(...)
          gy_data = xp.array(...)
          check_backward(func, (x1_data, x2_data), gy_data)
  
      This function creates :class:`~chainer.Variable` objects with ``x_data``
      and calls ``func`` with the :class:`~chainer.Variable`\\ s to get its
      result as :class:`~chainer.Variable`.
      Then, it sets ``y_grad`` array to ``grad`` attribute of the result and
      calls ``backward`` method to get gradients of the inputs.
      To check correctness of the gradients, the function calls
      :func:`numerical_grad` to calculate numerically the gradients and compares
      the types of gradients with :func:`chainer.testing.assert_allclose`.
  
      To reduce computational time, it uses directional derivative along a
      random vector. A function
      :math:`g: \\mathbb{R} \\rightarrow \\mathbb{R}^n` is defined as
      :math:`g(\\delta) = f(x + \\delta r)`, where
      :math:`\\delta \\in \\mathbb{R}`, :math:`r \\in \\mathbb{R}^n`
      is a random vector
      and :math:`f` is a function which you want to test.
      Its gradient is
  
      .. math::
         g'(\\delta) = f'(x + \\delta r) \\cdot r.
  
      Therefore, :math:`g'(0) = f'(x) \\cdot r`.
      So we can check the correctness of back propagation of :math:`f` indirectly
      by comparing this equation with the gradient of :math:`g` numerically
      calculated and that of :math:`f` computed by backprop.
      If :math:`r` is chosen from uniform distribution, we can conclude with
      high probability that the gradient of :math:`f` itself is correct.
  
      If the function is non-differentiable with respect to some input objects,
      we can check its backprop to such objects by ``no_grads`` argument.
      ``gradient_check`` computes numerical backward to inputs that correspond to
      ``False`` in ``no_grads``. It also asserts that the backprop leaves
      gradients ``None`` for inputs that correspond to ``True`` in ``no_grads``.
      The default of ``no_grads`` argument is the tuple of truth values whether
      input objects (``x1_data`` or/and ``x2_data`` in this example) represent
      integer variables.
  
      You can simplify a test when ``MyFunc`` gets only one argument:
  
      .. code-block:: python
  
          check_backward(func, x1_data, gy_data)
  
      If ``MyFunc`` is a loss function which returns a zero-dimensional
      array, pass ``None`` to ``gy_data``. In this case, it sets ``1`` to
      ``grad`` attribute of the result:
  
      .. code-block:: python
  
          check_backward(my_loss_func,
                         (x1_data, x2_data), None)
  
      If ``MyFunc`` returns multiple outputs, pass all gradients for outputs
      as a tuple:
  
      .. code-block:: python
  
          gy1_data = xp.array(...)
          gy2_data = xp.array(...)
          check_backward(func, x1_data, (gy1_data, gy2_data))
  
      You can also test a :class:`~chainer.Link`.
      To check gradients of parameters of the link, set a tuple of the parameters
      to ``params`` arguments:
  
      .. code-block:: python
  
          check_backward(my_link, (x1_data, x2_data), gy_data,
                         (my_link.W, my_link.b))
  
      Note that ``params`` are not ``ndarray``\\ s,
      but :class:`~chainer.Variables`\\ s.
  
      Function objects are acceptable as ``func`` argument:
  
      .. code-block:: python
  
          check_backward(lambda x1, x2: f(x1, x2),
                         (x1_data, x2_data), gy_data)
  
      .. note::
  
         ``func`` is called many times to get numerical gradients for all inputs.
         This function doesn't work correctly when ``func`` behaves randomly as
         it gets different gradients.
  
  
      Args:
          func (callable): A function which gets :class:`~chainer.Variable`\\ s
              and returns :class:`~chainer.Variable`\\ s. ``func`` must returns
              a tuple of :class:`~chainer.Variable`\\ s or one
              :class:`~chainer.Variable`. You can use a
              :class:`~chainer.Function`, :class:`~chainer.FunctionNode` or a
              :class:`~chainer.Link` object or any other function satisfying the
              condition.
          x_data (ndarray or tuple of ndarrays): A set of ``ndarray``\\ s to be
              passed to ``func``. If ``x_data`` is one ``ndarray`` object, it is
              treated as ``(x_data,)``.
          y_grad (ndarray or tuple of ndarrays or None):
              A set of ``ndarray``\\ s representing gradients of return-values of
              ``func``. If ``y_grad`` is one ``ndarray`` object, it is
              treated as ``(y_grad,)``. If ``func`` is a loss-function,
              ``y_grad`` should be set to ``None``.
          params (~chainer.Variable or tuple of ~chainder.Variable):
              A set of :class:`~chainer.Variable`\\ s whose gradients are
              checked. When ``func`` is a :class:`~chainer.Link` object,
              set its parameters as ``params``.
              If ``params`` is one :class:`~chainer.Variable` object,
              it is treated as ``(params,)``.
          eps (float): Epsilon value to be passed to :func:`numerical_grad`.
          atol (float): Absolute tolerance to be passed to
              :func:`chainer.testing.assert_allclose`.
          rtol (float): Relative tolerance to be passed to
              :func:`chainer.testing.assert_allclose`.
          no_grads (list of bool): Flag to skip variable for gradient assertion.
              It should be same length as ``x_data``.
          dtype (~numpy.dtype): ``x_data``, ``y_grad`` and ``params`` are casted
              to this dtype when calculating numerical gradients. Only float
              types and ``None`` are allowed.
          detect_nondifferentiable (bool):
              If ``True``, check for non-differentiable inputs is enabled.
              If ``func`` is non-differentiable at ``x_data``, ``check_backward``
              raises :class:`~chainer.gradient_check.NondifferentiableError`.
  
      .. seealso::
         :func:`numerical_grad`
      """
      _CheckBackward(
          func,
          x_data,
          y_grad,
          params,
          eps,
          atol,
          rtol,
          no_grads,
          dtype,
          detect_nondifferentiable,
          is_immutable_params=False,
      ).run()
  
imports: from __future__ import absolute_import
  import warnings
  import numpy
  import six
  import chainer
  from chainer import backend
  from chainer.backends import _cpu
  from chainer.backends import cuda
  from chainer import configuration
  from chainer import testing
  from chainer import utils
  from chainer import variable
  import chainerx
label_desc: Use image node_id 8 for calling a global function with example usage: check_backward(func, x_data, y_grad, params, eps, atol, rtol, no_grads, dtype, detect_nondifferentiable) without return types