
    6bij             
          S r SSKrSSKrSSKrSSKJs  Jr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  \" S5       " S S5      5       r\" S/ S9 " S S\5      5       r\" S5       " S S\5      5       r \" S5       " S S\5      5       r!\" S5       " S S\5      5       r"\" S5       " S  S!\5      5       r#\" S"5       " S# S$\5      5       r$\" S%5       " S& S'\5      5       r%\" S(5       " S) S*\5      5       r&\" S+5       " S, S-\5      5       r'\" S.5       " S/ S0\5      5       r(\" S15       " S2 S3\5      5       r)\" S45       " S5 S6\5      5       r*\" S75       " S8 S9\5      5       r+\" S:5       " S; S<\5      5       r,\" S=5       " S> S?\5      5       r-\" S@5       " SA SB\5      5       r.\" SC5       " SD SE\5      5       r/\" SF5       " SG SH\5      5       r0\" SISJSKSLSMSN5      \Rb                  R2                  Rd                  SO 5       5       r3SSP jr4\Rj                  " \3\Rl                  5      SQ 5       r7\" SRSSSTSUSVSW5      \Rb                  R2                  Rd                  SX 5       5       r8\Rj                  " \8\Rl                  5      SY 5       r9\" SZS[S\S]S^S_5      \Rb                  R2                  Rd                  S` 5       5       r:\Rj                  " \:\Rl                  5      Sa 5       r;\" SbScSdSeSfSg5      \Rb                  R2                  Rd                  Sh 5       5       r<\Rj                  " \<\Rl                  5      Si 5       r=Sj r>\" SkSl5      \Rb                  R2                  Rd                  Sm 5       5       r?\" SnSo5      \Rb                  R2                  Rd                  Sp 5       5       r@\" Sq5      \Rb                  R2                  Rd                  Sr 5       5       rA\" Ss/ S9\Rb                  R2                  Rd                  SSt j5       5       rB\" SuSvSwSx5      \Rb                  R2                  Rd                  Sy 5       5       rC\" SzS{5      \Rb                  R2                  Rd                   SS| j5       5       rD\Rj                  " \D\Rl                  5       SS} j5       rE\" S~S5      \Rb                  R2                  Rd                       SS j5       5       rF\Rj                  " \F\Rl                  5           SS j5       rG\" SS5      \Rb                  R2                  Rd                   SS j5       5       rH\Rj                  " \H\Rl                  5       SS j5       rI\" SS5      \Rb                  R2                  Rd                   SS j5       5       rJ\Rj                  " \J\Rl                  5       SS j5       rK\" SS5      \Rb                  R2                  Rd                        SS j5       5       rL\Rj                  " \L\Rl                  5            SS j5       rM\" SSSSSSSS5      \Rb                  R2                  Rd                  S 5       5       rN\" SS5      \Rb                  R2                  Rd                  S 5       5       rO\" S/ SQS9\Rb                  R2                  Rd                  SS j5       5       rP\J=rQrR\3=rSrT\8=rUrV\:=rWrX\<=rYrZ\N=r[=r\r]\Cr^\Br_S r`\" S5      SS j5       ra\" S5      SS j5       rb\" S5      S 5       rc\R
                  R                  R                  R                  S\HS0rgg)zBuilt-in loss functions.    N)backend)
saving_lib)serialization)deserialize_keras_object)serialize_keras_object)losses_utils)tf_utils)ragged_map_ops)ragged_util)dispatch)keras_export)doc_controlszkeras.losses.Lossc                       \ rS rSrSr\R                  R                  S4S jrS r	SS jr
\S 5       rS r\R                  \R"                  S	 5       5       rS
 rSrg)Loss)   a#  Loss base class.

To be implemented by subclasses:
* `call()`: Contains the logic for loss calculation using `y_true`,
    `y_pred`.

Example subclass implementation:

```python
class MeanSquaredError(Loss):

  def call(self, y_true, y_pred):
      return tf.reduce_mean(tf.math.square(y_pred - y_true), axis=-1)
```

When using a Loss under a `tf.distribute.Strategy`, except passing it
to `Model.compile()` for use by `Model.fit()`, please use reduction
types 'SUM' or 'NONE', and reduce losses explicitly. Using 'AUTO' or
'SUM_OVER_BATCH_SIZE' will raise an error when calling the Loss object
from a custom training loop or from user-defined code in `Layer.call()`.
Please see this custom training
[tutorial](https://www.tensorflow.org/tutorials/distribute/custom_training)
for more details on this.
Nc                     [         R                  R                  U5        Xl        X l        SU l        U R                  5         g)a  Initializes `Loss` class.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction option will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance.
FN)r   ReductionV2validate	reductionname_allow_sum_over_batch_size_set_name_scope)selfr   r   s      M/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/losses.py__init__Loss.__init__D   s8      	  )))4"	 +0'    c                     U R                   c+  U R                  R                  R                  S5      U l        gU R                   S:X  a  SU l        gU R                   R                  S5      U l        g)z"Creates a valid `name_scope` name.N_z<lambda>lambda)r   	__class____name__strip_name_scoper   s    r   r   Loss._set_name_scope\   sT    99#~~66<<SADYY*$'D  $yys3Dr   c                    [         R                  " XU5      n[        R                  " U R                  5         U   [
        R                  " 5       (       a  U R                  nOZ[
        R                  R                  R                  U R                  [
        R                  R                  R                  5       5      nU" X5      n[        R                  " U5      n[        R                  " U5      nUb  Ub  Xx-  n	OUb  Un	OUb  Un	OSn	U R                  5       n
[        R                  " XcX5      n[        R                   " XcU
S9sSSS5        sSSS5        $ ! , (       d  f       O= fSSS5        g! , (       d  f       g= f)a  Invokes the `Loss` instance.

Args:
    y_true: Ground truth values. shape = `[batch_size, d0, .. dN]`,
        except sparse loss functions such as sparse categorical
        crossentropy where shape = `[batch_size, d0, .. dN-1]`
    y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`
    sample_weight: Optional `sample_weight` acts as a coefficient for
        the loss. If a scalar is provided, then the loss is simply
        scaled by the given value. If `sample_weight` is a tensor of
        size `[batch_size]`, then the total loss for each sample of the
        batch is rescaled by the corresponding element in the
        `sample_weight` vector. If the shape of `sample_weight` is
        `[batch_size, d0, .. dN-1]` (or can be broadcasted to this
        shape), then each loss element of `y_pred` is scaled by the
        corresponding value of `sample_weight`. (Note on`dN-1`: all loss
        functions reduce by 1 dimension, usually axis=-1.)

Returns:
    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has
        shape `[batch_size, d0, .. dN-1]`; otherwise, it is scalar.
        (Note `dN-1` because all loss functions reduce by 1 dimension,
        usually axis=-1.)

Raises:
  ValueError: If the shape of `sample_weight` is invalid.
N)r   )r	   "graph_context_for_symbolic_tensorsr   
name_scoper$   tfexecuting_eagerlycall__internal__	autograph
tf_convertcontrol_status_ctxr   get_mask_get_reductionapply_valid_maskcompute_weighted_loss)r   y_truey_predsample_weight	graph_ctxcall_fnlossesin_maskout_maskmaskr   s              r   __call__Loss.__call__f   s-   < ??M
	  0 019##%%))//33>>IIr88KKM V,F"++F3G#,,V4H"x';)$%++-I(99tM  555 4=91199111s#   E2D
E	E2
E&	"E22
F c                     U " S0 UD6$ )zInstantiates a `Loss` from its config (output of `get_config()`).

Args:
    config: Output of `get_config()`.

Returns:
    A `Loss` instance.
 rA   )clsconfigs     r   from_configLoss.from_config   s     }V}r   c                 4    U R                   U R                  S.$ )z4Returns the config dictionary for a `Loss` instance.r   r   rG   r%   s    r   
get_configLoss.get_config   s    !^^TYY??r   c                     [        S5      e)aq  Invokes the `Loss` instance.

Args:
    y_true: Ground truth values. shape = `[batch_size, d0, .. dN]`,
        except sparse loss functions such as sparse categorical
        crossentropy where shape = `[batch_size, d0, .. dN-1]`
    y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`

Returns:
    Loss values with the shape `[batch_size, d0, .. dN-1]`.
z"Must be implemented in subclasses.)NotImplementedError)r   r5   r6   s      r   r,   	Loss.call   s     ""FGGr   c                    U R                   (       d~  [        R                  R                  5       (       a[  U R                  [
        R                  R                  :X  d(  U R                  [
        R                  R                  :X  a  [        S5      eU R                  [
        R                  R                  :X  a  [
        R                  R                  $ U R                  $ )z?Handles `AUTO` reduction cases and returns the reduction value.aZ  Please use `tf.keras.losses.Reduction.SUM` or `tf.keras.losses.Reduction.NONE` for loss reduction when losses are used with `tf.distribute.Strategy`, except for specifying losses in `Model.compile()` for use by the built-in training looop `Model.fit()`.
Please see https://www.tensorflow.org/tutorials/distribute/custom_training for more details.)
r   r*   
distributehas_strategyr   r   r   AUTOSUM_OVER_BATCH_SIZE
ValueErrorr%   s    r   r2   Loss._get_reduction   s     //**,,,":":"?"??>>++??@ @  >>\55:::++???~~r   )r   r$   r   r   N)r"   
__module____qualname____firstlineno____doc__r   r   rP   r   r   r>   classmethodrD   rH   abcabstractmethodr   for_subclass_implementersr,   r2   __static_attributes__rA   r   r   r   r   )   ss    2 ".!9!9!>!>T 04=~ 	 	@ 	++H , Hr   r   z-keras.__internal__.losses.LossFunctionWrapper)v1c                   ~   ^  \ rS rSrSr\R                  R                  S4U 4S jjrS r	U 4S jr
\S 5       rSrU =r$ )	LossFunctionWrapper   z*Wraps a loss function in the `Loss` class.Nc                 8   > [         TU ]  X#S9  Xl        X@l        g)aA  Initializes `LossFunctionWrapper` class.

Args:
    fn: The loss function to wrap, with signature `fn(y_true, y_pred,
        **kwargs)`.
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction option will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance.
    **kwargs: The keyword arguments that are passed on to `fn`.
rG   N)superr   fn
_fn_kwargs)r   rd   r   r   kwargsr!   s        r   r   LossFunctionWrapper.__init__   s    * 	98 r   c                 x   [         R                  " U5      (       a3  [         R                  " U5      (       a  [        R                  " X!5      u  p![         R                  R
                  R                  U R                  [         R                  R
                  R                  5       5      nU" X40 U R                  D6$ )zInvokes the `LossFunctionWrapper` instance.

Args:
    y_true: Ground truth values.
    y_pred: The predicted values.

Returns:
    Loss values per sample.
)
r*   	is_tensorr   squeeze_or_expand_dimensionsr-   r.   r/   rd   r0   re   )r   r5   r6   ag_fns       r   r,   LossFunctionWrapper.call   s     <<BLL$8$8)FFNF ))44GGR__..AAC
 V7t77r   c                   > 0 nU R                   R                  5        H:  u  p#[        R                  " U5      (       a  [        R
                  " U5      OUX'   M<     [        R                  " 5       (       a  SSKJ	n  U" U R                  5      US'   [        TU ]1  5       n[        [        UR                  5       5      [        UR                  5       5      -   5      $ )Nr   )get_registered_namerd   )re   itemsr	   is_tensor_or_variabler   evalr   saving_v3_enabledtf_keras.src.utilsrn   rd   rc   rH   dictlist)r   rC   kvrn   base_configr!   s         r   rH   LossFunctionWrapper.get_config  s    OO))+DA#+#A#A!#D#DQ! I ,
 ''))>.tww7F4Lg(*D**,-V\\^0DDEEr   c                     [         R                  " 5       (       a0  UR                  SS5      nU(       a  U [        L a  [	        U5      US'   U " S0 UD6$ )zInstantiates a `Loss` from its config (output of `get_config()`).

Args:
    config: Output of `get_config()`.

Returns:
    A `keras.losses.Loss` instance.
rd   NrA   )r   rr   popr`   get)rB   rC   fn_names      r   rD   LossFunctionWrapper.from_config  sG     ''))jjt,G3"55"7|t}V}r   )re   rd   )r"   rU   rV   rW   rX   r   r   rP   r   r,   rH   rY   rD   r]   __classcell__r!   s   @r   r`   r`      s=    4 )4499!28(F  r   r`   zkeras.losses.MeanSquaredErrorc                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )MeanSquaredErrori0  a  Computes the mean of squares of errors between labels and predictions.

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

Standalone usage:

>>> y_true = [[0., 1.], [0., 0.]]
>>> y_pred = [[1., 1.], [1., 0.]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> mse = tf.keras.losses.MeanSquaredError()
>>> mse(y_true, y_pred).numpy()
0.5

>>> # Calling with 'sample_weight'.
>>> mse(y_true, y_pred, sample_weight=[0.7, 0.3]).numpy()
0.25

>>> # Using 'sum' reduction type.
>>> mse = tf.keras.losses.MeanSquaredError(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> mse(y_true, y_pred).numpy()
1.0

>>> # Using 'none' reduction type.
>>> mse = tf.keras.losses.MeanSquaredError(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> mse(y_true, y_pred).numpy()
array([0.5, 0.5], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.MeanSquaredError())
```
mean_squared_errorc                 *   > [         TU ]  [        X!S9  g)a  Initializes `MeanSquaredError` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction option will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to
        'mean_squared_error'.
r   r   N)rc   r   r   r   r   r   r!   s      r   r   MeanSquaredError.__init__V  s    & 	+$Lr   rA   r"   rU   rV   rW   rX   r   r   rP   r   r]   r   r   s   @r   r   r   0  s)    "J %0055<PM Mr   r   zkeras.losses.MeanAbsoluteErrorc                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )MeanAbsoluteErroril  a  Computes the mean of absolute difference between labels and predictions.

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

Standalone usage:

>>> y_true = [[0., 1.], [0., 0.]]
>>> y_pred = [[1., 1.], [1., 0.]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> mae = tf.keras.losses.MeanAbsoluteError()
>>> mae(y_true, y_pred).numpy()
0.5

>>> # Calling with 'sample_weight'.
>>> mae(y_true, y_pred, sample_weight=[0.7, 0.3]).numpy()
0.25

>>> # Using 'sum' reduction type.
>>> mae = tf.keras.losses.MeanAbsoluteError(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> mae(y_true, y_pred).numpy()
1.0

>>> # Using 'none' reduction type.
>>> mae = tf.keras.losses.MeanAbsoluteError(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> mae(y_true, y_pred).numpy()
array([0.5, 0.5], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.MeanAbsoluteError())
```
mean_absolute_errorc                 *   > [         TU ]  [        X!S9  g)a  Initializes `MeanAbsoluteError` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction option will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to
        'mean_absolute_error'.
r   N)rc   r   r   r   s      r   r   MeanAbsoluteError.__init__  s    * 	,4Mr   rA   r   r   s   @r   r   r   l  s)    "L **//"N Nr   r   z(keras.losses.MeanAbsolutePercentageErrorc                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )MeanAbsolutePercentageErrori  aB  Computes the mean absolute percentage error between `y_true` & `y_pred`.

Formula:

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

Note that to avoid dividing by zero, a small epsilon value
is added to the denominator.

Standalone usage:

>>> y_true = [[2., 1.], [2., 3.]]
>>> y_pred = [[1., 1.], [1., 0.]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> mape = tf.keras.losses.MeanAbsolutePercentageError()
>>> mape(y_true, y_pred).numpy()
50.

>>> # Calling with 'sample_weight'.
>>> mape(y_true, y_pred, sample_weight=[0.7, 0.3]).numpy()
20.

>>> # Using 'sum' reduction type.
>>> mape = tf.keras.losses.MeanAbsolutePercentageError(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> mape(y_true, y_pred).numpy()
100.

>>> # Using 'none' reduction type.
>>> mape = tf.keras.losses.MeanAbsolutePercentageError(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> mape(y_true, y_pred).numpy()
array([25., 75.], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd',
              loss=tf.keras.losses.MeanAbsolutePercentageError())
```
mean_absolute_percentage_errorc                 *   > [         TU ]  [        X!S9  g)a  Initializes `MeanAbsolutePercentageError` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction option will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to
        'mean_absolute_percentage_error'.
r   N)rc   r   r   r   s      r   r   $MeanAbsolutePercentageError.__init__      * 	* 	 	
r   rA   r   r   s   @r   r   r     s'    (X **//-
 
r   r   z(keras.losses.MeanSquaredLogarithmicErrorc                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )MeanSquaredLogarithmicErrori  a  Computes the mean squared logarithmic error between `y_true` & `y_pred`.

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

Standalone usage:

>>> y_true = [[0., 1.], [0., 0.]]
>>> y_pred = [[1., 1.], [1., 0.]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> msle = tf.keras.losses.MeanSquaredLogarithmicError()
>>> msle(y_true, y_pred).numpy()
0.240

>>> # Calling with 'sample_weight'.
>>> msle(y_true, y_pred, sample_weight=[0.7, 0.3]).numpy()
0.120

>>> # Using 'sum' reduction type.
>>> msle = tf.keras.losses.MeanSquaredLogarithmicError(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> msle(y_true, y_pred).numpy()
0.480

>>> # Using 'none' reduction type.
>>> msle = tf.keras.losses.MeanSquaredLogarithmicError(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> msle(y_true, y_pred).numpy()
array([0.240, 0.240], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd',
              loss=tf.keras.losses.MeanSquaredLogarithmicError())
```
mean_squared_logarithmic_errorc                 *   > [         TU ]  [        X!S9  g)a  Initializes `MeanSquaredLogarithmicError` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction option will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to
        'mean_squared_logarithmic_error'.
r   N)rc   r   r   r   s      r   r   $MeanSquaredLogarithmicError.__init__  r   r   rA   r   r   s   @r   r   r     s'    #N **//-
 
r   r   zkeras.losses.BinaryCrossentropyc                   b   ^  \ rS rSrSrSSS\R                  R                  S4U 4S jjrSr	U =r
$ )	BinaryCrossentropyi1  aY  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`).

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

With `tf.keras` API:

```python
model.compile(
    loss=tf.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 = tf.keras.losses.BinaryCrossentropy(from_logits=True)
>>> bce(y_true, y_pred).numpy()
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 = tf.keras.losses.BinaryCrossentropy(from_logits=True)
>>> bce(y_true, y_pred).numpy()
0.865
>>> # Using 'sample_weight' attribute
>>> bce(y_true, y_pred, sample_weight=[0.8, 0.2]).numpy()
0.243
>>> # Using 'sum' reduction` type.
>>> bce = tf.keras.losses.BinaryCrossentropy(from_logits=True,
...     reduction=tf.keras.losses.Reduction.SUM)
>>> bce(y_true, y_pred).numpy()
1.730
>>> # Using 'none' reduction type.
>>> bce = tf.keras.losses.BinaryCrossentropy(from_logits=True,
...     reduction=tf.keras.losses.Reduction.NONE)
>>> bce(y_true, y_pred).numpy()
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`
>>> tf.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        binary_crossentropyc           	      >   > [         TU ]  [        UUUUUS9  Xl        g)aE  Initializes `BinaryCrossentropy` instance.

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` contains 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 `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction option will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Name for the op. Defaults to 'binary_crossentropy'.
r   r   from_logitslabel_smoothingaxisN)rc   r   r   r   r   r   r   r   r   r   r!   s         r   r   BinaryCrossentropy.__init__q  s2    D 	#+ 	 	
 'r   r   r   r   s   @r   r   r   1  s0    <@ **//"*' *'r   r   z$keras.losses.BinaryFocalCrossentropyc                   t   ^  \ rS rSrSrSSSSSS\R                  R                  S4U 4S	 jjrU 4S
 jr	Sr
U =r$ )BinaryFocalCrossentropyi  a?  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.

With the `compile()` API:

```python
model.compile(
  loss=tf.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 = tf.keras.losses.BinaryFocalCrossentropy(gamma=2,
...                                                from_logits=True)
>>> loss(y_true, y_pred).numpy()
0.691

>>> # Apply class weight
>>> loss = tf.keras.losses.BinaryFocalCrossentropy(
...     apply_class_balancing=True, gamma=2, from_logits=True)
>>> loss(y_true, y_pred).numpy()
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 = tf.keras.losses.BinaryFocalCrossentropy(gamma=3,
...                                                from_logits=True)
>>> loss(y_true, y_pred).numpy()
0.647

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

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

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

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

>>> # Apply class weight
>>> loss = tf.keras.losses.BinaryFocalCrossentropy(
...     apply_class_balancing=True, gamma=4, from_logits=True,
...     reduction=tf.keras.losses.Reduction.SUM)
>>> loss(y_true, y_pred).numpy()
0.914

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

>>> # Apply class weight
>>> loss = tf.keras.losses.BinaryFocalCrossentropy(
...     apply_class_balancing=True, gamma=5, from_logits=True,
...     reduction=tf.keras.losses.Reduction.NONE)
>>> loss(y_true, y_pred).numpy()
array([0.0004 0.8670], dtype=float32)


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 `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the reduction
        option will be determined by the usage context. For almost all cases
        this defaults to `SUM_OVER_BATCH_SIZE`. When used under a
        `tf.distribute.Strategy`, except via `Model.compile()` and
        `Model.fit()`, using `AUTO` or `SUM_OVER_BATCH_SIZE`
        will raise an error. Please see this custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Name for the op. Defaults to 'binary_focal_crossentropy'.
F      ?       @r   r   binary_focal_crossentropyc	                 h   > [         T	U ]  [        UUUUUUUUS9	  X@l        Xl        X l        X0l        g)z/Initializes `BinaryFocalCrossentropy` instance.)apply_class_balancingalphagammar   r   r   r   r   N)rc   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r!   s
            r   r    BinaryFocalCrossentropy.__init__&  sJ     	%"7#+ 	 
	
 '%:"

r   c                    > U R                   U R                  U R                  S.n[        TU ]  5       n[        [        UR                  5       5      [        UR                  5       5      -   5      $ )N)r   r   r   )r   r   r   rc   rH   rt   ru   ro   r   rC   rx   r!   s      r   rH   "BinaryFocalCrossentropy.get_configB  sY    %)%?%?ZZZZ

 g(*D**,-V\\^0DDEEr   )r   r   r   r   r"   rU   rV   rW   rX   r   r   rP   r   rH   r]   r   r   s   @r   r   r     sA    DP $**//(8F Fr   r   z$keras.losses.CategoricalCrossentropyc                   b   ^  \ rS rSrSrSSS\R                  R                  S4U 4S jjrSr	U =r
$ )	CategoricalCrossentropyiL  a~  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 `# classes` floating
point values per feature.

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

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 = tf.keras.losses.CategoricalCrossentropy()
>>> cce(y_true, y_pred).numpy()
1.177

>>> # Calling with 'sample_weight'.
>>> cce(y_true, y_pred, sample_weight=tf.constant([0.3, 0.7])).numpy()
0.814

>>> # Using 'sum' reduction type.
>>> cce = tf.keras.losses.CategoricalCrossentropy(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> cce(y_true, y_pred).numpy()
2.354

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

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd',
              loss=tf.keras.losses.CategoricalCrossentropy())
```
Fr   r   categorical_crossentropyc           	      2   > [         TU ]  [        UUUUUS9  g)a  Initializes `CategoricalCrossentropy` instance.

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 `tf.keras.losses.Reduction` to apply to loss.
        Default value is `AUTO`. `AUTO` indicates that the reduction
        option will be determined by the usage context. For almost all
        cases this defaults to `SUM_OVER_BATCH_SIZE`. When used under a
        `tf.distribute.Strategy`, except via `Model.compile()` and
        `Model.fit()`, using `AUTO` or `SUM_OVER_BATCH_SIZE`
        will raise an error. Please see this custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance.
        Defaults to 'categorical_crossentropy'.
r   N)rc   r   r   r   s         r   r    CategoricalCrossentropy.__init__{  s*    @ 	$#+ 	 	
r   rA   r   r   s   @r   r   r   L  s0    +^ **//''
 '
r   r   z)keras.losses.CategoricalFocalCrossentropyc                   r   ^  \ rS rSrSrSSSSS\R                  R                  S4U 4S	 jjrU 4S
 jr	Sr
U =r$ )CategoricalFocalCrossentropyi  u  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 `# classes` floating pointing values per
example. The shape of both `y_pred` and `y_true` are
`[batch_size, num_classes]`.

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 = tf.keras.losses.CategoricalFocalCrossentropy()
>>> cce(y_true, y_pred).numpy()
0.23315276

>>> # Calling with 'sample_weight'.
>>> cce(y_true, y_pred, sample_weight=tf.constant([0.3, 0.7])).numpy()
0.1632

>>> # Using 'sum' reduction type.
>>> cce = tf.keras.losses.CategoricalFocalCrossentropy(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> cce(y_true, y_pred).numpy()
0.46631

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

Usage with the `compile()` API:
```python
model.compile(optimizer='adam',
              loss=tf.keras.losses.CategoricalFocalCrossentropy())
```

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 `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the reduction
        option will be determined by the usage context. For almost all cases
        this defaults to `SUM_OVER_BATCH_SIZE`. When used under a
        `tf.distribute.Strategy`, except via `Model.compile()` and
        `Model.fit()`, using `AUTO` or `SUM_OVER_BATCH_SIZE`
        will raise an error. Please see this custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance.
        Defaults to 'categorical_focal_crossentropy'.

r   r   Fr   r   categorical_focal_crossentropyc                 Z   > [         TU ]  [        UUUUUUUS9  X0l        Xl        X l        g)z4Initializes `CategoricalFocalCrossentropy` instance.)r   r   r   r   r   r   r   N)rc   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r!   s	           r   r   %CategoricalFocalCrossentropy.__init__  sA     	*#+ 	 		
 '

r   c                    > U R                   U R                  S.n[        TU ]  5       n[	        [        UR                  5       5      [        UR                  5       5      -   5      $ )N)r   r   )r   r   rc   rH   rt   ru   ro   r   s      r   rH   'CategoricalFocalCrossentropy.get_config(  sP    ZZZZ
 g(*D**,-V\\^0DDEEr   )r   r   r   r   r   s   @r   r   r     s>    fT **//-2F Fr   r   z*keras.losses.SparseCategoricalCrossentropyc                   `   ^  \ rS rSrSrSS\R                  R                  S4U 4S jjrSr	U =r
$ )SparseCategoricalCrossentropyi1  a2  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 `# 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]`.

Standalone usage:

>>> 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 = tf.keras.losses.SparseCategoricalCrossentropy()
>>> scce(y_true, y_pred).numpy()
1.177

>>> # Calling with 'sample_weight'.
>>> scce(y_true, y_pred, sample_weight=tf.constant([0.3, 0.7])).numpy()
0.814

>>> # Using 'sum' reduction type.
>>> scce = tf.keras.losses.SparseCategoricalCrossentropy(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> scce(y_true, y_pred).numpy()
2.354

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

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd',
              loss=tf.keras.losses.SparseCategoricalCrossentropy())
```
FNsparse_categorical_crossentropyc                 0   > [         TU ]  [        UUUUS9  g)a  Initializes `SparseCategoricalCrossentropy` instance.

Args:
    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.
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction ption will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance.
        Defaults to 'sparse_categorical_crossentropy'.
)r   r   r   ignore_classN)rc   r   r   )r   r   r   r   r   r!   s        r   r   &SparseCategoricalCrossentropy.__init__b  s&    > 	+#% 	 	
r   rA   r   r   s   @r   r   r   1  s-    -b **//.%
 %
r   r   zkeras.losses.CosineSimilarityc                   ^   ^  \ rS rSrSrS\R                  R                  S4U 4S jjrSr	U =r
$ )CosineSimilarityi  a
  Computes the cosine similarity between labels and predictions.

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. The values closer to 1 indicate greater
dissimilarity. 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.

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

Standalone usage:

>>> y_true = [[0., 1.], [1., 1.]]
>>> y_pred = [[1., 0.], [1., 1.]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> cosine_loss = tf.keras.losses.CosineSimilarity(axis=1)
>>> # 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]]
>>> # loss = mean(sum(l2_norm(y_true) . l2_norm(y_pred), axis=1))
>>> #       = -((0. + 0.) +  (0.5 + 0.5)) / 2
>>> cosine_loss(y_true, y_pred).numpy()
-0.5

>>> # Calling with 'sample_weight'.
>>> cosine_loss(y_true, y_pred, sample_weight=[0.8, 0.2]).numpy()
-0.0999

>>> # Using 'sum' reduction type.
>>> cosine_loss = tf.keras.losses.CosineSimilarity(axis=1,
...     reduction=tf.keras.losses.Reduction.SUM)
>>> cosine_loss(y_true, y_pred).numpy()
-0.999

>>> # Using 'none' reduction type.
>>> cosine_loss = tf.keras.losses.CosineSimilarity(axis=1,
...     reduction=tf.keras.losses.Reduction.NONE)
>>> cosine_loss(y_true, y_pred).numpy()
array([-0., -0.999], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd',
              loss=tf.keras.losses.CosineSimilarity(axis=1))
```

Args:
    axis: The axis along which the cosine similarity is computed
        (the features axis). Defaults to -1.
    reduction: Type of `tf.keras.losses.Reduction` to apply to loss.
        Default value is `AUTO`. `AUTO` indicates that the reduction option
        will be determined by the usage context. For almost all cases this
        defaults to `SUM_OVER_BATCH_SIZE`. When used under a
        `tf.distribute.Strategy`, except via `Model.compile()` and
        `Model.fit()`, using `AUTO` or `SUM_OVER_BATCH_SIZE` will raise an
        error. Please see this custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to 'cosine_similarity'.
r   cosine_similarityc                 ,   > [         TU ]  [        X#US9  g )N)r   r   r   )rc   r   r   )r   r   r   r   r!   s       r   r   CosineSimilarity.__init__  s     	D 	 	
r   rA   r   r   s   @r   r   r     s*    >D **// 	
 
r   r   zkeras.losses.Hingec                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )Hingei  a  Computes the hinge loss between `y_true` & `y_pred`.

`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.

Standalone usage:

>>> y_true = [[0., 1.], [0., 0.]]
>>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> h = tf.keras.losses.Hinge()
>>> h(y_true, y_pred).numpy()
1.3

>>> # Calling with 'sample_weight'.
>>> h(y_true, y_pred, sample_weight=[1, 0]).numpy()
0.55

>>> # Using 'sum' reduction type.
>>> h = tf.keras.losses.Hinge(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> h(y_true, y_pred).numpy()
2.6

>>> # Using 'none' reduction type.
>>> h = tf.keras.losses.Hinge(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> h(y_true, y_pred).numpy()
array([1.1, 1.5], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.Hinge())
```
hingec                 *   > [         TU ]  [        X!S9  g)a  Initializes `Hinge` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction ption will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to 'hinge'.
r   N)rc   r   r   r   s      r   r   Hinge.__init__   s      	T?r   rA   r   r   s   @r   r   r     s(    %N ".!9!9!>!>W @ @r   r   zkeras.losses.SquaredHingec                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )SquaredHingei  a  Computes the squared hinge loss between `y_true` & `y_pred`.

`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.

Standalone usage:

>>> y_true = [[0., 1.], [0., 0.]]
>>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> h = tf.keras.losses.SquaredHinge()
>>> h(y_true, y_pred).numpy()
1.86

>>> # Calling with 'sample_weight'.
>>> h(y_true, y_pred, sample_weight=[1, 0]).numpy()
0.73

>>> # Using 'sum' reduction type.
>>> h = tf.keras.losses.SquaredHinge(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> h(y_true, y_pred).numpy()
3.72

>>> # Using 'none' reduction type.
>>> h = tf.keras.losses.SquaredHinge(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> h(y_true, y_pred).numpy()
array([1.46, 2.26], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.SquaredHinge())
```
squared_hingec                 *   > [         TU ]  [        X!S9  g)a  Initializes `SquaredHinge` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction ption will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to 'squared_hinge'.
r   N)rc   r   r   r   s      r   r   SquaredHinge.__init__<  s    $ 	TGr   rA   r   r   s   @r   r   r     s(    %P %0055OH Hr   r   zkeras.losses.CategoricalHingec                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )CategoricalHingeiQ  a  Computes the categorical hinge loss between `y_true` & `y_pred`.

`loss = maximum(neg - pos + 1, 0)`
where `neg=maximum((1-y_true)*y_pred) and pos=sum(y_true*y_pred)`

Standalone usage:

>>> y_true = [[0, 1], [0, 0]]
>>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> h = tf.keras.losses.CategoricalHinge()
>>> h(y_true, y_pred).numpy()
1.4

>>> # Calling with 'sample_weight'.
>>> h(y_true, y_pred, sample_weight=[1, 0]).numpy()
0.6

>>> # Using 'sum' reduction type.
>>> h = tf.keras.losses.CategoricalHinge(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> h(y_true, y_pred).numpy()
2.8

>>> # Using 'none' reduction type.
>>> h = tf.keras.losses.CategoricalHinge(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> h(y_true, y_pred).numpy()
array([1.2, 1.6], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.CategoricalHinge())
```
categorical_hingec                 *   > [         TU ]  [        X!S9  g)a  Initializes `CategoricalHinge` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction ption will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance.
        Defaults to 'categorical_hinge'.
r   N)rc   r   r   r   s      r   r   CategoricalHinge.__init__x  s    & 	*Kr   rA   r   r   s   @r   r   r   Q  s)    #L %0055<OL Lr   r   zkeras.losses.Poissonc                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )Poissoni  aT  Computes the Poisson loss between `y_true` & `y_pred`.

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

Standalone usage:

>>> y_true = [[0., 1.], [0., 0.]]
>>> y_pred = [[1., 1.], [0., 0.]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> p = tf.keras.losses.Poisson()
>>> p(y_true, y_pred).numpy()
0.5

>>> # Calling with 'sample_weight'.
>>> p(y_true, y_pred, sample_weight=[0.8, 0.2]).numpy()
0.4

>>> # Using 'sum' reduction type.
>>> p = tf.keras.losses.Poisson(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> p(y_true, y_pred).numpy()
0.999

>>> # Using 'none' reduction type.
>>> p = tf.keras.losses.Poisson(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> p(y_true, y_pred).numpy()
array([0.999, 0.], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.Poisson())
```
poissonc                 *   > [         TU ]  [        X!S9  g)a  Initializes `Poisson` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction ption will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to 'poisson'.
r   N)rc   r   r   r   s      r   r   Poisson.__init__  s      	tAr   rA   r   r   s   @r   r   r     s(    "H ".!9!9!>!>Y B Br   r   zkeras.losses.LogCoshc                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )LogCoshi  a  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`.

Standalone usage:

>>> y_true = [[0., 1.], [0., 0.]]
>>> y_pred = [[1., 1.], [0., 0.]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> l = tf.keras.losses.LogCosh()
>>> l(y_true, y_pred).numpy()
0.108

>>> # Calling with 'sample_weight'.
>>> l(y_true, y_pred, sample_weight=[0.8, 0.2]).numpy()
0.087

>>> # Using 'sum' reduction type.
>>> l = tf.keras.losses.LogCosh(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> l(y_true, y_pred).numpy()
0.217

>>> # Using 'none' reduction type.
>>> l = tf.keras.losses.LogCosh(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> l(y_true, y_pred).numpy()
array([0.217, 0.], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.LogCosh())
```
log_coshc                 *   > [         TU ]  [        X!S9  g)a  Initializes `LogCosh` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction ption will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to 'log_cosh'.
r   N)rc   r   r   r   s      r   r   LogCosh.__init__  s    $ 	Br   rA   r   r   s   @r   r   r     s(    #L %0055JC Cr   r   zkeras.losses.KLDivergencec                   \   ^  \ rS rSrSr\R                  R                  S4U 4S jjrSr	U =r
$ )KLDivergencei  a  Computes Kullback-Leibler divergence loss between `y_true` & `y_pred`.

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

See: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence

Standalone usage:

>>> y_true = [[0, 1], [0, 0]]
>>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> kl = tf.keras.losses.KLDivergence()
>>> kl(y_true, y_pred).numpy()
0.458

>>> # Calling with 'sample_weight'.
>>> kl(y_true, y_pred, sample_weight=[0.8, 0.2]).numpy()
0.366

>>> # Using 'sum' reduction type.
>>> kl = tf.keras.losses.KLDivergence(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> kl(y_true, y_pred).numpy()
0.916

>>> # Using 'none' reduction type.
>>> kl = tf.keras.losses.KLDivergence(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> kl(y_true, y_pred).numpy()
array([0.916, -3.08e-06], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.KLDivergence())
```
kl_divergencec                 *   > [         TU ]  [        X!S9  g)a  Initializes `KLDivergence` instance.

Args:
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction ption will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance.
        Defaults to 'kl_divergence'.
r   N)rc   r   r   r   s      r   r   KLDivergence.__init__+  s    & 	TGr   rA   r   r   s   @r   r   r     s(    $N %0055OH Hr   r   zkeras.losses.Huberc                   ^   ^  \ rS rSrSrS\R                  R                  S4U 4S jjrSr	U =r
$ )HuberiA  a  Computes the Huber loss between `y_true` & `y_pred`.

For each value x in `error = y_true - y_pred`:

```
loss = 0.5 * x^2                  if |x| <= d
loss = 0.5 * d^2 + d * (|x| - d)  if |x| > d
```
where d is `delta`. See: https://en.wikipedia.org/wiki/Huber_loss

Standalone usage:

>>> y_true = [[0, 1], [0, 0]]
>>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.
>>> h = tf.keras.losses.Huber()
>>> h(y_true, y_pred).numpy()
0.155

>>> # Calling with 'sample_weight'.
>>> h(y_true, y_pred, sample_weight=[1, 0]).numpy()
0.09

>>> # Using 'sum' reduction type.
>>> h = tf.keras.losses.Huber(
...     reduction=tf.keras.losses.Reduction.SUM)
>>> h(y_true, y_pred).numpy()
0.31

>>> # Using 'none' reduction type.
>>> h = tf.keras.losses.Huber(
...     reduction=tf.keras.losses.Reduction.NONE)
>>> h(y_true, y_pred).numpy()
array([0.18, 0.13], dtype=float32)

Usage with the `compile()` API:

```python
model.compile(optimizer='sgd', loss=tf.keras.losses.Huber())
```
      ?
huber_lossc                 ,   > [         TU ]  [        X2US9  g)a  Initializes `Huber` instance.

Args:
    delta: A float, the point where the Huber loss function changes from
        a quadratic to linear.
    reduction: Type of `tf.keras.losses.Reduction` to apply to
        loss. Default value is `AUTO`. `AUTO` indicates that the
        reduction ption will be determined by the usage context. For
        almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
        used under a `tf.distribute.Strategy`, except via
        `Model.compile()` and `Model.fit()`, using `AUTO` or
        `SUM_OVER_BATCH_SIZE` will raise an error. Please see this
        custom training [tutorial](
        https://www.tensorflow.org/tutorials/distribute/custom_training)
        for more details.
    name: Optional name for the instance. Defaults to 'huber_loss'.
)r   r   deltaN)rc   r   huber)r   r   r   r   r!   s       r   r   Huber.__init__m  s    . 	TeLr   rA   r   r   s   @r   r   r   A  s,    (X **//	M Mr   r   z keras.metrics.mean_squared_errorzkeras.metrics.msezkeras.metrics.MSEzkeras.losses.mean_squared_errorzkeras.losses.msezkeras.losses.MSEc                     [         R                  " U5      n[         R                  " XR                  5      n [        R
                  " [         R                  R                  X5      SS9$ )a  Computes the mean squared error between labels and predictions.

After computing the squared distance between the inputs, the mean value over
the last dimension is returned.

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

Standalone usage:

>>> y_true = np.random.randint(0, 2, size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.mean_squared_error(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> assert np.array_equal(
...     loss.numpy(), np.mean(np.square(y_true - y_pred), axis=-1))

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

Returns:
    Mean squared error values. shape = `[batch_size, d0, .. dN-1]`.
r   r   )r*   convert_to_tensorcastdtyper   meanmathsquared_differencer5   r6   s     r   r   r     sG    B !!&)FWWV\\*F<<226BLLr   c                 4  ^ ^^^ S mS mU 4S jmUUU U4S jn[        U[        R                  5      (       d  T " XR                  5       5      $ UR                  R                  5       SS n[        U5      S:  a  [        R                  " XRR                  S9nO[        R                  " / UR                  S9nX4 Vs/ s H  owR                  PM     nnU(       a5  U V	s/ s H  n	[        U	5      PM     n
n	U
S   U
S   S-
  :X  a  US   S	S US'   [        R                  " U[        U5      S:  S
9n[        R                  " U5      n[        R                  " U5         [         R"                  " XU4US9sS	S	S	5        $ s  snf s  sn	f ! , (       d  f       g	= f)a  Apply a loss function on a per batch basis.

Args:
    loss_fn: The loss function
    y_true: truth values (RaggedTensor)
    y_pred: predicted values (RaggedTensor)
    y_pred_extra_dim: whether y_pred has an additional dimension compared to
    y_true

Returns:
    Loss-function result. A dense tensor if the output has a single
    dimension (per-batch loss value); a ragged tensor otherwise.
c                 T   [         R                  " U R                  5        Vs/ s Ht  n[         R                  " [         R                  R                  [         R                  " U[        R                  " 5       5      5      [         R                  " S/5      5      PMv     sn5      $ s  snf )zReturns true if this RaggedTensor has the same row_lengths across

   all ragged dimensions and thus can be converted to a dense tensor
   without loss of information.

Args:
    rt: RaggedTensor.
r   )
r*   
reduce_allnested_row_lengthsequalr   reduce_variancer   r   floatxconstant)rtrow_lenss     r   rt_is_equiv_dense4_ragged_tensor_apply_loss.<locals>.rt_is_equiv_dense  s     }} !# 5 5 7 !8H GG++'..*:; KK&	 !8

 
	
s   A;B%c                 &    [        S U  5       5      $ )Nc              3      #    U  H6  n[        U[        R                  5      (       a  UR                  5       OUv   M8     g 7frT   )
isinstancer*   RaggedTensor	to_tensor).0r  s     r   	<genexpr>G_ragged_tensor_apply_loss.<locals>._convert_to_dense.<locals>.<genexpr>  s2      
 )R__==BLLN2Es   >A )tuple)inputss    r   _convert_to_dense4_ragged_tensor_apply_loss.<locals>._convert_to_dense  s     

 
 	
r   c                   > T" U 6 nU(       a@  [        U[        R                  5      (       d!  [        R                  R                  U5      nU$ U(       d/  [        U[        R                  5      (       a  UR	                  5       nU$ )zAdapt the result to ragged or dense tensor according to the expected

output type. This is done so that all the return values of the map
operation have the same type.
)r  r*   r	  from_tensorr
  )r  ragged_outputrloss_fns      r   
_call_loss-_ragged_tensor_apply_loss.<locals>._call_loss  sb     VAr!?!?++A.A  :a#A#AAr   c                    >^ ^ T u  p#[        U[        R                  5      (       a+  [        R                  " T" U5      UUU U4S jUU U4S j5      $ T" T 6 $ )Nc                  "   > T " T" T5      T5      $ rT   rA   )r  r  r  r  s   r   <lambda>=_ragged_tensor_apply_loss.<locals>._wrapper.<locals>.<lambda>  s    
#4V#<mLr   c                     > T " TT5      $ rT   rA   )r  r  r  s   r   r  r    s    
6=9r   )r  r*   r	  cond)r  r  r   r6   r  r  r  r  s   ``  r   _wrapper+_ragged_tensor_apply_loss.<locals>._wrapper  sG    	fboo..77!&)L9  r      r   r   )shaper   N)r  )elemsr   )r  r*   r	  r
  r"  as_listlenRaggedTensorSpecr   
TensorSpecnested_row_splits	functoolspartialr   assert_splits_matchcontrol_dependenciesr
   map_fn)r  r5   r6   y_pred_extra_dimr  lshapespecr  nested_splits_listslistrdimsr-  assertion_listr  r  r  s   `            @@@r   _ragged_tensor_apply_lossr5    s_   
*
	  	  fboo..v//122\\!!#Ab)F
6{Q""||D}}2V\\::@9IJ9I2..9IJ);<);U);<8uQx!|#$6q$9#2$>q!xs6{QGF 445GHN		 	 	0$$VF3C4P 
1	0 K = 
1	0s   >E? FF		
Fc                 "    [        [        X5      $ )a  Implements support for handling RaggedTensors.

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

Returns:
    Mean squared error values. shape = `[batch_size, d0, .. dN-1]`.
    When the number of dimensions of the batch feature vector [d0, .. dN] is
    greater than one the return value is a RaggedTensor. Otherwise, a Dense
    tensor with dimensions [batch_size] is returned.
)r5  r   r   s     r   _ragged_tensor_mser7    s     %%7HHr   z!keras.metrics.mean_absolute_errorzkeras.metrics.maezkeras.metrics.MAEz keras.losses.mean_absolute_errorzkeras.losses.maezkeras.losses.MAEc                     [         R                  " U5      n[         R                  " XR                  5      n [        R
                  " [         R                  " X-
  5      SS9$ )ax  Computes the mean absolute error between labels and predictions.

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

Standalone usage:

>>> y_true = np.random.randint(0, 2, size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.mean_absolute_error(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> assert np.array_equal(
...     loss.numpy(), np.mean(np.abs(y_true - y_pred), axis=-1))

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

Returns:
    Mean absolute error values. shape = `[batch_size, d0, .. dN-1]`.
r   r   )r*   r   r   r   r   r   absr   s     r   r   r     sB    < !!&)FWWV\\*F<<v/b99r   c                 "    [        [        X5      $ )z-RaggedTensor adapter for mean_absolute_error.)r5  r   r   s     r   _ragged_tensor_maer;  ;  s     %%8&IIr   z,keras.metrics.mean_absolute_percentage_errorzkeras.metrics.mapezkeras.metrics.MAPEz+keras.losses.mean_absolute_percentage_errorzkeras.losses.mapezkeras.losses.MAPEc                 L   [         R                  " U5      n[         R                  " XR                  5      n [         R                  " X-
  [
        R                  " [         R                  " U 5      [
        R                  " 5       5      -  5      nS[
        R                  " USS9-  $ )a   Computes the mean absolute percentage error between `y_true` & `y_pred`.

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

Standalone usage:

>>> y_true = np.random.random(size=(2, 3))
>>> y_true = np.maximum(y_true, 1e-7)  # Prevent division by zero
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.mean_absolute_percentage_error(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> assert np.array_equal(
...     loss.numpy(),
...     100. * np.mean(np.abs((y_true - y_pred) / y_true), axis=-1))

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

Returns:
    Mean absolute percentage error values. shape = `[batch_size, d0, ..
    dN-1]`.
g      Y@r   r   )	r*   r   r   r   r9  r   maximumepsilonr   )r5   r6   diffs      r   r   r   A  sq    B !!&)FWWV\\*F66	GOOBFF6NGOO<MNND 7<<2...r   c                 "    [        [        X5      $ )zSupport RaggedTensors.)r5  r   r   s     r   _ragged_tensor_maperA  j       %& r   z,keras.metrics.mean_squared_logarithmic_errorzkeras.metrics.mslezkeras.metrics.MSLEz+keras.losses.mean_squared_logarithmic_errorzkeras.losses.mslezkeras.losses.MSLEc                    [         R                  " U5      n[         R                  " XR                  5      n [         R                  R                  [        R                  " U[        R                  " 5       5      S-   5      n[         R                  R                  [        R                  " U [        R                  " 5       5      S-   5      n[        R                  " [         R                  R                  X#5      SS9$ )a*  Computes the mean squared logarithmic error between `y_true` & `y_pred`.

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

Standalone usage:

>>> y_true = np.random.randint(0, 2, size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.mean_squared_logarithmic_error(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> y_true = np.maximum(y_true, 1e-7)
>>> y_pred = np.maximum(y_pred, 1e-7)
>>> assert np.allclose(
...     loss.numpy(),
...     np.mean(
...         np.square(np.log(y_true + 1.) - np.log(y_pred + 1.)), axis=-1))

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

Returns:
    Mean squared logarithmic error values. shape = `[batch_size, d0, ..
    dN-1]`.
r   r   r   )r*   r   r   r   r   logr   r=  r>  r   r   )r5   r6   	first_log
second_logs       r   r   r   r  s    F !!&)FWWV\\*FGOOFGOO4EFLMIW__VW__5FG#MNJ<<
""99 r   c                 "    [        [        X5      $ )z.Implements support for handling RaggedTensors.)r5  r   r   s     r   _ragged_tensor_mslerH    rB  r   c                    ^  [         R                  " T S5      n[         R                  " T S5      n[         R                  " [         R                  " X5      5      nU 4S jn[         R                  R
                  R                  X4U 4S j5      nU$ )z!Converts binary labels into -1/1.r   r!  c                     > ST -  S-
  $ )Nr   r   rA   r5   s   r   _convert_binary_labels5_maybe_convert_labels.<locals>._convert_binary_labels  s    V|c!!r   c                     > T $ rT   rA   rK  s   r   r  '_maybe_convert_labels.<locals>.<lambda>  s    6r   )r*   r   r   
logical_orr-   
smart_cond)r5   	are_zerosare_ones	is_binaryrL  updated_y_trues   `     r   _maybe_convert_labelsrV    sh    #Ixx"HbmmI@AI" __//::>N r   zkeras.metrics.squared_hingezkeras.losses.squared_hingec           	      
   [         R                  " U5      n[         R                  " XR                  5      n [	        U 5      n [
        R                  " [         R                  " [         R                  " SX-  -
  S5      5      SS9$ )a*  Computes the squared hinge loss between `y_true` & `y_pred`.

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

Standalone usage:

>>> y_true = np.random.choice([-1, 1], size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.squared_hinge(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> assert np.array_equal(
...     loss.numpy(),
...     np.mean(np.square(np.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. shape = `[batch_size, d0, .. dN]`.
    y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`.

Returns:
    Squared hinge loss values. shape = `[batch_size, d0, .. dN-1]`.
r   r   r   r   )	r*   r   r   r   rV  r   r   squarer=  r   s     r   r   r     sa    4 !!&)FWWV\\*F"6*F<<
		"**S6?2C89 r   zkeras.metrics.hingezkeras.losses.hingec                     [         R                  " U5      n[         R                  " XR                  5      n [	        U 5      n [
        R                  " [         R                  " SX-  -
  S5      SS9$ )a  Computes the hinge loss between `y_true` & `y_pred`.

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

Standalone usage:

>>> y_true = np.random.choice([-1, 1], size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.hinge(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> assert np.array_equal(
...     loss.numpy(),
...     np.mean(np.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. shape = `[batch_size, d0, .. dN]`.
    y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`.

Returns:
    Hinge loss values. shape = `[batch_size, d0, .. dN-1]`.
r   r   r   r   )r*   r   r   r   rV  r   r   r=  r   s     r   r   r     sS    4 !!&)FWWV\\*F"6*F<<

3#8#>RHHr   zkeras.losses.categorical_hingec                 L   [         R                  " U5      n[         R                  " XR                  5      n [         R                  " X-  SS9n[         R
                  " SU -
  U-  SS9n[         R                  " SUR                  5      n[         R                  " X2-
  S-   U5      $ )a:  Computes the categorical hinge loss between `y_true` & `y_pred`.

`loss = maximum(neg - pos + 1, 0)`
where `neg=maximum((1-y_true)*y_pred) and pos=sum(y_true*y_pred)`

Standalone usage:

>>> y_true = np.random.randint(0, 3, size=(2,))
>>> y_true = tf.keras.utils.to_categorical(y_true, num_classes=3)
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.categorical_hinge(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> pos = np.sum(y_true * y_pred, axis=-1)
>>> neg = np.amax((1. - y_true) * y_pred, axis=-1)
>>> assert np.array_equal(loss.numpy(), np.maximum(0., neg - pos + 1.))

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).
    y_pred: The predicted values.

Returns:
    Categorical hinge loss values.
r   r   r   r   )r*   r   r   r   
reduce_sum
reduce_maxr=  )r5   r6   posnegzeros        r   r   r     s|    6 !!&)FWWV\\*F
--b
1C
--v/b
9C773%D::ci#ot,,r   zkeras.losses.huberc                 H   [         R                  " U[        R                  " 5       S9n[         R                  " U [        R                  " 5       S9n [         R                  " U[        R                  " 5       S9n[         R                  " X5      n[         R
                  " U5      n[         R                  " SUR                  S9n[        R                  " [         R                  " XB:*  U[         R                  " U5      -  X$-  U[         R                  " U5      -  -
  5      SS9$ )a  Computes Huber loss value.

For each value x in `error = y_true - y_pred`:

```
loss = 0.5 * x^2                  if |x| <= d
loss = d * |x| - 0.5 * d^2        if |x| > d
```
where d is `delta`. See: https://en.wikipedia.org/wiki/Huber_loss

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.

Returns:
    Tensor with one scalar loss entry per sample.
r         ?r   r   )r*   r   r   r   subtractr9  r   r   r   whererX  )r5   r6   r   error	abs_errorhalfs         r   r   r     s    , WWV7>>#34FWWV7>>#34FGGE!12EKK'EuI9??;D<<
299U##ryy'7 77	

  r   zkeras.losses.log_coshzkeras.losses.logcoshzkeras.metrics.log_coshzkeras.metrics.logcoshc                     [         R                  " U5      n[         R                  " XR                  5      n S n[        R
                  " U" X-
  5      SS9$ )a  Logarithm of the hyperbolic cosine of the prediction error.

`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.

Standalone usage:

>>> y_true = np.random.random(size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.logcosh(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> x = y_pred - y_true
>>> assert np.allclose(
...     loss.numpy(),
...     np.mean(x + np.log(np.exp(-2. * x) + 1.) - tf.math.log(2.),
...             axis=-1),
...     atol=1e-5)

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

Returns:
    Logcosh error values. shape = `[batch_size, d0, .. dN-1]`.
c                     U [         R                  R                  SU -  5      -   [         R                  " [         R                  R	                  S5      U R
                  5      -
  $ )Ng       r   )r*   r   softplusr   rD  r   )xs    r   _logcoshlog_cosh.<locals>._logcoshg  sC      **RWWRWW[[5Eqww-OO	
r   r   r   )r*   r   r   r   r   r   )r5   r6   rl  s      r   r   r   A  sF    F !!&)FWWV\\*F

 <<1;;r   z&keras.metrics.categorical_crossentropyz%keras.losses.categorical_crossentropyc                 (  ^ ^^^ [        T[        5      (       a  [        ST S[        T5       35      e[        R
                  " T5      m[        R                  " T TR                  5      m [        R
                  " TTR                  S9mTR                  S   S:X  a(  [        R                  " STR                   S3[        SS	9  UUUU 4S
 jn[        R                  R                  R                  TUU 4S j5      m [        R                  " T TUTS9$ )as  Computes the categorical crossentropy loss.

Standalone usage:

>>> y_true = [[0, 1, 0], [0, 0, 1]]
>>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
>>> loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> loss.numpy()
array([0.0513, 2.303], dtype=float32)

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.
-`axis` must be of type `int`. Received: axis=	 of type ra  r   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.   
stacklevelc                     > [         R                  " [         R                  " T5      T   TR                  5      n TST-
  -  TU -  -   $ )Nr   r*   r   r"  r   )num_classesr   r   r6   r5   s    r   _smooth_labels0categorical_crossentropy.<locals>._smooth_labels  sD    ggbhhv.t4fllC./k)
 	
r   c                     > T $ rT   rA   rK  s   r   r  *categorical_crossentropy.<locals>.<lambda>      r   )r   r   )r  boolrR   typer*   r   r   r   r"  warningswarnSyntaxWarningr-   rQ  r   r   r5   r6   r   r   r   rx  s   `` `` r   r   r   o  s    B $"V9T$ZL:
 	
 !!&)FWWVV\\*F**?&,,OO||B1<<BLL> JOO 	

 
 __''22F ++Kd r   c                 P    [         R                  " [        UUUS9n[        XPU5      $ )aH  Implements support for handling RaggedTensors.

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: The axis along which to compute crossentropy (the features axis).
        Defaults to -1.

Returns:
    Categorical crossentropy loss value.

Expected shape: (batch, sequence_len, n_classes) with sequence_len
being variable per batch.
Return shape: (batch, sequence_len).

When used by CategoricalCrossentropy() with the default reduction
(SUM_OVER_BATCH_SIZE), the reduction averages the loss over the
number of elements independent of the batch. E.g. if the RaggedTensor
has 2 batches with [2, 1] values respectively the resulting loss is
the sum of the individual loss values divided by 3.
r   r   r   )r)  r*  r   r5  r5   r6   r   r   r   rd   s         r   '_ragged_tensor_categorical_crossentropyr    s/    < 
		 '	
B %R88r   z,keras.metrics.categorical_focal_crossentropyz+keras.losses.categorical_focal_crossentropyc           	      (  ^ ^^ [        U[        5      (       a  [        SU S[        U5       35      e[        R
                  " T5      m[        R                  " T TR                  5      m [        R
                  " TTR                  S9mTR                  S   S:X  a(  [        R                  " STR                   S3[        SS	9  UUU 4S
 jn[        R                  R                  R                  TUU 4S j5      m [        R                  " T TUUUUS9$ )a  Computes the categorical focal crossentropy loss.

Standalone usage:
>>> y_true = [[0, 1, 0], [0, 0, 1]]
>>> y_pred = [[0.05, 0.9, 0.05], [0.1, 0.85, 0.05]]
>>> loss = tf.keras.losses.categorical_focal_crossentropy(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> loss.numpy()
array([2.63401289e-04, 6.75912094e-01], dtype=float32)

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.
ro  rp  ra  r   r!  zIn loss categorical_focal_crossentropy, expected y_pred.shape to be (batch_size, num_classes) with num_classes > 1. Received: y_pred.shape=rq  rr  rs  c                     > [         R                  " [         R                  " T5      S   TR                  5      n TST-
  -  TU -  -   $ )Nr   r   rv  )rw  r   r6   r5   s    r   rx  6categorical_focal_crossentropy.<locals>._smooth_labels	  sD    ggbhhv.r2FLLA./k)
 	
r   c                     > T $ rT   rA   rK  s   r   r  0categorical_focal_crossentropy.<locals>.<lambda>"	  r|  r   )targetoutputr   r   r   r   )r  r}  rR   r~  r*   r   r   r   r"  r  r  r  r-   rQ  r   r   )r5   r6   r   r   r   r   r   rx  s   ``   `  r   r   r     s   ^ $"V9T$ZL:
 	
 !!&)FWWVV\\*F**?&,,OO||B1<<BLL> JOO 	

 __''22F 11 r   c           	      T    [         R                  " [        UUUUUS9n[        XpU5      $ )a  Implements support for handling RaggedTensors.

Expected shape: (batch, sequence_len, n_classes) with sequence_len
being variable per batch.
Return shape: (batch, sequence_len).
When used by CategoricalFocalCrossentropy() with the default reduction
(SUM_OVER_BATCH_SIZE), the reduction averages the loss over the
number of elements independent of the batch. E.g. if the RaggedTensor
has 2 batches with [2, 1] values respectively the resulting loss is
the sum of the individual loss values divided by 3.

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 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.
)r   r   r   r   r   )r)  r*  r   r5  )r5   r6   r   r   r   r   r   rd   s           r   -_ragged_tensor_categorical_focal_crossentropyr  /	  s6    R 
		&'
B %R88r   z-keras.metrics.sparse_categorical_crossentropyz,keras.losses.sparse_categorical_crossentropyc                 2    [         R                  " U UUUUS9$ )a*  Computes the sparse categorical crossentropy loss.

Standalone usage:

>>> y_true = [1, 2]
>>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
>>> loss = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> loss.numpy()
array([0.0513, 2.303], dtype=float32)

>>> y_true = [[[ 0,  2],
...            [-1, -1]],
...           [[ 0,  2],
...            [-1, -1]]]
>>> y_pred = [[[[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]],
...             [[0.2, 0.5, 0.3], [0.0, 1.0, 0.0]]],
...           [[[1.0, 0.0, 0.0], [0.0, 0.5, 0.5]],
...            [[0.2, 0.5, 0.3], [0.0, 1.0, 0.0]]]]
>>> loss = tf.keras.losses.sparse_categorical_crossentropy(
...   y_true, y_pred, ignore_class=-1)
>>> loss.numpy()
array([[[2.3841855e-07, 2.3841855e-07],
        [0.0000000e+00, 0.0000000e+00]],
       [[2.3841855e-07, 6.9314730e-01],
        [0.0000000e+00, 0.0000000e+00]]], dtype=float32)

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.
    axis: Defaults to -1. The dimension along which the entropy is
        computed.
    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.

Returns:
    Sparse categorical crossentropy loss value.
r   r   r   )r   r   )r5   r6   r   r   r   s        r   r   r   c	  s'    f 22! r   c                 N    [         R                  " [        UUUS9n[        XPUSS9$ )a  Implements support for handling RaggedTensors.

Expected y_pred shape: (batch, sequence_len, n_classes) with sequence_len
being variable per batch.
Return shape: (batch, sequence_len).

When used by SparseCategoricalCrossentropy() with the default reduction
(SUM_OVER_BATCH_SIZE), the reduction averages the loss over the
number of elements independent of the batch. E.g. if the RaggedTensor
has 2 batches with [2, 1] values respectively, the resulting loss is
the sum of the individual loss values divided by 3.
r  T)r.  )r)  r*  r   r5  )r5   r6   r   r   r   rd   s         r   ._ragged_tensor_sparse_categorical_crossentropyr  	  s1      
		'!	
B %R$OOr   z!keras.metrics.binary_crossentropyz keras.losses.binary_crossentropyc                 n  ^ ^ [         R                  " U5      n[         R                  " T UR                  5      m [         R                  " TUR                  S9mUU 4S jn[         R                  R
                  R                  TUU 4S j5      m [        R                  " [        R                  " T XS9US9$ )a  Computes the binary crossentropy loss.

Standalone usage:

>>> y_true = [[0, 1], [0, 0]]
>>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
>>> loss = tf.keras.losses.binary_crossentropy(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> loss.numpy()
array([0.916 , 0.714], dtype=float32)

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]`.
ra  c                      > TST -
  -  ST -  -   $ Nr   rb  rA   r   r5   s   r   rx  +binary_crossentropy.<locals>._smooth_labels	      ./#2GGGr   c                     > T $ rT   rA   rK  s   r   r  %binary_crossentropy.<locals>.<lambda>	  r|  r   r   r   )	r*   r   r   r   r-   rQ  r   r   r   r  s   `  `  r   r   r   	  s    @ !!&)FWWVV\\*F**?&,,OOH __''22F <<##FFL r   c                 P    [         R                  " [        UUUS9n[        XPU5      $ )a  Implements support for handling RaggedTensors.

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: Axis along which to compute crossentropy.

Returns:
    Binary crossentropy loss value.

Expected shape: (batch, sequence_len) with sequence_len being variable
per batch.
Return shape: (batch,); returns the per batch mean of the loss values.

When used by BinaryCrossentropy() with the default reduction
(SUM_OVER_BATCH_SIZE), the reduction averages the per batch losses over
the number of batches.
r  )r)  r*  r   r5  r  s         r   "_ragged_tensor_binary_crossentropyr  	  s/    6 
		'	
B %R88r   z'keras.metrics.binary_focal_crossentropyz&keras.losses.binary_focal_crossentropyc                 v  ^ ^ [         R                  " U5      n[         R                  " T UR                  5      m [         R                  " TUR                  S9mUU 4S jn[         R                  R
                  R                  TUU 4S j5      m [        R                  " [        R                  " T UUUUUS9US9$ )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]`.

Standalone usage:

>>> y_true = [[0, 1], [0, 0]]
>>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
>>> loss = tf.keras.losses.binary_focal_crossentropy(y_true, y_pred,
...                                                  gamma=2)
>>> assert loss.shape == (2,)
>>> loss.numpy()
array([0.330, 0.206], dtype=float32)

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 higher than 0 then smooth the
        labels by squeezing them towards `0.5`, i.e., 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.
        shape = `[batch_size, d0, .. dN-1]`.
ra  c                      > TST -
  -  ST -  -   $ r  rA   r  s   r   rx  1binary_focal_crossentropy.<locals>._smooth_labelsQ
  r  r   c                     > T $ rT   rA   rK  s   r   r  +binary_focal_crossentropy.<locals>.<lambda>U
  r|  r   )r  r  r   r   r   r   r   )	r*   r   r   r   r-   rQ  r   r   r   )	r5   r6   r   r   r   r   r   r   rx  s	   `     `  r   r   r   
  s    @ !!&)FWWVV\\*F**?&,,OOH __''22F <<))"7#	
 
 
r   c           
      V    [         R                  " [        UUUUUUS9n[        XU5      $ )a:  Implements support for handling RaggedTensors.

Expected shape: `(batch, sequence_len)` with sequence_len being variable per
batch.
Return shape: `(batch,)`; returns the per batch mean of the loss values.

When used by BinaryFocalCrossentropy() with the default reduction
(SUM_OVER_BATCH_SIZE), the reduction averages the per batch losses over
the number of batches.

Args:
    y_true: Tensor of one-hot true targets.
    y_pred: Tensor of predicted targets.
    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 [Lin et al., 2018](
        https://arxiv.org/pdf/1708.02002.pdf). 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. 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: Axis along which to compute crossentropy.

Returns:
    Binary focal crossentropy loss value.
)r   r   r   r   r   r   )r)  r*  r   r5  )	r5   r6   r   r   r   r   r   r   rd   s	            r   (_ragged_tensor_binary_focal_crossentropyr  e
  s9    T 
		!3'
B %R88r   zkeras.metrics.kl_divergencez)keras.metrics.kullback_leibler_divergencezkeras.metrics.kldzkeras.metrics.KLDzkeras.losses.kl_divergencez(keras.losses.kullback_leibler_divergencezkeras.losses.kldzkeras.losses.KLDc                    [         R                  " U5      n[         R                  " XR                  5      n [        R
                  " U [        R                  " 5       S5      n [        R
                  " U[        R                  " 5       S5      n[         R                  " U [         R                  R                  X-  5      -  SS9$ )a.  Computes Kullback-Leibler divergence loss between `y_true` & `y_pred`.

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

See: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence

Standalone usage:

>>> y_true = np.random.randint(0, 2, size=(2, 3)).astype(np.float64)
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.kullback_leibler_divergence(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> y_true = tf.keras.backend.clip(y_true, 1e-7, 1)
>>> y_pred = tf.keras.backend.clip(y_pred, 1e-7, 1)
>>> assert np.array_equal(
...     loss.numpy(), np.sum(y_true * np.log(y_true / y_pred), axis=-1))

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

Returns:
    A `Tensor` with loss.

Raises:
    TypeError: If `y_true` cannot be cast to the `y_pred.dtype`.
r!  r   r   )
r*   r   r   r   r   clipr>  r[  r   rD  r   s     r   r   r   
  s~    N !!&)FWWV\\*F\\&'//"3Q7F\\&'//"3Q7F=="''++fo">>RHHr   zkeras.metrics.poissonzkeras.losses.poissonc           	         [         R                  " U5      n[         R                  " XR                  5      n [        R
                  " X[         R                  R                  U[        R                  " 5       -   5      -  -
  SS9$ )a  Computes the Poisson loss between y_true and y_pred.

The Poisson loss is the mean of the elements of the `Tensor`
`y_pred - y_true * log(y_pred)`.

Standalone usage:

>>> y_true = np.random.randint(0, 2, size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.poisson(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> y_pred = y_pred + 1e-7
>>> assert np.allclose(
...     loss.numpy(), np.mean(y_pred - y_true * np.log(y_pred), axis=-1),
...     atol=1e-5)

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

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

Raises:
    InvalidArgumentError: If `y_true` and `y_pred` have incompatible shapes.
r   r   )	r*   r   r   r   r   r   r   rD  r>  r   s     r   r   r   
  s]    : !!&)FWWV\\*F<<"''++fw/@&@AAA r   keras.losses.cosine_similarity)zkeras.metrics.cosine_proximityzkeras.metrics.cosinezkeras.losses.cosine_proximityzkeras.losses.cosiner  c                     [         R                  R                  XS9n [         R                  R                  XS9n[         R                  " X-  US9* $ )a  Computes the cosine similarity between labels and predictions.

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. The values closer to 1 indicate greater
dissimilarity. 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.

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

Standalone usage:

>>> y_true = [[0., 1.], [1., 1.], [1., 1.]]
>>> y_pred = [[1., 0.], [1., 1.], [-1., -1.]]
>>> loss = tf.keras.losses.cosine_similarity(y_true, y_pred, axis=1)
>>> loss.numpy()
array([-0., -0.999, 0.999], dtype=float32)

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

Returns:
    Cosine similarity tensor.
r   )r*   linalgl2_normalizer[  )r5   r6   r   s      r   r   r   
  sI    R YY##F#6FYY##F#6FMM&/555r   c                     [        U [        5      =(       d_    [        U [        5      =(       a    U R                  [        :H  =(       d.    [        U S5      =(       a    U R                  S:H  =(       d    U S:H  nU$ )Nr"   r   )r  r   r`   rd   r   hasattrr"   )lossresults     r   is_categorical_crossentropyr  '  sn    401 		0t01 433		0 D*% <!;;		0 ..  Mr   zkeras.losses.serializec                     U c  g[        U [        5      (       d#  [        R                  " S[	        U 5       S35        U(       a  [
        R                  " U 5      $ [        U 5      $ )a  Serializes loss function or `Loss` instance.

Args:
    loss: A TF-Keras `Loss` instance or a loss function.
    use_legacy_format: Boolean, whether to use the legacy serialization
        format. Defaults to `False`.

Returns:
    Loss configuration dictionary.
NzzThe `keras.losses.serialize()` API should only be used for objects of type `keras.losses.Loss`. Found an instance of type z+, which may lead to improper serialization.)r  r   r  r  r~  legacy_serializationr   )r  use_legacy_formats     r   	serializer  7  s^     |dD!!NDzlEG	

 #::4@@!$''r   zkeras.losses.deserializec                 v    U(       a  [         R                  " U [        5       USS9$ [        U [        5       USS9$ )a  Deserializes a serialized loss class/function instance.

Args:
    name: Loss configuration.
    custom_objects: Optional dictionary mapping names (strings) to custom
        objects (classes and functions) to be considered during
        deserialization.
    use_legacy_format: Boolean, whether to use the legacy serialization
        format. Defaults to `False`.

Returns:
    A TF-Keras `Loss` instance or a loss function.
zloss function)module_objectscustom_objectsprintable_module_name)r  r   globals)r   r  r  s      r   deserializer  P  sE     #<<"9)"1	
 	
 $y%-	 r   zkeras.losses.getc                     U c  g[        U [        5      (       a  [        U 5      n SU ;  n[        XS9$ [        U [        5      (       a  [        U 5      $ [	        U 5      (       a  U $ [        SU  35      e)aB  Retrieves a TF-Keras loss as a `function`/`Loss` class instance.

The `identifier` may be the string name of a loss function or `Loss` class.

>>> loss = tf.keras.losses.get("categorical_crossentropy")
>>> type(loss)
<class 'function'>
>>> loss = tf.keras.losses.get("CategoricalCrossentropy")
>>> type(loss)
<class '...keras.losses.CategoricalCrossentropy'>

You can also specify `config` of the loss to this function by passing dict
containing `class_name` and `config` as an identifier. Also note that the
`class_name` must map to a `Loss` class

>>> identifier = {"class_name": "CategoricalCrossentropy",
...               "config": {"from_logits": True}}
>>> loss = tf.keras.losses.get(identifier)
>>> type(loss)
<class '...keras.losses.CategoricalCrossentropy'>

Args:
    identifier: A loss identifier. One of None or string name of a loss
        function/class or loss configuration dictionary or a loss function
        or a loss class instance.

Returns:
    A TF-Keras loss as a `function`/ `Loss` class instance.

Raises:
    ValueError: If `identifier` cannot be interpreted.
Nmodule)r  z.Could not interpret loss function identifier: )r  strr  rt   callablerR   )
identifierr  s     r   r|   r|   n  sz    D *c""_
$J6:KK*d##:&&


8E r   int32)F)r   )Fr   r   )r   r   Fr   r   )Fr   N)Fr   r   Fr   r   )r   )NF)hrX   rZ   r)  r  tensorflow.compat.v2compatv2r*   tf_keras.srcr   tf_keras.src.savingr   tf_keras.src.saving.legacyr   r  %tf_keras.src.saving.serialization_libr   r   rs   r   r	   tensorflow.python.ops.raggedr
   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   tensorflow.tools.docsr   r   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   add_dispatch_supportr   r5  dispatch_for_typesr	  r7  r   r;  r   rA  r   rH  rV  r   r   r   r   r   r   r  r   r  r   r  r   r  r   r  r   r   r   bceBCEmseMSEmaeMAEmapeMAPEmsleMSLEkldKLDkullback_leibler_divergencelogcoshr   r  r  r  r|   r^   r:   sparse_softmax_cross_entropyLABEL_DTYPES_FOR_LOSSESrA   r   r   <module>r     s	        ! !   * L J H + ' 8 4 + 9 . !"r r #rj ="EM$ M FM` -.8M* 8M /8Mv ./:N+ :N 0:Nz 89B
"5 B
 :B
J 89=
"5 =
 :=
@ /0i', i' 1i'X 45jF1 jF 6jFZ 45U
1 U
 6U
p 9:HF#6 HF ;HFV :;U
$7 U
 <U
p -.I
* I
 /I
X "#8@ 8@ $8@v )*:H& :H +:Hz -.9L* 9L /9Lx $%5B! 5B &5Bp $%8C! 8C &8Cv )*:H& :H +:Hz "#BM BM $BMJ &% ..M /M:VQr 
/AI BI" '& ..: /:4 
0"//BJ CJ
 21 ../ //@ 
;R__M N 21 ..  / F 
;R__M N  +-IJ.. / K@ #%9:..I / ;I< ./..- / 0-B "r*..! / +!H 	 ..$< /$<N ,+ ..AC; /	
;| 
5rGAC#9 H#9L 21 .. 
	N /	
Nb 
;R__M 
	09 N09f 32 ..=A4 /	
4n 
<booN=AP OP0 ')K ..AC* /*Z 
0"//BAC 9 C 9F -, ..  

	P /	
Pf 
6H  

	29 I29j !/ .	 .. I /	 IF %'=>.. / ?D $	 .. 6 /	 6J   c c c, ,t, ,t*7 7 7c'

  &'( ((0 () *:  !- "-b IILL44g#W r   