
    ~Wh4                        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 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lmZ  ed           G d dej                              Z ed           G d dej                              Z ed           G d dej                              Z ed           G d dej                              Z ed           G d dej                              Z ed           G d d ej                              Z  ed!           G d" d#ej                              Z! ed$           G d% d&ej                              Z"d)d(Z#dS )*z%Regression metrics, e.g. MAE/MSE/etc.    N)backend)utils)logcosh)mean_absolute_error)mean_absolute_percentage_error)mean_squared_error)mean_squared_logarithmic_error)base_metric)losses_utils)metrics_utils)is_tensor_or_variable)keras_exportzkeras.metrics.MeanRelativeErrorc                   T     e Zd ZdZej        d fd	            Zd fd	Z fdZ xZ	S )MeanRelativeErrora  Computes the mean relative error by normalizing with the given values.

    This metric creates two local variables, `total` and `count` that are used
    to compute the mean relative error. This is weighted by `sample_weight`, and
    it is ultimately returned as `mean_relative_error`: an idempotent operation
    that simply divides `total` by `count`.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      normalizer: The normalizer values with same shape as predictions.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.MeanRelativeError(normalizer=[1, 3, 2, 3])
    >>> m.update_state([1, 3, 2, 3], [2, 4, 6, 8])

    >>> # metric = mean(|y_pred - y_true| / normalizer)
    >>> #        = mean([1, 1, 4, 5] / [1, 3, 2, 3]) = mean([1, 1/3, 2, 5/3])
    >>> #        = 5/4 = 1.25
    >>> m.result().numpy()
    1.25

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.MeanRelativeError(normalizer=[1, 3])])
    ```
    Nc                     t                                          ||           t          j        || j                  }|| _        d S )N)namedtype)super__init__tfcast_dtype
normalizer)selfr   r   r   	__class__s       f/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/keras/metrics/regression_metrics.pyr   zMeanRelativeError.__init__I   s>    d%000WZ55
$    c                    t          j        || j                  }t          j        || j                  }t          j        ||g|          \  \  }}}t          j        ||          \  }}t          j        || j                  \  }| _        |j	        
                    |j	                   t           j                            t          j        ||z
            | j                  }t                                          ||          S )a  Accumulates metric statistics.

        Args:
          y_true: The ground truth values.
          y_pred: The predicted values.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        sample_weight)r   r   r   r   ,ragged_assert_compatible_and_get_flat_valuesr   squeeze_or_expand_dimensionsremove_squeezable_dimensionsr   shapeassert_is_compatible_withmathdivide_no_nanabsr   update_state)r   y_truey_predr    relative_errorsr   s        r   r)   zMeanRelativeError.update_stateO   s    ---- )UVm
 
	 	
 &BF
 
 #/"KDO#
 #
 	..v|<<<'//F6F?##T_
 
 ww##= $ 
 
 	
r   c                 @   | j         }dt          |          rt          j        |          n|i}t	                                                      }t          t          |                                          t          |                                          z             S )Nr   )	r   r   r   evalr   
get_configdictlistitems)r   nconfigbase_configr   s       r   r/   zMeanRelativeError.get_configt   s~    O-B1-E-EL',q///1
 gg((**D**,,--V\\^^0D0DDEEEr   )NNN)
__name__
__module____qualname____doc__dtensor_utilsinject_meshr   r)   r/   __classcell__r   s   @r   r   r   #   s        " "H % % % % % %
#
 #
 #
 #
 #
 #
JF F F F F F F F Fr   r   zkeras.metrics.CosineSimilarityc                   >     e Zd ZdZej        d fd	            Z xZS )CosineSimilaritya  Computes the cosine similarity between the labels and predictions.

    `cosine similarity = (a . b) / ||a|| ||b||`

    See: [Cosine Similarity](https://en.wikipedia.org/wiki/Cosine_similarity).

    This metric keeps the average cosine similarity between `predictions` and
    `labels` over a stream of data.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      axis: (Optional) Defaults to -1. The dimension along which the cosine
        similarity is computed.

    Standalone usage:

    >>> # l2_norm(y_true) = [[0., 1.], [1./1.414, 1./1.414]]
    >>> # l2_norm(y_pred) = [[1., 0.], [1./1.414, 1./1.414]]
    >>> # l2_norm(y_true) . l2_norm(y_pred) = [[0., 0.], [0.5, 0.5]]
    >>> # result = mean(sum(l2_norm(y_true) . l2_norm(y_pred), axis=1))
    >>> #        = ((0. + 0.) +  (0.5 + 0.5)) / 2
    >>> m = tf.keras.metrics.CosineSimilarity(axis=1)
    >>> m.update_state([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]])
    >>> m.result().numpy()
    0.49999997

    >>> m.reset_state()
    >>> m.update_state([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]],
    ...                sample_weight=[0.3, 0.7])
    >>> m.result().numpy()
    0.6999999

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.CosineSimilarity(axis=1)])
    ```
    cosine_similarityNc                 \    t                                          t          |||           d S )N)r   axis)r   r   rA   )r   r   r   rD   r   s       r   r   zCosineSimilarity.__init__   s+    *DDIIIIIr   )rA   NrB   r7   r8   r9   r:   r;   r<   r   r=   r>   s   @r   r@   r@   }   sb        ) )V J J J J J J J J J Jr   r@   zkeras.metrics.MeanAbsoluteErrorc                   >     e Zd ZdZej        d fd	            Z xZS )MeanAbsoluteErrora  Computes the mean absolute error between the labels and predictions.

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

    Standalone usage:

    >>> m = tf.keras.metrics.MeanAbsoluteError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.25

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.5

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.MeanAbsoluteError()])
    ```
    r   Nc                 Z    t                                          t          ||           d S N)r   )r   r   r   r   r   r   r   s      r   r   zMeanAbsoluteError.__init__   s)    ,d%@@@@@r   )r   NrE   r>   s   @r   rG   rG      sa         : A A A A A A A A A Ar   rG   z)keras.metrics.MeanAbsolutePercentageErrorc                   >     e Zd ZdZej        d fd	            Z xZS )MeanAbsolutePercentageErrora  Computes the mean absolute percentage error between `y_true` and
    `y_pred`.

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

    Standalone usage:

    >>> m = tf.keras.metrics.MeanAbsolutePercentageError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    250000000.0

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    500000000.0

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.MeanAbsolutePercentageError()])
    ```
    r   Nc                 Z    t                                          t          ||           d S rI   )r   r   r   rJ   s      r   r   z$MeanAbsolutePercentageError.__init__   )    7UKKKKKr   )r   NrE   r>   s   @r   rL   rL      a         < L L L L L L L L L Lr   rL   zkeras.metrics.MeanSquaredErrorc                   >     e Zd ZdZej        d fd	            Z xZS )MeanSquaredErrora  Computes the mean squared error between `y_true` and `y_pred`.

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

    Standalone usage:

    >>> m = tf.keras.metrics.MeanSquaredError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.25

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.5

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.MeanSquaredError()])
    ```
    r   Nc                 Z    t                                          t          ||           d S rI   )r   r   r   rJ   s      r   r   zMeanSquaredError.__init__  s)    +T?????r   )r   NrE   r>   s   @r   rQ   rQ      sa         : @ @ @ @ @ @ @ @ @ @r   rQ   z)keras.metrics.MeanSquaredLogarithmicErrorc                   >     e Zd ZdZej        d fd	            Z xZS )MeanSquaredLogarithmicErrora  Computes the mean squared logarithmic error between `y_true` and
    `y_pred`.

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

    Standalone usage:

    >>> m = tf.keras.metrics.MeanSquaredLogarithmicError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.12011322

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.24022643

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.MeanSquaredLogarithmicError()])
    ```
    r	   Nc                 Z    t                                          t          ||           d S rI   )r   r   r	   rJ   s      r   r   z$MeanSquaredLogarithmicError.__init__<  rN   r   )r	   NrE   r>   s   @r   rT   rT     rO   r   rT   z"keras.metrics.RootMeanSquaredErrorc                   P     e Zd ZdZej        d fd	            Zd fd	Zd Z xZ	S )	RootMeanSquaredErroraS  Computes root mean squared error metric between `y_true` and `y_pred`.

    Standalone usage:

    >>> m = tf.keras.metrics.RootMeanSquaredError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.70710677

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.RootMeanSquaredError()])
    ```
    root_mean_squared_errorNc                 N    t                                          ||           d S rI   )r   r   rJ   s      r   r   zRootMeanSquaredError.__init__\  s&    U+++++r   c                 "   t          j        || j                  }t          j        || j                  }t          j        ||          \  }}t           j                            ||          }t                                          ||          S )a  Accumulates root mean squared error statistics.

        Args:
          y_true: The ground truth values.
          y_pred: The predicted values.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        r   )	r   r   r   r   r"   r&   squared_differencer   r)   )r   r*   r+   r    error_sqr   s        r   r)   z!RootMeanSquaredError.update_state`  s|     ----%BF
 
 7--ff==ww##HM#JJJr   c                 z    t          j        t           j                            | j        | j                            S r6   )r   sqrtr&   r'   totalcount)r   s    r   resultzRootMeanSquaredError.resultu  s(    wrw,,TZDDEEEr   )rX   Nr6   )
r7   r8   r9   r:   r;   r<   r   r)   ra   r=   r>   s   @r   rW   rW   A  s         2 , , , , , ,K K K K K K*F F F F F F Fr   rW   zkeras.metrics.LogCoshErrorc                   >     e Zd ZdZej        d fd	            Z xZS )LogCoshErrora,  Computes the logarithm of the hyperbolic cosine of the prediction error.

    `logcosh = log((exp(x) + exp(-x))/2)`, where x is the error (y_pred -
    y_true)

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

    Standalone usage:

    >>> m = tf.keras.metrics.LogCoshError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.10844523

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.21689045

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.LogCoshError()])
    ```
    r   Nc                 Z    t                                          t          ||           d S rI   )r   r   r   rJ   s      r   r   zLogCoshError.__init__  s(    $e44444r   )r   NrE   r>   s   @r   rc   rc   y  sW         > 5 5 5 5 5 5 5 5 5 5r   rc   rB   c                     t           j                            | |          } t           j                            ||          }t          j        | |z  |          S )a3  Computes the cosine similarity between labels and predictions.

    Args:
      y_true: The ground truth values.
      y_pred: The prediction values.
      axis: (Optional) Defaults to -1. The dimension along which the cosine
        similarity is computed.

    Returns:
      Cosine similarity value.
    )rD   )r   linalgl2_normalize
reduce_sum)r*   r+   rD   s      r   rA   rA     sQ     Y##F#66FY##F#66F=&t4444r   )rB   )$r:   tensorflow.compat.v2compatv2r   kerasr   keras.dtensorr   r;   keras.lossesr   r   r   r   r	   keras.metricsr
   keras.utilsr   r   keras.utils.tf_utilsr    tensorflow.python.util.tf_exportr   Meanr   MeanMetricWrapperr@   rG   rL   rQ   rT   rW   rc   rA    r   r   <module>rv      s   , + ! ! ! ! ! ! ! ! !       0 0 0 0 0 0             , , , , , , 7 7 7 7 7 7 + + + + + + 7 7 7 7 7 7 % % % % % % $ $ $ $ $ $ % % % % % % 6 6 6 6 6 6 : 9 9 9 9 9 /00VF VF VF VF VF( VF VF 10VFr .//.J .J .J .J .J{4 .J .J 0/.Jb /00 A  A  A  A  A5  A  A 10 AF 9::!L !L !L !L !L+"? !L !L ;:!LH .// @  @  @  @  @{4  @  @ 0/ @F 9::!L !L !L !L !L+"? !L !L ;:!LH 2334F 4F 4F 4F 4F;+ 4F 4F 434Fn *++"5 "5 "5 "5 "5;0 "5 "5 ,+"5J5 5 5 5 5 5r   