
    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  ed
      ej                   d               Z ed      ej                   	 dd              Zy)zGImplementation of Neural Net (NN) functions with distribution strategy.    )distribute_lib)ops)	array_ops)	check_ops)math_ops)util)dispatch)	tf_exportznn.scale_regularization_lossc                     t        j                         rt        j                         rt        d      t        j                         j
                  }t        j                  |       |z  S )a  Scales the sum of the given regularization losses by number of replicas.

  Usage with distribution strategy and custom training loop:

  ```python
  with strategy.scope():
    def compute_loss(self, label, predictions):
      per_example_loss = tf.keras.losses.sparse_categorical_crossentropy(
          labels, predictions)

      # Compute loss that is scaled by sample_weight and by global batch size.
      loss = tf.nn.compute_average_loss(
          per_example_loss,
          sample_weight=sample_weight,
          global_batch_size=GLOBAL_BATCH_SIZE)

      # Add scaled regularization losses.
      loss += tf.nn.scale_regularization_loss(tf.nn.l2_loss(weights))
      return loss
  ```

  Args:
    regularization_loss: Regularization loss.

  Returns:
    Scalar loss value.
  z|You are calling `scale_regularization_loss` in cross replica context, while it was expected to be called in replica context.)r   has_strategyin_cross_replica_contextRuntimeErrorget_strategynum_replicas_in_syncr   
reduce_sum)regularization_lossnum_replicass     X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/nn_impl_distribute.pyscale_regularization_lossr      s\    > !!#

1
1
3
	A 
  ,,.CC,			0	1L	@@    znn.compute_average_lossNc                    t        j                  |       } | j                  }t        j                  |       5  |+t        j                  |      }t        j
                  | |      } t        j                  | |      } |nt        j                         rt        j                         rt        d      t        j                         j                  }t        j                  |       d   }||z  }t!        j"                  |d       t!        j$                  |d       t!        j&                  |d       t        j(                  |       }t        j                  ||      }t        j*                  ||      cddd       S # 1 sw Y   yxY w)a  Scales per-example losses with sample_weights and computes their average.

  Usage with distribution strategy and custom training loop:

  ```python
  with strategy.scope():
    def compute_loss(labels, predictions, sample_weight=None):

      # If you are using a `Loss` class instead, set reduction to `NONE` so that
      # we can do the reduction afterwards and divide by global batch size.
      per_example_loss = tf.keras.losses.sparse_categorical_crossentropy(
          labels, predictions)

      # Compute loss that is scaled by sample_weight and by global batch size.
      return tf.nn.compute_average_loss(
          per_example_loss,
          sample_weight=sample_weight,
          global_batch_size=GLOBAL_BATCH_SIZE)
  ```

  Args:
    per_example_loss: Per-example loss.
    sample_weight: Optional weighting for each example.
    global_batch_size: Optional global batch size value. Defaults to (size of
      first dimension of `losses`) * (number of replicas).

  Returns:
    Scalar loss value, obtained by summing the `per_example_loss` and dividing
    by `global_batch_size`. If `global_batch_size` is zero, the result is zero.
  NzwYou are calling `compute_average_loss` in cross replica context, while it was expected to be called in replica context.r   z!global_batch_size must be scalar.)messagez%global_batch_size must be an integer.z'global_batch_size must be non-negative.)r   convert_to_tensordtypelosses_utilcheck_per_example_loss_rankscale_losses_by_sample_weightr   castr   r   r   r   r   r   r   shape_v2r   assert_scalar_v2assert_integer_v2assert_non_negative_v2r   
div_no_nan)per_example_losssample_weightglobal_batch_sizeinput_dtyper   per_replica_batch_sizelosss          r   compute_average_lossr*   F   sf   F **+;< &&+../?@ "8 ++M:m$BB
M  }}%5{C 

%
%
'557E
 	

 $002GGl(112BCAF0<?#F #J $$#L /0D &7Et%67E"8 "8 "8s   D9E::F)NN)__doc__tensorflow.python.distributer   tensorflow.python.frameworkr   tensorflow.python.opsr   r   r   tensorflow.python.ops.lossesr   r   tensorflow.python.utilr	    tensorflow.python.util.tf_exportr
   add_dispatch_supportr   r*    r   r   <module>r4      s{    N 7 + + + * < + 6 )*	&A  +&AR $%	<@F8  &F8r   