
    AVh              	          d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  edg       G d d             Zd Zd5dZd Z edg      ej8                  ddej:                  j<                  ej>                  fd              Z  edg      ej8                  ddej:                  j<                  ej>                  fd              Z! edg      ej8                   edd d!      dddej:                  j<                  ej>                  dfd"                     Z" ed#g      ej8                  ddej:                  j<                  ej>                  fd$              Z# ed%g      ej8                  dddej:                  j<                  ej>                  fd&              Z$ ed'g      ej8                  dd(dej:                  j<                  ej>                  fd)              Z% ed*g      ej8                  ddej:                  j<                  fd+              Z& ed,g      ej8                  ddej:                  j<                  ej>                  fd-              Z' ed.g      ej8                  dddej:                  j<                  ej>                  fd/              Z( ed0g      ej8                  dddej:                  j<                  ej>                  fd1              Z)	 d6d2Z* ed3g      ej8                  ddej:                  j<                  ej>                  fd4              Z+y)7z=Implementation of Loss operations for use in neural networks.    )context)dtypes)ops)	array_ops)cond)confusion_matrix)control_flow_ops)math_ops)nn)nn_ops)weights_broadcast_ops)util)dispatch)deprecated_args)deprecated_argument_lookup)	tf_exportzlosses.Reduction)v1c                   H    e Zd ZdZdZdZdZdZdZeZ	e
d        Ze
d        Zy	)
	Reductiona  Types of loss reduction.

  Contains the following values:

  * `NONE`: Un-reduced weighted losses with the same shape as input.
  * `SUM`: Scalar sum of weighted losses.
  * `MEAN`: Scalar `SUM` divided by sum of weights. DEPRECATED.
  * `SUM_OVER_BATCH_SIZE`: Scalar `SUM` divided by number of elements in losses.
  * `SUM_OVER_NONZERO_WEIGHTS`: Scalar `SUM` divided by number of non-zero
     weights. DEPRECATED.
  * `SUM_BY_NONZERO_WEIGHTS`: Same as `SUM_OVER_NONZERO_WEIGHTS`. DEPRECATED.
  noneweighted_sumweighted_sum_over_batch_sizeweighted_meanweighted_sum_by_nonzero_weightsc                     | j                   | j                  | j                  | j                  | j                  | j
                  fS N)NONESUMMEANSUM_OVER_BATCH_SIZESUM_OVER_NONZERO_WEIGHTSSUM_BY_NONZERO_WEIGHTS)clss    X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/losses/losses_impl.pyallzReduction.all9   s>     	$$""$ $    c                 h    || j                         vr t        d| d| j                          d      y )NzInvalid Reduction Key z. Key should be one of .)r%   
ValueError)r#   keys     r$   validatezReduction.validateC   s>    
#'')/u4K'')A' ( ( r&   N)__name__
__module____qualname____doc__r   r   r    r   r"   r!   classmethodr%   r+    r&   r$   r   r   #   sP     
$#6	$<3$ $ ( (r&   r   c                 \    t        j                  |       }t        j                  ||d      S )a,  Computes a safe mean of the losses.

  Args:
    losses: `Tensor` whose elements contain individual loss measurements.
    num_present: The number of measurable elements in `losses`.

  Returns:
    A scalar representing the mean of `losses`. If `num_present` is zero,
      then zero is returned.
  valuename)r
   
reduce_sum
div_no_nan)lossesnum_present
total_losss      r$   
_safe_meanr;   J   s)     ""6**			Z7	CCr&   c           
         t        |t              r|dk7  s=t        j                         r4|j	                         dk(  r!t        j                  |d      st        |       S t        j                  dd| |f      5 }t        j                  |t        j                        }t        j                  t        j                  |d      t        j                  |      t        j                   |            }t#        j$                  ||       }|rIt        j&                  |t        j(                  dt        j*                  |            d|      cddd       S t        j&                  ||	      cddd       S # 1 sw Y   yxY w)
a  Computes the number of elements in the loss function induced by `weights`.

  A given weights tensor induces different numbers of usable elements in the
  `losses` tensor. The `weights` tensor is broadcast across `losses` for all
  possible dimensions. For example, if `losses` is a tensor of dimension
  `[4, 5, 6, 3]` and `weights` is a tensor of shape `[4, 5]`, then `weights` is,
  in effect, tiled to match the shape of `losses`. Following this effective
  tile, the total number of present elements is the number of non-zero weights.

  Args:
    losses: `Tensor` of shape `[batch_size, d1, ... dN]`.
    weights: `Tensor` of shape `[]`, `[batch_size]` or
      `[batch_size, d1, ... dK]`, where K < N.
    per_batch: Whether to return the number of elements per batch or as a sum
      total.

  Returns:
    The number of present (non-zero) elements in the losses tensor. If
      `per_batch` is `True`, the value is returned as a tensor of size
      `[batch_size]`. Otherwise, a single scalar tensor is returned.
  g        r   Nr9   dtype   T)axiskeepdimsr5   r4   )
isinstancefloatr   executing_eagerly_rankr
   equal_num_elementsr   
name_scopecastr   float32r   where
zeros_like	ones_liker   broadcast_weightsr6   rangerank)r8   weights	per_batchscopepresents        r$   _num_presentrU   Y   s   , '5!gn  "w}}!';~~gs+  
~~dMFG+<= 4mmG6>>:Goow$W%G$&G $55gvFG  
~~a!89	4 4 wU34 4 4s   7CE/E//E8c                     t        j                  dd| g      5 }t        j                  t	        j
                  | |      | j                        cddd       S # 1 sw Y   yxY w)z3Computes the number of elements in `losses` tensor.Nnum_elements)valuesr4   r=   )r   rH   r
   rI   r   sizer>   )r8   rS   s     r$   rG   rG      sL    
~~dNF8< Q==U;6<<PQ Q Qs   6AA#zlosses.compute_weighted_loss      ?Nc                    t         j                  |       t        j                  |d| |f      5  |t        j                         _        d }t        j                          || |||      cddd       S t        j                  t        j                  ||       f      5   || |||      cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)a  Computes the weighted loss.

  Args:
    losses: `Tensor` of shape `[batch_size, d1, ... dN]`.
    weights: Optional `Tensor` whose rank is either 0, or the same rank as
      `losses`, and must be broadcastable to `losses` (i.e., all dimensions must
      be either `1`, or the same as the corresponding `losses` dimension).
    scope: the scope for the operations performed in computing the loss.
    loss_collection: the loss will be added to these collections.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss `Tensor` of the same type as `losses`. If `reduction` is
    `NONE`, this has the same shape as `losses`; otherwise, it is scalar.

  Raises:
    ValueError: If `weights` is `None` or the shape is not compatible with
      `losses`, or if the number of dimensions (rank) of either `losses` or
      `weights` is missing.

  Note:
    When calculating the gradient of a weighted loss contributions from
    both `losses` and `weights` are considered. If your `weights` depend
    on some model parameters but you do not want this to affect the loss
    gradient, you need to apply `tf.stop_gradient` to `weights` before
    passing them to `compute_weighted_loss`.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  weighted_lossc                    t        j                  |       } | j                  }t        j                  | t
        j                        } t        j                  |t
        j                        }t        j                  | |      }|t        j                  k(  r|}nt        j                  |      }|t        j                  k(  r6t        |t        j                  t        j                  |       |z              }ne|t        j                  k(  s|t        j                   k(  rt        |t#        | |            }n(|t        j$                  k(  rt        |t'        |             }t        j                  ||      }t)        j*                  ||       |S )Nr=   )r   convert_to_tensorr>   r
   rI   r   rJ   multiplyr   r   r6   r   r;   r   rM   r"   r!   rU   r    rG   r   add_loss)r8   rQ   loss_collection	reductioninput_dtypeweighted_losseslosss          r$   compute_lossz+compute_weighted_loss.<locals>.compute_loss   s    $$V,fLLk}}V6>>:fgV^^<g ))&':o	inn	$""?3	&H''	(;(;F(Cg(MNP$9;;;9===D,vw"?@$)777D-"78$ ]]4-d
mmD/*kr&   N)r   r+   r   rH   get_default_graph_last_loss_reductionr	   get_enclosing_xla_contextcontrol_dependenciesr   assert_broadcastable)r8   rQ   rS   ra   rb   rf   s         r$   compute_weighted_lossrl      s    J Y
~~e_vw.?@ &I 4=C08 113?&'?IFE&I &IH ## 55gvF
HJ IFG_iHI II&I &IHI I II&I &I &Is)   ;C4*CB<)	C<C	CCzlosses.absolute_differencec                    | t        d      |t        d      t        j                  |d|| |f      5 }t        j                  |t
        j                        }t        j                  | t
        j                        } |j                         j                  | j                                t        j                  t        j                  ||             }t        |||||      cddd       S # 1 sw Y   yxY w)aE  Adds an Absolute Difference loss to the training procedure.

  `weights` acts as a coefficient for the loss. If a scalar is provided, then
  the loss is simply scaled by the given value. If `weights` is a `Tensor` of
  shape `[batch_size]`, then the total loss for each sample of the batch is
  rescaled by the corresponding element in the `weights` vector. If the shape of
  `weights` matches the shape of `predictions`, then the loss of each
  measurable element of `predictions` is scaled by the corresponding value of
  `weights`.

  Args:
    labels: The ground truth output tensor, same dimensions as 'predictions'.
    predictions: The predicted outputs.
    weights: Optional `Tensor` whose rank is either 0, or the same rank as
      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
      be either `1`, or the same as the corresponding `losses` dimension).
    scope: The scope for the operations performed in computing the loss.
    loss_collection: collection to which this loss will be added.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
    shape as `labels`; otherwise, it is scalar.

  Raises:
    ValueError: If the shape of `predictions` doesn't match that of
      `labels` or if the shape of `weights` is invalid or if `labels`
      or `predictions` is None.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  N#Argument `labels` must not be None.(Argument `predictions` must not be None.absolute_differencer=   rb   )r)   r   rH   r
   rI   r   rJ   	get_shapeassert_is_compatible_withabssubtractrl   labelspredictionsrQ   rS   ra   rb   r8   s          r$   rp   rp      s    P ^
:
;;
?
@@
~~e2"FG46 F9>--6>>BK]]68F55f6F6F6HI\\(++K@AF 9FF F Fs   B0C//C8zlosses.cosine_distancez#dim is deprecated, use axis insteaddimc                 >   t        d|d|      }|t        d      | t        d      |t        d      t        j                  |d|| |f      5 }t	        j
                  |t        j                        }t	        j
                  | t        j                        } |j                         j                  | j                                t	        j                  ||       }d	t	        j                  ||fd
      z
  }	t        |	||||      cddd       S # 1 sw Y   yxY w)a  Adds a cosine-distance loss to the training procedure.

  Note that the function assumes that `predictions` and `labels` are already
  unit-normalized.

  Args:
    labels: `Tensor` whose shape matches 'predictions'
    predictions: An arbitrary matrix.
    axis: The dimension along which the cosine distance is computed.
    weights: Optional `Tensor` whose rank is either 0, or the same rank as
      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
      be either `1`, or the same as the corresponding `losses` dimension).
    scope: The scope for the operations performed in computing the loss.
    loss_collection: collection to which this loss will be added.
    reduction: Type of reduction to apply to loss.
    dim: The old (deprecated) name for `axis`.

  Returns:
    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
    shape as `labels`; otherwise, it is scalar.

  Raises:
    ValueError: If `predictions` shape doesn't match `labels` shape, or
      `axis`, `labels`, `predictions` or `weights` is `None`.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  r@   ry   Nz!You must specify argument `axis`.rn   ro   cosine_distance_lossr=   r?   Tr@   rA   rq   )r   r)   r   rH   r
   rI   r   rJ   rr   rs   r_   r6   rl   )
rw   rx   r@   rQ   rS   ra   rb   ry   radial_diffsr8   s
             r$   cosine_distancer~     s	   L 
$FD%	=$	\
8
99^
:
;;
?
@@
~~e3"FG46 	F9>--6>>BK]]68F55f6F6F6HI$$[&9L$$\$OOF 9F	F 	F 	Fs   B9DDzlosses.hinge_lossc           
      z   | t        d      |t        d      t        j                  |d|| |f      5 }t        j                  |t
        j                        }t        j                  | t
        j                        } |j                         j                  | j                                t        j                  |       }t        j                  d| z  |      } t        j                  t        j                  |t        j                  | |                  }t        |||||      cddd       S # 1 sw Y   yxY w)a^  Adds a hinge loss to the training procedure.

  Args:
    labels: The ground truth output tensor. Its shape should match the shape of
      logits. The values of the tensor are expected to be 0.0 or 1.0. Internally
      the {0,1} labels are converted to {-1,1} when calculating the hinge loss.
    logits: The logits, a float tensor. Note that logits are assumed to be
      unbounded and 0-centered. A value > 0 (resp. < 0) is considered a positive
      (resp. negative) binary prediction.
    weights: Optional `Tensor` whose rank is either 0, or the same rank as
      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
      be either `1`, or the same as the corresponding `losses` dimension).
    scope: The scope for the operations performed in computing the loss.
    loss_collection: collection to which the loss will be added.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
    shape as `labels`; otherwise, it is scalar.

  Raises:
    ValueError: If the shapes of `logits` and `labels` don't match or
      if `labels` or `logits` is None.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  Nrn   #Argument `logits` must not be None.
hinge_lossr=      rq   )r)   r   rH   r
   rI   r   rJ   rr   rs   r   rM   ru   r   relur_   rl   )rw   logitsrQ   rS   ra   rb   all_onesr8   s           r$   r   r   G  s   D ^
:
;;^
:
;;
~~e\FFG+DE 
F]]68F]]68F
001A1A1CD""6*Hq6z84F[[(H$5$5ff$EFHF 9F
F 
F 
Fs   C2D11D:zlosses.huber_lossc           
      4   | t        d      |t        d      t        j                  |d|| |f      5 }t        j                  |t
        j                        }t        j                  | t
        j                        } |j                         j                  | j                                t        j                  ||       }t        j                  |      }t        j                  ||      }	t        j                  ||	      }
t        j                  t        j                  t        j                  d|	j                        t        j                  |	|	            t        j                  ||
            }t!        |||||      cddd       S # 1 sw Y   yxY w)a  Adds a [Huber Loss](https://en.wikipedia.org/wiki/Huber_loss) term to the training procedure.

  For each value x in `error=labels-predictions`, the following is calculated:

  ```
    0.5 * x^2                  if |x| <= d
    0.5 * d^2 + d * (|x| - d)  if |x| > d
  ```

  where d is `delta`.

  `weights` acts as a coefficient for the loss. If a scalar is provided, then
  the loss is simply scaled by the given value. If `weights` 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 `weights` vector. If the shape of
  `weights` matches the shape of `predictions`, then the loss of each
  measurable element of `predictions` is scaled by the corresponding value of
  `weights`.

  Args:
    labels: The ground truth output tensor, same dimensions as 'predictions'.
    predictions: The predicted outputs.
    weights: Optional `Tensor` whose rank is either 0, or the same rank as
      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
      be either `1`, or the same as the corresponding `losses` dimension).
    delta: `float`, the point where the huber loss function changes from a
      quadratic to linear.
    scope: The scope for the operations performed in computing the loss.
    loss_collection: collection to which the loss will be added.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
    shape as `labels`; otherwise, it is scalar.

  Raises:
    ValueError: If the shape of `predictions` doesn't match that of `labels` or
      if the shape of `weights` is invalid.  Also if `labels` or
     `predictions` is None.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  Nrn   ro   
huber_lossr=         ?rq   )r)   r   rH   r
   rI   r   rJ   rr   rs   ru   rt   minimumaddr_   r^   r>   rl   )rw   rx   rQ   deltarS   ra   rb   error	abs_error	quadraticlinearr8   s               r$   r   r   z  sT   d ^
:
;;
?
@@
~~e\"FG46 F9>--6>>BK]]68F55f6F6F6HIk62EU#I  E2I y)4F\\!!#Y__=i3	5 	%(	*F
 !9F'F F Fs   EFFzlosses.log_lossgHz>c           	      d   | t        d      |t        d      t        j                  |d|| |f      5 }t        j                  |t
        j                        }t        j                  | t
        j                        } |j                         j                  | j                                t        j                  | t        j                  ||z                t        j                  d| z
  t        j                  d|z
  |z               z
  }t        |||||      cddd       S # 1 sw Y   yxY w)a~  Adds a Log Loss term to the training procedure.

  `weights` acts as a coefficient for the loss. If a scalar is provided, then
  the loss is simply scaled by the given value. If `weights` 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 `weights` vector. If the shape of
  `weights` matches the shape of `predictions`, then the loss of each
  measurable element of `predictions` is scaled by the corresponding value of
  `weights`.

  Args:
    labels: The ground truth output tensor, same dimensions as 'predictions'.
    predictions: The predicted outputs.
    weights: Optional `Tensor` whose rank is either 0, or the same rank as
      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
      be either `1`, or the same as the corresponding `losses` dimension).
    epsilon: A small increment to add to avoid taking a log of zero.
    scope: The scope for the operations performed in computing the loss.
    loss_collection: collection to which the loss will be added.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
    shape as `labels`; otherwise, it is scalar.

  Raises:
    ValueError: If the shape of `predictions` doesn't match that of `labels` or
      if the shape of `weights` is invalid.  Also if `labels` or `predictions`
      is None.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  Nrn   ro   log_lossr=   r?   rq   )r)   r   rH   r
   rI   r   rJ   rr   rs   r_   logrl   )rw   rx   rQ   epsilonrS   ra   rb   r8   s           r$   r   r     s   P ^
:
;;
?
@@
~~eZ"FG46 
F9>--6>>BK]]68F55f6F6F6HI[7*+- -/7/@/@Z(,,q;'@A0CCF !9F
F 
F 
Fs   C'D&&D/z"losses.mean_pairwise_squared_errorc                 *   | t        d      |t        d      t        j                  |d|| |f      5 }t        j                  |t
        j                        }t        j                  | t
        j                        } d }t        j                          || |||      cddd       S t        j                  t        j                  ||       f      5   || |||      cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)a  Adds a pairwise-errors-squared loss to the training procedure.

  Unlike `mean_squared_error`, which is a measure of the differences between
  corresponding elements of `predictions` and `labels`,
  `mean_pairwise_squared_error` is a measure of the differences between pairs of
  corresponding elements of `predictions` and `labels`.

  For example, if `labels`=[a, b, c] and `predictions`=[x, y, z], there are
  three pairs of differences are summed to compute the loss:
    loss = [ ((a-b) - (x-y)).^2 + ((a-c) - (x-z)).^2 + ((b-c) - (y-z)).^2 ] / 3

  Note that since the inputs are of shape `[batch_size, d0, ... dN]`, the
  corresponding pairs are computed within each batch sample but not across
  samples within a batch. For example, if `predictions` represents a batch of
  16 grayscale images of dimension [batch_size, 100, 200], then the set of pairs
  is drawn from each image, but not across images.

  `weights` acts as a coefficient for the loss. If a scalar is provided, then
  the loss is simply scaled by the given value. If `weights` 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 `weights` vector.

  Args:
    labels: The ground truth output tensor, whose shape must match the shape of
      `predictions`.
    predictions: The predicted outputs, a tensor of size
      `[batch_size, d0, .. dN]` where N+1 is the total number of dimensions in
      `predictions`.
    weights: Coefficients for the loss a scalar, a tensor of shape
      `[batch_size]` or a tensor whose shape matches `predictions`.
    scope: The scope for the operations performed in computing the loss.
    loss_collection: collection to which the loss will be added.

  Returns:
    A scalar `Tensor` that returns the weighted loss.

  Raises:
    ValueError: If the shape of `predictions` doesn't match that of `labels` or
      if the shape of `weights` is invalid.  Also if `labels` or `predictions`
      is None.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  Nrn   ro   mean_pairwise_squared_errorr=   c                    t        j                  |t        j                        }|j	                         j                  | j	                                t        j                  ||       }t        j                  dt        j                  |            }t        j                  t        j                  |      |d      }t        ||d      }dt        j                  |t        j                  |dz
  d      d	      z  }t        j                  ||d      }	dt        j                  t        j                  |	      t        j                  t        j                  ||dz
        d      d	      z  }
t        j                  ||
z
  |      }t        j                  |      }t        j                   t        j                  |      dkD  |t        j"                  |      d	      }t%        j&                  ||       |S )
Nr=   r?   Tr|   )rR   g       @r   r3   r4   )r
   rI   r   rJ   rr   rs   ru   rO   r   rP   r6   squarerU   r7   maximumr_   rK   rL   r   r`   )rw   rx   rQ   ra   diffsr@   sum_squares_diff_per_batchnum_present_per_batchterm1sum_diffterm2rd   re   	mean_losss                 r$   rf   z1mean_pairwise_squared_error.<locals>.compute_loss=  s   MM+V^^Dk778H8H8JKV4e^^Ay~~e45d#+#6#6
//%
 td$< *5'TJH''
$


014a
8 e
 $$UEhH''
//(
#


 5 5 9;<=?  e !))%%-Ao  1d//


3
4q
8



t
$	i
 mmI/r&   )r)   r   rH   r
   rI   r   rJ   r	   ri   rj   r   rk   )rw   rx   rQ   rS   ra   rf   s         r$   r   r     s    f ^
:
;;
?
@@
~~e:"FG46 2K9>mmG6>>:G]]68F"P 113?&+wH]2K 2K` ## 55gvF
HJ KFK/JK Ka2K 2K`K K Ka2K 2K 2Ks*   A,D	+*D	C3 	D	3C<	8D		Dzlosses.mean_squared_errorc                    | t        d      |t        d      t        j                  |d|| |f      5 }t        j                  |t
        j                        }t        j                  | t
        j                        } |j                         j                  | j                                t        j                  ||       }t        |||||      cddd       S # 1 sw Y   yxY w)a  Adds a Sum-of-Squares loss to the training procedure.

  `weights` acts as a coefficient for the loss. If a scalar is provided, then
  the loss is simply scaled by the given value. If `weights` 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 `weights` vector. If the shape of
  `weights` matches the shape of `predictions`, then the loss of each
  measurable element of `predictions` is scaled by the corresponding value of
  `weights`.

  Args:
    labels: The ground truth output tensor, same dimensions as 'predictions'.
    predictions: The predicted outputs.
    weights: Optional `Tensor` whose rank is either 0, or the same rank as
      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
      be either `1`, or the same as the corresponding `losses` dimension).
    scope: The scope for the operations performed in computing the loss.
    loss_collection: collection to which the loss will be added.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
    shape as `labels`; otherwise, it is scalar.

  Raises:
    ValueError: If the shape of `predictions` doesn't match that of `labels` or
      if the shape of `weights` is invalid.  Also if `labels` or `predictions`
      is None.

  @compatibility(TF2)

  `tf.compat.v1.losses.mean_squared_error` is mostly compatible with eager
  execution and `tf.function`. But, the `loss_collection` argument is
  ignored when executing eagerly and no loss will be written to the loss
  collections. You will need to either hold on to the return value manually
  or rely on `tf.keras.Model` loss tracking.


  To switch to native TF2 style, instantiate the
   `tf.keras.losses.MeanSquaredError` class and call the object instead.


  #### Structural Mapping to Native TF2

  Before:

  ```python
  loss = tf.compat.v1.losses.mean_squared_error(
    labels=labels,
    predictions=predictions,
    weights=weights,
    reduction=reduction)
  ```

  After:

  ```python
  loss_fn = tf.keras.losses.MeanSquaredError(
    reduction=reduction)
  loss = loss_fn(
    y_true=labels,
    y_pred=predictions,
    sample_weight=weights)
  ```

  #### How to Map Arguments

  | TF1 Arg Name          | TF2 Arg Name     | Note                       |
  | :-------------------- | :--------------- | :------------------------- |
  | `labels`              | `y_true`         | In `__call__()` method     |
  | `predictions`         | `y_pred`         | In `__call__()` method     |
  | `weights`             | `sample_weight`  | In `__call__()` method.    |
  : : : The shape requirements for `sample_weight` is different from      :
  : : : `weights`. Please check the [argument definition][api_docs] for   :
  : : : details.                                                          :
  | `scope`               | Not supported    | -                          |
  | `loss_collection`     | Not supported    | Losses should be tracked   |
  : : : explicitly or with Keras APIs, for example, [add_loss][add_loss], :
  : : : instead of via collections                                        :
  | `reduction`           | `reduction`      | In constructor. Value of   |
  : : : `tf.compat.v1.losses.Reduction.SUM_OVER_BATCH_SIZE`,              :
  : : : `tf.compat.v1.losses.Reduction.SUM`,                              :
  : : : `tf.compat.v1.losses.Reduction.NONE` in                           :
  : : : `tf.compat.v1.losses.softmax_cross_entropy` correspond to         :
  : : : `tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE`,                  :
  : : : `tf.keras.losses.Reduction.SUM`,                                  :
  : : : `tf.keras.losses.Reduction.NONE`, respectively. If you            :
  : : : used other value for `reduction`, including the default value     :
  : : :  `tf.compat.v1.losses.Reduction.SUM_BY_NONZERO_WEIGHTS`, there is :
  : : : no directly corresponding value. Please modify the loss           :
  : : : implementation manually.                                          :

  [add_loss]:https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer#add_loss
  [api_docs]:https://www.tensorflow.org/api_docs/python/tf/keras/losses/MeanSquaredError#__call__


  #### Before & After Usage Example

  Before:

  >>> y_true = [1, 2, 3]
  >>> y_pred = [1, 3, 5]
  >>> weights = [0, 1, 0.25]
  >>> # samples with zero-weight are excluded from calculation when `reduction`
  >>> # argument is set to default value `Reduction.SUM_BY_NONZERO_WEIGHTS`
  >>> tf.compat.v1.losses.mean_squared_error(
  ...    labels=y_true,
  ...    predictions=y_pred,
  ...    weights=weights).numpy()
  1.0

  >>> tf.compat.v1.losses.mean_squared_error(
  ...    labels=y_true,
  ...    predictions=y_pred,
  ...    weights=weights,
  ...    reduction=tf.compat.v1.losses.Reduction.SUM_OVER_BATCH_SIZE).numpy()
  0.66667

  After:

  >>> y_true = [[1.0], [2.0], [3.0]]
  >>> y_pred = [[1.0], [3.0], [5.0]]
  >>> weights = [1, 1, 0.25]
  >>> mse = tf.keras.losses.MeanSquaredError(
  ...    reduction=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE)
  >>> mse(y_true=y_true, y_pred=y_pred, sample_weight=weights).numpy()
  0.66667

  @end_compatibility
  Nrn   ro   mean_squared_errorr=   rq   )r)   r   rH   r
   rI   r   rJ   rr   rs   squared_differencerl   rv   s          r$   r   r   m  s    P ^
:
;;
?
@@
~~e1"FG46 F9>--6>>BK]]68F55f6F6F6HI((f=F 9FF F F   BCC%zlosses.sigmoid_cross_entropyc                    | t        d      |t        d      t        j                  |d|| |f      5 }t        j                  |      }t	        j
                  | |j                        } |j                         j                  | j                                |dkD  r| d|z
  z  d|z  z   } t        j                  | |d	      }t        |||||
      cddd       S # 1 sw Y   yxY w)a  Creates a cross-entropy loss using tf.nn.sigmoid_cross_entropy_with_logits.

  `weights` acts as a coefficient for the loss. If a scalar is provided,
  then the loss is simply scaled by the given value. If `weights` is a
  tensor of shape `[batch_size]`, then the loss weights apply to each
  corresponding sample.

  If `label_smoothing` is nonzero, smooth the labels towards 1/2:

      new_multiclass_labels = multiclass_labels * (1 - label_smoothing)
                              + 0.5 * label_smoothing

  Args:
    multi_class_labels: `[batch_size, num_classes]` target integer labels in
      `{0, 1}`.
    logits: Float `[batch_size, num_classes]` logits outputs of the network.
    weights: Optional `Tensor` whose rank is either 0, or the same rank as
    `multi_class_labels`, and must be broadcastable to `multi_class_labels`
    (i.e., all dimensions must be either `1`, or the same as the
    corresponding `losses` dimension).
    label_smoothing: If greater than `0` then smooth the labels.
    scope: The scope for the operations performed in computing the loss.
    loss_collection: collection to which the loss will be added.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss `Tensor` of the same type as `logits`. If `reduction` is
    `NONE`, this has the same shape as `logits`; otherwise, it is scalar.

  Raises:
    ValueError: If the shape of `logits` doesn't match that of
      `multi_class_labels` or if the shape of `weights` is invalid, or if
      `weights` is None.  Also if `multi_class_labels` or `logits` is None.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  Nz/Argument `multi_class_labels` must not be None.r   sigmoid_cross_entropy_lossr   r?   r   xentropyrw   r   r5   rq   )r)   r   rH   r^   r
   rI   r>   rr   rs   r   !sigmoid_cross_entropy_with_logitsrl   )multi_class_labelsr   rQ   label_smoothingrS   ra   rb   r8   s           r$   sigmoid_cross_entropyr     s    Z 
F
GG^
:
;;
~~e917;= F@E""6*F!'96<<H
001C1M1M1OP.!o2EF/12 119K9?7ACF !9FF F Fr   zlosses.softmax_cross_entropyc                 r   | t        d      |t        d      t        j                  |d|| |f      5 }t        j                  |      }t	        j
                  | |j                        } |j                         j                  | j                                |dkD  rHt	        j
                  t        j                  |       d   |j                        }d|z
  }||z  }	| |z  |	z   } t        j                  | d	      } t        j                  | |d
      }
t        |
||||      cddd       S # 1 sw Y   yxY w)ad  Creates a cross-entropy loss using tf.nn.softmax_cross_entropy_with_logits_v2.

  `weights` acts as a coefficient for the loss. If a scalar is provided,
  then the loss is simply scaled by the given value. If `weights` is a
  tensor of shape `[batch_size]`, then the loss weights apply to each
  corresponding sample.

  If `label_smoothing` is nonzero, smooth the labels towards 1/num_classes:
      new_onehot_labels = onehot_labels * (1 - label_smoothing)
                          + label_smoothing / num_classes

  Note that `onehot_labels` and `logits` must have the same shape,
  e.g. `[batch_size, num_classes]`. The shape of `weights` must be
  broadcastable to loss, whose shape is decided by the shape of `logits`.
  In case the shape of `logits` is `[batch_size, num_classes]`, loss is
  a `Tensor` of shape `[batch_size]`.

  Args:
    onehot_labels: One-hot-encoded labels.
    logits: Logits outputs of the network.
    weights: Optional `Tensor` that is broadcastable to loss.
    label_smoothing: If greater than 0 then smooth the labels.
    scope: the scope for the operations performed in computing the loss.
    loss_collection: collection to which the loss will be added.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss `Tensor` of the same type as `logits`. If `reduction` is
    `NONE`, this has shape `[batch_size]`; otherwise, it is scalar.

  Raises:
    ValueError: If the shape of `logits` doesn't match that of `onehot_labels`
      or if the shape of `weights` is invalid or if `weights` is None.  Also if
      `onehot_labels` or `logits` is None.

  @compatibility(TF2)

  `tf.compat.v1.losses.softmax_cross_entropy` is mostly compatible with eager
  execution and `tf.function`. But, the `loss_collection` argument is
  ignored when executing eagerly and no loss will be written to the loss
  collections. You will need to either hold on to the return value manually
  or rely on `tf.keras.Model` loss tracking.


  To switch to native TF2 style, instantiate the
   `tf.keras.losses.CategoricalCrossentropy` class with `from_logits` set
  as `True` and call the object instead.


  #### Structural Mapping to Native TF2

  Before:

  ```python
  loss = tf.compat.v1.losses.softmax_cross_entropy(
    onehot_labels=onehot_labels,
    logits=logits,
    weights=weights,
    label_smoothing=smoothing)
  ```

  After:

  ```python
  loss_fn = tf.keras.losses.CategoricalCrossentropy(
    from_logits=True,
    label_smoothing=smoothing)
  loss = loss_fn(
    y_true=onehot_labels,
    y_pred=logits,
    sample_weight=weights)
  ```

  #### How to Map Arguments

  | TF1 Arg Name          | TF2 Arg Name     | Note                       |
  | :-------------------- | :--------------- | :------------------------- |
  |  -                    | `from_logits`    | Set `from_logits` as True  |
  :                       :                  : to have identical behavior :
  | `onehot_labels`       | `y_true`         | In `__call__()` method     |
  | `logits`              | `y_pred`         | In `__call__()` method     |
  | `weights`             | `sample_weight`  | In `__call__()` method     |
  | `label_smoothing`     | `label_smoothing`| In constructor             |
  | `scope`               | Not supported    | -                          |
  | `loss_collection`     | Not supported    | Losses should be tracked   |
  :                       :                  : explicitly or with Keras   :
  :                       :                  : APIs, for example,         :
  :                       :                  : [add_loss][add_loss],      :
  :                       :                  : instead of via collections :
  | `reduction`           | `reduction`      | In constructor. Value of   |
  : : : `tf.compat.v1.losses.Reduction.SUM_OVER_BATCH_SIZE`,              :
  : : : `tf.compat.v1.losses.Reduction.SUM`,                              :
  : : : `tf.compat.v1.losses.Reduction.NONE` in                           :
  : : : `tf.compat.v1.losses.softmax_cross_entropy` correspond to         :
  : : : `tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE`,                  :
  : : : `tf.keras.losses.Reduction.SUM`,                                  :
  : : : `tf.keras.losses.Reduction.NONE`, respectively. If you            :
  : : : used other value for `reduction`, including the default value     :
  : : :  `tf.compat.v1.losses.Reduction.SUM_BY_NONZERO_WEIGHTS`, there is :
  : : : no directly corresponding value. Please modify the loss           :
  : : : implementation manually.                                          :

  [add_loss]:https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer#add_loss


  #### Before & After Usage Example

  Before:

  >>> y_true = [[0, 1, 0], [0, 0, 1]]
  >>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
  >>> weights = [0.3, 0.7]
  >>> smoothing = 0.2
  >>> tf.compat.v1.losses.softmax_cross_entropy(y_true, y_pred, weights=weights,
  ...   label_smoothing=smoothing).numpy()
  0.57618

  After:

  >>> cce = tf.keras.losses.CategoricalCrossentropy(from_logits=True,
  ...   label_smoothing=smoothing)
  >>> cce(y_true, y_pred, sample_weight=weights).numpy()
  0.57618

  @end_compatibility
  Nz*Argument `onehot_labels` must not be None.r   softmax_cross_entropy_lossr   rZ   labels_stop_gradientr4   r   r   rq   )r)   r   rH   r^   r
   rI   r>   rr   rs   r   shapestop_gradientr   $softmax_cross_entropy_with_logits_v2rl   )onehot_labelsr   rQ   r   rS   ra   rb   num_classessmooth_positivessmooth_negativesr8   s              r$   softmax_cross_entropyr   E  s:   H 
A
BB^
:
;;
~~e9}g68 F;@""6*FMM->M
001H1H1JKMM
//-
(
,fll<k.(;6#&669IIm++24M44V*>F !9F%F F Fs   C.D--D6c                 4   t        j                  | ||      \  } }t        j                        | j	                         j
                  }j	                         }|j
                  }|(|&||z
  }|dk(  rt        j                  dg      | |fS t        j                        t        j                  |       z
  }|#|dkD  rO|j                  d   j                  d      r1t        j                  t        j                  d|      fdfd      | |fS )aX  Internal version of _remove_squeezable_dimensions which handles weights.

  Squeezes `predictions` and `labels` if their ranks differ from expected by
  exactly 1.
  Squeezes `weights` if its rank is 1 more than the new rank of `predictions`

  This will use static shape if available. Otherwise, it will add graph
  operations, which could result in a performance hit.

  Args:
    labels: Label values, a `Tensor` whose dimensions match `predictions`.
    predictions: Predicted values, a `Tensor` of arbitrary dimensions.
    weights: Optional weight `Tensor`. It will be squeezed if it's not scalar,
      and its rank is 1 more than the new rank of `labels`.
    expected_rank_diff: Expected result of `rank(predictions) - rank(labels)`.

  Returns:
    Tuple of `predictions`, `labels` and `weights`, possibly with the last
    dimension squeezed.
  expected_rank_diffr?   r   r   c                  2    t        j                   dg      S )Nr   )r   squeezerQ   s   r$   <lambda>z/_remove_squeezable_dimensions.<locals>.<lambda>  s    )##GbT2 r&   c                       S r   r1   r   s   r$   r   z/_remove_squeezable_dimensions.<locals>.<lambda>  s    ' r&   )r   remove_squeezable_dimensionsr   r^   rr   ndimsr   r   rP   dimsis_compatible_withr   r
   rF   )rw   rx   rQ   r   labels_rankweights_shapeweights_rank	rank_diffs     `     r$   _remove_squeezable_dimensionsr     s   , )EEk.@B&+ ##G,G""$**K%%'M &&Ll&>,i	a##GbT2[')) w')..*@@Iq]//3FFqI		
..I
&
2
g
 
g	%%r&   z#losses.sparse_softmax_cross_entropyc                    | t        d      |t        d      t        j                  |d|| |f      5 }t        | ||d      \  } }}t	        j
                  | |d      }t        |||||	      cddd       S # 1 sw Y   yxY w)
aw  Cross-entropy loss using `tf.nn.sparse_softmax_cross_entropy_with_logits`.

  `weights` acts as a coefficient for the loss. If a scalar is provided,
  then the loss is simply scaled by the given value. If `weights` is a
  tensor of shape `[batch_size]`, then the loss weights apply to each
  corresponding sample.

  Args:
    labels: `Tensor` of shape `[d_0, d_1, ..., d_{r-1}]` (where `r` is rank of
      `labels` and result) and dtype `int32` or `int64`. Each entry in `labels`
      must be an index in `[0, num_classes)`. Other values will raise an
      exception when this op is run on CPU, and return `NaN` for corresponding
      loss and gradient rows on GPU.
    logits: Unscaled log probabilities of shape
      `[d_0, d_1, ..., d_{r-1}, num_classes]` and dtype `float16`, `float32` or
      `float64`.
    weights: Coefficients for the loss. This must be scalar or broadcastable to
      `labels` (i.e. same rank and each dimension is either 1 or the same).
    scope: the scope for the operations performed in computing the loss.
    loss_collection: collection to which the loss will be added.
    reduction: Type of reduction to apply to loss.

  Returns:
    Weighted loss `Tensor` of the same type as `logits`. If `reduction` is
    `NONE`, this has the same shape as `labels`; otherwise, it is scalar.

  Raises:
    ValueError: If the shapes of `logits`, `labels`, and `weights` are
      incompatible, or if any of them are None.

  @compatibility(eager)
  The `loss_collection` argument is ignored when executing eagerly. Consider
  holding on to the return value or collecting losses via a `tf.keras.Model`.
  @end_compatibility
  Nrn   r   !sparse_softmax_cross_entropy_lossr?   r   r   r   rq   )r)   r   rH   r   r   (sparse_softmax_cross_entropy_with_logitsrl   )rw   r   rQ   rS   ra   rb   r8   s          r$   sparse_softmax_cross_entropyr     s    R ^
:
;;^
:
;;
~~e@vw/1 F49 <A7FFG88@F>HJF !9FF F Fs   ;A::B)F)Nr   ),r/   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r   r	   r
   r   r   r   tensorflow.python.ops.lossesr   tensorflow.python.utilr   "tensorflow.python.util.deprecationr   r    tensorflow.python.util.tf_exportr   r   r;   rU   rG   add_dispatch_support	GraphKeysLOSSESr"   rl   rp   r~   r   r   r   r   r   r   r   r   r   r1   r&   r$   <module>r      s   D + . + + & 2 2 * $ ( 7 - + > I 6 !"##( #( $#(LD'4TQ -./	tS]]5I5I..JI  0JIZ +,-	!$DMM((..1F  .1Fh '()	<eD"CtMM((..	3F E  *3Fl "#$	'*$"}}33"99.F  %.Fb "#$	,/s$"}}33"99HF  %HFV  !"	*-t4 ]]11 774F  #4Fp 345	!$DMM((gK  6gKT *+,	!$DMM((..QF  -QFh -./	(+QdMM((..=F  0=F@ -./	#&MM((..YF  0YF| ;</&d 456	tMM((..6F  76Fr&   