
    0h!                     f    d Z ddlZddlmc mZ ddlmZ ddl	m
Z
  e
d       G d de             Zy)zContains the Reshape layer.    N)Layer)keras_exportzkeras.layers.Reshapec                   >     e Zd ZdZ fdZd Zd Zd Z fdZ xZ	S )Reshapea  Layer that reshapes inputs into the given shape.

    Input shape:
      Arbitrary, although all dimensions in the input shape must be known/fixed.
      Use the keyword argument `input_shape` (tuple of integers, does not
      include the samples/batch size axis) when using this layer as the first
      layer in a model.

    Output shape:
      `(batch_size,) + target_shape`

    Example:

    >>> # as first layer in a Sequential model
    >>> model = tf.keras.Sequential()
    >>> model.add(tf.keras.layers.Reshape((3, 4), input_shape=(12,)))
    >>> # model.output_shape == (None, 3, 4), `None` is the batch size.
    >>> model.output_shape
    (None, 3, 4)

    >>> # as intermediate layer in a Sequential model
    >>> model.add(tf.keras.layers.Reshape((6, 2)))
    >>> model.output_shape
    (None, 6, 2)

    >>> # also supports shape inference using `-1` as dimension
    >>> model.add(tf.keras.layers.Reshape((-1, 2, 2)))
    >>> model.output_shape
    (None, 3, 2, 2)
    c                 D    t        |   di | t        |      | _        y)a  Creates a `tf.keras.layers.Reshape`  layer instance.

        Args:
          target_shape: Target shape. Tuple of integers, does not include the
            samples dimension (batch size).
          **kwargs: Any additional layer keyword arguments.
        N )super__init__tupletarget_shape)selfr   kwargs	__class__s      _/var/www/html/engine/venv/lib/python3.12/site-packages/tf_keras/src/layers/reshaping/reshape.pyr
   zReshape.__init__<   s"     	"6"!,/    c                 J   t        |      }dj                  ||      }d\  }}t        |      D ]#  \  }}|dk  r||}t        d| d      ||z  }% t	        j
                  |t              }|"|dk(  s||z  dk7  rt        |      ||z  ||<   |S ||k7  rt        |      |S )a  Find and replace a missing dimension in an output shape.

        This is a near direct port of the internal Numpy function
        `_fix_unknown_dimension` in `numpy/core/src/multiarray/shape.c`

        Args:
          input_shape: Shape of array being reshaped
          output_shape: Desired shape of the array with at most a single -1
            which indicates a dimension that should be derived from the input
            shape.

        Returns:
          The new output shape with a -1 replaced with its computed value.

        Raises:
          ValueError: If the total array size of the output_shape is
          different than the input_shape, or more than one unknown dimension
          is specified.
        zNtotal size of new array must be unchanged, input_shape = {}, output_shape = {})   Nr   zTThere must be at most one unknown dimension in output_shape. Received: output_shape=.)dtype)listformat	enumerate
ValueErrornpprodint)	r   input_shapeoutput_shapemsgknownunknownindexdimoriginals	            r   _fix_unknown_dimensionzReshape._fix_unknown_dimensionG   s    ( L)228&\3 	 !w#L1 
	JE3Qw?#G$@@L~QP 
 
	 77;c2zX-2 o%$,$5L!  S/!r   c                    t        j                  |      j                         }d |dd  v r&|d   g}|t        d | j                  D              z  }n(|d   g}|| j                  |dd  | j                        z  }t        j                  |      S )Nr   r   c              3   .   K   | ]  }|d k7  r|nd  yw)Nr   ).0ss     r   	<genexpr>z/Reshape.compute_output_shape.<locals>.<genexpr>~   s       "+,Q"W$&"s   )tfTensorShapeas_listr   r   r%   )r   r   r   s      r   compute_output_shapezReshape.compute_output_shapey   s    nn[199;;qr?"'N+LE "040A0A"  L (N+LD77AB!2!2 L ~~l++r   c                     t        j                  |t        j                  |      d   f| j                  z         }t        j                         s*|j                  | j                  |j                               |S )Nr   )r,   reshapeshaper   executing_eagerly	set_shaper/   )r   inputsresults      r   callzReshape.call   s^    FRXXf%5a%8$:T=N=N$NO##% T66v||DEr   c                     d| j                   i}t        | 	         }t        t	        |j                               t	        |j                               z         S )Nr   )r   r	   
get_configdictr   items)r   configbase_configr   s      r   r9   zReshape.get_config   sI     $"3"34g(*D**,-V\\^0DDEEr   )
__name__
__module____qualname____doc__r
   r%   r/   r7   r9   __classcell__)r   s   @r   r   r      s)    >	00d,F Fr   r   )rA   numpyr   tensorflow.compat.v2compatv2r,   tf_keras.src.engine.base_layerr    tensorflow.python.util.tf_exportr   r   r   r   r   <module>rI      sD    "  ! ! 0 : $%xFe xF &xFr   