optuna / optuna

A hyperparameter optimization framework
https://optuna.org
MIT License
10.53k stars 1k forks source link

optuna.visualization.plot_intermediate_values(study) returns an empty plot (with no data) #3759

Closed AraceliAL closed 2 years ago

AraceliAL commented 2 years ago

Don't use GitHub Issues to ask support questions.

Good afternoon! I just wanted to ask if anyone knows how could I have an intermediate_values plot with no data (see the attached figure)

I have no problems with the other plots such as the plot_optimization_history Captura de pantalla 2022-07-06 a les 13 38 48

Thanks a lot and sorry for the inconvenience!

nzw0301 commented 2 years ago

Hi, could you share minimal reproducible codes with us?

AraceliAL commented 2 years ago

Yes sure! sorry! I simply train an xgboost using optuna as found in : https://github.com/optuna/optuna-examples/blob/main/xgboost/xgboost_simple.py

import numpy as np
import optuna

import sklearn.datasets
import sklearn.metrics
from sklearn.model_selection import train_test_split
import xgboost as xgb

def objective(trial):
    (data, target) = sklearn.datasets.load_breast_cancer(return_X_y=True)
    train_x, valid_x, train_y, valid_y = train_test_split(data, target, test_size=0.25)
    dtrain = xgb.DMatrix(train_x, label=train_y)
    dvalid = xgb.DMatrix(valid_x, label=valid_y)

    param = {
        "verbosity": 0,
        "objective": "binary:logistic",
        # use exact for small dataset.
        "tree_method": "exact",
        # defines booster, gblinear for linear functions.
        "booster": trial.suggest_categorical("booster", ["gbtree", "gblinear", "dart"]),
        # L2 regularization weight.
        "lambda": trial.suggest_float("lambda", 1e-8, 1.0, log=True),
        # L1 regularization weight.
        "alpha": trial.suggest_float("alpha", 1e-8, 1.0, log=True),
        # sampling ratio for training data.
        "subsample": trial.suggest_float("subsample", 0.2, 1.0),
        # sampling according to each tree.
        "colsample_bytree": trial.suggest_float("colsample_bytree", 0.2, 1.0),
    }

    if param["booster"] in ["gbtree", "dart"]:
        # maximum depth of the tree, signifies complexity of the tree.
        param["max_depth"] = trial.suggest_int("max_depth", 3, 9, step=2)
        # minimum child weight, larger the term more conservative the tree.
        param["min_child_weight"] = trial.suggest_int("min_child_weight", 2, 10)
        param["eta"] = trial.suggest_float("eta", 1e-8, 1.0, log=True)
        # defines how selective algorithm is.
        param["gamma"] = trial.suggest_float("gamma", 1e-8, 1.0, log=True)
        param["grow_policy"] = trial.suggest_categorical("grow_policy", ["depthwise", "lossguide"])

    if param["booster"] == "dart":
        param["sample_type"] = trial.suggest_categorical("sample_type", ["uniform", "weighted"])
        param["normalize_type"] = trial.suggest_categorical("normalize_type", ["tree", "forest"])
        param["rate_drop"] = trial.suggest_float("rate_drop", 1e-8, 1.0, log=True)
        param["skip_drop"] = trial.suggest_float("skip_drop", 1e-8, 1.0, log=True)

    bst = xgb.train(param, dtrain)
    preds = bst.predict(dvalid)
    pred_labels = np.rint(preds)
    accuracy = sklearn.metrics.accuracy_score(valid_y, pred_labels)
    return accuracy

if __name__ == "__main__":
    study = optuna.create_study(direction="maximize")
    study.optimize(objective, n_trials=100, timeout=600)
    trial = study.best_trial
    # here I  ask for optuna plots
    fig = optuna.visualization.plot_intermediate_values(study)
    fig.write_html("name.html")

works for all plots except for plot_intermediate_values, which is the one that interests me the most

nzw0301 commented 2 years ago

Thanks. plot_intermediate_values shows a trial's intermediate values that are stored by calling trial.report method. So the current trials don't have intermediate values; the plot generates an empty plot.