【笔记】ray.tune :超参最优化(1)

该博客展示了如何利用Ray Tune库进行超参数优化。通过grid_search和choice方法,作者配置了训练函数,分别对alpha和beta参数进行搜索。在训练过程中,观察到不同参数组合对mean_loss的影响,并在训练结束后,得到了最佳的配置参数。
摘要由CSDN通过智能技术生成

注:

其中        "beta": tune.choice([1, 2, 3]),choice()返回的是实参序列(不一定是列表)中随机下标的值(概率均等)。

from ray import tune


def objective(step, alpha, beta):
    print(step,alpha,beta)
    print((0.1 + alpha * step / 100)**(-1) + beta * 0.1)
    print("*******************")
    return (0.1 + alpha * step / 100)**(-1) + beta * 0.1


def training_function(config):
    # Hyperparameters
    alpha, beta = config["alpha"], config["beta"]
    print(beta)
    # input()
    for step in range(10):
        # Iterative training function - can be any arbitrary training procedure.
        intermediate_score = objective(step, alpha, beta)
        # Feed the score back back to Tune.
        tune.report(mean_loss=intermediate_score)


analysis = tune.run(
    training_function,
    config={
        "alpha": tune.grid_search([0.001, 0.01, 0.1]),
        "beta": tune.choice([1, 2, 3])
    })

print("Best config: ", analysis.get_best_config(
    metric="mean_loss", mode="min"))


2021-10-15 12:30:14,862	WARNING function_runner.py:559 -- Function checkpointing is disabled. This may result in unexpected behavior when using checkpointing features or certain schedulers. To enable, set the train function arguments to be `func(config, checkpoint_dir=None)`.
2021-10-15 12:30:15,397	WARNING tune.py:562 -- Tune detects GPUs, but no trials are using GPUs. To enable trials to use GPUs, set tune.run(resources_per_trial={'gpu': 1}...) which allows Tune to expose 1 GPU to each trial. You can also override `Trainable.default_resource_request` if using the Trainable API.
== Status ==
Memory usage on this node: 7.2/15.4 GiB
Using FIFO scheduling algorithm.
Resources requested: 1.0/16 CPUs, 0/1 GPUs, 0.0/5.88 GiB heap, 0.0/2.94 GiB objects (0.0/1.0 accelerator_type:RTX)
Result logdir: /home/wangbin/ray_results/training_function_2021-10-15_12-30-14
Number of trials: 3/3 (2 PENDING, 1 RUNNING)
+-------------------------------+----------+-------+---------+--------+
| Trial name                    | status   | loc   |   alpha |   beta |
|-------------------------------+----------+-------+---------+--------|
| training_function_97d87_00000 | RUNNING  |       |   0.001 |      1 |
| training_function_97d87_00001 | PENDING  |       |   0.01  |      1 |
| training_function_97d87_00002 | PENDING  |       |   0.1   |      3 |
+-------------------------------+----------+-------+---------+--------+


Result for training_function_97d87_00001:
  date: 2021-10-15_12-30-15
  done: false
  experiment_id: 433534e7022d41f2a8d81defb1ea9639
  hostname: wangbin-ZHENGJIUZHE-REN9000K-34IMZ
  iterations_since_restore: 1
  mean_loss: 10.1
  neg_mean_loss: -10.1
  node_ip: 10.16.155.112
  pid: 17550
  time_since_restore: 0.00013875961303710938
  time_this_iter_s: 0.00013875961303710938
  time_total_s: 0.00013875961303710938
  timestamp: 1634272215
  timesteps_since_restore: 0
  training_iteration: 1
  trial_id: 97d87_00001
  
Result for training_function_97d87_00000:
  date: 2021-10-15_12-30-15
  done: false
  experiment_id: a28d480c392a4859a1bd879f76156f51
  hostname: wangbin-ZHENGJIUZHE-REN9000K-34IMZ
  iterations_since_restore: 1
  mean_loss: 10.1
  neg_mean_loss: -10.1
  node_ip: 10.16.155.112
  pid: 17551
  time_since_restore: 0.0001780986785888672
  time_this_iter_s: 0.0001780986785888672
  time_total_s: 0.0001780986785888672
  timestamp: 1634272215
  timesteps_since_restore: 0
  training_iteration: 1
  trial_id: 97d87_00000
  
Result for training_function_97d87_00002:
  date: 2021-10-15_12-30-15
  done: false
  experiment_id: 4495f20a45ec41e8bccb3bc978f7d775
  hostname: wangbin-ZHENGJIUZHE-REN9000K-34IMZ
  iterations_since_restore: 1
  mean_loss: 10.3
  neg_mean_loss: -10.3
  node_ip: 10.16.155.112
  pid: 17508
  time_since_restore: 0.0001246929168701172
  time_this_iter_s: 0.0001246929168701172
  time_total_s: 0.0001246929168701172
  timestamp: 1634272215
  timesteps_since_restore: 0
  training_iteration: 1
  trial_id: 97d87_00002
  
Result for training_function_97d87_00000:
  date: 2021-10-15_12-30-15
  done: true
  experiment_id: a28d480c392a4859a1bd879f76156f51
  experiment_tag: 0_alpha=0.001,beta=1
  hostname: wangbin-ZHENGJIUZHE-REN9000K-34IMZ
  iterations_since_restore: 10
  mean_loss: 10.091008092716553
  neg_mean_loss: -10.091008092716553
  node_ip: 10.16.155.112
  pid: 17551
  time_since_restore: 0.026308536529541016
  time_this_iter_s: 0.005055665969848633
  time_total_s: 0.026308536529541016
  timestamp: 1634272215
  timesteps_since_restore: 0
  training_iteration: 10
  trial_id: 97d87_00000
  
Result for training_function_97d87_00001:
  date: 2021-10-15_12-30-15
  done: true
  experiment_id: 433534e7022d41f2a8d81defb1ea9639
  experiment_tag: 1_alpha=0.01,beta=1
  hostname: wangbin-ZHENGJIUZHE-REN9000K-34IMZ
  iterations_since_restore: 10
  mean_loss: 10.010802775024777
  neg_mean_loss: -10.010802775024777
  node_ip: 10.16.155.112
  pid: 17550
  time_since_restore: 0.022693395614624023
  time_this_iter_s: 0.0023262500762939453
  time_total_s: 0.022693395614624023
  timestamp: 1634272215
  timesteps_since_restore: 0
  training_iteration: 10
  trial_id: 97d87_00001
  
Result for training_function_97d87_00002:
  date: 2021-10-15_12-30-15
  done: true
  experiment_id: 4495f20a45ec41e8bccb3bc978f7d775
  experiment_tag: 2_alpha=0.1,beta=3
  hostname: wangbin-ZHENGJIUZHE-REN9000K-34IMZ
  iterations_since_restore: 10
  mean_loss: 9.474311926605504
  neg_mean_loss: -9.474311926605504
  node_ip: 10.16.155.112
  pid: 17508
  time_since_restore: 0.022800445556640625
  time_this_iter_s: 0.0011701583862304688
  time_total_s: 0.022800445556640625
  timestamp: 1634272215
  timesteps_since_restore: 0
  training_iteration: 10
  trial_id: 97d87_00002
  
== Status ==
Memory usage on this node: 7.1/15.4 GiB
Using FIFO scheduling algorithm.
Resources requested: 0/16 CPUs, 0/1 GPUs, 0.0/5.88 GiB heap, 0.0/2.94 GiB objects (0.0/1.0 accelerator_type:RTX)
Result logdir: /home/wangbin/ray_results/training_function_2021-10-15_12-30-14
Number of trials: 3/3 (3 TERMINATED)
+-------------------------------+------------+-------+---------+--------+----------+--------+------------------+-----------------+
| Trial name                    | status     | loc   |   alpha |   beta |     loss |   iter |   total time (s) |   neg_mean_loss |
|-------------------------------+------------+-------+---------+--------+----------+--------+------------------+-----------------|
| training_function_97d87_00000 | TERMINATED |       |   0.001 |      1 | 10.091   |     10 |        0.0263085 |       -10.091   |
| training_function_97d87_00001 | TERMINATED |       |   0.01  |      1 | 10.0108  |     10 |        0.0226934 |       -10.0108  |
| training_function_97d87_00002 | TERMINATED |       |   0.1   |      3 |  9.47431 |     10 |        0.0228004 |        -9.47431 |
+-------------------------------+------------+-------+---------+--------+----------+--------+------------------+-----------------+


(ImplicitFunc pid=17550) 1
(ImplicitFunc pid=17550) 0 0.01 1
(ImplicitFunc pid=17550) 10.1
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 1 0.01 1
(ImplicitFunc pid=17550) 10.090009990009989
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 2 0.01 1
(ImplicitFunc pid=17550) 10.08003992015968
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 3 0.01 1
(ImplicitFunc pid=17550) 10.070089730807577
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 4 0.01 1
(ImplicitFunc pid=17550) 10.0601593625498
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 5 0.01 1
(ImplicitFunc pid=17550) 10.050248756218904
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 6 0.01 1
(ImplicitFunc pid=17550) 10.040357852882703
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 7 0.01 1
(ImplicitFunc pid=17550) 10.030486593843097
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 8 0.01 1
(ImplicitFunc pid=17550) 10.02063492063492
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17550) 9 0.01 1
(ImplicitFunc pid=17550) 10.010802775024777
(ImplicitFunc pid=17550) *******************
(ImplicitFunc pid=17551) 1
(ImplicitFunc pid=17551) 0 0.001 1
(ImplicitFunc pid=17551) 10.1
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 1 0.001 1
(ImplicitFunc pid=17551) 10.09900009999
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 2 0.001 1
(ImplicitFunc pid=17551) 10.098000399920014
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 3 0.001 1
(ImplicitFunc pid=17551) 10.09700089973008
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 4 0.001 1
(ImplicitFunc pid=17551) 10.096001599360255
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 5 0.001 1
(ImplicitFunc pid=17551) 10.095002498750624
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 6 0.001 1
(ImplicitFunc pid=17551) 10.094003597841294
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 7 0.001 1
(ImplicitFunc pid=17551) 10.093004896572399
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 8 0.001 1
(ImplicitFunc pid=17551) 10.092006394884093
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17551) 9 0.001 1
(ImplicitFunc pid=17551) 10.091008092716553
(ImplicitFunc pid=17551) *******************
(ImplicitFunc pid=17508) 3
(ImplicitFunc pid=17508) 0 0.1 3
(ImplicitFunc pid=17508) 10.3
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 1 0.1 3
(ImplicitFunc pid=17508) 10.200990099009902
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 2 0.1 3
(ImplicitFunc pid=17508) 10.10392156862745
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 3 0.1 3
(ImplicitFunc pid=17508) 10.00873786407767
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 4 0.1 3
(ImplicitFunc pid=17508) 9.915384615384616
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 5 0.1 3
(ImplicitFunc pid=17508) 9.823809523809524
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 6 0.1 3
(ImplicitFunc pid=17508) 9.733962264150943
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 7 0.1 3
(ImplicitFunc pid=17508) 9.645794392523364
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 8 0.1 3
(ImplicitFunc pid=17508) 9.559259259259258
(ImplicitFunc pid=17508) *******************
(ImplicitFunc pid=17508) 9 0.1 3
(ImplicitFunc pid=17508) 9.474311926605504
(ImplicitFunc pid=17508) *******************
2021-10-15 12:30:16,068	INFO tune.py:617 -- Total run time: 1.21 seconds (0.56 seconds for the tuning loop).
Best config:  {'alpha': 0.1, 'beta': 3}

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值