Getting 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.

Using curl

To make it more convenient to run the curl samples in this topic, set the following environment variable. Replace project-id with the name of your GCP project.

export PROJECT_ID="project-id"

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 GCP 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 Platform Console.

    Go to the AutoML Tables page

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

  3. Select the Predict 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. Click Predict to get your prediction.

    AutoML Tables predict page

    For information about interpreting your results, see Interpreting your prediction results.

  7. (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/us-central1/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/us-central1/models/model-id:predict
    

    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
        }
      }
    }
    ]
    }
    
  3. (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/us-central1/models/model-id:undeploy
    

Java

/**
 * Demonstrates using the AutoML client to request prediction from automl tables using csv.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name. (e.g., "us-central1")
 * @param modelId the Id of the model which will be used for the prediction.
 * @param filePath the Local text file path of the content.
 * @throws IOException
 * @throws ExecutionException
 * @throws InterruptedException
 */
public static void predict(
    String projectId, String computeRegion, String modelId, String filePath)
    throws IOException, InterruptedException, ExecutionException {

  // Create client for prediction service.
  PredictionServiceClient predictionClient = PredictionServiceClient.create();

  // Get full path of model.
  ModelName modelName = ModelName.of(projectId, computeRegion, modelId);

  // Read the csv file content for prediction.
  Reader in = new FileReader(filePath);
  Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
  for (CSVRecord record : records) {
    int n = record.size();

    // Add all the values.
    List<Value> values = new ArrayList<Value>();
    for (int i = 0; i < n; i++) {
      Value value = Value.newBuilder().setStringValue(record.get(i)).build();
      values.add(value);
    }
    Iterable<Value> allValues = values;

    // Build the row.
    Row row = Row.newBuilder().addAllValues(allValues).build();

    // Build the payload data.
    ExamplePayload examplePayload = ExamplePayload.newBuilder().setRow(row).build();

    // params is additional domain-specific parameters.
    // currently there is no additional parameters supported.
    Map<String, String> params = new HashMap<String, String>();

    PredictResponse response = predictionClient.predict(modelName, examplePayload, params);
    System.out.println("Prediction results:");
    List<AnnotationPayload> annotationPayloadList = response.getPayloadList();
    if (response.getPayloadCount() == 1) {
      System.out.println(
          String.format(
              "\tRegression result: %0.3f",
              annotationPayloadList.get(0).getTables().getValue().getNumberValue()));
    } else {
      for (AnnotationPayload annotationPayload : annotationPayloadList) {
        System.out.println(
            String.format(
                "\tClassification label: %s\t\tClassification score: %.3f",
                annotationPayload.getTables().getValue().getStringValue(),
                annotationPayload.getTables().getScore()));
      }
    }
  }
}

Node.js

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: {}})
      .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}`);
        }
      })
      .catch(err => {
        console.error(err);
      });
  });
stream.read();

Python

# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# model_id = 'MODEL_ID_HERE'
# file_path = '/local/path/to/file'
# score_threshold = 'value from 0.0 to 0.5'

from google.cloud import automl_v1beta1 as automl
import csv

automl_client = automl.AutoMlClient()

# Get the full path of the model.
model_full_id = automl_client.model_path(
    project_id, compute_region, model_id
)

# Create client for prediction service.
prediction_client = automl.PredictionServiceClient()

# params is additional domain-specific parameters.
# score_threshold is used to filter the result
# Initialize params
params = {}
if score_threshold:
    params = {"score_threshold": score_threshold}

with open(file_path, "rt") as csv_file:
    # Read each row of csv
    content = csv.reader(csv_file)
    for row in content:
        # Create payload
        values = []
        for column in row:
            values.append({'number_value': float(column)})
        payload = {
            'row': {'values': values}
        }

        # Query model
        response = prediction_client.predict(model_full_id, payload)
        print("Prediction results:")
        for result in response.payload:
            print("Predicted class name: {}".format(result.display_name))
            print("Predicted class score: {}".format(result.classification.score))

هل كانت هذه الصفحة مفيدة؟ يرجى تقييم أدائنا:

إرسال تعليقات حول...

AutoML Tables Documentation