question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `__str__` to Optuna components.

See original GitHub issue

Motivation

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
omrycommented, Apr 7, 2021

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.

0reactions
g-vottecommented, Aug 31, 2022

Since this issue has been open for a long time, let me close once. Feel free to reopen upon necessity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optuna Documentation
pip install git+https://github.com/optuna/optuna.git ... A key is supposed to be a str, and a value be any object serializable with ...
Read more >
Optuna Guide: How to Monitor Hyper-Parameter Optimization ...
In this article, we'll explore how to set hyper-parameters for complex models using a popular framework, Optuna.
Read more >
Optuna Your Model Hyperparameters - Gretel.ai
We explore the popular open-source package Optuna to demonstrate how you can optimize ... project_name = "Tuning Experiment" + str(seconds).
Read more >
An Introduction to the Implementation of Optuna, a ... - Medium
Understand the main components of Optuna and how they are called ... by the objective function, so it can be a string, integer,...
Read more >
Efficient Hyperparameter Optimization with Optuna Framework
It is an open-source framework for efficient and automatic hyperparameter optimization. We will take a closer look at its components and optimization methods....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found