code: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  
  import numpy as np  # type: ignore
  
  import onnx
  from ..base import Base
  from . import expect
  
  
  class BatchNormalization(Base):
  
      @staticmethod
      def export():  # type: () -> None
          def _batchnorm_test_mode(x, s, bias, mean, var, epsilon=1e-5):  # type: ignore
              dims_x = len(x.shape)
              dim_ones = (1,) * (dims_x - 2)
              s = s.reshape(-1, *dim_ones)
              bias = bias.reshape(-1, *dim_ones)
              mean = mean.reshape(-1, *dim_ones)
              var = var.reshape(-1, *dim_ones)
              return s * (x - mean) / np.sqrt(var + epsilon) + bias
  
          # input size: (1, 2, 1, 3)
          x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
          s = np.array([1.0, 1.5]).astype(np.float32)
          bias = np.array([0, 1]).astype(np.float32)
          mean = np.array([0, 3]).astype(np.float32)
          var = np.array([1, 1.5]).astype(np.float32)
          y = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)
  
          node = onnx.helper.make_node(
              'BatchNormalization',
              inputs=['x', 's', 'bias', 'mean', 'var'],
              outputs=['y'],
          )
  
          # output size: (1, 2, 1, 3)
          expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
                 name='test_batchnorm_example')
  
          # input size: (2, 3, 4, 5)
          x = np.random.randn(2, 3, 4, 5).astype(np.float32)
          s = np.random.randn(3).astype(np.float32)
          bias = np.random.randn(3).astype(np.float32)
          mean = np.random.randn(3).astype(np.float32)
          var = np.random.rand(3).astype(np.float32)
          epsilon = 1e-2
          y = _batchnorm_test_mode(x, s, bias, mean, var, epsilon).astype(np.float32)
  
          node = onnx.helper.make_node(
              'BatchNormalization',
              inputs=['x', 's', 'bias', 'mean', 'var'],
              outputs=['y'],
              epsilon=epsilon,
          )
  
          # output size: (2, 3, 4, 5)
          expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
                 name='test_batchnorm_epsilon')
   from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  
  from collections import defaultdict
  import inspect
  from textwrap import dedent
  from typing import Dict, Text, List, Tuple, Type, Sequence, Any
  
  import numpy as np  # type: ignore
  from six import add_metaclass
  
  
  def process_snippet(op_name, name, export):  # type: (Text, Text, Any) -> Tuple[Text, Text]
      snippet_name = name[len('export_'):] or op_name.lower()
      source_code = dedent(inspect.getsource(export))
      # remove the function signature line
      lines = source_code.splitlines()
      assert lines[0] == '@staticmethod'
      assert lines[1].startswith('def export')
      return snippet_name, dedent("\n".join(lines[2:]))
  
  
  Snippets = defaultdict(list)  # type: Dict[Text, List[Tuple[Text, Text]]]
  
  
  class _Exporter(type):
      exports = defaultdict(list)  # type: Dict[Text, List[Tuple[Text, Text]]]
  
      def __init__(cls, name, bases, dct):  # type: (str, Tuple[Type[Any], ...], Dict[str, Any]) -> None
          for k, v in dct.items():
              if k.startswith('export'):
                  if not isinstance(v, staticmethod):
                      raise ValueError(
                          'Only staticmethods could be named as export.*')
                  export = getattr(cls, k)
                  Snippets[name].append(process_snippet(name, k, export))
                  # export functions should call expect and so populate
                  # TestCases
                  np.random.seed(seed=0)
                  export()
          super(_Exporter, cls).__init__(name, bases, dct)
  
  
  @add_metaclass(_Exporter)
  class Base(object):
      pass
  
apis: numpy.array numpy.random.randn numpy.random.rand numpy.sqrt
apis: numpy.random.seed