skypilot

Форк
0
/
tune_basic_example.py 
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
3
import time
4

5
import ray
6
from ray import tune
7

8

9
def evaluation_fn(step, width, height):
10
    time.sleep(0.1)
11
    return (0.1 + width * step / 100)**(-1) + height * 0.1
12

13

14
def easy_objective(config):
15
    # Hyperparameters
16
    width, height = config["width"], config["height"]
17

18
    for step in range(config["steps"]):
19
        # Iterative training function - can be any arbitrary training procedure
20
        intermediate_score = evaluation_fn(step, width, height)
21
        # Feed the score back back to Tune.
22
        tune.report(iterations=step, mean_loss=intermediate_score)
23

24

25
if __name__ == "__main__":
26
    import argparse
27

28
    parser = argparse.ArgumentParser()
29
    parser.add_argument("--smoke-test",
30
                        action="store_true",
31
                        help="Finish quickly for testing")
32
    parser.add_argument("--server-address",
33
                        type=str,
34
                        default="auto",
35
                        required=False,
36
                        help="The address of server to connect to if using "
37
                        "Ray Client.")
38
    args, _ = parser.parse_known_args()
39
    if args.server_address is not None:
40
        ray.init(args.server_address)
41
    else:
42
        ray.init(configure_logging=False)
43

44
    print('cluster_resources:', ray.cluster_resources())
45
    print('available_resources:', ray.available_resources())
46
    print('live nodes:', ray.state.node_ids())
47
    resources = ray.cluster_resources()
48
    assert resources["accelerator_type:V100"] > 1, resources
49

50
    # This will do a grid search over the `activation` parameter. This means
51
    # that each of the two values (`relu` and `tanh`) will be sampled once
52
    # 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

56
    analysis = tune.run(easy_objective,
57
                        metric="mean_loss",
58
                        mode="min",
59
                        num_samples=5 if args.smoke_test else 50,
60
                        config={
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

67
    print("Best hyperparameters found were: ", analysis.best_config)
68

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.