Online predictions

This page describes how to get online (single, low-latency) predictions from AutoML Tables.

Introduction

After you have created (trained) a model, you can deploy the model and request online (real-time) predictions. Online predictions accept one row of data and provide a predicted result based on your model for that data. You use online predictions when you need a prediction as input for your business logic flow.

Before you can request an online prediction, you must deploy your model. Deployed models incur charges. When you are finished making online predictions, you can undeploy your model to avoid further deployment charges. Learn more.

Getting an online prediction

Console

Generally, you use online predictions to get predictions from within your business applications. However, you can use AutoML Tables in the Cloud Console to test your data format or your model with a specific set of input.

  1. Visit the AutoML Tables page in the Google Cloud Console.

    Go to the AutoML Tables page

  2. Select Models and select the model that you want to use.

  3. Select the Test & Use tab and click Online prediction.

  4. If your model is not yet deployed, deploy it now by clicking Deploy model.

    Your model must be deployed to use online predictions. Deploying your model incurs costs. For more information, see the pricing page.

  5. Provide your input values in the text boxes provided.

    Alternatively, you can select JSON Code View to provide your input values in JSON format.

  6. If you want to see how each feature impacted the prediction, select Generate feature importance.

    Generating feature importance increases the time and compute resources required for your prediction. The Google Cloud Console truncates the local feature importance values for readability. If you need an exact value, use the Cloud AutoML API to make the prediction request.

  7. Click Predict to get your prediction.

    AutoML Tables predict page

    For information about interpreting your prediction results, see Interpreting your prediction results. For information about local feature importance, see Local feature importance.

  8. (Optional) If you do not plan to request more online predictions, you can undeploy your model to avoid deployment charges by clicking Undeploy model.

curl command

You request a prediction for a set of values by creating your JSON object with your feature values, and then using the predict method to get the prediction.

{
  "payload": {
    "row": {
      "values": [ value1, value2, ..., valueN ]
    }
  }
}

The values must contain the exact columns you included in training, and they must be in the same order as shown on the Evaluate tab by clicking on the included columns link.

If you want to reorder the values, you can optionally include a set of column spec IDs in the order of the values. You can get the column spec IDs from your model object; they are found in the TablesModelMetadata.inputFeatureColumnSpecs field.

The data type of each value (feature) in the Row object depends on the AutoML Tables data type of the feature. For a list of accepted data types by AutoML Tables data type, see Row object format.

Replace model-id with the ID of your model. The ID is the last element of the name of your model. For example, if the name of your model is projects/1234/locations/us-central1/models/TBL374, then the ID of your model is TBL374.

  1. If you haven't deployed your model yet, deploy it now:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/{location}/models/model-id:deploy
    
  2. Request the prediction:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Content-Type: application/json" \
    -d '{
        "payload": {
            "row": {
               "values": [
                    value1, value2,...
               ]
            }
        }
     }' \
     https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/{location}/models/model-id:predict
    

    To include local feature importance results, include the feature_importance parameter:

    -d '{
        "payload": {...},
        "params": {
            "feature_importance": "true"
        }
    }' \
    
  3. View your results.

    For a classification model, you should see output similar to the following example. Note that two results are returned, each with a confidence estimate (score). The confidence estimate is between 0 and 1, and shows how likely the model thinks this is the correct prediction value. For more information about how to use the confidence estimate, see Interpreting your prediction results.

    {
     "payload": [
       {
         "tables": {
           "score": 0.11210235,
           "value": "1"
         }
       },
       {
         "tables": {
           "score": 0.8878976,
           "value": "2"
         }
       }
     ]
    }
    

    For a regression model, you get a prediction value and a prediction interval, which provides a range that the model has 95% confidence contains the correct result.

    {
     "payload": [
       {
         "tables": {
           "value": 207.18209838867188,
           "predictionInterval": {
             "start": 29.712770462036133,
             "end": 937.42041015625
           }
         }
       }
     ]
    }
    

    If you requested local feature importance results, they add a section to the results like this example:

        "tablesModelColumnInfo": [
          {
            "columnSpecName": "projects/294348452381/locations/us-central1/datasets/TBL8440/tableSpecs/766336/columnSpecs/4704",
            "columnDisplayName": "Promo",
            "featureImportance": 1626.5464
          },
          {
            "columnSpecName": "projects/2381/locations/us-central1/datasets/TBL8440/tableSpecs/766336/columnSpecs/6800",
            "columnDisplayName": "Open",
            "featureImportance": -7496.5405
          },
          {
            "columnSpecName": "projects/2381/locations/us-central1/datasets/TBL8440/tableSpecs/766336/columnSpecs/9824",
            "columnDisplayName": "StateHoliday"
          }
        ],
    

    When a column has a feature importance value of 0, feature importance is not displayed for that column.

    For information about local feature importance results, see Local feature importance.

  4. (Optional) If you are finished requesting online predictions, you can undeploy your model to avoid deployment charges:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/{location}/models/model-id:undeploy
    

Java

If your resources are located in the EU region, you must explicitly set the endpoint. Learn more.

// Demonstrates using the AutoML client to request prediction from automl tables using csv.
public static void predict(String projectId, String modelId, List<Value> values)
    throws IOException {
  // String projectId = "YOUR_PROJECT_ID";
  // String modelId = "YOUR_MODEL_ID";
  // values should match the input expected by your model.
  // List<Value> values = new ArrayList<>();
  // values.add(Value.newBuilder().setBoolValue(true).build());
  // values.add(Value.newBuilder().setNumberValue(10).build());
  // values.add(Value.newBuilder().setStringValue("YOUR_STRING").build());

  try (PredictionServiceClient client = PredictionServiceClient.create()) {
    ModelName name = ModelName.of(projectId, "us-central1", modelId);
    Row row = Row.newBuilder().addAllValues(values).build();
    ExamplePayload payload = ExamplePayload.newBuilder().setRow(row).build();
    PredictRequest request =
        PredictRequest.newBuilder()
                .setName(name.toString())
                .setPayload(payload)
                .putParams("feature_importance", "true")
                .build();

    PredictResponse response = client.predict(request);

    System.out.println("Prediction results:");
    List<AnnotationPayload> annotationPayloadList = response.getPayloadList();
    if (response.getPayloadCount() == 1) {
      TablesAnnotation tablesAnnotation = annotationPayloadList.get(0).getTables();
      System.out.format(
          "\tRegression result: %.3f\n", tablesAnnotation.getValue().getNumberValue());
      // Get features of top importance
      for (TablesModelColumnInfo info : tablesAnnotation.getTablesModelColumnInfoList()) {
        System.out.format(
            "Column: %s - Importance: %.2f\n",
            info.getColumnDisplayName(), info.getFeatureImportance());
      }
    } else {
      for (AnnotationPayload annotationPayload : annotationPayloadList) {
        TablesAnnotation tablesAnnotation = annotationPayload.getTables();
        System.out.format(
            "\tClassification label: %s\t\tClassification score: %.3f\n",
            tablesAnnotation.getValue().getStringValue(), tablesAnnotation.getScore());
        // Get features of top importance
        for (TablesModelColumnInfo info : tablesAnnotation.getTablesModelColumnInfoList()) {
          System.out.format(
              "Column: %s - Importance: %.2f\n",
              info.getColumnDisplayName(), info.getFeatureImportance());
        }
      }
    }
  }
}

Node.js

If your resources are located in the EU region, you must explicitly set the endpoint. Learn more.

const automl = require('@google-cloud/automl');
const fs = require('fs');
const csv = require('csv');

// Create client for prediction service.
const client = new automl.v1beta1.PredictionServiceClient();

/**
 * Demonstrates using the AutoML client to request prediction from
 * automl tables using csv.
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project";
// const computeRegion = '[REGION_NAME]' e.g., "us-central1";
// const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800";
// const filePath = '[FILE_PATH]'
// e.g., "<resource>/<csv file>", `local csv file path`;

// Get the full path of the model.
const modelFullId = client.modelPath(projectId, computeRegion, modelId);

// Read the csv file content for prediction.
const stream = fs
  .createReadStream(filePath)
  .pipe(csv.parse())
  .on(`data`, function(data) {
    const values = [];

    for (const val of data) {
      values.push({stringValue: val});
    }

    // Set the payload by giving the row values.
    const payload = {
      row: {
        values: values,
      },
    };

    // Params is additional domain-specific parameters.
    // Currently there is no additional parameters supported.
    client
      .predict({
        name: modelFullId,
        payload: payload,
        params: {feature_importance: true},
      })
      .then(responses => {
        console.log(responses);
        console.log(`Prediction results:`);

        for (const result of responses[0].payload) {
          console.log(`Predicted class name: ${result.displayName}`);
          console.log(`Predicted class score: ${result.tables.score}`);

          // Get features of top importance
          const featureList = [];

          for (const feature of result.tables.tablesModelColumnInfo) {
            featureList.push({
              importance: feature.featureImportance,
              displayName: feature.columnDisplayName,
            });
          }
          // Sort features by their importance, highest importance first
          featureList.sort(function(a, b) {
            return b.importance - a.importance;
          });

          // Print top 10 important features
          console.log('Features of top importance');
          console.log(featureList.slice(0, 10));
        }
      })
      .catch(err => {
        console.error(err);
      });
  });
stream.read();

Python

The client library for AutoML Tables includes additional Python methods that simplify using the AutoML Tables API. These methods refer to datasets and models by name instead of id. Your dataset and model names must be unique. For more information, see the Client reference.

If your resources are located in the European Union region, you must explicitly set the endpoint. Learn more.

# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# model_display_name = 'MODEL_DISPLAY_NAME_HERE'
# inputs = {'value': 3, ...}

from google.cloud import automl_v1beta1 as automl

client = automl.TablesClient(project=project_id, region=compute_region)

if feature_importance:
    response = client.predict(
        model_display_name=model_display_name,
        inputs=inputs,
        feature_importance=True,
    )
else:
    response = client.predict(
        model_display_name=model_display_name, inputs=inputs
    )

print("Prediction results:")
for result in response.payload:
    print(
        "Predicted class name: {}".format(result.tables.value.string_value)
    )
    print("Predicted class score: {}".format(result.tables.score))

    if feature_importance:
        # get features of top importance
        feat_list = [
            (column.feature_importance, column.column_display_name)
            for column in result.tables.tables_model_column_info
        ]
        feat_list.sort(reverse=True)
        if len(feat_list) < 10:
            feat_to_show = len(feat_list)
        else:
            feat_to_show = 10

        print("Features of top importance:")
        for feat in feat_list[:feat_to_show]:
            print(feat)

What's next