简单的并行化

并行化非常直接 optuna.study.Study.optimize().

如果你想要手动执行 Optuna 的优化:

  1. 启动一个 RDB 服务器(在本例中我们用 MySQL)

  2. 通过 –storage 参数创建 study

  3. 在多个节点和进程之间共享 study

当然,你可以像 the kubernetes examples 里一样使用 Kubernetes.

要看并行优化怎么进行的化,请查看下面这个视频。

创建 study

你可以通过 optuna create-study 来创建 study.或者在 Python 脚本中通过 optuna.create_study() 也行。

$ mysql -u root -e "CREATE DATABASE IF NOT EXISTS example"
$ optuna create-study --study-name "distributed-example" --storage "mysql://root@localhost/example"
[I 2020-07-21 13:43:39,642] A new study created with name: distributed-example

然后写一个优化脚本。假设 foo.py 包含了以下代码。

import optuna


def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    return (x - 2) ** 2


if __name__ == "__main__":
    study = optuna.load_study(
        study_name="distributed-example", storage="mysql://root@localhost/example"
    )
    study.optimize(objective, n_trials=100)

在多个节点和进程之间分享该 study

最后,在多个进程中运行这个共享的 study. 比如,在一个终端中运行 Process 1 而在另一个终端中运行 Process 2. 它们基于共享参数的历史记录来获取参数 suggestion.

Process 1:

$ python foo.py
[I 2020-07-21 13:45:02,973] Trial 0 finished with value: 45.35553104173011 and parameters: {'x': 8.73465151598285}. Best is trial 0 with value: 45.35553104173011.
[I 2020-07-21 13:45:04,013] Trial 2 finished with value: 4.6002397305938905 and parameters: {'x': 4.144816945707463}. Best is trial 1 with value: 0.028194513284051464.
...

Process 2 (the same command as process 1):

$ python foo.py
[I 2020-07-21 13:45:03,748] Trial 1 finished with value: 0.028194513284051464 and parameters: {'x': 1.8320877810162361}. Best is trial 1 with value: 0.028194513284051464.
[I 2020-07-21 13:45:05,783] Trial 3 finished with value: 24.45966755098074 and parameters: {'x': 6.945671597566982}. Best is trial 1 with value: 0.028194513284051464.
...

备注

我们不建议在大规模的分布式优化中采用 SQLite 因为其可能造成死锁和烟zhngde性能问题。请考虑其他数据库引擎,比如 PostgreSQL 或者 MySQL.

备注

在分布式优化里,请不要将 SQLite 数据库放在网络文件系统上。参见 https://www.sqlite.org/faq.html#q5

Total running time of the script: ( 0 minutes 0.000 seconds)

Gallery generated by Sphinx-Gallery