ML Engine - Machine Learning in the Cloud
Key Takeaways
The video demonstrates how to build and deploy a machine learning model to Cloud ML Engine and make it available via Firebase Cloud Functions, using tools like Firestore, Data Lab, ML Engine, and Firebase Cloud Function. It covers topics such as data analysis, predictive modeling, and cloud computing, with a focus on machine learning fundamentals.
Full Transcript
[Music] today we'll start from zero and finish the video with our own custom machine learning model deployed to the cloud as an API we will pull some actual user data that we have saved in the firestore database then we'll export it to data lab so we can train a Python based machine learning model with virtually unlimited compute resources once we have a solid model in place we'll deploy it to ml engine so we can version it and analyze it in production and finally we will expose it to the universe by making it available in a firebase cloud function but before you that make sure to LIKE and subscribe and I'll be giving away this one-of-a-kind Python brain t-shirt all you have to do is leave a comment below then we'll pick a random winner via livestream next week this video was made in collaboration with Roddy Chilika who is get this only in the seventh grade and already doing crazy stuff with tensorflow firebase and a bunch of other tech in addition to this video you'll also want to check out his github repo that shows you how to train a tensor flow model directly in a cloud function now we're ready to get started and we'll do something that we've never done on this channel before and that's right Python code Python is the most commonly used language for machine learning problems and it's pretty easy to learn if you already know JavaScript if you want to get inspired by Python head over to Kaggle and check out some of the amazing work done by a real data scientist in today's video you'll learn not only how to build and train a machine learning model but also how to deploy it to the cloud as a real product that people can use the first step in any machine learning problem is the underlying data a practical use case for a firebase developer would be to save some information about a user in the firestore database then train that data to build a predictive model that adds some value to the user experience if we look in our database you can see here we have a collection of developer records and each one will tell us what tools this developer uses and their overall happiness as a developer this is just synthetic data that I generated for this demo but you can think of it as being information that you might collect from a user survey or something along those lines machine learning problems generally require a huge amount of data and also a huge amount of compute resources to run operations on that data we're going to use a service on GCP called data lab it allows us to stream and process our data in the cloud which means you can spin up a GPU powered supercomputer from your laptop and then just shut it off when you're done to get going with data lab you'll need to have a firebase or GCP project and then we have a couple of things to run here from the command-line basically you're just installing data lab and then calling data lab create with the name that you want to give it what this does is give you an interactive Python or Jupiter notebook in the cloud that you can use to run Python code and you can allocate as much CPU or memory that you need and also attach GPUs to it the nice thing about this notebook is that it's tightly integrated with all of your other GCP resources such as storage buckets bigquery and everything else it should give you a link to localhost that will show a notebook that looks like this the first thing I'm going to do is install firebase admin by running exclamation pip install firebase admin now if you're not familiar with Jupiter notebooks they're basically just a nice UI for executing Python code we can put each code block in its own cell and then execute those one by one by clicking the play button you'll notice that there are a ton of examples in the data lab environment so definitely check those out especially if you're not super comfortable with Python then notebook we're building today does four main things first it brings in our data from fire store then we do an exploratory data analysis then we split the data up and train an algorithm and then finally export it to a cloud storage bucket first we'll need access to fire store so we'll go into the firebase project settings and generate a new private key that will give you a JSON file that you can save in the root of the data lab there are two dependencies that you'll see commonly in machine learning problems one is pandas which allows us to put data in a data frame and the second one is numpy which helps us make scientific calculations on arrays what we'll be doing in just a second here is taking our fire stored data and putting it into a panda's data frame and numpy will help us generate some random synthetic data that we can use for this demo from there we have a library called pi plot that will help us generate some charts then we'll import firebase admin and initialize it with our credentials the process is basically identical to node J s or any other language for that matter this next part shows how I generated the synthetic data and I'm not going to go through it line by line but just explain what's going on at a highway almost all machine learning problems boil down to finding a signal that's hidden within some noise so the data I've generated below is slightly random but also has some structure to it as well the value that we're trying to predict is developer happiness so I started by giving every user a random happiness between negative 25 and 25 then I have a dictionary with 6 different programming languages in it each one that will give the developer a different level of happiness and I also gave each developer a value of firebase which when true makes the developer much happier than when false but the general idea here is that the data is partially random and partially fixed our goal is to create an algorithm that can detect which features are important and which aren't in this example firebase should be the most important followed by programming language and an age shouldn't be important at all if you have your own data in fire store you can just skip this step and then in the next cell I will actually retrieve that data from the database and then save it to a CSV file so we don't have to reread it each time and get charged for additional reads in fire store conveniently pandas has a to CSV method that we can use to just quickly save our data to a CSV file so now if we come back to our notebook we can just run pandas read CSV and then the first thing I'll do is just sample 10 items from that data frame to see what the data looks like machine learning is not magic and the first thing you should do once you have your data is run an exploratory analysis the idea is to uncover some signals here that you can use to nudge your algorithm in the right direction and the reason we put our data in a panda's data frame is because it gives us a pull bunch of methods to help analyze and also clean the data before it goes into an algorithm so here we're running data frame sample to just get ten random rows out of the data set and a data frame is treated very much like an array you can see here that I'm doing data frame with the happiness key on there and then I'm gonna run plot histogram to give us a visual idea of what the actual label looks like so you can see here we have a pretty normal distribution of happiness values and this is what we want our algorithm to be able to predict based on the other features in the data another thing you can do is run data frame describe to get common statistics on the data set such the mean standard deviation and things like that so now that we've explored our data it's time to introduce a new library called scikit-learn you've probably heard a lot of hype surrounding tensorflow and deep neural networks and those tools are great but they're often overkill for a lot of problems scikit-learn contains a huge collection of tools for tackling machine learning problems the first thing we'll do is use it to clean our data to make sure that all the values are numeric which is required for most machine learning algorithms currently you'll notice that our programming language value is a string value so what we need to do is get all of the unique elements in that array and convert them to a number then assign each value to the corresponding number we can do this easily with scikit-learn by creating a label encoder and then having it fit transform that column to the numeric classes if we sample the data frame again you'll see that our language column is now numeric and no longer a string the next thing scikit-learn will help us with is slicing the data into different chunks that we can use to train an algorithm we'll define a new variable named X which is all of our features and the data - the one we're trying to predict which is happiness then we'll define y as the variable that we are trying to predict now we need to separate these into sets for training and then another one for testing or validation so I'm using this train test split method passing it our X Y values and in making the test size be one third of the total data set size which means we'll be training on 66% of the data the reason you don't train all the data is because then you have no way to validate whether or not the model is any good and you want a model that can kind of generalize the overall data set so now we're going to train an algorithm that's usually very good out of the box which is a random forest in our case we have a value between negative 100 and positive 100 and the predicted value can be anything in between there so we have an infinite number of possible predictions which means we're dealing with a regression problem this differs from a classification problem where you might be predicting different labels like whether or not a picture is a dog or a cat the code in this last cell is very simple we just have the random forest regressor which we fit to our X&Y data and then we predict on the features in the test set of that there are a bunch of different options you can pass to the regressor to try to fine-tune it that's called hyper parameter tuning and then you need to have a metric to analyze its performance in this case we're going to use mean absolute error which means that our random forest model is about 18 points wrong on average so how good is that exactly well the best place to start is just by comparing it to a random set of predictions in the next cell we can see that completely random predictions produce a mean absolute error of about 56 which means that our algorithm is relatively effective now the last thing we want to do is figure out which features and the data were most important in the decision-making process as you can see here the age was not important at all because it was completely random and then firebase and programming languages were the most important so at this point we have a predictive model but we have no way for anybody to use it if you're building a product you most likely want to serve that up as an API so it can be consumed by some kind of front-end application like angular or iOS or whatever and that's where ml engine comes in the last cell in the notebook that you're looking at here is taking the model that we just trained dumping it as a job load file and then saving it in our firebase storage bucket and they'll engine will be able to read the model file in the bucket and then serve out predictions in a highly performant way with predictions being served over the network in just a matter of a few hundred milliseconds so before we head over to ml engine go into your firebase storage bucket and make sure that you have your job load file saved in its own individual folder from there you'll want to go back to the GCP console and then go to enable api's you'll need to enable the cloud build API because your models will actually be versioned and of course we'll need the ml engine API as well from there you can navigate over to the cloud ml engine dashboard and the first thing I want to do is create a model and give that model a name you'll need this name later when we get to the cloud function the great thing about this system is that your models can have multiple versions so you can see how their performance changes over time it's important that you fill out things correctly here like our Python version as 2.7 in the environment and we're using the scikit-learn framework and the version is 19 and the runtime version is 1.9 from there you'll need to select the folder that has this model in it which is that job would file that we saved earlier it'll take a few minutes to save but once that's done you'll be able to either make predictions from the command line or from other Google api's such as a cloud function as we'll see here next once the model goes into production we'll be able to get some actual performance metrics about how it's handling its workload so now we're on to the final step and that's writing a cloud function that can consume this model for that will switch over to vs code and first we'll go into the command line run firebase and net functions then we'll see the end of the functions directory and install Google api's the Google API package for node will allow us to authenticate the cloud function and then make requests to the ml API without the need for an HTTP library I'm using typescript here but you could just as easily use vanilla Jas will import Google from Google API s then reference ml l version 1 the function itself is called predict happiness and it's just a regular HTTP function but you could use any kind of cloud function here you want a good alternative would be to run a prediction every time a new record is added to the firestore database we're going to use this single cloud function to make requests to potentially multiple models so we'll have the request body define which model should make the prediction and then the instances will be the actual data that we're trying to predict which is typically an array with the same shape in which you trained your actual machine learning model so now that we have the request body we can format our request to the Google API this request requires authentication which we can get by calling Google auth get application default and that will connect our firebase credentials with the Google API from there we'll set up a variable for the model name which needs to have your project ID in it as well as the name of the actual model that you gave it in the ml engine console and now everything is in place to make a prediction call to the ml engine API we call projects predict pass in our auth credentials the name of the model and then also the instances that we're trying to predict this can actually be an array of arrays if we want to make multiple predictions from a single cloud function call and then typescript gets a little confused right here so we need to add as any after the argument to predict so that should resolve with the predictions from our model and then we can send that back out as the response from this HTTP function at this point you could deploy this cloud function and you essentially have an API endpoint with your machine learning model but we're just going to run it locally and then we'll test it with an application called insomnia insomnia is a free desktop app that you can install and it allows you to make HTTP calls in this nice user interface so first in the JSON body will declare the model name which is developer happiness for the instances we pass an array of arrays where each array is going to be a prediction that we're trying to make so these values match the features in our original data set for example column one would be the users age column to whether or not these firebase and column three they're encoded programming language now if we send this request to our function we get a response back in about 400 milliseconds that gives us the prediction from those instances so each set of features sent from the request here on the Left got a different happiness prediction from the response here on the right in other words we started with basically nothing and now we have an endpoint in the cloud for our custom machine learning model I'm gonna go ahead and wrap things up there if this video helped you please like and subscribe and you can find the full source code on angular firebase com if you're serious about building a project with firebase or Google cloud consider upgrading to a pro membership at angular firebase com you'll get access to a whole bunch of exclusive content designed to help you build and ship your app faster thanks for watching and I'll talk to you soon you
Original Description
Learn how to build and deploy a machine learning model to Cloud ML Engine, then make it available to the world via Firebase Cloud Functions. https://angularfirebase.com/lessons/serverless-machine-learning-with-python-and-firebase-cloud-functions/
- Radi Cho's TensorFlow Repo https://github.com/radi-cho/tfjs-firebase
- ML Engine https://cloud.google.com/ml-engine/
- Datalab https://cloud.google.com/datalab/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Fireship · Fireship · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Angular 4 Development and Production Environments with Firebase
Fireship
OAuth with Angular and Firebase Tutorial
Fireship
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
Angular Router Guards for Firebase Users
Fireship
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
Upload Files from Angular to Firebase Storage
Fireship
How to Deploy an Angular App to Firebase Hosting
Fireship
Sharing Data between Components in Angular
Fireship
Loading Spinners for Asynchronous Firebase Data
Fireship
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
Firebase Database Rules Tutorial
Fireship
Autocomplete Search with Angular4 and Firebase
Fireship
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
Custom Usernames with Firebase Authentication
Fireship
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
Simple Pagination with Firebase and Angular 4
Fireship
How to Connect Firebase Users to their Data - 3 Methods
Fireship
Add Toast Message Notifications to your Angular App
Fireship
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
Learn NgModule in Angular with Examples
Fireship
Lazy Loading Components in Angular 4
Fireship
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
Angular 4 Full Text Search with Algolia - Part 1
Fireship
Algolia with Firebase Cloud Functions - Part 2
Fireship
Firebase Phone Authentication in Angular 4
Fireship
Top 7 RxJS Concepts for Angular Developers
Fireship
Learn Angular Animations with 5 Examples
Fireship
Advanced Firebase Data Filtering (Multi-Property)
Fireship
Realtime Maps with Mapbox + Firebase + Angular
Fireship
Angular Reactive Forms with Firebase Database Backend
Fireship
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
Top 7 Ways to Debug Angular 4 Apps
Fireship
Infinite Scroll with Angular and Firebase
Fireship
Use TypeScript with Firebase Cloud Functions
Fireship
Realtime Graphs and Charts with Plotly and Firebase
Fireship
Role-Based User Permissions in Firebase
Fireship
User Presence System in Realtime - Online, Offline, Away
Fireship
Location-based Queries with GeoFire and Angular Google Maps
Fireship
Angular ngrx Redux Quick Start Tutorial
Fireship
Angular Ngrx Effects with Firebase Database
Fireship
Progressive Web Apps with Angular
Fireship
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
RxJS Quick Start with Practical Examples
Fireship
Send SMS Text Messages with Twilio and Firebase
Fireship
Firebase Database Performance Profiling
Fireship
Native Desktop Apps with Angular and Electron
Fireship
Subscription Payments with Stripe, Angular, and Firebase
Fireship
Firestore with AngularFire5 Quick Start Tutorial
Fireship
Angular HTTP Client Quick Start Tutorial
Fireship
Google Sign-In with Firestore Custom User Data
Fireship
Star Review System from Scratch with Firestore + Angular
Fireship
Angular Chatbot with Dialogflow (API.ai)
Fireship
Learn @ngrx/entity and Feature Modules
Fireship
Infinite Scroll Pagination with Firestore
Fireship
Faster Firestore via Data Aggregation
Fireship
Contentful - CMS for Angular Progressive Web Apps
Fireship
More on: Supervised Learning
View skill →Related Reads
📰
📰
📰
📰
We Gave Our Engineering Team a Memory — Here’s How PRECOG Uses Cognee
Medium · Machine Learning
# A 94% pass rate hid a PII leak in 6 test cases
Dev.to AI
Day 98 of Learning MERN Stack
Dev.to · Ali Hamza
CLM Series/Chapter-9: Carrying Uncertainty — What the Machine Cannot Do
Medium · Data Science
🎓
Tutor Explanation
DeepCamp AI