Influence the future of Ray with our Ray Community Pulse survey. Complete it by Monday, January 27th, 2025 to get exclusive swag for eligible participants.
#!/usr/bin/env pythonimportargparseimporttimefromrayimporttrain,tunefromray.tune.schedulersimportAsyncHyperBandSchedulerdefevaluation_fn(step,width,height):time.sleep(0.1)return(0.1+width*step/100)**(-1)+height*0.1defeasy_objective(config):# Hyperparameterswidth,height=config["width"],config["height"]forstepinrange(config["steps"]):# Iterative training function - can be an arbitrary training procedureintermediate_score=evaluation_fn(step,width,height)# Feed the score back back to Tune.train.report({"iterations":step,"mean_loss":intermediate_score})if__name__=="__main__":parser=argparse.ArgumentParser()parser.add_argument("--smoke-test",action="store_true",help="Finish quickly for testing")args,_=parser.parse_known_args()# AsyncHyperBand enables aggressive early stopping of bad trials.scheduler=AsyncHyperBandScheduler(grace_period=5,max_t=100)# 'training_iteration' is incremented every time `trainable.step` is calledstopping_criteria={"training_iteration":1ifargs.smoke_testelse9999}tuner=tune.Tuner(tune.with_resources(easy_objective,{"cpu":1,"gpu":0}),run_config=train.RunConfig(name="asynchyperband_test",stop=stopping_criteria,verbose=1,),tune_config=tune.TuneConfig(metric="mean_loss",mode="min",scheduler=scheduler,num_samples=20),param_space={# Hyperparameter space"steps":100,"width":tune.uniform(10,100),"height":tune.uniform(0,100),},)results=tuner.fit()print("Best hyperparameters found were: ",results.get_best_result().config)