hyperopt中Trials()对象的内容

时间:2019-03-07 13:28:25

标签: python-3.x hyperopt

此查询是指将试验的使用作为fmin中的参数。

trials = Trials()
best = fmin(objective, space=hp.uniform('x', -10, 10), algo=tpe.suggest,
    max_evals=100, trials=trials)

文档说明试验对象具有类似 trials.trials,trials.results,trials.losses() trials.statuses( )

但是,我看到诸如 trials.best_trial trials.trial_attachments 之类的用法未在其中提及文档。

现在我想知道如何获取 试验 对象的所有内容的列表?对象类型为 hyperopt.base.Trials

3 个答案:

答案 0 :(得分:2)

这只是我对Hyperopt代码的调查的部分答案:

有一个._dynamic_trials,用于存储优化中使用的信息。

答案 1 :(得分:2)

如果只想将所有内容转储到屏幕上,则可以执行类似this的操作。这是在Trials对象上使用策略的方式:

from hyperopt import Trials

def dump(obj):
   for attr in dir(obj):
       if hasattr( obj, attr ):
           print( "obj.%s = %s" % (attr, getattr(obj, attr)))

tpe_trials = Trials()

dump(tpe_trials)

这将打印Trials对象的所有属性和方法。我不会在这里包括所有内容,因为它很长,但以下几行内容:

obj.__class__ = <class 'hyperopt.base.Trials'>
obj.__delattr__ = <method-wrapper '__delattr__' of Trials object at 0x0000012880AA3108>
obj.__dict__ = {'_ids': set(), '_dynamic_trials': [], '_exp_key': None, 'attachments': {}, '_trials': []}
obj.__dir__ = <built-in method __dir__ of Trials object at 0x0000012880AA3108>

. . .

obj._ids = set()
obj._insert_trial_docs = <bound method Trials._insert_trial_docs of <hyperopt.base.Trials object at 0x0000012880AA3108>>
obj._trials = []
obj.aname = <bound method Trials.aname of <hyperopt.base.Trials object at 0x0000012880AA3108>>

但是我发现查看源代码更为有用。在__init__函数下声明了一些属性,然后使用@property装饰器声明了一组属性。这些方法都是def

不确定如何设置环境,但文件存储在我的conda环境中的..\env\Lib\site-packages\yperopt\base.py处。 class Trials(object)应该在第228行附近声明。

答案 2 :(得分:1)

根据Hyperopt code: `试用-文件列表,至少包括子文件

['spec'] - the specification of hyper-parameters for a job
['result'] - the result of Domain.evaluate(). Typically includes:
    ['status'] - one of the STATUS_STRINGS
    ['loss'] - real-valued scalar that hyperopt is trying to minimize
['idxs'] - compressed representation of spec
['vals'] - compressed representation of spec
['tid'] - trial id (unique in Trials list)`