neural_tangents/stax.py
def _average_pool_nngp_5or6d(mat, window_shape, strides, padding,
                             normalize_edges):
  """Get covariances of average pooling outputs given inputs covariances `mat`.

  Args:
    mat: a 5D or 6D `np.ndarray` containing sample-(sample-)pixel-pixel
      covariances. Has shape
      `[batch_size_1, (batch_size_2,) height, height, width, width]`.
    window_shape: tuple of two positive integers, the pooling spatial shape
      (e.g. `(3, 3)`).
    strides: tuple of two positive integers, the pooling strides, e.g. `(1, 1)`.
    padding: a `Padding` enum, e.g. `Padding.CIRCULAR`.
    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`.

  Returns:
    a 5D or 6D `np.ndarray` containing sample-(sample-)pixel-pixel covariances
    of the average pooling outputs. Has shape
    `[batch_size_1, (batch_size_2,) new_height, new_height,
      new_width, new_width]`.
  """
  if not _is_array(mat):
    return mat

  if padding == Padding.CIRCULAR:
    pixel_axes = tuple(range(mat.ndim)[-4:])
    mat = _same_pad_for_filter_shape(mat, _double_tuple(window_shape),
                                     _double_tuple(strides), pixel_axes, 'wrap')
    padding = Padding.VALID

  window_shape = (1,) * (mat.ndim - 4) + _double_tuple(window_shape)
  strides = (1,) * (mat.ndim - 4) + _double_tuple(strides)

  nngp_out = lax.reduce_window(mat, 0., lax.add, window_shape, strides,
                               padding.name)

  if padding == Padding.SAME and normalize_edges:
    # `SAME` padding in `jax.experimental.stax.AvgPool` normalizes by actual
    # window size, which is smaller at the edges.
    one = np.ones(mat.shape, mat.dtype)
    window_sizes = lax.reduce_window(one, 0., lax.add, window_shape, strides,
                                     padding.name)
    nngp_out /= window_sizes
  else:
    nngp_out /= np.prod(window_shape)

  return nngp_out
Get covariances of average pooling outputs given inputs covariances `mat`.

  Args:
    mat: a 5D or 6D `np.ndarray` containing sample-(sample-)pixel-pixel
      covariances. Has shape
      `[batch_size_1, (batch_size_2,) height, height, width, width]`.
    window_shape: tuple of two positive integers, the pooling spatial shape
      (e.g. `(3, 3)`).
    strides: tuple of two positive integers, the pooling strides, e.g. `(1, 1)`.
    padding: a `Padding` enum, e.g. `Padding.CIRCULAR`.
    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`.

  Returns:
    a 5D or 6D `np.ndarray` containing sample-(sample-)pixel-pixel covariances
    of the average pooling outputs. Has shape
    `[batch_size_1, (batch_size_2,) new_height, new_height,
      new_width, new_width]`.
  
Get covariances of average pooling outputs given inputs covariances `mat`.
Get covariances of average pooling outputs given inputs covariances ` mat ` .
def _average_pool_nngp_5or6d(mat, window_shape, strides, padding,
                             normalize_edges):
  if not _is_array(mat):
    return mat
  if padding == Padding.CIRCULAR:
    pixel_axes = tuple(range(mat.ndim)[-4:])
    mat = _same_pad_for_filter_shape(mat, _double_tuple(window_shape),
                                     _double_tuple(strides), pixel_axes, 'wrap')
    padding = Padding.VALID
  window_shape = (1,) * (mat.ndim - 4) + _double_tuple(window_shape)
  strides = (1,) * (mat.ndim - 4) + _double_tuple(strides)
  nngp_out = lax.reduce_window(mat, 0., lax.add, window_shape, strides,
                               padding.name)
  if padding == Padding.SAME and normalize_edges:
    one = np.ones(mat.shape, mat.dtype)
    window_sizes = lax.reduce_window(one, 0., lax.add, window_shape, strides,
                                     padding.name)
    nngp_out /= window_sizes
  else:
    nngp_out /= np.prod(window_shape)
  return nngp_out
def _average_pool_nngp_5or6d ( mat , window_shape , strides , padding , normalize_edges ) : if not _is_array ( mat ) : return mat if padding == Padding . CIRCULAR : pixel_axes = tuple ( range ( mat . ndim ) [ - 4 : ] ) mat = _same_pad_for_filter_shape ( mat , _double_tuple ( window_shape ) , _double_tuple ( strides ) , pixel_axes , 'wrap' ) padding = Padding . VALID window_shape = ( 1 , ) * ( mat . ndim - 4 ) + _double_tuple ( window_shape ) strides = ( 1 , ) * ( mat . ndim - 4 ) + _double_tuple ( strides ) nngp_out = lax . reduce_window ( mat , 0. , lax . add , window_shape , strides , padding . name ) if padding == Padding . SAME and normalize_edges : one = np . ones ( mat . shape , mat . dtype ) window_sizes = lax . reduce_window ( one , 0. , lax . add , window_shape , strides , padding . name ) nngp_out /= window_sizes else : nngp_out /= np . prod ( window_shape ) return nngp_out
Get covariances of average pooling outputs given inputs covariances `mat`.
Get covariances of average pooling outputs given inputs covariances ` mat ` .
"""Get covariances of average pooling outputs given inputs covariances `mat`.

  Args:
    mat: a 5D or 6D `np.ndarray` containing sample-(sample-)pixel-pixel
      covariances. Has shape
      `[batch_size_1, (batch_size_2,) height, height, width, width]`.
    window_shape: tuple of two positive integers, the pooling spatial shape
      (e.g. `(3, 3)`).
    strides: tuple of two positive integers, the pooling strides, e.g. `(1, 1)`.
    padding: a `Padding` enum, e.g. `Padding.CIRCULAR`.
    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`.

  Returns:
    a 5D or 6D `np.ndarray` containing sample-(sample-)pixel-pixel covariances
    of the average pooling outputs. Has shape
    `[batch_size_1, (batch_size_2,) new_height, new_height,
      new_width, new_width]`.
  """ # `SAME` padding in `jax.experimental.stax.AvgPool` normalizes by actual # window size, which is smaller at the edges.
a 5D or 6D `np.ndarray` containing sample-(sample-)pixel-pixel covariances
of the average pooling outputs. Has shape
`[batch_size_1, (batch_size_2,) new_height, new_height,
new_width, new_width]`.
a 5D or 6D ` np . ndarray ` containing sample - ( sample - ) pixel - pixel covariances of the average pooling outputs . Has shape ` [ batch_size_1 ( batch_size_2 ) new_height new_height new_width new_width ] ` .
mat
a 5D or 6D `np.ndarray` containing sample-(sample-)pixel-pixel
covariances. Has shape
`[batch_size_1, (batch_size_2,) height, height, width, width]`.
a 5D or 6D ` np . ndarray ` containing sample - ( sample - ) pixel - pixel covariances . Has shape ` [ batch_size_1 ( batch_size_2 ) height height width width ] ` .
window_shape
tuple of two positive integers, the pooling spatial shape
`).
tuple of two positive integers the pooling spatial shape ` ) .
strides
tuple of two positive integers, the pooling strides, e.g.
tuple of two positive integers the pooling strides e . g .
padding
a `Padding` enum, e.g. `Padding.CIRCULAR`.
a ` Padding ` enum e . g . ` Padding . CIRCULAR ` .
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 .