
    Αi                       % S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKr	S SK
r
S SKJr  SSKJr  \(       a>  S SKJrJrJr  S SKJrJr  S S	KJr  SS
KJr  \S   rS\S'    " S S\5      r " S S\5      r/ r           S%                       S&S jjr " S S5      r " S S5      r  " S S\ 5      r! " S S\ 5      r" " S S\ 5      r# " S S\ 5      r$ " S S \ 5      r% " S! S"\ 5      r& " S# S$\ 5      r'g)'    )annotationsN)TYPE_CHECKING)
try_import   )ProgressBar)AnyLiteral	TypedDict)IteratorSequence)	TypeAlias)Modeltrainevalpredictr   _CallbackModec                  H    \ rS rSr% S\S'   S\S'   S\S'   S\S'   S\S'   S	rg
)_CallbackParams'   int
batch_sizeepochsstepsverbose	list[str]metrics N__name__
__module____qualname____firstlineno____annotations____static_attributes__r       U/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/hapi/callbacks.pyr   r   '   s    
r&   r   c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg	)
_CallbackLogs.   floatlossr   r   r   r   r   Nr   r   r&   r'   r)   r)   .   s    r&   r)   CallbackListc                4   U =(       d    / n[        [        U[         [        45      (       a  UOU/5      n[        S U 5       5      (       d  U(       a  [	        XVS9/UQn[        S U 5       5      (       d  / UQ[        Xx5      PnU H   n[        U[        5      (       d  M  Xl        M"     [        S U 5       5      (       d  / UQ[        5       Pn[        U5      nUR                  U5        U
S:w  a  U	=(       d    / O/ n	UUUUU	S.nUR                  U5        U$ )Nc              3  B   #    U  H  n[        U[        5      v   M     g 7fN)
isinstanceProgBarLogger.0ks     r'   	<genexpr>#config_callbacks.<locals>.<genexpr>H   s     :Tz!]++T   )r   c              3  B   #    U  H  n[        U[        5      v   M     g 7fr0   )r1   ModelCheckpointr3   s     r'   r6   r7   K   s     <t!z!_--tr8   c              3  B   #    U  H  n[        U[        5      v   M     g 7fr0   )r1   LRSchedulerr3   s     r'   r6   r7   Q   s     84az![))4r8   test)r   r   r   r   r   )listr1   tupleanyr2   r:   EarlyStoppingsave_dirr<   r-   	set_model
set_params)	callbacksmodelr   r   r   log_freqr   	save_freqrB   r   mode_cbkscbksr5   cbk_listparamss                   r'   config_callbacksrN   7   s    OEED%=11wD :T:::wh8@4@<t<<<<<y;<a''!J  84888%%{}%D!Hu#v~gm2G F Or&   c                     \ rS rSrSSS jjrSS jrSS jrSS jrSS jrSS jr	SS	 jr
 S     SS
 jjr S     SS jjr S     SS jjr S     SS jjr  S       SS jjr  S       SS jjrSrg)r-   b   Nc                J    Uc   e[        U5      U l        0 U l        S U l        g r0   )r>   rE   rM   rF   )selfrE   s     r'   __init__CallbackList.__init__c   s'    $$$i
r&   c                :    U R                   R                  U5        g r0   )rE   append)rR   callbacks     r'   rV   CallbackList.appendj   s    h'r&   c                ,    [        U R                  5      $ r0   )iterrE   rR   s    r'   __iter__CallbackList.__iter__m   s    DNN##r&   c                L    U R                    H  nUR                  U5        M     g r0   )rE   rD   )rR   rM   cs      r'   rD   CallbackList.set_paramsp   s    ALL   r&   c                L    U R                    H  nUR                  U5        M     g r0   )rE   rC   )rR   rF   r_   s      r'   rC   CallbackList.set_modelt   s    AKK  r&   c                J    U R                    H  n[        X15      nU" U6   M     g r0   )rE   getattr)rR   nameargsr_   funcs        r'   _callCallbackList._callx   s!    A1#D$K  r&   c                    US;   d   S5       eg )Nr   z%mode should be train, eval or predictr   )rR   rI   s     r'   _check_modeCallbackList._check_mode}   s#     
 
 	3 3		3 
r&   c                T    U R                  U5        SU S3nU R                  X25        g )Non__beginrk   rh   rR   rI   logsre   s       r'   on_beginCallbackList.on_begin   s,     	TF&!

4r&   c                T    U R                  U5        SU S3nU R                  X25        g )Nrn   _endrp   rq   s       r'   on_endCallbackList.on_end   s,     	TF$

4r&   c                (    U R                  SX5        g )Non_epoch_beginrh   rR   epochrr   s      r'   rz   CallbackList.on_epoch_begin   s     	

#U1r&   c                (    U R                  SX5        g )Non_epoch_endr{   r|   s      r'   r   CallbackList.on_epoch_end   s     	

>5/r&   c                V    U R                  U5        SU S3nU R                  XBU5        g )Nrn   _batch_beginrp   rR   rI   steprr   re   s        r'   on_batch_beginCallbackList.on_batch_begin   s.     	TF,'

4t$r&   c                V    U R                  U5        SU S3nU R                  XBU5        g )Nrn   
_batch_endrp   r   s        r'   on_batch_endCallbackList.on_batch_end   s.     	TF*%

4t$r&   )rE   rF   rM   r0   )rE   zSequence[Callback] | NonereturnNone)rW   Callbackr   r   )r   zIterator[Callback]rM   r   r   r   rF   r   r   r   )re   strrf   r   r   r   )rI   r   r   r   )rI   r   rr   _CallbackLogs | Noner   r   NNr}   
int | Nonerr   r   r   r   )rI   r   r   r   rr   r   r   r   )r    r!   r"   r#   rS   rV   r\   rD   rC   rh   rk   rs   rw   rz   r   r   r   r%   r   r&   r'   r-   r-   b   s    ($!
3 AE!)=	 AE!)=	 FJ22.B2	2 FJ00.B0	0  %)	%% % #	%
 
%  %)	%% % #	%
 
% %r&   c                  p   \ rS rSr% SrS\S'   S\S'   SS jrSS jrSS	 jrSSS jjr	SSS jjr
SSS jjrSSS jjrSSS jjrSSS jjr S     SS jjr S     SS jjr S     S S jjr S     S S jjr S     S S jjr S     S S jjr S     S S jjr S     S S jjrSrg
)!r      a   
Base class used to build new callbacks. And new callbacks could also
terminate training by setting `model.stop_training=True`.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> # build a simple model checkpoint callback
        >>> class ModelCheckpoint(paddle.callbacks.Callback):
        ...     def __init__(self, save_freq=1, save_dir=None):
        ...         self.save_freq = save_freq
        ...         self.save_dir = save_dir
        ...
        ...     def on_epoch_end(self, epoch, logs=None):
        ...         if self.model is not None and epoch % self.save_freq == 0:
        ...             path = '{}/{}'.format(self.save_dir, epoch)
        ...             print('save checkpoint at {}'.format(path))
        ...             self.model.save(path)

Model | NonerF   r   rM   c                     S U l         0 U l        g r0   rF   rM   r[   s    r'   rS   Callback.__init__   s    
r&   c                    Xl         g)a  
Set parameters, which is dict. The keys contain:

  - 'batch_size': an integer. Number of samples per batch.
  - 'epochs': an integer. Number of epochs.
  - 'steps': an integer. Number of steps of one epoch.
  - 'verbose': an integer. Verbose mode is 0, 1 or 2. 0 = silent, 1 = progress bar, 2 = one line per epoch.
  - 'metrics': a list of str. Names of metrics, including 'loss' and the names of paddle.metric.Metric.
N)rM   )rR   rM   s     r'   rD   Callback.set_params   s	     r&   c                    Xl         g)z"model is instance of paddle.Model.N)rF   )rR   rF   s     r'   rC   Callback.set_model   s    
r&   Nc                    g)zUCalled at the start of training.

Args:
    logs (dict): The logs is a dict or None.
Nr   rR   rr   s     r'   on_train_beginCallback.on_train_begin       r&   c                    g)zCalled at the end of training.

Args:
    logs (dict): The logs is a dict or None. The keys of logs
        passed by paddle.Model contains 'loss', metric names and
        `batch_size`.
Nr   r   s     r'   on_train_endCallback.on_train_end   r   r&   c                    g)aR  Called at the start of evaluation.

Args:
    logs (dict): The logs is a dict or None. The keys of logs
        passed by paddle.Model contains 'steps' and 'metrics',
        The `steps` is number of total steps of validation dataset.
        The `metrics` is a list of str including 'loss' and the names
        of paddle.metric.Metric.
Nr   r   s     r'   on_eval_beginCallback.on_eval_begin   r   r&   c                    g)zCalled at the end of evaluation.

Args:
    logs (dict): The logs is a dict or None. The `logs` passed by
        paddle.Model is a dict contains 'loss', metrics and 'batch_size'
        of last batch of validation dataset.
Nr   r   s     r'   on_eval_endCallback.on_eval_end   r   r&   c                    g)zXCalled at the beginning of predict.

Args:
    logs (dict): The logs is a dict or None.
Nr   r   s     r'   on_predict_beginCallback.on_predict_begin  r   r&   c                    g)zRCalled at the end of predict.

Args:
    logs (dict): The logs is a dict or None.
Nr   r   s     r'   on_predict_endCallback.on_predict_end  r   r&   c                    g)zCalled at the beginning of each epoch.

Args:
    epoch (int): The index of epoch.
    logs (dict): The logs is a dict or None. The `logs` passed by
        paddle.Model is None.
Nr   r|   s      r'   rz   Callback.on_epoch_begin  r   r&   c                    g)zCalled at the end of each epoch.

Args:
    epoch (int): The index of epoch.
    logs (dict): The logs is a dict or None. The `logs` passed by
        paddle.Model is a dict, contains 'loss', metrics and 'batch_size'
        of last batch.
Nr   r|   s      r'   r   Callback.on_epoch_end  r   r&   c                    g)zCalled at the beginning of each batch in training.

Args:
    step (int): The index of step (or iteration).
    logs (dict): The logs is a dict or None. The `logs` passed by
        paddle.Model is empty.
Nr   rR   r   rr   s      r'   on_train_batch_beginCallback.on_train_batch_begin*  r   r&   c                    g)a  Called at the end of each batch in training.

Args:
    step (int): The index of step (or iteration).
    logs (dict): The logs is a dict or None. The `logs` passed by
        paddle.Model is a dict, contains 'loss', metrics and 'batch_size'
        of current batch.
Nr   r   s      r'   on_train_batch_endCallback.on_train_batch_end5  r   r&   c                    g)zCalled at the beginning of each batch in evaluation.

Args:
    step (int): The index of step (or iteration).
    logs (dict): The logs is a dict or None. The `logs` passed by
        paddle.Model is empty.
Nr   r   s      r'   on_eval_batch_beginCallback.on_eval_batch_beginA  r   r&   c                    g)a  Called at the end of each batch in evaluation.

Args:
    step (int): The index of step (or iteration).
    logs (dict): The logs is a dict or None. The `logs` passed by
        paddle.Model is a dict, contains 'loss', metrics and 'batch_size'
        of current batch.
Nr   r   s      r'   on_eval_batch_endCallback.on_eval_batch_endL  r   r&   c                    g)zCalled at the beginning of each batch in predict.

Args:
    step (int): The index of step (or iteration).
    logs (dict): The logs is a dict or None.
Nr   r   s      r'   on_predict_batch_beginCallback.on_predict_batch_beginX  r   r&   c                    g)zCalled at the end of each batch in predict.

Args:
    step (int): The index of step (or iteration).
    logs (dict): The logs is a dict or None.
Nr   r   s      r'   on_predict_batch_endCallback.on_predict_batch_endb  r   r&   r   r   r   r   r   r0   rr   r   r   r   r}   r   rr   r   r   r   r   r   rr   r   r   r   )r    r!   r"   r#   __doc__r$   rS   rD   rC   r   r   r   r   r   r   rz   r   r   r   r   r   r   r   r%   r   r&   r'   r   r      sW   0 
	 8<		 4			 8<

 4
	
 7;		3			 7;

3
	
 7;		3			 7;

3
	
 7;3	 7;3	 r&   r   c                     \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S\S
'   SSS jjrS rSS S jjr S!     S"S jjr	S#S jr
 S     S$S jjr S     S$S jjr S     S%S jjrSS S jjr S     S$S jjr S     S$S jjrSS S jjr S     S$S jjr S     S$S jjrSS S jjrSS S jjrSrg)&r2   im  a*  
Logger callback function to print loss and metrics to stdout. It supports
silent mode (not print), progress bar or one line per each printing,
see arguments for more detailed.

Args:
    log_freq (int): The frequency, in number of steps,
        the logs such as loss, metrics are printed. Default: 1.
    verbose (int): The verbosity mode, should be 0, 1, or 2.
        0 = silent, 1 = progress bar, 2 = one line each printing, 3 = 2 +
        time counter, such as average reader cost, samples per second.
        Default: 2.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.vision.transforms as T
        >>> from paddle.vision.datasets import MNIST
        >>> from paddle.static import InputSpec

        >>> inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')]
        >>> labels = [InputSpec([None, 1], 'int64', 'label')]

        >>> transform = T.Compose([
        ...     T.Transpose(),
        ...     T.Normalize([127.5], [127.5])
        ... ])
        >>> train_dataset = MNIST(mode='train', transform=transform)

        >>> lenet = paddle.vision.models.LeNet()
        >>> model = paddle.Model(lenet,
        ...     inputs, labels)

        >>> optim = paddle.optimizer.Adam(0.001, parameters=lenet.parameters())
        >>> model.prepare(optimizer=optim,
        ...             loss=paddle.nn.CrossEntropyLoss(),
        ...             metrics=paddle.metric.Accuracy())

        >>> callback = paddle.callbacks.ProgBarLogger(log_freq=10)
        >>> model.fit(train_dataset, batch_size=64, callbacks=callback)
r   r   r   zProgressBar | Noneprogbarr   r   rG   c                F    S U l         S U l        S U l        X l        Xl        g r0   )r   r   r   r   rG   )rR   rG   r   s      r'   rS   ProgBarLogger.__init__  s!    
 r&   c                ~    U R                   =(       a+    [        R                  R                  5       R                  S:H  $ Nr   )r   paddledistributedParallelEnv
local_rankr[   s    r'   	_is_printProgBarLogger._is_print  s+    ||P 2 2 > > @ K Kq PPr&   Nc                    U R                   S   U l        U R                  (       d   eU R                   S   U l        U R                  (       d   eSSSSS.U l        U R	                  5       (       a  [        S5        g g )Nr   r   r   	data_time
batch_timecountsampleszmThe loss value printed in the log is the current step, and the metric is the average value of previous steps.)rM   r   train_metrics_train_timerr   printr   s     r'   r   ProgBarLogger.on_train_begin  su    kk(+{{{![[3!!!! 	
 >> r&   c                X   U R                   S   U l        Xl        SU l        U R                  (       a3  U R                  5       (       a  [        SUS-    SU R                   35        [        U R                  U R                  S9U l	        [        R                  " 5       U R                  S'   g )Nr   r   Epoch r   /numr   batch_start_time)rM   r   r}   
train_stepr   r   r   r   r   train_progbartimer   r|   s      r'   rz   ProgBarLogger.on_epoch_begin  s~     [[)

;;4>>++F519+Qt{{m45(TZZN04		,-r&   c                L   / n[        X S35      n[        X S35      n[        X S35      nU H  nXq;   d  M
  UR                  XqU   45        M!     U R                  S:X  a  [        U SU S35      (       a  [        U SU S35      nUS   S:  a  US   OS	n	US
   S:  a  US
   OS	n
UR                  SSUS   U	-  -  45        UR                  SSUS   U	-  -  45        UR                  SSXS   US   -   -  -  45        SUS'   SUS
'   SUS'   SUS'   UR	                  Xc5        g )N_metrics_progbar_step   __timerr   r         ?r   avg_reader_costz%.5f secr   avg_batch_costr   ipsz%.5f samples/secg        )rd   rV   r   hasattrupdate)rR   rr   rI   valuesr   r   r   r5   timercntr   s              r'   _updatesProgBarLogger._updates  sq   $& 12$& 12en-Ayqq'l+  <<14&/?!@!@DAdV6"23E$)'NQ$6%.CC*/	*:Q*>eI&CGMM"J%2Ds2J$KL MM!:|1Ds1J#KL MM&"4u\7J"JKM E'N E)!$E+"%E,u%r&   c                    [         R                   " 5       U R                  S'   U R                  S==   U R                  S   U R                  S   -
  -  ss'   g Nbatch_data_end_timer   r   )r   r   r   s      r'   r   "ProgBarLogger.on_train_batch_begin  sT     4899;/0+&34 234	
&r&   c                R   U=(       d    0 nU =R                   S-  sl         U R                  S==   [        R                  " 5       U R                  S   -
  -  ss'   U R                  S==   S-  ss'   UR                  SS5      nU R                  S==   U-  ss'   U R	                  5       (       aV  U R                   U R
                  -  S:X  a9  U R                  b  U R                   U R                  :  a  U R                  US5        [        R                  " 5       U R                  S	'   g )
Nr   r   r  r   r   r   r   r   r   )r   r   r   getr   rG   r   r   rR   r   rr   r   s       r'   r    ProgBarLogger.on_train_batch_end  s     zr1,'IIK$++,ABB	
' 	'"a'"((<+)$/$>>$-- ?1 Dzz!T__tzz%AdG,04		,-r&   c                    U=(       d    0 nU R                  5       (       a!  U R                  b  U R                  US5        g g g )Nr   )r   r   r   r|   s      r'   r   ProgBarLogger.on_epoch_end  s9     zr>>!7MM$( "8r&   c                ^   UR                  SS 5      U l        UR                  S/ 5      U l        SU l        SU l        SSSSS.U l        [        U R                  U R                  S9U l        U R                  5       (       a  [        S5        [        R                  " 5       U R
                  S'   g )Nr   r   r   r   r   zEval begin...r   )r  
eval_stepseval_metrics	eval_stepevaled_samples_eval_timerr   r   eval_progbarr   r   r   r   s     r'   r   ProgBarLogger.on_eval_begin	  s    ((7D1 HHY3 	
 (
 >>/"/3yy{+,r&   c                    [         R                   " 5       U R                  S'   U R                  S==   U R                  S   U R                  S   -
  -  ss'   g r  )r   r  r   s      r'   r   !ProgBarLogger.on_eval_batch_begin  T     37))+./%23123	
%r&   c                   U=(       d    0 nU =R                   S-  sl         UR                  SS5      nU =R                  U-  sl        U R                  S==   [        R                  " 5       U R                  S   -
  -  ss'   U R                  S==   S-  ss'   UR                  SS5      nU R                  S==   U-  ss'   U R                  5       (       aV  U R                   U R                  -  S:X  a9  U R                  b  U R                   U R                  :  a  U R                  US5        [        R                  " 5       U R                  S	'   g )
Nr   r   r   r  r   r   r   r   r   )	r  r  r  r  r   r   rG   r  r   r  s       r'   r   ProgBarLogger.on_eval_batch_end'  s	    zr!((<+w&&IIK$**+@AA	
& 	!Q&!((<+#w.#>> >! C&$..4??*JdF+/3yy{+,r&   c                ^   UR                  SS 5      U l        UR                  S/ 5      U l        SU l        SU l        SSSSS.U l        [        U R                  U R                  S9U l        U R                  5       (       a  [        S5        [        R                  " 5       U R
                  S'   g )Nr   r   r   r   r   zPredict begin...r   )r  
test_stepstest_metrics	test_steptested_samples_test_timerr   r   test_progbarr   r   r   r   s     r'   r   ProgBarLogger.on_predict_begin<  s    ((7D1 HHY3 	
 (
 >>$%/3yy{+,r&   c                    [         R                   " 5       U R                  S'   U R                  S==   U R                  S   U R                  S   -
  -  ss'   g r  )r   r  r   s      r'   r   $ProgBarLogger.on_predict_batch_beginQ  r  r&   c                   U=(       d    0 nU =R                   S-  sl         UR                  SS5      nU =R                  U-  sl        U R                  S==   [        R                  " 5       U R                  S   -
  -  ss'   U R                  S==   S-  ss'   UR                  SS5      nU R                  S==   U-  ss'   U R                   U R
                  -  S:X  aN  U R                  5       (       a9  U R                  b  U R                   U R                  :  a  U R                  US5        [        R                  " 5       U R                  S	'   g )
Nr   r   r   r  r   r   r   r=   r   )	r  r  r  r  r   rG   r   r  r   r  s       r'   r   "ProgBarLogger.on_predict_batch_endZ  s	    zr!((<+w&&IIK$**+@AA	
& 	!Q&!((<+#w.#>>DMM)Q.4>>3C3C&$..4??*JdF+/3yy{+,r&   c                    U=(       d    0 nU R                  5       (       a9  U R                  b+  U R                  US5        [        SU R                   35        g g g )Nr   zEval samples: )r   r  r   r   r  r   s     r'   r   ProgBarLogger.on_eval_endo  sM    zr>>!<MM$'N4#6#6"789 "=r&   c                    U=(       d    0 nU R                  5       (       aX  U R                  U R                  -  S:w  d  U R                  S:X  a  U R	                  US5        [        SU R                   35        g g )Nr   r   r=   zPredict samples: )r   r  rG   r   r   r   r  r   s     r'   r   ProgBarLogger.on_predict_endu  sb    zr>>~~-2dlla6GdF+%d&9&9%:;< r&   )r  r  r   r}   r   r  r  r  r  r  rG   r   r   r  r  r  r  r  r   r   r   r   )r      )rG   r   r   r   r   r   r0   r   r   r   rr   r   rI   r   r   r   r   r   )r    r!   r"   r#   r   r$   rS   r   r   rz   r   r   r   r   r   r   r   r   r   r   r   r   r%   r   r&   r'   r2   r2   m  sg   )V LM!Q$ FJ
<
<.B
<	
< &F 7;

3
	
 7;<<3<	<$ 8<)) 4)	);, 7;

3
	
 7;;;3;	;*;, 7;

3
	
 7;;;3;	;*:= =r&   r2   c                  n    \ rS rSrSrS
SS jjr S     SS jjrS r S     SS jjrSSS jjr	S	r
g)r:   i}  a  
Model checkpoint callback function to save model weights and optimizer
state during training in conjunction with model.fit(). Currently,
ModelCheckpoint only supports saving after a fixed number of epochs.

Args:
    save_freq(int): The frequency, in number of epochs, the model checkpoint
        are saved. Default: 1.
    save_dir(str|None): The directory to save checkpoint during training.
        If None, will not save checkpoint. Default: None.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.vision.transforms as T
        >>> from paddle.vision.datasets import MNIST
        >>> from paddle.static import InputSpec

        >>> inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')]
        >>> labels = [InputSpec([None, 1], 'int64', 'label')]

        >>> transform = T.Compose([
        ...     T.Transpose(),
        ...     T.Normalize([127.5], [127.5])
        ... ])
        >>> train_dataset = MNIST(mode='train', transform=transform)

        >>> lenet = paddle.vision.models.LeNet()
        >>> model = paddle.Model(lenet,
        ...     inputs, labels)

        >>> optim = paddle.optimizer.Adam(0.001, parameters=lenet.parameters())
        >>> model.prepare(optimizer=optim,
        ...             loss=paddle.nn.CrossEntropyLoss(),
        ...             metrics=paddle.metric.Accuracy())

        >>> callback = paddle.callbacks.ModelCheckpoint(save_dir='./temp')
        >>> model.fit(train_dataset, batch_size=64, callbacks=callback)
Nc                    Xl         X l        g r0   )rH   rB   )rR   rH   rB   s      r'   rS   ModelCheckpoint.__init__  s    " r&   c                    Xl         g r0   )r}   r|   s      r'   rz   ModelCheckpoint.on_epoch_begin  s	     
r&   c                    U R                   =(       a>    U R                  =(       a+    [        R                  R	                  5       R
                  S:H  $ r   )rF   rB   r   r   r   r   r[   s    r'   _is_saveModelCheckpoint._is_save  s>    JJ AA""..0;;q@	
r&   c                   U R                  5       (       av  U R                  U R                  -  S:X  aX  U R                   SU 3n[	        S[
        R                  R                  U5       35        U R                  R                  U5        g g g )Nr   r   save checkpoint at )
r1  r}   rH   rB   r   ospathabspathrF   save)rR   r}   rr   r6  s       r'   r   ModelCheckpoint.on_epoch_end  sm     ==??tzzDNN:a?mm_AeW-D'(='>?@JJOOD!  @?r&   c                    U R                  5       (       aV  U R                   S3n[        S[        R                  R                  U5       35        U R                  R                  U5        g g )Nz/finalr4  )r1  rB   r   r5  r6  r7  rF   r8  )rR   rr   r6  s      r'   r   ModelCheckpoint.on_train_end  sP    ==??mm_F+D'(='>?@JJOOD! r&   )r}   rB   rH   )r   N)rH   r   rB   
str | Noner   r   r   r   r0   r   r   )r    r!   r"   r#   r   rS   rz   r1  r   r   r%   r   r&   r'   r:   r:   }  sb    'R!
 FJ.B	

 8<"" 4"	"" "r&   r:   c                  Z    \ rS rSrSrSS	S jjr S
     SS jjr S
     SS jjrSrg)r<   i  a	  Lr scheduler callback function

Args:
    by_step(bool, optional): whether to update learning rate scheduler
        by step. Default: True.
    by_epoch(bool, optional): whether to update learning rate scheduler
        by epoch. Default: False.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.vision.transforms as T
        >>> from paddle.static import InputSpec

        >>> inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')]
        >>> labels = [InputSpec([None, 1], 'int64', 'label')]

        >>> transform = T.Compose([
        ...     T.Transpose(),
        ...     T.Normalize([127.5], [127.5])
        ... ])
        >>> train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform)

        >>> lenet = paddle.vision.models.LeNet()
        >>> model = paddle.Model(lenet,
        ...     inputs, labels)

        >>> base_lr = 1e-3
        >>> boundaries = [5, 8]
        >>> wamup_steps = 4

        >>> def make_optimizer(parameters=None):
        ...     momentum = 0.9
        ...     weight_decay = 5e-4
        ...     values = [base_lr * (0.1**i) for i in range(len(boundaries) + 1)]
        ...     learning_rate = paddle.optimizer.lr.PiecewiseDecay(
        ...         boundaries=boundaries, values=values)
        ...     learning_rate = paddle.optimizer.lr.LinearWarmup(
        ...         learning_rate=learning_rate,
        ...         warmup_steps=wamup_steps,
        ...         start_lr=base_lr / 5.,
        ...         end_lr=base_lr,
        ...         verbose=True)
        ...     optimizer = paddle.optimizer.Momentum(
        ...         learning_rate=learning_rate,
        ...         weight_decay=weight_decay,
        ...         momentum=momentum,
        ...         parameters=parameters)
        ...     return optimizer

        >>> optim = make_optimizer(parameters=lenet.parameters())
        >>> model.prepare(optimizer=optim,
        ...             loss=paddle.nn.CrossEntropyLoss(),
        ...             metrics=paddle.metric.Accuracy())

        >>> # if LRScheduler callback not set, an instance LRScheduler update by step
        >>> # will be created auto.
        >>> model.fit(train_dataset, batch_size=64)

        >>> # create a learning rate scheduler update by epoch
        >>> callback = paddle.callbacks.LRScheduler(by_step=False, by_epoch=True)
        >>> model.fit(train_dataset, batch_size=64, callbacks=callback)
c                N    U(       a  U(       a  [        S5      eXl        X l        g )Nz2by_step option is mutually exclusive with by_epoch)
ValueErrorby_stepby_epoch)rR   r@  rA  s      r'   rS   LRScheduler.__init__  s#    xD   r&   Nc                   U R                   (       a  U R                  R                  (       a  [        U R                  R                  S5      (       a  [	        U R                  R                  R
                  [        R                  R                  R                  5      (       a/  U R                  R                  R
                  R                  5         g g g g g N_learning_rate)rA  rF   
_optimizerr   r1   rE  r   	optimizerlrr<   r   r|   s      r'   r   LRScheduler.on_epoch_end  s     ==

%%DJJ113CDDJJ))88$$''33 
 

%%4499; E & r&   c                   U R                   (       a  U R                  R                  (       a  [        U R                  R                  S5      (       a  [	        U R                  R                  R
                  [        R                  R                  R                  5      (       a/  U R                  R                  R
                  R                  5         g g g g g rD  )r@  rF   rF  r   r1   rE  r   rG  rH  r<   r   r   s      r'   r   LRScheduler.on_train_batch_end  s     <<

%%DJJ113CDDJJ))88$$''33 
 

%%4499; E & r&   )rA  r@  )TF)r@  boolrA  rL  r   r   r0   r   r   )	r    r!   r"   r#   r   rS   r   r   r%   r   r&   r'   r<   r<     sV    ?B! 8<<< 4<	< 7;<<3<	< <r&   r<   c                  |   ^  \ rS rSrSr       S               SU 4S jjjrS	S
S jjrS	S
S jjrSrU =r	$ )rA   i.  a4  Stop training when the given monitor stopped improving during evaluation
by setting `model.stop_training=True`.

Args:
    monitor(str): Quantity to be monitored. Default: 'loss'.
    mode(str|None): Mode should be one of 'auto', 'min' or 'max'. In 'min'
        mode, training will stop until monitored quantity stops decreasing.
        In 'max' mode, training will stop until monitored quantity stops
        increasing. In 'auto' mode, exact mode can be inferred by the name
        of monitor. If 'acc' in monitor, the mode will be considered as
        'max', otherwise the mode will be set to 'min'. Default: 'auto'.
    patience(int): Number of epochs with no improvement after which
        training will be stopped. Default: 0.
    verbose(int): The verbosity mode, should be 0 or 1. When verbose=0,
        logs will not be printed. When verbose=1, logs will be printed.
        Default: 1.
    min_delta(int|float): The minimum change of monitored quantity. If
        the change is less than min_delta, model could be considered as no
        improvement. Default: 0.
    baseline(int|float|None): Baseline value for the monitored quantity.
        Training will stop if the model doesn't show improvement over the
        baseline. Default: None.
    save_best_model(bool): Whether to save best model. Default: True.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle import Model
        >>> from paddle.static import InputSpec
        >>> from paddle.vision.models import LeNet
        >>> from paddle.vision.datasets import MNIST
        >>> from paddle.metric import Accuracy
        >>> from paddle.nn import CrossEntropyLoss
        >>> import paddle.vision.transforms as T

        >>> device = paddle.set_device('cpu')
        >>> sample_num = 200
        >>> save_dir = './best_model_checkpoint'
        >>> transform = T.Compose(
        ...     [T.Transpose(), T.Normalize([127.5], [127.5])])
        >>> train_dataset = MNIST(mode='train', transform=transform)
        >>> val_dataset = MNIST(mode='test', transform=transform)
        >>> net = LeNet()
        >>> optim = paddle.optimizer.Adam(
        ...     learning_rate=0.001, parameters=net.parameters())

        >>> inputs = [InputSpec([None, 1, 28, 28], 'float32', 'x')]
        >>> labels = [InputSpec([None, 1], 'int64', 'label')]

        >>> model = Model(net, inputs=inputs, labels=labels)
        >>> model.prepare(
        ...     optim,
        ...     loss=CrossEntropyLoss(reduction="sum"),
        ...     metrics=[Accuracy()])
        >>> callbacks = paddle.callbacks.EarlyStopping(
        ...     'loss',
        ...     mode='min',
        ...     patience=1,
        ...     verbose=1,
        ...     min_delta=0,
        ...     baseline=None,
        ...     save_best_model=True)
        >>> model.fit(train_dataset,
        ...           val_dataset,
        ...           batch_size=64,
        ...           log_freq=200,
        ...           save_freq=10,
        ...           save_dir=save_dir,
        ...           epochs=20,
        ...           callbacks=[callbacks])
c                r  > [         TU ]  5         Xl        X0l        X@l        X`l        [        U5      U l        SU l        S U l	        SU l
        Xpl        S U l        US;  a  [        R                  " SU S35        SnUS:X  a  [        R                   U l        OWUS:X  a  [        R$                  U l        O;SU R                  ;   a  [        R$                  U l        O[        R                   U l        U R"                  [        R$                  :X  a  U =R                  S	-  sl        g U =R                  S
-  sl        g )Nr   autominmaxzEarlyStopping mode # is unknown, fallback to auto mode.rP  rQ  rR  accr   )superrS   monitorpatiencer   baselineabs	min_delta
wait_epochbest_weightsstopped_epochsave_best_modelrB   warningswarnnpless
monitor_opgreater)	rR   rW  rI   rX  r   r[  rY  r_  	__class__s	           r'   rS   EarlyStopping.__init__x  s     	  Y .$(--MM%dV+NO D5= ggDOU] jjDO $"$**"$''??bjj(NNaNNNb Nr&   c                    SU l         U R                  b  U R                  U l        g U R                  [        R
                  :X  a  [        R                  O[        R                  * U l        S U l        g r   )r\  rY  
best_valuerd  rb  rc  infr]  r   s     r'   r   EarlyStopping.on_train_begin  sH    ==$"mmDO(,277(BbffDO $Dr&   c           
     4   Ub  U R                   U;  a  [        R                  " S5        g XR                      n[        U[        [
        45      (       a  US   nO#[        U[        R                  5      (       a  UnOg U R                  X R                  -
  U R                  5      (       aq  X l
        SU l        U R                  (       aR  U R                  bE  [        R                  R!                  U R                  S5      nU R"                  R%                  U5        OU =R                  S-  sl        U R                  U R&                  :  a  SU R"                  l        U R*                  S:  a  [-        SU R.                  S-    S35        U R                  (       al  U R                  b_  [-        SR1                  [        R                  R3                  [        R                  R!                  U R                  S5      5      5      5        U =R.                  S-  sl        g )	Nz7Monitor of EarlyStopping should be loss or metric name.r   
best_modelr   Tr   z: Early stopping.z$Best checkpoint has been saved at {})rW  r`  ra  r1   r>   r?   numbersNumberrd  r[  ri  r\  r_  rB   r5  r6  joinrF   r8  rX  stop_trainingr   r   r^  formatr7  )rR   rr   currentr6  s       r'   r   EarlyStopping.on_eval_end  s}   <4<<t3MMI ||$ge}--ajG00G??7^^3T__EE%ODO##(Aww||DMM<@

%OOq O??dmm+'+DJJ$||at11A566GHI''DMM,E>EEGGOO "T]]L I 	ar&   )rY  ri  r]  r[  rW  rd  rX  r_  rB   r^  r   r\  )r,   rP  r   r   r   NT)rW  r   rI   Literal['auto', 'min', 'max']rX  r   r   r   r[  r+   rY  zfloat | Noner_  rL  r   r   r0   r   )
r    r!   r"   r#   r   rS   r   r   r%   __classcell__rf  s   @r'   rA   rA   .  s    GV .4!% $)!)! ,)! 	)!
 )! )! )! )! 
)! )!V%"  " r&   rA   c                      \ rS rSrSrSS jrSS jrSSS jjr S     SS jjrSS jr	 S     SS	 jjr
SSS
 jjrSSS jjrSSS jjrSrg)VisualDLi  a  
VisualDL callback class. After storing the loss values and evaluation metrics in a log file during the training time , the panel is launched to view the visual results.

Args:
    log_dir (str): The directory to save visualdl log file.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.vision.transforms as T
        >>> from paddle.static import InputSpec

        >>> inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')]
        >>> labels = [InputSpec([None, 1], 'int64', 'label')]

        >>> transform = T.Compose([
        ...     T.Transpose(),
        ...     T.Normalize([127.5], [127.5])
        ... ])
        >>> train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform)
        >>> eval_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform)

        >>> net = paddle.vision.models.LeNet()
        >>> model = paddle.Model(net, inputs, labels)

        >>> optim = paddle.optimizer.Adam(0.001, parameters=net.parameters())
        >>> model.prepare(optimizer=optim,
        ...             loss=paddle.nn.CrossEntropyLoss(),
        ...             metrics=paddle.metric.Accuracy())

        >>> ## uncomment following lines to fit model with visualdl callback function
        >>> # callback = paddle.callbacks.VisualDL(log_dir='visualdl_log_dir')
        >>> # model.fit(train_dataset, eval_dataset, batch_size=64, callbacks=callback)

c                :    Xl         S U l        S U l        SU l        g r   )log_dirr   r   r}   )rR   r{  s     r'   rS   VisualDL.__init__  s    

r&   c                X    [         R                  R                  5       R                  S:H  $ r   r   r   r   r   r[   s    r'   	_is_writeVisualDL._is_write  "    !!--/::a??r&   Nc                    U R                   S   U l        U R                  (       d   eU R                   S   U l        U R                  (       d   eSU l        SU l        g )Nr   r   Tr   )rM   r   r   _is_fitr   r   s     r'   r   VisualDL.on_train_begin  sL    kk(+{{{![[3!!!!r&   c                8    U R                   S   U l        Xl        g Nr   rM   r   r}   r|   s      r'   rz   VisualDL.on_epoch_begin       [[)

r&   c                   U R                  5       (       d  g [        U S5      (       d+  [        S5      nUR                  U R                  5      U l        [        X S35      n[        X S35      nUS:X  a  UnOU R                  nU Hy  nXq;   d  M
  US-   U-   n[        X   [        [        45      (       a  X   S   n	O([        X   [        R                  5      (       a  X   n	OM_  U R
                  R                  XU	S9  M{     g )	Nwritervisualdlr   r   r   r   r   )tagr   value)r  r   r   	LogWriterr{  r  rd   r}   r1   r>   r?   rn  ro  
add_scalar)
rR   rr   rI   r  r   current_step
total_stepr5   temp_tag
temp_values
             r'   r   VisualDL._updates  s    ~~tX&&!*-H",,T\\:DK$& 12tvU^47?%JJAy#:>dge}55!%J88!%J&&  '  r&   c                    U=(       d    0 nU =R                   S-  sl         U R                  5       (       a  U R                  US5        g g Nr   r   r   r  r   r   s      r'   r   VisualDL.on_train_batch_end+  ;     zr1>>MM$( r&   c                |    UR                  SS 5      U l        UR                  S/ 5      U l        SU l        SU l        g Nr   r   r   r  r  r  r  r  r   s     r'   r   VisualDL.on_eval_begin4  6    ((7D1 HHY3r&   c                t    [        U S5      (       a'  U R                  R                  5         [        U S5        g g )Nr  )r   r  closedelattrr   s     r'   r   VisualDL.on_train_end:  s.    4""KKD(# #r&   c                    U R                  5       (       a]  U R                  US5        [        U S5      (       d9  [        U S5      (       a'  U R                  R	                  5         [        U S5        g g g g )Nr   r  r  )r  r   r   r  r  r  r   s     r'   r   VisualDL.on_eval_end?  s^    >>MM$'D),,'$2I2I!!#h' 3J, r&   )r  r}   r   r  r  r  r  r{  r   r   r   r  )r{  r   r   r   r   rL  r0   r   r   r   )rr   r   rI   r   r   r   r   )r    r!   r"   r#   r   rS   r  r   rz   r   r   r   r   r   r%   r   r&   r'   ry  ry    sw    #J@ FJ.B	> 7;))3)	) $
( (r&   ry  c                      \ rS rSrSr      S               SS jjrS r\S 5       rSSS jjr	 S     SS jjr
SS	 jr S     SS
 jjrSSS jjrSSS jjrSSS jjrSrg)WandbCallbackiH  a  Track your training and system metrics using `Weights and Biases <https://docs.wandb.ai>`_.

**Installation and set-up**

Install with pip and log in to your W&B account:

.. code-block:: bash

    pip install wandb
    wandb login

Args:
    project(str|None, optional): Name of the project. Default: uncategorized
    entity(str|None, optional): Name of the team/user creating the run. Default: Logged in user
    name(str|None, optional): Name of the run. Default: randomly generated by wandb
    dir(str|None, optional): Directory in which all the metadata is stored. Default: `wandb`
    mode(str|None, optional): Can be "online", "offline" or "disabled". Default: "online".
    job_type(str|None, optional): the type of run, for grouping runs together. Default: None

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.vision.transforms as T
        >>> from paddle.static import InputSpec

        >>> inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')]
        >>> labels = [InputSpec([None, 1], 'int64', 'label')]

        >>> transform = T.Compose([
        ...     T.Transpose(),
        ...     T.Normalize([127.5], [127.5])
        ... ])
        >>> train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform)
        >>> eval_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform)

        >>> net = paddle.vision.models.LeNet()
        >>> model = paddle.Model(net, inputs, labels)

        >>> optim = paddle.optimizer.Adam(0.001, parameters=net.parameters())
        >>> model.prepare(optimizer=optim,
        ...             loss=paddle.nn.CrossEntropyLoss(),
        ...             metrics=paddle.metric.Accuracy())

        >>> ## uncomment following lines to fit model with wandb callback function
        >>> # callback = paddle.callbacks.WandbCallback(project='paddle_mnist')
        >>> # model.fit(train_dataset, eval_dataset, batch_size=64, callbacks=callback)

Nc                    [        SS5      U l        UUUUUUS.U l        S U l        U R                  R                  " S0 UD6  U R
                  ng )NwandbzVYou want to use `wandb` which is not installed yet install it with `pip install wandb`)projectre   entitydirrI   job_typer   )r   r  
wandb_args_runr   run)	rR   r  r  re   r  rI   r  kwargsr   s	            r'   rS   WandbCallback.__init__{  sZ      d

  
 	((HHr&   c                X    [         R                  R                  5       R                  S:H  $ r   r~  r[   s    r'   r  WandbCallback._is_write  r  r&   c                \   U R                  5       (       a  U R                  c  U R                  R                  b=  [        R
                  " S5        U R                  R                  U l        U R                  $ U R                  R                  " S0 U R                  D6U l        U R                  $ )NzThere is a wandb run already in progress and newly created instances of `WandbCallback` will reuse this run. If this is not desired , call `wandb.finish()` before instantiating `WandbCallback`.r   )r  r  r  r  r`  ra  initr  r[   s    r'   r  WandbCallback.run  s}    >>yy ::>>-MMY
 !%

DI yy !%

 B$// BDIyyr&   c                   U R                   S   U l        U R                  (       d   eU R                   S   U l        U R                  (       d   eSU l        SU l        U R                  5       (       ak  U R                  R                  S5        U R                  R                  SSS9  U R                  R                  S5        U R                  R                  S	SS9  g g )
Nr   r   Tr   
train/stepztrain/*)step_metricr}   zeval/*)rM   r   r   r  r   r  r  define_metricr   s     r'   r   WandbCallback.on_train_begin  s    kk(+{{{![[3!!!!>>HH""<0HH""9,"GHH""7+HH""8"A r&   c                8    U R                   S   U l        Xl        g r  r  r|   s      r'   rz   WandbCallback.on_epoch_begin  r  r&   c                &   U R                  5       (       d  g [        X S35      n[        X S35      n0 nUS:X  a  UnUR                  SU05        OU R                  nUR                  SU05        U H  nXq;   d  M
  US-   U-   n[	        X   [
        [        45      (       a  UR                  XU   S   05        MI  [	        X   [        R                  5      (       a  UR                  XU   05        M  M     U R                  R                  U5        g )Nr   r   r   r  r}   r   r   )r  rd   r   r}   r1   r>   r?   rn  ro  r  log)	rR   rr   rI   r   r  r   r  r5   r  s	            r'   r   WandbCallback._updates  s    ~~$& 12tvU^47?%JOO\:67JOOWj12Ay#:>dge}55OOXAwqz$:;88OOXAw$78  	Xr&   c                    U=(       d    0 nU =R                   S-  sl         U R                  5       (       a  U R                  US5        g g r  r  r   s      r'   r    WandbCallback.on_train_batch_end  r  r&   c                |    UR                  SS 5      U l        UR                  S/ 5      U l        SU l        SU l        g r  r  r   s     r'   r   WandbCallback.on_eval_begin  r  r&   c                d    U R                  5       (       a  U R                  R                  5         g g r0   )r  r  finishr   s     r'   r   WandbCallback.on_train_end  s"    >>HHOO r&   c                    U R                  5       (       a]  U R                  US5        [        U S5      (       d9  [        U S5      (       a'  U R                  R	                  5         [        U S5        g g g g )Nr   r  r  )r  r   r   r  r  r  r   s     r'   r   WandbCallback.on_eval_end  s\    >>MM$'D),,'$2F2F!e$ 3G, r&   )r  r  r}   r   r  r  r  r  r   r   r   r  r  )NNNNNN)r  r<  r  r<  re   r<  r  r<  rI   z/Literal['online', 'offline', 'disabled'] | Noner  r<  r  r   r   r   r0   r   r   r*  r   )r    r!   r"   r#   r   rS   r  propertyr  r   rz   r   r   r   r   r   r%   r   r&   r'   r  r  H  s    0h #!@D#  	
  >   
:@  B  8< 4	< 7;))3)	) % %r&   r  c                     ^  \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'   S\S'   S\S'           S                 SU 4S jjjrSS jrSSS jjrSSS jjr	SS jr
SrU =r$ )ReduceLROnPlateaui  a  Reduce learning rate when a metric of evaluation has stopped improving.
Models often benefit from reducing the learning rate by a factor
of 2-10 once learning stagnates. This callback monitors a
quantity and if no improvement is seen for a 'patience' number
of epochs, the learning rate is reduced.

Args:
    monitor(str, optional): Quantity to be monitored. Default: 'loss'.
    factor(float, optional): factor by which the learning rate will be reduced.
        `new_lr = lr * factor`. Default: 0.1.
    patience(int, optional): Number of epochs with no improvement after which
        learning rate will be reduced. Default: 10.
    verbose(int, optional): The verbosity mode. 0: quiet, 1: update messages.
        Default: 1.
    mode(str, optional): one of `{'auto', 'min', 'max'}`. In `'min'` mode,
        the learning rate will be reduced when the quantity monitored has
        stopped decreasing. In 'max' mode, learning rate will reduce until
        monitored quantity stops increasing. In 'auto' mode, exact mode
        can be inferred by the name of monitor. If 'acc' in monitor, the
        mode will be considered as 'max', otherwise the mode will be set
        to 'min'. Default: 'auto'.
    min_delta(int|float, optional): threshold for measuring the new optimum,
        to only focus on significant changes. Default: 0.
    cooldown(int, optional): number of epochs to wait before resuming normal operation after
        lr has been reduced. Default: 0.
    min_lr(float, optional): lower bound on the learning rate. Default: 0.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle import Model
        >>> from paddle.static import InputSpec
        >>> from paddle.vision.models import LeNet
        >>> from paddle.vision.datasets import MNIST
        >>> from paddle.metric import Accuracy
        >>> from paddle.nn.layer.loss import CrossEntropyLoss
        >>> import paddle.vision.transforms as T
        >>> sample_num = 200
        >>> transform = T.Compose(
        ...      [T.Transpose(), T.Normalize([127.5], [127.5])])
        >>> train_dataset = MNIST(mode='train', transform=transform)
        >>> val_dataset = MNIST(mode='test', transform=transform)
        >>> net = LeNet()
        >>> optim = paddle.optimizer.Adam(
        ...     learning_rate=0.001, parameters=net.parameters())
        >>> inputs = [InputSpec([None, 1, 28, 28], 'float32', 'x')]
        >>> labels = [InputSpec([None, 1], 'int64', 'label')]
        >>> model = Model(net, inputs=inputs, labels=labels)
        >>> model.prepare(
        ...     optim,
        ...     loss=CrossEntropyLoss(),
        ...     metrics=[Accuracy()])
        >>> callbacks = paddle.callbacks.ReduceLROnPlateau(patience=3, verbose=1)
        >>> model.fit(train_dataset,
        ...             val_dataset,
        ...             batch_size=64,
        ...             log_freq=200,
        ...             save_freq=10,
        ...             epochs=20,
        ...             callbacks=[callbacks])

r   rW  r+   factorr   rX  r   ru  rI   r[  cooldownmin_lrc	                
  > [         T	U ]  5         Xl        US:  a  [        S5      eX l        Xl        X`l        X0l        X@l        Xpl	        SU l
        SU l        SU l        XPl        S U l        SU l        U R!                  5         g )Nr   z3ReduceLROnPlateau does not support a factor >= 1.0.r   )rV  rS   rW  r?  r  r  r[  rX  r   r  cooldown_counterwaitbestrI   rd  r}   _reset)
rR   rW  r  rX  r   rI   r[  r  r  rf  s
            r'   rS   ReduceLROnPlateau.__init__C  s~     	S=E  "   !			
r&   c                |  ^  T R                   S;  a+  [        R                  " ST R                    S35        ST l         T R                   S:X  d   T R                   S:X  a1  ST R                  ;  a!  U 4S jT l        [
        R                  T l        O!U 4S jT l        [
        R                  * T l        S	T l        S	T l	        g
)z)Resets wait counter and cooldown counter.rO  zLearning rate reduction mode rS  rP  rQ  rT  c                J   > [         R                  " XTR                  -
  5      $ r0   )rb  rc  r[  abrR   s     r'   <lambda>*ReduceLROnPlateau._reset.<locals>.<lambda>o  s    2771$..6H+Ir&   c                J   > [         R                  " XTR                  -   5      $ r0   )rb  re  r[  r  s     r'   r  r  r  s    2::aT^^9K+Lr&   r   N)
rI   r`  ra  rW  rd  rb  rj  r  r  r  r[   s   `r'   r  ReduceLROnPlateau._resetd  s    9922MM/		{ ;) ) DI99IIE$=IDODILDODI !	r&   c                $    U R                  5         g r0   )r  r   s     r'   r    ReduceLROnPlateau.on_train_beginw  s    r&   c                x   Ub  U R                   U;  a  [        R                  " S5        g  U R                  R                  R
                  n[        U[        5      (       d$  [        R                  " S[        U5       S35        g  XR                      n[        U[        [        45      (       a  US   nO#[        U[        R                  5      (       a  UnOg U R                  5       (       a  U =R                  S-  sl        SU l        U R#                  X@R$                  5      (       a  X@l        SU l        GO=U R                  5       (       Gd'  U =R                   S-  sl        U R                   U R&                  :  a  U R                  R                  R)                  5       nU[*        R,                  " U R.                  5      :  a  XPR0                  -  n[3        X`R.                  5      nX`R                  R                  l        U R4                  S:  aK  [6        R8                  R;                  5       R<                  S:X  a  [?        SU R@                  S-    SU S35        U RB                  U l        SU l        U =R@                  S-  sl         g ! [         a$  n[        R                  " SU S35         S nAg S nAff = f)	Nz;Monitor of ReduceLROnPlateau should be loss or metric name.z)Expected learning_rate be float, bug got .zAThere are something wrong when get learning_rate from optimizer: r   r   z
Epoch z.: ReduceLROnPlateau reducing learning rate to )"rW  r`  ra  rF   rF  rE  r1   r+   type	Exceptionr>   r?   rn  ro  in_cooldownr  r  rd  r  rX  get_lrrb  float32r  r  rR  r   r   r   r   r   r   r}   r  )rR   rr   rH  ers  old_lrnew_lrs          r'   r   ReduceLROnPlateau.on_eval_endz  s$   <4<<t3MMM ZZ**99!"e,,MMCDH:QO 	 - ||$ge}--ajG00G!!Q&!DI??7II..IDI!!##IINIyyDMM)..557BJJt{{33#kk1F 5F;AJJ))8q("..::<GG1L&tzzA~&6 7''-ha1 -1MMD) !DI

a
O  WXYWZZ[\ 	s   AJ 
J9J44J9c                     U R                   S:  $ r   )r  r[   s    r'   r  ReduceLROnPlateau.in_cooldown  s    $$q((r&   )r  r  r  r}   r  r[  r  rI   rW  rd  rX  r   r  )r,   g?
   r   rP  g-C6?r   r   )rW  r   r  r+   rX  r   r   r   rI   ru  r[  r+   r  r   r  r+   r   r   r   r0   r   r  )r    r!   r"   r#   r   r$   rS   r  r   r   r  r%   rv  rw  s   @r'   r  r    s    >@ LMML
''MM .4  	
  ,    
 B&5n) )r&   r  )NNNNNr)  r)  r   NNr   )rE   z$Sequence[Callback] | Callback | NonerF   r   r   r   r   r   r   r   rG   r   r   r   rH   r   rB   r<  r   zlist[str] | NonerI   zLiteral['train', 'test']r   r-   )(
__future__r   rn  r5  r   r`  typingr   numpyrb  r   paddle.utilsr   progressbarr   r   r	   r
   collection.abcr   r   typing_extensionsr   rF   r   r   r$   r   r)   __all__rN   r-   r   r2   r:   r<   rA   ry  r  r  r   r&   r'   <module>r     s   #  	       # $..1+&'ABM9B) 	   7;! $%,(3(( ( 	(
 ( ( ( ( ( ( #( (VL% L%^y yxM=H M=`F"h F"Re<( e<P_ H _ Du(x u(pn%H n%by) y)r&   