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

Is it possible log a variable in the objective function? #2520

Closed
yywangvr opened this issue Mar 25, 2021 · 3 comments
Closed

Is it possible log a variable in the objective function? #2520

yywangvr opened this issue Mar 25, 2021 · 3 comments
Labels
question Question about Optuna.

Comments

@yywangvr
Copy link

import optuna

def objective(trial):
    x = trial.suggest_float("x", 0, 5)
    y = trial.suggest_float("y", 0, 3)

    v0 = 4 * x ** 2 + 4 * y ** 2
    v1 = (x - 5) ** 2 + (y - 5) ** 2
    v=v0+v1
    return v

Is it possible to log the value of v0 and v1 in some functions such asstudy.trials_dataframe. As I also wish to analyse the intermediate values in the objective functions.

@yywangvr yywangvr added the question Question about Optuna. label Mar 25, 2021
@nzw0301
Copy link
Collaborator

nzw0301 commented Mar 25, 2021

Hi @yywangvr, Yes! I think we can store such values in Trial.user_attrs. To do so, we call Trial.set_user_attr during each objective evaluation. For your example

import optuna

def objective(trial):
    x = trial.suggest_float("x", 0, 5)
    y = trial.suggest_float("y", 0, 3)

    v0 = 4 * x ** 2 + 4 * y ** 2
    v1 = (x - 5) ** 2 + (y - 5) ** 2
    trial.set_user_attr("v0", v0)
    trial.set_user_attr("v1", v1)
    v= v0 + v1
    return v

@yywangvr
Copy link
Author

Hi @nzw0301 , it works. I really appreciate your sustaining support and help during my using of Optuna!

@nzw0301
Copy link
Collaborator

nzw0301 commented Mar 25, 2021

It's my pleasure! I'm glad to hear that I could support your optuna life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about Optuna.
Projects
None yet
Development

No branches or pull requests

2 participants