
    2Vh                         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 d dl	m
Z
 d Z ed	       G d
 de             Z ed       G d de             Z ed       G d de             Zy)    )backend)initializers)losses)ops)keras_export)Metric)serialization_libc           	         |xs t        j                         }t        j                  |       }t        j                  | |      } |t        j
                  ||      }|#t        j                  j                  |||d      }t        j                  j                  | |      \  } }t        |j                        }t        | j                        }||kD  r || t        t        ||                  } t        j                  |t        j                  |             }| |z  } |dkD  r ||t        t        d|                  }t        | j                        }|dkD  r || t        t        d|                  } | |fS )Ndtypesumr   	reduction)axis   )r   floatxget_keras_maskr   castconvert_to_tensorr   loss
apply_masksqueeze_or_expand_to_same_ranklenshapelistrangebroadcast_to)valuessample_weight	reduce_fnr   maskweight_ndimvalues_ndims          S/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/metrics/reduction_metrics.pyreduce_to_samplewise_valuesr%   
   sY   %W^^%E!!&)DXXfE*F --m5I"KK22t5E 3 M !' J JM!
 ---.&,,'$T%["ABF
 ((		&8IJ-'?%Dq+)>$?M fll#KQ6U1k-B(CD=      zkeras.metrics.Sumc                   8     e Zd ZdZd fd	ZddZd Zd Z xZS )Suma  Compute the (weighted) sum of the given values.

    For example, if `values` is `[1, 3, 5, 7]` then their sum is 16.
    If `sample_weight` was specified as `[1, 1, 0, 0]` then the sum would be 4.

    This metric creates one variable, `total`.
    This is ultimately returned as the sum value.

    Args:
        name: (Optional) string name of the metric instance.
        dtype: (Optional) data type of the metric result.

    Example:

    >>> m = metrics.Sum()
    >>> m.update_state([1, 3, 5, 7])
    >>> m.result()
    16.0

    >>> m = metrics.Sum()
    >>> m.update_state([1, 3, 5, 7], sample_weight=[1, 1, 0, 0])
    >>> m.result()
    4.0
    c                     t         |   ||       | j                  dt        j                         | j
                  d      | _        y )Nnamer    totalr   initializerr   r+   )super__init__add_variabler   Zerosr   r-   selfr+   r   	__class__s      r$   r1   zSum.__init__J   sE    d%0&&$**,**	 ' 

r&   c                     t        ||t        j                  | j                        \  }}| j                  j                  t        j                  |             y )Nr    r   )r%   r   r   r   r-   
assign_add)r5   r   r   _s       r$   update_statezSum.update_stateS   s=    /MSWWDJJ
	 	

cggfo.r&   c                 :    | j                   j                  d       y Nr   )r-   assignr5   s    r$   reset_statezSum.reset_stateY   s    

!r&   c                 V    t        j                  | j                  | j                        S N)r   r   r-   r   r?   s    r$   resultz
Sum.result\   s    xx

DJJ//r&   )r   NrB   	__name__
__module____qualname____doc__r1   r;   r@   rC   __classcell__r6   s   @r$   r(   r(   /   s    2
/0r&   r(   zkeras.metrics.Meanc                   8     e Zd ZdZd fd	ZddZd Zd Z xZS )Meana  Compute the (weighted) mean of the given values.

    For example, if values is `[1, 3, 5, 7]` then the mean is 4.
    If `sample_weight` was specified as `[1, 1, 0, 0]` then the mean would be 2.

    This metric creates two variables, `total` and `count`.
    The mean value returned is simply `total` divided by `count`.

    Args:
        name: (Optional) string name of the metric instance.
        dtype: (Optional) data type of the metric result.

    Example:

    >>> m = Mean()
    >>> m.update_state([1, 3, 5, 7])
    >>> m.result()
    4.0

    >>> m.reset_state()
    >>> m.update_state([1, 3, 5, 7], sample_weight=[1, 1, 0, 0])
    >>> m.result()
    2.0
    c                     t         |   ||       | j                  dt        j                         | j
                  d      | _        | j                  dt        j                         | j
                  d      | _        y )Nr*   r,   r-   r.   count)r0   r1   r2   r   r3   r   r-   rN   r4   s      r$   r1   zMean.__init__{   sw    d%0&&$**,**	 ' 

 &&$**,**	 ' 

r&   c                    t        ||t        j                  | j                        \  }}| j                  j                  t        j                  |             |t        j                  |      }n3t        |j                        dk\  rt        j                  |      d   }nd}| j                  j                  t        j                  || j                               y )Nr8   r   r   r   )r%   r   meanr   r-   r9   r   r   r   rN   r   )r5   r   r   num_sampless       r$   r;   zMean.update_state   s     ;MSXXTZZ!
 	

cggfo.$''-0K!#))F+A.KK

chh{$**EFr&   c                 p    | j                   j                  d       | j                  j                  d       y r=   )r-   r>   rN   r?   s    r$   r@   zMean.reset_state   s&    

!

!r&   c                     t        j                  | j                  t        j                  | j                  | j
                              S )Nr   )r   divide_no_nanr-   r   rN   r   r?   s    r$   rC   zMean.result   s1      JJ4::>
 	
r&   )rP   NrB   rD   rJ   s   @r$   rL   rL   `   s    2
G
r&   rL   zkeras.metrics.MeanMetricWrapperc                   J     e Zd ZdZd fd	Zd fd	Z fdZed        Z xZ	S )MeanMetricWrappera  Wrap a stateless metric function with the `Mean` metric.

    You could use this class to quickly build a mean metric from a function. The
    function needs to have the signature `fn(y_true, y_pred)` and return a
    per-sample loss array. `MeanMetricWrapper.result()` will return
    the average metric value across all samples seen so far.

    For example:

    ```python
    def mse(y_true, y_pred):
        return (y_true - y_pred) ** 2

    mse_metric = MeanMetricWrapper(fn=mse)
    ```

    Args:
        fn: The metric function to wrap, with signature
            `fn(y_true, y_pred, **kwargs)`.
        name: (Optional) string name of the metric instance.
        dtype: (Optional) data type of the metric result.
        **kwargs: Keyword arguments to pass on to `fn`.
    c                    t         |   ||       || _        || _        | j                  t        j
                  v s<t        | j                  d      r/| j                  j                  t        j
                  v rd| _        y y y )Nr*   r6   down)	r0   r1   _fn
_fn_kwargsr   ALL_OBJECTShasattrr6   
_direction)r5   fnr+   r   kwargsr6   s        r$   r1   zMeanMetricWrapper.__init__   sn    d%0 
 HH***txx-""f&8&88$DO 9 .r&   c                     t        j                  |      } | j                  ||fi | j                  }|/|-t        j
                  j                  ||| j                  d      }t        | %  ||      S )Nr   r   )r   )
r   r   rY   rZ   r   r   r   r   r0   r;   )r5   y_truey_predr   r!   r   r6   s         r$   r;   zMeanMetricWrapper.update_state   sv    %%f-&&<DOO<$)9"KK22t4:: 3 M w#F-#HHr&   c                     t         |          }dt        j                  | j                        i}|j                  t        j                  | j                               i ||S )Nr^   )r0   
get_configr	   serialize_keras_objectrY   updaterZ   )r5   base_configconfigr6   s      r$   rd   zMeanMetricWrapper.get_config   sV    g(*)@@JK'>>tOP(+(((r&   c                 D    d|v rt        j                  |      } | di |S )Nr^   r,   )r	   deserialize_keras_object)clsrh   s     r$   from_configzMeanMetricWrapper.from_config   s%    6>&??GF}V}r&   )NNrB   )
rE   rF   rG   rH   r1   r;   rd   classmethodrl   rI   rJ   s   @r$   rV   rV      s,    0%I)  r&   rV   N)	keras.srcr   r   r   r   keras.src.api_exportr   keras.src.metrics.metricr   keras.src.savingr	   r%   r(   rL   rV   r,   r&   r$   <module>rr      s     "   - + ."!J !"-0& -0 #-0` "#=
6 =
 $=
@ /0: : 1:r&   