Ejemplo n.º 1
0
def test_get_all_study_summaries_with_no_trials(storage_mode: str) -> None:

    with StorageSupplier(storage_mode) as storage:
        study = create_study(storage=storage)

        summaries = get_all_study_summaries(study._storage)
        summary = [s for s in summaries if s._study_id == study._study_id][0]

        assert summary.study_name == study.study_name
        assert summary.n_trials == 0
        assert summary.datetime_start is None
Ejemplo n.º 2
0
def test_get_all_study_summaries(storage_mode: str) -> None:

    with StorageSupplier(storage_mode) as storage:
        study = create_study(storage=storage)
        study.optimize(Func(), n_trials=5)

        summaries = get_all_study_summaries(study._storage)
        summary = [s for s in summaries if s._study_id == study._study_id][0]

        assert summary.study_name == study.study_name
        assert summary.n_trials == 5
Ejemplo n.º 3
0
def test_get_all_study_summaries_with_no_trials(storage_mode, cache_mode):
    # type: (str, bool) -> None

    with StorageSupplier(storage_mode, cache_mode) as storage:
        study = optuna.create_study(storage=storage)

        summaries = optuna.get_all_study_summaries(study._storage)
        summary = [s for s in summaries if s.study_id == study.study_id][0]

        assert summary.study_name == study.study_name
        assert summary.n_trials == 0
        assert summary.datetime_start is None
Ejemplo n.º 4
0
def test_get_all_study_summaries(storage_mode, cache_mode):
    # type: (str, bool) -> None

    with StorageSupplier(storage_mode, cache_mode) as storage:
        study = optuna.create_study(storage=storage)
        study.optimize(Func(), n_trials=5)

        summaries = optuna.get_all_study_summaries(study._storage)
        summary = [s for s in summaries if s.study_id == study.study_id][0]

        assert summary.study_name == study.study_name
        assert summary.n_trials == 5
Ejemplo n.º 5
0
    def take_action(self, parsed_args):
        # type: (Namespace) -> Tuple[Tuple, Tuple[Tuple, ...]]

        storage_url = _check_storage_url(self.app_args.storage)
        summaries = optuna.get_all_study_summaries(storage=storage_url)

        rows = []
        for s in summaries:
            start = (s.datetime_start.strftime(self._datetime_format)
                     if s.datetime_start is not None else None)
            row = (s.study_name, s.direction.name, s.n_trials, start)
            rows.append(row)

        return self._study_list_header, tuple(rows)
Ejemplo n.º 6
0
    def take_action(self,
                    parsed_args: Namespace) -> Tuple[Tuple, Tuple[Tuple, ...]]:

        storage_url = _check_storage_url(self.app_args.storage)
        summaries = optuna.get_all_study_summaries(storage=storage_url)

        rows = []
        for s in summaries:
            start = (s.datetime_start.strftime(_DATETIME_FORMAT)
                     if s.datetime_start is not None else None)
            row = (s.study_name, tuple(d.name for d in s.directions),
                   s.n_trials, start)
            rows.append(row)

        return self._study_list_header, tuple(rows)
Ejemplo n.º 7
0
def test_get_all_study_summaries(storage_mode: str,
                                 include_best_trial: bool) -> None:

    with StorageSupplier(storage_mode) as storage:
        study = create_study(storage=storage)
        study.optimize(func, n_trials=5)

        summaries = get_all_study_summaries(study._storage, include_best_trial)
        summary = [s for s in summaries if s._study_id == study._study_id][0]

        assert summary.study_name == study.study_name
        assert summary.n_trials == 5
        if include_best_trial:
            assert summary.best_trial is not None
        else:
            assert summary.best_trial is None
Ejemplo n.º 8
0
Archivo: cli.py Proyecto: not522/optuna
    def take_action(self, parsed_args: Namespace) -> None:

        storage_url = _check_storage_url(self.app_args.storage)
        summaries = optuna.get_all_study_summaries(storage=storage_url)

        records = []
        for s in summaries:
            start = (s.datetime_start.strftime(_DATETIME_FORMAT)
                     if s.datetime_start is not None else None)
            record: Dict[Tuple[str, str], Any] = {}
            record[("name", "")] = s.study_name
            record[("direction", "")] = tuple(d.name for d in s.directions)
            record[("n_trials", "")] = s.n_trials
            record[("datetime_start", "")] = start
            records.append(record)

        print(
            _format_output(records, self._study_list_header,
                           parsed_args.format, parsed_args.flatten))
Ejemplo n.º 9
0
import optuna # pip install optuna

optuna.create_study()
optuna.load_study()
optuna.delete_study()
optuna.copy_study()
optuna.get_all_study_summaries()
optuna.TrialPruned

trial = Trial(study, trial_id)
trial.datetime_start
trial.distributions
trial.number
trial.params
trial.system_attrs
trial.user_attrs

trial.report(value, step)
trial.set_system_attr(key, value)
trial.set_user_attr(key, value)
trial.should_prune()
trial.suggest_categorical(name, choices)
trial.suggest_discrete_uniform(name, low, high, q)
trial.suggest_float(name, low, high, *, ?step, ?log)
trial.suggest_int(name, low, high, ?step, ?log)
trial.suggest_loguniform(name, low, high)
trial.suggest_uniform(name, low, high)

#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import tensorflow as tf
from tf import keras
Ejemplo n.º 10
0
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')

###################################################################################################
# We can access annotated attributes with :attr:`~optuna.study.Study.user_attr` property.

study.user_attrs  # {'contributors': ['Akiba', 'Sano'], 'dataset': 'MNIST'}

###################################################################################################
# :class:`~optuna.struct.StudySummary` object, which can be retrieved by
# :func:`~optuna.study.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'}

###################################################################################################
# .. seealso::
#     ``optuna study set-user-attr`` command, which sets an attribute via command line interface.

###################################################################################################
# Adding User Attributes to Trials
# --------------------------------
#
# As with :class:`~optuna.study.Study`, a :class:`~optuna.trial.Trial` object provides
# :func:`~optuna.trial.Trial.set_user_attr` method.
# Attributes are set inside an objective function.
Ejemplo n.º 11
0
import argparse
import json
import re
import sys

import optuna

parser = argparse.ArgumentParser()
parser.add_argument("storage", type=str)
parser.add_argument("target_study_name", type=str)
args = parser.parse_args()

study_names = [
    study.study_name for study in optuna.get_all_study_summaries(args.storage)
    if re.match(args.target_study_name, study.study_name)
]

print("Target studies: {}".format(study_names), file=sys.stderr)

trials = []
for study_name in study_names:
    study = optuna.load_study(study_name=study_name, storage=args.storage)
    for trial in study.trials:
        values = [
            v if v is None or d == optuna.study.StudyDirection.MINIMIZE else -v
            for v, d in zip(trial.values, study.directions)
        ]
        distributions = {
            name: json.loads(optuna.distributions.distribution_to_json(d))
            for name, d in trial.distributions.items()
        }
Ejemplo n.º 12
0
    # Create a study for single-objective optimization.
    study = optuna.create_study(storage=args.storage_url, study_name="single")
    study.set_system_attr("c", 2)
    study.set_user_attr("d", 3)
    study.optimize(objective_test_upgrade, n_trials=1)

    # Create a study for multi-objective optimization.
    try:
        optuna.create_study(storage=args.storage_url,
                            study_name="multi_empty",
                            directions=["minimize", "minimize"])
        study = optuna.create_study(storage=args.storage_url,
                                    study_name="multi",
                                    directions=["minimize", "minimize"])
        study.set_system_attr("c", 2)
        study.set_user_attr("d", 3)
        study.optimize(mo_objective_test_upgrade, n_trials=1)
    except TypeError:
        print(
            f"optuna=={optuna.__version__} does not support multi-objective optimization."
        )

    # Create a study for distributions upgrade.
    if version.parse(optuna.__version__) >= version.parse("2.4.0"):
        study = optuna.create_study(storage=args.storage_url,
                                    study_name="schema migration")
        study.optimize(objective_test_upgrade_distributions, n_trials=1)

    for s in optuna.get_all_study_summaries(args.storage_url):
        print(f"{s.study_name}, {s.n_trials}")
Ejemplo n.º 13
0
 def _get_study_summary(self) -> List[optuna.study.StudySummary]:
     if self.summary_cache is not None:
         return self.summary_cache
     summary = optuna.get_all_study_summaries(storage=self.database_url)
     self.summary_cache = summary
     return summary
Ejemplo n.º 14
0
def optuna_run(
    db_name: str,
    n_gpus: int,
    n_workers_per_gpu: int,
    n_trials_per_worker: int,
    n_startup_trials: int,
    objective_func: Callable,
):

    db_file = Path(db_name) / f'{db_name}.db'
    db_file.parent.mkdir(parents=True, exist_ok=True)
    db_url = f'sqlite:///{db_file.as_posix()}'
    if db_file.exists():
        print(f'Studies in storage: {db_url}')
        for study in optuna.get_all_study_summaries(db_url):
            print(f'  Study {study.study_name} with {study.n_trials} trials')

    storage = optuna.storages.RDBStorage(url=db_url, )

    study = optuna.create_study(
        study_name='study',
        storage=storage,
        load_if_exists=True,
        direction='maximize',
    )

    # FAIL any zombie trials that are stuck in `RUNNING` state
    trials = storage.get_all_trials(study._study_id,
                                    deepcopy=False,
                                    states=(optuna.trial.TrialState.RUNNING, ))
    for trial in trials:
        print(f'Setting trial {trial.number} with state {trial.state} to FAIL')
        status = storage.set_trial_state(trial._trial_id,
                                         optuna.trial.TrialState.FAIL)
        print(f'Success?: {status}')

    # launch workers
    n_workers = n_gpus * n_workers_per_gpu
    with concurrent.futures.ProcessPoolExecutor(
            max_workers=n_workers) as executor:
        # with concurrent.futures.ThreadPoolExecutor(max_workers=n_workers) as executor:
        futures = []
        for i_worker in range(n_workers):
            i_gpu = i_worker % n_gpus
            print(
                f'Launching worker {i_worker+1} (of {n_workers}) on gpu {i_gpu} and running {n_trials_per_worker} trials'
            )
            future = executor.submit(
                worker,  # callable that calls study.optimize()
                db_url,
                db_file.parent.as_posix(),
                n_trials_per_worker,
                i_gpu,
                n_startup_trials,
                objective_func,
            )
            futures.append(future)
            time.sleep(0.1)
        concurrent.futures.wait(futures)
        for i_future, future in enumerate(futures):
            if future.exception() is None:
                print(f'Future {i_future} returned {future.result()}')
            else:
                print(f'Future {i_future} exception:')
                print(future.exception())