Machine Learning in GCP

Sahilkumar
CapTech Corner

--

Introduction

In this blog, I will briefly talk about the different Machine Learning options that are available in Google Cloud Platform and walk through an example project of my own. This will include briefly talking about the older AI Platform service as well as introducing the new Vertex AI service. My project will give an example of how to read data from a GCS bucket, perform exploratory data analysis in a managed Jupyter notebook instance, train a model in that notebook, save the model to a different GCS bucket, and finally use that model in a full-stack application. Here is the repository with the code for this application.

AI Platfrom

AI Platform was GCP’s original Machine Learning service. AI Platform gives users the ability to label their data, train models, and use those trained models to serve predictions. While AI Platform was once the recommended Machine Learning service to use for GCP, Google has since released a more comprehensive AI service that is recommended to use. As a result, I won’t go into detail on AI Platform, but if you want to learn more here in the documentation and another blog to reference.

Vertex AI

Along with everything AI Platform offered users, the new Vertex AI service also offers users the ability to not only use custom training code but to take advantage of Google’s AutoML as well. With AutoML, users can train models on image, tabular, text, and video datasets without having to write a single line of code. Users can interact with Vertex AI through the Cloud Console, gcloud command line, AI Platform’s REST API and their Python Client libraries, managed Jupyter Notebooks, or a Deep Learning Virtual Machine. Vertex AI also offers users the ability to not only deploy endpoints for their trained models, but to manage those endpoint from the Vertex AI console as well. Furthermore, this creates the ability to specify traffic splitting and versions when deploying a new model. Here is a screenshot of the Vertex AI dashboard:

As shown, Vertex AI offers everything AI Platform does and so much more such as a feature store, a place to track training and experiments, and even store metadata. Vertex AI is a service that contains everything a company would need over the course of the Machine Learning workflow.

Project Overview

For my example project, I trained a Machine Learning model to help predict an NFL player’s fantasy, ppr, draft kings, and fan duel score for the 2021 season based on their 2020 statistics. I utilized a managed Jupyter Notebook in Vertex AI to import my data, cleanse it, and train my model. This is just one example of how to utilize GCP’s Vertex AI service, however, the service gives users the flexibility to create their own Machine Learning workflow in many different ways.

Perform EDA, Train and Save Model

The first step to my (and any) Machine Learning project is collecting quality data. I was able to find a find a site that kept track of player’s season statistics as well as fantasy output with quality data dating back all the way to 1995. I created a web-scraping script to collect this data into JSON files and saved each file to a GCS bucket. Now with the data stored in a GCS bucket in my project, I was able to import this data into my managed Juypter Notebook instance I created in the Vertex AI console. I was able to load the JSON files stored in GCS into my Notebook by assigning the Notebook a service account with the need permissions and following code:

The Juypter Notebook I had created came with all the Python packages I needed to train my model such as Pandas, NumPy, and Scikit-learn. GCP can also spin up Notebooks that come with packages needed for Deep Learning such as TensorFlow and PyTorch. Since I had only collected about 5500 rows of usable data, I didn’t have nearly enough for a Deep Learning Framework to serve as any benefit. The models were trained using the Linear Regression algorithm by splitting the data into training/test sets, and then fitting them to the training data using the following code:

After the models were evaluated, the final step was packaging them up and saving them on the instance using the Joblib library. Finally, they were able to be saved to a different GCS bucket to be used for predictions using this code:

Consume Model From API Service

Vertex AI had the capability of hosting the endpoint for my model, but I chose to go with the approach of creating a Flask application on Google Cloud Run to import my model and serve predictions to my web client using REST. My reasoning for doing this was to take advantage of Google Cloud Run’s server-less capabilities as well as formatting the request body on the API side instead of the client side. I also chose to use Flask because it is Python-based micro framework. This is important because Python is a great language to use for anything Machine Learning related and as a micro framework, Flask gives me the flexibility to customize my application as needed. To start creating my application, I first gave my Cloud Run application a service account with permissions to read from my GCS bucket storing my models. After loading the needed model, I formatted the JSON body of the POST request to made it valid input for my model to predict a score. Here is an example of that code for the fantasy score endpoint:

I start by using the Joblib library to load my model, followed by a helper method to format the request’s body so that input order matches the input order of the data used to train the model. My last step is structuring the request body into a format that my model is able to get a prediction from. The reason for formatting the request body in the same order as the training input is because models give predictions based on the order of the input and not based on the label names. I also dropped the name field from the model input because it is a string and models only use numbers as input. This input cleansing gives me the ability to serve my models’ predictions from a client’s POST requests.

Send Data From Web Client

The final step of this project was creating a web client to use my Cloud Run service and get predictions for players based on their 2020 statistics. In order to do this, I initialized my application with the Firebase SDK in order to query for my players stored in Cloud Firestore. Cloud Firestore only allows reads and writes from a Firebase application if a user is authenticated by default. I had to change the security rules to allow only reads from my application without needing authentication. Using the name of a player, I was able to query my Cloud Firestore database by that name and return his 2020 statistics to be used in my POST request body. Before sending off the request, I needed to format the request to give my API service the information it needed and ease the amount of the work it has to do. I formatted my request using the following code:

This code creates what is known as dummy variables for the different positions a fantasy football player can have. Dummy variables are when you take input information that is represented by a string value and convert it into numerical values that a Machine Learning model can use. This is an example of taking the four positions an NFL fantasy player can have, and representing what position they are by assigning them a 1 or a 0 for that position value. Note: this only works for string data that has a small set of possible values. Lastly, I deleted the position key from the request body because it has a string value. This simple formatting trick allows me to use the data returned from my Cloud Firestore database and get predictions from my Cloud Run endpoint.

Conclusion

Google Cloud Platform has the ability to meet the needs of any company’s Machine Learning workflow whether it would be utilizing the AI Platform or Vertex AI service. GCP also allows for the flexibility of being creative with how you orchestrate and deploy your Machine learning processes and models. My project is only one of many different ways to implement a Machine Learning solution in GCP. Once again, here is the repository with the project and its code for reference.

--

--

Sahilkumar
CapTech Corner

Full Stack developer @ CapTech. Speciality in Cloud technologies, mainly GCP