Repository navigation
fix Int distribution midpoint bias in _SearchSpaceTransform and add tests - #6771
Conversation
|
This pull request has not seen any recent activity. |
c-bata
left a comment
There was a problem hiding this comment.
I confirmed that this PR fixes the midpoint bias. LGTM.
from collections import Counter
import optuna
optuna.logging.set_verbosity(optuna.logging.WARN)
sampler = optuna.samplers.QMCSampler(scramble=False, seed=0, qmc_type="sobol")
study = optuna.create_study(sampler=sampler)
def objective(trial: optuna.Trial) -> float:
x = trial.suggest_int("x", 0, 3)
y = trial.suggest_int("y", 0, 3)
return x ** 2 + y ** 2
study.optimize(objective, n_trials=17)
qmc_trials = study.trials[1:]
x_counts = Counter(t.params["x"] for t in qmc_trials)
y_counts = Counter(t.params["y"] for t in qmc_trials)
print("x:", dict(sorted(x_counts.items())))
print("y:", dict(sorted(y_counts.items())))Before:
$ python tmp/optuna_single.py
/Users/c-bata/go/src/github.com/optuna/optuna/tmp/optuna_single.py:5: ExperimentalWarning: QMCSampler is experimental (supported from v3.0.0). The interface can change in the future.
sampler = optuna.samplers.QMCSampler(scramble=False, seed=0, qmc_type="sobol")
x: {0: 5, 1: 3, 2: 5, 3: 3}
y: {0: 5, 1: 3, 2: 5, 3: 3}
After (This PR):
$ python tmp/optuna_single.py
/Users/c-bata/go/src/github.com/optuna/optuna/tmp/optuna_single.py:5: ExperimentalWarning: QMCSampler is experimental (supported from v3.0.0). The interface can change in the future.
sampler = optuna.samplers.QMCSampler(scramble=False, seed=0, qmc_type="sobol")
x: {0: 4, 1: 4, 2: 4, 3: 4}
y: {0: 4, 1: 4, 2: 4, 3: 4}
| offset = trans_param - (d.low - 0.5 * d.step) | ||
| param = int(np.clip(math.floor(offset / d.step) * d.step + d.low, d.low, d.high)) |
There was a problem hiding this comment.
Computing d.low - 0.5 * d.step before subtracting it from trans_param can lose the half-step for large integer bounds. For example, with IntDistribution(2**52 + 1, 2**52 + 11), transforming and then untransforming d.low returns d.low + 1. Could we subtract d.low first, e.g. floor((trans_param - d.low) / d.step + 0.5), and add a large-integer round-trip test?
d = IntDistribution(2**52 + 1, 2**52 + 11)
transform = _SearchSpaceTransform({"x": d})
transform.untransform(transform.transform({"x": d.low}))
# {"x": 4503599627370498}
# expected: 4503599627370497There was a problem hiding this comment.
Thank you for the recommendation. I have confirmed that is indeed a problem, and modified the order as proposed.
def test_search_space_transform_int_large_low_round_trip() -> None:
distribution = IntDistribution(2**52 + 1, 2**52 + 11)
transform = _SearchSpaceTransform({"x": distribution})
assert transform.untransform(transform.transform({"x": distribution.low})) == {
"x": distribution.low
}I used this test to test the behaviors and it worked this time.
Motivation
This PR removes midpoint bias when untransforming
IntDistributionvalues, first mentioned in https://github.com/optuna/optuna/pull/6742 .Description of the changes
Previously,
_SearchSpaceTransform._untransform_numerical_paramusednp.round(...)for non-log integer distributions. Because NumPy uses round-half-to-even, transformed values that land exactly on step midpoints were systematically pulled toward even indices.This PR changes non-log
IntDistributionuntransforming to use equal-width half-open bins instead of ties-to-even rounding withmath.floor(trans_param - d.low + 0.5 * d.step) / d.step. Updated_SearchSpaceTransform._untransform_numerical_paramfor non-logIntDistributionto use equal-width binning instead ofnp.round(..).Also added the following tests.
tests/test_transform.py: Verifies that midpoint values such as0.5,1.5, and2.5map using equal-width binsrather than ties-to-even rounding.
tests/samplers_tests/test_qmc.py:suggest_int(...)regression tests that assert exact balanced coverage after the independently sampled first trial.RandomSampler,CmaEsSampler,NSGA-IImutation, andNSGA-IIcrossover.