Skip to content

Commit

Permalink
Adding experiment to MLflow_and_NeuralProphet.ipynb (#1499)
Browse files Browse the repository at this point in the history
* Update MLflow_and_NeuralProphet.ipynb

Adding example with MLflow Autologging, dataset, requirements & environment metadata with model signature inference

* Updating commentary

Updating commentary and fixing test related issues (e.g. docs build test).

* Updating documented code

Changed if local loop start.

* Unexpected indentation

Resolving unexpected indentation issue

---------

Co-authored-by: Oskar Triebe <ourownstory@users.noreply.github.com>
  • Loading branch information
cargecla1 and ourownstory authored Feb 13, 2024
1 parent 305de42 commit f140e1e
Showing 1 changed file with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"outputs": [],
"source": [
"# for this tutorial, we need to install MLflow.\n",
"#!pip install mlflow\n",
"# import mlflow\n",
"!pip install mlflow\n",
"\n",
"# Start a MLflow tracking-server on your local machine\n",
"# !mlflow server --host 127.0.0.1 --port 8080\n",
Expand All @@ -42,7 +41,7 @@
" # uninstall preinstalled packages from Colab to avoid conflicts\n",
" !pip uninstall -y torch notebook notebook_shim tensorflow tensorflow-datasets prophet torchaudio torchdata torchtext torchvision\n",
" !pip install git+https://github.com/ourownstory/neural_prophet.git # may take a while\n",
"\n",
" \n",
"# much faster using the following code, but may not have the latest upgrades/bugfixes\n",
"# pip install neuralprophet"
]
Expand All @@ -58,6 +57,7 @@
"source": [
"import pandas as pd\n",
"from neuralprophet import NeuralProphet, set_log_level, save\n",
"import mlflow\n",
"import time\n",
"\n",
"set_log_level(\"ERROR\")\n",
Expand Down Expand Up @@ -177,6 +177,7 @@
"# Start a new MLflow run\n",
"if local:\n",
" with mlflow.start_run():\n",
"\n",
" # Create a new MLflow experiment\n",
" mlflow.set_experiment(\"NP-MLflow Quickstart_v1\")\n",
"\n",
Expand All @@ -185,8 +186,8 @@
"\n",
" # Define NeuralProphet hyperparameters\n",
" params = {\n",
" \"n_lags\": 5,\n",
" \"n_forecasts\": 3,\n",
" \"n_lags\": 5,\n",
" \"n_forecasts\": 3,\n",
" }\n",
"\n",
" # Log Hyperparameters\n",
Expand All @@ -199,12 +200,12 @@
" end = time.time()\n",
"\n",
" # Log training duration\n",
" mlflow.log_metric(\"duration\", end - start)\n",
" mlflow.log_metric('duration', end-start)\n",
"\n",
" # Log training metrics\n",
" mlflow.log_metric(\"MAE_train\", value=list(metrics_train[\"MAE\"])[-1])\n",
" mlflow.log_metric(\"RMSE_train\", value=list(metrics_train[\"RMSE\"])[-1])\n",
" mlflow.log_metric(\"Loss_train\", value=list(metrics_train[\"Loss\"])[-1])\n",
" mlflow.log_metric('MAE_train', value=list(metrics_train['MAE'])[-1])\n",
" mlflow.log_metric('RMSE_train', value=list(metrics_train['RMSE'])[-1])\n",
" mlflow.log_metric('Loss_train', value=list(metrics_train['Loss'])[-1])\n",
"\n",
" # save model\n",
" model_path = \"np-model.np\"\n",
Expand Down Expand Up @@ -234,6 +235,90 @@
"In order to see the results of our run, we can navigate to the MLflow UI. Since we have already started the Tracking Server at http://localhost:8080, we can simply navigate to that URL in our browser and observe our experiments. \n",
"If we click on the respective experiments we can see a list of all runs associated with the experiment. Clicking on the run will take us to the run page, where the details of what we’ve logged will be shown."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example with MLflow Autologging, dataset, requirements & environment metadata with model signature inference, another MLflow Experiment with NeuralProphet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# MLflow setup\n",
"# Run this command with environment activated: mlflow ui --port xxxx (e.g. 5000, 5001, 5002)\n",
"# Copy and paste url from command line to web browser\n",
"\n",
"import mlflow\n",
"import torchmetrics\n",
"from mlflow.data.pandas_dataset import PandasDataset\n",
"\n",
"if local:\n",
"\n",
" mlflow.pytorch.autolog(log_every_n_epoch=1, log_every_n_step=None,\n",
" log_models=True, log_datasets=True, disable=False,\n",
" exclusive=False, disable_for_unsupported_versions=False,\n",
" silent=False, registered_model_name=None, extra_tags=None\n",
" )\n",
"\n",
" import mlflow.pytorch\n",
" from mlflow.client import MlflowClient\n",
"\n",
" model_name = \"NeuralProphet\"\n",
"\n",
"\n",
" with mlflow.start_run() as run:\n",
"\n",
" dataset: PandasDataset = mlflow.data.from_pandas(df, source=\"AirPassengersDataset\")\n",
"\n",
" # Log the dataset to the MLflow Run. Specify the \"training\" context to indicate that the\n",
" # dataset is used for model training\n",
" mlflow.log_input(dataset, context=\"training\")\n",
"\n",
" mlflow.log_param(\"model_type\", \"NeuralProphet\")\n",
" mlflow.log_param(\"n_lags\", 8)\n",
" mlflow.log_param(\"ar_layers\", [8, 8, 8, 8])\n",
" mlflow.log_param(\"accelerator\", \"gpu\")\n",
"\n",
" #To Train \n",
" # Import the NeuralProphet class\n",
" from neuralprophet import NeuralProphet, set_log_level\n",
"\n",
" # Disable logging messages unless there is an error\n",
" set_log_level(\"ERROR\")\n",
"\n",
" # Create a NeuralProphet model with default parameters\n",
" m = NeuralProphet(n_lags=8, \n",
" ar_layers=[8, 8, 8, 8],\n",
" trainer_config = {\"accelerator\":\"gpu\"})\n",
"\n",
" # Use static plotly in notebooks\n",
" m.set_plotting_backend(\"plotly-resampler\")\n",
"\n",
" # Fit the model on the dataset\n",
" metrics = m.fit(df)\n",
"\n",
" df_future = m.make_future_dataframe(df, n_historic_predictions=48, periods=12)\n",
"\n",
" # Predict the future\n",
" forecast = m.predict(df_future)\n",
"\n",
" mlflow.metrics.mae()\n",
"\n",
" # Save conda environment used to run the model\n",
" mlflow.pytorch.get_default_conda_env()\n",
"\n",
" # Save pip requirements\n",
" mlflow.pytorch.get_default_pip_requirements()\n",
"\n",
" # Registering model\n",
" model_uri = f\"runs:/{run.info.run_id}/NeuralProphet_test\"\n",
" mlflow.register_model(model_uri=model_uri, name=model_name)"
]
}
],
"metadata": {
Expand Down

0 comments on commit f140e1e

Please sign in to comment.