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

[bug] Fix an error caused by infinity in trial.values #724

Merged

Conversation

nabenabe0928
Copy link
Collaborator

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 issue discussed in PR#718.
Basically, when trial.values includes inf or -inf, which is a string, but not number, the Number maps them to nan.
Therefore, this PR circumvents this problem by mapping TrialValueNumber to number.

What does this implement/fix? Explain your changes.

This PR simply maps inf to Infinity and -inf to -Infinity.

You can simply check the behavior of the modification using:

type TrialValueNumber = number | "inf" | "-inf"

const convertTrialValueToNumber = (value: TrialValueNumber): number => {
    // TrialValueNumber takes `number`, "inf", or "-inf".
    return typeof(value) === "number" ? value : value.includes("-") ? -Infinity : Infinity
};

const targetValues: TrialValueNumber[] = [1, "inf", "-inf"]
for (const target of targetValues){
    const val = convertTrialValueToNumber(target)
    console.log(`Converted Value is ${val} with the type ${typeof(val)}`)
}

const convertedValues = targetValues.map((v) => convertTrialValueToNumber(v))
console.log(convertedValues.slice().sort((a, b) => a - b))

We can run this file by the following when the file name is debug.ts:

$ tsc debug.ts
$ node debug.js

The output in my environment was:

Converted Value is 1 with the type number
Converted Value is Infinity with the type number
Converted Value is -Infinity with the type number
[ -Infinity, 1, Infinity ]

@nabenabe0928 nabenabe0928 marked this pull request as ready for review December 6, 2023 06:13
Copy link

codecov bot commented Dec 6, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (9e314a9) 67.20% compared to head (bdd2759) 67.20%.
Report is 19 commits behind head on main.

Files Patch % Lines
optuna_dashboard/_app.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #724   +/-   ##
=======================================
  Coverage   67.20%   67.20%           
=======================================
  Files          35       35           
  Lines        2293     2293           
=======================================
  Hits         1541     1541           
  Misses        752      752           

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

Copy link
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

LGTM.

@c-bata c-bata self-assigned this Dec 7, 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 👍

@c-bata c-bata merged commit 1282eb2 into optuna:main Dec 7, 2023
11 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.

None yet

3 participants