
    2Vhy&                     \    d dl mZ d dl mZ d dlmZ  eddg       G d d             Zd Zy	)
    )backend)tree)keras_exportzkeras.InputSpeczkeras.layers.InputSpecc                   F    e Zd ZdZ	 	 	 	 	 	 	 	 	 ddZd Zd Zed        Zy)	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)
    ```
    Nc
                    |t        j                  |      nd | _        |+t        j                  |      | _        t        |      | _        n|| _        d | _        || _        || _        || _	        |	| _
        || _        	 |xs i }|D 
ci c]  }
t        |
      ||
    c}
| _        | j                  rv| j                  | j                  ]| j                  r| j                  n| j                  dz
  }t!        | j                        }||kD  rt        dj#                  ||            y y y c c}
w # t        t        f$ r t        d|       w xY w)NzAArgument `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_squeezeintaxes
ValueError	TypeErrormaxformat)selfr   r   r   r   r   r   r   r   r   kmax_dimmax_axiss                K/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/input_spec.py__init__zInputSpec.__init__3   sM    160AG%%e,t 	
  2259DJE
DIDIDJ  	 '>$	:2D267QQa7DI 99$))/4==3L$(IItyy4==AEG499~H'! ((.x(A  " 4M9 8I& 	""&) 	s   >
D. D)D. )D. .Ec           	         | j                   rdt        | j                         z   nd| j                  rdt        | j                        z   nd| j                  rdt        | j                        z   nd| j                  rdt        | j                        z   nd| j
                  rdt        | j
                        z   nd| j                  rdt        | j                        z   ndg}dd	j                  d
 |D               dS )Nzdtype= zshape=zndim=z	max_ndim=z	min_ndim=zaxes=z
InputSpec(z, c              3   &   K   | ]	  }|s|  y w)N ).0xs     r   	<genexpr>z%InputSpec.__repr__.<locals>.<genexpr>h   s     %;Aa%;s   ))r   strr   r   r   r   r   join)r   specs     r   __repr__zInputSpec.__repr___   s    ,0JJXDJJ'B,0JJXDJJ'B*.))Ws499~%26--[3t}}--R26--[3t}}--R*.))Ws499~%
 DII%;%;;<A>>    c                     | j                   | j                  | j                  | j                  | j                  | j
                  dS )Nr   r   r   r   r   r   r/   )r   s    r   
get_configzInputSpec.get_configj   s6    ZZZZIIII
 	
r-   c                      | di |S )Nr$   r$   )clsconfigs     r   from_configzInputSpec.from_configt   s    }V}r-   )	NNNNNNFNF)	__name__
__module____qualname____doc__r    r,   r0   classmethodr4   r$   r-   r   r   r      sI    )Z  %*X	?
  r-   r   c                    | syt        j                  |       } t        |t              rr| D cg c]  }|j                   }}t        |      rNg }|D ]E  }||vr+t        d| dt        |j                                d|       |j                  ||          G |}t        j                  |      }t        |      t        |       k7  r)t        d| dt        |        dt        |       d|       t        t        ||             D ]l  \  }\  }}|||j                  rt        |d	      st        d
| dt        |       d| d      t!        j"                  |j$                        }	t        |	      }
|j&                  ?|j(                  s3|
|j&                  k7  r$t        d| d| d|j&                   d|
 d|	 
      |j*                  2|
0|
|j*                  kD  r!t        d| d| d|j*                   d|
       |j,                  5|
3|
|j,                  k  r$t        d| d| d|j,                   d|
 d|	 
      |j.                  Ot!        j0                  |j.                        }||j.                  k7  r!t        d| d| d|j.                   d|       |j2                  rH|j2                  j5                         D ]+  \  }}|	|	|   |dhvst        d| d| d| d| d|	 
       |j$                  |j$                  }|j(                  r|	r|	d   dk(  r|	dd }	|r|d   dk(  r|dd }t        ||	      D ]1  \  }}|	|||k7  st        d| d| d|j$                   d|	        o yc c}w )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.
    Nz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dictr   allr   listkeysappendr   	enumeratezipr   hasattrtyper   r   r   r   r   r   r   r   r
   r   items)
input_specinputs
layer_namer+   nameslist_inputsr   input_indexr&   r   r   r   axisvalue
spec_shapespec_dimdims                    r   assert_input_compatibilityrS   y   sY   " j)J&$'12t22u:K 1v%$24& 9B./ 088=w@  ""6$<01 !F\\&!F
6{c*o%j\C
O+< =  #F} -  &x)
 	

 #,C
,C"D UYa<9 q'"<QC @ G9$::,bJ 
 ))!''25z99 )E)Etyy  [MZL A%%)YYK}TF C,,174  ==$D4==$8 [MZL A))- 8""&)  ==$D4==$8 [MZL A))- 8""& (,,17	4  ::!--agg6E

" [MZL A&&*jj\ 2##('+  99#yy0 e$t= * % [ EFFJV L88=w ?!!&	)  ::!J++U2Y!^!#2JE*R.A"5!+CRJ!$Z!7 #'CO3($[MZL I..2jj\ :++0'3 [U+ 3s   M6N)	keras.srcr   r   keras.src.api_exportr   r   rS   r$   r-   r   <module>rV      s=      -  ":;<o o =odAr-   