o
    ={c  ã                   @   sR   d Z ddlZddlm  mZ ddlmZ ddl	m
Z
 e
dƒG dd„ deƒƒZdS )zContains the Reshape layer.é    N)ÚLayer)Úkeras_exportzkeras.layers.Reshapec                       sD   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡ fd
d„Z‡  Z	S )ÚReshapeaÛ  Layer that reshapes inputs into the given shape.

    Input shape:
      Arbitrary, although all dimensions in the input shape must be known/fixed.
      Use the keyword argument `input_shape` (tuple of integers, does not
      include the samples/batch size axis) when using this layer as the first
      layer in a model.

    Output shape:
      `(batch_size,) + target_shape`

    Example:

    >>> # as first layer in a Sequential model
    >>> model = tf.keras.Sequential()
    >>> model.add(tf.keras.layers.Reshape((3, 4), input_shape=(12,)))
    >>> # model.output_shape == (None, 3, 4), `None` is the batch size.
    >>> model.output_shape
    (None, 3, 4)

    >>> # as intermediate layer in a Sequential model
    >>> model.add(tf.keras.layers.Reshape((6, 2)))
    >>> model.output_shape
    (None, 6, 2)

    >>> # also supports shape inference using `-1` as dimension
    >>> model.add(tf.keras.layers.Reshape((-1, 2, 2)))
    >>> model.output_shape
    (None, 3, 2, 2)
    c                    s    t ƒ jdi |¤Ž t|ƒ| _dS )a  Creates a `tf.keras.layers.Reshape`  layer instance.

        Args:
          target_shape: Target shape. Tuple of integers, does not include the
            samples dimension (batch size).
          **kwargs: Any additional layer keyword arguments.
        N© )ÚsuperÚ__init__ÚtupleÚtarget_shape)Úselfr	   Úkwargs©Ú	__class__r   ú>lib/python3.10/site-packages/keras/layers/reshaping/reshape.pyr   <   s   zReshape.__init__c           	      C   s²   t |ƒ}d ||¡}d\}}t|ƒD ]\}}|dk r)|du r!|}qtd|› dƒ‚||9 }qtj|td}|durO|dksC|| dkrGt|ƒ‚|| ||< |S ||krWt|ƒ‚|S )aÕ  Find and replace a missing dimension in an output shape.

        This is a near direct port of the internal Numpy function
        `_fix_unknown_dimension` in `numpy/core/src/multiarray/shape.c`

        Args:
          input_shape: Shape of array being reshaped
          output_shape: Desired shape of the array with at most a single -1
            which indicates a dimension that should be derived from the input
            shape.

        Returns:
          The new output shape with a -1 replaced with its computed value.

        Raises:
          ValueError: If the total array size of the output_shape is
          different than the input_shape, or more than one unknown dimension
          is specified.
        zNtotal size of new array must be unchanged, input_shape = {}, output_shape = {})é   Nr   NzTThere must be at most one unknown dimension in output_shape. Received: output_shape=Ú.)Zdtype)ÚlistÚformatÚ	enumerateÚ
ValueErrorÚnpZprodÚint)	r
   Úinput_shapeÚoutput_shapeÚmsgZknownÚunknownÚindexZdimZoriginalr   r   r   Ú_fix_unknown_dimensionG   s4   ÿþÿÿ
þzReshape._fix_unknown_dimensionc                 C   sp   t  |¡ ¡ }d |dd … v r!|d g}|tdd„ | jD ƒƒ7 }n|d g}||  |dd … | j¡7 }t  |¡S )Nr   r   c                 s   s     | ]}|d kr
|ndV  qdS )éÿÿÿÿNr   )Ú.0Úsr   r   r   Ú	<genexpr>~   s   € 
ÿz/Reshape.compute_output_shape.<locals>.<genexpr>)ÚtfZTensorShapeZas_listr   r	   r   )r
   r   r   r   r   r   Úcompute_output_shapey   s   

ÿ
ÿ
zReshape.compute_output_shapec                 C   s<   t  |t  |¡d f| j ¡}t  ¡ s| |  |j¡¡ |S )Nr   )r!   ZreshapeÚshaper	   Zexecuting_eagerlyZ	set_shaper"   )r
   ZinputsÚresultr   r   r   Úcallˆ   s   zReshape.callc                    s0   d| j i}tƒ  ¡ }tt| ¡ ƒt| ¡ ƒ ƒS )Nr	   )r	   r   Ú
get_configÚdictr   Úitems)r
   ZconfigZbase_configr   r   r   r&   ‘   s   

zReshape.get_config)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r%   r&   Ú__classcell__r   r   r   r   r      s    2	r   )r,   Znumpyr   Ztensorflow.compat.v2ÚcompatZv2r!   Zkeras.engine.base_layerr   Z tensorflow.python.util.tf_exportr   r   r   r   r   r   Ú<module>   s   