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)
def test_fit_evaluate(self): modelBuilder_keras = ModelBuilder.from_tfkeras(model_creator_keras) model = modelBuilder_keras.build(config={ "lr": 1e-2, "batch_size": 32, "metric": "mse" }) val_result = model.fit_eval(x=self.data["x"], y=self.data["y"], validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) assert val_result is not None
def test_fit_evaluate(self): modelBuilder = ModelBuilder.from_pytorch( model_creator=model_creator_pytorch, optimizer_creator=optimizer_creator, loss_creator=loss_creator) model = modelBuilder.build(config={ "lr": 1e-2, "batch_size": 32, }) val_result = model.fit_eval(x=self.data["x"], y=self.data["y"], validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) assert val_result is not None
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 test_predict(self): modelBuilder = ModelBuilder.from_pytorch( model_creator=model_creator_pytorch, optimizer_creator=optimizer_creator, loss_creator=loss_creator) model = modelBuilder.build(config={ "lr": 1e-2, "batch_size": 32, }) model.fit_eval(x=self.data["x"], y=self.data["y"], validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) pred = model.predict(x=self.data["val_x"]) try: import onnx import onnxruntime pred_onnx = model.predict_with_onnx(x=self.data["val_x"]) np.testing.assert_almost_equal(pred, pred_onnx) except ImportError: pass
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 test_evaluate(self): modelBuilder = ModelBuilder.from_pytorch( model_creator=model_creator_pytorch, optimizer_creator=optimizer_creator, loss_creator=loss_creator) model = modelBuilder.build(config={ "lr": 1e-2, "batch_size": 32, }) model.fit_eval(x=self.data["x"], y=self.data["y"], validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) mse_eval = model.evaluate(x=self.data["val_x"], y=self.data["val_y"]) try: import onnx import onnxruntime mse_eval_onnx = model.evaluate_with_onnx(x=self.data["val_x"], y=self.data["val_y"]) np.testing.assert_almost_equal(mse_eval, mse_eval_onnx) except ImportError: pass # incremental training test model.fit_eval(x=self.data["x"], y=self.data["y"], validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) mse_eval = model.evaluate(x=self.data["val_x"], y=self.data["val_y"]) try: import onnx import onnxruntime mse_eval_onnx = model.evaluate_with_onnx(x=self.data["val_x"], y=self.data["val_y"]) np.testing.assert_almost_equal(mse_eval, mse_eval_onnx) except ImportError: pass
def get_linear_data(a, b, size): x = np.arange(0, 10, 10 / size, dtype=np.float32) y = a*x + b return x, y train_x, train_y = get_linear_data(2, 5, 1000) df = pd.DataFrame({'x': train_x, 'y': train_y}) val_x, val_y = get_linear_data(2, 5, 400) val_df = pd.DataFrame({'x': val_x, 'y': val_y}) return df, val_df if __name__ == "__main__": # 1. the way to enable auto tuning model from creators. init_orca_context(init_ray_on_spark=True) 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="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. df, val_df = get_data() searcher.compile(df, modelBuilder, recipe=SimpleRecipe(), feature_cols=["x"], target_col="y",
def get_linear_data(a, b, size): x = np.arange(0, 10, 10 / size, dtype=np.float32) y = a * x + b return x, y train_x, train_y = get_linear_data(2, 5, 1000) val_x, val_y = get_linear_data(2, 5, 400) data = {'x': train_x, 'y': train_y, 'val_x': val_x, 'val_y': val_y} return data if __name__ == "__main__": # 1. the way to enable auto tuning model from creators. init_orca_context(init_ray_on_spark=True) 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="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 = get_data() searcher.compile(data=data, model_create_func=modelBuilder, recipe=SimpleRecipe())