
    &`iO                     *   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dl	Z	d dl	m
Z
 d dlmZmZmZmZ d dlmZ d dlmZ  G d de
j                  Zed	k    r: e j                    Ze                    d
dd           e                                \  ZZ e	j        d            ej        ddd            ee            eddd g dd          Z G d de
j                   Z! e!            Z" e
j#        e e
j$        de"d e
j%        ddd                      e
j&        edd!dd"           e
j'        d#d           e
j'        d#d          d$          Z(e()                                Z*e*+                                Z,e,j-        Z.dS dS )%    N)datasets)tune)ConvNetget_data_loaders	test_func
train_func)PopulationBasedTraining)validate_save_restorec                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )PytorchTrainablea  Train a Pytorch ConvNet with Trainable and PopulationBasedTraining
       scheduler. The example reuse some of the functions in mnist_pytorch,
       and is a good demo for how to add the tuning function without
       changing the original training code.
    c                    t                      \  | _        | _        t                      | _        t          j        | j                                        |                    dd          |                    dd                    | _	        d S )Nlrg{Gz?momentum?r   r   )
r   train_loadertest_loaderr   modeloptimSGD
parametersget	optimizer)selfconfigs     y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/examples/pbt_convnet_example.pysetupzPytorchTrainable.setup%   sr    .>.@.@+4+YY
J!!##zz$%%ZZ
C002 2 2    c                 ~    t          | j        | j        | j                   t	          | j        | j                  }d|iS )Nmean_accuracy)r   r   r   r   r   r   )r   accs     r   stepzPytorchTrainable.step-   s:    4:t~t/@AAA
D$455%%r   c                     t           j                            |d          }t          j        | j                                        |           d S Nz	model.pth)ospathjointorchsaver   
state_dictr   checkpoint_dircheckpoint_paths      r   save_checkpointz PytorchTrainable.save_checkpoint2   s=    ',,~{CC
4:((**O<<<<<r   c                     t           j                            |d          }| j                            t          j        |                     d S r$   )r%   r&   r'   r   load_state_dictr(   loadr+   s      r   load_checkpointz PytorchTrainable.load_checkpoint6   s=    ',,~{CC
""5:o#>#>?????r   c                 n    | j         j        D ] }d|v r|d         |d<   d|v r|d         |d<   !|| _        dS )Nr   r   T)r   param_groupsr   )r   
new_configparam_groups      r   reset_configzPytorchTrainable.reset_config:   sY    >6 	A 	AKz!!$.t$4D!Z''*4Z*@J' tr   N)	__name__
__module____qualname____doc__r   r"   r.   r2   r7    r   r   r   r      sl         2 2 2& & &
= = =@ @ @    r   r   __main__z--smoke-test
store_truezFinish quickly for testing)actionhelp   )num_cpusz~/dataT)traindownloadtraining_iteration   c                  B    t           j                            dd          S )Ng-C6?   )nprandomuniformr<   r   r   <lambda>rL   X   s    ")++FA66 r   )g?r   gGz?r   )	time_attrperturbation_intervalhyperparam_mutationsc                        e Zd Zd Zd Zd ZdS )CustomStopperc                     d| _         d S )NFshould_stopr   s    r   __init__zCustomStopper.__init__`   s    $Dr   c                 |    t           j        rdnd}| j        s|d         dk    rd| _        | j        p|d         |k    S )NrF   d   r    gQ?TrE   )args
smoke_testrT   )r   trial_idresultmax_iters       r   __call__zCustomStopper.__call__c   sM     O4qqH# ((?$(F(F#' #Ov.B'Cx'OOr   c                     | j         S )NrS   rU   s    r   stop_allzCustomStopper.stop_alli   s    ##r   N)r8   r9   r:   rV   r^   r`   r<   r   r   rQ   rQ   _   sD        	% 	% 	%	P 	P 	P	$ 	$ 	$ 	$ 	$r   rQ   pbt_testrH   r       )checkpoint_score_attributecheckpoint_frequencynum_to_keep)namestopverbosecheckpoint_configmax)	schedulermetricmodenum_samplesreuse_actorsgMbP?)
run_configtune_configparam_space)/argparser%   numpyrI   r(   torch.optimr   torchvisionr   rayr   ray.tune.examples.mnist_pytorchr   r   r   r   ray.tune.schedulersr	   ray.tune.utilsr
   	Trainabler   r8   ArgumentParserparseradd_argumentparse_known_argsrY   _initMNISTrk   StopperrQ   stopperTuner	RunConfigCheckpointConfig
TuneConfigrK   tunerfitresultsget_best_resultbest_result
checkpointbest_checkpointr<   r   r   <module>r      s    				                        



                  8 7 7 7 7 7 0 0 0 0 0 0$ $ $ $ $t~ $ $ $P z$X$&&F
|2N  P P P%%''GD!CHaHN84$7777 *+++ ('& 76(((	
 
  I$ $ $ $ $ $ $ $ mooGDJ!4>3d3+:%&  		
 	
 	
 $DO"
 
 
 $,ua(($UA..
 
'  E0 iikkG ))++K!,OOOI r   