o
    $i!                     @   s   d dl Z d dlZd dlZ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mZ dd Zddd	Zed
kr_e  Zejddddd e \ZZejrWejdd eejd dS dS )    N)tune)
Checkpoint)PopulationBasedTrainingc              	   C   s  | d }|  dd}d}d}t }|rS| '}ttj|dd}t	|}W d   n1 s3w   Y  W d   n1 sBw   Y  |d }|d	 }	|	d }d
}
d}d}	 ||
k red| |
 }n
dd||
  |
  }t
dtd|}t||t
|| }||k r|d| t  7 }n||kr||| t  8 }||tj  7 }td|}||||||
d kd}|| dkrt 7}ttj|dd}||d}t|| W d   n1 sw   Y  tj|t|d W d   n1 sw   Y  nt| |d7 }qZ)a  Toy PBT problem for benchmarking adaptive learning rate.

    The goal is to optimize this trainable's accuracy. The accuracy increases
    fastest at the optimal lr, which is a function of the current accuracy.

    The optimal lr schedule for this problem is the triangle wave as follows.
    Note that many lr schedules for real models also follow this shape:

     best lr
      ^
      |    /      |   /        |  /          | /            ------------> accuracy

    In this problem, using PBT with a population of 2-4 is sufficient to
    roughly approximate this lr schedule. Higher population sizes will yield
    faster convergence. Training will not converge without PBT.
    lrcheckpoint_interval   g        zcheckpoint.jsonrNaccstepd         Tg{Gz?gMbP?g      ?r   )mean_accuracycur_lr
optimal_lrq_errdonew)r	   r
   )
checkpoint)getr   get_checkpointas_directoryopenospathjoinjsonloadminmaxrandomnpnormaltempfileTemporaryDirectorydumpreportr   from_directory)configr   r   accuracyr
   r   checkpoint_dirfcheckpoint_dict	last_stepmidpointq_tolerancenoise_levelr   r   metricstempdir r3   [/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/examples/pbt_function.pypbt_function   s`   





r5   Fc                 C   s   d}t d|tddddgdd}tjttjd	d
d| rdnddtjddtjddddtj|ddddddd|dd}|	 }t
d| j d S )N   training_iterationg-C6?g{Gz?r   r   )r   some_other_factor)	time_attrperturbation_intervalhyperparam_mutationspbt_function_api_exampleFT
   i  )r   r7   )	fail_fastr   )checkpoint_score_attributenum_to_keep)nameverbosestopfailure_configcheckpoint_configr      )	schedulermetricmodenum_samplesreuse_actors)r   r8   r   )
run_configtune_configparam_spacez!Best hyperparameters found were: )r   r   uniformTunerr5   	RunConfigFailureConfigCheckpointConfig
TuneConfigfitprintget_best_resultr(   )
smoke_testr:   pbttunerresultsr3   r3   r4   run_tune_pbtj   sL   

-r\   __main__z--smoke-test
store_truezFinish quickly for testing)actiondefaulthelpr   )num_cpus)rX   )F)argparser   r   r    r#   numpyr!   rayr   ray.tuner   ray.tune.schedulersr   r5   r\   __name__ArgumentParserparseradd_argumentparse_known_argsargs_rX   initr3   r3   r3   r4   <module>   s2   
Y?