PRIYANKA VERGADIA: Hi, I'm Priyanka Vergadia,
and this is AI Simplifed, where we
learn to make our data useful.
In this series, we've been learning
about ingesting data, training models, and getting
predictions.
It would be handy to learn how to automate all of these tasks
by implementing them programmatically.
In this video, we will learn how to do exactly
that with the Vertex AI SDK.
Now, before we dive into the SDK,
let's take a quick look at the options
to interact with Vertex AI.
There is Google Cloud Console.
It is the UI to work with our model assets, the data
sets, the endpoints, and the predictions in Cloud.
Now, so far we've been using the console in this series,
so you must be familiar.
But if not, I've included the links to the previous episode
below.
Now, anything that you can do through the UI,
you can also do programmatically.
And for that, we have the Google Cloud Vertex AI SDK.
And then there are Vertex AI client libraries,
which help us make calls to the Vertex AI API.
Now, the benefits of using client library
is that they are optimized for languages
that you choose to use--
for example, Java, Python, or Node.
Now the question becomes when to use the console
and when to use the SDK.
The UI helps us get familiar with the features and functions
available in Vertex AI.
So for that reason, using the UI is my go-to recommendation
if you are new to machine learning and AI,
or just prefer to do things visually.
The SDK is best if you're looking
to programmatically implement the steps in your ML workflow.
Now, if you are an experienced machine learning engineer
or data scientist, then you would
benefit from the ease of automating your workflow.
If SDK isn't available in the language of your choice,
then you can use the client libraries or the REST API.
Now that we know the different ways to interact with Vertex
AI, we need a fun example to play with and create
a model using the SDK.
I've picked out the perfect data set for this--
beans.
Well, who does not like beans?
Being a math nerd, I found out that Pythagoras--
yeah, the one who came up with a squared plus b squared
equals c squared, that Pythagoras--
had an aversion to beans.
Who would have thought, right?
OK, so here's the game plan.
Overall, we will get the data, explore it a little bit,
then create a model and deploy it
to an endpoint from which we can make predictions.
And we will do all of this using the SDK.
We will need the tools to explore the data
and prototype this ML model.
Now for this, we will use notebooks
within Vertex, which is the hosted JupyterLab Notebooks.
They come preinstalled with your favorite machine learning
workflows, such as TensorFlow or PyTorch.
These frameworks are already installed,
and you also have customized hardware,
so you can use the CPUs and the GPUs if you need it.
It also integrates with Git, so we can customize and initialize
a Git repo within the notebook, and it also
integrates with other Google Cloud services.
So we'll put our data into BigQuery,
which is Google Cloud enterprise data warehouse,
and inside the notebook, we will download that data
from BigQuery and then put it into use
to explore that data using pandas DataFrame.
And then the next step would be to create our training
job, creating the Vertex AI model,
deploying it, and then making predictions,
and then finally monitoring that job in the console.
Now, ready to go?
Let's see it.
Now in the Vertex AI console, we can create a hosted Jupyter
Notebook.
And if we click on a New Instance,
we see that there are different types of notebooks
that we can choose from.
They come preinstalled with our favorite machine learning
frameworks, and we can add GPUs if needed, and can also
install additional libraries after the notebook is created.
I've already created a notebook here, so let's get into it.
The first step is to download our data.
I'm using Dry Beans Dataset from UCI, which I have linked below.
This is a tabular data set that was
taken from images for different types of beans,
and different data points were collected on each type of bean.
In manufacturing and other industries,
it's common to extract tabular metadata from images
and use that to train a model.
I've already uploaded the data in BigQuery,
and we can see the different data points in the schema.
The label that we are predicting is called Class,
and we can run this [? quick ?] query
in the data set to see roughly what the distribution of bean
type is across our data set.
So here, I'm using the BigQuery client library
to download that data inside my notebook as pandas DataFrame.
And using this pandas method, we can
see a plot of how the data is distributed by parameters.
Next, we use the Seaborn library to create a pair plot, which
can help us identify our relationships
between the features in our data and the label
column within the class.
Now that we have a good idea of what our data looks like
and how it is balanced, we are ready to kick off
the training job in Vertex AI.
We initialize the project that we want to use.
Then to train this model using AutoML,
we first need a data set.
We can pass the URL to the data in BigQuery
directly to Vertex AI.
There's no need to manually move our data--
Vertex will handle that for us.
Now if we go in Vertex AI console into our data set,
we can see our created data set here
and then we can see more details,
such as the schema and the data source in BigQuery.
We could kick off the training job here,
but I trust that you can click a button,
so let's stick to the SDK for this one.
Back in our notebook, here is the code
to create the training job.
This is a classification model, where we are classifying
the beans into seven classes.
So we define that and pass all the different features
that we want to use in training this model.
You could add more options here, such as how
you would want AutoML to optimize
the training, specifically useful for an imbalanced data
set.
I'm not doing that here.
Finally, job.run will run the training job.
We specify the data set we want to use, the target
column or label that we are predicting, how long we want
to run the training for, and enable early stopping,
so Vertex AI stops the training if it does not
see that the model can be further improved.
Now you specify this, which is useful to keep the cost low.
Once the model has trained--
this could take some time--
we can see in the console that the training job has finished.
And we can see the evaluation metrics
to see how our model actually performed.
The confusion matrix shows how good a job our model
did in distinguishing the different bean types.
Since this is a tabular model, Vertex AI
also gives us feature importance out of the box with AutoML.
We can see the features in our data set
that signaled our model's prediction the most.
Here, it looks like shape and eccentricity were
the most important features.
Now that we have our model created,
let's deploy our endpoints so we can
use it to serve predictions.
You can do that right here in the UI,
but let me show you how to do that in the SDK.
All you have to do to deploy the model
is model.deploy and the type of machine type you want to use.
There are more things you can specify here,
but I'm just going with a minimum.
Once created, we can see it in the console
and test to make predictions from our endpoint.
Let's head back over into our notebook
and see how to make predictions using the API.
We provide a test set.
Then we need to create the endpoint object,
grab the resource name from the deployed model and use it here,
and then we use that to make a prediction
request to our endpoint.
And there's our prediction.
We just trained an AutoML model and made a prediction
using the SDK.
We could use the same data set to train a custom model, too,
all using the SDK.
Here are the code samples that can help you do that.
Try it out yourself.
And if you need help, refer to the links below.
All right, so today we created a bean classification model
using the Vertex AI Python SDK.
Give this a try yourself, and let
me know how it goes in the comments below.