Return the result of a randomly-chosen suggest function For example to search by sometimes using random search, sometimes anneal, and sometimes tpe, type: fmin(..., algo=partial(mix.suggest, p_suggest=[ (.1, rand.suggest),
(new_ids, domain, trials, seed, p_suggest)
| 2 | |
| 3 | |
| 4 | def suggest(new_ids, domain, trials, seed, p_suggest): |
| 5 | """Return the result of a randomly-chosen suggest function |
| 6 | |
| 7 | For example to search by sometimes using random search, sometimes anneal, |
| 8 | and sometimes tpe, type: |
| 9 | |
| 10 | fmin(..., |
| 11 | algo=partial(mix.suggest, |
| 12 | p_suggest=[ |
| 13 | (.1, rand.suggest), |
| 14 | (.2, anneal.suggest), |
| 15 | (.7, tpe.suggest),]), |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | Parameters |
| 20 | ---------- |
| 21 | |
| 22 | p_suggest: list of (probability, suggest) pairs |
| 23 | Make a suggestion from one of the suggest functions, |
| 24 | in proportion to its corresponding probability. |
| 25 | sum(probabilities) must be [close to] 1.0 |
| 26 | |
| 27 | """ |
| 28 | rng = np.random.default_rng(seed) |
| 29 | ps, suggests = list(zip(*p_suggest)) |
| 30 | assert len(ps) == len(suggests) == len(p_suggest) |
| 31 | if not np.isclose(sum(ps), 1.0): |
| 32 | raise ValueError("Probabilities should sum to 1", ps) |
| 33 | idx = rng.multinomial(n=1, pvals=ps).argmax() |
| 34 | return suggests[idx](new_ids, domain, trials, seed=rng.integers(2 ** 31)) |