optuna_mlflow

See also

Full documentation with examples can be found here: documentation page

Wrapper to log to Optuna and MLflow at the same time.

class hpoflow.optuna_mlflow.OptunaMLflow(tracking_uri=None, num_name_digits=3, enforce_clean_git=False, optuna_result_name='optuna_result')[source]

Bases: object

Wrapper to log to Optuna and MLflow at the same time.

Constructor.

Parameters:
  • tracking_uri (Optional[str]) – The MLflow tracking URL. Defaults to None which logs to the default locale folder ./mlruns or uses the MLFLOW_TRACKING_URI environment variable if it is available. Also see mlflow.set_tracking_uri().

  • num_name_digits (int) – Number of digits for the MLflow run_name.

  • enforce_clean_git (bool) – Check and enforce that the GIT repository has no uncommitted changes (see git.repo.base.Repo.is_dirty()).

  • optuna_result_name (str) – Name of the metric which is logged to MLflo and is returned by the objective function.

__call__(func)[source]

Returns the decorator for the Optuna objective function.

Parameters:

func (Callable[[Union[Trial, OptunaMLflow]], float]) – The optuna objective function for the decorator.

Return type:

Callable[[Trial], float]

static _end_run(status, exc_text=None)[source]

End the active MLflow run (see mlflow.end_run()).

Parameters:
Return type:

None

_get_hostname()[source]

Get the hostname.

Return type:

str

_log_iter(run_name, metrics, step)[source]

Log an iteration or a fold as a nested run (see mlflow.log_metrics()).

The data is logged only to MLflow and not to Optuna.

Parameters:
log_iter(metrics, step=None)[source]

Log an iteration or a fold as a nested run (see mlflow.log_metrics()).

The data is logged to MLflow and also added to Optuna as a user attribute (see optuna.trial.Trial.set_user_attr()).

Parameters:
Return type:

None

log_metric(key, value, step=None, optuna_log=True)[source]

Log a metric under the current run.

Wrapper of the corresponding MLflow function (see mlflow.log_metric()). The data is logged to MLflow and also added to Optuna as a user attribute (see optuna.trial.Trial.set_user_attr()).

Parameters:
  • key (str) – x

  • value (float) – x

  • step (Optional[int]) – x

  • optuna_log (Optional[bool]) – If False this is not logged to Optuna. This is an internal parameter that should be ignored by the API user.

Return type:

None

log_metrics(metrics, step=None, optuna_log=True)[source]

Log multiple metrics for the current run.

Wrapper of the corresponding MLflow function (see mlflow.log_metrics()). The data is logged to MLflow and also added to Optuna as a user attribute (see optuna.trial.Trial.set_user_attr()).

Parameters:
  • metrics (Dict[str, float]) – x

  • step (Optional[int]) – x

  • optuna_log (Optional[bool]) – If False this is not logged to Optuna. This is an internal parameter that should be ignored by the API user.

Return type:

None

log_param(key, value, optuna_log=True)[source]

Log a parameter under the current run.

Wrapper of the corresponding MLflow function (see mlflow.log_param()). The data is logged to MLflow and also added to Optuna as a user attribute (see optuna.trial.Trial.set_user_attr()).

Parameters:
  • key (str) – x

  • value (Any) – x

  • optuna_log (Optional[bool]) – If False this is not logged to Optuna. This is an internal parameter that should be ignored by the API user.

Return type:

None

log_params(params)[source]

Log a batch of params for the current run.

Wrapper of the corresponding MLflow function (see mlflow.log_params()). The data is logged to MLflow and also added to Optuna as a user attribute (see optuna.trial.Trial.set_user_attr()).

Parameters:

params (Dict[str, Any]) –

Return type:

None

report(value, step)[source]

Report an objective function value for a given step.

Wrapper of the corresponding Optuna function (see optuna.trial.Trial.report()).

Parameters:
  • value (float) – A value returned from the evaluation.

  • step (int) – Step of the trial (e.g., Epoch of neural network training). Note that pruners assume that step starts at zero. For example,

Return type:

None

set_tag(key, value, optuna_log=True)[source]

Set a tag under the current run.

Wrapper of the corresponding MLflow function (see mlflow.set_tag()). The data is logged to MLflow and also added to Optuna as a user attribute (see optuna.trial.Trial.set_user_attr()).

Parameters:
  • key (str) – x

  • value (Any) – x

  • optuna_log (Optional[bool]) – If False this is not logged to Optuna. This is an internal parameter that should be ignored by the API user.

Return type:

None

set_tags(tags, optuna_log=True)[source]

Log a batch of tags for the current run.

Wrapper of the corresponding MLflow function (see mlflow.set_tags()). The data is logged to MLflow and also added to Optuna as a user attribute (see optuna.trial.Trial.set_user_attr()).

Parameters:
  • tags (Dict[str, Any]) – x

  • optuna_log (Optional[bool]) – If False this is not logged to Optuna. This is an internal parameter that should be ignored by the API user.

Return type:

None

should_prune()[source]

Suggest whether the trial should be pruned or not.

Wrapper of the corresponding Optuna function (see optuna.trial.Trial.should_prune()).

Return type:

bool

suggest_categorical(name, choices)[source]

Suggest a value for the categorical parameter.

Wrapper of the corresponding Optuna function (see optuna.trial.Trial.suggest_categorical()).

Parameters:
Return type:

Union[None, bool, int, float, str]

suggest_discrete_uniform(name, low, high, q)[source]

Suggest a value for the discrete parameter.

Wrapper of the corresponding Optuna function (see optuna.trial.Trial.suggest_discrete_uniform()).

Parameters:
  • name (str) – A parameter name.

  • low (float) – Lower endpoint of the range of suggested values. low is included in the range.

  • high (float) – Upper endpoint of the range of suggested values. high is included in the range.

  • q (float) – A step of discretization.

Return type:

float

suggest_int(name, low, high, step=1, log=False)[source]

Suggest a value for the integer parameter.

Wrapper of the corresponding Optuna function (see optuna.trial.Trial.suggest_int()).

Parameters:
  • name (str) – A parameter name.

  • low (int) – Lower endpoint of the range of suggested values. low is included in the range.

  • high (int) – Upper endpoint of the range of suggested values. high is included in the range.

  • step (int) – A step of discretization.

  • log (bool) – A flag to sample the value from the log domain or not.

Return type:

int

suggest_loguniform(name, low, high)[source]

Suggest a value in the log domain for the continuous parameter.

Wrapper of the corresponding Optuna function (see optuna.trial.Trial.suggest_loguniform()).

Parameters:
  • name (str) – A parameter name.

  • low (float) – Lower endpoint of the range of suggested values. low is included in the range.

  • high (float) – Upper endpoint of the range of suggested values. high is excluded from the range.

Return type:

float

suggest_uniform(name, low, high)[source]

Suggest a value for the continuous parameter.

Wrapper of the corresponding Optuna function (see optuna.trial.Trial.suggest_uniform()).

Parameters:
  • name (str) – A parameter name.

  • low (float) – Lower endpoint of the range of suggested values. low is included in the range.

  • high (float) – Upper endpoint of the range of suggested values. high is excluded from the range.

Return type:

float