By deploying or using this software you agree to comply with the AI Hub Terms of Service and the Google APIs Terms of Service. To the extent of a direct conflict of terms, the AI Hub Terms of Service will control.

Overview

This notebook provides an example workflow of using the Factorization Machines ML container for training a classification ML model.

Dataset

The notebook uses the Iris dataset. It consists of 3 different types of irises (Setosa, Versicolour, and Virginica) petal and sepal length, stored in a 150x5 table. The target variable is preprocessed to integer IDs starting from zero.

Objective

The goal of this notebook is to go through a common training workflow:

  • Create a dataset
  • Train an ML model using the AI Platform Training service
  • Monitor the training job with TensorBoard
  • Identify if the model was trained successfully by looking at the generated "Run Report"
  • Deploy the model for serving using the AI Platform Prediction service
  • Use the endpoint for online predictions
  • Interactively inspect the deployed ML model with the What-If Tool

Costs

This tutorial uses billable components of Google Cloud Platform (GCP):

  • Cloud AI Platform
  • Cloud Storage

Learn about Cloud AI Platform pricing and Cloud Storage pricing, and use the Pricing Calculator to generate a cost estimate based on your projected usage.

Set up your local development environment

If you are using Colab or AI Platform Notebooks, your environment already meets all the requirements to run this notebook. You can skip this step.

Otherwise, make sure your environment meets this notebook's requirements. You need the following:

  • The Google Cloud SDK
  • Git
  • Python 3
  • virtualenv
  • Jupyter notebook running in a virtual environment with Python 3

The Google Cloud guide to Setting up a Python development environment and the Jupyter installation guide provide detailed instructions for meeting these requirements. The following steps provide a condensed set of instructions:

  1. Install and initialize the Cloud SDK.

  2. Install Python 3.

  3. Install virtualenv and create a virtual environment that uses Python 3.

  4. Activate that environment and run pip install jupyter in a shell to install Jupyter.

  5. Run jupyter notebook in a shell to launch Jupyter.

  6. Open this notebook in the Jupyter Notebook Dashboard.

Set up your GCP project

The following steps are required, regardless of your notebook environment.

  1. Select or create a GCP project.. When you first create an account, you get a $300 free credit towards your compute/storage costs.

  2. Make sure that billing is enabled for your project.

  3. Enable the AI Platform APIs and Compute Engine APIs.

  4. Enter your project ID in the cell below. Then run the cell to make sure the Cloud SDK uses the right project for all the commands in this notebook.

Note: Jupyter runs lines prefixed with ! as shell commands, and it interpolates Python variables prefixed with $ into these commands.


In [ ]:
PROJECT_ID = "[your-project-id]" #@param {type:"string"}
! gcloud config set project $PROJECT_ID

Authenticate your GCP account

If you are using AI Platform Notebooks, your environment is already authenticated. Skip this step.

If you are using Colab, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.

Otherwise, follow these steps:

  1. In the GCP Console, go to the Create service account key page.

  2. From the Service account drop-down list, select New service account.

  3. In the Service account name field, enter a name.

  4. From the Role drop-down list, select Machine Learning Engine > AI Platform Admin and Storage > Storage Object Admin.

  5. Click Create. A JSON file that contains your key downloads to your local environment.

  6. Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell.


In [ ]:
import sys

# If you are running this notebook in Colab, run this cell and follow the
# instructions to authenticate your GCP account. This provides access to your
# Cloud Storage bucket and lets you submit training jobs and prediction
# requests.

if 'google.colab' in sys.modules:
  from google.colab import auth as google_auth
  google_auth.authenticate_user()

# If you are running this notebook locally, replace the string below with the
# path to your service account key and run this cell to authenticate your GCP
# account.
else:
  %env GOOGLE_APPLICATION_CREDENTIALS ''

Create a Cloud Storage bucket

The following steps are required, regardless of your notebook environment.

You need to have a "workspace" bucket that will hold the dataset and the output from the ML Container. Set the name of your Cloud Storage bucket below. It must be unique across all Cloud Storage buckets.

You may also change the REGION variable, which is used for operations throughout the rest of this notebook. Make sure to choose a region where Cloud AI Platform services are available. You may not use a Multi-Regional Storage bucket for training with AI Platform.


In [ ]:
BUCKET_NAME = "[your-bucket-name]" #@param {type:"string"}
REGION = 'us-central1' #@param {type:"string"}

Only if your bucket doesn't already exist: Run the following cell to create your Cloud Storage bucket.


In [ ]:
! gsutil mb -l $REGION gs://$BUCKET_NAME

Finally, validate access to your Cloud Storage bucket by examining its contents:


In [ ]:
! gsutil ls -al gs://$BUCKET_NAME

PIP Install Packages and dependencies


In [ ]:
! pip install witwidget

Import libraries and define constants


In [ ]:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import time
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from IPython.core.display import HTML
from googleapiclient import discovery

Create a dataset


In [4]:
# load Iris dataset
iris = datasets.load_iris()
names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
data = pd.DataFrame(iris.data, columns=names)

# add target
data['target'] = iris.target

# split
training, validation = train_test_split(data, test_size=50, stratify=data['target'])

# standardization
training_targets = training.pop('target')
validation_targets = validation.pop('target')

data_mean = training.mean(axis=0)
data_std = training.std(axis=0)
training = (training - data_mean) / data_std
training['target'] = training_targets

validation = (validation - data_mean) / data_std
validation['target'] = validation_targets

print('Training data head')
display(training.head())

training_data = os.path.join('gs://', BUCKET_NAME, 'data/train.csv')
validation_data = os.path.join('gs://', BUCKET_NAME, 'data/valid.csv')

print('Copy the data in bucket ...')
with tf.io.gfile.GFile(training_data, 'w') as f:
  training.to_csv(f, index=False)
with tf.io.gfile.GFile(validation_data, 'w') as f:
  validation.to_csv(f, index=False)


Training data head
sepal_length sepal_width petal_length petal_width target
35 -1.015231 0.348345 -1.464187 -1.307227 0
69 -0.304451 -1.255877 0.068696 -0.131898 1
100 0.524793 0.577520 1.260938 1.696391 2
49 -1.015231 0.577520 -1.350640 -1.307227 0
129 1.590963 -0.110004 1.147391 0.521062 2
Copy the data in bucket ...

Model training

Accelerator and distribution support

GPU Multi-GPU Node TPU Workers Parameter Server
Yes Yes No Yes Yes

To have distribution and/or accelerators to your AI Platform training call, use parameters similar to the examples as shown below.

--master-machine-type standard_gpu
    --worker-machine-type standard_gpu \
    --worker-count 2 \
    --parameter-server-machine-type standard \
    --parameter-server-count 1 \

AI Platform training


In [ ]:
output_location = os.path.join('gs://', BUCKET_NAME, 'output')

job_name = "fm_classification_{}".format(time.strftime("%Y%m%d%H%M%S"))
! gcloud ai-platform jobs submit training $job_name \
    --master-image-uri gcr.io/aihub-c2t-containers/kfp-components/oob_algorithm/factorization_machines:latest \
    --region $REGION \
    --scale-tier CUSTOM \
    --master-machine-type standard \
    -- \
    --output-location {output_location} \
    --training-data {training_data} \
    --validation-data {validation_data} \
    --target-column target \
    --data-type csv \
    --training-epochs 100 \
    --number-of-classes 3 \
    --fresh-start True \
    --batch-size 32 \
    --learning-rate .1 \
    --objective classification

Local training snippet

Note that the training can also be done locally with Docker

docker run \
    -v /tmp:/tmp \
    -it gcr.io/aihub-c2t-containers/kfp-components/oob_algorithm/factorization_machines:latest \
    --output-location /tmp/fm_classification \
    --training-data /tmp/iris_train.csv \
    --validation-data /tmp/iris_valid.csv \
    --target-column target \
    --data-type csv \
    --training-epochs 100 \
    --number-of-classes 3 \
    --batch-size 32 \
    --learning-rate .1 \
    --objective classification

Monitor the training with TensorBoard


In [ ]:
try:
  %load_ext tensorboard
  %tensorboard --logdir {output_location}
except:
  ! tensorboard --logdir {output_location}

Inspect the Run Report

The "Run Report" will help you identify if the model was successfully trained.


In [8]:
if not tf.io.gfile.exists(os.path.join(output_location, 'report.html')):
  raise RuntimeError('The file report.html was not found. Did the training job finish?')

with tf.io.gfile.GFile(os.path.join(output_location, 'report.html')) as f:
  display(HTML(f.read()))


temp_input_nb
+ Table of Contents

Runtime arguments

value
training_data gs://aihub-content-test/fm_classification/data/train.csv
output_location gs://aihub-content-test/fm_classification/output
validation_data gs://aihub-content-test/fm_classification/data/valid.csv
data_type csv
objective classification
target_column target
fresh_start True
number_of_components 64
number_of_classes 3
learning_rate 0.1
batch_size 32
training_epochs 100
regularization_rate_linear 0.001
regularization_rate_factors 0.001
remainder None

Tensorboard snippet

To see the training progress, you can need to install the latest tensorboard with the command: pip install -U tensorboard and then run one of the following commands.

Local tensorboard

tensorboard --logdir gs://aihub-content-test/fm_classification/output

Publicly shared tensorboard

tensorboard dev upload --logdir gs://aihub-content-test/fm_classification/output

Datasets

Data reading snippet

import tensorflow as tf
import pandas as pd

sample = pd.DataFrame()
for filename in tf.io.gfile.glob('gs://aihub-content-test/fm_classification/data/valid.csv'):
  with tf.io.gfile.GFile(filename, 'r') as f:
    sample = sample.append(
      pd.read_csv(f, nrows=sample_size-len(sample)))
  if len(sample) >= sample_size:
    break

Training dataset sample

sepal_length sepal_width petal_length petal_width target
26 -1.252158 -0.110004 -1.350640 -1.176635 0
86 -1.726012 -0.110004 -1.407414 -1.307227 0
2 -1.015231 0.806695 -1.237093 -1.046043 0
55 2.183280 1.723393 1.658352 1.304615 2
75 0.287866 -1.026702 1.033844 0.259878 2

Validation dataset sample

sepal_length sepal_width petal_length petal_width target
28 0.643256 -0.339178 0.295790 0.129286 1
11 0.287866 -0.110004 0.636430 0.782247 2
10 -0.067524 -0.797528 0.749977 0.912839 2
41 -0.422914 -1.255877 0.125469 0.129286 1
2 -1.489085 0.119171 -1.293867 -1.307227 0

Data summary

Dataset inspection

You can use AI Platform to create a detailed inspection report for your dataset with the following console snippet:

DATA=gs://aihub-content-test/fm_classification/data/valid.csv
#DATA=gs://aihub-content-test/fm_classification/data/train.csv
OUTPUT_LOCATION=gs://aihub-content-test/fm_classification/output
# can be one of: tfrecord, parquet, avro, csv, json, bigquery
DATA_TYPE=csv
MAX_SAMPLE_SIZE=10000
JOB_NAME=tabular_data_inspection_$(date '+%Y%m%d_%H%M%S')

gcloud ai-platform jobs submit training $JOB_NAME \
  --stream-logs \
  --master-image-uri gcr.io/kf-pipeline-contrib/kfp-components/oob_algorithm/tabular_data_inspection:latest \
  -- \
  --output-location $OUTPUT_LOCATION \
  --data $DATA \
  --data-type $DATA_TYPE \
  --max-sample-size $MAX_SAMPLE_SIZE

Predictions

Local predictions snippet

import tensorflow as tf

# The input data should have format: {f1: [[1],[2]], f2: [[4,2],[3,1], ...]}
saved_model = 'gs://aihub-content-test/fm_classification/output/export/1582057072'
predict_fn = tf.contrib.predictor.from_saved_model(saved_model)
predictions = predict_fn(estimator_input)

Deploy for serving snippet

MODEL_NAME='REPLACE_WITH_YOUR_MODEL_NAME'
MODEL_VERSION='v1'

# create model name
gcloud ai-platform models create $MODEL_NAME

# create version name
gcloud ai-platform versions create $MODEL_VERSION \
  --model $MODEL_NAME \
  --origin gs://aihub-content-test/fm_classification/output/export/1582057072 \
  --runtime-version=1.15 \
  --framework=tensorflow \
  --python-version=3.7

Training predictions sample

score
0 1 2
26 6.0450 -5.3333 1.5618
86 7.6974 -6.2201 2.2941
2 9.4621 -6.5352 3.9219
55 -7.6889 -0.7516 2.0315
75 -1.2703 3.1036 2.0350

Validation predictions sample

score
0 1 2
28 -0.8107 1.3617 0.4299
11 -1.2635 0.5708 1.8709
10 -1.4642 0.6413 2.9074
41 -2.1767 1.6344 -0.3777
2 8.2059 -6.2481 2.6975

Classification metrics

Training dataset

Confusion matrix

Count
Predicted
0 1 2
Actual 0 33 0 0
1 0 27 7
2 0 2 31
Relative
Predicted
0 1 2
Actual 0 1 0 0
1 0 0.8 0.2
2 0 0.06 0.9
Aggregated metrics
accuracy f1-score precision recall
weighted value 0.91 0.9096 0.9158 0.91

Classification metrics

Per class metrics
precision recall f1-score support
Label 0 1.0000 1.0000 1.0000 33
1 0.9310 0.7941 0.8571 34
2 0.8158 0.9394 0.8732 33

Validation dataset

Confusion matrix

Count
Predicted
0 1 2
Actual 0 16 1 0
1 0 12 4
2 0 1 16
Relative
Predicted
0 1 2
Actual 0 0.9 0.06 0
1 0 0.8 0.2
2 0 0.06 0.9
Aggregated metrics
accuracy f1-score precision recall
weighted value 0.88 0.8798 0.8863 0.88

Classification metrics

Per class metrics
precision recall f1-score support
Label 0 1.0000 0.9412 0.9697 17
1 0.8571 0.7500 0.8000 16
2 0.8000 0.9412 0.8649 17

Classification plots

ROC Curve

Training ROC Curve

0 1 2 Mean AUC
Area Under Curve 1.0 0.9844 0.9873 0.9906

Validation ROC Curve

0 1 2 Mean AUC
Area Under Curve 1.0 0.9651 0.9804 0.9818

Prediction tables

Training data and prediction

Best predictions

target predicted-target log_loss sepal_length sepal_width petal_length petal_width
row
94 0 0 0.0001797 -1.489 1.265 -1.578 -1.307
30 0 0 0.0002885 -0.7783 2.411 -1.294 -1.438
74 0 0 0.0005877 -0.8968 1.723 -1.237 -1.307
90 0 0 0.0008157 -0.8968 1.723 -1.067 -1.046
34 0 0 0.0008541 -1.015 1.265 -1.351 -1.307
92 0 0 0.0009084 -1.726 0.3483 -1.407 -1.307
64 0 0 0.001024 -1.252 0.8067 -1.237 -1.307
84 0 0 0.001052 -1.252 0.8067 -1.067 -1.307
60 1 1 0.001284 -1.015 -2.402 -0.1584 -0.2625
23 0 0 0.001419 -0.4229 2.64 -1.351 -1.307
3 0 0 0.001544 -1.489 0.3483 -1.351 -1.307
19 0 0 0.001924 -1.015 0.8067 -1.294 -1.307
42 0 0 0.001956 -1.371 0.3483 -1.407 -1.307
72 0 0 0.001994 -0.6598 1.494 -1.294 -1.307
8 0 0 0.002004 -1.844 -0.11 -1.521 -1.438
40 0 0 0.002065 -1.371 0.3483 -1.237 -1.307
7 0 0 0.00225 -0.5414 1.953 -1.18 -1.046
61 0 0 0.00227 -0.5414 1.953 -1.407 -1.046
45 2 2 0.002364 2.183 -0.11 1.318 1.435
18 0 0 0.002371 -1.015 1.036 -1.237 -0.7849
2 0 0 0.002612 -1.015 0.8067 -1.237 -1.046
48 0 0 0.002857 -1.015 0.5775 -1.351 -1.307
91 1 1 0.002939 0.1694 -1.943 0.1255 -0.2625
56 2 2 0.002947 2.065 -0.11 1.602 1.174
86 0 0 0.002995 -1.726 -0.11 -1.407 -1.307
73 0 0 0.004148 -1.015 0.3483 -1.464 -1.307
12 2 2 0.004183 2.183 -0.5684 1.658 1.043
98 0 0 0.004277 -1.252 0.1192 -1.237 -1.307
6 0 0 0.004543 -1.134 0.1192 -1.294 -1.438
21 0 0 0.00572 -1.252 -0.11 -1.351 -1.438

Worst predictions

target predicted-target log_loss sepal_length sepal_width petal_length petal_width
row
47 1 2 0.9412 0.05094 0.3483 0.5797 0.7822
75 2 1 0.9066 0.2879 -1.027 1.034 0.2599
53 1 2 0.6465 -0.5414 -0.11 0.4093 0.3905
59 1 2 0.6111 -0.3045 -0.11 0.4093 0.3905
14 1 2 0.608 0.1694 -0.7975 0.75 0.5211
80 1 2 0.6064 0.6433 0.3483 0.4093 0.3905
43 1 2 0.5462 0.05094 -0.11 0.239 0.3905
46 2 1 0.5114 0.5248 -0.5684 0.75 0.3905
36 1 2 0.5025 1.236 0.1192 0.6364 0.3905
69 1 1 0.4927 -0.186 -0.11 0.239 -0.001306
70 1 1 0.4701 0.1694 -0.3392 0.4093 0.3905
16 1 1 0.4373 1.354 0.3483 0.5229 0.2599
33 1 1 0.4168 0.9986 0.1192 0.3526 0.2599
29 1 1 0.3809 -0.186 -0.3392 0.239 0.1293
11 1 1 0.3703 0.7617 -0.5684 0.4661 0.3905
65 1 1 0.3662 0.8802 -0.11 0.3526 0.2599
31 1 1 0.3651 0.2879 -0.3392 0.5229 0.2599
50 2 2 0.3385 -1.134 -1.256 0.4093 0.6517
66 1 1 0.3065 -0.7783 -0.7975 0.0687 0.2599
67 1 1 0.2912 0.4063 -0.3392 0.2958 0.1293
37 1 1 0.2812 -0.186 -0.5684 0.1822 0.1293
58 1 1 0.2736 0.5248 -1.256 0.6364 0.3905
51 1 1 0.2289 0.8802 -0.3392 0.4661 0.1293
79 2 2 0.2234 1.591 -0.11 1.147 0.5211
78 2 2 0.1948 0.1694 -0.11 0.5797 0.7822
17 2 2 0.1802 0.4063 -0.5684 0.5797 0.7822
22 1 1 0.1426 0.4063 -1.943 0.4093 0.3905
71 1 1 0.1423 0.2879 -0.5684 0.5229 -0.001306
62 1 1 0.09275 -0.06752 -1.027 0.1255 -0.001306
85 2 2 0.08696 0.5248 -0.3392 1.034 0.7822

Validation data and prediction

Best predictions

target predicted-target log_loss sepal_length sepal_width petal_length petal_width
row
34 0 0 0.0004997 -1.134 1.265 -1.351 -1.438
0 0 0 0.0006254 -1.489 0.8067 -1.351 -1.177
46 0 0 0.0006889 -0.8968 1.723 -1.294 -1.177
43 0 0 0.001153 -0.8968 1.494 -1.294 -1.046
36 2 2 0.001348 2.183 -1.027 1.772 1.435
42 0 0 0.001451 -1.015 1.036 -1.407 -1.177
27 0 0 0.00182 -0.8968 1.036 -1.351 -1.307
44 0 0 0.002071 -0.8968 1.036 -1.351 -1.177
1 0 0 0.002592 -0.7783 1.036 -1.294 -1.307
33 0 0 0.002647 -0.8968 0.8067 -1.294 -1.307
2 0 0 0.002696 -1.489 0.1192 -1.294 -1.307
21 0 0 0.003029 -0.5414 1.494 -1.294 -1.307
23 0 0 0.003626 -0.7783 0.8067 -1.351 -1.307
9 0 0 0.00397 -0.186 3.098 -1.294 -1.046
16 0 0 0.005219 -1.134 0.1192 -1.294 -1.307
15 0 0 0.005621 -1.726 -0.3392 -1.351 -1.307
7 2 2 0.007063 -0.06752 -0.5684 0.75 1.566
40 2 2 0.007166 0.9986 0.1192 1.034 1.566
3 2 2 0.007175 0.6433 -0.5684 1.034 1.305
30 0 0 0.007902 -1.134 -0.11 -1.351 -1.307
39 1 1 0.01698 -0.4229 -1.485 -0.04485 -0.2625
13 2 2 0.03608 1.709 -0.3392 1.431 0.7822
5 2 2 0.03671 0.5248 0.8067 1.034 1.566
47 2 2 0.04863 0.9986 -1.256 1.147 0.7822
12 2 2 0.06746 0.4063 0.8067 0.9203 1.435
10 2 2 0.07298 -0.06752 -0.7975 0.75 0.9128

Worst predictions

target predicted-target log_loss sepal_length sepal_width petal_length petal_width
row
22 2 1 0.9995 0.1694 -1.943 0.6932 0.3905
25 1 2 0.8388 0.1694 0.8067 0.4093 0.5211
35 1 2 0.8249 0.9986 -0.11 0.6932 0.6517
14 1 2 0.7499 0.5248 0.5775 0.5229 0.5211
24 0 1 0.7078 -1.608 -1.714 -1.407 -1.177
8 1 1 0.561 -0.3045 -0.11 0.1822 0.1293
17 1 2 0.5081 0.9986 0.1192 0.5229 0.3905
49 1 1 0.5008 -0.3045 -0.3392 -0.1016 0.1293
45 1 1 0.4496 0.2879 -0.11 0.4661 0.2599
28 1 1 0.264 0.6433 -0.3392 0.2958 0.1293
26 1 1 0.2485 -0.186 -0.5684 0.4093 0.1293
29 1 1 0.2465 1.117 -0.5684 0.5797 0.2599
48 1 1 0.243 0.2879 -0.5684 0.1255 0.1293
4 1 1 0.1959 -0.3045 -0.7975 0.239 0.1293
11 2 2 0.1801 0.2879 -0.11 0.6364 0.7822
31 1 1 0.1581 -0.06752 -0.7975 0.0687 -0.001306
32 2 2 0.156 0.5248 -0.7975 0.6364 0.7822
37 2 2 0.1486 0.05094 -0.11 0.75 0.7822
20 1 1 0.1123 -0.8968 -1.256 -0.4423 -0.1319
6 2 2 0.1118 0.6433 0.1192 0.9771 0.7822
38 2 2 0.1076 0.7617 0.3483 0.75 1.043
18 2 2 0.1037 0.7617 -0.11 0.9771 0.7822
19 2 2 0.1008 2.42 1.723 1.488 1.043
41 1 1 0.09569 -0.4229 -1.256 0.1255 0.1293
10 2 2 0.07298 -0.06752 -0.7975 0.75 0.9128
12 2 2 0.06746 0.4063 0.8067 0.9203 1.435

Deployment parameters


In [ ]:
#@markdown ---
model = 'fm_iris' #@param {type:"string"}
version = 'v1' #@param {type:"string"}
#@markdown ---

In [ ]:
# the exact location of the model is in model_uri.txt
with tf.io.gfile.GFile(os.path.join(output_location, 'model_uri.txt')) as f:
  model_uri = f.read()

# create a model
! gcloud ai-platform models create $model --regions $REGION

# create a version
! gcloud ai-platform versions create $version \
  --model $model \
  --runtime-version 1.15 \
  --origin $model_uri \
  --project $PROJECT_ID

Use the endpoint for online predictions


In [11]:
validation_targets = validation['target']

# format the data for serving
instances = validation.drop(columns='target').to_dict(orient='records')
display(instances[:2])

# make a REST call for online inference
service = discovery.build('ml', 'v1')
name = 'projects/{project}/models/{model}/versions/{version}'.format(project=PROJECT_ID,
                                                                    model=model,
                                                                    version=version)
body = {'instances': instances}

response = service.projects().predict(name=name, body=body).execute()
if 'error' in response:
    raise RuntimeError(response['error'])

class_probabilties = [row['score'] for row in response['predictions']]
predicted_classes = np.array(class_probabilties).argmax(axis=1)
accuracy = (predicted_classes == validation_targets).mean()
print('Accuracy of the predictions: {}'.format(accuracy))


[{'sepal_length': 0.9986464102176773,
  'sepal_width': 0.1191708046136686,
  'petal_length': 1.0338444052852982,
  'petal_width': 1.5657991119978514},
 {'sepal_length': 0.05093925935867274,
  'sepal_width': -0.11000381964338443,
  'petal_length': 0.7499771880186045,
  'petal_width': 0.7822465955685677}]
/Users/evo/Library/Python/3.7/lib/python/site-packages/google/auth/_default.py:66: UserWarning: Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/
  warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
Accuracy of the predictions: 0.88

Interactively inspect the ML model


In [ ]:
import witwidget
from witwidget.notebook.visualization import WitWidget, WitConfigBuilder

config_builder = WitConfigBuilder(examples=validation.to_dict(orient='records'))
config_builder.set_ai_platform_model(project=PROJECT_ID,
                                     model=model,
                                     version=version)
config_builder.set_label_vocab(iris.target_names.tolist())
config_builder.set_model_type('classification')
config_builder.set_target_feature('target')
config_builder.set_predict_output_tensor('score')
WitWidget(config_builder)

Cleaning up

To clean up all GCP resources used in this project, you can delete the GCP project you used for the tutorial.


In [ ]:
# Delete model version resource
! gcloud ai-platform versions delete $version --quiet --model $model 

# Delete model resource
! gcloud ai-platform models delete $model --quiet

# If training job is still running, cancel it
! gcloud ai-platform jobs cancel $job_name --quiet

# Delete Cloud Storage objects that were created
! gsutil -m rm -r $BUCKET_NAME