o
    ci                     @   s  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 G dd dejZ	e
dkre  Zejddd	d
 e \ZZejrDejdd dZededd ddgddZeje	ejddejr`dnddd ejeddddejedddddd ddd!Ze Zed"e j dS dS )#    N)tune)PopulationBasedTrainingc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )PBTBenchmarkExamplea  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.
    c                 C   s   |d | _ d| _d S )Nlrg        )r   accuracy)selfconfig r	   Q/home/ubuntu/.local/lib/python3.10/site-packages/ray/tune/examples/pbt_example.pysetup#   s   

zPBTBenchmarkExample.setupc                 C   s   d}d}d}| j |k rd| j  | }ndd| j |  |  }tdtd|}t| j|t| j| }||k rD|  j d| t  7  _ n| j|krV|  j || t  8  _ |  j |tj  7  _ td| j | _ | j | j||| j |d kdS )	Nd         g{Gz?gMbP?g      ?r   )mean_accuracycur_lr
optimal_lrq_errdone)r   minmaxr   randomnpnormal)r   midpointq_tolerancenoise_levelr   r   r	   r	   r
   step'   s(   

zPBTBenchmarkExample.stepc                 C   s   | j | jdS )Nr   r   r   )r   checkpoint_dirr	   r	   r
   save_checkpointF   s   z#PBTBenchmarkExample.save_checkpointc                 C   s   |d | _ d S )Nr   )r   )r   
checkpointr	   r	   r
   load_checkpointL   s   z#PBTBenchmarkExample.load_checkpointc                 C   s   |d | _ || _dS )Nr   T)r   r   )r   
new_configr	   r	   r
   reset_configO   s   
z PBTBenchmarkExample.reset_configN)	__name__
__module____qualname____doc__r   r   r   r!   r#   r	   r	   r	   r
   r      s    r   __main__z--smoke-test
store_truezFinish quickly for testing)actionhelpr   )num_cpus   training_iterationc                   C   s   t ddS )N-C6?g{Gz?)r   uniformr	   r	   r	   r
   <lambda>e   s    r1      )r   some_other_factor)	time_attrperturbation_intervalhyperparam_mutationspbt_class_api_exampleT
   i  )r   r.   r      )checkpoint_frequencycheckpoint_score_attributenum_to_keep)namestopverbosecheckpoint_configr      )	schedulermetricmodereuse_actorsnum_samplesr/   )
run_configtune_configparam_spacez!Best hyperparameters found were: )argparser   numpyr   rayr   ray.tune.schedulersr   	Trainabler   r$   ArgumentParserparseradd_argumentparse_known_argsargs_
smoke_testinitr5   pbtTuner	RunConfigCheckpointConfig
TuneConfigtunerfitresultsprintget_best_resultr   r	   r	   r	   r
   <module>   sf   H%