如何在云端机器学习引擎上为我的模型获取javascript的在线预测? [英] How do I get online predictions in javascript for my model on Cloud Machine Learning Engine?

查看:95
本文介绍了如何在云端机器学习引擎上为我的模型获取javascript的在线预测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功部署在Cloud ML Engine上的模型,并通过以下 gcloud ml-engine模型预测 .google.com / ml-engine / docs / how-tos / online-predictrel =nofollow noreferrer>说明,现在我想从我的网络应用/ javascript代码向它发送预测。我该怎么做?

I have successfully deployed on model on Cloud ML Engine and verified it is working with gcloud ml-engine models predict by following the instructions, now I want to send predictions to it from my web app / javascript code. How do I do that?

推荐答案

在线预测API是一个REST API,因此您可以使用任何库来发送HTTPS请求,虽然您需要使用 Google的OAuth 库来获取您的凭据。我们将使用 googleapis 库为简单起见。

The online prediction API is a REST API, so you can use any library for sending HTTPS requests, although you will need to use Google's OAuth library to get your credentials. We will use the googleapis library for simplicity.

预测请求的格式为JSON,如 docs

The format of the prediction request is JSON, as described in the docs.

举例说明,考虑人口普查示例。客户端可能如下所示:

To exemplify, consider the Census example. A client for that might look like:

var google = require('googleapis');

var ml = google.ml('v1');

function auth(callback) {
    google.auth.getApplicationDefault(function(err, authClient) {
        if (err) {
            return callback(err);
        }

        if (authClient.createScopedRequired && authClient.createScopedRequired()) {
            authClient = authClient.createScoped([
                'https://www.googleapis.com/auth/cloud-platform'
            ]);
        }
        callback(null, authClient);
    });
}

var instance = {
    age: 25,
    workclass: " Private",
    education: " 11th",
    education_num: 7,
    marital_status: " Never - married",
    occupation: " Machine - op - inspct",
    relationship: " Own - child",
    race: " Black",
    gender: " Male",
    capital_gain: 0,
    capital_loss: 0,
    hours_per_week: 40,
    native_country: " United - Stats"
}

auth(function(err, authClient) {
    if (err) {
        console.error(err);
    } else {
        var ml = google.ml({
            version: 'v1',
            auth: authClient
        });

        // Predict
        ml.projects.predict({
            name: 'projects/MY_PROJECT/models/census',
            resource: {
                instances: [instance]
            }
        }, function(err, result) {
            if (err) {
                return callback(err);
            }

            console.log(JSON.stringify(result));
        });
    }
});

这篇关于如何在云端机器学习引擎上为我的模型获取javascript的在线预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆