如何防止“ hyperopt”将失败的模型结果集成到参数更新中?

时间:2019-03-08 18:28:32

标签: python neural-network hyperopt

在python中使用hyperopt库,我想优化神经网络的参数。有时,选择的参数组合会导致模型不稳定,从而导致模型构建过程崩溃。

现在,我创建了一个try / except异常处理程序,该处理程序可防止整个超参数优化过程停止。我面临的问题是,hyperopt更新仍会整合失败模型的(任意选择的)损失结果,以告知后续参数选择。我希望hyperopt忽略失败的模型。我的目标函数如下:

def objective_fn_for_ann_hyperopt(params, nfolds=5):

     config.ITERATION += 1
     try:
            model = h2o.estimators.H2ODeepLearningEstimator(
            activation='rectifier_with_dropout',
            rho=params['rho'],
            epsilon=params['epsilon'],
            max_w2=10.,
            epochs=params['epochs'],
            hidden=hidden,
            nfolds=nfolds,
            hidden_dropout_ratios=hidden_dropout_ratios,
            input_dropout_ratio=params['input_dropout_ratio'],
            l2=params['l2'],
            l1=1e-5,
            distribution=params['distribution'],
            stopping_metric='mse',
            stopping_tolerance=0.05,
            stopping_rounds=15,

            keep_cross_validation_predictions=True,
            fold_assignment="Modulo",

        )

            model.train(config.x, config.y, training_frame=config.train,
                        validation_frame=config.test)


            run_time = timer() - start

            loss = model._model_json['output']['cross_validation_metrics_summary'].as_data_frame(
            ).iloc[5]['mean']


            success = STATUS_OK #'ok'
        except:

            success = STATUS_FAIL  #'fail'              
            loss = 0 #arbitrary number
            run_time = timer() - start


        return {'loss': loss, 'params': params, 'iteration': config.ITERATION,

                'train_time': run_time, 'status': success}

如何与hyperopt更新器通信,而不是整合失败模型的信息?

1 个答案:

答案 0 :(得分:0)

可以通过从返回的字典中省略丢失密钥或将其设置为None来解决此问题。

此请求请求中已对此进行了讨论-https://github.com/hyperopt/hyperopt/pull/176