
    BVh+                         d Z ddlmZ ddl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	       G d
 de             Zd Zd Zd ZddZy)zContains the InputSpec class.    )dtypes)tensor_shape)tensor_spec)backend)nest)	tf_exportzlayers.InputSpec)v1c                   D    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.

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

  Args:
    dtype: Expected DataType of the input.
    shape: Shape tuple, expected shape of the input
      (may include None for unchecked 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, then 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.

  Example:

  ```python
  class MyLayer(Layer):
      def __init__(self):
          super(MyLayer, self).__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                  |      j                  nd | _        t	        j
                  |      }|j                  d }n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)Nz"The keys in axes must be integers.   z5Axis {} is greater than the maximum allowed value: {})r   as_dtypenamedtyper   TensorShaperanktupleas_listlenndimshapemax_ndimmin_ndimallow_last_axis_squeezeintaxes
ValueError	TypeErrormaxformat)selfr   r   r   r   r   r   r   r   kmax_dimmax_axiss               Y/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/engine/input_spec.py__init__zInputSpec.__init__E   sW    160A',,tDJ$$U+EzzeEMMO$ee*didjdidjDMDMDI#:D <ZRd,01q3q647?1di yydii+t}}/H"iiT]]a?gTYYh	G	P &735 	5 
 0Iy	 2	" <:;;<s   +
E 5EE E E6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              z  S )Nzdtype= zshape=zndim=z	max_ndim=z	min_ndim=zaxes=zInputSpec(%s)z, c              3   &   K   | ]	  }|s|  y wN ).0xs     r%   	<genexpr>z%InputSpec.__repr__.<locals>.<genexpr>r   s     &<Q!q&<s   )r   strr   r   r   r   r   join)r!   specs     r%   __repr__zInputSpec.__repr__k   s    ,0JJXDJJ'B,0JJXDJJ'B*.))Ws499~%26--[3t}}--R26--[3t}}--R*.))Ws499~%=D TYY&<$&<<<<    c                     | j                   | j                  | j                  | j                  | j                  | j
                  dS )Nr   r   r   r   r   r   r5   )r!   s    r%   
get_configzInputSpec.get_configt   s6    		MMMM		 r3   c                      | di |S )Nr+   r+   )clsconfigs     r%   from_configzInputSpec.from_config}   s    ==r3   )NNNNNNFN)	__name__
__module____qualname____doc__r&   r2   r6   classmethodr:   r+   r3   r%   r   r      sF    &R ',$5L=  r3   r   c                 B   | j                   !| j                  t        j                  d      S | j                  t        j                  | j                        S dg| j                   z  }| j                  D ]  }| j                  |   ||<    t        j                  |      S )a2  Returns a tf.TensorShape object that matches the shape specifications.

  If the InputSpec's shape or ndim is defined, this method will return a fully
  or partially-known shape. Otherwise, the returned TensorShape is None.

  Args:
    spec: an InputSpec object.

  Returns:
    a tf.TensorShape object
  N)r   r   r   r   r   )r1   r   as      r%   to_tensor_shaperB      s     
YY4::-##D))zz##DJJ//FTYYEYY 1eAh##E**r3   c           
      D
   | 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                  |      }|D ]  }t        |d      rt        d|       t        |      t        |       k7  rMt        d|z   dz   t        t        |             z   d	z   t        t        |            z   d
z   t        |      z         t        t        ||             D ]  \  }\  }}|t!        j"                  |j$                        }	|	j&                   y|j(                  |j*                  sx|	j&                  }
|
|j(                  k7  r]t        dt        |      z   dz   |z   dz   t        |j(                        z   dz   t        |
      z   dz   t        t-        |	            z         |j.                  l|j$                  j&                  }
|
T|
|j.                  kD  rEt        dt        |      z   dz   |z   dz   t        |j.                        z   dz   t        |
      z         |j0                  |j$                  j&                  }
|
l|
|j0                  k  r]t        dt        |      z   dz   |z   dz   t        |j0                        z   dz   t        |
      z   dz   t        t-        |	            z         |j2                  r|j2                  j                  |j2                  k7  rOt        dt        |      z   dz   |z   dz   t        |j2                        z   dz   t        |j2                        z         |	j5                         }|j6                  r|j6                  j9                         D ]  \  }}t        |d      r|j:                  }|!|t=        |         |dhvs4t        dt        |      z   dz   |z   dz   t        |      z   dz   t        |      z   dz   t?        |j$                        z          |j$                  |	j&                  +|j$                  }|j*                  r|r|d   dk(  r|dd }|r|d   dk(  r|dd }t        ||      D ]_  \  }}|	|||k7  st        dt        |      z   dz   |z   dz   t        |j$                        z   dz   t?        |j$                        z           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: r   z*Inputs to a layer should be tensors. Got: zLayer z	 expects z input(s), but it received z! input tensors. Inputs received: zInput z
 of layer z/ is incompatible with the layer: expected ndim=z, found ndim=z. Full shape received: z3 is incompatible with the layer: expected max_ndim=z5 is incompatible with the layer: : expected min_ndim=z0 is incompatible with the layer: expected dtype=z, found dtype=valuez/ is incompatible with the layer: expected axis z of input shape to have value z but received input with shape r   z is incompatible with layer z: expected shape=z, found shape=) r   flatten
isinstancedictr   allr   listkeysappendhasattrr   r   r/   	enumeratezipr   r   r   r   r   r   r   r   r   r   r   r   itemsrD   r   display_shape)
input_specinputs
layer_namer1   nameslist_inputsr   r-   input_indexr   r   shape_as_listaxisrD   
spec_shapespec_dimdims                    r%   assert_input_compatibilityr]      s   " 

||J'*#-.4TYY.E.
5zk )$v !$v{{}"5u> ? ? 	6$<() f<<& Ma
 1gKLLM 	[C
O#
X
*[8Z)*-((*-c&k*:; 99 <?v;G H H !*#fj*A B BHk9At|$$QWW-EzzyyT%A%AZZd		C$44|C#$'**,/		N;=LM T# &?? U5\*	+ , 	,
 }} WW\\d		dT]]2C$44|C#$'..03DMM0BC )) ,/t95 6 	6 }} WW\\d		dT]]2C$44|C#$'0025dmm2DE )) ,/t95 3	3
 U5\*+ , 	, zz	
	#C$44|C#$'++-0_= ** -0L9 : 	: MMOMyy* J+$5'"++%s4y!9%!N[))L8:E I= =?B4yI./14U< 00 3@2HIJ J	J zz%**"8::j		%	%]2.!3',-*R.A-!#2*z=9 H-(CCO_XK(88;<>HI0136tzz?C .. 1>agg0FG H HHyBH5 /s   Tc                 F    t        t        | j                                     S r*   )r/   r   r   )r   s    r%   rQ   rQ     s    	U5==?#	$$r3   Nc                     |xs t        j                         }t        | t              r/| j                  xs |}t        j                  t        |       |      S t        j                  d|      S )z2Converts a Keras InputSpec object to a TensorSpec.N)r   floatxrG   r   r   r   
TensorSpecrB   )rR   default_dtyper   s      r%   to_tensor_specrc     sX    37>>#3-
I&-E!!/*"=uEE			m	44r3   r*   )r>   tensorflow.python.frameworkr   r   r   tensorflow.python.kerasr   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   objectr   rB   r]   rQ   rc   r+   r3   r%   <module>ri      s[   " $ . 4 3 + ' 6 !"#c c $cL+.sHl%5r3   