-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathhyperopt_imize.py
More file actions
executable file
·54 lines (45 loc) · 1.62 KB
/
hyperopt_imize.py
File metadata and controls
executable file
·54 lines (45 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
from argparse import ArgumentParser
from hyperopt import fmin, hp, STATUS_OK, tpe, Trials
import subprocess
import sys
import time
def function(params):
x, y = params
cmd = ['./continuous_process.py', '--x', str(x),
'--y', str(y)]
process = subprocess.run(cmd, stdout=subprocess.PIPE,
encoding='utf8')
return {
'loss': float(process.stdout), 'time': time.time(),
'x': x, 'y': y, 'status': STATUS_OK,
}
def optimize(max_evals):
space = (hp.uniform('x', -3.0, 3.0),
hp.uniform('y', -3.0, 3.0))
trials = Trials()
best = fmin(function, space=space, algo=tpe.suggest,
max_evals=max_evals, trials=trials)
return best, trials
def main():
arg_parser = ArgumentParser(description='optimize external '
'process')
arg_parser.add_argument('--max-evals', type=int,
default=100, help='maximum evals')
arg_parser.add_argument('--trials', action='store_true',
help='display trials')
options = arg_parser.parse_args()
best, trials = optimize(options.max_evals)
if options.trials:
print('x y value')
for trial in trials:
x = trial['result']['x']
y = trial['result']['y']
value = -trial['result']['loss']
print(f'{x:6.3f} {y:6.3f} {value:10.3e}')
x, y = best['x'], best['y']
value = function((x, y))['loss']
print(f'# best: f({x:6.3f}, {y:6.3f}) = {-value:10.3e}')
return 0
if __name__ == '__main__':
sys.exit(main())