Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 60 additions & 38 deletions tutorials/get-started-notebooks/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
"import pandas as pd\n",
"import mlflow\n",
"import mlflow.sklearn\n",
"import joblib\n",
"from sklearn.ensemble import GradientBoostingClassifier\n",
"from sklearn.metrics import classification_report\n",
"from sklearn.model_selection import train_test_split\n",
Expand All @@ -192,6 +193,7 @@
" parser.add_argument(\"--n_estimators\", required=False, default=100, type=int)\n",
" parser.add_argument(\"--learning_rate\", required=False, default=0.1, type=float)\n",
" parser.add_argument(\"--registered_model_name\", type=str, help=\"model name\")\n",
" parser.add_argument(\"--model\", type=str, help=\"path to model output folder\", default=\"./outputs\")\n",
" args = parser.parse_args()\n",
" \n",
" # Start Logging\n",
Expand Down Expand Up @@ -252,41 +254,28 @@
" ##########################\n",
" #<save and register model>\n",
" ##########################\n",
" # Registering the model to the workspace\n",
" print(\"Registering the model via MLFlow\")\n",
"\n",
" # pin numpy\n",
" conda_env = {\n",
" 'name': 'mlflow-env',\n",
" 'channels': ['conda-forge'],\n",
" 'dependencies': [\n",
" 'python=3.10.15',\n",
" 'pip<=21.3.1',\n",
" {\n",
" 'pip': [\n",
" 'mlflow==2.17.0',\n",
" 'cloudpickle==2.2.1',\n",
" 'pandas==1.5.3',\n",
" 'psutil==5.8.0',\n",
" 'scikit-learn==1.5.2',\n",
" 'numpy==1.26.4',\n",
" ]\n",
" }\n",
" ],\n",
" }\n",
"\n",
" mlflow.sklearn.log_model(\n",
" sk_model=clf,\n",
" registered_model_name=args.registered_model_name,\n",
" artifact_path=args.registered_model_name,\n",
" conda_env=conda_env,\n",
" )\n",
"\n",
" # Saving the model to a file\n",
" mlflow.sklearn.save_model(\n",
" sk_model=clf,\n",
" path=os.path.join(args.registered_model_name, \"trained_model\"),\n",
" )\n",
" # Save the model to the component output folder first\n",
" print(\"Saving the model to the component output folder\")\n",
" os.makedirs(args.model, exist_ok=True)\n",
" model_path = os.path.join(args.model, \"model.pkl\")\n",
" joblib.dump(clf, model_path)\n",
" print(f\"Model saved to: {model_path}\")\n",
" \n",
" # Log model as artifact\n",
" mlflow.log_artifact(model_path, artifact_path=\"model\")\n",
" \n",
" # Log parameters manually to ensure they're captured\n",
" mlflow.log_param(\"n_estimators\", args.n_estimators)\n",
" mlflow.log_param(\"learning_rate\", args.learning_rate)\n",
" mlflow.log_param(\"test_train_ratio\", args.test_train_ratio)\n",
" \n",
" # Log additional metrics\n",
" from sklearn.metrics import accuracy_score\n",
" accuracy = accuracy_score(y_test, y_pred)\n",
" mlflow.log_metric(\"accuracy\", accuracy)\n",
" \n",
" print(f\"Model accuracy: {accuracy:.4f}\")\n",
" print(\"Model training completed and artifacts logged\")\n",
" ###########################\n",
" #</save and register model>\n",
" ###########################\n",
Expand Down Expand Up @@ -326,6 +315,33 @@
"* Since a compute resource was not specified, the script will be run on a [serverless compute cluster](https://learn.microsoft.com/azure/machine-learning/how-to-use-serverless-compute?view=azureml-api-2&tabs=python) that is automatically created."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a custom environment using the openmpi5.0-cuda12.4-ubuntu22.04 image\n",
"# This provides GPU support with CUDA 12.4 and updated Ubuntu 22.04\n",
"\n",
"from azure.ai.ml.entities import Environment\n",
"\n",
"# Create custom environment with the specified image\n",
"job_env = Environment(\n",
" name=\"openmpi5-cuda12-env\",\n",
" description=\"Custom environment with OpenMPI 5.0, CUDA 12.4, Ubuntu 22.04\",\n",
" image=\"mcr.microsoft.com/azureml/openmpi5.0-cuda12.4-ubuntu22.04:latest\",\n",
")\n",
"\n",
"# Register the environment\n",
"job_env = ml_client.environments.create_or_update(job_env)\n",
"\n",
"print(f\"✅ Custom environment created: {job_env.name}\")\n",
"print(f\" Environment version: {job_env.version}\")\n",
"print(\" Base image: openmpi5.0-cuda12.4-ubuntu22.04\")\n",
"print(\" This environment provides OpenMPI 5.0, CUDA 12.4 support, and Ubuntu 22.04\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -342,6 +358,7 @@
"\n",
"registered_model_name = \"credit_defaults_model\"\n",
"\n",
"# Use a stable curated environment for training\n",
"job = command(\n",
" inputs=dict(\n",
" data=Input(\n",
Expand All @@ -351,12 +368,15 @@
" test_train_ratio=0.2,\n",
" learning_rate=0.25,\n",
" registered_model_name=registered_model_name,\n",
" model=\"./outputs\",\n",
" ),\n",
" code=\"./src/\", # location of source code\n",
" command=\"python main.py --data ${{inputs.data}} --test_train_ratio ${{inputs.test_train_ratio}} --learning_rate ${{inputs.learning_rate}} --registered_model_name ${{inputs.registered_model_name}}\",\n",
" environment=\"azureml://registries/azureml/environments/sklearn-1.5/labels/latest\",\n",
" command=\"python main.py --data ${{inputs.data}} --test_train_ratio ${{inputs.test_train_ratio}} --learning_rate ${{inputs.learning_rate}} --registered_model_name ${{inputs.registered_model_name}} --model ${{inputs.model}}\",\n",
" environment=f\"{job_env.name}:{job_env.version}\", # Use our custom environment with openmpi5.0-cuda12.4-ubuntu22.04\n",
" display_name=\"credit_default_prediction\",\n",
")"
")\n",
"\n",
"print(f\"✅ Job configured with custom environment: {job_env.name}:{job_env.version}\")"
]
},
{
Expand Down Expand Up @@ -564,10 +584,12 @@
" name=\"blue\",\n",
" endpoint_name=online_endpoint_name,\n",
" model=model,\n",
" environment=f\"{job_env.name}:{job_env.version}\", # Use the same custom environment\n",
" instance_type=\"Standard_DS3_v2\",\n",
" instance_count=1,\n",
")\n",
"\n",
"print(f\"✅ Deployment configured with custom environment: {job_env.name}:{job_env.version}\")\n",
"blue_deployment = ml_client.begin_create_or_update(blue_deployment).result()"
]
},
Expand Down
Loading