|
1
|
|
|
"""Search execution utilities for hyperparameter optimization. |
|
2
|
|
|
|
|
3
|
|
|
Author: Simon Blanke |
|
4
|
|
|
Email: [email protected] |
|
5
|
|
|
License: MIT License |
|
6
|
|
|
""" |
|
7
|
|
|
|
|
8
|
|
|
from .distribution import ( |
|
9
|
|
|
joblib_wrapper, |
|
10
|
|
|
multiprocessing_wrapper, |
|
11
|
|
|
pathos_wrapper, |
|
12
|
|
|
single_process, |
|
13
|
|
|
) |
|
14
|
|
|
from .process import _process_ |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def proxy(args): |
|
18
|
|
|
"""Proxy function.""" |
|
19
|
|
|
return _process_(*args) |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
dist_dict = { |
|
23
|
|
|
"joblib": (joblib_wrapper, _process_), |
|
24
|
|
|
"multiprocessing": (multiprocessing_wrapper, proxy), |
|
25
|
|
|
"pathos": (pathos_wrapper, proxy), |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def _get_distribution(distribution): |
|
30
|
|
|
if callable(distribution): |
|
31
|
|
|
return (distribution, _process_), {} |
|
32
|
|
|
|
|
33
|
|
|
elif isinstance(distribution, dict): |
|
34
|
|
|
dist_key = next(iter(distribution)) |
|
35
|
|
|
dist_paras = distribution[dist_key] |
|
36
|
|
|
|
|
37
|
|
|
return dist_dict[dist_key], dist_paras |
|
38
|
|
|
|
|
39
|
|
|
elif isinstance(distribution, str): |
|
40
|
|
|
return dist_dict[distribution], {} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
def run_search(opt_pros, distribution, n_processes): |
|
44
|
|
|
"""Run Search function.""" |
|
45
|
|
|
process_infos = list(opt_pros.items()) |
|
46
|
|
|
|
|
47
|
|
|
if n_processes == "auto": |
|
48
|
|
|
n_processes = len(process_infos) |
|
49
|
|
|
|
|
50
|
|
|
if n_processes == 1: |
|
51
|
|
|
results_list = single_process(_process_, process_infos) |
|
52
|
|
|
else: |
|
53
|
|
|
(distribution, process_func), dist_paras = _get_distribution(distribution) |
|
54
|
|
|
|
|
55
|
|
|
results_list = distribution(process_func, process_infos, n_processes) |
|
56
|
|
|
|
|
57
|
|
|
return results_list |
|
58
|
|
|
|