o
    ={c©  ã                   @   sz   d Z ddlZddlZddlZddlm  mZ ddl	m
Z
 ddl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 flatten layer.é    N)ÚLayer)Ú	InputSpec)Ú
conv_utils)Úkeras_exportzkeras.layers.Flattenc                       s>   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Z‡ fd	d
„Z‡  ZS )ÚFlattenaÓ  Flattens the input. Does not affect the batch size.

    Note: If inputs are shaped `(batch,)` without a feature axis, then
    flattening adds an extra channel dimension and output shape is `(batch, 1)`.

    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, ..., channels)` while `channels_first` corresponds to
        inputs with shape `(batch, channels, ...)`.
        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".

    Example:

    >>> model = tf.keras.Sequential()
    >>> model.add(tf.keras.layers.Conv2D(64, 3, 3, input_shape=(3, 32, 32)))
    >>> model.output_shape
    (None, 1, 10, 64)

    >>> model.add(Flatten())
    >>> model.output_shape
    (None, 640)

    Nc                    s:   t ƒ jdi |¤Ž t |¡| _tdd| _| jdk| _d S )Né   )Zmin_ndimZchannels_first© )ÚsuperÚ__init__r   Znormalize_data_formatÚdata_formatr   Z
input_specÚ_channels_first)Úselfr   Úkwargs©Ú	__class__r   ú>lib/python3.10/site-packages/keras/layers/reshaping/flatten.pyr
   ?   s   zFlatten.__init__c           	      C   s  | j r$|jj}|r$|dkr$dg}| td|ƒ¡ | d¡ tj||d}t ¡ r8t 	|jd dg¡}t 
||¡S |j}|j}|dkrItj|ddS tj |d ¡}|dd … }| ¡ rltt tj|¡ƒ}t 	d|g¡}n|d urzt 	t|ƒdg¡}n	t |¡d dg}t 
||¡S )Nr   r   é   )Zperméÿÿÿÿ)Zaxis)r   ÚshapeÚrankÚextendÚrangeÚappendÚtfZ	transposeZexecuting_eagerlyZconstantZreshapeZexpand_dimsÚcompatZdimension_valueZis_fully_definedÚintÚ	functoolsÚreduceÚoperatorÚmul)	r   Zinputsr   ZpermutationZflattened_shapeÚinput_shapeZ	batch_dimZnon_batch_dimsZlast_dimr   r   r   ÚcallE   s2   
ÿzFlatten.callc                 C   sn   t  |¡ ¡ }|st  dg¡}n|d g}t |dd … ¡r-|tj|dd … tdg7 }n|d g7 }t  |¡S )Nr   r   )Zdtype)r   ZTensorShapeZas_listÚnpÚallZprodr   )r   r    Zoutput_shaper   r   r   Úcompute_output_shapej   s   


zFlatten.compute_output_shapec                    s   t ƒ  ¡ }| d| ji¡ |S )Nr   )r	   Ú
get_configÚupdater   )r   Zconfigr   r   r   r%   v   s   
zFlatten.get_config)N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r!   r$   r%   Ú__classcell__r   r   r   r   r       s    %r   )r*   r   r   Znumpyr"   Ztensorflow.compat.v2r   Zv2r   Zkeras.engine.base_layerr   Zkeras.engine.input_specr   Zkeras.utilsr   Z tensorflow.python.util.tf_exportr   r   r   r   r   r   Ú<module>   s   