o
    2hX                     @   s@   d dl Z d dlmZ d dlmZ d dlmZ G dd deZdS )    N)ops)Callback)compile_utilsc                       s:   e Zd ZdZ				d fdd	Zdd	 Zd
d Z  ZS )MonitorCallbacka  Base class for callbacks that monitor a quantity and evaluates
    improvements.

    This class provides common functionality for callbacks that monitor a
    metric during training to determine whether a condition has been met,
    such as improvement over time. It encapsulates logic for selecting
    the comparison operation based on a `monitor` value and `mode`, and
    computing whether a new value is an improvement.

    It is intended to be subclassed by other callbacks like `ModelCheckpoint`,
    `EarlyStopping`, or `ReduceLROnPlateau`, and is not meant to be used
    directly.

    Arguments:
        monitor: Quantity to be monitored. Defaults to `"val_loss"`.
        mode: One of `{"auto", "min", "max"}`. In `min` mode, training will aim
            to minimize the monitored quantity; in `'max'` mode it will aim to
            maximize it.; in `"auto"` mode, the direction is automatically
            inferred from the name of the monitored quantity. Defaults to
            `"auto"`.
        baseline: Floating point initial "best" value of the metric to be
            monitored. If `None` (default), the first monitored value will be
            used.
        min_delta: Minimum change in the monitored quantity to qualify as an
            improvement, i.e. an absolute change of less than min_delta, will
            count as no improvement. Defaults to `0`.

    Raises:
        ValueError: If `mode='auto'` is selected and the direction of the metric
        cannot be inferred.
    val_lossautoNr   c                    sZ   t    |dvrtj| jj d| ddd d}|| _|| _|| _t	|| _
d | _d S )N)r   minmaxz mode 'z$' is unknown, fallback to auto mode.   )
stacklevelr   )super__init__warningswarn	__class____name__monitormodebestabs	min_delta
monitor_op)selfr   r   baseliner   r    _/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/callbacks/monitor_callback.pyr   )   s   


zMonitorCallback.__init__c                 C   s   | j dkr
tj| _ne| j dkrtj| _n[| jd}|dkr"tj| _t| jdr]g }| jj	D ]}t
|tjtjfr?||j	 q.|D ]}|j|kr\t|dr\|jdkrXtj| _qBtj| _qB| jd u rot| jj d| j d	| jtjkr~|  jd
9  _d S d S )Nr   r	   val_lossmetrics
_directionupz callback received monitor=z, but Keras isn't able to automatically determine whether that metric should be maximized or minimized. Pass `mode='max'` in order to monitor based on the highest metric value, or pass `mode='min'` in order to use the lowest value.)r   r   lessr   greaterr   removeprefixhasattrmodelr   
isinstancer   CompileMetricsMetricsListextendnamer    
ValueErrorr   r   r   )r   metric_nameall_metricsmr   r   r   _set_monitor_op>   sD   









zMonitorCallback._set_monitor_opc                 C   s   |d u rdS |  || j |S )NT)r   r   )r   monitor_valuereference_valuer   r   r   _is_improvemente   s   zMonitorCallback._is_improvement)r   r   Nr   )r   
__module____qualname____doc__r   r1   r4   __classcell__r   r   r   r   r      s    "'r   )r   	keras.srcr   keras.src.callbacks.callbackr   keras.src.trainersr   r   r   r   r   r   <module>   s
    