PRIYANKA VERGADIA: Hi I'm Priyanka Vergadia,
and this is AI Simplified where we
learn to make your data useful.
In the previous episodes, we learned
how to create data sets in Vertex AI and trained machine
learning model using auto ML.
In this video, we help our friends at Fuel Symbol
build custom machine learning models.
Their team of machine learning experts
have decided to write their own training
code to predict the fuel efficiency of a vehicle.
Join me as I walk them through the different ways in which you
can run custom training in Vertex AI.
And finally, we will kick off our training job in the console
and see how it's done.
Now what do we need to create a custom job?
Before we submit any of the custom jobs to Vertex AI,
we need to create a Python training application or custom
container to write the training code and the dependencies.
There are pre-built containers to run our code
if we write our training application in Python
using TensorFlow, scikit-learn, XG Boost or PyTorch.
If you are using pre-built containers,
then you can just provide the path
to the training package in Cloud storage.
You can also build your own custom containers,
if you wish to.
And in that case, you provide a location
to the container from your container registry
or your artifact registry.
If you're not sure whether to use pre-built or custom
containers, then check out the link I've included below.
In both cases, you would need a folder within Cloud storage
bucket to put your model output artifacts into.
When running custom training jobs on Vertex AI,
we can also make use of Vertex's hyperparameter tuning service.
If you're not familiar with hyperparameters,
they are variables that govern the process of training
your models, such as batch size or number of hidden layers
in a deep neural network.
In hyperparameter tuning jobs, the Vertex AI
creates trials of your training job
with different sets of hyperparameters,
and then searches for the best combination of hyperparameters
across a series of those trials.
Now to run the training, you need compute resources.
It can be either single node or multiple worker
pools for distributed training.
This is also the step where you would
select the type of machines, the CPUs, the disk
size, and the disk type, and the accelerators, such as GPUs.
And once that training is done, we
need the trained module as an endpoint
to be served for predictions.
In Vertex AI, we can do that by using pre-built containers that
are supported runtime such as TensorFlow, scikit-learn,
PyTorch.
Or you can also build your own custom container
that is stored in container registry or artifact registry.
Of course, if you just need the module artifacts
to be downloaded, then you can skip these steps altogether.
Or you can just use the model for batch predictions.
Now that we have the basic understanding of our custom
training choices, let's help Fuel Symbol
train and sell a custom model that predicts the fuel
efficiency of a vehicle.
We will be using TensorFlow to build this model.
And then we'll package our training code
in a Docker container.
So let's see.
In the Google Cloud console, before we begin,
make sure to enable the Vertex AI API Compute Engine
and Container Registry APIs.
In this example, we will use Notebooks
to create the code for our Docker container.
But you can use any environment you like to do this.
Create a new notebook instance with the latest version
of TensorFlow Enterprise.
For this particular demo, I am training without GPUs.
But depending on your use case, choose GPUs if you need them.
Now that our instance is created,
let's open the JupyterLab and navigate to the terminal
window in our notebook.
For the purpose of this demo, we will use the training code
that already exists in TensorFlow Docs
to predict the fuel efficiency of a vehicle.
It uses the auto MPG data set from Kaggle to predict,
and I've included the link below to this page.
What we will do here is we submit the training job
to Vertex AI by putting our training called in a Docker
container and pushing the container to Google Container
Registry.
Using this approach, we can train a model
built with any framework.
Now, our first step is to create a Docker file.
This Docker file uses one of the deep learning container
TensorFlow Enterprise Docker images.
Now the deep learning containers on Google Cloud
come with many common machine learning and data science
frameworks preinstalled.
The one we're using here includes TensorFlow Enterprise,
Pandas, scikit-learn, and others.
Now after downloading that image,
this Docker file sets up the entry point
for our training code.
And we haven't created these files yet.
But we will do that now.
Before that, we need a Cloud Storage bucket
to export our trained TensorFlow model into.
Vertex AI will then use this bucket
to read our imported model assets,
and to deploy the model.
Now for our model code, let's create a trainer directory
and a train.py file.
We would add our training code to the train.py file.
Here, I have adapted the code from the TensorFlow
docks for the model to predict fuel efficiency
for the vehicles.
Now if you're interested in using the same code,
follow the codelab that's linked below.
We need to make sure we update the bucket
variable with the name of the storage bucket
that we just created earlier.
Now we need to build and test the container locally.
Let's run the container within our notebook
to see if it is working correctly.
Now, it's building now.
And now that it is built, let's run this container.
There it is.
Working fine.
In a production environment, training
will likely take longer, which is why you would
want to run it on the Cloud.
Now let's see how to do that.
Now we push it to Google Container Registry
so Vertex AI can access it.
With our container pushed to Container Registry,
we are now ready to kick off a custom model training job.
Now we go to the training in Vertex AI
and select No managed data sets since a data
set is being pulled from the source in our model code.
Now Vertex AI gives us two options
for training the models.
With auto ML, we can train high quality models
with minimal effort and machine learning expertise.
And we covered this in our previous episodes.
So check them out.
Now here, we are using custom training.
Give the model a name and in container settings,
we have two options.
Now pre-built containers support PyTorch, scikit-learn,
TensorFlow and XG Boost.
It requires us to package and upload
our application code and dependencies to a Cloud storage
bucket.
Now because we have our own custom container
on Google Container Registry, we will select Custom Container
here, which supports not just the common machine learning
frameworks, but also non machine learning dependencies,
libraries, and binaries.
Now we provide our image URL and our Cloud storage bucket.
And we will see hyperparameter tuning in later episodes.
So let's leave this unchecked for now and continue.
In Compute and Pricing, we set up a standard machine
since this model trains quickly.
But you're welcome to experiment with larger machine types
and GPUs if you like.
Just remember that if you use the GPUs,
you will need to use a GPU enabled base container image.
The Prediction Container section is
where we specify how we would like to serve our model.
Now here we use pre-built containers.
And we provide the path to our Cloud storage bucket.
And, we're ready for training.
We can check to see the training in progress.
This could take some time.
I have fast forwarded the process here for you.
Now that our model is trained, we
need to deploy it to an endpoint to make predictions.
Now give it a name and since this is a demo,
we will just leave the traffic split at 100%,
and enter 1 for minimum number of compute nodes.
Depending on the use case, you can set the maximum number
of nodes for autoscaling when the traffic on the endpoint
is high.
I'm selecting a standard machine type here.
Pick the one that makes sense for your use case, and deploy.
Deploying the endpoint will take a few minutes.
And when it is done, we can see it in our Endpoint tab.
Now that our endpoint is deployed,
we can start making predictions.
We will get predictions on our trained
model from a Python notebook using the Vertex AI Python SDK.
But you can get predictions from any environment you like.
So I'm back in our notebook instance
and creating a Python 3 notebook from the launcher.
Now we install the Vertex AI SDK.
Then add a cell to import the SDK
and create a reference to the endpoint we just deployed.
Replace the endpoint and the project
ID with our values of the endpoint and project ID.
And then finally, we make a prediction
to our endpoint with some test data.
And there's our predicted value.
With that, we've helped our friends at Fuel Symbol
train a custom model using Vertex AI.
We provided the training code in custom container,
and used TensorFlow model here.
But you can train a model built with any framework using
custom containers.
Then we deployed the TensorFlow model
using pre-built containers.
And we finally created a model endpoint
and generated a prediction.
In the next video, we will build a vision model using auto ML.
In the meantime, let's continue our discussion
in the comments below.
And don't forget to like and subscribe.