Deploying Models

You can host your trained machine learning models in the cloud and use the Cloud ML prediction service to infer target values for new data. This page discusses model hosting and prediction and introduces considerations you should keep in mind for your projects.

Model Deployment

Cloud ML Engine can host your models so that you can get predictions from them in the cloud. The process of hosting a saved model is called deployment. The prediction service manages the infrastructure needed to run your model at scale, and makes it available for online and batch prediction requests. This section describes model deployment.

Exporting a SavedModel

The Cloud ML prediction service makes use of models exported through the export_savedmodel() function which is available for models created using the tensorflow, keras and tfestimators packages or any other tool that support the tf.train.Saver interface.

For instance, we can use examples/keras/train.R included in this package to define and train an MNIST keras model by running:

Deploying the Model

Deployment is performed through cloudml_deploy() which uses the same gcloud and cloudml configuration concepts used while training. We can train any exported model by running:

Copying file://savedmodel/variables/ [Content-Type=application/octet-stream]...
Copying file://savedmodel/saved_model.pb [Content-Type=application/octet-stream]...
Copying file://savedmodel/variables/variables.index [Content-Type=application/octet-stream]...
/ [3/3 files][  1.9 MiB/  1.9 MiB] 100% Done                                    
Operation completed over 3 objects/1.9 MiB.

Model created and available in

Notice that models make use of unique names and versions which can be specified using the name and version parameters in cloudml_deploy().


Once a model is deployed, predictions can be performed by providing a list of inputs into cloudml_predict():

mnist_image <- keras::dataset_mnist()$train$x[1,,]
grid::grid.raster(mnist_image / 255)

  name = "keras_mnist",
1 0, 0, 0, 0, 0, 1, 0, 0, 0, 0

For additional information visit Google Cloud Platform - Prediction Basics