PRIYANKA VERGADIA: In the last episode,
we learned about TFX, which helps us engineer our machine
learning pipeline so we can deploy
our models in production.
Are you ready to see it all in action?
Let's go.
[MUSIC PLAYING]
Welcome to A.I. Adventures, where
we explore the art, science, and tools of machine learning.
I'm Priyanka Vergadia, and today I'm
going to show you how to manage a production machine learning
pipeline using TFX.
For this, we are going to use the Chicago Taxi
Dataset, available in BitQuery.
And the goal of our model is to predict if customers
tip more or less than 20%.
So it is a binary classification problem.
Here's what we will do.
First, we will set up our environment on Google Cloud.
Then we'll set up and deploy A.I. pipelines on a Kubernetes
cluster using Kubeflow.
Then, we set up Cloud A.I. Platform, Notebook Instance,
and work in the notebook to run the pipeline.
We hop into the Google Cloud console
and create an A.I. platform pipeline cluster.
Configure the Kubeflow pipeline.
Make sure to allow access to the Cloud APIs.
This is an important step for the cluster
to access other pieces of our project.
Once the cluster is created, we deploy the pipeline.
Now we open our pipeline dashboard and the TF notebook.
We create a new notebook instance here.
You can change the machine type here to a faster processing
machine, but remember that there's a cost associated
with your choice.
Now we are in our notebook.
The first step is to install TFX and Kubeflow pipeline packages.
Downloading Scaffold module, we then
set the path to include user Python binary directory,
and a directory containing Scaffold.
Then we check which version of TFX is installed.
And then set the project ID as the environment variable.
Set the Kubeflow cluster endpoint,
and then we set the name for the custom Docker
image of our pipeline.
And now we set the name of our pipeline
and copy the pipeline template into the project directory.
Today, for this example, we're using Chicago Taxi Dataset
to perform binary classification.
So the template sets the model to taxi.
As a result of this command, in the file browser,
we see a basic scaffold of files that
build a pipeline, including pipeline source codes, sample
data, and Jupyter notebooks.
Now, if you're working on a different problem,
these files might look a little bit different.
Here, let's see what these files are.
Pipeline directory contains the definition of the pipeline.
configs.py the common constants for the pipeline runners.
pipeline.py defines the TFX components,
and a pipeline models directory contains the machine
learning model definitions.
features.py and features_test.py defines the met features
of the model.
preprocessing.py and preprocessing_test.py defines
feature engineering using TFX transform.
Estimated directory contains an estimator-based model.
Inside that, constants.py defines constants of the model,
and model.py and model_test.py defines the DNN model using
the TensorFlow estimator.
Keras directory contains a Keras-based model,
which is constants.py file, defines the constants
of the model, and the model.py and model_test.py defines
the DNN model using Keras.
Kubeflow_dag_runner file defines runners for each orchestration
engine.
This example includes both an estimator-based model
and a Keras-based model for reference.
So you should choose whichever one is best for you.
Now, before we run our TFX pipeline on Kubeflow,
we first need somewhere to store the artifacts that
will be created when we run the pipeline.
These artifacts are called payloads,
and we will use Google Cloud Storage to store them.
We copy our sample data to GCS, and then we
create the pipeline.
This will generate a Docker file and build.yaml
to build a Docker image.
Make sure to add these files to your source code system,
for example, Git, along with other source files.
Now we are ready to run our TFX pipeline using
the TFX run create command.
We can also see this run under experiments in the Kubeflow
pipelines dashboard.
Now, the first task in any data science or machine learning
project is to understand and clean the data.
ExampleGen ingests and splits the input data set.
StatisticsGen calculates statistics for the data set.
SchemaGen examines the statistics
and creates a data schema.
Example Validator looks for the anomalies and missing values
inside the data set.
In the pipeline.py file, we will enable these components
and update the pipeline for a rerun.
After the rerun, we can see our pipeline in the visual format.
And after understanding the data and cleaning it,
the next step is to transform to increase the predictive quality
of our data, and reduce the dimensionality and feature
engineering.
After transforming the data, we use the Trainer component
of the TFX to train our model.
Then we would run Evaluator to perform deep analysis
of the training results.
And then we use the Pusher component to deploy the model
to a serving infrastructure.
I enabled all these in the pipeline.py,
and the resulting pipeline, with all the components,
looks like this in the Kubeflow dashboard.
Depending on the requirements of your workload,
your pipeline might need more CPU, GPUs, or memory.
To modify these, I have included the documentation below.
Now that we know how the TFX pipeline works,
you might be wondering how we can directly use
BigQuery as the data source.
There's a component called BigQuery Example
Gen that connects to BigQuery in a specified Google Cloud
project.
TFX inter-operates with several other
managed Google Cloud Services, such as Cloud Dataflow.
Several TFX components use Apache Beam
to implement data parallel pipelines,
and it means that you can distribute data processing
workloads using Cloud Dataflow.
TFX also interoperates with Cloud A.I. Platform
for training and prediction.
You can set your Trainer component
to use Cloud A.I. Platform training, which
is a managed service for training machine learning
models.
And when your model is built and ready to be served,
you can push your model to Cloud A.I. Platform Prediction
for serving.
In this video, we just created, and ran,
a machine learning pipeline, and visualized
the results of each run.
Give TFX a try yourself with a different data
set, and let me know how it goes in the comments below.
If you like this video, and would
like to see more such content, give us a like and subscribe.