test_ipy.py
2.05 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
To use this test script, there should be a cluster of ipython parallel engines
instantiated already. Their working directory should be the current
directory: hyperopt/tests
To start the engines in hyperopt/hyperopt/tests/
use: $ ipcluster start --n=2
"""
from __future__ import print_function
import sys
from nose import SkipTest
try:
from IPython.parallel import Client
except ImportError:
print("Skipping IPython Tests (IPython not found)", file=sys.stderr)
raise SkipTest("IPython not present")
from hyperopt.ipy import IPythonTrials
import hyperopt.hp
import hyperopt.tpe
import hyperopt
def test0():
try:
client = Client(debug=True)
except IOError:
raise SkipTest()
client[:].use_cloudpickle()
trials = IPythonTrials(client, "log")
def simple_objective(args):
# -- why are these imports here !?
# -- is it because they need to be imported on the client?
#
# Yes, the client namespace is empty, so some imports may be
# needed here. Errors on the engines can be found by
# using debug=True when instantiating the Client.
import hyperopt
return {"loss": args ** 2, "status": hyperopt.STATUS_OK}
space = hyperopt.hp.uniform("x", 0, 1)
minval = trials.fmin(
simple_objective,
space=space,
algo=hyperopt.tpe.suggest,
max_evals=25,
verbose=True,
)
print(minval)
assert minval["x"] < 0.2
def test_fmin_fn():
try:
client = Client()
except IOError:
raise SkipTest()
client[:].use_cloudpickle()
trials = IPythonTrials(client, "log")
assert not trials._testing_fmin_was_called
def simple_objective(args):
import hyperopt
return {"loss": args ** 2, "status": hyperopt.STATUS_OK}
space = hyperopt.hp.uniform("x", 0, 1)
minval = hyperopt.fmin(
simple_objective,
space=space,
algo=hyperopt.tpe.suggest,
max_evals=25,
trials=trials,
)
assert minval["x"] < 0.2
assert trials._testing_fmin_was_called