Pass arguments to callback
See original GitHub issueI would like to be able to give heavy objects computed in optimize(trial) as parameters to the callback(study, trial).
Motivation
I want to save predictions when I have an improvement, that is when study.best_trial.number == trial.number.
Description
My optimize(trial) function performs cross-validation, and I would like to be able to reuse the fitted estimators to make my predictions.
The models are too heavy to be stored in trial.user_attrs.
Propositions
I think there are 2 options:
-
provide a field
callback_datainFrozenTrialthat can be set fromobjective(trial)withtrial.set_callback_data(value)ortrial.set_callback_data(key, value) -
allow
objective(trial)to return more complex values, e.g. through an objectResult(value, *args, **kwargs)that is then used oncallback(study, trial, *args, **kwargs)
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
Yes. Contrary to a lambda function, the output of partial is picklable if its arguments are (the function is importable and the arguments are pickable) so I believe it should work. If I understand you problem correctly, I don’t think the arguments will be sent to the process anyway in this case, they will just be there in memory after forking the main process.
FYI
functools.partialmight be a simpler and more idiomatic solution as well.