neural_tangents/stax.py
def AvgPool(window_shape,
            strides=None,
            padding=Padding.VALID.name,
            normalize_edges=True):
  """Layer construction function for a 2D average pooling layer.

  Based on `jax.experimental.stax.AvgPool`. Has a similar API apart from:

  Args:
    padding: in addition to `VALID` and `SAME' padding, supports `CIRCULAR`,
      not available in `jax.experimental.stax.GeneralConv`.
    normalize_edges: `True` to normalize output by the effective receptive
      field, `False` to normalize by the window size. Only has effect at the
      edges when `SAME` padding is used. Set to `True` to retain correspondence
      to `ostax.AvgPool`.
  """
  strides = strides or (1,) * len(window_shape)
  padding = Padding(padding)

  if padding == Padding.CIRCULAR:
    init_fn, _ = ostax.AvgPool(window_shape, strides, Padding.SAME.name)
    _, apply_fn_0 = ostax.AvgPool(window_shape, strides, Padding.VALID.name)

    def apply_fn(params, inputs, **kwargs):
      inputs = _same_pad_for_filter_shape(inputs, window_shape, strides, (1, 2),
                                          'wrap')
      res = apply_fn_0(params, inputs, **kwargs)
      return res

  elif not normalize_edges:

    def rescaler(*args, **kwargs):
      del args, kwargs  # Unused.
      return lambda outputs, _: outputs / np.prod(window_shape)

    avgPool = ostax._pooling_layer(lax.add, 0., rescaler)
    init_fn, apply_fn = avgPool(window_shape, strides, padding.name)

  else:
    init_fn, apply_fn = ostax.AvgPool(window_shape, strides, padding.name)

  def kernel_fn(kernels):
    """Kernel transformation."""
    var1, nngp, var2, ntk, is_gaussian, is_height_width, marginal, cross = (
        kernels.var1, kernels.nngp, kernels.var2, kernels.ntk,
        kernels.is_gaussian, kernels.is_height_width, kernels.marginal,
        kernels.cross)

    if is_height_width:
      window_shape_nngp = window_shape
      strides_nngp = strides
    else:
      window_shape_nngp = window_shape[::-1]
      strides_nngp = strides[::-1]

    nngp = _average_pool_nngp_5or6d(nngp, window_shape_nngp, strides_nngp,
                                    padding, normalize_edges)
    ntk = _average_pool_nngp_5or6d(ntk, window_shape_nngp, strides_nngp,
                                   padding, normalize_edges)
    var1 = _average_pool_nngp_5or6d(var1, window_shape_nngp, strides_nngp,
                                    padding, normalize_edges)
    if var2 is not None:
      var2 = _average_pool_nngp_5or6d(var2, window_shape_nngp, strides_nngp,
                                      padding, normalize_edges)

    return kernels._replace(
        var1=var1, nngp=nngp, var2=var2, ntk=ntk, is_gaussian=is_gaussian,
        is_height_width=is_height_width, marginal=marginal, cross=cross)

  setattr(kernel_fn, _COVARIANCES_REQ, {'marginal': M.OVER_POINTS,
                                        'cross': M.NO})
  return init_fn, apply_fn, kernel_fn
Layer construction function for a 2D average pooling layer.

  Based on `jax.experimental.stax.AvgPool`. Has a similar API apart from:

  Args:
    padding: in addition to `VALID` and `SAME' padding, supports `CIRCULAR`,
      not available in `jax.experimental.stax.GeneralConv`.
    normalize_edges: `True` to normalize output by the effective receptive
      field, `False` to normalize by the window size. Only has effect at the
      edges when `SAME` padding is used. Set to `True` to retain correspondence
      to `ostax.AvgPool`.
  
Layer construction function for a 2D average pooling layer.
Layer construction function for a 2D average pooling layer .
def AvgPool(window_shape,
            strides=None,
            padding=Padding.VALID.name,
            normalize_edges=True):
  strides = strides or (1,) * len(window_shape)
  padding = Padding(padding)
  if padding == Padding.CIRCULAR:
    init_fn, _ = ostax.AvgPool(window_shape, strides, Padding.SAME.name)
    _, apply_fn_0 = ostax.AvgPool(window_shape, strides, Padding.VALID.name)
    def apply_fn(params, inputs, **kwargs):
      inputs = _same_pad_for_filter_shape(inputs, window_shape, strides, (1, 2),
                                          'wrap')
      res = apply_fn_0(params, inputs, **kwargs)
      return res
  elif not normalize_edges:
    def rescaler(*args, **kwargs):
      del args, kwargs  
      return lambda outputs, _: outputs / np.prod(window_shape)
    avgPool = ostax._pooling_layer(lax.add, 0., rescaler)
    init_fn, apply_fn = avgPool(window_shape, strides, padding.name)
  else:
    init_fn, apply_fn = ostax.AvgPool(window_shape, strides, padding.name)
  def kernel_fn(kernels):
    var1, nngp, var2, ntk, is_gaussian, is_height_width, marginal, cross = (
        kernels.var1, kernels.nngp, kernels.var2, kernels.ntk,
        kernels.is_gaussian, kernels.is_height_width, kernels.marginal,
        kernels.cross)
    if is_height_width:
      window_shape_nngp = window_shape
      strides_nngp = strides
    else:
      window_shape_nngp = window_shape[::-1]
      strides_nngp = strides[::-1]
    nngp = _average_pool_nngp_5or6d(nngp, window_shape_nngp, strides_nngp,
                                    padding, normalize_edges)
    ntk = _average_pool_nngp_5or6d(ntk, window_shape_nngp, strides_nngp,
                                   padding, normalize_edges)
    var1 = _average_pool_nngp_5or6d(var1, window_shape_nngp, strides_nngp,
                                    padding, normalize_edges)
    if var2 is not None:
      var2 = _average_pool_nngp_5or6d(var2, window_shape_nngp, strides_nngp,
                                      padding, normalize_edges)
    return kernels._replace(
        var1=var1, nngp=nngp, var2=var2, ntk=ntk, is_gaussian=is_gaussian,
        is_height_width=is_height_width, marginal=marginal, cross=cross)
  setattr(kernel_fn, _COVARIANCES_REQ, {'marginal': M.OVER_POINTS,
                                        'cross': M.NO})
  return init_fn, apply_fn, kernel_fn
def AvgPool ( window_shape , strides = None , padding = Padding . VALID . name , normalize_edges = True ) : strides = strides or ( 1 , ) * len ( window_shape ) padding = Padding ( padding ) if padding == Padding . CIRCULAR : init_fn , _ = ostax . AvgPool ( window_shape , strides , Padding . SAME . name ) _ , apply_fn_0 = ostax . AvgPool ( window_shape , strides , Padding . VALID . name ) def apply_fn ( params , inputs , ** kwargs ) : inputs = _same_pad_for_filter_shape ( inputs , window_shape , strides , ( 1 , 2 ) , 'wrap' ) res = apply_fn_0 ( params , inputs , ** kwargs ) return res elif not normalize_edges : def rescaler ( * args , ** kwargs ) : del args , kwargs return lambda outputs , _ : outputs / np . prod ( window_shape ) avgPool = ostax . _pooling_layer ( lax . add , 0. , rescaler ) init_fn , apply_fn = avgPool ( window_shape , strides , padding . name ) else : init_fn , apply_fn = ostax . AvgPool ( window_shape , strides , padding . name ) def kernel_fn ( kernels ) : """Kernel transformation.""" var1 , nngp , var2 , ntk , is_gaussian , is_height_width , marginal , cross = ( kernels . var1 , kernels . nngp , kernels . var2 , kernels . ntk , kernels . is_gaussian , kernels . is_height_width , kernels . marginal , kernels . cross ) if is_height_width : window_shape_nngp = window_shape strides_nngp = strides else : window_shape_nngp = window_shape [ : : - 1 ] strides_nngp = strides [ : : - 1 ] nngp = _average_pool_nngp_5or6d ( nngp , window_shape_nngp , strides_nngp , padding , normalize_edges ) ntk = _average_pool_nngp_5or6d ( ntk , window_shape_nngp , strides_nngp , padding , normalize_edges ) var1 = _average_pool_nngp_5or6d ( var1 , window_shape_nngp , strides_nngp , padding , normalize_edges ) if var2 is not None : var2 = _average_pool_nngp_5or6d ( var2 , window_shape_nngp , strides_nngp , padding , normalize_edges ) return kernels . _replace ( var1 = var1 , nngp = nngp , var2 = var2 , ntk = ntk , is_gaussian = is_gaussian , is_height_width = is_height_width , marginal = marginal , cross = cross ) setattr ( kernel_fn , _COVARIANCES_REQ , { 'marginal' : M . OVER_POINTS , 'cross' : M . NO } ) return init_fn , apply_fn , kernel_fn
Layer construction function for a 2D average pooling layer.
Layer construction function for a 2D average pooling layer .
"""Layer construction function for a 2D average pooling layer.

  Based on `jax.experimental.stax.AvgPool`. Has a similar API apart from:

  Args:
    padding: in addition to `VALID` and `SAME' padding, supports `CIRCULAR`,
      not available in `jax.experimental.stax.GeneralConv`.
    normalize_edges: `True` to normalize output by the effective receptive
      field, `False` to normalize by the window size. Only has effect at the
      edges when `SAME` padding is used. Set to `True` to retain correspondence
      to `ostax.AvgPool`.
  """ # Unused. """Kernel transformation."""
window_shape
strides
padding
in addition to `VALID` and `SAME' padding, supports `CIRCULAR`,
not available in `jax.experimental.stax.GeneralConv`.
in addition to ` VALID ` and ` SAME ' padding supports ` CIRCULAR ` not available in ` jax . experimental . stax . GeneralConv ` .
normalize_edges
`True` to normalize output by the effective receptive
field, `False` to normalize by the window size. Only has effect at the
edges when `SAME` padding is used.
` True ` to normalize output by the effective receptive field ` False ` to normalize by the window size . Only has effect at the edges when ` SAME ` padding is used .