def prepare_searcher(data,
                     search_space,
                     stop,
                     validation_data=None,
                     model_creator=linear_model_creator,
                     optimizer_creator=optimizer_creator,
                     loss_creator=loss_creator,
                     feature_transformer=None,
                     metric="mse",
                     name="demo"):
    modelBuilder = PytorchModelBuilder(model_creator=model_creator,
                                       optimizer_creator=optimizer_creator,
                                       loss_creator=loss_creator)
    searcher = SearchEngineFactory.create_engine(
        backend="ray",
        logs_dir="~/zoo_automl_logs",
        resources_per_trial={"cpu": 2},
        name=name)
    searcher.compile(data=data,
                     validation_data=validation_data,
                     model_create_func=modelBuilder,
                     search_space=search_space,
                     n_sampling=2,
                     epochs=stop["training_iteration"],
                     metric_threshold=stop["reward_metric"],
                     feature_transformers=feature_transformer,
                     metric=metric)
    return searcher
Beispiel #2
0
    def from_keras(
        *,
        model_creator,
        logs_dir,
        resources_per_trial,
        name,
    ):
        """
        Create an AutoEstimator for tensorflow keras.

        :param model_creator: Tensorflow keras model creator function.
        :param logs_dir: Local directory to save logs and results.
        :param resources_per_trial: Dict. resources for each trial. e.g. {"cpu": 2}.
        :param name: Name of the auto estimator.
        :return: an AutoEstimator object.
        """
        from zoo.automl.model import ModelBuilder
        from zoo.automl.search import SearchEngineFactory
        model_builder = ModelBuilder.from_tfkeras(model_creator=model_creator)
        searcher = SearchEngineFactory.create_engine(
            backend="ray",
            logs_dir=logs_dir,
            resources_per_trial=resources_per_trial,
            name=name)
        return AutoEstimator(model_builder=model_builder, searcher=searcher)
Beispiel #3
0
 def __init__(self,
              model_builder,
              logs_dir="/tmp/auto_estimator_logs",
              resources_per_trial=None,
              name=None):
     self.model_builder = model_builder
     self.searcher = SearchEngineFactory.create_engine(
         backend="ray",
         logs_dir=logs_dir,
         resources_per_trial=resources_per_trial,
         name=name)
     self._fitted = False
Beispiel #4
0
    def from_torch(
        *,
        model_creator,
        optimizer,
        loss,
        logs_dir,
        resources_per_trial,
        name,
    ):
        """
        Create an AutoEstimator for torch.

        :param model_creator: PyTorch model creator function.
        :param optimizer: PyTorch optimizer creator function or pytorch optimizer name (string).
            Note that you should specify learning rate search space with key as "lr" or LR_NAME
            (from zoo.orca.automl.pytorch_utils import LR_NAME) if input optimizer name.
            Without learning rate search space specified, the default learning rate value of 1e-3
            will be used for all estimators.
        :param loss: PyTorch loss instance or PyTorch loss creator function
            or pytorch loss name (string).
        :param logs_dir: Local directory to save logs and results.
        :param resources_per_trial: Dict. resources for each trial. e.g. {"cpu": 2}.
        :param name: Name of the auto estimator.
        :return: an AutoEstimator object.
        """
        from zoo.orca.automl.pytorch_utils import validate_pytorch_loss, \
            validate_pytorch_optim
        from zoo.automl.model import ModelBuilder
        from zoo.automl.search import SearchEngineFactory
        loss = validate_pytorch_loss(loss)
        optimizer = validate_pytorch_optim(optimizer)
        model_builder = ModelBuilder.from_pytorch(model_creator=model_creator,
                                                  optimizer_creator=optimizer,
                                                  loss_creator=loss)
        searcher = SearchEngineFactory.create_engine(
            backend="ray",
            logs_dir=logs_dir,
            resources_per_trial=resources_per_trial,
            name=name)
        return AutoEstimator(model_builder=model_builder, searcher=searcher)
def prepare_searcher(data,
                     model_creator=linear_model_creator,
                     optimizer_creator=optimizer_creator,
                     loss_creator=loss_creator,
                     feature_transformer=None,
                     recipe=SimpleRecipe(),
                     name="demo"):
    modelBuilder = ModelBuilder.from_pytorch(model_creator=model_creator,
                                             optimizer_creator=optimizer_creator,
                                             loss_creator=loss_creator)
    searcher = SearchEngineFactory.create_engine(backend="ray",
                                                 logs_dir="~/zoo_automl_logs",
                                                 resources_per_trial={"cpu": 2},
                                                 name=name)
    search_space = recipe.search_space(feature_transformer.get_feature_list())\
        if feature_transformer else None
    searcher.compile(data=data,
                     model_create_func=modelBuilder,
                     recipe=recipe,
                     feature_transformers=feature_transformer,
                     search_space=search_space)
    return searcher
def prepare_searcher(data,
                     model_creator=linear_model_creator,
                     optimizer_creator=optimizer_creator,
                     loss_creator=loss_creator,
                     feature_transformer=None,
                     recipe=SimpleRecipe(),
                     metric="mse",
                     name="demo"):
    modelBuilder = PytorchModelBuilder(model_creator=model_creator,
                                       optimizer_creator=optimizer_creator,
                                       loss_creator=loss_creator)
    searcher = SearchEngineFactory.create_engine(
        backend="ray",
        logs_dir="~/zoo_automl_logs",
        resources_per_trial={"cpu": 2},
        name=name)
    searcher.compile(data=data,
                     model_create_func=modelBuilder,
                     recipe=recipe,
                     feature_transformers=feature_transformer,
                     metric=metric)
    return searcher
    train_x, train_y = get_linear_data(2, 5, 1000)
    val_x, val_y = get_linear_data(2, 5, 400)
    data = (train_x, train_y)
    validation_data = (val_x, val_y)
    return data, validation_data


if __name__ == "__main__":
    # 1. the way to enable auto tuning model from creators.
    init_orca_context(init_ray_on_spark=True)
    modelBuilder = PytorchModelBuilder(model_creator=model_creator,
                                       optimizer_creator=optimizer_creator,
                                       loss_creator=loss_creator)

    searcher = SearchEngineFactory.create_engine(backend="ray",
                                                 logs_dir="~/zoo_automl_logs",
                                                 resources_per_trial={"cpu": 2},
                                                 name="demo")

    # pass input data, modelbuilder and recipe into searcher.compile. Note that if user doesn't pass
    # feature transformer, the default identity feature transformer will be used.
    data, validation_data = get_data()
    searcher.compile(data=data,
                     validation_data=validation_data,
                     model_builder=modelBuilder,
                     search_space=create_simple_recipe(),
                     n_sampling=2,
                     epochs=20)

    searcher.run()
    best_trials = searcher.get_best_trials(k=1)
    print(best_trials[0].config)