Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support numpy scalars in Trial.user_attrs #706

Merged

Conversation

toshihikoyanase
Copy link
Member

Contributor License Agreement

This repository (optuna-dashboard) and Goptuna share common code.
This pull request may therefore be ported to Goptuna.
Make sure that you understand the consequences concerning licenses and check the box below if you accept the term before creating this pull request.

  • I agree this patch may be ported to Goptuna by other Goptuna contributors.

Reference Issues/PRs

This PR addresses the following TODO comment:

# TODO(c-bata): Support numpy-specific number types.

What does this implement/fix? Explain your changes.

  • This change enables the conversion of numpy scalars to Python's native numerical values during the serialization of FrozenTrial.
  • It recognizes np.integer and np.floating as sortable values.
  • The process of checking for sortable values has been extracted into a private method for better clarity.

Discussion

numpy scalars can only stored in InMemoryStorage since they are not JSON-serializable.
I guess most of users use persistent storages such as RDBStorage and JournalFileStorage with the dashboard, so the change might benefit limited number of users.

Additional Context

Below is the code provided to verify the behavior described.

numpy-scalars-in-user-attrs.py
from __future__ import annotations

import logging
import os.path

import numpy as np
import optuna
from optuna.artifacts import FileSystemArtifactStore
from optuna_dashboard._app import create_app


host = "0.0.0.0"
port = 8080

optuna.logging.set_verbosity(logging.CRITICAL)


def create_optuna_storage() -> optuna.storages.InMemoryStorage:
    storage = optuna.storages.InMemoryStorage()

    # Single-objective study
    study = optuna.create_study(study_name="single-objective-user-attrs", storage=storage)

    def objective_single_user_attr(trial: optuna.Trial) -> float:
        x1 = trial.suggest_float("x1", 0, 10)
        x2 = trial.suggest_float("x2", 0, 10)
        if x1 < 5:
            trial.set_user_attr("X", "foo")
        else:
            trial.set_user_attr("X", "bar")
        trial.set_user_attr("Y", x1 + x2)
        trial.set_user_attr("np.float64", np.float64(x1 + x2))
        trial.set_user_attr("np.int32", np.int32(x1 + x2))
        trial.set_user_attr("int", int(x1 - x2))
        return (x1 - 2) ** 2 + (x2 - 5) ** 2

    study.optimize(objective_single_user_attr, n_trials=100)

    return storage


def main() -> None:
    storage = create_optuna_storage()
    base_path = os.path.join(os.path.dirname(__file__), "artifact")
    if not os.path.exists(base_path):
        os.mkdir(base_path)
    artifact_store = FileSystemArtifactStore(base_path=base_path)
    app = create_app(storage, artifact_store, debug=True)
    app.run(host=host, port=port, reloader=True)


if __name__ == "__main__":
    main()

Copy link

codecov bot commented Nov 22, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c1c6adc) 64.35% compared to head (ad4d29f) 64.41%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #706      +/-   ##
==========================================
+ Coverage   64.35%   64.41%   +0.06%     
==========================================
  Files          35       35              
  Lines        2250     2254       +4     
==========================================
+ Hits         1448     1452       +4     
  Misses        802      802              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@c-bata c-bata self-assigned this Nov 27, 2023
Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you for your pull request.

@c-bata c-bata merged commit 1759389 into optuna:main Nov 27, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants