Google Cloud AI: Using Custom Layers

Hi and welcome to the first project in my series called “End to end Deep Learning with Google Cloud AI”. In this first project, we are going to learn a couple of things: First, we will learn how to use custom layers using a Lambda layer in Keras. And secondly, we will do this project in Google Cloud AI platform - so we will go through the basics of that and see how to create a project and setup a notebook in the cloud, how to provision a virtual machine instance for it and also how to enable GPUs for a project. Then, in future projects, we will utilise the provisioned GPUs to train our models. We will learn to use the power of google cloud to ultimately create end to end deep learning pipelines. This will be extremely useful when you create production ready deep learning applications.

Available On Coursera
Google Cloud AI: Using Custom Layers

Duration (mins)


NA / 5


Task List

We will cover the following tasks in 48 minutes:

Introduction & Importing Libraries

Before we get started, let’s quickly understand the pre-requisites: You will need to know a bit of Python. You should know the basics of Keras and Tensorflow. If you are new to them, please check out my Tensorflow beginner series on Rhyme to get up to speed. You will also need a google account to sign up for Google Cloud Platform but I assume almost everyone has one so that won’t be a problem. Please note that while you don’t need to use GPUs for this project, we will use them in the future projects and for that GCP or Google Cloud Platform may ask you to provide credit card info. They will give you some free credit when you get started though so you shouldn’t actually incur any actual charges even by the end of the whole series. So, if you don’t have a GCP account, then please sign up for it and if you do, then log into it.

Preparing Data

So far, we know that we are going to use custom layers in Keras but that’s about it. More specifically, we are going to create a model which will be used to verify if any two given images from the MNIST dataset are of the same digit class or not. There can be many ways to do this, but the approach that we want to focus on does not use any classifier training like what we’ve done before in the TensorFlow beginner series.

Instead, we will create a model which will compare compressed feature representations of any two MNIST images and if the two feature representations, also called embeddings, are similar to each other then the model will classify the image pair as a positive pair or a pair of images of the same class. If the two embeddings are different enough from each other, the model will classify the pair as a negative pair.

Get Dataset Functions

Because we want to generate pairs of examples for both the training set and the test set, it makes sense to define a function to help us do that. Here, we want to send two images at a time to our model. However, the label will still remain just a single value of either 0 for a negative pair or 1 for a positive pair. Basically we have two images as inputs to our model instead of just one.

Creating the Model

e want to create a model that will take a pair of images as input and give us a binary 0 or 1 output. 1 if the pair of images are both of the digit 0 and output 0 if they are not of the same digit. We are going to use Keras’ functional API to create our model. This is pretty straight-forward to use. Once the model architecture is implemented, all we have to do now is train it on the training data and then evaluate it on the test data. Let’s do that in the next task.

Model Training and Evaluation

Our test set accuracy is almost the same as the validation accuracy, which is good. So, congratulations, we have successfully created and trained a model which uses a custom layer and can tell us if any two images are both of the digit 0 or not. Now, this application may seem pretty useless in the real world but the underlying idea can be quite useful. This technique is actually similar to what is used in face verification systems and we created a simple version of it using VGG Face in our Tensorflow advanced series available on Rhyme. Later in this Google Cloud AI Platform series, we will create a more complex, more practical and production ready face verification system completely from scratch.

Plotting Embeddings

We will get embeddings for these images with the help of the trained model. Then, we will reduce the dimensionality of the resulting data to fit just 1 value per example. We are using TSNE to do this dimensionality reduction though we could use other ideas or even our networks final layers linear output before activation. But the idea here is to represent our 16 dimensional vectors on just a single axis. This way, we can visually see if the images of 0s are easily separable from the images of other digits as far as their learned embeddings are concerned.

Watch Preview

Preview the instructions that you will follow along in a hands-on session in your browser.

Amit Yadav

About the Host (Amit Yadav)

I am a machine learning engineer with focus in computer vision and sequence modelling for automated signal processing using deep learning techniques. My previous experiences include leading chatbot development for a large corporation.

Frequently Asked Questions

In Rhyme, all projects are completely hands-on. You don't just passively watch someone else. You use the software directly while following the host's (Amit Yadav) instructions. Using the software is the only way to achieve mastery. With the "Live Guide" option, you can ask for help and get immediate response.
Nothing! Just join through your web browser. Your host (Amit Yadav) has already installed all required software and configured all data.
You can go to, sign up for free, and follow this visual guide How to use Rhyme to create your own projects. If you have custom needs or company-specific environment, please email us at
Absolutely. We offer Rhyme for workgroups as well larger departments and companies. Universities, academies, and bootcamps can also buy Rhyme for their settings. You can select projects and trainings that are mission critical for you and, as well, author your own that reflect your own needs and tech environments. Please email us at
Rhyme strives to ensure that visual instructions are helpful for reading impairments. The Rhyme interface has features like resolution and zoom that will be helpful for visual impairments. And, we are currently developing a close-caption functionality to help with hearing impairments. Most of the accessibility options of the cloud desktop's operating system or the specific application can also be used in Rhyme. If you have questions related to accessibility, please email us at
We started with windows and linux cloud desktops because they have the most flexibility in teaching any software (desktop or web). However, web applications like Salesforce can run directly through a virtual browser. And, others like Jupyter and RStudio can run on containers and be accessed by virtual browsers. We are currently working on such features where such web applications won't need to run through cloud desktops. But, the rest of the Rhyme learning, authoring, and monitoring interfaces will remain the same.
Please email us at and we'll respond to you within one business day.

No sessions available