Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

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

I want suggest ratio in Optuna.

The ratio is X_1, X_2, ..., X_k bounded to ∑X_i = 1 and 0 <= X_i <= 1 for all i.

Optuna doesn't offer Dirichlet distribution.

I tried this but it doesn't work.

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)

And I tried this and finished without any errors. However, this is inconsistent because degree of freedom is k - 1 for the bound but suggest k times.

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)

How can I suggest ratio or multiple variables with bound?

This is a simple example so it's differentiable but I need variables in more complex objective.

question from:https://stackoverflow.com/questions/65713063/how-to-suggest-multivariate-of-ratio-with-bound-in-optuna

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This worked.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...