skypilot
67 строк · 2.5 Кб
1"""This example demonstrates basic Ray Tune random search and grid search."""
2### Source: https://docs.ray.io/en/latest/tune/examples/tune_basic_example.html
3import time4
5import ray6from ray import tune7
8
9def evaluation_fn(step, width, height):10time.sleep(0.1)11return (0.1 + width * step / 100)**(-1) + height * 0.112
13
14def easy_objective(config):15# Hyperparameters16width, height = config["width"], config["height"]17
18for step in range(config["steps"]):19# Iterative training function - can be any arbitrary training procedure20intermediate_score = evaluation_fn(step, width, height)21# Feed the score back back to Tune.22tune.report(iterations=step, mean_loss=intermediate_score)23
24
25if __name__ == "__main__":26import argparse27
28parser = argparse.ArgumentParser()29parser.add_argument("--smoke-test",30action="store_true",31help="Finish quickly for testing")32parser.add_argument("--server-address",33type=str,34default="auto",35required=False,36help="The address of server to connect to if using "37"Ray Client.")38args, _ = parser.parse_known_args()39if args.server_address is not None:40ray.init(args.server_address)41else:42ray.init(configure_logging=False)43
44print('cluster_resources:', ray.cluster_resources())45print('available_resources:', ray.available_resources())46print('live nodes:', ray.state.node_ids())47resources = ray.cluster_resources()48assert resources["accelerator_type:V100"] > 1, resources49
50# This will do a grid search over the `activation` parameter. This means51# that each of the two values (`relu` and `tanh`) will be sampled once52# for each sample (`num_samples`). We end up with 2 * 50 = 100 samples.53# The `width` and `height` parameters are sampled randomly.54# `steps` is a constant parameter.55
56analysis = tune.run(easy_objective,57metric="mean_loss",58mode="min",59num_samples=5 if args.smoke_test else 50,60config={61"steps": 5 if args.smoke_test else 100,62"width": tune.uniform(0, 20),63"height": tune.uniform(-100, 100),64"activation": tune.grid_search(["relu", "tanh"])65})66
67print("Best hyperparameters found were: ", analysis.best_config)68