How do I make ray.tune.run reproducible?

无人久伴 提交于 2021-01-29 07:40:10

问题


I'm using Tune class-based Trainable API. See code sample:

from ray import tune
import numpy as np

np.random.seed(42)
# first run
tune.run(tune.Trainable, ...)
# second run, expecting same result
np.random.seed(42)
tune.run(tune.Trainable, ...)

The problem is that tune.run results are still different, likely reason being that each ray actor still has different seed.

Question: how do I make ray.tune.run reproducible?


回答1:


(This answer focuses on class API and ray version 0.8.7. Function API does not support reproducibility due to implementation specifics)

There are two main sources of undeterministic results.

1. Search algorithm

Every search algorithm supports random seed, although interface to it may vary. This initializes hyperparameter space sampling.

For example, if you're using AxSearch, it looks like this:

from ax.service.ax_client import AxClient
from ray.tune.suggest.ax import AxSearch

client = AxClient(..., random_seed=42)
client.create_experiment(...)
algo = AxSearch(client)

2. Trainable API

This is distributed among worker processes, which requires seeding within tune.Trainable class. Depending on the tune.Trainable.train logic that you implement, you need to manually seed numpy, tf, or whatever other framework you use, inside tune.Trainable.setup by passing seed with config argument of tune.run.

The following code is based on RLLib PR5197 that handled the same issue:

See the example:

from ray import tune
import numpy as np
import random

class Tuner(tune.Trainable):
  def setup(self, config):
    seed = config['seed']
    np.random.seed(seed)
    random.seed(seed)
    ...
  ...

seed = 42

tune.run(Tuner, config={'seed': seed})


来源:https://stackoverflow.com/questions/63498865/how-do-i-make-ray-tune-run-reproducible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!