I am trying to user hyperopt.fmin
to optimize the hyperparameters of a XGBoost classifier. I have an objection function:
from hyperopt import fmin
def objective(space, x1, x2):
'do whatever to define loss_array'
return {'loss': -loss_array.mean(), 'loss_variance': np.var(loss_array, ddof=1),'status': STATUS_OK, 'scores':score_dataframe}
I'd like to have the optional argument x1
and x2
(for e.g., if I want to specify a different loss function).
Then, I can minimize:
argmin = fmin(
fn=objective,
space=space,
algo=tpe.suggest,
max_evals = 700,
trials=trials,
rstate = np.random.RandomState(1),
max_queue_len=8,
early_stop_fn=no_progress_loss_custom(50,0) # Stops hyperopt tuning when loss does not improve after x iterations. Non custom version does not work with SparkTrials
)
How can I make fn=objective
to accept the two additional positional arguments?
So long as x1 and x2 are specified at the fmin scope, you can use a lambda function:
fn=lambda space, x1=x1, x2=x2: objective(space, x1, x2),