o
    2h                     @   s   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d	 Zed
G dd deZedG dd deZedG dd deZdS )    )backend)initializers)losses)ops)keras_export)Metric)serialization_libc                 C   s  |pt  }t | }tj| |d} |d uritj||d}|d ur+tjj|||dd}tj	| |\} }t
|j}t
| j}||krM|| tt||d} t|t| }| | } |dkri||ttd|d}t
| j}|dkr}|| ttd|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_ndim r"   ^/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/metrics/reduction_metrics.pyreduce_to_samplewise_values
   s8   



r$   zkeras.metrics.Sumc                       <   e Zd ZdZd fdd	ZdddZdd	 Zd
d Z  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
    r   Nc                    s.   t  j||d | jdt | jdd| _d S )Nnamer
   r"   totalr   initializerr
   r(   )super__init__add_variabler   Zerosr
   r)   selfr(   r
   	__class__r"   r#   r-   J   s   zSum.__init__c                 C   s.   t ||tj| jd\}}| jt| d S )Nr   r
   )r$   r   r   r
   r)   
assign_add)r1   r   r   _r"   r"   r#   update_stateS   s   
zSum.update_statec                 C   s   | j d d S Nr   )r)   assignr1   r"   r"   r#   reset_stateY      zSum.reset_statec                 C   s   t | j| jS N)r   r   r)   r
   r:   r"   r"   r#   result\   r<   z
Sum.result)r   Nr=   	__name__
__module____qualname____doc__r-   r7   r;   r>   __classcell__r"   r"   r2   r#   r&   /   s    
	r&   zkeras.metrics.Meanc                       r%   )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
    meanNc                    sH   t  j||d | jdt | jdd| _| jdt | jdd| _d S )Nr'   r"   r)   r*   count)r,   r-   r.   r   r/   r
   r)   rG   r0   r2   r"   r#   r-   {   s   zMean.__init__c                 C   s|   t ||tj| jd\}}| jt| |d urt|}nt|jdkr.t|d }nd}| j	tj
|| jd d S )Nr4   r   r   r	   )r$   r   rF   r
   r)   r5   r   r   r   rG   r   )r1   r   r   num_samplesr"   r"   r#   r7      s   
zMean.update_statec                 C   s   | j d | jd d S r8   )r)   r9   rG   r:   r"   r"   r#   r;      s   zMean.reset_statec                 C   s   t | jt j| j| jdS )Nr	   )r   divide_no_nanr)   r   rG   r
   r:   r"   r"   r#   r>      s   zMean.result)rF   Nr=   r?   r"   r"   r2   r#   rE   `   s    
rE   zkeras.metrics.MeanMetricWrapperc                       sH   e Zd ZdZd fdd	Zd fdd	Z fddZed	d
 Z  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`.
    Nc                    sT   t  j||d || _|| _| jtjv s!t| jdr&| jjtjv r(d| _d S d S d S )Nr'   r3   down)	r,   r-   _fn
_fn_kwargsr   ALL_OBJECTShasattrr3   
_direction)r1   fnr(   r
   kwargsr2   r"   r#   r-      s   

zMeanMetricWrapper.__init__c                    sV   t |}| j||fi | j}|d ur#|d ur#tjj||| jdd}t j	||dS )Nr   r   )r   )
r   r   rL   rM   r   r   r   r
   r,   r7   )r1   y_truey_predr   r   r   r2   r"   r#   r7      s   

zMeanMetricWrapper.update_statec                    s8   t   }dt| ji}|t| j i ||S )NrQ   )r,   
get_configr   serialize_keras_objectrL   updaterM   )r1   base_configconfigr2   r"   r#   rU      s   
zMeanMetricWrapper.get_configc                 C   s    d|v r	t |}| di |S )NrQ   r"   )r   deserialize_keras_object)clsrY   r"   r"   r#   from_config   s   
zMeanMetricWrapper.from_config)NNr=   )
r@   rA   rB   rC   r-   r7   rU   classmethodr\   rD   r"   r"   r2   r#   rJ      s    	rJ   N)	keras.srcr   r   r   r   keras.src.api_exportr   keras.src.metrics.metricr   keras.src.savingr   r$   r&   rE   rJ   r"   r"   r"   r#   <module>   s    %0@