B
    d                 @   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                s6   t  jf | 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__ P/var/www/html/venv/lib/python3.7/site-packages/keras/layers/reshaping/flatten.pyr	   ?   s    zFlatten.__init__c       	      C   s  | j rH|jj}|rH|dkrHdg}|td| |d tj||d}t rpt	|jd dg}t
||S |j}|j}|dkrtj|ddS tj|d }|dd  }| rtttj|}t	d|g}n.|d k	rt	t|dg}nt|d dg}t
||S d 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   inputsr   ZpermutationZflattened_shapeinput_shapeZ	batch_dimZnon_batch_dimsZlast_dimr   r   r   callE   s0    
zFlatten.callc             C   sn   t | }|s t dg}n
|d g}t|dd  rZ|tj|dd  tdg7 }n
|d g7 }t |S )Nr   r   )Zdtype)r   ZTensorShapeas_listnpall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   config)r   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   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   