o
    2hy&                     @   sJ   d dl mZ d dl mZ d dlmZ eddgG dd dZdd	 Zd
S )    )backend)tree)keras_exportzkeras.InputSpeczkeras.layers.InputSpecc                   @   sH   e Zd ZdZ									dddZdd Zdd	 Zed
d ZdS )	InputSpeca  Specifies the rank, dtype and shape of every input to a layer.

    Layers can expose (if appropriate) an `input_spec` attribute:
    an instance of `InputSpec`, or a nested structure of `InputSpec` instances
    (one per input tensor). These objects enable the layer to run input
    compatibility checks for input structure, input rank, input shape, and
    input dtype for the first argument of `Layer.__call__`.

    A `None` entry in a shape is compatible with any dimension.

    Args:
        dtype: Expected dtype of the input.
        shape: Shape tuple, expected shape of the input
            (may include `None` for dynamic axes).
            Includes the batch size.
        ndim: Integer, expected rank of the input.
        max_ndim: Integer, maximum rank of the input.
        min_ndim: Integer, minimum rank of the input.
        axes: Dictionary mapping integer axes to
            a specific dimension value.
        allow_last_axis_squeeze: If `True`, allow inputs of rank N+1 as long
            as the last axis of the input is 1, as well as inputs of rank N-1
            as long as the last axis of the spec is 1.
        name: Expected key corresponding to this input when passing data as
            a dictionary.
        optional: Boolean, whether the input is optional or not.
            An optional input can accept `None` values.

    Example:

    ```python
    class MyLayer(Layer):
        def __init__(self):
            super().__init__()
            # The layer will accept inputs with
            # shape (*, 28, 28) & (*, 28, 28, 1)
            # and raise an appropriate error message otherwise.
            self.input_spec = InputSpec(
                shape=(None, 28, 28, 1),
                allow_last_axis_squeeze=True)
    ```
    NFc
              	      s  |d ur	t |nd | _|d urt || _t|| _n|| _d | _|| _|| _|| _	|	| _
|| _z p5i   fdd D | _W n ttfyQ   td  w | jr{| jd us_| jd ur}| jre| jn| jd }
t| j}||
krtd||
d S d S d S )Nc                    s   i | ]	}t | | qS  )int).0kaxesr   V/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/layers/input_spec.py
<dictcomp>O   s    z&InputSpec.__init__.<locals>.<dictcomp>zAArgument `axes` must be a dict with integer keys. Received: axes=   z5Axis {} is greater than the maximum allowed value: {})r   standardize_dtypedtypestandardize_shapeshapelenndimmax_ndimmin_ndimnameoptionalallow_last_axis_squeezer   
ValueError	TypeErrormaxformat)selfr   r   r   r   r   r   r   r   r   max_dimmax_axisr   r
   r   __init__3   s@   
zInputSpec.__init__c                 C   s   | j r
dt| j  nd| jrdt| j nd| jr dt| j nd| jr+dt| j nd| jr6dt| j nd| jrAdt| j ndg}dd	d
d |D  dS )Nzdtype= zshape=zndim=z	max_ndim=z	min_ndim=zaxes=z
InputSpec(z, c                 s   s    | ]}|r|V  qd S )Nr   )r   xr   r   r   	<genexpr>h   s    z%InputSpec.__repr__.<locals>.<genexpr>))r   strr   r   r   r   r   join)r   specr   r   r   __repr___   s   zInputSpec.__repr__c                 C   s   | j | j| j| j| j| jdS )Nr   r   r   r   r   r   r*   )r   r   r   r   
get_configj   s   zInputSpec.get_configc                 C   s   | di |S )Nr   r   )clsconfigr   r   r   from_configt   s   zInputSpec.from_config)	NNNNNNFNF)	__name__
__module____qualname____doc__r!   r)   r+   classmethodr.   r   r   r   r   r      s     -
,
r   c                 C   sR  | sdS t | } t|tr>dd | D }t|r>g }|D ]}||vr4td| dt|  d| |||  q|}t |}t	|t	| kr_td| dt	|  d	t	| d
| t
t|| D ]?\}\}}|du rrqf|du rz|jrzqft|dstd| dt| d| dt|j}	t	|	}
|jdur|js|
|jkrtd| d| d|j d|
 d|	 
|jdur|
dur|
|jkrtd| d| d|j d|
 |jdur|
dur|
|jk rtd| d| d|j d|
 d|	 
|jdurt|j}||jkrtd| d| d|j d| |jrN|j D ]&\}}|durL|	| |dhvrLtd| d| d| d| d|	 
q'|jdur|j}|jr{|	rk|	d dkrk|	dd }	|r{|d dkr{|dd }t||	D ]%\}}|dur|dur||krtd| d| d|j d|	 qqfdS ) a  Checks compatibility between the layer and provided inputs.

    This checks that the tensor(s) `inputs` verify the input assumptions
    of a layer (if any). If not, a clear and actional exception gets raised.

    Args:
        input_spec: An InputSpec instance, list of InputSpec instances, a nested
            structure of InputSpec instances, or None.
        inputs: Input tensor, list of input tensors, or a nested structure of
            input tensors.
        layer_name: String, name of the layer (for error message formatting).

    Raises:
        ValueError: in case of mismatch between
            the provided inputs and the expectations of the layer.
    Nc                 S   s   g | ]}|j qS r   )r   )r   r(   r   r   r   
<listcomp>   s    z.assert_input_compatibility.<locals>.<listcomp>zMissing data for input "z*". You passed a data dictionary with keys z. Expected the following keys: zLayer "z
" expects z input(s), but it received z! input tensors. Inputs received: r   z*Inputs to a layer should be tensors. Got 'z' (of type z) as input for layer 'z'.zInput z of layer "z0" is incompatible with the layer: expected ndim=z, found ndim=z. Full shape received: z4" is incompatible with the layer: expected max_ndim=z4" is incompatible with the layer: expected min_ndim=z1" is incompatible with the layer: expected dtype=z, found dtype=z0" is incompatible with the layer: expected axis z of input shape to have value z , but received input with shape r   z1" is incompatible with the layer: expected shape=z, found shape=)r   flatten
isinstancedictallr   listkeysappendr   	enumeratezipr   hasattrtyper   r   r   r   r   r   r   r   r   r   items)
input_specinputs
layer_namenameslist_inputsr   input_indexr#   r(   r   r   r   axisvalue
spec_shapespec_dimdimr   r   r   assert_input_compatibilityy   s   








rM   N)	keras.srcr   r   keras.src.api_exportr   r   rM   r   r   r   r   <module>   s    
r