
    ij 
                     \    d dl Zd dlmZ d dlmZ d dlmZ  ed       G d de             Zy)    N)keras_export)Callback)io_utilszkeras.callbacks.TerminateOnNaNc                   2     e Zd ZdZddef fdZddZ xZS )TerminateOnNaNa  Callback that terminates training when a NaN loss is encountered.

    This callback monitors the loss value during training
    and terminates training when a NaN or Inf loss is detected.
    By default, training is stopped gracefully
    by setting `model.stop_training = True`, which triggers all callback cleanup
    methods including `on_train_end()`.

    Alternatively, you can use `raise_error=True` to immediately raise a
    RuntimeError when NaN/Inf is detected. This raise_error termination
    prevents `on_train_end()` from being called on other callbacks, which
    is useful for preserving backup states or preventing unintended cleanup
    when training fails.

    Args:
        raise_error: Boolean, default False. If False, uses graceful stop via
            `model.stop_training = True`. If True, immediately raises
            RuntimeError on NaN/Inf loss, bypassing callback cleanup methods.

    Example:

    ```
    # Graceful termination (default)
    callback = keras.callbacks.TerminateOnNaN()
    model.fit(x, y, callbacks=[callback])

    # raise_error termination (strict failure)
    callback = keras.callbacks.TerminateOnNaN(raise_error=True)
    model.fit(x, y, callbacks=[callback])
    ```
    raise_errorc                 0    t         |           || _        y N)super__init__r   )selfr   	__class__s     y/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/callbacks/terminate_on_nan.pyr   zTerminateOnNaN.__init__*   s    &    c                    |xs i }|j                  d      }|tt        j                  |      st        j                  |      rI| j                  rt        d| d| d      t        j                  d| d       d| j                  _	        yyy)	a,  Check for NaN/Inf loss at the end of each batch.

        Args:
            batch: Integer, index of batch within the current epoch.
            logs: Dict, contains the return value of `model.train_step()`.

        Raises:
            RuntimeError: If loss is NaN/Inf and raise_error=True.
        lossNz%NaN or Inf loss encountered at batch z. Loss value: z#. Terminating training immediately.zBatch z$: Invalid loss, terminating trainingT)
getnpisnanisinfr   RuntimeErrorr   	print_msgmodelstop_training)r   batchlogsr   s       r   on_batch_endzTerminateOnNaN.on_batch_end.   s     zrxxxx~$##&?w G''+f,OQ 
 && 'KL 04DJJ, "0 r   )Fr
   )__name__
__module____qualname____doc__boolr   r   __classcell__)r   s   @r   r   r      s    @'D '4r   r   )	numpyr   keras.src.api_exportr   keras.src.callbacks.callbackr   keras.src.utilsr   r    r   r   <module>r)      s2     - 1 $ ./<4X <4 0<4r   