from hyperopt import fmin, tpe, hp
best = fmin(fn=lambda x: x ** 2,
space=hp.uniform('x', -10, 10),
algo=tpe.suggest,
max_evals=100)
print(best)
results in
TypeError Traceback (most recent call last)
<ipython-input-1-d4c67c96f9c0> in <module>()
3 space=hp.uniform('x', -10, 10),
4 algo=tpe.suggest,
----> 5 max_evals=100)
6 print(best)
~/.local/lib/python3.6/site-packages/hyperopt/fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin)
312
313 domain = base.Domain(fn, space,
--> 314 pass_expr_memo_ctrl=pass_expr_memo_ctrl)
315
316 rval = FMinIter(algo, domain, trials, max_evals=max_evals,
~/.local/lib/python3.6/site-packages/hyperopt/base.py in __init__(self, fn, expr, workdir, pass_expr_memo_ctrl, name, loss_target)
784 before = pyll.dfs(self.expr)
785 # -- raises exception if expr contains cycles
--> 786 pyll.toposort(self.expr)
787 vh = self.vh = VectorizeHelper(self.expr, self.s_new_ids)
788 # -- raises exception if v_expr contains cycles
~/.local/lib/python3.6/site-packages/hyperopt/pyll/base.py in toposort(expr)
713 G.add_edges_from([(n_in, node) for n_in in node.inputs()])
714 order = nx.topological_sort(G)
--> 715 assert order[-1] == expr
716 return order
717
TypeError: 'generator' object is not subscriptable
This is hyperopt-0.1 freshly installed via pip install.
$ python --version
Python 3.6.4
Btw, commenting out that assert seemingly fixes the issue.
short answer: install from source, this is fixed in master (the hyperopt version on pip is pretty old)
slightly longer answer: a few years ago, nx.topological_sort was changed to return a generator instead of a list, so that assert will fail because it tries to index into a generator, but there is no problem if the subsequent code just iterates over the generator.
you can use pip install --upgrade git+git://github.com/hyperopt/hyperopt.git to upgrade hyperopt
Most helpful comment
short answer: install from source, this is fixed in master (the hyperopt version on pip is pretty old)
slightly longer answer: a few years ago, nx.topological_sort was changed to return a generator instead of a list, so that assert will fail because it tries to index into a generator, but there is no problem if the subsequent code just iterates over the generator.