In my setting I have an abstract situation like the following, which shall only function as an example case:
base = trial.suggest_int(1, 3)
power = trial.suggest_int(1, 10)
# value = base ** power
As when the base == 1
the power parameter becomes irrelevant and I would like to fix it to 1.
For example:
base = trial.suggest_int("base", 1, 3)
if base == 1:
# Different distribution! But still inside the other.
power = trial.suggest_int("power", 1, 1)
else:
power = trial.suggest_int("power", 1, 10)
While this works it later causes problems in the form of ValueError
s because the underlying distributions are not the same.
How can I suggest a fixed value with the same parameter name that depends on another value that is sampled within the trial?
Avoid dynamic redefinition of the same parameter name, instead of trying to reuse "power"
with different distributions (which breaks optuna’s assumption of fixed search space), use a dummy name like "power_if_base1"
and manually set power = 1
when base == 1
. optuna won’t error out, and you preserve search consistency.
wrap conditional logic outside of the sampler, this keeps optuna and your logic clean:
base = trial.suggest_int("base",1,3)
power = 1 if base == 1 else trial.suggest_int("power",2,10)
this avoids declaring a 1–1 distribution that optuna sees as a conflict later.