当前位置:   article > 正文

分布式执行引擎ray入门--(4)Ray Tune

分布式执行引擎ray入门--(4)Ray Tune

目录

1.基础入门

1.1 基础版

1.2 Keras+Hyperopt 调参

1.3 PyTorch+Optuna

1.4. Tune的优势

2.核心概念

搜索空间

实验(Ray Trial)

搜索算法(Tune Search Algorithms)

调度器(Tune Schedulers)

结果Tune ResultGrid


1.基础入门

1.1 基础版

安装相关依赖:pip install "ray[tune]"

  1. from ray import train, tune
  2. def objective(config): # ①
  3. score = config["a"] ** 2 + config["b"]
  4. return {"score": score}
  5. search_space = { # ②
  6. "a": tune.grid_search([0.001, 0.01, 0.1, 1.0]),
  7. "b": tune.choice([1, 2, 3]),
  8. }
  9. tuner = tune.Tuner(objective, param_space=search_space) # ③
  10. results = tuner.fit()
  11. print(results.get_best_result(metric="score", mode="min").config)

使用ray调参一共只需要3步:

① 定义目标函数

② 定义搜索空间

③ 启动一个Tune运行并打印出最佳结果

1.2 Keras+Hyperopt 调参

  1. from ray import tune
  2. from ray.tune.search.hyperopt import HyperOptSearch
  3. import keras
  4. def objective(config): # ①
  5. model = keras.models.Sequential()
  6. model.add(keras.layers.Dense(784, activation=config["activation"]))
  7. model.add(keras.layers.Dense(10, activation="softmax"))
  8. model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
  9. # model.fit(...)
  10. # loss, accuracy = model.evaluate(...)
  11. return {"accuracy": accuracy}
  12. search_space = {"activation": tune.choice(["relu", "tanh"])} # ②
  13. algo = HyperOptSearch()
  14. tuner = tune.Tuner( # ③
  15. objective,
  16. tune_config=tune.TuneConfig(
  17. metric="accuracy",
  18. mode="max",
  19. search_alg=algo,
  20. ),
  21. param_space=search_space,
  22. )
  23. results = tuner.fit()

和基础版本类似,也是分三步,只是第一步要将Keras模型包装在一个目标函数中。

这里使用到了HyperOptSearch搜索算法,也可以自行尝试其他搜索算法

1.3 PyTorch+Optuna

  1. import torch
  2. from ray import train, tune
  3. from ray.tune.search.optuna import OptunaSearch
  4. def objective(config): # ①
  5. train_loader, test_loader = load_data() # Load some data
  6. model = ConvNet().to("cpu") # Create a PyTorch conv net
  7. optimizer = torch.optim.SGD( # Tune the optimizer
  8. model.parameters(), lr=config["lr"], momentum=config["momentum"]
  9. )
  10. while True:
  11. train(model, optimizer, train_loader) # Train the model
  12. acc = test(model, test_loader) # Compute test accuracy
  13. train.report({"mean_accuracy": acc}) # Report to Tune
  14. search_space = {"lr": tune.loguniform(1e-4, 1e-2), "momentum": tune.uniform(0.1, 0.9)}
  15. algo = OptunaSearch() # ②
  16. tuner = tune.Tuner( # ③
  17. objective,
  18. tune_config=tune.TuneConfig(
  19. metric="mean_accuracy",
  20. mode="max",
  21. search_alg=algo,
  22. ),
  23. run_config=train.RunConfig(
  24. stop={"training_iteration": 5},
  25. ),
  26. param_space=search_space,
  27. )
  28. results = tuner.fit()
  29. print("Best config is:", results.get_best_result().config)

和1.2中的类似,也是第一步要将PyTorch模型包装在一个目标函数中。

这里使用的是OptunaSearch搜索算法,同时设定了5次迭代就停止的条件,当然也可以设置其他

停止条件。

1.4. Tune的优势

  • 前沿的优化算法
  • 提升效率
    • 仅需要加一点代码就可以适应ray tune
    • 支持多种存储选项以存储实验结果(如NFS、云存储)
  • 支持多GPU、分布式训练
  • 友好支持其他调参工具

2.核心概念

搜索空间

  1. config = {
  2. "uniform": tune.uniform(-5, -1), # Uniform float between -5 and -1
  3. "quniform": tune.quniform(3.2, 5.4, 0.2), # Round to multiples of 0.2
  4. "loguniform": tune.loguniform(1e-4, 1e-1), # Uniform float in log space
  5. "qloguniform": tune.qloguniform(1e-4, 1e-1, 5e-5), # Round to multiples of 0.00005
  6. "randn": tune.randn(10, 2), # Normal distribution with mean 10 and sd 2
  7. "qrandn": tune.qrandn(10, 2, 0.2), # Round to multiples of 0.2
  8. "randint": tune.randint(-9, 15), # Random integer between -9 and 15
  9. "qrandint": tune.qrandint(-21, 12, 3), # Round to multiples of 3 (includes 12)
  10. "lograndint": tune.lograndint(1, 10), # Random integer in log space
  11. "qlograndint": tune.qlograndint(1, 10, 2), # Round to multiples of 2
  12. "choice": tune.choice(["a", "b", "c"]), # Choose one of these options uniformly
  13. "func": tune.sample_from(
  14. lambda spec: spec.config.uniform * 0.01
  15. ), # Depends on other value
  16. "grid": tune.grid_search([32, 64, 128]), # Search over all these values
  17. }

实验(Ray Trial)

只需要传递trainable和搜参空间,就可以重复多次实验,选出最佳参数。

  1. # Pass in a Trainable class or function, along with a search space "config".
  2. tuner = tune.Tuner(trainable, param_space={"a": 2, "b": 4})
  3. tuner.fit()

搜索算法(Tune Search Algorithms)

没有指定的话,默认随机搜索。ray中支持BayesOptSearch、 HyperOpt 、 Optuna等常用优化算法库,这些算法库基于一定的策略去寻找参数,提高了搜索的效率。

  1. from ray.tune.search.bayesopt import BayesOptSearch
  2. from ray import train
  3. # Define the search space
  4. search_space = {"a": tune.uniform(0, 1), "b": tune.uniform(0, 20)}
  5. algo = BayesOptSearch(random_search_steps=4)
  6. tuner = tune.Tuner(
  7. trainable,
  8. tune_config=tune.TuneConfig(
  9. metric="score",
  10. mode="min",
  11. search_alg=algo,
  12. ),
  13. run_config=train.RunConfig(stop={"training_iteration": 20}),
  14. param_space=search_space,
  15. )
  16. tuner.fit()

调度器(Tune Schedulers)

基于每次上报的结果,scheduler决定是否继续或停止响应的trial

  1. from ray.tune.schedulers import HyperBandScheduler
  2. # Create HyperBand scheduler and minimize the score
  3. hyperband = HyperBandScheduler(metric="score", mode="max")
  4. config = {"a": tune.uniform(0, 1), "b": tune.uniform(0, 1)}
  5. tuner = tune.Tuner(
  6. trainable,
  7. tune_config=tune.TuneConfig(
  8. num_samples=20,
  9. scheduler=hyperband,
  10. ),
  11. param_space=config,
  12. )
  13. tuner.fit()

注意,调度器和搜索算法可能并不兼容,调度器有时还需要checkpoint。

结果Tune ResultGrid

可以通过Tune ResultGrid来分析Tune的结果

  1. tuner = tune.Tuner(
  2. trainable,
  3. tune_config=tune.TuneConfig(
  4. metric="score",
  5. mode="min",
  6. search_alg=BayesOptSearch(random_search_steps=4),
  7. ),
  8. run_config=train.RunConfig(
  9. stop={"training_iteration": 20},
  10. ),
  11. param_space=config,
  12. )
  13. results = tuner.fit()
  14. best_result = results.get_best_result() # Get best result object
  15. best_config = best_result.config # Get best trial's hyperparameters
  16. best_logdir = best_result.path # Get best trial's result directory
  17. best_checkpoint = best_result.checkpoint # Get best trial's best checkpoint
  18. best_metrics = best_result.metrics # Get best trial's last results
  19. best_result_df = best_result.metrics_dataframe # Get best result as pandas dataframe

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/236020
推荐阅读
相关标签
  

闽ICP备14008679号