Compare commits

...
20 Commits
Author SHA1 Message Date
Andrew FerlitschandGitHub f4cd4a1b3e Merge branch 'main' into ml_ops_6 2022-02-16 17:00:03 -08:00
Andrew Ferlitsch cac8c83edf fix: XAI for sklearn 2022-02-17 00:58:09 +00:00
Andrew Ferlitsch bf6a20922d fix: XAI for sklearn 2022-02-17 00:32:12 +00:00
Andrew FerlitschandGitHub b64ca322cb Merge branch 'main' into ml_ops_6 2022-02-15 11:20:00 -08:00
Andrew Ferlitsch c0a7b2cb89 Merge branch 'ml_ops_6' of https://github.com/GoogleCloudPlatform/vertex-ai-samples into ml_ops_6 2022-02-15 19:19:02 +00:00
Andrew Ferlitsch 68f3811ea2 feat: update to v1 2022-02-15 19:18:15 +00:00
Andrew Ferlitsch cd6625bb67 feat: update to v1 2022-02-15 19:16:25 +00:00
Andrew FerlitschandGitHub 045d80abb5 Merge branch 'main' into ml_ops_6 2022-02-14 13:21:18 -08:00
Andrew Ferlitsch 33946b7b84 Merge branch 'ml_ops_6' of https://github.com/GoogleCloudPlatform/vertex-ai-samples into ml_ops_6 2022-02-14 21:20:21 +00:00
Andrew Ferlitsch 4330e499c7 feat: upgrade FS to SDK 2022-02-14 21:19:25 +00:00
Andrew Ferlitsch c303e2eb65 feat: upgrade FS to SDK 2022-02-14 21:16:00 +00:00
Andrew FerlitschandGitHub 0f5109053d Merge branch 'main' into ml_ops_6 2022-02-10 14:23:12 -08:00
Andrew Ferlitsch 8d2c87f2da Merge branch 'ml_ops_6' of https://github.com/GoogleCloudPlatform/vertex-ai-samples into ml_ops_6 2022-02-10 22:22:00 +00:00
Andrew Ferlitsch a001a3e6c9 feat: add covert component example 2022-02-10 22:21:49 +00:00
Andrew Ferlitsch e41562e591 feat: add covert component example 2022-02-10 22:19:52 +00:00
Andrew FerlitschandGitHub eeb0ba2d8f Merge branch 'main' into ml_ops_6 2022-02-09 10:16:49 -08:00
Andrew Ferlitsch ef1b71a2b4 feat: XAI with sklearn 2022-02-09 18:14:15 +00:00
Andrew Ferlitsch 35e5a8cc69 feat: XAI with sklearn 2022-02-09 18:13:00 +00:00
Andrew Ferlitsch 5862690e1b feat: get started XAI 2022-02-08 22:11:31 +00:00
Andrew Ferlitsch db59f89990 feat: get started XAI 2022-02-08 22:08:52 +00:00
@@ -130,7 +130,9 @@
"- Train an custom XGBoost tabular model.\n",
" - Manually set configuration metadata.\n",
" - Do an online prediction with explanations.\n",
"- Train an custom Scikit-learn tabular model.\n",
"- Train an custom scikit-learn tabular model.\n",
" - Manually set configuration metadata.\n",
" - Do an online prediction with explanations.\n",
"\n",
"Learn more about [Introduction to Vertex AI Explainable AI ](https://cloud.google.com/vertex-ai/docs/explainable-ai/overview)"
]
@@ -4969,7 +4971,7 @@
"id": "xai_intro:sklearn"
},
"source": [
"## Explainable AI for Sciket-learn models\n",
"## Explainable AI for scikit-learn models\n",
"\n",
"To use Vertex Explainable AI with a scikit-learn tabular model, you must configure certain options when you create the `Model` resource that you plan to request explanations from, when you deploy the model, or when you submit a batch explanation job. To configure manually, you do the following:\n",
"\n",
@@ -5039,18 +5041,29 @@
"! touch custom/trainer/__init__.py"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "taskpy_contents:scilearn,census,regressor"
},
"source": [
"#### Task.py contents\n",
"\n",
"In the next cell, you write the contents of the training script task.py."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "taskpy_contents:scilearn,census"
"id": "taskpy_contents:scilearn,census,regressor"
},
"outputs": [],
"source": [
"%%writefile custom/trainer/task.py\n",
"# Single Instance Training for Census Income\n",
"\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.ensemble import RandomForestRegressor\n",
"import joblib\n",
"from sklearn.feature_selection import SelectKBest\n",
"from sklearn.pipeline import FeatureUnion\n",
@@ -5169,16 +5182,16 @@
"# Combine all the features using FeatureUnion\n",
"preprocess = FeatureUnion(categorical_pipelines)\n",
"\n",
"# Create the classifier\n",
"classifier = RandomForestClassifier()\n",
"# Create the regressor\n",
"regressor = RandomForestRegressor()\n",
"\n",
"# Transform the features and fit them to the classifier\n",
"classifier.fit(preprocess.transform(train_features), train_labels)\n",
"regressor.fit(preprocess.transform(train_features), train_labels)\n",
"\n",
"# Create the overall model as a single pipeline\n",
"pipeline = Pipeline([\n",
" ('union', preprocess),\n",
" ('classifier', classifier)\n",
" ('classifier', regressor)\n",
"])\n",
"\n",
"# Split path into bucket and subdirectory\n",
@@ -5514,7 +5527,7 @@
" inputs={\n",
" \"features\": {\"index_feature_mapping\": COLUMNS, \"encoding\": \"BAG_OF_FEATURES\"}\n",
" },\n",
" outputs={\"specifies\": {}},\n",
" outputs={\"income\": {}},\n",
")\n",
"\n",
"MODEL_DIR = MODEL_DIR + \"/model\"\n",
@@ -5640,64 +5653,23 @@
},
"outputs": [],
"source": [
"instance = [\n",
" 25,\n",
"INSTANCE = [\n",
" 31,\n",
" \"Private\",\n",
" 226802,\n",
" \"11th\",\n",
" 7,\n",
" 45781,\n",
" \"Masters\",\n",
" 14,\n",
" \"Never-married\",\n",
" \"Machine-op-inspct\",\n",
" \"Own-child\",\n",
" \"Black\",\n",
" \"Male\",\n",
" \"Prof-specialty\",\n",
" \"Not-in-family\",\n",
" \"White\",\n",
" \"Female\",\n",
" 14084,\n",
" 0,\n",
" 0,\n",
" 40,\n",
" 50,\n",
" \"United-States\",\n",
"]\n",
"instances = [instance]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "predict_request:mbsdk,custom,lbn"
},
"source": [
"### Make the prediction\n",
"\n",
"Now that your `Model` resource is deployed to an `Endpoint` resource, you can do online predictions by sending prediction requests to the `Endpoint` resource.\n",
"\n",
"#### Request\n",
"\n",
"The format of each instance is:\n",
"\n",
" [feature_list]\n",
"\n",
"Since the predict() method can take multiple items (instances), send your single test item as a list of one test item.\n",
"\n",
"#### Response\n",
"\n",
"The response from the predict() call is a Python dictionary with the following entries:\n",
"\n",
"- `ids`: The internal assigned unique identifiers for each prediction request.\n",
"- `predictions`: The predicted confidence, between 0 and 1, per class label.\n",
"- `deployed_model_id`: The Vertex AI identifier for the deployed `Model` resource which did the predictions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "predict_request:mbsdk,custom,lbn"
},
"outputs": [],
"source": [
"instances_list = [INSTANCE]\n",
"\n",
"prediction = endpoint.predict(instances_list)\n",
"print(prediction)"
"instances = [INSTANCE]"
]
},
{
@@ -5760,18 +5732,18 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "examine_feature_attributions:mbsdk,iris"
"id": "examine_feature_attributions:mbsdk,sklearn"
},
"outputs": [],
"source": [
"from tabulate import tabulate\n",
"\n",
"feature_names = [\"sepal_length\", \"sepal_width\", \"petal_length\", \"petal_width\"]\n",
"feature_names = COLUMNS\n",
"attributions = prediction.explanations[0].attributions[0].feature_attributions\n",
"\n",
"rows = []\n",
"for i, val in enumerate(feature_names):\n",
" rows.append([val, INSTANCE[val], attributions[val]])\n",
" rows.append([val, INSTANCE[i], attributions[val]])\n",
"print(tabulate(rows, headers=[\"Feature name\", \"Feature value\", \"Attribution value\"]))"
]
},