optuna.trial.FrozenTrial

class optuna.trial.FrozenTrial(number, state, value, datetime_start, datetime_complete, params, distributions, user_attrs, system_attrs, intermediate_values, trial_id, *, values=None)[源代码]

Status and results of a Trial.

This object has the same methods as Trial, and it suggests best parameter values among performed trials. In contrast to Trial, FrozenTrial does not depend on Study, and it is useful for deploying optimization results.

示例

Re-evaluate an objective function with parameter values optimized study.

import optuna


def objective(trial):
    x = trial.suggest_uniform("x", -1, 1)
    return x ** 2


study = optuna.create_study()
study.optimize(objective, n_trials=3)

assert objective(study.best_trial) == study.best_value

备注

Attributes are set in optuna.Study.optimize(), but several attributes can be updated after the optimization. That means such attributes are overwritten by the re-evaluation if your objective updates attributes of Trial.

Example:

Overwritten attributes.

import copy
import datetime

import optuna


def objective(trial):
    x = trial.suggest_uniform("x", -1, 1)

    # this user attribute always differs
    trial.set_user_attr("evaluation time", datetime.datetime.now())

    return x ** 2


study = optuna.create_study()
study.optimize(objective, n_trials=3)

best_trial = study.best_trial
best_trial_copy = copy.deepcopy(best_trial)

# re-evaluate
objective(best_trial)

# the user attribute is overwritten by re-evaluation
assert best_trial.user_attrs != best_trial_copy.user_attrs

备注

Please refer to Trial for details of methods and properties.

number

Unique and consecutive number of Trial for each Study. Note that this field uses zero-based numbering.

state

TrialState of the Trial.

value

Objective value of the Trial.

values

Sequence of objective values of the Trial. The length is greater than 1 if the problem is multi-objective optimization.

datetime_start

Datetime where the Trial started.

datetime_complete

Datetime where the Trial finished.

params

Dictionary that contains suggested parameters.

user_attrs

Dictionary that contains the attributes of the Trial set with optuna.trial.Trial.set_user_attr().

intermediate_values

Intermediate objective values set with optuna.trial.Trial.report().

引发

ValueError – If both value and values are specified.

参数
返回类型

None

Methods

report(value, step)

Interface of report function.

set_system_attr(key, value)

set_user_attr(key, value)

should_prune()

Suggest whether the trial should be pruned or not.

suggest_categorical(name, choices)

suggest_discrete_uniform(name, low, high, q)

suggest_float(name, low, high, *[, step, log])

suggest_int(name, low, high[, step, log])

suggest_loguniform(name, low, high)

suggest_uniform(name, low, high)

Attributes

datetime_start

distributions

Dictionary that contains the distributions of params.

duration

Return the elapsed time taken to complete the trial.

last_step

Return the maximum step of intermediate_values in the trial.

number

params

system_attrs

user_attrs

value

values

property distributions: Dict[str, optuna.distributions.BaseDistribution]

Dictionary that contains the distributions of params.

property duration: Optional[datetime.timedelta]

Return the elapsed time taken to complete the trial.

返回

The duration.

property last_step: Optional[int]

Return the maximum step of intermediate_values in the trial.

返回

The maximum step of intermediates.

report(value, step)[源代码]

Interface of report function.

Since FrozenTrial is not pruned, this report function does nothing.

参见

Please refer to should_prune().

参数
  • value (float) – A value returned from the objective function.

  • step (int) – Step of the trial (e.g., Epoch of neural network training). Note that pruners assume that step starts at zero. For example, MedianPruner simply checks if step is less than n_warmup_steps as the warmup mechanism.

返回类型

None

should_prune()[源代码]

Suggest whether the trial should be pruned or not.

The suggestion is always False regardless of a pruning algorithm.

备注

FrozenTrial only samples one combination of parameters.

返回

False.

返回类型

bool