简体   繁体   English

如何使用optuna或hyperopt调整条件目标函数

[英]How to tune conditional objective function using optuna or hyperopt

I tried to use optuna to tune hyperparameters. 我尝试使用optuna调整超参数。 But my objective function is conditional which creates issues in getting optimal parameters. 但是我的目标函数是有条件的,这在获取最佳参数时会产生问题。

i want to get cwc only if the condtion is met otherwise continue trial for next hyperparameters. 我只想在满足条件的情况下获得CWC,否则请继续尝试下一个超参数。 But i guess since the condition is not met and objective func reurns cwc it gives error 但是我想因为条件不满足并且目标函数重播了cwc它给出了错误

UnboundLocalError: local variable 'cwc_train' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cwc_train”

define objective (trial):
    k_dis = trial.suggest_uniform('k_dis', 0.0, 5.0)
    l_dis = trial.suggest_uniform('l_dis', 0.0, 5.0)
    k_bound = trial.suggest_uniform('k_bound', 0.0, 5.0)
    l_bound = trial.suggest_uniform('l_bound', 0.0, 5.0) 

    picp = .....
    pinrw = .....


    if picp_train >= 0.8 and pinrw_train < 0.18: 
        cwc_train = fc.CWC_proposed(predict_bound_train, Y_train)
    else:
        print("error = ")
    return  cwc_train
study = optuna.create_study()
study.optimize(objective, n_trials=100)

UnboundLocalError: local variable 'cwc_train' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cwc_train”

i want to get cwc only if the condtion is met otherwise continue trial for next hyperparameters. 我只想在满足条件的情况下获得CWC,否则请继续尝试下一个超参数。

In this case, please raise optuna.structs.TrialPruned instead of returning cwc_train. 在这种情况下,请引发optuna.structs.TrialPruned而不是返回cwc_train。 Note that the default sampler ( TPESampler ) is aware of pruned solutions so that it can lower the probability of re-sampling. 请注意,默认采样器( TPESampler )知道修剪的解决方案,因此可以降低重新采样的可能性。

if picp_train >= 0.8 and pinrw_train < 0.18: 
    cwc_train = fc.CWC_proposed(predict_bound_train, Y_train)
    return cwc_train
raise optuna.structs.TrialPruned()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM