def test_unaligned_metric_value(self):
     modelBuilder_keras = KerasModelBuilder(model_creator_keras)
     model = modelBuilder_keras.build(config={
         "lr": 1e-2,
         "batch_size": 32,
     })
     with pytest.raises(ValueError):
         model.fit_eval(data=(self.data["x"], self.data["y"]),
                        validation_data=(self.data["val_x"], self.data["val_y"]),
                        metric='mae',
                        epochs=20)
 def test_fit_evaluate(self):
     modelBuilder_keras = KerasModelBuilder(model_creator_keras)
     model = modelBuilder_keras.build(config={
         "lr": 1e-2,
         "batch_size": 32,
         "metric": "mse"
     })
     val_result = model.fit_eval(data=(self.data["x"], self.data["y"]),
                                 validation_data=(self.data["val_x"], self.data["val_y"]),
                                 epochs=20)
     assert val_result is not None
    def test_uncompiled_model(self):
        def model_creator(config):
            """Returns a tf.keras model"""
            model = tf.keras.models.Sequential([
                tf.keras.layers.Dense(1)
            ])
            return model

        modelBuilder_keras = KerasModelBuilder(model_creator)
        with pytest.raises(ValueError):
            model = modelBuilder_keras.build(config={
                "lr": 1e-2,
                "batch_size": 32,
                "metric": "mse"
            })
            model.fit_eval(data=(self.data["x"], self.data["y"]),
                           validation_data=(self.data["val_x"], self.data["val_y"]),
                           epochs=20)
예제 #4
0
    def from_keras(
        *,
        model_creator,
        logs_dir="/tmp/auto_estimator_logs",
        resources_per_trial=None,
        name=None,
    ):
        """
        Create an AutoEstimator for tensorflow keras.

        :param model_creator: Tensorflow keras model creator function.
        :param logs_dir: Local directory to save logs and results. It defaults to
            "/tmp/auto_estimator_logs"
        :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 KerasModelBuilder
        model_builder = KerasModelBuilder(model_creator=model_creator)
        return AutoEstimator(model_builder=model_builder,
                             logs_dir=logs_dir,
                             resources_per_trial=resources_per_trial,
                             name=name)
    # 2. you can also use the model builder with a fix config
    model = modelBuilder.build(config={
        "lr": 1e-2,  # used in optimizer_creator
        "batch_size": 32,  # used in data_creator
    })

    model.fit_eval(data=data,
                   validation_data=validation_data,
                   epochs=20)
    val_result_pytorch_manual = model.evaluate(x=validation_data[0],
                                               y=validation_data[1],
                                               metrics=['rmse'])

    # 3. try another modelbuilder based on tfkeras
    modelBuilder_keras = KerasModelBuilder(model_creator_keras)
    model = modelBuilder_keras.build(config={
        "lr": 1e-2,  # used in optimizer_creator
        "batch_size": 32,  # used in data_creator
        "metric": "mse"
    })

    model.fit_eval(data=data,
                   validation_data=validation_data,
                   epochs=20)
    val_result_tensorflow_manual = model.evaluate(x=validation_data[0],
                                                  y=validation_data[1],
                                                  metrics=['rmse'])
    print("Searched best model validation rmse:", searched_best_model)
    print("Pytorch model validation rmse:", val_result_pytorch_manual)
    print("Tensorflow model validation rmse:", val_result_tensorflow_manual)