Study¶
-
class
optuna.study.
Study
(study_name, storage, sampler=None, pruner=None)[source]¶ A study corresponds to an optimization task, i.e., a set of trials.
This object provides interfaces to run a new
Trial
, access trials’ history, set/get user-defined attributes of the study itself.Note that the direct use of this constructor is not recommended. To create and load a study, please refer to the documentation of
create_study()
andload_study()
respectively.-
best_params
¶ Return parameters of the best trial in the study.
Returns: A dictionary containing parameters of the best trial.
-
best_trial
¶ Return the best trial in the study.
Returns: A FrozenTrial
object of the best trial.
-
best_value
¶ Return the best objective value in the study.
Returns: A float representing the best objective value.
-
direction
¶ Return the direction of the study.
Returns: A StudyDirection
object.
-
get_trials
(deepcopy=True)¶ Return all trials in the study.
The returned trials are ordered by trial number.
For library users, it’s recommended to use more handy
trials
property to get the trials instead.Parameters: deepcopy – Flag to control whether to apply copy.deepcopy()
to the trials. Note that if you set the flag toFalse
, you shouldn’t mutate any fields of the returned trial. Otherwise the internal state of the study may corrupt and unexpected behavior may happen.Returns: A list of FrozenTrial
objects.
-
optimize
(func, n_trials=None, timeout=None, n_jobs=1, catch=(), callbacks=None, gc_after_trial=True)[source]¶ Optimize an objective function.
Parameters: - func – A callable that implements objective function.
- n_trials – The number of trials. If this argument is set to
None
, there is no limitation on the number of trials. Iftimeout
is also set toNone
, the study continues to create trials until it receives a termination signal such as Ctrl+C or SIGTERM. - timeout – Stop study after the given number of second(s). If this argument is set to
None
, the study is executed without time limitation. Ifn_trials
is also set toNone
, the study continues to create trials until it receives a termination signal such as Ctrl+C or SIGTERM. - n_jobs – The number of parallel jobs. If this argument is set to
-1
, the number is set to CPU count. - catch – A study continues to run even when a trial raises one of the exceptions specified
in this argument. Default is an empty tuple, i.e. the study will stop for any
exception except for
TrialPruned
. - callbacks – List of callback functions that are invoked at the end of each trial.
- gc_after_trial – Flag to execute garbage collection at the end of each trial. By default, garbage collection is enabled, just in case. You can turn it off with this argument if memory is safely managed in your objective function.
-
set_user_attr
(key, value)[source]¶ Set a user attribute to the study.
Parameters: - key – A key string of the attribute.
- value – A value of the attribute. The value should be JSON serializable.
-
study_id
¶ Return the study ID.
Deprecated since version 0.20.0: The direct use of this attribute is deprecated and it is recommended that you use
study_name
instead.Returns: The study ID.
-
trials
¶ Return all trials in the study.
The returned trials are ordered by trial number.
This is a short form of
self.get_trials(deepcopy=True)
.Returns: A list of FrozenTrial
objects.
-
trials_dataframe
(attrs=('number', 'value', 'datetime_start', 'datetime_complete', 'params', 'user_attrs', 'system_attrs', 'state'), multi_index=False)[source]¶ Export trials as a pandas DataFrame.
The DataFrame provides various features to analyze studies. It is also useful to draw a histogram of objective values and to export trials as a CSV file. If there are no trials, an empty DataFrame is returned.
Example
import optuna import pandas def objective(trial): x = trial.suggest_uniform('x', -1, 1) return x ** 2 study = optuna.create_study() study.optimize(objective, n_trials=3) # Create a dataframe from the study. df = study.trials_dataframe() assert isinstance(df, pandas.DataFrame) assert df.shape[0] == 3 # n_trials.
Parameters: - attrs – Specifies field names of
FrozenTrial
to include them to a DataFrame of trials. - multi_index – Specifies whether the returned DataFrame employs MultiIndex or not. Columns that
are hierarchical by nature such as
(params, x)
will be flattened toparams_x
when set toFalse
.
Returns: - attrs – Specifies field names of
-
user_attrs
¶ Return user attributes.
Returns: A dictionary containing all user attributes.
-
-
optuna.study.
create_study
(storage=None, sampler=None, pruner=None, study_name=None, direction='minimize', load_if_exists=False)[source]¶ Create a new
Study
.Parameters: - storage –
Database URL. If this argument is set to None, in-memory storage is used, and the
Study
will not be persistent.Note
When a database URL is passed, Optuna internally uses SQLAlchemy to handle the database. Please refer to SQLAlchemy’s document for further details. If you want to specify non-default options to SQLAlchemy Engine, you can instantiateRDBStorage
with your desired options and pass it to thestorage
argument instead of a URL. - sampler – A sampler object that implements background algorithm for value suggestion.
If
None
is specified,TPESampler
is used as the default. See alsosamplers
. - pruner – A pruner object that decides early stopping of unpromising trials. See also
pruners
. - study_name – Study’s name. If this argument is set to None, a unique name is generated automatically.
- direction – Direction of optimization. Set
minimize
for minimization andmaximize
for maximization. - load_if_exists – Flag to control the behavior to handle a conflict of study names.
In the case where a study named
study_name
already exists in thestorage
, aDuplicatedStudyError
is raised ifload_if_exists
is set toFalse
. Otherwise, the creation of the study is skipped, and the existing one is returned.
Returns: A
Study
object.- storage –
-
optuna.study.
load_study
(study_name, storage, sampler=None, pruner=None)[source]¶ Load the existing
Study
that has the specified name.Parameters: - study_name – Study’s name. Each study has a unique name as an identifier.
- storage – Database URL such as
sqlite:///example.db
. Please see also the documentation ofcreate_study()
for further details. - sampler – A sampler object that implements background algorithm for value suggestion.
If
None
is specified,TPESampler
is used as the default. See alsosamplers
. - pruner – A pruner object that decides early stopping of unpromising trials.
If
None
is specified,MedianPruner
is used as the default. See alsopruners
.
-
optuna.study.
delete_study
(study_name, storage)[source]¶ Delete a
Study
object.Parameters: - study_name – Study’s name.
- storage – Database URL such as
sqlite:///example.db
. Please see also the documentation ofcreate_study()
for further details.
-
optuna.study.
get_all_study_summaries
(storage)[source]¶ Get all history of studies stored in a specified storage.
Parameters: storage – Database URL such as sqlite:///example.db
. Please see also the documentation ofcreate_study()
for further details.Returns: List of study history summarized as StudySummary
objects.