如何在 Optuna 中建议多变量比率(有界限)?

How to suggest multivariate of ratio (with bound) in Optuna?

我想要 Optuna 中的建议比例。

所有 i 的比率 X_1, X_2, ..., X_k∑X_i = 10 <= X_i <= 1 相关。

Optuna 不提供 Dirichlet 分布。

我试过了,但没用。

def objective(trial):
    k = 10
    ratios = np.zeros(k)
    
    residual = 1
    for i in range(k - 1):
        ratios[i] = trial.suggest_float(f'ratio_{i}', 0, residual)
        residual -= ratios[i]
        
#     ratios[k - 1] = trial.suggest_float(f'ratio_{k - 1}', residual, residual)
    ratios[k - 1] = residual
    return np.log(ratios).sum()

study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=20)

我尝试了这个并且没有任何错误地完成了。然而,这是不一致的,因为自由度是 k - 1 的边界,但建议 k 次。

def objective(trial):
    k = 10
    ratios = np.zeros(k)
    
    for i in range(k):
        ratios[i] = trial.suggest_float(f'ratio_{i}', 0, 1)
    
    ratios /= ratios.sum()
    return np.log(ratios).sum()

study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=20)

我如何建议比率或多个具有界限的变量?

这是一个简单的例子,所以它是可微的,但我需要更复杂的变量 objective。

这有效。

class Objective:
    def __init__(self):
        self.max = 1
    def __call__(self, trial):
        k = 10
        ratios = np.zeros(k)

        for i in range(k):
            ratios[i] = trial.suggest_float(f'ratio_{i}', 0, self.max)

        ratios /= ratios.sum()
        self.max = (self.max + ratios.max()) / 2
        return np.log(ratios).sum()

study = optuna.create_study(direction='maximize')
study.optimize(Objective(), n_trials=100)