o
    Ö2úhªÀ  ã                   @   sÀ  d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd „ d eƒZG d!d"„ d"eƒZG d#d$„ d$eƒZG d%d&„ d&eƒZG d'd(„ d(eƒZG d)d*„ d*eƒZG d+d,„ d,eƒZ G d-d.„ d.eƒZ!eZ"eZ#eZ$eZ%eZ&eZ'eZ(eZ)e!Z*eZ+eZ,e Z-dS )/zPooling layers.é    N)Útensor_shape)Úbackend)ÚLayer)Ú	InputSpec)Ú
conv_utils)Ú	array_ops)Úmath_ops)Únnc                       óB   e Zd ZdZ		d‡ fdd„	Zdd„ Zd	d
„ Z‡ fdd„Z‡  ZS )Ú	Pooling1DaŒ  Pooling layer for arbitrary pooling functions, for 1D inputs.

  This class only exists for code reuse. It will never be an exposed API.

  Args:
    pool_function: The pooling function to apply, e.g. `tf.nn.max_pool2d`.
    pool_size: An integer or tuple/list of a single integer,
      representing the size of the pooling window.
    strides: An integer or tuple/list of a single integer, specifying the
      strides of the pooling operation.
    padding: A string. The padding method, either 'valid' or 'same'.
      Case-insensitive.
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, steps, features)` while `channels_first`
      corresponds to inputs with shape
      `(batch, features, steps)`.
    name: A string, the name of the layer.
  ÚvalidÚchannels_lastNc                    ó„   t t| ƒjdd|i|¤Ž |d u rt ¡ }|d u r|}|| _t |dd¡| _t |dd¡| _	t 
|¡| _t |¡| _tdd| _d S )NÚnameé   Ú	pool_sizeÚstridesé   ©Úndim© )Úsuperr   Ú__init__r   Úimage_data_formatÚpool_functionr   Únormalize_tupler   r   Únormalize_paddingÚpaddingÚnormalize_data_formatÚdata_formatr   Ú
input_spec©Úselfr   r   r   r   r   r   Úkwargs©Ú	__class__r   úa/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/keras/layers/pooling.pyr   4   ó   zPooling1D.__init__c                 C   sN   | j dkrdnd}t ||¡}| j|| jd | jd | j| j d}t ||¡S )Nr   é   r   ©r   )r   r   r   )r   r   Úexpand_dimsr   r   r   r   Úsqueeze)r"   ÚinputsÚpad_axisÚoutputsr   r   r&   ÚcallC   s   ûzPooling1D.callc                 C   sŠ   t  |¡ ¡ }| jdkr|d }|d }n|d }|d }t || jd | j| jd ¡}| jdkr;t  |d ||g¡S t  |d ||g¡S )NÚchannels_firstr(   r   r   ©	r   ÚTensorShapeÚas_listr   r   Úconv_output_lengthr   r   r   )r"   Úinput_shapeÚstepsÚfeaturesÚlengthr   r   r&   Úcompute_output_shapeN   s   

ý
zPooling1D.compute_output_shapec                    ó@   | j | j| j| jdœ}tt| ƒ ¡ }tt| 	¡ ƒt| 	¡ ƒ ƒS )N)r   r   r   r   )
r   r   r   r   r   r   Ú
get_configÚdictÚlistÚitems©r"   ÚconfigÚbase_configr$   r   r&   r;   _   ó   üzPooling1D.get_config©r   r   N©	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r/   r9   r;   Ú__classcell__r   r   r$   r&   r      s    þr   c                       ó&   e Zd ZdZ		d‡ fdd„	Z‡  ZS )	ÚMaxPooling1Da%  Max pooling operation for 1D temporal data.

  Downsamples the input representation by taking the maximum value over a
  spatial window of size `pool_size`. The window is shifted by `strides`.  The
  resulting output, when using the `"valid"` padding option, has a shape of:
  `output_shape = (input_shape - pool_size + 1) / strides)`

  The resulting output shape when using the `"same"` padding option is:
  `output_shape = input_shape / strides`

  For example, for `strides=1` and `padding="valid"`:

  >>> x = tf.constant([1., 2., 3., 4., 5.])
  >>> x = tf.reshape(x, [1, 5, 1])
  >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2,
  ...    strides=1, padding='valid')
  >>> max_pool_1d(x)
  <tf.Tensor: shape=(1, 4, 1), dtype=float32, numpy=
  array([[[2.],
          [3.],
          [4.],
          [5.]]], dtype=float32)>

  For example, for `strides=2` and `padding="valid"`:

  >>> x = tf.constant([1., 2., 3., 4., 5.])
  >>> x = tf.reshape(x, [1, 5, 1])
  >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2,
  ...    strides=2, padding='valid')
  >>> max_pool_1d(x)
  <tf.Tensor: shape=(1, 2, 1), dtype=float32, numpy=
  array([[[2.],
          [4.]]], dtype=float32)>

  For example, for `strides=1` and `padding="same"`:

  >>> x = tf.constant([1., 2., 3., 4., 5.])
  >>> x = tf.reshape(x, [1, 5, 1])
  >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2,
  ...    strides=1, padding='same')
  >>> max_pool_1d(x)
  <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
  array([[[2.],
          [3.],
          [4.],
          [5.],
          [5.]]], dtype=float32)>

  Args:
    pool_size: Integer, size of the max pooling window.
    strides: Integer, or None. Specifies how much the pooling window moves
      for each pooling step.
      If None, it will default to `pool_size`.
    padding: One of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding evenly to
      the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, steps, features)` while `channels_first`
      corresponds to inputs with shape
      `(batch, features, steps)`.

  Input shape:
    - If `data_format='channels_last'`:
      3D tensor with shape `(batch_size, steps, features)`.
    - If `data_format='channels_first'`:
      3D tensor with shape `(batch_size, features, steps)`.

  Output shape:
    - If `data_format='channels_last'`:
      3D tensor with shape `(batch_size, downsampled_steps, features)`.
    - If `data_format='channels_first'`:
      3D tensor with shape `(batch_size, features, downsampled_steps)`.
  r(   Nr   r   c                    ó2   t t| ƒjtjtjddf||||dœ|¤Ž d S )NÚmax©Ú	pool_mode©r   r   r   r   )r   rK   r   Ú	functoolsÚpartialr   Úpool2d©r"   r   r   r   r   r#   r$   r   r&   r   ¹   s   
ÿû
úzMaxPooling1D.__init__©r(   Nr   r   ©rE   rF   rG   rH   r   rI   r   r   r$   r&   rK   j   s
    NÿrK   c                       rJ   )	ÚAveragePooling1Da  Average pooling for temporal data.

  Downsamples the input representation by taking the average value over the
  window defined by `pool_size`. The window is shifted by `strides`.  The
  resulting output when using "valid" padding option has a shape of:
  `output_shape = (input_shape - pool_size + 1) / strides)`

  The resulting output shape when using the "same" padding option is:
  `output_shape = input_shape / strides`

  For example, for strides=1 and padding="valid":

  >>> x = tf.constant([1., 2., 3., 4., 5.])
  >>> x = tf.reshape(x, [1, 5, 1])
  >>> x
  <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
    array([[[1.],
            [2.],
            [3.],
            [4.],
            [5.]], dtype=float32)>
  >>> avg_pool_1d = tf.keras.layers.AveragePooling1D(pool_size=2,
  ...    strides=1, padding='valid')
  >>> avg_pool_1d(x)
  <tf.Tensor: shape=(1, 4, 1), dtype=float32, numpy=
  array([[[1.5],
          [2.5],
          [3.5],
          [4.5]]], dtype=float32)>

  For example, for strides=2 and padding="valid":

  >>> x = tf.constant([1., 2., 3., 4., 5.])
  >>> x = tf.reshape(x, [1, 5, 1])
  >>> x
  <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
    array([[[1.],
            [2.],
            [3.],
            [4.],
            [5.]], dtype=float32)>
  >>> avg_pool_1d = tf.keras.layers.AveragePooling1D(pool_size=2,
  ...    strides=2, padding='valid')
  >>> avg_pool_1d(x)
  <tf.Tensor: shape=(1, 2, 1), dtype=float32, numpy=
  array([[[1.5],
          [3.5]]], dtype=float32)>

  For example, for strides=1 and padding="same":

  >>> x = tf.constant([1., 2., 3., 4., 5.])
  >>> x = tf.reshape(x, [1, 5, 1])
  >>> x
  <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
    array([[[1.],
            [2.],
            [3.],
            [4.],
            [5.]], dtype=float32)>
  >>> avg_pool_1d = tf.keras.layers.AveragePooling1D(pool_size=2,
  ...    strides=1, padding='same')
  >>> avg_pool_1d(x)
  <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
  array([[[1.5],
          [2.5],
          [3.5],
          [4.5],
          [5.]]], dtype=float32)>

  Args:
    pool_size: Integer, size of the average pooling windows.
    strides: Integer, or None. Factor by which to downscale.
      E.g. 2 will halve the input.
      If None, it will default to `pool_size`.
    padding: One of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding evenly to
      the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, steps, features)` while `channels_first`
      corresponds to inputs with shape
      `(batch, features, steps)`.

  Input shape:
    - If `data_format='channels_last'`:
      3D tensor with shape `(batch_size, steps, features)`.
    - If `data_format='channels_first'`:
      3D tensor with shape `(batch_size, features, steps)`.

  Output shape:
    - If `data_format='channels_last'`:
      3D tensor with shape `(batch_size, downsampled_steps, features)`.
    - If `data_format='channels_first'`:
      3D tensor with shape `(batch_size, features, downsampled_steps)`.
  r(   Nr   r   c                    rL   )NÚavgrN   rP   )r   rW   r   rQ   rR   r   rS   rT   r$   r   r&   r   )  s   
ÿû
úzAveragePooling1D.__init__rU   rV   r   r   r$   r&   rW   Å   s
    cÿrW   c                       sB   e Zd ZdZ		d‡ fdd„	Zdd„ Zdd	„ Z‡ fd
d„Z‡  ZS )Ú	Pooling2Da^  Pooling layer for arbitrary pooling functions, for 2D inputs (e.g. images).

  This class only exists for code reuse. It will never be an exposed API.

  Args:
    pool_function: The pooling function to apply, e.g. `tf.nn.max_pool2d`.
    pool_size: An integer or tuple/list of 2 integers: (pool_height, pool_width)
      specifying the size of the pooling window.
      Can be a single integer to specify the same value for
      all spatial dimensions.
    strides: An integer or tuple/list of 2 integers,
      specifying the strides of the pooling operation.
      Can be a single integer to specify the same value for
      all spatial dimensions.
    padding: A string. The padding method, either 'valid' or 'same'.
      Case-insensitive.
    data_format: A string, one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, height, width, channels)` while `channels_first` corresponds to
      inputs with shape `(batch, channels, height, width)`.
    name: A string, the name of the layer.
  r   Nc                    r   )Nr   r(   r   r   é   r   r   )r   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r$   r   r&   r   M  r'   zPooling2D.__init__c              	   C   sd   | j dkrd| j d }d| j d }n
d| j }d| j }| j|||| j ¡ t | j d¡d}|S )Nr   r)   )r   r   rZ   )Úksizer   r   r   )r   r   r   r   r   Úupperr   Úconvert_data_format©r"   r,   Ú
pool_shaper   r.   r   r   r&   r/   \  s   


ûzPooling2D.callc                 C   s´   t  |¡ ¡ }| jdkr|d }|d }n|d }|d }t || jd | j| jd ¡}t || jd | j| jd ¡}| jdkrMt  |d |d ||g¡S t  |d |||d g¡S )Nr0   r(   r   r   r   r1   )r"   r5   ÚrowsÚcolsr   r   r&   r9   k  s&   

ÿÿ
ÿÿzPooling2D.compute_output_shapec                    r:   ©N)r   r   r   r   )
r   r   r   r   r   rY   r;   r<   r=   r>   r?   r$   r   r&   r;   ~  rB   zPooling2D.get_config)r   NNrD   r   r   r$   r&   rY   4  s    þrY   c                       ó*   e Zd ZdZ				d‡ fdd„	Z‡  ZS )ÚMaxPooling2DaJ  Max pooling operation for 2D spatial data.

  Downsamples the input along its spatial dimensions (height and width)
  by taking the maximum value over an input window
  (of size defined by `pool_size`) for each channel of the input.
  The window is shifted by `strides` along each dimension.

  The resulting output,
  when using the `"valid"` padding option, has a spatial shape
  (number of rows or columns) of:
  `output_shape = math.floor((input_shape - pool_size) / strides) + 1`
  (when `input_shape >= pool_size`)

  The resulting output shape when using the `"same"` padding option is:
  `output_shape = math.floor((input_shape - 1) / strides) + 1`

  For example, for `strides=(1, 1)` and `padding="valid"`:

  >>> x = tf.constant([[1., 2., 3.],
  ...                  [4., 5., 6.],
  ...                  [7., 8., 9.]])
  >>> x = tf.reshape(x, [1, 3, 3, 1])
  >>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2),
  ...    strides=(1, 1), padding='valid')
  >>> max_pool_2d(x)
  <tf.Tensor: shape=(1, 2, 2, 1), dtype=float32, numpy=
    array([[[[5.],
             [6.]],
            [[8.],
             [9.]]]], dtype=float32)>

  For example, for `strides=(2, 2)` and `padding="valid"`:

  >>> x = tf.constant([[1., 2., 3., 4.],
  ...                  [5., 6., 7., 8.],
  ...                  [9., 10., 11., 12.]])
  >>> x = tf.reshape(x, [1, 3, 4, 1])
  >>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2),
  ...    strides=(2, 2), padding='valid')
  >>> max_pool_2d(x)
  <tf.Tensor: shape=(1, 1, 2, 1), dtype=float32, numpy=
    array([[[[6.],
             [8.]]]], dtype=float32)>

  Usage Example:

  >>> input_image = tf.constant([[[[1.], [1.], [2.], [4.]],
  ...                            [[2.], [2.], [3.], [2.]],
  ...                            [[4.], [1.], [1.], [1.]],
  ...                            [[2.], [2.], [1.], [4.]]]])
  >>> output = tf.constant([[[[1], [0]],
  ...                       [[0], [1]]]])
  >>> model = tf.keras.models.Sequential()
  >>> model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2),
  ...    input_shape=(4, 4, 1)))
  >>> model.compile('adam', 'mean_squared_error')
  >>> model.predict(input_image, steps=1)
  array([[[[2.],
           [4.]],
          [[4.],
           [4.]]]], dtype=float32)

  For example, for stride=(1, 1) and padding="same":

  >>> x = tf.constant([[1., 2., 3.],
  ...                  [4., 5., 6.],
  ...                  [7., 8., 9.]])
  >>> x = tf.reshape(x, [1, 3, 3, 1])
  >>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2),
  ...    strides=(1, 1), padding='same')
  >>> max_pool_2d(x)
  <tf.Tensor: shape=(1, 3, 3, 1), dtype=float32, numpy=
    array([[[[5.],
             [6.],
             [6.]],
            [[8.],
             [9.],
             [9.]],
            [[8.],
             [9.],
             [9.]]]], dtype=float32)>

  Args:
    pool_size: integer or tuple of 2 integers,
      window size over which to take the maximum.
      `(2, 2)` will take the max value over a 2x2 pooling window.
      If only one integer is specified, the same window length
      will be used for both dimensions.
    strides: Integer, tuple of 2 integers, or None.
      Strides values.  Specifies how far the pooling window moves
      for each pooling step. If None, it will default to `pool_size`.
    padding: One of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding evenly to
      the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, height, width, channels)` while `channels_first`
      corresponds to inputs with shape
      `(batch, channels, height, width)`.
      It defaults to the `image_data_format` value found in your
      Keras config file at `~/.keras/keras.json`.
      If you never set it, then it will be "channels_last".

  Input shape:
    - If `data_format='channels_last'`:
      4D tensor with shape `(batch_size, rows, cols, channels)`.
    - If `data_format='channels_first'`:
      4D tensor with shape `(batch_size, channels, rows, cols)`.

  Output shape:
    - If `data_format='channels_last'`:
      4D tensor with shape `(batch_size, pooled_rows, pooled_cols, channels)`.
    - If `data_format='channels_first'`:
      4D tensor with shape `(batch_size, channels, pooled_rows, pooled_cols)`.

  Returns:
    A tensor of rank 4 representing the maximum pooled values.  See above for
    output shape.
  ©r(   r(   Nr   c                    ó(   t t| ƒjtjf||||dœ|¤Ž d S ©NrP   )r   rd   r   r	   Úmax_poolrT   r$   r   r&   r     ó   
ÿý
ýzMaxPooling2D.__init__©re   Nr   NrV   r   r   r$   r&   rd   ‰  s    |ürd   c                       rc   )ÚAveragePooling2Da  Average pooling operation for spatial data.

  Downsamples the input along its spatial dimensions (height and width)
  by taking the average value over an input window
  (of size defined by `pool_size`) for each channel of the input.
  The window is shifted by `strides` along each dimension.

  The resulting output when using `"valid"` padding option has a shape
  (number of rows or columns) of:
  `output_shape = math.floor((input_shape - pool_size) / strides) + 1`
  (when `input_shape >= pool_size`)

  The resulting output shape when using the `"same"` padding option is:
  `output_shape = math.floor((input_shape - 1) / strides) + 1`

  For example, for `strides=(1, 1)` and `padding="valid"`:

  >>> x = tf.constant([[1., 2., 3.],
  ...                  [4., 5., 6.],
  ...                  [7., 8., 9.]])
  >>> x = tf.reshape(x, [1, 3, 3, 1])
  >>> avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
  ...    strides=(1, 1), padding='valid')
  >>> avg_pool_2d(x)
  <tf.Tensor: shape=(1, 2, 2, 1), dtype=float32, numpy=
    array([[[[3.],
             [4.]],
            [[6.],
             [7.]]]], dtype=float32)>

  For example, for `stride=(2, 2)` and `padding="valid"`:

  >>> x = tf.constant([[1., 2., 3., 4.],
  ...                  [5., 6., 7., 8.],
  ...                  [9., 10., 11., 12.]])
  >>> x = tf.reshape(x, [1, 3, 4, 1])
  >>> avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
  ...    strides=(2, 2), padding='valid')
  >>> avg_pool_2d(x)
  <tf.Tensor: shape=(1, 1, 2, 1), dtype=float32, numpy=
    array([[[[3.5],
             [5.5]]]], dtype=float32)>

  For example, for `strides=(1, 1)` and `padding="same"`:

  >>> x = tf.constant([[1., 2., 3.],
  ...                  [4., 5., 6.],
  ...                  [7., 8., 9.]])
  >>> x = tf.reshape(x, [1, 3, 3, 1])
  >>> avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
  ...    strides=(1, 1), padding='same')
  >>> avg_pool_2d(x)
  <tf.Tensor: shape=(1, 3, 3, 1), dtype=float32, numpy=
    array([[[[3.],
             [4.],
             [4.5]],
            [[6.],
             [7.],
             [7.5]],
            [[7.5],
             [8.5],
             [9.]]]], dtype=float32)>

  Args:
    pool_size: integer or tuple of 2 integers,
      factors by which to downscale (vertical, horizontal).
      `(2, 2)` will halve the input in both spatial dimension.
      If only one integer is specified, the same window length
      will be used for both dimensions.
    strides: Integer, tuple of 2 integers, or None.
      Strides values.
      If None, it will default to `pool_size`.
    padding: One of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding evenly to
      the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, height, width, channels)` while `channels_first`
      corresponds to inputs with shape
      `(batch, channels, height, width)`.
      It defaults to the `image_data_format` value found in your
      Keras config file at `~/.keras/keras.json`.
      If you never set it, then it will be "channels_last".

  Input shape:
    - If `data_format='channels_last'`:
      4D tensor with shape `(batch_size, rows, cols, channels)`.
    - If `data_format='channels_first'`:
      4D tensor with shape `(batch_size, channels, rows, cols)`.

  Output shape:
    - If `data_format='channels_last'`:
      4D tensor with shape `(batch_size, pooled_rows, pooled_cols, channels)`.
    - If `data_format='channels_first'`:
      4D tensor with shape `(batch_size, channels, pooled_rows, pooled_cols)`.
  re   Nr   c                    rf   rg   )r   rk   r   r	   Úavg_poolrT   r$   r   r&   r   v  ri   zAveragePooling2D.__init__rj   rV   r   r   r$   r&   rk     s    eürk   c                       r
   )Ú	Pooling3Dav  Pooling layer for arbitrary pooling functions, for 3D inputs.

  This class only exists for code reuse. It will never be an exposed API.

  Args:
    pool_function: The pooling function to apply, e.g. `tf.nn.max_pool2d`.
    pool_size: An integer or tuple/list of 3 integers:
      (pool_depth, pool_height, pool_width)
      specifying the size of the pooling window.
      Can be a single integer to specify the same value for
      all spatial dimensions.
    strides: An integer or tuple/list of 3 integers,
      specifying the strides of the pooling operation.
      Can be a single integer to specify the same value for
      all spatial dimensions.
    padding: A string. The padding method, either 'valid' or 'same'.
      Case-insensitive.
    data_format: A string, one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, depth, height, width, channels)`
      while `channels_first` corresponds to
      inputs with shape `(batch, channels, depth, height, width)`.
    name: A string, the name of the layer.
  r   r   Nc                    r   )Nr   r   r   r   é   r   r   )r   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r$   r   r&   r     r'   zPooling3D.__init__c                 C   sd   d| j  d }d| j d }| jdkrt |d¡}| j|||| j ¡ d}| jdkr0t |d¡}|S )Nr)   r0   )r   r(   r   rZ   r   )r[   r   r   )r   rZ   r   r(   r   )r   r   r   r   Ú	transposer   r   r\   r^   r   r   r&   r/   ¬  s   
ü
zPooling3D.callc                 C   sæ   t  |¡ ¡ }| jdkr|d }|d }|d }n|d }|d }|d }t || jd | j| jd ¡}t || jd | j| jd ¡}t || jd | j| jd ¡}| jdkret  |d |d |||g¡S t  |d ||||d g¡S )Nr0   r(   r   rZ   r   r   r1   )r"   r5   Úlen_dim1Úlen_dim2Úlen_dim3r   r   r&   r9   À  s0   

ÿÿÿ
ÿÿzPooling3D.compute_output_shapec                    r:   rb   )
r   r   r   r   r   rm   r;   r<   r=   r>   r?   r$   r   r&   r;   ×  rB   zPooling3D.get_configrC   rD   r   r   r$   r&   rm   ‚  s    þrm   c                       rc   )ÚMaxPooling3DaÊ  Max pooling operation for 3D data (spatial or spatio-temporal).

  Downsamples the input along its spatial dimensions (depth, height, and width)
  by taking the maximum value over an input window
  (of size defined by `pool_size`) for each channel of the input.
  The window is shifted by `strides` along each dimension.

  Args:
    pool_size: Tuple of 3 integers,
      factors by which to downscale (dim1, dim2, dim3).
      `(2, 2, 2)` will halve the size of the 3D input in each dimension.
    strides: tuple of 3 integers, or None. Strides values.
    padding: One of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding evenly to
      the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
      while `channels_first` corresponds to inputs with shape
      `(batch, channels, spatial_dim1, spatial_dim2, spatial_dim3)`.
      It defaults to the `image_data_format` value found in your
      Keras config file at `~/.keras/keras.json`.
      If you never set it, then it will be "channels_last".

  Input shape:
    - If `data_format='channels_last'`:
      5D tensor with shape:
      `(batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
    - If `data_format='channels_first'`:
      5D tensor with shape:
      `(batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)`

  Output shape:
    - If `data_format='channels_last'`:
      5D tensor with shape:
      `(batch_size, pooled_dim1, pooled_dim2, pooled_dim3, channels)`
    - If `data_format='channels_first'`:
      5D tensor with shape:
      `(batch_size, channels, pooled_dim1, pooled_dim2, pooled_dim3)`

  Example:

  ```python
  depth = 30
  height = 30
  width = 30
  input_channels = 3

  inputs = tf.keras.Input(shape=(depth, height, width, input_channels))
  layer = tf.keras.layers.MaxPooling3D(pool_size=3)
  outputs = layer(inputs)  # Shape: (batch_size, 10, 10, 10, 3)
  ```
  ©r(   r(   r(   Nr   c                    rf   rg   )r   rs   r   r	   Ú
max_pool3drT   r$   r   r&   r     ri   zMaxPooling3D.__init__©rt   Nr   NrV   r   r   r$   r&   rs   â  ó    :ürs   c                       rc   )ÚAveragePooling3DaÒ  Average pooling operation for 3D data (spatial or spatio-temporal).

  Downsamples the input along its spatial dimensions (depth, height, and width)
  by taking the average value over an input window
  (of size defined by `pool_size`) for each channel of the input.
  The window is shifted by `strides` along each dimension.

  Args:
    pool_size: tuple of 3 integers,
      factors by which to downscale (dim1, dim2, dim3).
      `(2, 2, 2)` will halve the size of the 3D input in each dimension.
    strides: tuple of 3 integers, or None. Strides values.
    padding: One of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding evenly to
      the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
      while `channels_first` corresponds to inputs with shape
      `(batch, channels, spatial_dim1, spatial_dim2, spatial_dim3)`.
      It defaults to the `image_data_format` value found in your
      Keras config file at `~/.keras/keras.json`.
      If you never set it, then it will be "channels_last".

  Input shape:
    - If `data_format='channels_last'`:
      5D tensor with shape:
      `(batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
    - If `data_format='channels_first'`:
      5D tensor with shape:
      `(batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)`

  Output shape:
    - If `data_format='channels_last'`:
      5D tensor with shape:
      `(batch_size, pooled_dim1, pooled_dim2, pooled_dim3, channels)`
    - If `data_format='channels_first'`:
      5D tensor with shape:
      `(batch_size, channels, pooled_dim1, pooled_dim2, pooled_dim3)`

  Example:

  ```python
  depth = 30
  height = 30
  width = 30
  input_channels = 3

  inputs = tf.keras.Input(shape=(depth, height, width, input_channels))
  layer = tf.keras.layers.AveragePooling3D(pool_size=3)
  outputs = layer(inputs)  # Shape: (batch_size, 10, 10, 10, 3)
  ```
  rt   Nr   c                    rf   rg   )r   rx   r   r	   Ú
avg_pool3drT   r$   r   r&   r   b  ri   zAveragePooling3D.__init__rv   rV   r   r   r$   r&   rx   (  rw   rx   c                       ó>   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Z‡ fd
d„Z‡  ZS )ÚGlobalPooling1Dz6Abstract class for different global pooling 1D layers.r   Fc                    s8   t t| ƒjdi |¤Ž tdd| _t |¡| _|| _d S )Nr   r   r   )	r   r{   r   r   r    r   r   r   Úkeepdims©r"   r   r|   r#   r$   r   r&   r   q  ó   
zGlobalPooling1D.__init__c                 C   s€   t  |¡ ¡ }| jdkr&| jrt  |d |d dg¡S t  |d |d g¡S | jr5t  |d d|d g¡S t  |d |d g¡S )Nr0   r   r   r(   ©r   r2   r3   r   r|   ©r"   r5   r   r   r&   r9   w  s   
z$GlobalPooling1D.compute_output_shapec                 C   ó   t ‚©N©ÚNotImplementedError©r"   r,   r   r   r&   r/   „  ó   zGlobalPooling1D.callc                    ó8   | j | jdœ}tt| ƒ ¡ }tt| ¡ ƒt| ¡ ƒ ƒS ©N)r   r|   )r   r|   r   r{   r;   r<   r=   r>   r?   r$   r   r&   r;   ‡  ó   zGlobalPooling1D.get_config)r   F©	rE   rF   rG   rH   r   r9   r/   r;   rI   r   r   r$   r&   r{   n  s    r{   c                       s6   e Zd ZdZd
‡ fdd„	Zddd„Zddd	„Z‡  ZS )ÚGlobalAveragePooling1DaW  Global average pooling operation for temporal data.

  Examples:

  >>> input_shape = (2, 3, 4)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.GlobalAveragePooling1D()(x)
  >>> print(y.shape)
  (2, 4)

  Args:
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, steps, features)` while `channels_first`
      corresponds to inputs with shape
      `(batch, features, steps)`.
    keepdims: A boolean, whether to keep the temporal dimension or not.
      If `keepdims` is `False` (default), the rank of the tensor is reduced
      for spatial dimensions.
      If `keepdims` is `True`, the temporal dimension are retained with
      length 1.
      The behavior is the same as for `tf.reduce_mean` or `np.mean`.

  Call arguments:
    inputs: A 3D tensor.
    mask: Binary tensor of shape `(batch_size, steps)` indicating whether
      a given step should be masked (excluded from the average).

  Input shape:
    - If `data_format='channels_last'`:
      3D tensor with shape:
      `(batch_size, steps, features)`
    - If `data_format='channels_first'`:
      3D tensor with shape:
      `(batch_size, features, steps)`

  Output shape:
    - If `keepdims`=False:
      2D tensor with shape `(batch_size, features)`.
    - If `keepdims`=True:
      - If `data_format='channels_last'`:
        3D tensor with shape `(batch_size, 1, features)`
      - If `data_format='channels_first'`:
        3D tensor with shape `(batch_size, features, 1)`
  r   c                    s$   t t| ƒjdd|i|¤Ž d| _d S )Nr   Tr   )r   r‹   r   Úsupports_masking)r"   r   r#   r$   r   r&   r   ¾  s   ÿ
zGlobalAveragePooling1D.__init__Nc                 C   s„   | j dkrdnd}|d ur9t ||d j¡}t || j dkr dnd¡}||9 }tj||| jdtj	||| jd S tj
||| jdS )Nr   r   r(   r   ©Úaxisr|   )r   r   ÚcastÚdtyper   r*   r   Úsumr|   Ú
reduce_sumÚmean)r"   r,   ÚmaskÚ
steps_axisr   r   r&   r/   Ã  s    ÿþÿþzGlobalAveragePooling1D.callc                 C   s   d S r‚   r   )r"   r,   r”   r   r   r&   Úcompute_maskÑ  r†   z#GlobalAveragePooling1D.compute_mask)r   r‚   )rE   rF   rG   rH   r   r/   r–   rI   r   r   r$   r&   r‹     s
    0
r‹   c                   @   ó   e Zd ZdZdd„ ZdS )ÚGlobalMaxPooling1Da,  Global max pooling operation for 1D temporal data.

  Downsamples the input representation by taking the maximum value over
  the time dimension.

  For example:

  >>> x = tf.constant([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
  >>> x = tf.reshape(x, [3, 3, 1])
  >>> x
  <tf.Tensor: shape=(3, 3, 1), dtype=float32, numpy=
  array([[[1.], [2.], [3.]],
         [[4.], [5.], [6.]],
         [[7.], [8.], [9.]]], dtype=float32)>
  >>> max_pool_1d = tf.keras.layers.GlobalMaxPooling1D()
  >>> max_pool_1d(x)
  <tf.Tensor: shape=(3, 1), dtype=float32, numpy=
  array([[3.],
         [6.],
         [9.], dtype=float32)>

  Args:
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, steps, features)` while `channels_first`
      corresponds to inputs with shape
      `(batch, features, steps)`.
    keepdims: A boolean, whether to keep the temporal dimension or not.
      If `keepdims` is `False` (default), the rank of the tensor is reduced
      for spatial dimensions.
      If `keepdims` is `True`, the temporal dimension are retained with
      length 1.
      The behavior is the same as for `tf.reduce_max` or `np.max`.

  Input shape:
    - If `data_format='channels_last'`:
      3D tensor with shape:
      `(batch_size, steps, features)`
    - If `data_format='channels_first'`:
      3D tensor with shape:
      `(batch_size, features, steps)`

  Output shape:
    - If `keepdims`=False:
      2D tensor with shape `(batch_size, features)`.
    - If `keepdims`=True:
      - If `data_format='channels_last'`:
        3D tensor with shape `(batch_size, 1, features)`
      - If `data_format='channels_first'`:
        3D tensor with shape `(batch_size, features, 1)`
  c                 C   s$   | j dkrdnd}tj||| jdS )Nr   r   r(   r   ©r   r   rM   r|   )r"   r,   r•   r   r   r&   r/     s   zGlobalMaxPooling1D.callN©rE   rF   rG   rH   r/   r   r   r   r&   r˜   Õ  s    6r˜   c                       rz   )ÚGlobalPooling2Dz9Abstract class for different global pooling 2D layers.
  NFc                    ó8   t t| ƒjdi |¤Ž t |¡| _tdd| _|| _d S )NrZ   r   r   )	r   r›   r   r   r   r   r   r    r|   r}   r$   r   r&   r     r~   zGlobalPooling2D.__init__c                 C   s„   t  |¡ ¡ }| jdkr'| jrt  |d dd|d g¡S t  |d |d g¡S | jr7t  |d |d ddg¡S t  |d |d g¡S )Nr   r   r   r   r   r€   r   r   r&   r9     s   
z$GlobalPooling2D.compute_output_shapec                 C   r   r‚   rƒ   r…   r   r   r&   r/   (  r†   zGlobalPooling2D.callc                    r‡   rˆ   )r   r|   r   r›   r;   r<   r=   r>   r?   r$   r   r&   r;   +  r‰   zGlobalPooling2D.get_config©NFrŠ   r   r   r$   r&   r›     s    r›   c                   @   r—   )ÚGlobalAveragePooling2Da…  Global average pooling operation for spatial data.

  Examples:

  >>> input_shape = (2, 4, 5, 3)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.GlobalAveragePooling2D()(x)
  >>> print(y.shape)
  (2, 3)

  Args:
      data_format: A string,
        one of `channels_last` (default) or `channels_first`.
        The ordering of the dimensions in the inputs.
        `channels_last` corresponds to inputs with shape
        `(batch, height, width, channels)` while `channels_first`
        corresponds to inputs with shape
        `(batch, channels, height, width)`.
        It defaults to the `image_data_format` value found in your
        Keras config file at `~/.keras/keras.json`.
        If you never set it, then it will be "channels_last".
      keepdims: A boolean, whether to keep the spatial dimensions or not.
        If `keepdims` is `False` (default), the rank of the tensor is reduced
        for spatial dimensions.
        If `keepdims` is `True`, the spatial dimensions are retained with
        length 1.
        The behavior is the same as for `tf.reduce_mean` or `np.mean`.

  Input shape:
    - If `data_format='channels_last'`:
      4D tensor with shape `(batch_size, rows, cols, channels)`.
    - If `data_format='channels_first'`:
      4D tensor with shape `(batch_size, channels, rows, cols)`.

  Output shape:
    - If `keepdims`=False:
      2D tensor with shape `(batch_size, channels)`.
    - If `keepdims`=True:
      - If `data_format='channels_last'`:
        4D tensor with shape `(batch_size, 1, 1, channels)`
      - If `data_format='channels_first'`:
        4D tensor with shape `(batch_size, channels, 1, 1)`
  c                 C   ó6   | j dkrtj|ddg| jdS tj|ddg| jdS ©Nr   r   r(   r   r   ©r   r   r“   r|   r…   r   r   r&   r/   ^  ó   
zGlobalAveragePooling2D.callNrš   r   r   r   r&   rž   1  ó    ,rž   c                   @   r—   )ÚGlobalMaxPooling2DaX  Global max pooling operation for spatial data.

  Examples:

  >>> input_shape = (2, 4, 5, 3)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.GlobalMaxPool2D()(x)
  >>> print(y.shape)
  (2, 3)

  Args:
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, height, width, channels)` while `channels_first`
      corresponds to inputs with shape
      `(batch, channels, height, width)`.
      It defaults to the `image_data_format` value found in your
      Keras config file at `~/.keras/keras.json`.
      If you never set it, then it will be "channels_last".
    keepdims: A boolean, whether to keep the spatial dimensions or not.
      If `keepdims` is `False` (default), the rank of the tensor is reduced
      for spatial dimensions.
      If `keepdims` is `True`, the spatial dimensions are retained with
      length 1.
      The behavior is the same as for `tf.reduce_max` or `np.max`.

  Input shape:
    - If `data_format='channels_last'`:
      4D tensor with shape `(batch_size, rows, cols, channels)`.
    - If `data_format='channels_first'`:
      4D tensor with shape `(batch_size, channels, rows, cols)`.

  Output shape:
    - If `keepdims`=False:
      2D tensor with shape `(batch_size, channels)`.
    - If `keepdims`=True:
      - If `data_format='channels_last'`:
        4D tensor with shape `(batch_size, 1, 1, channels)`
      - If `data_format='channels_first'`:
        4D tensor with shape `(batch_size, channels, 1, 1)`
  c                 C   rŸ   r    r™   r…   r   r   r&   r/   ’  r¢   zGlobalMaxPooling2D.callNrš   r   r   r   r&   r¤   e  r£   r¤   c                       rz   )ÚGlobalPooling3Dz6Abstract class for different global pooling 3D layers.NFc                    rœ   )Nrn   r   r   )	r   r¥   r   r   r   r   r   r    r|   r}   r$   r   r&   r   œ  r~   zGlobalPooling3D.__init__c                 C   sˆ   t  |¡ ¡ }| jdkr(| jrt  |d ddd|d g¡S t  |d |d g¡S | jr9t  |d |d dddg¡S t  |d |d g¡S )Nr   r   r   rZ   r   r€   r   r   r&   r9   ¢  s   
ÿÿz$GlobalPooling3D.compute_output_shapec                 C   r   r‚   rƒ   r…   r   r   r&   r/   ±  r†   zGlobalPooling3D.callc                    r‡   rˆ   )r   r|   r   r¥   r;   r<   r=   r>   r?   r$   r   r&   r;   ´  r‰   zGlobalPooling3D.get_configr   rŠ   r   r   r$   r&   r¥   ™  s    r¥   c                   @   r—   )ÚGlobalAveragePooling3Da9  Global Average pooling operation for 3D data.

  Args:
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
      while `channels_first` corresponds to inputs with shape
      `(batch, channels, spatial_dim1, spatial_dim2, spatial_dim3)`.
      It defaults to the `image_data_format` value found in your
      Keras config file at `~/.keras/keras.json`.
      If you never set it, then it will be "channels_last".
    keepdims: A boolean, whether to keep the spatial dimensions or not.
      If `keepdims` is `False` (default), the rank of the tensor is reduced
      for spatial dimensions.
      If `keepdims` is `True`, the spatial dimensions are retained with
      length 1.
      The behavior is the same as for `tf.reduce_mean` or `np.mean`.

  Input shape:
    - If `data_format='channels_last'`:
      5D tensor with shape:
      `(batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
    - If `data_format='channels_first'`:
      5D tensor with shape:
      `(batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)`

  Output shape:
    - If `keepdims`=False:
      2D tensor with shape `(batch_size, channels)`.
    - If `keepdims`=True:
      - If `data_format='channels_last'`:
        5D tensor with shape `(batch_size, 1, 1, 1, channels)`
      - If `data_format='channels_first'`:
        5D tensor with shape `(batch_size, channels, 1, 1, 1)`
  c                 C   ó6   | j dkrtj|g d¢| jdS tj|g d¢| jdS ©Nr   )r   r(   r   r   )r(   r   rZ   r¡   r…   r   r   r&   r/   á  r¢   zGlobalAveragePooling3D.callNrš   r   r   r   r&   r¦   º  ó    &r¦   c                   @   r—   )ÚGlobalMaxPooling3Da3  Global Max pooling operation for 3D data.

  Args:
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
      The ordering of the dimensions in the inputs.
      `channels_last` corresponds to inputs with shape
      `(batch, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
      while `channels_first` corresponds to inputs with shape
      `(batch, channels, spatial_dim1, spatial_dim2, spatial_dim3)`.
      It defaults to the `image_data_format` value found in your
      Keras config file at `~/.keras/keras.json`.
      If you never set it, then it will be "channels_last".
    keepdims: A boolean, whether to keep the spatial dimensions or not.
      If `keepdims` is `False` (default), the rank of the tensor is reduced
      for spatial dimensions.
      If `keepdims` is `True`, the spatial dimensions are retained with
      length 1.
      The behavior is the same as for `tf.reduce_max` or `np.max`.

  Input shape:
    - If `data_format='channels_last'`:
      5D tensor with shape:
      `(batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
    - If `data_format='channels_first'`:
      5D tensor with shape:
      `(batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)`

  Output shape:
    - If `keepdims`=False:
      2D tensor with shape `(batch_size, channels)`.
    - If `keepdims`=True:
      - If `data_format='channels_last'`:
        5D tensor with shape `(batch_size, 1, 1, 1, channels)`
      - If `data_format='channels_first'`:
        5D tensor with shape `(batch_size, channels, 1, 1, 1)`
  c                 C   r§   r¨   r™   r…   r   r   r&   r/     r¢   zGlobalMaxPooling3D.callNrš   r   r   r   r&   rª   è  r©   rª   ).rH   rQ   Útensorflow.python.frameworkr   Útensorflow.python.kerasr   Ú)tensorflow.python.keras.engine.base_layerr   Ú)tensorflow.python.keras.engine.input_specr   Útensorflow.python.keras.utilsr   Útensorflow.python.opsr   r   r	   r   rK   rW   rY   rd   rk   rm   rs   rx   r{   r‹   r˜   r›   rž   r¤   r¥   r¦   rª   Ú	AvgPool1DÚ	MaxPool1DÚ	AvgPool2DÚ	MaxPool2DÚ	AvgPool3DÚ	MaxPool3DÚGlobalMaxPool1DÚGlobalMaxPool2DÚGlobalMaxPool3DÚGlobalAvgPool1DÚGlobalAvgPool2DÚGlobalAvgPool3Dr   r   r   r&   Ú<module>   sR   M[oU 	q`FFH< 44!.0