[MUSIC PLAYING]
IBRAHIM ULUKAYA: Do you want to create your own recommendation
engine in your app to comment content to your users
based on their interests?
I'll show you how to do it in five easy steps.
Hello, there.
I'm Ibrahim from Firebase ML, and welcome to Adding
on-device recommendation to your app with TensorFlow
and Firebase.
I have an app that recommends movies.
Up until now, I hand created recommendations to my users
as I didn't have the data science skills,
and I didn't know how to build a personalized recommendations
engine.
I also didn't want to use a Cloud recommendation solution
as I prefer to keep my users' data private on their device,
keep the solution lightweight with low latency,
and have the feature work even when the network is offline.
Creating a custom ML model typically
involves these five steps.
First, identify the business problem.
In this case, creating movie recommendations.
Second, gather, pre-process, and split data.
In this case, it's gathering their [INAUDIBLE]
reflecting a user's content interests,
then pre-processing it to be able to feed into the model
and splitting it into training and test data.
Third, create a model architecture.
Fourth, train, tune your model, and evaluate it
with the test data.
And finally, deploy and implement your recommendations
engine.
Today I'll show you how to do all of that using
Firebase and a few other tools.
By the way, this tutorial and other reference codelabs
are available at codelabs.developers.google.com
so you can access the source code
and run the tutorial at your own pace.
Let's get started.
[SWISH]
[CLICK]
[SWISH]
Here is my app.
As you can see, I have a list of movies
where our users can show their interest using
the like buttons.
They can then go to the Recommendations tab
to get a list of movies they might be interested in.
Currently the app is working, but I'm not
showing any recommendations.
At the end of the codelab, I will
implement a recommendation engine
and present the recommendations.
As a first step I want to gather data,
so I'll go to Firebase console and enable
Firebase Analytics for my app.
With Analytics, we would like to obtain user behavior data.
In this case, which movies our users
like in a sequential order.
This data will be used in future steps
to train the recommendations model.
Now I go back to my app and add this code snippet, which
will trigger an Analytics event each time one of our apps users
likes a movie.
We'll send the movie's ID in a select item event.
Later, when we aggregate these events,
we'll draw conclusions on which movies our users
are likely to watch after showing previous interest
in a group of other movies.
Now that we edit this snippet, our app
will start sending Analytics Data back to Firebase.
Note that there may be a small delay before the events show up
in console.
In the meantime, you can use the back view to test your event.
Now that my events have come in, I
need a place to store and aggregate this data
where I can query just the right part to feed into my model.
I wish there was a tool for it.
Ah, BigQuery comes to the rescue.
BigQuery is a Google Cloud product
that allows you to examine and process large amounts of data,
and it is a great integration with Firebase.
In this step you will connect your Firebase console project
to BigQuery so that the Analytics Data generated
by your app is automatically exported to BigQuery.
We'll go to Firebase console, click Settings,
and Project Settings.
From the Integrations tab, we'll click BigQuery.
We'll select Analytics events and accept.
You have now enabled your Firebase console project
to automatically send Firebase Analytics event
data to BigQuery.
This happens automatically without
any further interaction.
However, the first export that creates the Analytics dataset
in BigQuery may not happen for 24 hours.
After the dataset is created, Firebase continually
exports new Analytics events to BigQuery
into the intraday table and groups events
from past days in the events table.
Training your recommendations model requires a lot of data.
Since we don't already have an app generating
large amounts of data, in the next step
we will import a sample dataset into BigQuery
to use for the rest of this tutorial.
Let's import sample dataset into BigQuery.
We'll go to BigQuery dashboard in the Google Cloud console.
We'll select our project name.
We'll click Create Dataset.
Let's enter Firebase recommendations
dataset for the dataset ID and create dataset.
The new dataset shows up in the left menu under the project
name.
Let's click it.
We'll now create table.
For Create table from, we'll select Google Cloud Storage.
In the Select file from GCS bucket field,
we'll enter the path of our sample dataset.
For our example, Firebase recommendations,
recommendations test, formatted, data, filtered, txt.
We'll select JSONL in the file format dropdown.
Let's check the box for Auto detect
Schema and input parameters.
We'll enter recommendations table for the table name,
and select Create table.
Now we can explore the sample dataset.
We'll click on the dataset and the table we just created,
and select Preview to see the actual Analytics event
data this table contains.
Now we will create service account credentials
in our Google Cloud console project
to access and load our BigQuery data from the Google CoLab
environment in the next step.
First, we'll enable the BigQuery, BigQuery Storage,
and Firebase ML APIs.
Then we'll go to Create Service Account page,
and create a new service account with owner
role for this example.
Our browser will download the key.
In the next step, we will use Google CoLab
to import the BigQuery data, pre-process this data,
train our recommendations model, and export
this model in TFLite format suitable for using mobile apps.
In the CoLab, let's run the first cell
to clone the TensorFlow recommendations model
sample from GitHub.
This is the model we will use with our Analytics training
data to create the recommendations model.
The model uses a convolutional neural network encoder.
It applies multiple layers of convolutional neural network
to generate an encoding of the user history Analytics data.
Hey, if you're a mobile developer like me,
you can just use the steps below as is
without any previous machine learning experience.
But if you do want more details, you
can simply refer to the documentation
for the underlying TensorFlow model.
In this notebook, we use Analytics data from BigQuery
to generate training data for our recommendations model.
To access BigQuery data from the notebook,
we'll upload the service account file that we just downloaded
in the previous step.
Let's start with a test import for only 10 rows.
We can see that Firebase Analytics exports
a lot of additional information, such as device type, platform
version, that we don't need.
For the purposes of training this model,
the fields we are interested in are Event Name, Timestamp,
Items, and User Pseudo ID.
Notice that some fields, such as item fields,
are actually an object.
We'll extract the sub-field to create the item ID.
Here, we can give it a try.
Now let's import the whole dataset
with only the fields we are interested in.
So far so good.
Now we can move to the pre-process stage.
We'll start with the lambda function to extract item ID
and rename it as movie ID.
Now we can safely drop the items column.
To train our model, we would like
to create a timeline of movies for each user.
So we'll group these events per user
and sort them using the timestamp.
Finally, we'll convert these timelines
to training and testing data.
We'll skip timelines less than three IDs,
and pad all timelines up to 100 IDs.
Finally, for testing, we'll expect our model
to infer the latest movie after seeing all the previous movies
in each timeline.
We'll go ahead and write these examples into TFRecords
to be loaded in the training set.
Now we can start training our model.
Here we are only training 100 epochs
to speed up training time, but you
might want to use increased number of epochs to trade
longer training time for a higher quality, higher accuracy
model.
There are multiple types of models
you can use for recommendation from a [INAUDIBLE] model,
to an RNN model, or a CNN model.
In this example, we use a CNN model
as that is suited best for our particular dataset and use
case, but refer to the codelab for more details selecting
your model for your use case.
This may take a little more than an hour to train,
so I'll fast forward.
But hey, if you really want to see this,
let me know in the comments below
and I'll shoot another video.
Here we go.
We are at the latest epoch.
In the next step we'll compress and export our model
into a TFLite file so we could actually
use it on mobile devices.
As you can see, our model export was successful.
We can see our TFLite file under the export directory.
Let's give it a quick try and make
sure the model file actually works.
Oh, here it is.
Now that our model is ready, the best way
to deploy it on the fly is using Firebase ML.
We can easily upload the model using Firebase Admin SDK.
Let's initialize our app instance.
First, we'll upload the model file to our Cloud Storage
bucket, then we'll create a model object
with the name Recommendation.
Finally, we'll publish this new model
so it's available to be downloaded from our app.
Now that our model is on Firebase ML,
we'll go to our app and add the model download snippet.
Here I'm checking if the model is downloaded before.
If not, I'll create a download task,
and when it is successful, I'll access the TFLite file.
With the TFLite file downloaded, I
can initialize my interpreter.
Let's make sure our model name is the same
as we named it on our publishing step, which is Recommendations.
As you can see here, we are loading the movie names
into our app to be able to label the results.
For our inference to work as expected,
I'll convert users' liked movies into a similar timeline with
paddings just as we trained the model.
This will be our pre-process stage,
and I'll simply run my interpreter
in the inference stage.
For our model and the inference stage,
all our movies were given as integer IDs,
so the result will also be an array
of movie IDs with corresponding float numbers of confidence.
In the post-processing stage, I'll
simply match the labels, in our case,
the movie names with the movie IDs.
Let's run our app and see if it works.
Here, as I move to the Recommended tab,
I get the message that my remote model is downloaded.
So let me like a few movies and here are my recommendations.
Beautiful.
Just to make sure, I can go back and change my likes
and come back to brand new recommendations.
[SWISH]
Today, we were able to build a recommendation
engine from scratch by training our own TensorFlow
model in Colab.
To achieve that, we gathered data
in Google Analytics, processed it in BigQuery,
finally, Firebase ML helped us to deploy this model
to our device.
You can go further ahead, monitor
your model's performance with Firebase Performance
Monitoring.
Pick the most accurate and performant one
using A/B testing.
You can check our digit classifier codelab
for those steps.
You can also create a continuous training pipeline,
running the training steps we used at CoLab instead
in a Cloud environment.
We can't wait to see what you can achieve with Firebase ML.
Please check out the docs linked in the description
to learn more about these tools, as well as the tools
we co-wrote in this video.
And hey, if you'd like us to do another video on more
of these tools in the future, let us know by liking the video
and subscribing to the channel.
And maybe we'll be back with a part two.
I'm Ibrahim from Firebase ML and goodbye for now.
[MUSIC PLAYING]