def build_search_space(self):
        """Build and return a search space object using the infered data shapes after loading data.

        Returns:
            NxSearchSpace: A search space instance.
        """
        config = self._space
        input_shape, output_shape = setup_data(config)

        search_space = get_search_space(config, input_shape, output_shape, seed=self.seed)
        return search_space
    def build_search_space(self) -> deephyper.nas.space.AutoKSearchSpace:
        """Build and return a search space object using the infered data shapes after loading data.

        Returns:
            NxSearchSpace: A search space instance.
        """
        config = self._space
        input_shape, output_shape = setup_data(config, add_to_config=False)

        search_space = get_search_space(config, input_shape, output_shape, seed=self.seed)
        return search_space
def test_trainer_regressor_train_valid_with_multiple_generator_inputs():
    from deephyper.nas.run.util import (
        load_config,
        setup_data,
    )
    from deephyper.benchmark.nas.linearRegMultiInputsGen.problem import Problem

    config = Problem.space

    load_config(config)

    input_shape, output_shape = setup_data(config)

    create_search_space = config["create_search_space"]["func"]
    cs_kwargs = config["create_search_space"].get("kwargs")
    if cs_kwargs is None:
        search_space = create_search_space(input_shape, output_shape, seed=42)
    else:
        search_space = create_search_space(input_shape,
                                           output_shape,
                                           seed=42,
                                           **cs_kwargs)

    arch_seq = [random() for i in range(search_space.num_nodes)]
    config["arch_seq"] = arch_seq
    search_space.set_ops(arch_seq)

    config["hyperparameters"]["num_epochs"] = 2

    search_space.set_ops(arch_seq)
    search_space.draw_graphviz("trainer_keras_regressor_test.dot")

    model = search_space.create_model()
    plot_model(model,
               to_file="trainer_keras_regressor_test.png",
               show_shapes=True)

    trainer = TrainerTrainValid(config=config, model=model)

    res = trainer.train()
    assert res != sys.float_info.max
Beispiel #4
0
def run(config):

    physical_devices = tf.config.list_physical_devices("GPU")
    try:
        for i in range(len(physical_devices)):
            tf.config.experimental.set_memory_growth(physical_devices[i], True)
    except:
        # Invalid device or cannot modify virtual devices once initialized.
        pass

    distributed_strategy = tf.distribute.MirroredStrategy()
    n_replicas = distributed_strategy.num_replicas_in_sync

    seed = config["seed"]
    if seed is not None:
        np.random.seed(seed)
        tf.random.set_seed(seed)

    load_config(config)

    # Scale batch size and learning rate according to the number of ranks
    if config[a.hyperparameters].get("lsr_batch_size"):
        batch_size = config[a.hyperparameters][a.batch_size] * n_replicas
    else:
        batch_size = config[a.hyperparameters][a.batch_size]
    if config[a.hyperparameters].get("lsr_learning_rate"):
        learning_rate = config[a.hyperparameters][a.learning_rate] * n_replicas
    else:
        learning_rate = config[a.hyperparameters][a.learning_rate]
    logger.info(
        f"Scaled: 'batch_size' from {config[a.hyperparameters][a.batch_size]} to {batch_size} "
    )
    logger.info(
        f"Scaled: 'learning_rate' from {config[a.hyperparameters][a.learning_rate]} to {learning_rate} "
    )
    config[a.hyperparameters][a.batch_size] = batch_size
    config[a.hyperparameters][a.learning_rate] = learning_rate

    input_shape, output_shape = setup_data(config)

    search_space = setup_search_space(config,
                                      input_shape,
                                      output_shape,
                                      seed=seed)

    model_created = False
    with distributed_strategy.scope():
        try:
            model = search_space.create_model()
            model_created = True
        except:
            logger.info("Error: Model creation failed...")
            logger.info(traceback.format_exc())
        else:
            # Setup callbacks
            callbacks = []
            cb_requires_valid = False  # Callbacks requires validation data
            callbacks_config = config["hyperparameters"].get("callbacks")
            if callbacks_config is not None:
                for cb_name, cb_conf in callbacks_config.items():
                    if cb_name in default_callbacks_config:
                        default_callbacks_config[cb_name].update(cb_conf)

                        # Special dynamic parameters for callbacks
                        if cb_name == "ModelCheckpoint":
                            default_callbacks_config[cb_name][
                                "filepath"] = f'best_model_{config["id"]}.h5'

                        # replace patience hyperparameter
                        if "patience" in default_callbacks_config[cb_name]:
                            patience = config["hyperparameters"].get(
                                f"patience_{cb_name}")
                            if patience is not None:
                                default_callbacks_config[cb_name][
                                    "patience"] = patience

                        # Import and create corresponding callback
                        Callback = import_callback(cb_name)
                        callbacks.append(
                            Callback(**default_callbacks_config[cb_name]))

                        if cb_name in ["EarlyStopping"]:
                            cb_requires_valid = "val" in cb_conf[
                                "monitor"].split("_")
                    else:
                        logger.error(
                            f"'{cb_name}' is not an accepted callback!")
            trainer = TrainerTrainValid(config=config, model=model)
            trainer.callbacks.extend(callbacks)

            last_only, with_pred = preproc_trainer(config)
            last_only = last_only and not cb_requires_valid

    if model_created:
        history = trainer.train(with_pred=with_pred, last_only=last_only)

        # save history
        save_history(config.get("log_dir", None), history, config)

        result = compute_objective(config["objective"], history)
    else:
        # penalising actions if model cannot be created
        result = -1
    if result < -10 or np.isnan(result):
        result = -10

    return result
Beispiel #5
0
def train(config):
    seed = config["seed"]
    repeat = config["post_train"]["repeat"]
    if seed is not None:
        np.random.seed(seed)
        # must be between (0, 2**32-1)
        seeds = [np.random.randint(0, 2**32 - 1) for _ in range(repeat)]

    for rep in range(repeat):
        tf.keras.backend.clear_session()

        default_callbacks_config = copy.deepcopy(CB_CONFIG)
        if seed is not None:
            np.random.seed(seeds[rep])
            tf.random.set_seed(seeds[rep])

        logger.info(f"Training replica {rep+1}")
        # Pre-settings: particularly import for BeholderCB to work
        sess = tf.Session()
        K.set_session(sess)

        # override hyperparameters with post_train hyperparameters
        keys = filter(lambda k: k in config["hyperparameters"],
                      config["post_train"].keys())
        for k in keys:
            config["hyperparameters"][k] = config["post_train"][k]

        load_config(config)

        input_shape, output_shape = setup_data(config)

        search_space = setup_search_space(config,
                                          input_shape,
                                          output_shape,
                                          seed=seed)
        search_space.draw_graphviz(f'model_{config["id"]}.dot')
        logger.info("Model operations set.")

        model_created = False
        try:
            model = search_space.create_model()
            model_created = True
        except:
            model_created = False
            logger.info("Error: Model creation failed...")
            logger.info(traceback.format_exc())

        if model_created:
            # model.load_weights(default_cfg['model_checkpoint']['filepath'])

            # Setup callbacks
            callbacks = []
            callbacks_config = config["post_train"].get("callbacks")
            if callbacks_config is not None:
                for cb_name, cb_conf in callbacks_config.items():
                    if cb_name in default_callbacks_config:
                        default_callbacks_config[cb_name].update(cb_conf)

                        if cb_name == "ModelCheckpoint":
                            default_callbacks_config[cb_name][
                                "filepath"] = f'best_model_id{config["id"]}_r{rep}.h5'
                        elif cb_name == "TensorBoard":
                            if default_callbacks_config[cb_name]["beholder"]:
                                callbacks.append(
                                    BeholderCB(
                                        logdir=default_callbacks_config[
                                            cb_name]["log_dir"],
                                        sess=sess,
                                    ))
                            default_callbacks_config[cb_name].pop("beholder")

                        Callback = getattr(keras.callbacks, cb_name)
                        callbacks.append(
                            Callback(**default_callbacks_config[cb_name]))

                        logger.info(
                            f"Adding new callback {type(Callback).__name__} with config: {default_callbacks_config[cb_name]}!"
                        )

                    else:
                        logger.error(
                            f"'{cb_name}' is not an accepted callback!")

            trainer = TrainerTrainValid(config=config, model=model)
            trainer.callbacks.extend(callbacks)

            json_fname = f'post_training_hist_{config["id"]}.json'
            # to log the number of trainable parameters before running training
            trainer.init_history()
            try:
                with open(json_fname, "r") as f:
                    fhist = json.load(f)
            except FileNotFoundError:
                fhist = trainer.train_history
                for k, v in fhist.items():
                    fhist[k] = [v]
                with open(json_fname, "w") as f:
                    json.dump(fhist, f, cls=Encoder)

            hist = trainer.train(with_pred=False, last_only=False)

            # Timing of prediction for validation dataset
            t = time()  # ! TIMING - START
            trainer.predict(dataset="valid")
            hist["val_predict_time"] = time() - t  # ! TIMING - END

            for k, v in hist.items():
                fhist[k] = fhist.get(k, [])
                fhist[k].append(v)

            with open(json_fname, "w") as f:
                json.dump(fhist, f, cls=Encoder)

        return model
Beispiel #6
0
def run(config: dict) -> float:
    # Threading configuration
    if os.environ.get("OMP_NUM_THREADS", None) is not None:
        logger.debug(f"OMP_NUM_THREADS is {os.environ.get('OMP_NUM_THREADS')}")
        num_intra = int(os.environ.get("OMP_NUM_THREADS"))
        tf.config.threading.set_intra_op_parallelism_threads(num_intra)
        tf.config.threading.set_inter_op_parallelism_threads(2)

    seed = config["seed"]
    if seed is not None:
        np.random.seed(seed)
        tf.random.set_seed(seed)

    load_config(config)

    input_shape, output_shape = setup_data(config)

    search_space = setup_search_space(config,
                                      input_shape,
                                      output_shape,
                                      seed=seed)

    model_created = False
    try:
        model = search_space.create_model()
        model_created = True
    except:
        logger.info("Error: Model creation failed...")
        logger.info(traceback.format_exc())

    if model_created:

        # Setup callbacks
        callbacks = []
        cb_requires_valid = False  # Callbacks requires validation data
        callbacks_config = config["hyperparameters"].get("callbacks")
        if callbacks_config is not None:
            for cb_name, cb_conf in callbacks_config.items():
                if cb_name in default_callbacks_config:
                    default_callbacks_config[cb_name].update(cb_conf)

                    # Special dynamic parameters for callbacks
                    if cb_name == "ModelCheckpoint":
                        default_callbacks_config[cb_name][
                            "filepath"] = f'best_model_{config["id"]}.h5'

                    # replace patience hyperparameter
                    if "patience" in default_callbacks_config[cb_name]:
                        patience = config["hyperparameters"].get(
                            f"patience_{cb_name}")
                        if patience is not None:
                            default_callbacks_config[cb_name][
                                "patience"] = patience

                    # Import and create corresponding callback
                    Callback = import_callback(cb_name)
                    callbacks.append(
                        Callback(**default_callbacks_config[cb_name]))

                    if cb_name in ["EarlyStopping"]:
                        cb_requires_valid = "val" in cb_conf["monitor"].split(
                            "_")
                else:
                    logger.error(f"'{cb_name}' is not an accepted callback!")

        trainer = TrainerTrainValid(config=config, model=model)
        trainer.callbacks.extend(callbacks)

        last_only, with_pred = preproc_trainer(config)
        last_only = last_only and not cb_requires_valid

        history = trainer.train(with_pred=with_pred, last_only=last_only)

        # save history
        save_history(config.get("log_dir", None), history, config)

        result = compute_objective(config["objective"], history)
    else:
        # penalising actions if model cannot be created
        result = -1
    if result < -10 or np.isnan(result):
        result = -10
    return result