
    0h!                         d Z ddlmc 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	e             Z ed
      dd       Zy)z'Layer that concatenates several inputs.    N)backend)_Merge)tf_utils)keras_exportzkeras.layers.Concatenatec                        e Zd ZdZd fd	Zej                  d        Zd Zej                  d        Z	d	dZ
 fdZ xZS )
Concatenatea  Layer that concatenates a list of inputs.

    It takes as input a list of tensors, all of the same shape except
    for the concatenation axis, and returns a single tensor that is the
    concatenation of all inputs.

    >>> x = np.arange(20).reshape(2, 2, 5)
    >>> print(x)
    [[[ 0  1  2  3  4]
      [ 5  6  7  8  9]]
     [[10 11 12 13 14]
      [15 16 17 18 19]]]
    >>> y = np.arange(20, 30).reshape(2, 1, 5)
    >>> print(y)
    [[[20 21 22 23 24]]
     [[25 26 27 28 29]]]
    >>> tf.keras.layers.Concatenate(axis=1)([x, y])
    <tf.Tensor: shape=(2, 3, 5), dtype=int64, numpy=
    array([[[ 0,  1,  2,  3,  4],
            [ 5,  6,  7,  8,  9],
            [20, 21, 22, 23, 24]],
           [[10, 11, 12, 13, 14],
            [15, 16, 17, 18, 19],
            [25, 26, 27, 28, 29]]])>

    >>> x1 = tf.keras.layers.Dense(8)(np.arange(10).reshape(5, 2))
    >>> x2 = tf.keras.layers.Dense(8)(np.arange(10, 20).reshape(5, 2))
    >>> concatted = tf.keras.layers.Concatenate()([x1, x2])
    >>> concatted.shape
    TensorShape([5, 16])

    c                 N    t        |   di | || _        d| _        d| _        y)a.  Instantiates a Concatenate layer.

        >>> x = np.arange(20).reshape(2, 2, 5)
        >>> print(x)
        [[[ 0  1  2  3  4]
          [ 5  6  7  8  9]]
         [[10 11 12 13 14]
          [15 16 17 18 19]]]
        >>> y = np.arange(20, 30).reshape(2, 1, 5)
        >>> print(y)
        [[[20 21 22 23 24]]
         [[25 26 27 28 29]]]
        >>> tf.keras.layers.Concatenate(axis=1)([x, y])
        <tf.Tensor: shape=(2, 3, 5), dtype=int64, numpy=
        array([[[ 0,  1,  2,  3,  4],
                [ 5,  6,  7,  8,  9],
                [20, 21, 22, 23, 24]],
               [[10, 11, 12, 13, 14],
                [15, 16, 17, 18, 19],
                [25, 26, 27, 28, 29]]])>

        Args:
          axis: Axis along which to concatenate.
          **kwargs: standard layer keyword arguments.
        TFN )super__init__axissupports_masking_reshape_required)selfr   kwargs	__class__s      a/var/www/html/engine/venv/lib/python3.12/site-packages/tf_keras/src/layers/merging/concatenate.pyr   zConcatenate.__init__?   s,    4 	"6"	 $!&    c                 d  
 t        |      dk  st        |d   t              st        d|       t	        d |D              ry |D cg c]  }t        |       }}t               }t        t        |            D ]/  }||   | j                  = |j                  t        ||                1 t        |      dk7  rrd| }t        d |D              }t        |      dk7  rt        |      |\  }t        |      D ]/  
t        
fd|D              }	t        |	      dkD  s&t        |       y y c c}w )N   r   z\A `Concatenate` layer should be called on a list of at least 1 input. Received: input_shape=c              3   $   K   | ]  }|d u  
 y wNr
   .0shapes     r   	<genexpr>z$Concatenate.build.<locals>.<genexpr>f   s     6u}6   ztA `Concatenate` layer requires inputs with matching shapes except for the concatenation axis. Received: input_shape=c              3   2   K   | ]  }t        |        y wr   )lenr   s     r   r   z$Concatenate.build.<locals>.<genexpr>u   s     :uE
:s   c              3   2   K   | ]  }|   |     y wr   r
   )r   r   r   s     r   r   z$Concatenate.build.<locals>.<genexpr>}   s'      "T{. $K"s   )
r   
isinstancetuple
ValueErroralllistsetranger   add)r   input_shaper   reduced_inputs_shapes	shape_setierr_msgranksrankunique_dimsr   s             @r   buildzConcatenate.build^   sR    {az+a.%'H;;F-I  6+66:E Fe F FE	s012 	;A%a(3MM% 5a 89:	; y>Q))47  :	::E5zQ ))GTd 	. " "!*" 
 {#a'$W--	.  !Gs   D-c                 D    t        j                  || j                        S )Nr   )r   concatenater   )r   inputss     r   _merge_functionzConcatenate._merge_function   s    ""6		::r   c                    t        |t        t        f      rt        |d   t        t        f      st        d|       |}t        |d         }|dd  D ]_  }|| j                     || j                     d || j                  <    t        |      S || j                  xx   || j                     z  cc<   a t        |      S )Nr   zRA `Concatenate` layer should be called on a list of inputs. Received: input_shape=r   )r!   r"   r%   r#   r   )r   r)   input_shapesoutput_shaper   s        r   compute_output_shapez Concatenate.compute_output_shape   s    ;6;q>E4=9 ))47  #LO,!!"% 	8EDII&.%		2B2J*.TYY'\"" #uTYY'77#		8
 \""r   c                    |y t        |t        t        f      st        d|       t        |t        t        f      st        d|       t	        |      t	        |      k7  r)t        d| dt	        |       d| dt	        |             t        d |D              ry g }t        ||      D ]  \  }}|'|j                  t        j                  |d             /t        j                  |      t        j                  |      k  r'|j                  t        j                  |d	
             |j                  |        t        j                  || j                  
      }t        j
                  |d	d      S )Nz'`mask` should be a list. Received mask=z,`inputs` should be a list. Received: inputs=zLThe lists `inputs` and `mask` should have the same length. Received: inputs=z of length z, and mask=c              3   $   K   | ]  }|d u  
 y wr   r
   )r   ms     r   r   z+Concatenate.compute_mask.<locals>.<genexpr>   s     'QqDy'r   bool)dtyper3   F)r   keepdims)r!   r"   r%   r#   r   r$   zipappendtf	ones_liker   ndimexpand_dimsr4   r   )r   r5   maskmasksinput_imask_iconcatenateds          r   compute_maskzConcatenate.compute_mask   sU   <$.FtfMNN&5$-0>vhG  t9F#$$*8;s6{m Dv[T5 
 '$'' "640 	%OGV~R\\'@Af%W(==R^^F<=V$	% **5tyyA{{<b5AAr   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   rO   zConcatenate.get_config   sK    DII
 g(*D**,-V\\^0DDEEr   r@   r   )__name__
__module____qualname____doc__r   r   shape_type_conversionr1   r6   r:   rM   rO   __classcell__)r   s   @r   r   r      s[    B'> ##$. $$.L; ### $#*BBF Fr   r   zkeras.layers.concatenatec                 (     t        dd|i||       S )av  Functional interface to the `Concatenate` layer.

    >>> x = np.arange(20).reshape(2, 2, 5)
    >>> print(x)
    [[[ 0  1  2  3  4]
      [ 5  6  7  8  9]]
     [[10 11 12 13 14]
      [15 16 17 18 19]]]
    >>> y = np.arange(20, 30).reshape(2, 1, 5)
    >>> print(y)
    [[[20 21 22 23 24]]
     [[25 26 27 28 29]]]
    >>> tf.keras.layers.concatenate([x, y],
    ...                             axis=1)
    <tf.Tensor: shape=(2, 3, 5), dtype=int64, numpy=
    array([[[ 0,  1,  2,  3,  4],
          [ 5,  6,  7,  8,  9],
          [20, 21, 22, 23, 24]],
         [[10, 11, 12, 13, 14],
          [15, 16, 17, 18, 19],
          [25, 26, 27, 28, 29]]])>

    Args:
        inputs: A list of input tensors.
        axis: Concatenation axis.
        **kwargs: Standard layer keyword arguments.

    Returns:
        A tensor, the concatenation of the inputs alongside axis `axis`.
    r   r
   )r   )r5   r   r   s      r   r4   r4      s    @ ,;+D+F+F33r   rT   )rX   tensorflow.compat.v2compatv2rD   tf_keras.srcr   &tf_keras.src.layers.merging.base_merger   tf_keras.src.utilsr    tensorflow.python.util.tf_exportr   r   r4   r
   r   r   <module>rc      sb    . " !   9 ' : ()gF& gF *gFT ()4 *4r   