
    &Vf5                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d Z
d Z	 	 dd
Z	 	 	 	 ddZd Zd Zd ZddZ ed          d             ZdS )    N)tree)keras_exportcanonicalize_axis)to_tuple_or_listc                    t          |           } t          |          }| }|}t          |           t          |          k    r&dgt          |           t          |          z
  z  |z   }t          |           t          |          k     r&dgt          |          t          |           z
  z  | z   } t          |           }t          t          |                     D ]}| |         dk    r||         ||<   | |         ||         dk    rdn||         ||<   <||         dk    s||         ||         | |         k    r| |         ||<   nt          d| |          d||          d| d| d	          |S )aS  Broadcast input shapes to a unified shape.

    Convert to list for mutability.

    Args:
        shape1: A tuple or list of integers.
        shape2: A tuple or list of integers.

    Returns:
        output_shape (list of integers or `None`): The broadcasted shape.

    Example:
    >>> broadcast_shapes((5, 3), (1, 3))
    [5, 3]
       Nz2Cannot broadcast shape, the failure dim has value z!, which cannot be broadcasted to z. Input shapes are: z and .)listlenrange
ValueError)shape1shape2origin_shape1origin_shape2output_shapeis         Z/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/ops/operation_utils.pybroadcast_shapesr      s     &\\F&\\FMM
6{{S[[  Fc&kk12V;
6{{S[[  Fc&kk12V;<<L3v;;  !9>>$QiLOOAY&,Qi1nndd&)LOOayA~~!2fQi6!96L6L"()Q NayN NCI!9N N)6N N=JN N N       c                 4   t          |           } t          |           t                    t                    t          |           z   fdD             t          |           fdt	                    D             }t          |          S )a  Compute the output shape for the `expand_dims` operation.

    Args:
        input_shape: Input shape.
        axis: int or sequence of ints for the axis to expand.

    Returns:
        Tuple of ints: The output shape after the `expand_dims` operation.
    Nc                 0    g | ]}t          |          S  r   ).0aout_ndims     r   
<listcomp>z4compute_expand_dims_output_shape.<locals>.<listcomp>F   s$    999qa**999r   c                 :    g | ]}|v rd nt                    S r	   )next)r   axaxis
shape_iters     r   r   z4compute_expand_dims_output_shape.<locals>.<listcomp>H   s:       24R4ZZT*--  r   )r   r   r   iterr   tuple)input_shaper#   	new_shaper   r$   s    ` @@r    compute_expand_dims_output_shaper)   7   s     {##K|;D!!D4yy3{+++H9999D999Dk""J    8=h  I r   validchannels_lastc                 $   ||n|}t          |           }t          j        |           } |dk    r| dd         }n
| dd         }g }t          t	          |                    D ]$}||         d||<   |                    |           %t          j        |          }|dk    rct          j        ||z
  |z            dz   }	t          t	          |	                    D ](}||vr"|	|         dk     rt          d|  d	| d
          )n6|dk    rt          j        |dz
  |z            dz   }	nt          d|           d |	D             }	|D ]}d|	|<   t          |	          }	|dk    r|d         f|	z   |d         fz   }
n|d         |d         f|	z   }
|
S )a3  Computes the output shape of pooling operations.

    Args:
        input_shape: Input shape. Must be a tuple of integers.
        pool_size: Size of the pooling operation. Must be a tuple of integers.
        strides: Stride of the pooling operation. Must be a tuple of integers.
            Defaults to `pool_size`.
        padding: Padding method. Available methods are `"valid"` or `"same"`.
            Defaults to `"valid"`.
        data_format: String, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, height, width, channels)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, channels, height, weight)`. Defaults to `"channels_last"`.

    Returns:
        Tuple of ints: The output shape of the pooling operation.

    Examples:

    # Basic usage with square pooling on a single image
    >>> compute_pooling_output_shape((1, 4, 4, 1), (2, 2))
    (1, 2, 2, 1)

    # Strided pooling on a single image with strides different from pool_size
    >>> compute_pooling_output_shape((1, 4, 4, 1), (2, 2), strides=(1, 1))
    (1, 3, 3, 1)

    # Pooling on a batch of images
    >>> compute_pooling_output_shape((32, 4, 4, 3), (2, 2))
    (32, 2, 2, 3)
    Nr+   r	      r*   r   z@Computed output size would be negative. Received: `inputs.shape=z` and `pool_size=`.samezGArgument `padding` must be either 'valid' or 'same'. Received: padding=c                 ,    g | ]}t          |          S r   intr   r   s     r   r   z0compute_pooling_output_shape.<locals>.<listcomp>       AAAqCFFAAAr   )	r   nparrayr   r   appendfloorr   r&   )r'   	pool_sizestridespaddingdata_formatinput_shape_originspatial_shape	none_dimsr   output_spatial_shaper   s              r   compute_pooling_output_shaperB   N   sX   N #?iiGk**(;''Ko%%#AbD)#ABBI3}%%&&    #  "M!Q##I'Hmi/7:;;a? 	 s/0011 	 	A	!!&:1&=&A&A Q%0Q QCLQ Q Q  	 
F		!x):g(EFFJ!! !
 
 	
 BA,@AAA ' '"&Q !566o%%"$"#!"%'( 	 q!q!
 !! r   r	   c           
         |dk    r| dd         }|| d         |fz   }n| dd         }|| d         |fz   }t          |          t          |           k    rt          d| d|  d          t          |t                    r|ft          |          z  }t          |t                    r|ft          |          z  }t          |          t          |          k    rt          d	| d
|  d          g }	t	          j        |          }t          t          |                    D ]$}
||
         d||
<   |	                    |
           %t	          j        |dd                   }t	          j        |          }|dk    rot	          j        |||dz
  z  z
  dz
  |z            dz   }t          t          |                    D ]+}
|
|	vr%||
         dk     rt          d|  d| d| d          ,n=|dk    s|dk    rt	          j        |dz
  |z            dz   }nt          d| d          d |D             }|	D ]}
d||
<   t          |          }|dk    r| d         f|z   |d         fz   }n| d         |d         f|z   }|S )z%Compute the output shape of conv ops.r+   r	   r-   r.   NzNKernel shape must have the same length as input, but received kernel of shape z and input of shape r
   zlDilation must be None, scalar or tuple/list of length of inputs' spatial shape, but received `dilation_rate=z` and input of shape r*   r   z?Computed output size would be negative. Received `inputs shape=z`, `kernel shape=z`, `dilation_rate=r/   r0   causalz9`padding` must be either `'valid'` or `'same'`. Received c                 ,    g | ]}t          |          S r   r2   r4   s     r   r   z-compute_conv_output_shape.<locals>.<listcomp>   r5   r   )
r   r   
isinstancer3   r6   r7   r   r8   r9   r&   )r'   filterskernel_sizer;   r<   r=   dilation_rater?   kernel_shaper@   r   kernel_spatial_shaperA   r   s                 r   compute_conv_output_shaperM      s    o%%#AbD)"k"ow%??#ABB"k!ng%>>
<C,,,,-+- -)- - -
 
 	

 -%% >&(3}+=+=='3 2*s=111
=S////-+- - *- - -
 
 	
 IH]++M3}%%&&    #  "M!Q8L"$566H]++M'H2F2J!KKaO  	 	 s/0011 	 	A	!!&:1&=&A&A 8%08 8%18 8 '48 8 8  	 
F		g11!x):g(EFFJ  
 
 	
 BA,@AAA ' '"&Q !566o%%^ 44R8H7JJ 	 $AR(89<PPr   c                    t          |           dk    r
d| d         f} t          |          dk    r
|d         df}| d         0|d         (| d         |d         k    rt          d|  d| d          t          | dd         |dd                   }| d         |d         g}||z   }t          |           dk    r|d= t          |          dk    r|d= t          |          S )	zCompute the output shape of a `matmul` operation.

    Args:
        shape1: Shape of the left operand.
        shape2: Shape of the right operand.

    Returns:
        Tuple of ints: The output shape for the `matmul` operation.
    r	   r   r-   NrD   z[Inner dimensions (`x1.shape[-1]` and `x2.shape[-2]`) must be equal, but received `x1.shape=z` and `x2.shape=r/   )r   r   r   r&   )r   r   leading_shapelast_2_dims_shaper   s        r   compute_matmul_output_shaperQ      s    6{{aVAY
6{{a)Qr
2J"2J&*$$$-3$ $$ $ $
 
 	
 %VCRC[&"+>>MVBZ0 #44L
6{{a
6{{ar   c           
      *   |                     d          }|dk    rt          d| d| d| d          d| v rt          d |D                       S t          j        |           }|d	k    r2|t          j        |          k    rt          d
|  d| d|           |S d}d}t          |          D ]\  }}|dk    r|}||z  }|d	k    s	||z  d	k    rt          d| d|  d| d|           t          |          }	||z  |	|<   t          |	          S )zConverts `-1` in `newshape` to either an actual dimension or `None`.

    This utility does not special case the 0th dimension (batch size).
    r-   r	   z4There must be at most one unknown dimension (-1) in z. Received: =r
   Nc              3   *   K   | ]}|d k    r|ndV  dS )r-   Nr   )r   dims     r   	<genexpr>z/compute_reshape_output_shape.<locals>.<genexpr>(  s.      DDCC2IISS4DDDDDDr   r   zFThe total size of the tensor must be unchanged. Received: input_shape=z, zyThe total size of the tensor must be unchanged, however, the input size cannot by divided by the specified dimensions in z. Received: input_shape=)countr   r&   mathprod	enumerater   )
r'   newshapenewshape_arg_nameunknown_dim_count
input_sizeknown_output_sizeunknown_dim_indexindexrU   r   s
             r   compute_reshape_output_shaperb     s   
 !r**1N N N.?N NBJN N N
 
 	
 {DD8DDDDDD;''JA8,,,,M*M M.?M MBJM M    )) % %
s"99 %$A.?!?1!D!D. . .:E. . !. . $,. .
 
 	
 >>L&04E&EL"#r   c                 ,    t                      |t           ddd                   S t          |          t                     k    r0t          dt                      dt          |           d          t           fd|D                       S )a  Compute the output shape for the `transpose` operation.

    Args:
        input_shape: Input shape.
        axes: Permutation of the dimensions for the `transpose` operation.

    Returns:
        Tuple of ints: The output shape after the `transpose` operation.
    Nr-   zDaxis must be a list of the same length as the input shape, expected z, but received r
   c              3   (   K   | ]}|         V  d S Nr   )r   r"   r'   s     r   rV   z1compute_transpose_output_shape.<locals>.<genexpr>]  s'      00RR000000r   )r   r&   r   r   )r'   axess   ` r   compute_transpose_output_shaperg   J  s     {##K|[2&'''
4yyC$$$$FK((F F9<TF F F
 
 	
 00004000000r   Fc                     t          |           } |*|rt          d | D                       S t          g           S |r|D ]}d| |<   t          |           S t          |d          D ]}| |= t          |           S )Nc                     g | ]}d S r    r   )r   _s     r   r   z reduce_shape.<locals>.<listcomp>d  s    +++!+++r   r	   T)reverse)r   r&   sorted)shaper#   keepdimsr"   s       r   reduce_shapero   `  s    KKE| 	++U+++,,,99  	 	BE"IIU||t,,, 	 	Bb		U||r   zkeras.utils.get_source_inputsc                 T   t          | d          s| S | j        \  }}}|r|j        s| gS |j        |         }|j        rt	          j        |j                  S g }|j        D ]F} t          |           }|D ]2t          fd|D                       r|
                               3G|S )zReturns the list of input tensors necessary to compute `tensor`.

    Output will always be a list of tensors
    (potentially with 1 element).

    Args:
        tensor: The tensor to start from.

    Returns:
        List of input tensors.
    _keras_historyc              3       K   | ]}|uV  	d S re   r   )r   txs     r   rV   z$get_source_inputs.<locals>.<genexpr>  s'      >>!1A:>>>>>>r   )hasattrrq   _inbound_nodesis_inputr   flattenoutput_tensorsinput_tensorsget_source_inputsallr8   )tensor	operation
node_indexrj   nodesource_tensorsprevious_sourcesrt   s          @r   r{   r{   r  s     6+,, %4Iz1 "I4 "x'
3= 	"< 3444N, 1 1#4V#<#< ) 1 1A>>>>~>>>>> 1&--a0001 "!r   )r*   r+   )r	   r*   r+   r	   )NF)rX   numpyr6   	keras.srcr   keras.src.api_exportr   &keras.src.backend.common.backend_utilsr   r   r   r)   rB   rM   rQ   rb   rg   ro   r{   r   r   r   <module>r      s=              - - - - - - D D D D D D C C C C C C) ) )X  6 V V V Vz M M M M`     F- - -`1 1 1,   $ -.." " /." " "r   