
    &Vf                       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 dlmZ d dl	m
Z
 d dlmZ  G d	 d
e          Z ed           G d de                      Z ed           G d de                      Z ed           G d de                      Z ed           G d de                      Z ed           G d de                      Z ed           G d de                      Z ed           G d de                      Z ed            G d! d"e                      Z ed#           G d$ d%e                      Z ed&           G d' d(e                      Z ed)           G d* d+e                      Z ed,           G d- d.e                      Z ed/           G d0 d1e                      Z ed2           G d3 d4e                      Z ed5           G d6 d7e                      Z ed8           G d9 d:e                      Z ed;           G d< d=e                      Zd> Z ed?d@g          dA             Z  edBdCg          dD             Z! edEdFg          dG             Z" eg dH          dI             Z# eg dJ          dK             Z$ eg dL          dM             Z% eg dN          dO             Z& edP          ddR            Z' edSdTg          ddV            Z( eg dW          dX             Z) eg dY          dZ             Z* ed[d\g          d]             Z+ ed^d_g          	 ddb            Z, edcddg          	 	 	 	 	 ddg            Z- edhdig          	 ddj            Z. edkdlg          	 ddm            Z/ edndog          	 	 	 	 	 	 ddp            Z0 edq           G dr dse                      Z1 edt          du             Z2 edv           G dw dxe                      Z3 edy          dz             Z4 ed{           G d| d}e                      Z5 ed~          dd            Z6dS )    N)backend)ops)keras_export)Loss)squeeze_or_expand_to_same_rank)serialization_lib)	normalizec                   H     e Zd Z	 d fd	Zd Z fdZed             Z xZS )LossFunctionWrappersum_over_batch_sizeNc                 j    t                                          ||           || _        || _        d S N)	reductionname)super__init__fn
_fn_kwargs)selfr   r   r   kwargs	__class__s        T/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/losses/losses.pyr   zLossFunctionWrapper.__init__   s4     	94888     c                 P    t          ||          \  }} | j        ||fi | j        S N)r   r   r   )r   y_truey_preds      r   callzLossFunctionWrapper.call   s3    7GGtwvv99999r   c                     t                                                      }dt          j        | j                  i}|                    t          j        | j                             i ||S )Nr   )r   
get_configr   serialize_keras_objectr   updater   )r   base_configconfigr   s      r   r    zLossFunctionWrapper.get_config   s`    gg((**)@IIJ'>tOOPPP(+(((r   c                 B    d|v rt          j        |          } | di |S )Nr    )r   deserialize_keras_object)clsr$   s     r   from_configzLossFunctionWrapper.from_config   s-    6>>&?GGFs}}V}}r   )r   N)	__name__
__module____qualname__r   r   r    classmethodr)   __classcell__r   s   @r   r   r      s        8<! ! ! ! ! !: : :) ) ) ) )   [    r   r   zkeras.losses.MeanSquaredErrorc                   ,     e Zd ZdZ	 d fd	Zd Z xZS )MeanSquaredErrora  Computes the mean of squares of errors between labels and predictions.

    Formula:

    ```python
    loss = mean(square(y_true - y_pred))
    ```

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   mean_squared_errorc                 Z    t                                          t          ||           d S r   )r   r   r2   r   r   r   r   s      r   r   zMeanSquaredError.__init__6   s+     	+ytLLLLLr   c                 *    t          j        |           S r   r   r    r   s    r   r    zMeanSquaredError.get_config;       t$$$r   )r   r2   r*   r+   r,   __doc__r   r    r.   r/   s   @r   r1   r1   %   sb           5IM M M M M M
% % % % % % %r   r1   zkeras.losses.MeanAbsoluteErrorc                   ,     e Zd ZdZ	 d fd	Zd Z xZS )MeanAbsoluteErrora  Computes the mean of absolute difference between labels and predictions.

    Formula:

    ```python
    loss = mean(abs(y_true - y_pred))
    ```

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   mean_absolute_errorc                 Z    t                                          t          ||           d S r   )r   r   r=   r4   s      r   r   zMeanAbsoluteError.__init__P   s+     	,	MMMMMr   c                 *    t          j        |           S r   r6   r7   s    r   r    zMeanAbsoluteError.get_configU   r8   r   )r   r=   r9   r/   s   @r   r<   r<   ?   sb           5JN N N N N N
% % % % % % %r   r<   z(keras.losses.MeanAbsolutePercentageErrorc                   .     e Zd ZdZ	 	 d fd	Zd Z xZS )MeanAbsolutePercentageErrora  Computes the mean absolute percentage error between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = 100 * mean(abs((y_true - y_pred) / y_true))
    ```

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   mean_absolute_percentage_errorc                 Z    t                                          t          ||           d S r   )r   r   rB   r4   s      r   r   z$MeanAbsolutePercentageError.__init__j   7    
 	*id 	 	
 	
 	
 	
 	
r   c                 *    t          j        |           S r   r6   r7   s    r   r    z&MeanAbsolutePercentageError.get_configs   r8   r   )r   rB   r9   r/   s   @r   rA   rA   Y   ^         " (-
 
 
 
 
 
% % % % % % %r   rA   z(keras.losses.MeanSquaredLogarithmicErrorc                   .     e Zd ZdZ	 	 d fd	Zd Z xZS )MeanSquaredLogarithmicErrora  Computes the mean squared logarithmic error between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = mean(square(log(y_true + 1) - log(y_pred + 1)))
    ```

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   mean_squared_logarithmic_errorc                 Z    t                                          t          ||           d S r   )r   r   rI   r4   s      r   r   z$MeanSquaredLogarithmicError.__init__   rD   r   c                 *    t          j        |           S r   r6   r7   s    r   r    z&MeanSquaredLogarithmicError.get_config   r8   r   )r   rI   r9   r/   s   @r   rH   rH   w   rF   r   rH   zkeras.losses.CosineSimilarityc                   0     e Zd ZdZ	 	 	 d fd	Zd Z xZS )CosineSimilaritya  Computes the cosine similarity between `y_true` & `y_pred`.

    Note that it is a number between -1 and 1. When it is a negative number
    between -1 and 0, 0 indicates orthogonality and values closer to -1
    indicate greater similarity. This makes it usable as a loss function in a
    setting where you try to maximize the proximity between predictions and
    targets. If either `y_true` or `y_pred` is a zero vector, cosine similarity
    will be 0 regardless of the proximity between predictions and targets.

    Formula:

    ```python
    loss = -sum(l2_norm(y_true) * l2_norm(y_pred))
    ```

    Args:
        axis: The axis along which the cosine similarity is computed
            (the features axis). Defaults to `-1`.
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   cosine_similarityc                 \    t                                          t          |||           d S )N)r   r   axis)r   r   rO   )r   rQ   r   r   r   s       r   r   zCosineSimilarity.__init__   s9     	D 	 	
 	
 	
 	
 	
r   c                 *    t          j        |           S r   r6   r7   s    r   r    zCosineSimilarity.get_config   r8   r   )rN   r   rO   r9   r/   s   @r   rM   rM      sa         4 ' 	
 
 
 
 
 
% % % % % % %r   rM   zkeras.losses.Huberc                   0     e Zd ZdZ	 	 	 d fd	Zd Z xZS )Hubera  Computes the Huber loss between `y_true` & `y_pred`.

    Formula:

    ```python
    for x in error:
        if abs(x) <= delta:
            loss.append(0.5 * x^2)
        elif abs(x) > delta:
            loss.append(delta * abs(x) - 0.5 * delta^2)

    loss = mean(loss, axis=-1)
    ```
    See: [Huber loss](https://en.wikipedia.org/wiki/Huber_loss).

    Args:
        delta: A float, the point where the Huber loss function changes from a
            quadratic to linear.
        reduction: Type of reduction to apply to loss. Options are `"sum"`,
            `"sum_over_batch_size"` or `None`. Defaults to
            `"sum_over_batch_size"`.
        name: Optional name for the instance.
          ?r   
huber_lossc                 \    t                                          t          |||           d S )N)r   r   delta)r   r   huber)r   rX   r   r   r   s       r   r   zHuber.__init__   s,     	TYeLLLLLr   c                 *    t          j        |           S r   r6   r7   s    r   r    zHuber.get_config   r8   r   )rU   r   rV   r9   r/   s   @r   rT   rT      sg         4 '	M M M M M M% % % % % % %r   rT   zkeras.losses.LogCoshc                   *     e Zd ZdZd fd	Zd Z xZS )LogCosha  Computes the logarithm of the hyperbolic cosine of the prediction error.

    Formula:

    ```python
    error = y_pred - y_true
    logcosh = mean(log((exp(error) + exp(-error))/2), axis=-1)`
    ```
    where x is the error `y_pred - y_true`.

    Args:
        reduction: Type of reduction to apply to loss. Options are `"sum"`,
            `"sum_over_batch_size"` or `None`. Defaults to
            `"sum_over_batch_size"`.
        name: Optional name for the instance.
    r   log_coshc                 Z    t                                          t          ||           d S Nr   r   )r   r   r]   r4   s      r   r   zLogCosh.__init__   s(    	BBBBBr   c                 *    t          j        |           S r   r6   r7   s    r   r    zLogCosh.get_config   r8   r   )r   r]   r9   r/   s   @r   r\   r\      s\         "C C C C C C% % % % % % %r   r\   zkeras.losses.Hingec                   *     e Zd ZdZd fd	Zd Z xZS )Hingea  Computes the hinge loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = maximum(1 - y_true * y_pred, 0)
    ```

    `y_true` values are expected to be -1 or 1. If binary (0 or 1) labels are
    provided we will convert them to -1 or 1.

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   hingec                 Z    t                                          t          ||           d S r   )r   r   rd   r4   s      r   r   zHinge.__init__  s(    )$?????r   c                 *    t          j        |           S r   r6   r7   s    r   r    zHinge.get_config  r8   r   )r   rd   r9   r/   s   @r   rc   rc      s\         $@ @ @ @ @ @% % % % % % %r   rc   zkeras.losses.SquaredHingec                   *     e Zd ZdZd fd	Zd Z xZS )SquaredHingea%  Computes the squared hinge loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = square(maximum(1 - y_true * y_pred, 0))
    ```

    `y_true` values are expected to be -1 or 1. If binary (0 or 1) labels are
    provided we will convert them to -1 or 1.

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   squared_hingec                 Z    t                                          t          ||           d S r   )r   r   ri   r4   s      r   r   zSquaredHinge.__init__,  (    )$GGGGGr   c                 *    t          j        |           S r   r6   r7   s    r   r    zSquaredHinge.get_config/  r8   r   )r   ri   r9   r/   s   @r   rh   rh     s\         $H H H H H H% % % % % % %r   rh   zkeras.losses.CategoricalHingec                   ,     e Zd ZdZ	 d fd	Zd Z xZS )CategoricalHingea  Computes the categorical hinge loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = maximum(neg - pos + 1, 0)
    ```

    where `neg=maximum((1-y_true)*y_pred)` and `pos=sum(y_true*y_pred)`

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   categorical_hingec                 Z    t                                          t          ||           d S r   )r   r   ro   r4   s      r   r   zCategoricalHinge.__init__F  s+     	*idKKKKKr   c                 *    t          j        |           S r   r6   r7   s    r   r    zCategoricalHinge.get_configK  r8   r   )r   ro   r9   r/   s   @r   rn   rn   3  sb         $ 5HL L L L L L
% % % % % % %r   rn   zkeras.losses.KLDivergencec                   *     e Zd ZdZd fd	Zd Z xZS )KLDivergenceaC  Computes Kullback-Leibler divergence loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = y_true * log(y_true / y_pred)
    ```

    `y_true` and `y_pred` are expected to be probability
    distributions, with values between 0 and 1. They will get
    clipped to the `[0, 1]` range.

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   kl_divergencec                 Z    t                                          t          ||           d S r   )r   r   rt   r4   s      r   r   zKLDivergence.__init__d  rk   r   c                 *    t          j        |           S r   r6   r7   s    r   r    zKLDivergence.get_configg  r8   r   )r   rt   r9   r/   s   @r   rs   rs   O  s\         &H H H H H H% % % % % % %r   rs   zkeras.losses.Poissonc                   *     e Zd ZdZd fd	Zd Z xZS )Poissona  Computes the Poisson loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = y_pred - y_true * log(y_pred)
    ```

    Args:
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.
    r   poissonc                 Z    t                                          t          ||           d S r   )r   r   ry   r4   s      r   r   zPoisson.__init__|  s(    IDAAAAAr   c                 *    t          j        |           S r   r6   r7   s    r   r    zPoisson.get_config  r8   r   )r   ry   r9   r/   s   @r   rx   rx   k  s\         B B B B B B% % % % % % %r   rx   zkeras.losses.BinaryCrossentropyc                   4     e Zd ZdZ	 	 	 	 	 d	 fd	Zd Z xZS )
BinaryCrossentropya{  Computes the cross-entropy loss between true labels and predicted labels.

    Use this cross-entropy loss for binary (0 or 1) classification applications.
    The loss function requires the following inputs:

    - `y_true` (true label): This is either 0 or 1.
    - `y_pred` (predicted value): This is the model's prediction, i.e, a single
        floating-point value which either represents a
        [logit](https://en.wikipedia.org/wiki/Logit), (i.e, value in [-inf, inf]
        when `from_logits=True`) or a probability (i.e, value in [0., 1.] when
        `from_logits=False`).

    Args:
        from_logits: Whether to interpret `y_pred` as a tensor of
            [logit](https://en.wikipedia.org/wiki/Logit) values. By default, we
            assume that `y_pred` is probabilities (i.e., values in [0, 1]).
        label_smoothing: Float in range [0, 1]. When 0, no smoothing occurs.
            When > 0, we compute the loss between the predicted labels
            and a smoothed version of the true labels, where the smoothing
            squeezes the labels towards 0.5. Larger values of
            `label_smoothing` correspond to heavier smoothing.
        axis: The axis along which to compute crossentropy (the features axis).
            Defaults to `-1`.
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.

    Examples:

    **Recommended Usage:** (set `from_logits=True`)

    With `compile()` API:

    ```python
    model.compile(
        loss=keras.losses.BinaryCrossentropy(from_logits=True),
        ...
    )
    ```

    As a standalone function:

    >>> # Example 1: (batch_size = 1, number of samples = 4)
    >>> y_true = [0, 1, 0, 0]
    >>> y_pred = [-18.6, 0.51, 2.94, -12.8]
    >>> bce = keras.losses.BinaryCrossentropy(from_logits=True)
    >>> bce(y_true, y_pred)
    0.865

    >>> # Example 2: (batch_size = 2, number of samples = 4)
    >>> y_true = [[0, 1], [0, 0]]
    >>> y_pred = [[-18.6, 0.51], [2.94, -12.8]]
    >>> # Using default 'auto'/'sum_over_batch_size' reduction type.
    >>> bce = keras.losses.BinaryCrossentropy(from_logits=True)
    >>> bce(y_true, y_pred)
    0.865
    >>> # Using 'sample_weight' attribute
    >>> bce(y_true, y_pred, sample_weight=[0.8, 0.2])
    0.243
    >>> # Using 'sum' reduction` type.
    >>> bce = keras.losses.BinaryCrossentropy(from_logits=True,
    ...     reduction="sum")
    >>> bce(y_true, y_pred)
    1.730
    >>> # Using 'none' reduction type.
    >>> bce = keras.losses.BinaryCrossentropy(from_logits=True,
    ...     reduction=None)
    >>> bce(y_true, y_pred)
    array([0.235, 1.496], dtype=float32)

    **Default Usage:** (set `from_logits=False`)

    >>> # Make the following updates to the above "Recommended Usage" section
    >>> # 1. Set `from_logits=False`
    >>> keras.losses.BinaryCrossentropy() # OR ...('from_logits=False')
    >>> # 2. Update `y_pred` to use probabilities instead of logits
    >>> y_pred = [0.6, 0.3, 0.2, 0.8] # OR [[0.6, 0.3], [0.2, 0.8]]
    F        rN   r   binary_crossentropyc                     t                                          t          |||||           || _        || _        || _        d S Nr   r   from_logitslabel_smoothingrQ   )r   r   r   r   r   rQ   r   r   r   rQ   r   r   r   s         r   r   zBinaryCrossentropy.__init__  sU     	#+ 	 	
 	
 	
 '.			r   c                 D    | j         | j        | j        | j        | j        dS r   r   r7   s    r   r    zBinaryCrossentropy.get_config  ,    I+#3I
 
 	
r   )Fr~   rN   r   r   r9   r/   s   @r   r}   r}     sj        N Nd '"     (
 
 
 
 
 
 
r   r}   z$keras.losses.BinaryFocalCrossentropyc                   :     e Zd ZdZ	 	 	 	 	 	 	 	 d fd		Zd
 Z xZS )BinaryFocalCrossentropya  Computes focal cross-entropy loss between true labels and predictions.

    Binary cross-entropy loss is often used for binary (0 or 1) classification
    tasks. The loss function requires the following inputs:

    - `y_true` (true label): This is either 0 or 1.
    - `y_pred` (predicted value): This is the model's prediction, i.e, a single
        floating-point value which either represents a
        [logit](https://en.wikipedia.org/wiki/Logit), (i.e, value in [-inf, inf]
        when `from_logits=True`) or a probability (i.e, value in `[0., 1.]` when
        `from_logits=False`).

    According to [Lin et al., 2018](https://arxiv.org/pdf/1708.02002.pdf), it
    helps to apply a "focal factor" to down-weight easy examples and focus more
    on hard examples. By default, the focal tensor is computed as follows:

    `focal_factor = (1 - output) ** gamma` for class 1
    `focal_factor = output ** gamma` for class 0
    where `gamma` is a focusing parameter. When `gamma=0`, this function is
    equivalent to the binary crossentropy loss.

    Args:
        apply_class_balancing: A bool, whether to apply weight balancing on the
            binary classes 0 and 1.
        alpha: A weight balancing factor for class 1, default is `0.25` as
            mentioned in reference [Lin et al., 2018](
            https://arxiv.org/pdf/1708.02002.pdf).  The weight for class 0 is
            `1.0 - alpha`.
        gamma: A focusing parameter used to compute the focal factor, default is
            `2.0` as mentioned in the reference
            [Lin et al., 2018](https://arxiv.org/pdf/1708.02002.pdf).
        from_logits: Whether to interpret `y_pred` as a tensor of
            [logit](https://en.wikipedia.org/wiki/Logit) values. By default, we
            assume that `y_pred` are probabilities (i.e., values in `[0, 1]`).
        label_smoothing: Float in `[0, 1]`. When `0`, no smoothing occurs.
            When > `0`, we compute the loss between the predicted labels
            and a smoothed version of the true labels, where the smoothing
            squeezes the labels towards `0.5`.
            Larger values of `label_smoothing` correspond to heavier smoothing.
        axis: The axis along which to compute crossentropy (the features axis).
            Defaults to `-1`.
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.

    Examples:

    With the `compile()` API:

    ```python
    model.compile(
        loss=keras.losses.BinaryFocalCrossentropy(
            gamma=2.0, from_logits=True),
        ...
    )
    ```

    As a standalone function:

    >>> # Example 1: (batch_size = 1, number of samples = 4)
    >>> y_true = [0, 1, 0, 0]
    >>> y_pred = [-18.6, 0.51, 2.94, -12.8]
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...    gamma=2, from_logits=True)
    >>> loss(y_true, y_pred)
    0.691

    >>> # Apply class weight
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...     apply_class_balancing=True, gamma=2, from_logits=True)
    >>> loss(y_true, y_pred)
    0.51

    >>> # Example 2: (batch_size = 2, number of samples = 4)
    >>> y_true = [[0, 1], [0, 0]]
    >>> y_pred = [[-18.6, 0.51], [2.94, -12.8]]
    >>> # Using default 'auto'/'sum_over_batch_size' reduction type.
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...     gamma=3, from_logits=True)
    >>> loss(y_true, y_pred)
    0.647

    >>> # Apply class weight
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...      apply_class_balancing=True, gamma=3, from_logits=True)
    >>> loss(y_true, y_pred)
    0.482

    >>> # Using 'sample_weight' attribute with focal effect
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...     gamma=3, from_logits=True)
    >>> loss(y_true, y_pred, sample_weight=[0.8, 0.2])
    0.133

    >>> # Apply class weight
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...      apply_class_balancing=True, gamma=3, from_logits=True)
    >>> loss(y_true, y_pred, sample_weight=[0.8, 0.2])
    0.097

    >>> # Using 'sum' reduction` type.
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...     gamma=4, from_logits=True,
    ...     reduction="sum")
    >>> loss(y_true, y_pred)
    1.222

    >>> # Apply class weight
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...     apply_class_balancing=True, gamma=4, from_logits=True,
    ...     reduction="sum")
    >>> loss(y_true, y_pred)
    0.914

    >>> # Using 'none' reduction type.
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...     gamma=5, from_logits=True,
    ...     reduction=None)
    >>> loss(y_true, y_pred)
    array([0.0017 1.1561], dtype=float32)

    >>> # Apply class weight
    >>> loss = keras.losses.BinaryFocalCrossentropy(
    ...     apply_class_balancing=True, gamma=5, from_logits=True,
    ...     reduction=None)
    >>> loss(y_true, y_pred)
    array([0.0004 0.8670], dtype=float32)
    F      ?       @r~   rN   r   binary_focal_crossentropyc	                     t                                          t          ||||||||	  	         || _        || _        || _        || _        || _        || _        d S )N)apply_class_balancingalphagammar   r   r   r   rQ   )	r   r   r   r   r   rQ   r   r   r   )
r   r   r   r   r   r   rQ   r   r   r   s
            r   r   z BinaryFocalCrossentropy.__init__w  st     	%"7#+ 	 
	
 
	
 
	
 '.	%:"



r   c           	      h    | j         | j        | j        | j        | j        | j        | j        | j        dS )Nr   r   r   r   rQ   r   r   r   r   r7   s    r   r    z"BinaryFocalCrossentropy.get_config  s<    I+#3I%)%?ZZ	
 	
 		
r   )Fr   r   Fr~   rN   r   r   r9   r/   s   @r   r   r     ss        @ @H $'(     :

 

 

 

 

 

 

r   r   z$keras.losses.CategoricalCrossentropyc                   4     e Zd ZdZ	 	 	 	 	 d	 fd	Zd Z xZS )
CategoricalCrossentropya  Computes the crossentropy loss between the labels and predictions.

    Use this crossentropy loss function when there are two or more label
    classes. We expect labels to be provided in a `one_hot` representation. If
    you want to provide labels as integers, please use
    `SparseCategoricalCrossentropy` loss. There should be `num_classes` floating
    point values per feature, i.e., the shape of both `y_pred` and `y_true` are
    `[batch_size, num_classes]`.

    Args:
        from_logits: Whether `y_pred` is expected to be a logits tensor. By
            default, we assume that `y_pred` encodes a probability distribution.
        label_smoothing: Float in [0, 1]. When > 0, label values are smoothed,
            meaning the confidence on label values are relaxed. For example, if
            `0.1`, use `0.1 / num_classes` for non-target labels and
            `0.9 + 0.1 / num_classes` for target labels.
        axis: The axis along which to compute crossentropy (the features
            axis). Defaults to `-1`.
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.

    Examples:

    Standalone usage:

    >>> y_true = [[0, 1, 0], [0, 0, 1]]
    >>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
    >>> # Using 'auto'/'sum_over_batch_size' reduction type.
    >>> cce = keras.losses.CategoricalCrossentropy()
    >>> cce(y_true, y_pred)
    1.177

    >>> # Calling with 'sample_weight'.
    >>> cce(y_true, y_pred, sample_weight=np.array([0.3, 0.7]))
    0.814

    >>> # Using 'sum' reduction type.
    >>> cce = keras.losses.CategoricalCrossentropy(
    ...     reduction="sum")
    >>> cce(y_true, y_pred)
    2.354

    >>> # Using 'none' reduction type.
    >>> cce = keras.losses.CategoricalCrossentropy(
    ...     reduction=None)
    >>> cce(y_true, y_pred)
    array([0.0513, 2.303], dtype=float32)

    Usage with the `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss=keras.losses.CategoricalCrossentropy())
    ```
    Fr~   rN   r   categorical_crossentropyc                     t                                          t          |||||           || _        || _        || _        d S r   )r   r   r   r   r   rQ   r   s         r   r   z CategoricalCrossentropy.__init__  sU     	$#+ 	 	
 	
 	
 '.			r   c                 D    | j         | j        | j        | j        | j        dS r   r   r7   s    r   r    z"CategoricalCrossentropy.get_config  r   r   )Fr~   rN   r   r   r9   r/   s   @r   r   r     sh        8 8x ''     (
 
 
 
 
 
 
r   r   z)keras.losses.CategoricalFocalCrossentropyc                   8     e Zd ZdZ	 	 	 	 	 	 	 d fd		Zd
 Z xZS )CategoricalFocalCrossentropya/  Computes the alpha balanced focal crossentropy loss.

    Use this crossentropy loss function when there are two or more label
    classes and if you want to handle class imbalance without using
    `class_weights`. We expect labels to be provided in a `one_hot`
    representation.

    According to [Lin et al., 2018](https://arxiv.org/pdf/1708.02002.pdf), it
    helps to apply a focal factor to down-weight easy examples and focus more on
    hard examples. The general formula for the focal loss (FL)
    is as follows:

    `FL(p_t) = (1 - p_t) ** gamma * log(p_t)`

    where `p_t` is defined as follows:
    `p_t = output if y_true == 1, else 1 - output`

    `(1 - p_t) ** gamma` is the `modulating_factor`, where `gamma` is a focusing
    parameter. When `gamma` = 0, there is no focal effect on the cross entropy.
    `gamma` reduces the importance given to simple examples in a smooth manner.

    The authors use alpha-balanced variant of focal loss (FL) in the paper:
    `FL(p_t) = -alpha * (1 - p_t) ** gamma * log(p_t)`

    where `alpha` is the weight factor for the classes. If `alpha` = 1, the
    loss won't be able to handle class imbalance properly as all
    classes will have the same weight. This can be a constant or a list of
    constants. If alpha is a list, it must have the same length as the number
    of classes.

    The formula above can be generalized to:
    `FL(p_t) = alpha * (1 - p_t) ** gamma * CrossEntropy(y_true, y_pred)`

    where minus comes from `CrossEntropy(y_true, y_pred)` (CE).

    Extending this to multi-class case is straightforward:
    `FL(p_t) = alpha * (1 - p_t) ** gamma * CategoricalCE(y_true, y_pred)`

    In the snippet below, there is `num_classes` floating pointing values per
    example. The shape of both `y_pred` and `y_true` are
    `(batch_size, num_classes)`.

    Args:
        alpha: A weight balancing factor for all classes, default is `0.25` as
            mentioned in the reference. It can be a list of floats or a scalar.
            In the multi-class case, alpha may be set by inverse class
            frequency by using `compute_class_weight` from `sklearn.utils`.
        gamma: A focusing parameter, default is `2.0` as mentioned in the
            reference. It helps to gradually reduce the importance given to
            simple (easy) examples in a smooth manner.
        from_logits: Whether `output` is expected to be a logits tensor. By
            default, we consider that `output` encodes a probability
            distribution.
        label_smoothing: Float in [0, 1]. When > 0, label values are smoothed,
            meaning the confidence on label values are relaxed. For example, if
            `0.1`, use `0.1 / num_classes` for non-target labels and
            `0.9 + 0.1 / num_classes` for target labels.
        axis: The axis along which to compute crossentropy (the features
            axis). Defaults to `-1`.
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.

    Examples:

    Standalone usage:

    >>> y_true = [[0., 1., 0.], [0., 0., 1.]]
    >>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
    >>> # Using 'auto'/'sum_over_batch_size' reduction type.
    >>> cce = keras.losses.CategoricalFocalCrossentropy()
    >>> cce(y_true, y_pred)
    0.23315276

    >>> # Calling with 'sample_weight'.
    >>> cce(y_true, y_pred, sample_weight=np.array([0.3, 0.7]))
    0.1632

    >>> # Using 'sum' reduction type.
    >>> cce = keras.losses.CategoricalFocalCrossentropy(
    ...     reduction="sum")
    >>> cce(y_true, y_pred)
    0.46631

    >>> # Using 'none' reduction type.
    >>> cce = keras.losses.CategoricalFocalCrossentropy(
    ...     reduction=None)
    >>> cce(y_true, y_pred)
    array([3.2058331e-05, 4.6627346e-01], dtype=float32)

    Usage with the `compile()` API:

    ```python
    model.compile(optimizer='adam',
                  loss=keras.losses.CategoricalFocalCrossentropy())
    ```
    r   r   Fr~   rN   r   categorical_focal_crossentropyc           
          t                                          t          |||||||           || _        || _        || _        || _        || _        dS )z4Initializes `CategoricalFocalCrossentropy` instance.)r   r   r   r   r   r   rQ   N)r   r   r   r   r   rQ   r   r   )	r   r   r   r   r   rQ   r   r   r   s	           r   r   z%CategoricalFocalCrossentropy.__init__`  si     	*#+ 	 		
 		
 		
 '.	



r   c                 \    | j         | j        | j        | j        | j        | j        | j        dS )Nr   r   r   r   rQ   r   r   r   r7   s    r   r    z'CategoricalFocalCrossentropy.get_config{  s6    I+#3IZZ
 
 	
r   )r   r   Fr~   rN   r   r   r9   r/   s   @r   r   r     sp        a aJ '-     6	
 	
 	
 	
 	
 	
 	
r   r   z*keras.losses.SparseCategoricalCrossentropyc                   2     e Zd ZdZ	 	 	 	 d fd	Zd Z xZS )	SparseCategoricalCrossentropya  Computes the crossentropy loss between the labels and predictions.

    Use this crossentropy loss function when there are two or more label
    classes.  We expect labels to be provided as integers. If you want to
    provide labels using `one-hot` representation, please use
    `CategoricalCrossentropy` loss.  There should be `# classes` floating point
    values per feature for `y_pred` and a single floating point value per
    feature for `y_true`.

    In the snippet below, there is a single floating point value per example for
    `y_true` and `num_classes` floating pointing values per example for
    `y_pred`. The shape of `y_true` is `[batch_size]` and the shape of `y_pred`
    is `[batch_size, num_classes]`.

    Args:
        from_logits: Whether `y_pred` is expected to be a logits tensor. By
            default, we assume that `y_pred` encodes a probability distribution.
        reduction: Type of reduction to apply to the loss. In almost all cases
            this should be `"sum_over_batch_size"`.
            Supported options are `"sum"`, `"sum_over_batch_size"` or `None`.
        name: Optional name for the loss instance.

    Examples:

    >>> y_true = [1, 2]
    >>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
    >>> # Using 'auto'/'sum_over_batch_size' reduction type.
    >>> scce = keras.losses.SparseCategoricalCrossentropy()
    >>> scce(y_true, y_pred)
    1.177

    >>> # Calling with 'sample_weight'.
    >>> scce(y_true, y_pred, sample_weight=np.array([0.3, 0.7]))
    0.814

    >>> # Using 'sum' reduction type.
    >>> scce = keras.losses.SparseCategoricalCrossentropy(
    ...     reduction="sum")
    >>> scce(y_true, y_pred)
    2.354

    >>> # Using 'none' reduction type.
    >>> scce = keras.losses.SparseCategoricalCrossentropy(
    ...     reduction=None)
    >>> scce(y_true, y_pred)
    array([0.0513, 2.303], dtype=float32)

    Usage with the `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss=keras.losses.SparseCategoricalCrossentropy())
    ```
    FNr   sparse_categorical_crossentropyc                 z    t                                          t          ||||           || _        || _        d S Nr   r   r   ignore_class)r   r   r   r   r   )r   r   r   r   r   r   s        r   r   z&SparseCategoricalCrossentropy.__init__  sM     	+#% 	 	
 	
 	
 '(r   c                 8    | j         | j        | j        | j        dS r   r   r7   s    r   r    z(SparseCategoricalCrossentropy.get_config  s'    I+ -	
 
 	
r   )FNr   r   r9   r/   s   @r   r   r     se        5 5r '.) ) ) ) ) )"
 
 
 
 
 
 
r   r   c                      t          j         d          }t          j         d          }t          j        t          j        ||                    } fd} fd}t          j        |||          }|S )zCConverts binary labels into -1/1 for hinge loss/metric calculation.r      c                      d z  dz
  S )Nr   rU   r&   r   s   r   _convert_binary_labelsz>convert_binary_labels_to_hinge.<locals>._convert_binary_labels  s    V|c!!r   c                       S r   r&   r   s   r   _return_labels_unconvertedzBconvert_binary_labels_to_hinge.<locals>._return_labels_unconverted  s    r   )r   equalall
logical_orcond)r   	are_zerosare_ones	is_binaryr   r   updated_y_trues   `      r   convert_binary_labels_to_hinger     s    	&!$$Iy##H	8<<>>I" " " " "     X)+E N r   zkeras.metrics.hingezkeras.losses.hingec                    t          j        |          }t          j        | |j                  } t          j        |           } t	          |           } t          j        t          j        d| |z  z
  d          d          S )a  Computes the hinge loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = mean(maximum(1 - y_true * y_pred, 0), axis=-1)
    ```

    Args:
        y_true: The ground truth values. `y_true` values are expected to be -1
            or 1. If binary (0 or 1) labels are provided they will be converted
            to -1 or 1 with shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

    Returns:
        Hinge loss values with shape = `[batch_size, d0, .. dN-1]`.

    Example:

    >>> y_true = np.random.choice([-1, 1], size=(2, 3))
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.hinge(y_true, y_pred)
    dtyperU   r~   rN   rQ   )r   convert_to_tensorcastr   r   meanmaximumr   r   s     r   rd   rd     sq    < "6**FXfFL111F"6**F+F33F8CKfvo 5s;;"EEEEr   zkeras.metrics.squared_hingezkeras.losses.squared_hingec           	          t          j        |          }t          j        | |j                  } t	          |           } t          j        t          j        t          j        d| |z  z
  d                    d          S )a  Computes the squared hinge loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = mean(square(maximum(1 - y_true * y_pred, 0)), axis=-1)
    ```

    Args:
        y_true: The ground truth values. `y_true` values are expected to be -1
            or 1. If binary (0 or 1) labels are provided we will convert them
            to -1 or 1 with shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

    Returns:
        Squared hinge loss values with shape = `[batch_size, d0, .. dN-1]`.

    Example:

    >>> y_true = np.random.choice([-1, 1], size=(2, 3))
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.squared_hinge(y_true, y_pred)
    rU   r~   rN   r   )r   r   r   r   r   r   squarer   r   s     r   ri   ri     sq    < "6**FXffl++F+F33F8
3;sVf_4c::;;"   r   zkeras.metrics.categorical_hingezkeras.losses.categorical_hingec                 2   t          j        |          }t          j        | |j                  } t          j        | |z  d          }t          j        d| z
  |z  d          }t          j        d|j                  }t          j        ||z
  dz   |          S )a<  Computes the categorical hinge loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = maximum(neg - pos + 1, 0)
    ```

    where `neg=maximum((1-y_true)*y_pred)` and `pos=sum(y_true*y_pred)`

    Args:
        y_true: The ground truth values. `y_true` values are expected to be
            either `{-1, +1}` or `{0, 1}` (i.e. a one-hot-encoded tensor) with
            shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

    Returns:
        Categorical hinge loss values with shape = `[batch_size, d0, .. dN-1]`.

    Example:

    >>> y_true = np.random.randint(0, 3, size=(2,))
    >>> y_true = np.eye(np.max(y_true) + 1)[y_true]
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.categorical_hinge(y_true, y_pred)
    rN   r   rU   r~   )r   r   r   r   summaxr   )r   r   posnegzeros        r   ro   ro   9  s    B "6**FXffl++F
'&6/
+
+
+C
'3<6)
3
3
3C8C&&D;sSy3---r   )z keras.metrics.mean_squared_errorzkeras.losses.mean_squared_errorzkeras._legacy.losses.msezkeras._legacy.losses.MSEzkeras._legacy.metrics.msezkeras._legacy.metrics.MSEc                     t          j        |          }t          j        | |j                  } t          | |          \  } }t          j        t          j        | |z
            d          S )aL  Computes the mean squared error between labels and predictions.

    Formula:

    ```python
    loss = mean(square(y_true - y_pred), axis=-1)
    ```

    Example:

    >>> y_true = np.random.randint(0, 2, size=(2, 3))
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.mean_squared_error(y_true, y_pred)

    Args:
        y_true: Ground truth values with shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

    Returns:
        Mean squared error values with shape = `[batch_size, d0, .. dN-1]`.
    r   rN   r   )r   r   r   r   r   r   r   s     r   r2   r2   b  sb    B "6**F"6>>>F3FFCCNFF8CJv//b9999r   )z!keras.metrics.mean_absolute_errorz keras.losses.mean_absolute_errorzkeras._legacy.losses.MAEzkeras._legacy.losses.maezkeras._legacy.metrics.MAEzkeras._legacy.metrics.maec                     t          j        |          }t          j        | |j                  } t          | |          \  } }t          j        t          j        | |z
            d          S )a>  Computes the mean absolute error between labels and predictions.

    ```python
    loss = mean(abs(y_true - y_pred), axis=-1)
    ```

    Args:
        y_true: Ground truth values with shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

    Returns:
        Mean absolute error values with shape = `[batch_size, d0, .. dN-1]`.

    Example:

    >>> y_true = np.random.randint(0, 2, size=(2, 3))
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.mean_absolute_error(y_true, y_pred)
    r   rN   r   )r   r   r   r   r   absr   s     r   r=   r=     sa    > "6**F"6>>>F3FFCCNFF8CGFVO,,26666r   )z,keras.metrics.mean_absolute_percentage_errorz+keras.losses.mean_absolute_percentage_errorzkeras._legacy.losses.mapezkeras._legacy.losses.MAPEzkeras._legacy.metrics.mapezkeras._legacy.metrics.MAPEc                    t          j        |          }t          j        | |j                  } t          j        t          j                              }t          | |          \  } }t          j        | |z
  t          j        t          j        |           |          z            }dt          j        |d          z  S )a  Computes the mean absolute percentage error between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = 100 * mean(abs((y_true - y_pred) / y_true), axis=-1)
    ```

    Division by zero is prevented by dividing by `maximum(y_true, epsilon)`
    where `epsilon = keras.backend.epsilon()`
    (default to `1e-7`).

    Args:
        y_true: Ground truth values with shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

    Returns:
        Mean absolute percentage error values with shape = `[batch_size, d0, ..
        dN-1]`.

    Example:

    >>> y_true = np.random.random(size=(2, 3))
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.mean_absolute_percentage_error(y_true, y_pred)
    r   g      Y@rN   r   )	r   r   r   r   epsilonr   r   r   r   )r   r   r   diffs       r   rB   rB     s    L "6**F"6>>>F#GO$5$566G3FFCCNFF7FVOs{376??G'L'LLMMD38Dr*****r   )z,keras.metrics.mean_squared_logarithmic_errorz+keras.losses.mean_squared_logarithmic_errorzkeras._legacy.losses.mslezkeras._legacy.losses.MSLEzkeras._legacy.metrics.mslezkeras._legacy.metrics.MSLEc                    t          j        t          j                              }t          j        |          }t          j        | |j                  } t          | |          \  } }t          j        t          j        ||          dz             }t          j        t          j        | |          dz             }t          j        t          j	        ||z
            d          S )a4  Computes the mean squared logarithmic error between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = mean(square(log(y_true + 1) - log(y_pred + 1)), axis=-1)
    ```

    Note that `y_pred` and `y_true` cannot be less or equal to 0. Negative
    values and 0 values will be replaced with `keras.backend.epsilon()`
    (default to `1e-7`).

    Args:
        y_true: Ground truth values with shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

    Returns:
        Mean squared logarithmic error values with shape = `[batch_size, d0, ..
        dN-1]`.

    Example:

    >>> y_true = np.random.randint(0, 2, size=(2, 3))
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.mean_squared_logarithmic_error(y_true, y_pred)
    r   rU   rN   r   )
r   r   r   r   r   r   logr   r   r   )r   r   r   	first_log
second_logs        r   rI   rI     s    L #GO$5$566G"6**F"6>>>F3FFCCNFFFG44s:;;IVW55;<<J8CJy:566R@@@@r   zkeras.losses.cosine_similarityrN   c                     t          j        |          }t          j        | |j                  } t          | |          \  } }t	          ||          }t	          | |          } t          j        | |z  |           S )a  Computes the cosine similarity between labels and predictions.

    Formula:
    ```python
    loss = -sum(l2_norm(y_true) * l2_norm(y_pred))
    ```

    Note that it is a number between -1 and 1. When it is a negative number
    between -1 and 0, 0 indicates orthogonality and values closer to -1
    indicate greater similarity. This makes it usable as a loss function in a
    setting where you try to maximize the proximity between predictions and
    targets. If either `y_true` or `y_pred` is a zero vector, cosine
    similarity will be 0 regardless of the proximity between predictions
    and targets.

    Args:
        y_true: Tensor of true targets.
        y_pred: Tensor of predicted targets.
        axis: Axis along which to determine similarity. Defaults to `-1`.

    Returns:
        Cosine similarity tensor.

    Example:

    >>> y_true = [[0., 1.], [1., 1.], [1., 1.]]
    >>> y_pred = [[1., 0.], [1., 1.], [-1., -1.]]
    >>> loss = keras.losses.cosine_similarity(y_true, y_pred, axis=-1)
    [-0., -0.99999994, 0.99999994]
    r   r   )r   r   r   r   r	   r   )r   r   rQ   s      r   rO   rO     s    @ "6**F"6>>>F3FFCCNFFvD)))FvD)))FGFVO$/////r   zkeras.losses.huberzkeras.metrics.huberrU   c                    t          j        |          }t          j        | |j                  } t          | |          \  } }t          j        |          }t          j        ||           }t          j        |          }t          j        d|j                  }t          j        t          j        ||k    |t          j        |          z  ||z  |t          j        |          z  z
            d          S )a  Computes Huber loss value.

    Formula:
    ```python
    for x in error:
        if abs(x) <= delta:
            loss.append(0.5 * x^2)
        elif abs(x) > delta:
            loss.append(delta * abs(x) - 0.5 * delta^2)

    loss = mean(loss, axis=-1)
    ```
    See: [Huber loss](https://en.wikipedia.org/wiki/Huber_loss).

    Example:

    >>> y_true = [[0, 1], [0, 0]]
    >>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
    >>> loss = keras.losses.huber(y_true, y_pred)
    0.155


    Args:
        y_true: tensor of true targets.
        y_pred: tensor of predicted targets.
        delta: A float, the point where the Huber loss function changes from a
            quadratic to linear. Defaults to `1.0`.

    Returns:
        Tensor with one scalar loss entry per sample.
    r         ?rN   r   )	r   r   r   r   subtractr   r   wherer   )r   r   rX   error	abs_errorhalfs         r   rY   rY   3  s    B "6**F"6>>>F3FFCCNFF!%((EL((EI IO<<<D8	3:e$$$Isz%'8'8 88	
 	

    r   )zkeras.losses.log_coshzkeras.metrics.log_coshzkeras._legacy.losses.logcoshzkeras._legacy.metrics.logcoshc                 0   t          j        |          }t          j        | |j                  } t          | |          \  } }t          j        t          j        d          |j                  fd}t          j         ||| z
            d          S )aM  Logarithm of the hyperbolic cosine of the prediction error.

    Formula:
    ```python
    loss = mean(log(cosh(y_pred - y_true)), axis=-1)
    ```

    Note that `log(cosh(x))` is approximately equal to `(x ** 2) / 2` for small
    `x` and to `abs(x) - log(2)` for large `x`. This means that 'logcosh' works
    mostly like the mean squared error, but will not be so strongly affected by
    the occasional wildly incorrect prediction.

    Example:

    >>> y_true = [[0., 1.], [0., 0.]]
    >>> y_pred = [[1., 1.], [0., 0.]]
    >>> loss = keras.losses.log_cosh(y_true, y_pred)
    0.108

    Args:
        y_true: Ground truth values with shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

    Returns:
        Logcosh error values with shape = `[batch_size, d0, .. dN-1]`.
    r   r   c                 >    | t          j        | dz            z   z
  S )Ng       )r   softplus)xlog2s    r   _logcoshzlog_cosh.<locals>._logcosh  s!    3<D)))D00r   rN   r   )r   r   r   r   r   r   )r   r   r   r   s      @r   r]   r]   e  s    H "6**F"6>>>F3FFCCNFF V\BBBD1 1 1 1 1 8HHVf_--B7777r   )zkeras.metrics.kl_divergencezkeras.losses.kl_divergencezkeras._legacy.losses.KLDzkeras._legacy.losses.kldz0keras._legacy.losses.kullback_leibler_divergencezkeras._legacy.metrics.KLDzkeras._legacy.metrics.kldz1keras._legacy.metrics.kullback_leibler_divergencec                 V   t          j        |          }t          j        | |j                  } t          j        | t	          j                    d          } t          j        |t	          j                    d          }t          j        | t          j        | |z            z  d          S )a  Computes Kullback-Leibler divergence loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = y_true * log(y_true / y_pred)
    ```

    `y_true` and `y_pred` are expected to be probability
    distributions, with values between 0 and 1. They will get
    clipped to the `[0, 1]` range.

    Args:
        y_true: Tensor of true targets.
        y_pred: Tensor of predicted targets.

    Returns:
        KL Divergence loss values with shape = `[batch_size, d0, .. dN-1]`.

    Example:

    >>> y_true = np.random.randint(0, 2, size=(2, 3)).astype(np.float32)
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.kl_divergence(y_true, y_pred)
    >>> assert loss.shape == (2,)
    >>> y_true = ops.clip(y_true, 1e-7, 1)
    >>> y_pred = ops.clip(y_pred, 1e-7, 1)
    >>> assert np.array_equal(
    ...     loss, np.sum(y_true * np.log(y_true / y_pred), axis=-1))
    r   rN   r   )r   r   r   clipr   r   r   r   r   s     r   rt   rt     s    X "6**F"66<88FXfgo//33FXfgo//33F76CGFVO4442>>>>r   zkeras.metrics.poissonzkeras.losses.poissonc                    t          j        |          }t          j        | |j                  } t          j        t          j                              }t          j        || t          j        ||z             z  z
  d          S )a  Computes the Poisson loss between y_true and y_pred.

    Formula:

    ```python
    loss = y_pred - y_true * log(y_pred)
    ```

    Args:
        y_true: Ground truth values. shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`.

    Returns:
        Poisson loss values with shape = `[batch_size, d0, .. dN-1]`.

    Example:

    >>> y_true = np.random.randint(0, 2, size=(2, 3))
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.poisson(y_true, y_pred)
    >>> assert loss.shape == (2,)
    >>> y_pred = y_pred + 1e-7
    >>> assert np.allclose(
    ...     loss, np.mean(y_pred - y_true * np.log(y_pred), axis=-1),
    ...     atol=1e-5)
    r   rN   r   )r   r   r   r   r   r   r   )r   r   r   s      r   ry   ry     sr    B "6**F"6>>>F#GO$5$566G8FVcgfw.>&?&???bIIIIr   z&keras.metrics.categorical_crossentropyz%keras.losses.categorical_crossentropyFr~   c                    t          |t                    r"t          d| dt          |                     t	          j        |          }t	          j        | |j                  } |j        d         dk    r%t          j
        d|j         dt          d           |r@t	          j        t	          j        |           d         |j                  }| d	|z
  z  ||z  z   } t	          j        | |||
          S )a  Computes the categorical crossentropy loss.

    Args:
        y_true: Tensor of one-hot true targets.
        y_pred: Tensor of predicted targets.
        from_logits: Whether `y_pred` is expected to be a logits tensor. By
            default, we assume that `y_pred` encodes a probability distribution.
        label_smoothing: Float in [0, 1]. If > `0` then smooth the labels. For
            example, if `0.1`, use `0.1 / num_classes` for non-target labels
            and `0.9 + 0.1 / num_classes` for target labels.
        axis: Defaults to `-1`. The dimension along which the entropy is
            computed.

    Returns:
        Categorical crossentropy loss value.

    Example:

    >>> y_true = [[0, 1, 0], [0, 0, 1]]
    >>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
    >>> loss = keras.losses.categorical_crossentropy(y_true, y_pred)
    >>> assert loss.shape == (2,)
    >>> loss
    array([0.0513, 2.303], dtype=float32)
    -`axis` must be of type `int`. Received: axis=	 of type rN   r   zIn loss categorical_crossentropy, expected y_pred.shape to be (batch_size, num_classes) with num_classes > 1. Received: y_pred.shape=B. Consider using 'binary_crossentropy' if you only have 2 classes.   
stacklevelrU   r   rQ   )
isinstancebool
ValueErrortyper   r   r   r   shapewarningswarnSyntaxWarningr   )r   r   r   r   rQ   num_classess         r   r   r     s0   D $ 
:": :-1$ZZ: :
 
 	
 "6**FXffl++F|B1O<BLO O O 	
 	
 	
 	
  
hsy004flCC301k)
 'Kd   r   z,keras.metrics.categorical_focal_crossentropyz+keras.losses.categorical_focal_crossentropyr   r   c                 z   t          |t                    r"t          d| dt          |                     t	          j        |          }t	          j        | |j                  } |j        d         dk    r%t          j
        d|j         dt          d           |r@t	          j        t	          j        |           d         |j                  }| d	|z
  z  ||z  z   } |rt	          j        ||
          }|t	          j        ||d          z  }t	          j        |t          j                    d	t          j                    z
            }|  t	          j        |          z  }	t	          j        d	|z
  |          }
t	          j        |
|          }t	          j        ||	          }t	          j        ||
          }|S )a4  Computes the categorical focal crossentropy loss.

    Args:
        y_true: Tensor of one-hot true targets.
        y_pred: Tensor of predicted targets.
        alpha: A weight balancing factor for all classes, default is `0.25` as
            mentioned in the reference. It can be a list of floats or a scalar.
            In the multi-class case, alpha may be set by inverse class
            frequency by using `compute_class_weight` from `sklearn.utils`.
        gamma: A focusing parameter, default is `2.0` as mentioned in the
            reference. It helps to gradually reduce the importance given to
            simple examples in a smooth manner. When `gamma` = 0, there is
            no focal effect on the categorical crossentropy.
        from_logits: Whether `y_pred` is expected to be a logits tensor. By
            default, we assume that `y_pred` encodes a probability
            distribution.
        label_smoothing: Float in [0, 1]. If > `0` then smooth the labels. For
            example, if `0.1`, use `0.1 / num_classes` for non-target labels
            and `0.9 + 0.1 / num_classes` for target labels.
        axis: Defaults to `-1`. The dimension along which the entropy is
            computed.

    Returns:
        Categorical focal crossentropy loss value.

    Example:

    >>> y_true = [[0, 1, 0], [0, 0, 1]]
    >>> y_pred = [[0.05, 0.9, 0.05], [0.1, 0.85, 0.05]]
    >>> loss = keras.losses.categorical_focal_crossentropy(y_true, y_pred)
    >>> assert loss.shape == (2,)
    >>> loss
    array([2.63401289e-04, 6.75912094e-01], dtype=float32)
    r   r   rN   r   zIn loss categorical_focal_crossentropy, expected y_pred.shape to be (batch_size, num_classes) with num_classes > 1. Received: y_pred.shape=r   r   r   rU   r   T)rQ   keepdims)r   r   r   r   r   r   r   r   r   r   r   r   softmaxr   r   r   r   r   powermultiply)r   r   r   r   r   r   rQ   r   outputccemodulating_factorweighting_factor	focal_cces                r   r   r   -  s   b $ 
:": :-1$ZZ: :
 
 	
 "6**FXffl++F|B1O<BLO O O 	
 	
 	
 	
  
hsy004flCC301k)
  0V$/// cgf4$????FXfgo//w7H7H1HIIF 'CGFOO
#C 	#,66|$5u== -s33I	---Ir   z-keras.metrics.sparse_categorical_crossentropyz,keras.losses.sparse_categorical_crossentropyc                    |t          j        |          dd         }t          j        | t          j        ||j                            }| t          j        || j                  z  } |t          j        t          j        |d          |j                  z  }t          j        | |||          }|Dt          j        ||          }t          j        ||d          }	 ||_	        n# t          $ r Y nw xY w|S )a  Computes the sparse categorical crossentropy loss.

    Args:
        y_true: Ground truth values.
        y_pred: The predicted values.
        from_logits: Whether `y_pred` is expected to be a logits tensor. By
            default, we assume that `y_pred` encodes a probability distribution.
        ignore_class: Optional integer. The ID of a class to be ignored during
            loss computation. This is useful, for example, in segmentation
            problems featuring a "void" class (commonly -1 or 255) in
            segmentation maps. By default (`ignore_class=None`), all classes are
            considered.
        axis: Defaults to `-1`. The dimension along which the entropy is
            computed.

    Returns:
        Sparse categorical crossentropy loss value.

    Examples:

    >>> y_true = [1, 2]
    >>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
    >>> loss = keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
    >>> assert loss.shape == (2,)
    >>> loss
    array([0.0513, 2.303], dtype=float32)
    NrN   r   r~   )r   r   	not_equalr   r   expand_dimsr   reshaper   _keras_maskAttributeError)r   r   r   r   rQ   	res_shape
valid_maskress           r   r   r     s   J If%%crc*	]638L&,+O+OPP
#(:v|<<<#(OJ++V\
 
 
 
-	  C [Y77
i
C--	(COO 	 	 	D	 Js   C' '
C43C4z!keras.metrics.binary_crossentropyz keras.losses.binary_crossentropyc                     t          j        |          }t          j        | |j                  } |r| d|z
  z  d|z  z   } t          j        t          j        | ||          |          S )a  Computes the binary crossentropy loss.

    Args:
        y_true: Ground truth values. shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`.
        from_logits: Whether `y_pred` is expected to be a logits tensor. By
            default, we assume that `y_pred` encodes a probability distribution.
        label_smoothing: Float in `[0, 1]`. If > `0` then smooth the labels by
            squeezing them towards 0.5, that is,
            using `1. - 0.5 * label_smoothing` for the target class
            and `0.5 * label_smoothing` for the non-target class.
        axis: The axis along which the mean is computed. Defaults to `-1`.

    Returns:
        Binary crossentropy loss value. shape = `[batch_size, d0, .. dN-1]`.

    Example:

    >>> y_true = [[0, 1], [0, 0]]
    >>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
    >>> loss = keras.losses.binary_crossentropy(y_true, y_pred)
    >>> assert loss.shape == (2,)
    >>> loss
    array([0.916 , 0.714], dtype=float32)
    rU   r   )r   r   )r   r   r   r   r   r   )r   r   r   r   rQ   s        r   r   r     s{    D "6**FXffl++F J301C/4II8KHHH   r   z'keras.metrics.binary_focal_crossentropyz&keras.losses.binary_focal_crossentropyc                    t          j        |          }t          j        | |j                  } |r| d|z
  z  d|z  z   } |rt          j        |          }t          j        | |d          }| |z  d| z
  d|z
  z  z   }	t          j        d|	z
  |          }
|
|z  }|r| |z  d| z
  d|z
  z  z   }||z  }t          j        ||          S )a  Computes the binary focal crossentropy loss.

    According to [Lin et al., 2018](https://arxiv.org/pdf/1708.02002.pdf), it
    helps to apply a focal factor to down-weight easy examples and focus more on
    hard examples. By default, the focal tensor is computed as follows:

    `focal_factor = (1 - output) ** gamma` for class 1
    `focal_factor = output ** gamma` for class 0
    where `gamma` is a focusing parameter. When `gamma` = 0, there is no focal
    effect on the binary crossentropy loss.

    If `apply_class_balancing == True`, this function also takes into account a
    weight balancing factor for the binary classes 0 and 1 as follows:

    `weight = alpha` for class 1 (`target == 1`)
    `weight = 1 - alpha` for class 0
    where `alpha` is a float in the range of `[0, 1]`.

    Args:
        y_true: Ground truth values, of shape `(batch_size, d0, .. dN)`.
        y_pred: The predicted values, of shape `(batch_size, d0, .. dN)`.
        apply_class_balancing: A bool, whether to apply weight balancing on the
            binary classes 0 and 1.
        alpha: A weight balancing factor for class 1, default is `0.25` as
            mentioned in the reference. The weight for class 0 is `1.0 - alpha`.
        gamma: A focusing parameter, default is `2.0` as mentioned in the
            reference.
        from_logits: Whether `y_pred` is expected to be a logits tensor. By
            default, we assume that `y_pred` encodes a probability distribution.
        label_smoothing: Float in `[0, 1]`. If > `0` then smooth the labels by
            squeezing them towards 0.5, that is,
            using `1. - 0.5 * label_smoothing` for the target class
            and `0.5 * label_smoothing` for the non-target class.
        axis: The axis along which the mean is computed. Defaults to `-1`.

    Returns:
        Binary focal crossentropy loss value
        with shape = `[batch_size, d0, .. dN-1]`.

    Example:

    >>> y_true = [[0, 1], [0, 0]]
    >>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
    >>> loss = keras.losses.binary_focal_crossentropy(
    ...        y_true, y_pred, gamma=2)
    >>> assert loss.shape == (2,)
    >>> loss
    array([0.330, 0.206], dtype=float32)
    rU   r   F)targetr   r   r   r   )r   r   r   r   sigmoidr   r   r   )r   r   r   r   r   r   r   rQ   bcep_tfocal_factor	focal_bceweights                r   r   r     s    B "6**FXffl++F J301C/4II %V$$

!  C 6/QZAJ7
7C9S3Y..Ls"I '%1v:!e)"<<Y&	8ID))))r   zkeras.losses.CTCc                   .     e Zd ZdZ	 	 d fd	Zd Z xZS )CTCa  CTC (Connectionist Temporal Classification) loss.

    Args:
        y_true: A tensor of shape `(batch_size, target_max_length)` containing
            the true labels in integer format. `0` always represents
            the blank/mask index and should not be used for classes.
        y_pred: A tensor of shape `(batch_size, output_max_length, num_classes)`
            containing logits (the output of your model).
            They should *not* be normalized via softmax.
    r   ctcc                 Z    t                                          t          ||           d S r_   )r   r   r  r4   s      r   r   zCTC.__init__e  s9    
 	 	 	
 	
 	
 	
 	
r   c                      | j         | j        dS r_   r`   r7   s    r   r    zCTC.get_configp      I
 
 	
r   )r   r  r9   r/   s   @r   r  r  X  s^        	 	 (	
 	
 	
 	
 	
 	

 
 
 
 
 
 
r   r  zkeras.losses.ctcc                    t          t          j        |                     dk    r$t          dt          j        |                      t          t          j        |                    dk    r$t          dt          j        |                     t          j        t          j        |           d         d          }t          j        t          j        |          d         d          }t          j        t          j        |           d         d          }|t          j        |fd          z  }|t          j        |fd          z  }t          j        | |||d	          S )
a  CTC (Connectionist Temporal Classification) loss.

    Args:
        y_true: A tensor of shape `(batch_size, max_length)` containing
            the true labels in integer format. `0` always represents
            the blank/mask index and should not be used for classes.
        y_pred: A tensor of shape `(batch_size, max_length, num_classes)`
            containing logits (the output of your model).
            They should *not* be normalized via softmax.
    r   z{Targets `y_true` are expected to be a tensor of shape `(batch_size, max_length)` in integer format. Received: y_true.shape=   zuLogits `y_pred` are expected to be a tensor of shape `(batch_size, max_length, num_classes)`. Received: y_pred.shape=r   int32r   r   )
mask_index)lenr   r   r   r   onesctc_loss)r   r   batch_lengthinput_lengthlabel_lengths        r   r  r  w  sa    39V"":&)i&7&7: :
 
 	

 39V"":&)i&7&7: :
 
 	
 8CIf--a0@@@L8CIf--a0@@@L8CIf--a0@@@L#(L?'"J"J"JJL#(L?'"J"J"JJL<lq   r   zkeras.losses.Dicec                   .     e Zd ZdZ	 	 d fd	Zd Z xZS )Dice3  Computes the Dice loss value between `y_true` and `y_pred`.

    Formula:
    ```python
    loss = 1 - (2 * sum(y_true * y_pred)) / (sum(y_true) + sum(y_pred))
    ```

    Args:
        y_true: tensor of true targets.
        y_pred: tensor of predicted targets.

    Returns:
        Dice loss value.
    r   dicec                 Z    t                                          t          ||           d S r_   )r   r   r"  r4   s      r   r   zDice.__init__  s9    
 	 	 	
 	
 	
 	
 	
r   c                      | j         | j        dS r_   r`   r7   s    r   r    zDice.get_config  r  r   )r   r"  r9   r/   s   @r   r   r     s^         " (	
 	
 	
 	
 	
 	

 
 
 
 
 
 
r   r   zkeras.losses.dicec                    t          j        |          }t          j        | |j                  } t          j        | dg          }t          j        |dg          }t          j        ||z            }t          j        d|z  t          j        |           t          j        |          z   t          j                    z             }d|z
  S )r!  rN   r   r   	r   r   r   r   r   r   divider   r   )r   r   inputstargetsintersectionr"  s         r   r"  r"    s      "6**FXffl++F["&&Fk&2$''G76G+,,L:l#'&//)GO,=,== D
 t8Or   zkeras.losses.Tverskyc                   2     e Zd ZdZ	 	 	 	 d fd	Zd Z xZS )Tversky}  Computes the Tversky loss value between `y_true` and `y_pred`.

    This loss function is weighted by the alpha and beta coefficients
    that penalize false positives and false negatives.

    With `alpha=0.5` and `beta=0.5`, the loss value becomes equivalent to
    Dice Loss.

    Args:
        y_true: tensor of true targets.
        y_pred: tensor of predicted targets.
        alpha: coefficient controlling incidence of false positives.
        beta: coefficient controlling incidence of false negatives.

    Returns:
        Tversky loss value.

    Reference:

    - [Salehi et al., 2017](https://arxiv.org/abs/1706.05721)
    r   r   tverskyc                 z    t                                          t          ||||           || _        || _        d S )N)r   betar   r   )r   r   r.  r   r0  )r   r   r0  r   r   r   s        r   r   zTversky.__init__  sI     	 	 	
 	
 	
 
			r   c                 8    | j         | j        | j        | j        dS )Nr   r   r0  r   r2  r7   s    r   r    zTversky.get_config  s%    IZI	
 
 	
r   )r   r   r   r.  r9   r/   s   @r   r,  r,    sd         0 '     "
 
 
 
 
 
 
r   r,  zkeras.losses.tverskyr   c                    t          j        |          }t          j        | |j                  } t          j        | dg          }t          j        |dg          }t          j        ||z            }t          j        d|z
  |z            }t          j        |d|z
  z            }t          j        ||||z  z   ||z  z   t          j                    z             }	d|	z
  S )r-  rN   r   r&  )
r   r   r   r0  r(  r)  r*  fpr   r.  s
             r   r.  r.    s    . "6**FXffl++F["&&Fk&2$''G76G+,,L	!g+'	(	(B	AJ'	(	(BjrEz!BI-0A0AA G
 w;r   )rN   )rU   )Fr~   rN   )r   r   Fr~   rN   )FNrN   )Fr   r   Fr~   rN   )r   r   )7r   	keras.srcr   r   keras.src.api_exportr   keras.src.losses.lossr   r   keras.src.savingr   keras.src.utils.numerical_utilsr	   r   r1   r<   rA   rH   rM   rT   r\   rc   rh   rn   rs   rx   r}   r   r   r   r   r   rd   ri   ro   r2   r=   rB   rI   rO   rY   r]   rt   ry   r   r   r   r   r   r  r  r   r"  r,  r.  r&   r   r   <module>r:     s
                - - - - - - & & & & & & @ @ @ @ @ @ . . . . . . 5 5 5 5 5 5    $   2 -..% % % % %* % % /.%2 .//% % % % %+ % % 0/%2 899% % % % %"5 % % :9%: 899% % % % %"5 % % :9%: -..$% $% $% $% $%* $% $% /.$%N "##"% "% "% "% "% "% "% $#"%J $%%% % % % %! % % &%%2 "##% % % % % % % $#%4 )**% % % % %& % % +*%4 -..% % % % %* % % /.%6 )**% % % % %& % % +*%6 $%%% % % % %! % % &%%. /00l
 l
 l
 l
 l
, l
 l
 10l
^ 455j
 j
 j
 j
 j
1 j
 j
 65j
Z 455V
 V
 V
 V
 V
1 V
 V
 65V
r 9::H
 H
 H
 H
 H
#6 H
 H
 ;:H
V :;;O
 O
 O
 O
 O
$7 O
 O
 <;O
d  (  F F F> %$   @ )(  .  .  .F   
 
: :
 
:8   
 
7 7
 
74   
 
 +  +
 
 +F   
 
!A !A
 
!AH .//$0 $0 $0 0/$0N #%:;<<. . . =<.b    #8 #8 #8L 
 
 
 #? #? #?L  J J JB 0/  BD6 6 6 6r 65  
	W W W Wt 76  @B7 7 7 7t +*  BD% % % %P 10   

	T* T* T* T*n  !!
 
 
 
 

 
 
 "!
<  !!    "! F !""
 
 
 
 
 
 
 #"
D !""  #"< $%%.
 .
 .
 .
 .
! .
 .
 &%.
b $%%$ $ $ &%$ $ $r   