Compare commits

...
Author SHA1 Message Date
Andrew Ferlitsch ec1e44372c Merge branch 'model_monitoring_custom' of https://github.com/GoogleCloudPlatform/vertex-ai-samples into model_monitoring_custom 2022-08-26 15:57:07 +00:00
Andrew Ferlitsch 798a3bcf2e fix: review updates 2022-08-26 15:56:26 +00:00
Andrew Ferlitsch 21b16af453 fix: review updates 2022-08-26 15:55:41 +00:00
Andrew FerlitschandGitHub 8574def9cb Merge branch 'main' into model_monitoring_custom 2022-08-26 08:46:30 -07:00
89f4571a2c Create explore_data_in_bigquery_with_workbench.ipynb (#885)
* Create explore_data_in_bigquery_with_workbench.ipynb

Adding in notebook for exploratory data analysis as part of "Data to AI" effort. See this Colab for what this notebook looks like after it is run: https://colab.research.google.com/drive/1JeNeMtj2A_5P5vo9wxSkrwHQM5JQSoAu. Submitting it with outputs shown since a lot of this about interactive visualization, which can inspire folks to use/read the notebook beyond just the code.

* Update CODEOWNERS

Adding owner for forthcoming exploratory data analysis notebook

* Update CODEOWNERS

* Updating exploratory data analysis notebook with latest updates from linter/review

* Updated notebook formatting to try to pass format test

* Trying again to pass notebook formatting test

* Trying again to pass notebook formatting test

* Trying again to pass notebook formatting test

* Linted version of notebook & better project picker

* Uploading linted version from ivanmkc@

* Update CODEOWNERS with EDA notebook

* Updated notebook w/ Tech Writer edits, re-ran all

* 1-2 minor text updates, try to pass linter again

* Trying w/ updated linted file from ivanmkc@

Co-authored-by: Ivan Cheung <ivans.mailbox@gmail.com>
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-08-26 08:46:17 -07:00
eaff81fd97 New Build model experimentation lineage with prebuild code (#785)
* new changes of build model notebook

* new changes of build model notebook

* linter test issues

* linter test issues

* review changes

* review changes

* review changes

* review changes

* review changes

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-08-26 08:42:15 -07:00
976ed94cf2 metrics_viz_run_compare_kfp (#847)
* added new cell for is_colab condition

* added new cell for is_colab condition

* changes andrew comments

* changes andrew comments

* review changes

* review changes

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-08-26 08:26:58 -07:00
Andrew Ferlitsch af1243b240 Merge branch 'model_monitoring_custom' of https://github.com/GoogleCloudPlatform/vertex-ai-samples into model_monitoring_custom 2022-08-26 04:36:33 +00:00
Andrew Ferlitsch 3fd9512bc0 fix: refining 2022-08-26 04:35:48 +00:00
Andrew Ferlitsch f3d12fd184 fix: refining 2022-08-26 04:34:36 +00:00
Andrew FerlitschandGitHub 8be3da91cc Merge branch 'main' into model_monitoring_custom 2022-08-25 10:35:45 -07:00
haomengchaoandGitHub 8ab8ef9ca9 update featurestore api version (#861)
* update api version

* fix tests

* remove unused import

* Run lint to format the change
2022-08-25 10:05:45 -07:00
Andrew Ferlitsch ac5b0f8575 feat: notebook for custom models 2022-08-25 16:51:55 +00:00
Andrew Ferlitsch 4a8d195df9 feat: notebook for custom models 2022-08-25 16:50:55 +00:00
eaff80a920 Fraud detection notebook2 (#821)
* Made minor changes

* Ran linter test

* Made changes mentioned in the review

* Ran Linter test

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-08-25 09:49:24 -07:00
Andrew FerlitschandGitHub 8646285c26 fix: fine-tuning (#902)
* feat: new mm notebook

* feat: new mm notebook

* fix: fine-tuning

* fix: fine-tuning

* fix: review comments

* fix: review comments

* fix: review comments

* fix: review comments
2022-08-25 08:34:34 -07:00
8 changed files with 822640 additions and 577 deletions
@@ -292,6 +292,37 @@
" PROJECT_ID = \"python-docs-samples-tests\" # @param {type:\"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d9f118b92c74"
},
"source": [
"#### UUID\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3ee72715c0fd"
},
"outputs": [],
"source": [
"import random\n",
"import string\n",
"\n",
"\n",
"# Generate a uuid of a specifed length(default=8)\n",
"def generate_uuid(length: int = 8) -> str:\n",
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
"\n",
"\n",
"UUID = generate_uuid()"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -478,7 +509,6 @@
"from google.cloud.aiplatform_v1.types import \\\n",
" featurestore_service as featurestore_service_pb2\n",
"from google.cloud.aiplatform_v1.types import io as io_pb2\n",
"from google.protobuf.duration_pb2 import Duration\n",
"\n",
"# Create admin_client for CRUD and data_client for reading feature values.\n",
"admin_client = FeaturestoreServiceClient(client_options={\"api_endpoint\": API_ENDPOINT})\n",
@@ -542,7 +572,7 @@
},
"outputs": [],
"source": [
"FEATURESTORE_ID = \"movie_prediction\"\n",
"FEATURESTORE_ID = f\"movie_prediction_{UUID}\"\n",
"try:\n",
" create_lro = admin_client.create_featurestore(\n",
" featurestore_service_pb2.CreateFeaturestoreRequest(\n",
@@ -567,7 +597,7 @@
"id": "ag8pCQ7rNjVf"
},
"source": [
"You can use [GetFeaturestore](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1beta1#google.cloud.aiplatform.v1beta1.FeaturestoreService.GetFeaturestore) or [ListFeaturestores](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1beta1#google.cloud.aiplatform.v1beta1.FeaturestoreService.ListFeaturestores) to check if the Featurestore was successfully created. The following example gets the details of the Featurestore.\n"
"You can use [GetFeaturestore](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1#google.cloud.aiplatform.v1.FeaturestoreService.GetFeaturestore) or [ListFeaturestores](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1#google.cloud.aiplatform.v1.FeaturestoreService.ListFeaturestores) to check if the Featurestore was successfully created. The following example gets the details of the Featurestore.\n"
]
},
{
@@ -589,7 +619,7 @@
"id": "018ab19d934f"
},
"source": [
"Auto scaling is available in v1beta1 since v1.11. Below is the example for the `CreateFeaturestoreRequest` with auto-scaling, use it with `aiplatform_v1beta1.FeaturestoreServiceClient` to create Featurestore:"
"Auto scaling is available in v1 since v1.11. Below is the example for the `CreateFeaturestoreRequest` with auto-scaling, use it with `aiplatform_v1.FeaturestoreServiceClient` to create Featurestore:"
]
},
{
@@ -600,17 +630,17 @@
},
"outputs": [],
"source": [
"from google.cloud.aiplatform_v1beta1.types import \\\n",
" featurestore as v1beta1_featurestore_pb2\n",
"from google.cloud.aiplatform_v1beta1.types import \\\n",
" featurestore_service as v1beta1_featurestore_service_pb2\n",
"from google.cloud.aiplatform_v1.types import \\\n",
" featurestore as v1_featurestore_pb2\n",
"from google.cloud.aiplatform_v1.types import \\\n",
" featurestore_service as v1_featurestore_service_pb2\n",
"\n",
"create_featurestore_request = v1beta1_featurestore_service_pb2.CreateFeaturestoreRequest(\n",
"create_featurestore_request = v1_featurestore_service_pb2.CreateFeaturestoreRequest(\n",
" parent=BASE_RESOURCE_PATH,\n",
" featurestore_id=FEATURESTORE_ID,\n",
" featurestore=v1beta1_featurestore_pb2.Featurestore(\n",
" online_serving_config=v1beta1_featurestore_pb2.Featurestore.OnlineServingConfig(\n",
" scaling=v1beta1_featurestore_pb2.Featurestore.OnlineServingConfig.Scaling(\n",
" featurestore=v1_featurestore_pb2.Featurestore(\n",
" online_serving_config=v1_featurestore_pb2.Featurestore.OnlineServingConfig(\n",
" scaling=v1_featurestore_pb2.Featurestore.OnlineServingConfig.Scaling(\n",
" min_node_count=1, max_node_count=5\n",
" )\n",
" ),\n",
@@ -681,7 +711,7 @@
"id": "dPkT7KDuEvWv"
},
"source": [
"Feature [monitoring](https://cloud.google.com/vertex-ai/docs/featurestore/monitoring) is in preview, so you need to use v1beta1 Python. Import feature analysis is only available through SDK for now."
"Feature [monitoring](https://cloud.google.com/vertex-ai/docs/featurestore/monitoring) is in preview, so you need to use v1 Python. Import feature analysis is only available through SDK for now."
]
},
{
@@ -692,36 +722,35 @@
},
"outputs": [],
"source": [
"from google.cloud.aiplatform_v1beta1 import \\\n",
" FeaturestoreServiceClient as v1beta1_FeaturestoreServiceClient\n",
"from google.cloud.aiplatform_v1beta1.types import \\\n",
" entity_type as v1beta1_entity_type_pb2\n",
"from google.cloud.aiplatform_v1beta1.types import \\\n",
" featurestore_monitoring as v1beta1_featurestore_monitoring_pb2\n",
"from google.cloud.aiplatform_v1beta1.types import \\\n",
" featurestore_service as v1beta1_featurestore_service_pb2\n",
"from google.cloud.aiplatform_v1 import \\\n",
" FeaturestoreServiceClient as v1_FeaturestoreServiceClient\n",
"from google.cloud.aiplatform_v1.types import entity_type as v1_entity_type_pb2\n",
"from google.cloud.aiplatform_v1.types import \\\n",
" featurestore_monitoring as v1_featurestore_monitoring_pb2\n",
"from google.cloud.aiplatform_v1.types import \\\n",
" featurestore_service as v1_featurestore_service_pb2\n",
"\n",
"v1beta1_admin_client = v1beta1_FeaturestoreServiceClient(\n",
"v1_admin_client = v1_FeaturestoreServiceClient(\n",
" client_options={\"api_endpoint\": API_ENDPOINT}\n",
")\n",
"\n",
"# Enable import feature analysis for users entity type.\n",
"# All Features belonging to this EntityType will by default inherit the monitoring config.\n",
"v1beta1_admin_client.update_entity_type(\n",
" v1beta1_featurestore_service_pb2.UpdateEntityTypeRequest(\n",
" entity_type=v1beta1_entity_type_pb2.EntityType(\n",
"v1_admin_client.update_entity_type(\n",
" v1_featurestore_service_pb2.UpdateEntityTypeRequest(\n",
" entity_type=v1_entity_type_pb2.EntityType(\n",
" name=admin_client.entity_type_path(\n",
" PROJECT_ID, REGION, FEATURESTORE_ID, \"users\"\n",
" ),\n",
" monitoring_config=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig(\n",
" import_features_analysis=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ImportFeaturesAnalysis(\n",
" anomaly_detection_baseline=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ImportFeaturesAnalysis.Baseline.LATEST_STATS,\n",
" state=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ImportFeaturesAnalysis.State.ENABLED,\n",
" monitoring_config=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig(\n",
" import_features_analysis=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ImportFeaturesAnalysis(\n",
" anomaly_detection_baseline=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ImportFeaturesAnalysis.Baseline.LATEST_STATS,\n",
" state=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ImportFeaturesAnalysis.State.ENABLED,\n",
" ),\n",
" numerical_threshold_config=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ThresholdConfig(\n",
" numerical_threshold_config=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ThresholdConfig(\n",
" value=0.001,\n",
" ),\n",
" categorical_threshold_config=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ThresholdConfig(\n",
" categorical_threshold_config=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ThresholdConfig(\n",
" value=0.001,\n",
" ),\n",
" ),\n",
@@ -736,7 +765,7 @@
"id": "85b1f59fbf6d"
},
"source": [
"The easiest way to set up snapshot analysis for now is using [console UI](https://console.cloud.google.com/vertex-ai/features). For completeness, below is example to do this using v1beta1 SDK.\n",
"The easiest way to set up snapshot analysis for now is using [console UI](https://console.cloud.google.com/vertex-ai/features). For completeness, below is example to do this using v1 SDK.\n",
"\n",
"You can view monitoring statistics on [console UI](https://console.cloud.google.com/vertex-ai/features)."
]
@@ -749,36 +778,35 @@
},
"outputs": [],
"source": [
"from google.cloud.aiplatform_v1beta1 import \\\n",
" FeaturestoreServiceClient as v1beta1_FeaturestoreServiceClient\n",
"from google.cloud.aiplatform_v1beta1.types import \\\n",
" entity_type as v1beta1_entity_type_pb2\n",
"from google.cloud.aiplatform_v1beta1.types import \\\n",
" featurestore_monitoring as v1beta1_featurestore_monitoring_pb2\n",
"from google.cloud.aiplatform_v1beta1.types import \\\n",
" featurestore_service as v1beta1_featurestore_service_pb2\n",
"from google.cloud.aiplatform_v1 import \\\n",
" FeaturestoreServiceClient as v1_FeaturestoreServiceClient\n",
"from google.cloud.aiplatform_v1.types import entity_type as v1_entity_type_pb2\n",
"from google.cloud.aiplatform_v1.types import \\\n",
" featurestore_monitoring as v1_featurestore_monitoring_pb2\n",
"from google.cloud.aiplatform_v1.types import \\\n",
" featurestore_service as v1_featurestore_service_pb2\n",
"\n",
"v1beta1_admin_client = v1beta1_FeaturestoreServiceClient(\n",
"v1_admin_client = v1_FeaturestoreServiceClient(\n",
" client_options={\"api_endpoint\": API_ENDPOINT}\n",
")\n",
"\n",
"# Enable snapshot analysis for users entity type.\n",
"# All Features belonging to this EntityType will by default inherit the monitoring config.\n",
"v1beta1_admin_client.update_entity_type(\n",
" v1beta1_featurestore_service_pb2.UpdateEntityTypeRequest(\n",
" entity_type=v1beta1_entity_type_pb2.EntityType(\n",
"v1_admin_client.update_entity_type(\n",
" v1_featurestore_service_pb2.UpdateEntityTypeRequest(\n",
" entity_type=v1_entity_type_pb2.EntityType(\n",
" name=admin_client.entity_type_path(\n",
" PROJECT_ID, REGION, FEATURESTORE_ID, \"users\"\n",
" ),\n",
" monitoring_config=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig(\n",
" snapshot_analysis=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.SnapshotAnalysis(\n",
" monitoring_interval=Duration(seconds=86400), # 1 day\n",
" monitoring_config=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig(\n",
" snapshot_analysis=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.SnapshotAnalysis(\n",
" monitoring_interval_days=1, # 1 day\n",
" staleness_days=30,\n",
" ),\n",
" numerical_threshold_config=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ThresholdConfig(\n",
" numerical_threshold_config=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ThresholdConfig(\n",
" value=0.001,\n",
" ),\n",
" categorical_threshold_config=v1beta1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ThresholdConfig(\n",
" categorical_threshold_config=v1_featurestore_monitoring_pb2.FeaturestoreMonitoringConfig.ThresholdConfig(\n",
" value=0.001,\n",
" ),\n",
" ),\n",
@@ -891,8 +919,8 @@
"source": [
"## Search created features\n",
"\n",
"While the [ListFeatures](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1beta1#google.cloud.aiplatform.v1beta1.FeaturestoreService.ListFeatures) method allows you to easily view all features of a single\n",
"entity type, the [SearchFeatures](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1beta1#google.cloud.aiplatform.v1beta1.FeaturestoreService.SearchFeatures) method searches across all featurestores\n",
"While the [ListFeatures](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1#google.cloud.aiplatform.v1.FeaturestoreService.ListFeatures) method allows you to easily view all features of a single\n",
"entity type, the [SearchFeatures](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1#google.cloud.aiplatform.v1.FeaturestoreService.SearchFeatures) method searches across all featurestores\n",
"and entity types in a given location (such as `us-central1`). This can help you discover features that were created by someone else.\n",
"\n",
"You can query based on feature properties including feature ID, entity type ID,\n",
@@ -1206,7 +1234,7 @@
},
"source": [
"The\n",
"[Online Serving APIs](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1beta1#featurestoreonlineservingservice)\n",
"[Online Serving APIs](https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1#featurestoreonlineservingservice)\n",
"lets you serve feature values for small batches of entities. It's designed for latency-sensitive service, such as online model prediction. For example, for a movie service, you might want to quickly shows movies that the current user would most likely watch by using online predictions."
]
},
File diff suppressed because it is too large Load Diff
@@ -502,6 +502,86 @@
"- For the CSV data source, grant both service accounts the \"Storage Object Viewer\" role."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bucket:custom"
},
"source": [
"### Create a Cloud Storage bucket\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"Set the name of your Cloud Storage bucket below, which you use in this tutorial to upload the `input schema` for the monitoring service.\n",
"\n",
"Bucket names must be globally unique across all Google Cloud projects, including those outside of your organization."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "bucket"
},
"outputs": [],
"source": [
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "autoset_bucket"
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_bucket"
},
"source": [
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_bucket"
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "validate_bucket"
},
"source": [
"Finally, validate access to your Cloud Storage bucket by examining its contents:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "validate_bucket"
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -694,16 +774,16 @@
"2. Configure a model monitoring specification.\n",
"3. Upload the model monitoring specification to the `Vertex AI Endpoint`.\n",
"4. Upload or automatic generation of the `input schema` for parsing.\n",
"5. For feature skew detection, upload the training data to automatic generation of the feature distribution.\n",
"5. For feature skew detection, upload the training data for automatic generation of the feature distribution.\n",
"6. For feature attributions, upload corresponding `Vertex AI Explainability` specification.\n",
"\n",
"Once configured, you can enable/disable monitoring, change alerts and update the model monitoring configuration. \n",
"\n",
"When model monitoring is enabled, incoming prediction requests are logged in a BigQuery table. The input feature values contained in the logged requests are then analyzed for skew or drift. You can set a sampling rate to monitor a subset of the production inputs to a model.\n",
"When model monitoring is enabled, the sampled incoming prediction requests are logged into a BigQuery table. The input feature values contained in the logged requests are then analyzed for skew or drift on an specified interval basis. You set a sampling rate to monitor a subset of the production inputs to a model, and the monitoring interval.\n",
"\n",
"The model monitoring service needs to know how to parse the feature values, which is referred to as the input schema. For AutoML tabular models, the input schema is automatically provided. For custom tabular models, the service will attempt to automatically derive the input schema from the first 1000 prediction requests. Alternatively, one can upload the input schema.\n",
"\n",
"For skew detection, requires a baseline for the statistical distribution of values in the training data. For AutoML tabular models this is automatically derived. For custom tabular models, you upload the training data to the service, and have the service automatically derived the distribution.\n",
"For skew detection, the monitoring service requires a baseline for the statistical distribution of values in the training data. For AutoML tabular models this is automatically derived. For custom tabular models, you upload the training data to the service, and have the service automatically derive the distribution.\n",
"\n",
"For feature attribution skew and drift detection, requires enabling your deployed model for `Vertex AI Explainability`\n",
"\n",
@@ -1144,7 +1224,11 @@
"source": [
"#### Monitoring Job State\n",
"\n",
"After you start the `Vertex AI Model Monitoring` job, it will be in a `PENDING` state until the `input schema` and `skew distribution baselines` are calculated. The process happens sequentially. In this example where we use automatic generation of the `input schema` will stay in a `PENDING` state until the 1000 prediction request (discussed subsequently) is sent. Once the `input schema` has been generated, then a batch job will be initiated to generate the distribution baseline from the training data. Once the baseline distribution is generated, then the monitoring job will enter `RUNNING` state."
"After you start the `Vertex AI Model Monitoring` job, it will be in a `PENDING` state until the `input schema` and `skew distribution baselines` are calculated. The process happens sequentially. In this example where you use automatic generation of the `input schema`, the service stays in a `PENDING` state until the 1000 prediction request (discussed subsequently) is sent. \n",
"\n",
"Once the `input schema` has been generated, then a batch job will be initiated to generate the distribution baseline from the training data. Again, the service stays in a `PENDING` state until the baseline distribution is calculated.\n",
"\n",
"Once the baseline distribution is generated, then the monitoring job will enter `OFFLINE` state. On the per interval basis -- e.g., once an hour, the monitoring job will enter `RUNNING` state while analyzing the sampled data. Once completed, it will return to an `OFFLINE` state while awaiting the next scheduled analysis."
]
},
{
@@ -1243,7 +1327,7 @@
"\n",
"### Automatic generation of the baseline distribution\n",
"\n",
"After the `input schema` is generated, the monitoring service creates a batch job to analyze for first 1000 predictions to generate the baseline distribution. Once completed, the monitoring service will be in `RUNNING` state."
"After the `input schema` is generated, the monitoring service creates a batch job to analyze the training data to determine the baseline distribution. "
]
},
{
@@ -1254,16 +1338,9 @@
},
"outputs": [],
"source": [
"import time\n",
"\n",
"while True:\n",
" time.sleep(60)\n",
" jobs = monitoring_job.list(filter=f\"display_name=churn_{UUID}\")\n",
" job = jobs[0]\n",
" print(job.state)\n",
" if job.state == aiplatform.gapic.JobState.JOB_STATE_PENDING:\n",
" continue\n",
" break"
"# Pause a bit for the baseline distribution to be calculated\n",
"if os.getenv(\"IS_TESTING\"):\n",
" time.sleep(120)"
]
},
{
@@ -1415,7 +1492,7 @@
"source": [
"# Delete the monitoring logged data BigQuery dataset\n",
"\n",
"! bq rm -r -f {PROJECT_ID}:model_deployment_monitoring_{ENDPOINT_ID}"
"! bq rm -r -f {PROJECT_ID}.model_deployment_monitoring_{ENDPOINT_ID}"
]
},
{
@@ -1430,7 +1507,7 @@
"\n",
"#### Create the predefined input schema\n",
"\n",
"The predefined `input schema` is specified as a YAML file. In this example, you retrieve the BigQuery schema for the training data, which includes the feature names and data types, to generate the YAML specification.\n",
"The predefined `input schema` is specified as a YAML file. In this example, you retrieve the BigQuery schema for the training data, which includes the feature names and data types, to generate the YAML specification. The predefined `input schema` must be loaded to a Cloud Storage location.\n",
"\n",
"Learn more about [Custom instance schemas for parsing input](https://cloud.google.com/vertex-ai/docs/model-monitoring/overview#custom-input-schemas)."
]
@@ -1475,7 +1552,9 @@
"print(yaml)\n",
"\n",
"with open(\"schema.yaml\", \"w\") as f:\n",
" f.write(yaml)"
" f.write(yaml)\n",
"\n",
"! gsutil cp schema.yaml {BUCKET_URI}/schema.yaml"
]
},
{
@@ -1508,7 +1587,7 @@
" schedule_config=schedule_config,\n",
" alert_config=alerting_config,\n",
" objective_configs=objective_config,\n",
" analysis_instance_schema_uri=\"schema.yaml\",\n",
" analysis_instance_schema_uri=f\"{BUCKET_URI}/schema.yaml\",\n",
")\n",
"\n",
"print(monitoring_job)"
@@ -1588,7 +1667,12 @@
"id": "18889460bd33"
},
"source": [
"### Cleanup"
"## Cleaning up\n",
"\n",
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial."
]
},
{
@@ -1599,7 +1683,14 @@
},
"outputs": [],
"source": [
"! rm -f schema.yaml"
"delete_bucket = False\n",
"\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -rf {BUCKET_URI}\n",
"\n",
"! rm -f schema.yaml\n",
"\n",
"! bq rm -r -f {PROJECT_ID}.model_deployment_monitoring_{ENDPOINT_ID}"
]
}
],
+1
View File
@@ -30,3 +30,4 @@
/custom/custom_training_tensorboard_profiler.ipynb @itseric
/workbench/spark/spark_sample_notebook.ipynb @bmiro
/model-registry/bqml-vertexai-model-registry.ipynb @soheilazangeneh
/workbench/exploratory_data_analysis/explore_data_in_bigquery_with_workbench.ipynb @alokpattani
@@ -8,7 +8,7 @@
},
"outputs": [],
"source": [
"# Copyright 2022 Google LLC\n",
"# Copyright 2021 Google LLC\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
@@ -54,13 +54,31 @@
{
"cell_type": "markdown",
"metadata": {
"id": "tvgnzT1CKxrO"
"id": "d975c5729f18"
},
"source": [
"## Overview\n",
"\n",
"As a Data Scientist, you want to be able to reuse code path (data preprocessing, feature engineering etc...) that others within your team have written to simplify and standardize all the complex data wrangling. \n",
"As a Data Scientist, you want to be able to reuse code path (data preprocessing, feature engineering etc...) that others within your team have written to simplify and standardize all the complex data wrangling. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3a0f8061b9c1"
},
"source": [
"### Objective\n",
"\n",
"In this notebook, you learn how to integrate preprocessing code in a Vertex AI experiments. Also you build the experiment lineage lets you record, analyze, debug, and audit metadata and artifacts produced along your ML journey."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tvgnzT1CKxrO"
},
"source": [
"### Dataset\n",
"\n",
"This dataset is the UCI News Aggregator Data Set which contains 422,937 news collected between March 10th, 2014 and August 10th, 2014. Below are example records from the dataset:\n",
@@ -72,13 +90,15 @@
"|2 |Fed's Charles Plosser sees high bar for change in pace of tapering |http://www.livemint.com/Politics/H2EvwJSK2VE6OF7iK1g3PP/Feds-Charles-Plosser-sees-high-bar-for-change-in-pace-of-ta.html |Livemint |b |ddUyU0VZz0BRneMioxUPQVP6sIxvM|www.livemint.com |1394470371207|\n",
"|3 |US open: Stocks fall after Fed official hints at accelerated tapering|http://www.ifamagazine.com/news/us-open-stocks-fall-after-fed-official-hints-at-accelerated-tapering-294436 |IFA Magazine |b |ddUyU0VZz0BRneMioxUPQVP6sIxvM|www.ifamagazine.com|1394470371550|\n",
"|4 |Fed risks falling 'behind the curve', Charles Plosser says |http://www.ifamagazine.com/news/fed-risks-falling-behind-the-curve-charles-plosser-says-294430 |IFA Magazine |b |ddUyU0VZz0BRneMioxUPQVP6sIxvM|www.ifamagazine.com|1394470371793|\n",
"|5 |Fed's Plosser: Nasty Weather Has Curbed Job Growth |http://www.moneynews.com/Economy/federal-reserve-charles-plosser-weather-job-growth/2014/03/10/id/557011 |Moneynews |b |ddUyU0VZz0BRneMioxUPQVP6sIxvM|www.moneynews.com |1394470372027|\n",
"\n",
"\n",
"### Objective\n",
"\n",
"In this notebook, you learn how to integrate preprocessing code in a Vertex AI experiments. Also you will build the experiment lineage lets you record, analyze, debug, and audit metadata and artifacts produced along your ML journey.\n",
"\n",
"|5 |Fed's Plosser: Nasty Weather Has Curbed Job Growth |http://www.moneynews.com/Economy/federal-reserve-charles-plosser-weather-job-growth/2014/03/10/id/557011 |Moneynews |b |ddUyU0VZz0BRneMioxUPQVP6sIxvM|www.moneynews.com |1394470372027|"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5e2eba58ad71"
},
"source": [
"### Costs \n",
"\n",
"This tutorial uses billable components of Google Cloud:\n",
@@ -150,7 +170,7 @@
"source": [
"### Install additional packages\n",
"\n",
"Install additional package dependencies not installed in your notebook environment, such as TensorFlow or Vertex AI SDK. Use the latest major GA version of each package."
"Install additional package dependencies not installed in your notebook environment,TensorFlow or Vertex AI SDK. Use the latest major GA version of each package."
]
},
{
@@ -175,7 +195,7 @@
" USER_FLAG = \"--user\"\n",
"\n",
"! pip3 install {USER_FLAG} --upgrade joblib fsspec gcsfs scikit-learn -q\n",
"! pip3 install {USER_FLAG} --force-reinstall 'google-cloud-aiplatform>=1.15' -q"
"! pip install {USER_FLAG} --upgrade google-cloud-aiplatform -q"
]
},
{
@@ -208,21 +228,14 @@
" app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lWEdiXsJg0XY"
},
"source": [
"## Before you begin"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BF1j6f9HApxa"
},
"source": [
"## Before you begin\n",
"\n",
"### Set up your Google Cloud project\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
@@ -233,7 +246,7 @@
"\n",
"1. [Enable APIs](https://console.cloud.google.com/flows/enableapi?apiid=cloudresourcemanager.googleapis.com,aiplatform.googleapis.com).\n",
"\n",
"1. If you are running this notebook locally, you will need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
"1. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
"\n",
"1. Enter your project ID in the cell below. Then run the cell to make sure the\n",
"Cloud SDK uses the right project for all the commands in this notebook.\n",
@@ -343,9 +356,9 @@
"id": "06571eb4063b"
},
"source": [
"#### Timestamp\n",
"#### UUID\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
]
},
{
@@ -356,9 +369,16 @@
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import random\n",
"import string\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
"\n",
"# Generate a uuid of a specifed length(default=8)\n",
"def generate_uuid(length: int = 8) -> str:\n",
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
"\n",
"\n",
"UUID = generate_uuid()"
]
},
{
@@ -370,7 +390,7 @@
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
"authenticated. Skip this step."
"authenticated."
]
},
{
@@ -484,7 +504,7 @@
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
" BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
@@ -537,17 +557,6 @@
"### Set project folder"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "AARD6Fsr-DSi"
},
"outputs": [],
"source": [
"DATA_PATH = \"data\""
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -556,6 +565,7 @@
},
"outputs": [],
"source": [
"DATA_PATH = \"data\"\n",
"!mkdir -m 777 -p {DATA_PATH}"
]
},
@@ -568,17 +578,6 @@
"### Get the data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3V6W2nIo9FtL"
},
"outputs": [],
"source": [
"DATASET_URL = \"https://archive.ics.uci.edu/ml/machine-learning-databases/00359/NewsAggregatorDataset.zip\""
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -587,6 +586,7 @@
},
"outputs": [],
"source": [
"DATASET_URL = \"https://archive.ics.uci.edu/ml/machine-learning-databases/00359/NewsAggregatorDataset.zip\"\n",
"!wget --no-parent {DATASET_URL} --directory-prefix={DATA_PATH}\n",
"!mkdir -m 777 -p {DATA_PATH}/temp {DATA_PATH}/raw\n",
"!unzip {DATA_PATH}/*.zip -d {DATA_PATH}/temp\n",
@@ -662,7 +662,7 @@
"# Experiments\n",
"TASK = \"classification\"\n",
"MODEL_TYPE = \"naivebayes\"\n",
"EXPERIMENT_NAME = f\"{TASK}-{MODEL_TYPE}-{TIMESTAMP}\"\n",
"EXPERIMENT_NAME = f\"{TASK}-{MODEL_TYPE}-{UUID}\"\n",
"EXPERIMENT_RUN_NAME = \"run-1\"\n",
"\n",
"# Preprocessing\n",
@@ -690,7 +690,7 @@
"FEATURES = \"title\"\n",
"TEST_SIZE = 0.2\n",
"SEED = 8\n",
"TRAINED_MODEL_URI = f\"{BUCKET_URI}/deliverables/{TIMESTAMP}\"\n",
"TRAINED_MODEL_URI = f\"{BUCKET_URI}/deliverables/{UUID}\"\n",
"MODEL_NAME = f\"{EXPERIMENT_NAME}-model\""
]
},
@@ -800,7 +800,7 @@
"source": [
"#### Create a Dataset Metadata Artifact\n",
"\n",
"First you create the Dataset artifact to track the dataset resource in the Vertex AI ML Metadata and create the experiment lineage."
"First you create the Dataset artifact to track the dataset resource in the Vertex ML Metadata and create the experiment lineage."
]
},
{
@@ -839,7 +839,6 @@
"Preprocess module\n",
"\"\"\"\n",
"\n",
"import string\n",
"\n",
"import pandas as pd\n",
"\n",
@@ -869,7 +868,10 @@
"source": [
"#### Add the `preprocessing` Execution\n",
"\n",
"You add the preprocessing step to track its execution in the lineage associated to Vertex AI Experiment. "
"Vertex AI Experiments supports tracking both executions and artifacts. Executions are steps in an ML workflow that can include but are not limited to data preprocessing, training, and model evaluation. Executions can consume artifacts such as datasets and produce artifacts such as models.\n",
"\n",
"You add the preprocessing step to track its execution in the lineage associated to Vertex AI Experiment. \n",
"For Vertex AI, the parameters are passed inside the message field which we see in the logs. These structures of the logs are predefined."
]
},
{
@@ -943,7 +945,16 @@
"source": [
"#### Create model training module\n",
"\n",
"Below the training module."
"Below the training module.\n",
"\n",
"**get_training_split :** It takes parameters like x(The data to be split), y(The labels to be split), test_size(The proportion of the data to be reserved for testing) and random_state(The seed used by the random number generator).\n",
"This function return training data, testing data , The training labels and The testing labels.\n",
"\n",
"**get_pipeline :** It return's the model.\n",
"\n",
"**train_pipeline :** It train the model by using model, training data, training lables and return's the trained model.\n",
"\n",
"**evaluate_model :** It evaluate the model and return the accuracy of the model.\n"
]
},
{
@@ -1151,6 +1162,15 @@
" exc.assign_output_artifacts([model])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e595c893de8d"
},
"source": [
"### Stop Experiment run"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -1170,7 +1190,7 @@
"source": [
"### Visualize Experiment Lineage\n",
"\n",
"Below you will get the link to Vertex AI Metadata UI in the console that will show the experiment lineage."
"Below you get the link to Vertex AI Metadata UI in the console that show the experiment lineage."
]
},
{
@@ -1208,17 +1228,8 @@
"source": [
"# Delete experiment\n",
"exp = vertex_ai.Experiment(EXPERIMENT_NAME)\n",
"exp.delete()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gW8Ddbr8xaKp"
},
"outputs": [],
"source": [
"exp.delete()\n",
"\n",
"# Delete model\n",
"model_list = vertex_ai.Model.list(filter=f'display_name=\"{MODEL_NAME}\"')\n",
"for model in model_list:\n",
@@ -1230,22 +1241,15 @@
" filter=f'display_name=\"{dataset_name}\"'\n",
" )\n",
" for dataset in dataset_list:\n",
" dataset.delete()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "sx_vKniMq9ZX"
},
"outputs": [],
"source": [
" dataset.delete()\n",
"\n",
"# Delete Cloud Storage objects that were created\n",
"delete_bucket = True\n",
"\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil -m rm -r $BUCKET_URI"
" ! gsutil -m rm -r $BUCKET_URI\n",
"\n",
"!rm -Rf {DATA_PATH}"
]
},
{
@@ -64,30 +64,6 @@
"This notebook shows how to use [the Kubeflow Pipelines (KFP) SDK](https://www.kubeflow.org/docs/components/pipelines/) to build [Vertex AI Pipelines](https://cloud.google.com/vertex-ai/docs/pipelines) that generate model metrics and metrics visualizations, and comparing pipeline runs."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dataset:wine,lcn,sklearn"
},
"source": [
"### Datasets\n",
"\n",
"The dataset used for this tutorial is the [Wine dataset](https://archive.ics.uci.edu/ml/datasets/wine) from [Scikit-learn builtin datasets](https://scikit-learn.org/stable/datasets.html).\n",
"\n",
"The dataset predicts the origin of a wine."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dataset:iris,lcn,sklearn"
},
"source": [
"The dataset used for this tutorial is the [Iris dataset](https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html) from [Scikit-learn builtin datasets](https://scikit-learn.org/stable/datasets.html).\n",
"\n",
"The dataset predicts the type of Iris flower species from a class of three species: setosa, virginica, or versicolor."
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -112,6 +88,30 @@
"- Compare metrics across pipeline runs"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dataset:wine,lcn,sklearn"
},
"source": [
"### Datasets\n",
"\n",
"The dataset used for this tutorial is the [Wine dataset](https://archive.ics.uci.edu/ml/datasets/wine) from [Scikit-learn builtin datasets](https://scikit-learn.org/stable/datasets.html).\n",
"\n",
"The dataset predicts the origin of a wine."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dataset:iris,lcn,sklearn"
},
"source": [
"The dataset used for this tutorial is the [Iris dataset](https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html) from [Scikit-learn builtin datasets](https://scikit-learn.org/stable/datasets.html).\n",
"\n",
"The dataset predicts the type of Iris flower species from a class of three species: setosa, virginica, or versicolor."
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -202,7 +202,7 @@
"! pip3 install {USER_FLAG} kfp google-cloud-pipeline-components --upgrade -q\n",
"\n",
"if os.getenv(\"IS_TESTING\"):\n",
" ! pip3 install --upgrade matplotlib $USER_FLAG"
" ! pip3 install --upgrade matplotlib $USER_FLAG -q"
]
},
{
@@ -240,6 +240,8 @@
"id": "check_versions"
},
"source": [
"### KFP SDK version\n",
"\n",
"Check the versions of the packages you installed. The KFP SDK version should be >=1.6."
]
},
@@ -349,7 +351,10 @@
},
"outputs": [],
"source": [
"REGION = \"us-central1\" # @param {type: \"string\"}"
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -358,9 +363,9 @@
"id": "timestamp"
},
"source": [
"#### Timestamp\n",
"#### UUID\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append the timestamp onto the name of resources you create in this tutorial."
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
]
},
{
@@ -371,9 +376,16 @@
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import random\n",
"import string\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
"\n",
"# Generate a uuid of a specifed length(default=8)\n",
"def generate_uuid(length: int = 8) -> str:\n",
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
"\n",
"\n",
"UUID = generate_uuid()"
]
},
{
@@ -384,7 +396,7 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Vertex AI Workbench Notebook**, your environment is already authenticated. Skip this step.\n",
"**If you are using Vertex AI Workbench Notebook**, your environment is already authenticated.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
@@ -479,7 +491,7 @@
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
@@ -553,6 +565,10 @@
},
"outputs": [],
"source": [
"import sys\n",
"\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"\n",
"if (\n",
" SERVICE_ACCOUNT == \"\"\n",
" or SERVICE_ACCOUNT is None\n",
@@ -912,12 +928,12 @@
},
"outputs": [],
"source": [
"DISPLAY_NAME = \"iris_\" + TIMESTAMP\n",
"DISPLAY_NAME = \"iris_\" + UUID\n",
"\n",
"job = aip.PipelineJob(\n",
" display_name=DISPLAY_NAME,\n",
" template_path=\"tabular classification_pipeline.json\".replace(\" \", \"_\"),\n",
" job_id=f\"tabular classification-v2{TIMESTAMP}-1\".replace(\" \", \"\"),\n",
" job_id=f\"tabular classification-v2{UUID}-1\".replace(\" \", \"\"),\n",
" pipeline_root=PIPELINE_ROOT,\n",
" parameter_values={\"seed\": 7, \"splits\": 10},\n",
")\n",
@@ -950,7 +966,18 @@
"\n",
"Next, generate another pipeline run that uses a different `seed` and `split` for the `iris_logregression` step.\n",
"\n",
"Submit the new pipeline run:"
"Submit the new pipeline run:\n",
"\n",
"\n",
"**pipeline_root :** Specify a Cloud Storage URI that your pipelines service account can access. The artifacts of your pipeline runs are stored within the pipeline root. \n",
"\n",
"**display_name :** The name of the pipeline, this will show up in the Google Cloud console. \n",
"\n",
"**parameter_values :** The pipeline parameters to pass to this run. For example, create a dict() with the parameter names as the dictionary keys and the parameter values as the dictionary values. \n",
"\n",
"**job_id :** A unique identifier for this pipeline run. If the job ID is not specified, Vertex AI Pipelines creates a job ID for you using the pipeline name and the timestamp of when the pipeline run was started. \n",
"\n",
"**template_path :** complete pipeline path"
]
},
{
@@ -962,9 +989,9 @@
"outputs": [],
"source": [
"job = aip.PipelineJob(\n",
" display_name=\"iris_\" + TIMESTAMP,\n",
" display_name=\"iris_\" + UUID,\n",
" template_path=\"tabular classification_pipeline.json\".replace(\" \", \"_\"),\n",
" job_id=f\"tabular classification-pipeline-v2{TIMESTAMP}-2\".replace(\" \", \"\"),\n",
" job_id=f\"tabular classification-pipeline-v2{UUID}-2\".replace(\" \", \"\"),\n",
" pipeline_root=PIPELINE_ROOT,\n",
" parameter_values={\"seed\": 5, \"splits\": 7},\n",
")\n",
@@ -1081,16 +1108,7 @@
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial -- *Note:* this is auto-generated and not all resources may be applicable for this tutorial:\n",
"\n",
"- Dataset\n",
"- Pipeline\n",
"- Model\n",
"- Endpoint\n",
"- Batch Job\n",
"- Custom Job\n",
"- Hyperparameter Tuning Job\n",
"- Cloud Storage Bucket"
"Otherwise, you can delete the individual resources you created in this tutorial -- *Note:* this is auto-generated and not all resources may be applicable for this tutorial:"
]
},
{
@@ -1101,94 +1119,9 @@
},
"outputs": [],
"source": [
"delete_dataset = True\n",
"delete_pipeline = True\n",
"delete_model = True\n",
"delete_endpoint = True\n",
"delete_batchjob = True\n",
"delete_customjob = True\n",
"delete_hptjob = True\n",
"delete_bucket = True\n",
"\n",
"try:\n",
" if delete_model and \"DISPLAY_NAME\" in globals():\n",
" models = aip.Model.list(\n",
" filter=f\"display_name={DISPLAY_NAME}\", order_by=\"create_time\"\n",
" )\n",
" model = models[0]\n",
" aip.Model.delete(model)\n",
" print(\"Deleted model:\", model)\n",
"except Exception as e:\n",
" print(e)\n",
"\n",
"try:\n",
" if delete_endpoint and \"DISPLAY_NAME\" in globals():\n",
" endpoints = aip.Endpoint.list(\n",
" filter=f\"display_name={DISPLAY_NAME}_endpoint\", order_by=\"create_time\"\n",
" )\n",
" endpoint = endpoints[0]\n",
" endpoint.undeploy_all()\n",
" aip.Endpoint.delete(endpoint.resource_name)\n",
" print(\"Deleted endpoint:\", endpoint)\n",
"except Exception as e:\n",
" print(e)\n",
"\n",
"if delete_dataset and \"DISPLAY_NAME\" in globals():\n",
" if \"tabular\" == \"tabular\":\n",
" try:\n",
" datasets = aip.TabularDataset.list(\n",
" filter=f\"display_name={DISPLAY_NAME}\", order_by=\"create_time\"\n",
" )\n",
" dataset = datasets[0]\n",
" aip.TabularDataset.delete(dataset.resource_name)\n",
" print(\"Deleted dataset:\", dataset)\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" if \"tabular\" == \"image\":\n",
" try:\n",
" datasets = aip.ImageDataset.list(\n",
" filter=f\"display_name={DISPLAY_NAME}\", order_by=\"create_time\"\n",
" )\n",
" dataset = datasets[0]\n",
" aip.ImageDataset.delete(dataset.resource_name)\n",
" print(\"Deleted dataset:\", dataset)\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" if \"tabular\" == \"text\":\n",
" try:\n",
" datasets = aip.TextDataset.list(\n",
" filter=f\"display_name={DISPLAY_NAME}\", order_by=\"create_time\"\n",
" )\n",
" dataset = datasets[0]\n",
" aip.TextDataset.delete(dataset.resource_name)\n",
" print(\"Deleted dataset:\", dataset)\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" if \"tabular\" == \"video\":\n",
" try:\n",
" datasets = aip.VideoDataset.list(\n",
" filter=f\"display_name={DISPLAY_NAME}\", order_by=\"create_time\"\n",
" )\n",
" dataset = datasets[0]\n",
" aip.VideoDataset.delete(dataset.resource_name)\n",
" print(\"Deleted dataset:\", dataset)\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
"try:\n",
" if delete_pipeline and \"DISPLAY_NAME\" in globals():\n",
" pipelines = aip.PipelineJob.list(\n",
" filter=f\"display_name={DISPLAY_NAME}\", order_by=\"create_time\"\n",
" )\n",
" pipeline = pipelines[0]\n",
" aip.PipelineJob.delete(pipeline.resource_name)\n",
" print(\"Deleted pipeline:\", pipeline)\n",
"except Exception as e:\n",
" print(e)\n",
"delete_bucket = False\n",
"\n",
"job.delete()\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
File diff suppressed because one or more lines are too long
@@ -1,14 +1,5 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "c4b363e1330b"
},
"source": [
"# Build a fraud detection model on Vertex AI"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -38,6 +29,8 @@
"id": "05c670d35496"
},
"source": [
"# Build a fraud detection model on Vertex AI\n",
"\n",
"<table align=\"left\">\n",
"\n",
" <td>\n",
@@ -60,28 +53,6 @@
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4c5fb7f2090f"
},
"source": [
"## Table of contents\n",
"\n",
"* [Overview](#section-1)\n",
"* [Dataset](#section-2)\n",
"* [Objective](#section-3)\n",
"* [Costs](#section-4)\n",
"* [Analyze the dataset](#section-5)\n",
"* [Fit a random forest model](#section-6)\n",
"* [Analyzing results](#section-7)\n",
"* [Save the model to a Cloud Storagae path](#section-8)\n",
"* [Create a model in Vertex AI](#section-9)\n",
"* [Create an Endpoint](#section-10) \n",
"* [What-If Tool ](#section-11)\n",
"* [Clean up](#section-12)"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -94,19 +65,6 @@
"This tutorial shows you how to build, deploy, and analyze predictions from a simple [random forest](https://en.wikipedia.org/wiki/Random_forest) model using tools like scikit-learn, Vertex AI, and the [What-IF Tool (WIT)](https://cloud.google.com/ai-platform/prediction/docs/using-what-if-tool) on a synthetic fraud transaction dataset to solve a financial fraud detection problem.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9625185ccee9"
},
"source": [
"## Dataset\n",
"<a name=\"section-2\"></a>\n",
"\n",
"\n",
"The dataset used in this tutorial is publicly available at Kaggle. See [Synthetic Financial Datasets For Fraud Detection](https://www.kaggle.com/ealaxi/paysim1)."
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -118,6 +76,13 @@
"\n",
"This tutorial demonstrates data analysis and model-building using a synthetic financial dataset. The model is trained on identifying fraudulent cases among the transactions. Then, the trained model is deployed on a Vertex AI Endpoint and analyzed using the What-If Tool. The steps taken in this tutorial are as follows: \n",
"\n",
"This tutorial uses the following Google Cloud ML services and resources:\n",
"\n",
"- Vertex AI Model\n",
"- Vertex AI Endpoint\n",
"\n",
"The steps performed include:\n",
"\n",
"- Installation of required libraries\n",
"- Reading the dataset from a Cloud Storage bucket\n",
"- Performing exploratory analysis on the dataset\n",
@@ -129,6 +94,19 @@
"- Un-deploying the model and cleaning up the model resources"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3037523e7523"
},
"source": [
"## Dataset\n",
"<a name=\"section-2\"></a>\n",
"\n",
"\n",
"The dataset used in this tutorial is publicly available at Kaggle. See [Synthetic Financial Datasets For Fraud Detection](https://www.kaggle.com/ealaxi/paysim1)."
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -154,21 +132,15 @@
{
"cell_type": "markdown",
"metadata": {
"id": "1ba37fa1511f"
"id": "cd1bc75a1cb2"
},
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Google Cloud Notebooks**, your environment already meets\n",
"all the requirements to run this notebook. You can skip this step."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cd1bc75a1cb2"
},
"source": [
"all the requirements to run this notebook. You can skip this step.\n",
"\n",
"\n",
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
"You need the following:\n",
"\n",
@@ -211,142 +183,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"id": "172533a994ad"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
"flake8 4.0.1 requires importlib-metadata<4.3; python_version < \"3.8\", but you have importlib-metadata 4.12.0 which is incompatible.\u001b[0m\u001b[31m\n",
"\u001b[0m"
]
}
],
"source": [
"import os\n",
"\n",
"import google.auth\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
"if \"default\" in dir(google.auth):\n",
" USER_FLAG = \"--user\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "a465cf9367de"
},
"source": [
"Install the latest version of the Vertex AI client library.\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"Run the following command in your notebook environment to install the Vertex SDK for Python:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6380f7ee5f54"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade google-cloud-aiplatform"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1969a1cc46cf"
},
"source": [
"Run the following command in your notebook environment to install witwidget:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8b10e59b0911"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} witwidget"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4099ce79705a"
},
"source": [
"Run the following command in your notebook environment to install joblib:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1e56d524753a"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} joblib"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b87ee3041f7d"
},
"source": [
"Run the following command in your notebook environment to install scikit-learn:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "c3ebecd9bd72"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} scikit-learn"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b624b5163531"
},
"source": [
"Run the following command in your notebook environment to install fsspec:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "79c7a64b04de"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} fsspec"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5593090dcf0a"
},
"source": [
"Run the following command in your notebook environment to install gcsfs:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7bf981bc5bf6"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} gcsfs"
"# Install the latest version of the Vertex AI client library.\n",
"! pip3 install --upgrade google-cloud-aiplatform {USER_FLAG} -q\n",
"\n",
"# Install additional libraries\n",
"! pip3 install {USER_FLAG} witwidget -q\n",
"! pip3 install {USER_FLAG} joblib -q\n",
"! pip3 install {USER_FLAG} scikit-learn -q\n",
"! pip3 install {USER_FLAG} fsspec -q\n",
"! pip3 install {USER_FLAG} gcsfs -q"
]
},
{
@@ -378,21 +252,14 @@
" app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2d9b3731b3e0"
},
"source": [
"## Before you begin"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7a5cb1df1ef7"
},
"source": [
"## Before you begin\n",
"\n",
"### Set up your Google Cloud project\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
@@ -426,19 +293,26 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "b27f37ed1ccf"
"id": "dcdfccf50581"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"PROJECT_ID = \"\"\n",
"\n",
"# Get your Google Cloud project ID from gcloud\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null\n",
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5bf9979b96ff"
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID: \", PROJECT_ID)"
" print(\"Project ID:\", PROJECT_ID)"
]
},
{
@@ -450,18 +324,6 @@
"Otherwise, set your project ID here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3dbdf6a5c539"
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -473,15 +335,49 @@
"! gcloud config set project $PROJECT_ID"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "264543a144ad"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Below are regions supported for Vertex AI. It is recommended that you choose the region closest to you.\n",
"\n",
"- Americas: `us-central1`\n",
"- Europe: `europe-west4`\n",
"- Asia Pacific: `asia-east1`\n",
"\n",
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
"\n",
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3281bedf6d3c"
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e663bd062c6f"
},
"source": [
"#### Timestamp\n",
"#### UUID\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
]
},
{
@@ -492,21 +388,16 @@
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import random\n",
"import string\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0c7f603fcdcf"
},
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already\n",
"authenticated. Skip this step."
"# Generate a uuid of length 8\n",
"def generate_uuid():\n",
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=8))\n",
"\n",
"\n",
"UUID = generate_uuid()"
]
},
{
@@ -515,6 +406,11 @@
"id": "72bf8f7c9ab3"
},
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
"authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
"\n",
@@ -547,19 +443,19 @@
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -601,27 +497,25 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f56c52ba662c"
"id": "5e9a782f5608"
},
"outputs": [],
"source": [
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"REGION = \"[your-region]\" # @param {type:\"string\"}"
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "68d1f4908641"
"id": "6d0729c4ae94"
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"-vertex-ai-\" + TIMESTAMP\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
" BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -697,6 +591,7 @@
"import numpy as np\n",
"import pandas as pd\n",
"from google.cloud import aiplatform, storage\n",
"from IPython.display import display\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.metrics import (average_precision_score, classification_report,\n",
" confusion_matrix, f1_score)\n",
@@ -706,6 +601,15 @@
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fdcb614c716f"
},
"source": [
"## Load dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -714,7 +618,6 @@
},
"outputs": [],
"source": [
"# Load dataset\n",
"df = pd.read_csv(\n",
" \"gs://cloud-samples-data/vertex-ai/managed_notebooks/fraud_detection/fraud_detection_data.csv\"\n",
")"
@@ -1031,6 +934,8 @@
"\n",
"# Upload the saved model file to Cloud Storage\n",
"BLOB_PATH = \"[your-blob-path]\"\n",
"if BLOB_PATH == \"[your-blob-path]\":\n",
" BLOB_PATH = \"fraud-detection-model-path\"\n",
"BLOB_NAME = os.path.join(BLOB_PATH, FILE_NAME)\n",
"\n",
"bucket = storage.Client(PROJECT_ID).bucket(BUCKET_NAME)\n",
@@ -1057,6 +962,8 @@
"outputs": [],
"source": [
"MODEL_DISPLAY_NAME = \"[your-model-display-name]\"\n",
"if MODEL_DISPLAY_NAME == \"[your-model-display-name]\":\n",
" MODEL_DISPLAY_NAME = \"fraud-detection-model-display-name\"\n",
"ARTIFACT_GCS_PATH = f\"{BUCKET_URI}/{BLOB_PATH}\"\n",
"SERVING_CONTAINER_IMAGE_URI = (\n",
" \"us-docker.pkg.dev/vertex-ai/prediction/sklearn-cpu.1-0:latest\"\n",
@@ -1105,7 +1012,9 @@
},
"outputs": [],
"source": [
"ENDPOINT_DISPLAY_NAME = \"[your-endpoint-display-name]\""
"ENDPOINT_DISPLAY_NAME = \"[your-endpoint-display-name]\"\n",
"if ENDPOINT_DISPLAY_NAME == \"[your-endpoint-display-name]\":\n",
" ENDPOINT_DISPLAY_NAME = \"fraud-detection-endpoint\""
]
},
{
@@ -1143,6 +1052,8 @@
"outputs": [],
"source": [
"DEPLOYED_MODEL_NAME = \"[your-deployed-model-name]\"\n",
"if DEPLOYED_MODEL_NAME == \"[your-deployed-model-name]\":\n",
" DEPLOYED_MODEL_NAME = \"fraud-detection-deployed-model\"\n",
"MACHINE_TYPE = \"n1-standard-2\""
]
},
@@ -1224,34 +1135,33 @@
},
"outputs": [],
"source": [
"# define target and labels\n",
"TARGET_FEATURE = \"isFraud\"\n",
"LABEL_VOCAB = [\"not-fraud\", \"fraud\"]\n",
"if not IS_COLAB:\n",
" # define target and labels\n",
" TARGET_FEATURE = \"isFraud\"\n",
" LABEL_VOCAB = [\"not-fraud\", \"fraud\"]\n",
"\n",
"# define the function to adjust the predictions\n",
" # define the function to adjust the predictions\n",
"\n",
" def adjust_prediction(pred):\n",
" return [1 - pred, pred]\n",
"\n",
"def adjust_prediction(pred):\n",
" return [1 - pred, pred]\n",
"\n",
"\n",
"# Combine the features and labels into one array for the What-If Tool\n",
"test_examples = np.hstack(\n",
" (test_samples_X.to_numpy(), test_samples_y.to_numpy().reshape(-1, 1))\n",
")\n",
"\n",
"# Configure the WIT to run on the locally trained model\n",
"config_builder = (\n",
" WitConfigBuilder(\n",
" test_examples.tolist(), test_samples_X.columns.tolist() + [\"isFraud\"]\n",
" # Combine the features and labels into one array for the What-If Tool\n",
" test_examples = np.hstack(\n",
" (test_samples_X.to_numpy(), test_samples_y.to_numpy().reshape(-1, 1))\n",
" )\n",
" .set_custom_predict_fn(forest.predict_proba)\n",
" .set_target_feature(TARGET_FEATURE)\n",
" .set_label_vocab(LABEL_VOCAB)\n",
")\n",
"\n",
"# display the WIT widget\n",
"WitWidget(config_builder, height=600)"
" # Configure the WIT to run on the locally trained model\n",
" config_builder = (\n",
" WitConfigBuilder(\n",
" test_examples.tolist(), test_samples_X.columns.tolist() + [\"isFraud\"]\n",
" )\n",
" .set_custom_predict_fn(forest.predict_proba)\n",
" .set_target_feature(TARGET_FEATURE)\n",
" .set_label_vocab(LABEL_VOCAB)\n",
" )\n",
"\n",
" # display the WIT widget\n",
" display(WitWidget(config_builder, height=600))"
]
},
{
@@ -1271,36 +1181,35 @@
},
"outputs": [],
"source": [
"# configure the target and class-labels\n",
"TARGET_FEATURE = \"isFraud\"\n",
"LABEL_VOCAB = [\"not-fraud\", \"fraud\"]\n",
"if not IS_COLAB:\n",
" # configure the target and class-labels\n",
" TARGET_FEATURE = \"isFraud\"\n",
" LABEL_VOCAB = [\"not-fraud\", \"fraud\"]\n",
"\n",
"# function to return predictions from the deployed Model\n",
" # function to return predictions from the deployed Model\n",
"\n",
" def endpoint_predict_sample(instances: list):\n",
" prediction = endpoint.predict(instances=instances)\n",
" preds = [[1 - i, i] for i in prediction.predictions]\n",
" return preds\n",
"\n",
"def endpoint_predict_sample(instances: list):\n",
" prediction = endpoint.predict(instances=instances)\n",
" preds = [[1 - i, i] for i in prediction.predictions]\n",
" return preds\n",
"\n",
"\n",
"# Combine the features and labels into one array for the What-If Tool\n",
"test_examples = np.hstack(\n",
" (test_samples_X.to_numpy(), test_samples_y.to_numpy().reshape(-1, 1))\n",
")\n",
"\n",
"# Configure the WIT with the prediction function\n",
"config_builder = (\n",
" WitConfigBuilder(\n",
" test_examples.tolist(), test_samples_X.columns.tolist() + [\"isFraud\"]\n",
" # Combine the features and labels into one array for the What-If Tool\n",
" test_examples = np.hstack(\n",
" (test_samples_X.to_numpy(), test_samples_y.to_numpy().reshape(-1, 1))\n",
" )\n",
" .set_custom_predict_fn(endpoint_predict_sample)\n",
" .set_target_feature(TARGET_FEATURE)\n",
" .set_label_vocab(LABEL_VOCAB)\n",
")\n",
"\n",
"# run the WIT-widget\n",
"WitWidget(config_builder, height=400)"
" # Configure the WIT with the prediction function\n",
" config_builder = (\n",
" WitConfigBuilder(\n",
" test_examples.tolist(), test_samples_X.columns.tolist() + [\"isFraud\"]\n",
" )\n",
" .set_custom_predict_fn(endpoint_predict_sample)\n",
" .set_target_feature(TARGET_FEATURE)\n",
" .set_label_vocab(LABEL_VOCAB)\n",
" )\n",
"\n",
" # run the WIT-widget\n",
" display(WitWidget(config_builder, height=400))"
]
},
{