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.
"""This example demonstrates basic Ray Tune random search and grid search."""importtimeimportrayfromrayimporttrain,tunedefevaluation_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 any 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__":importargparseparser=argparse.ArgumentParser()parser.add_argument("--smoke-test",action="store_true",help="Finish quickly for testing")args,_=parser.parse_known_args()ray.init(configure_logging=False)# This will do a grid search over the `activation` parameter. This means# that each of the two values (`relu` and `tanh`) will be sampled once# for each sample (`num_samples`). We end up with 2 * 50 = 100 samples.# The `width` and `height` parameters are sampled randomly.# `steps` is a constant parameter.tuner=tune.Tuner(easy_objective,tune_config=tune.TuneConfig(metric="mean_loss",mode="min",num_samples=5ifargs.smoke_testelse50,),param_space={"steps":5ifargs.smoke_testelse100,"width":tune.uniform(0,20),"height":tune.uniform(-100,100),"activation":tune.grid_search(["relu","tanh"]),},)results=tuner.fit()print("Best hyperparameters found were: ",results.get_best_result().config)