Note
Click here to download the full example code
User Attributes¶
This feature is to annotate experiments with user-defined attributes.
Adding User Attributes to Studies¶
A Study object provides set_user_attr() method
to register a pair of key and value as an user-defined attribute.
A key is supposed to be a str, and a value be any object serializable with json.dumps.
import sklearn.datasets
import sklearn.svm
import sklearn.model_selection
import optuna
study = optuna.create_study(storage='sqlite:///example.db')
study.set_user_attr('contributors', ['Akiba', 'Sano'])
study.set_user_attr('dataset', 'MNIST')
Out:
/Users/harutakakawamura/.pyenv/versions/miniconda3-4.7.12/envs/optuna-dev-env/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
return f(*args, **kwds)
We can access annotated attributes with user_attr property.
study.user_attrs # {'contributors': ['Akiba', 'Sano'], 'dataset': 'MNIST'}
Out:
{'contributors': ['Akiba', 'Sano'], 'dataset': 'MNIST'}
StudySummary object, which can be retrieved by
get_all_study_summaries(), also contains user-defined attributes.
study_summaries = optuna.get_all_study_summaries('sqlite:///example.db')
study_summaries[0].user_attrs # {'contributors': ['Akiba', 'Sano'], 'dataset': 'MNIST'}
Out:
{'contributors': ['Akiba', 'Sano'], 'dataset': 'MNIST'}
See also
optuna study set-user-attr command, which sets an attribute via command line interface.
Adding User Attributes to Trials¶
As with Study, a Trial object provides
set_user_attr() method.
Attributes are set inside an objective function.
def objective(trial):
iris = sklearn.datasets.load_iris()
x, y = iris.data, iris.target
svc_c = trial.suggest_loguniform('svc_c', 1e-10, 1e10)
clf = sklearn.svm.SVC(C=svc_c)
accuracy = sklearn.model_selection.cross_val_score(clf, x, y).mean()
trial.set_user_attr('accuracy', accuracy)
return 1.0 - accuracy # return error for minimization
study.optimize(objective, n_trials=1)
We can access annotated attributes as:
study.trials[0].user_attrs
Out:
{'accuracy': 0.9266666666666667}
Note that, in this example, the attribute is not annotated to a Study
but a single Trial.
Total running time of the script: ( 0 minutes 0.167 seconds)