Add `__str__` to Optuna components.
See original GitHub issueMotivation
When we use experiment management frameworks like Hydra with Optuna, we sometimes want to log the Optuna’s settings including sampler types. Currently, the sampler class does not have __repr__
or __str__
and the messages are less human-friendly if we simply print the sampler instance:
[HYDRA] Sampler: <optuna.samplers._tpe.sampler.TPESampler object at 0x7fd7901d6d30>
Adding the __str__
, we can simplify the logging in the experiment management. I think we can find similar requests during ML pipeline development.
c.f., https://github.com/facebookresearch/hydra/pull/1503
Description
Add __str__
to optuna.samplers
classes.
I guess the minimum requirement is the class name as can be seen in https://github.com/facebookresearch/hydra/pull/1503.
class RandomSampler(BaseSampler):
...
def __str__(self):
return self.__class__.__name__
I think we can also implement similar behavior to pruners and storages, but we may need design discussions.
Additional context (optional)
See also @omry’s comment: https://github.com/facebookresearch/hydra/pull/1503#discussion_r602631532
__str__
or __repr___
I originally suggested to implement __repr__
, but __str__
is more appropriate since we cannot ensure Sampler.__repr__
returns reconstructable strings. This is because some samplers like NSGAIISampler
have user-defined functions, which might not provide reconstructable strings by __repr__
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Yeah, seems noisy. One option is to write those by hand per class, if you have a small number of such classes it’s not too bad. Another is to have something generic but flexible.
I got something pretty fancy here.
It’s a generic function in the base class that is calling _get_attributes and _get_flags methods from the subclass that are then used to determine how to create the string representation.
The generated output can actually be used directly to instantiate the represented objects in my case, it’s very handy when creating tests (I can copy the expected string representation and use directly as code).
Feel free to borrow the idea/code.
Since this issue has been open for a long time, let me close once. Feel free to reopen upon necessity.