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 K-Nearest Neighbors 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
  • Identify if the model was trained successfully by looking at the generated "Run Report"

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

Import libraries and define constants


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

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

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
87 0.579039 -1.684739 0.373408 0.150740 1
4 -0.985932 1.274938 -1.311138 -1.316643 0
96 -0.143255 -0.318734 0.261105 0.150740 1
84 -0.504402 -0.091067 0.429559 0.417537 1
120 1.301334 0.364268 1.103377 1.484725 2
Copy the data in bucket ...

Cloud training

Accelerator and distribution support

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

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

AI Platform training

Local Training


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

job_name = "knn_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/knn: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 \
    --k-neighbors 5 \
    --fresh-start True \
    --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/knn:latest \
    --output-location /tmp/knn_classification \
    --training-data /tmp/iris_train.csv \
    --validation-data /tmp/iris_valid.csv \
    --target-column target \
    --data-type csv \
    --k-neighbors 5 \
    --objective classification

Inspect the Run Report

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


In [6]:
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
k_neighbors 5
target_column target
objective classification
fresh_start True
training_data gs://aihub-content-test/knn_classification/data/train.csv
validation_data gs://aihub-content-test/knn_classification/data/valid.csv
testing_data None
output_location gs://aihub-content-test/knn_classification/output
data_type csv
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/knn_classification/output

Publicly shared tensorboard

tensorboard dev upload --logdir gs://aihub-content-test/knn_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/knn_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 preview

sepal_length sepal_width petal_length petal_width target
0 -1.1063 -0.0911 -1.3111 -1.3166 0
1 0.5790 -0.3187 1.0472 0.8177 2
... ... ... ... ... ...
98 -0.0229 -0.7741 0.7665 0.9511 2
99 -1.2267 0.8196 -1.1988 -1.3166 0

100 rows × 5 columns

Validation dataset preview

sepal_length sepal_width petal_length petal_width target
0 -0.0229 -0.7741 0.7665 0.9511 2
1 -1.1063 0.1366 -1.2550 -1.3166 0
... ... ... ... ... ...
48 -1.2267 -0.0911 -1.3111 -1.4500 0
49 0.6994 0.3643 0.8788 1.4847 2

50 rows × 5 columns

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/knn_classification/data/valid.csv
#DATA=gs://aihub-content-test/knn_classification/data/train.csv
OUTPUT_LOCATION=gs://aihub-content-test/knn_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/knn_classification/output'
predict_fn = tf.contrib.predictor.from_saved_model(saved_model)
predictions = predict_fn(estimator_input)

Local predictions snippet

import faiss
import scipy
import numpy as np
import tensorflow as tf


objective = 'classification' # or 'regression'

# copy the model files to local dir
tf.io.gfile.copy('gs://aihub-content-test/knn_classification/output/model.faiss'), '/tmp/model.faiss')
tf.io.gfile.copy('gs://aihub-content-test/knn_classification/output/label.txt'), '/tmp/label.txt')

# load model files
index = faiss.read_index('/tmp/model.faiss')
train_labels = np.loadtxt('/tmp/label.txt')

# create predictions
features = np.ascontiguousarray(data.values.astype(np.float32))
distance, features_index = index.search(features, k=5)

if objective == 'classification'
  y_pred = scipy.stats.mode(train_labels[features_index], axis=1)[0].ravel()
else:
  assert objective == 'regression'
  y_pred = train_labels[features_index].mean(axis=1)

Sample of the resulting predictions

Training predictions sample

26    0
86    1
2     0
55    1
75    1
dtype: int32

Validation predictions sample

28    1
11    1
10    0
41    0
2     1
dtype: int32

Training dataset

Confusion matrix

Count
Predicted
0 1 2
Actual 0 34 0 0
1 0 33 0
2 0 2 31
Relative
Predicted
0 1 2
Actual 0 1 0 0
1 0 1 0
2 0 0.06 0.9
Aggregated metrics
accuracy f1-score precision recall
weighted value 0.98 0.98 0.9811 0.98

Classification metrics

Per class metrics
precision recall f1-score support
Label 0 1.0000 1.0000 1.0000 34
1 0.9429 1.0000 0.9706 33
2 1.0000 0.9394 0.9688 33

Validation dataset

Confusion matrix

Count
Predicted
0 1 2
Actual 0 16 0 0
1 0 16 1
2 0 0 17
Relative
Predicted
0 1 2
Actual 0 1 0 0
1 0 0.9 0.06
2 0 0 1
Aggregated metrics
accuracy f1-score precision recall
weighted value 0.98 0.98 0.9811 0.98

Classification metrics

Per class metrics
precision recall f1-score support
Label 0 1.0000 1.0000 1.0000 16
1 1.0000 0.9412 0.9697 17
2 0.9444 1.0000 0.9714 17

Training data and prediction

Best predictions

target predicted-target log_loss sepal_length sepal_width petal_length petal_width
row
0 0 0 -1e-09 -1.106 -0.09107 -1.311 -1.317
71 1 1 -1e-09 1.061 0.1366 0.5419 0.4175
70 1 1 -1e-09 -0.384 -1.002 0.3734 0.01734
69 1 1 -1e-09 0.2179 -0.3187 0.4296 0.4175
68 2 2 -1e-09 0.6994 -0.5464 1.047 1.351
67 1 1 -1e-09 -0.1433 -0.5464 0.4296 0.1507
66 1 1 -1e-09 0.6994 -0.3187 0.3173 0.1507
65 2 2 -1e-09 1.061 0.5919 1.103 1.752
64 2 2 -1e-09 0.8198 -0.09107 0.9911 0.8177
63 0 0 -1e-09 -0.6248 1.503 -1.255 -1.317
62 0 0 -1e-09 -0.9859 1.275 -1.311 -1.317
61 0 0 -1e-09 -1.588 -1.685 -1.367 -1.183
60 0 0 -1e-09 -0.8655 1.047 -1.311 -1.183
59 2 2 -1e-09 0.8198 -0.09107 0.8226 1.085
58 2 2 -1e-09 2.264 -0.09107 1.328 1.485
57 0 0 -1e-09 -1.829 -0.09107 -1.48 -1.45
56 1 1 -1e-09 0.6994 0.3643 0.4296 0.4175
55 1 1 -1e-09 0.579 0.5919 0.5419 0.5509
54 2 2 -1e-09 1.061 -1.229 1.16 0.8177
53 2 2 -1e-09 0.6994 -0.5464 1.047 1.218
52 0 0 -1e-09 -1.708 0.3643 -1.367 -1.317
51 0 0 -1e-09 -1.708 -0.09107 -1.367 -1.317
72 1 1 -1e-09 -0.5044 -0.09107 0.4296 0.4175
50 2 2 -1e-09 1.061 0.5919 1.103 1.218
73 2 2 -1e-09 0.2179 -0.09107 0.598 0.8177
75 1 1 -1e-09 -0.384 -1.685 0.1488 0.1507
97 0 0 -1e-09 -1.467 0.3643 -1.311 -1.317
96 2 2 -1e-09 2.264 -1.002 1.777 1.485
95 0 0 -1e-09 -0.1433 1.73 -1.143 -1.183
94 2 2 -1e-09 1.301 0.1366 0.9349 1.218

Worst predictions

target predicted-target log_loss sepal_length sepal_width petal_length petal_width
row
27 2 1 13.82 -1.106 -1.229 0.4296 0.6843
93 2 1 13.82 0.2179 -1.912 0.7103 0.4175
99 0 0 -1e-09 -1.227 0.8196 -1.199 -1.317
36 1 1 -1e-09 0.579 -1.229 0.6542 0.4175
26 0 0 -1e-09 -1.106 1.275 -1.311 -1.45
28 1 1 -1e-09 1.061 -0.09107 0.7103 0.6843
29 1 1 -1e-09 0.3383 -0.5464 0.5419 0.01734
30 1 1 -1e-09 0.9402 -0.3187 0.4857 0.1507
31 0 0 -1e-09 -0.5044 1.958 -1.143 -1.05
32 0 0 -1e-09 -0.7452 0.8196 -1.311 -1.317
33 1 1 -1e-09 -0.02287 -0.7741 0.205 -0.2495
34 2 2 -1e-09 -0.2636 -0.5464 0.6542 1.085
35 2 2 -1e-09 0.8198 0.3643 0.7665 1.085
37 0 0 -1e-09 -1.467 0.8196 -1.311 -1.183
24 2 2 -1e-09 1.301 0.3643 1.103 1.485
38 1 1 -1e-09 -0.7452 -0.7741 0.09265 0.2841
39 2 2 -1e-09 1.662 -0.09107 1.16 0.5509
40 2 2 -1e-09 2.144 -0.09107 1.609 1.218
41 0 0 -1e-09 -1.347 0.3643 -1.367 -1.317
42 0 0 -1e-09 -0.8655 0.8196 -1.255 -1.317
43 0 0 -1e-09 -0.8655 1.73 -1.03 -1.05
44 1 1 -1e-09 0.579 -1.685 0.3734 0.1507
45 2 2 -1e-09 1.061 0.1366 1.047 1.618
46 2 2 -1e-09 2.264 1.73 1.665 1.351
25 1 1 -1e-09 -0.02287 -0.7741 0.09265 0.01734
23 2 2 -1e-09 2.264 -0.5464 1.665 1.085
48 1 1 -1e-09 -0.384 -1.457 -0.01965 -0.2495
22 2 2 -1e-09 1.662 0.3643 1.272 0.8177
1 2 2 -1e-09 0.579 -0.3187 1.047 0.8177
2 0 0 -1e-09 -0.02287 2.186 -1.423 -1.317

Validation data and prediction

Best predictions

target predicted-target log_loss sepal_length sepal_width petal_length petal_width
row
0 2 2 -1e-09 -0.02287 -0.7741 0.7665 0.9511
27 2 2 -1e-09 1.903 -0.5464 1.328 0.9511
28 1 1 -1e-09 -0.2636 -0.3187 -0.0758 0.1507
29 2 2 -1e-09 1.181 -0.09107 0.9911 1.218
30 0 0 -1e-09 -0.1433 3.096 -1.255 -1.05
31 1 1 -1e-09 1.061 0.1366 0.3734 0.2841
32 2 2 -1e-09 0.8198 -0.09107 1.16 1.351
33 0 0 -1e-09 -0.8655 1.047 -1.311 -1.317
34 1 1 -1e-09 0.3383 -0.5464 0.1488 0.1507
35 2 2 -1e-09 1.301 0.1366 0.7665 1.485
36 2 2 -1e-09 -0.02287 -0.5464 0.7665 1.618
37 1 1 -1e-09 0.4587 -0.3187 0.3173 0.1507
38 2 2 -1e-09 1.061 -0.09107 0.8226 1.485
39 0 0 -1e-09 -1.467 0.1366 -1.255 -1.317
40 2 2 -1e-09 2.505 1.73 1.496 1.085
41 0 0 -1e-09 -0.9859 0.8196 -1.255 -1.317
42 1 1 -1e-09 -0.1433 -0.5464 0.205 0.1507
43 1 1 -1e-09 0.09751 0.3643 0.598 0.8177
44 1 1 -1e-09 0.3383 -0.3187 0.5419 0.2841
45 0 0 -1e-09 -0.384 1.047 -1.367 -1.317
46 2 2 -1e-09 0.579 -0.5464 0.7665 0.4175
47 2 2 -1e-09 0.6994 0.1366 0.9911 0.8177
26 1 1 -1e-09 -0.384 -1.457 0.0365 -0.1161
25 2 2 -1e-09 0.579 0.8196 1.047 1.618
24 0 0 -1e-09 -1.106 0.1366 -1.255 -1.45
23 1 1 -1e-09 0.09751 -0.09107 0.2611 0.4175

Worst predictions

target predicted-target log_loss sepal_length sepal_width petal_length petal_width
row
6 1 2 13.82 0.2179 -0.7741 0.7665 0.5509
49 2 2 -1e-09 0.6994 0.3643 0.8788 1.485
12 0 0 -1e-09 -1.227 0.8196 -1.03 -1.317
22 2 2 -1e-09 0.3383 -1.002 1.047 0.2841
21 2 2 -1e-09 1.783 -0.3187 1.44 0.8177
20 0 0 -1e-09 -0.7452 2.413 -1.255 -1.45
19 1 1 -1e-09 0.2179 -1.912 0.1488 -0.2495
18 1 1 -1e-09 1.301 0.1366 0.6542 0.4175
17 2 2 -1e-09 0.3383 -0.09107 0.6542 0.8177
16 1 1 -1e-09 -0.8655 -1.229 -0.4127 -0.1161
15 2 2 -1e-09 0.4587 0.8196 0.9349 1.485
14 0 0 -1e-09 -1.347 0.3643 -1.199 -1.317
13 1 1 -1e-09 1.422 0.3643 0.5419 0.2841
11 1 1 -1e-09 -0.384 -1.229 0.1488 0.1507
48 0 0 -1e-09 -1.227 -0.09107 -1.311 -1.45
10 0 0 -1e-09 -1.227 -0.09107 -1.311 -1.183
9 0 0 -1e-09 -0.9859 0.5919 -1.311 -1.317
8 0 0 -1e-09 -0.9859 1.047 -1.367 -1.183
7 0 0 -1e-09 -1.708 -0.3187 -1.311 -1.317
5 2 2 -1e-09 1.662 1.275 1.328 1.752
4 1 1 -1e-09 -0.2636 -0.7741 0.2611 0.1507
3 0 0 -1e-09 -0.384 2.641 -1.311 -1.317
2 1 1 -1e-09 1.181 -0.5464 0.598 0.2841
1 0 0 -1e-09 -1.106 0.1366 -1.255 -1.317
23 1 1 -1e-09 0.09751 -0.09107 0.2611 0.4175
24 0 0 -1e-09 -1.106 0.1366 -1.255 -1.45

Cleaning up

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


In [ ]:
# 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