Ejemplo n.º 1
0
    from types import TracebackType  # NOQA
    from typing import Any  # NOQA
    from typing import Callable  # NOQA
    from typing import Dict  # NOQA
    from typing import Optional  # NOQA
    from typing import Type  # NOQA

try:
    import chainermn
    from chainermn.communicators.communicator_base import CommunicatorBase  # NOQA
    _available = True
except ImportError:
    _available = False

STORAGE_MODES = ['new', 'common']
PRUNER_INIT_FUNCS = [lambda: pruners.MedianPruner(), lambda: pruners.SuccessiveHalvingPruner()]
CACHE_MODES = [True, False]


def setup_module():
    # type: () -> None

    StorageSupplier.setup_common_tempfile()


def teardown_module():
    # type: () -> None

    StorageSupplier.teardown_common_tempfile()

Ejemplo n.º 2
0
from optuna.testing.sampler import DeterministicRelativeSampler
from optuna.testing.storage import StorageSupplier
from optuna.trial import Trial
from optuna.trial import TrialState

try:
    import chainermn
    from chainermn.communicators.communicator_base import CommunicatorBase  # NOQA

    _available = True
except ImportError:
    _available = False

STORAGE_MODES = ["sqlite"]
PRUNER_INIT_FUNCS = [
    lambda: pruners.MedianPruner(), lambda: pruners.SuccessiveHalvingPruner()
]


class Func(object):
    def __init__(self) -> None:

        self.suggested_values: Dict[int, Dict[str, Any]] = {}

    def __call__(self, trial: ChainerMNTrial, comm: CommunicatorBase) -> float:

        x = trial.suggest_uniform("x", -10, 10)
        y = trial.suggest_loguniform("y", 20, 30)
        z = trial.suggest_categorical("z", (-1.0, 1.0))

        self.suggested_values[trial.number] = {}
            'n_embed':100,
            'n_categ':48,
            'n_area':8
        }

    def objective(trial:Trial):
        if args_cl.study != '':# 過去の best_param 使う場合
            model = CbLossClassifier(**model_params)
        else:
            model = get_model(trial,model_params)

        trainer,reporter = get_trainer_and_reporter(
            trial=trial,
            model=model,
            iter_train=iter_train,
            iter_test=iter_test,
            batch_converter=batch_converter_area,
            args=args_cl,
            device=0,
            best_params=best_params
        )
        trainer.run()
        (epoch,best_loss) = get_bestresult(reporter.log,args_cl.pruning_key)
        return best_loss
    
    study = create_study(study_name=args_cl.optuna_output,
                            storage=f'sqlite:///{args_cl.optuna_output}/optuna_{args_cl.desc}.db',
                            load_if_exists=True,
                            pruner=pruners.SuccessiveHalvingPruner())
    study.optimize(objective, n_trials=args_cl.n_trials)