将数据发送到 Google Cloud ML 自定义预测例程的未知错误

Unknown Error Sending Data to Google Cloud ML Custom Prediction Routine

我正在尝试在 AI Platform 上编写自定义 ML 预测例程,以从客户端获取文本数据,进行一些自定义预处理,将其传递到模型中,然后 运行 模型。我能够成功地将此代码打包并部署到 Google 云上。但是,每次我尝试从 node.js 向它发送请求时,我都会返回 data: { error: 'Prediction failed: unknown error.' },.

这是我的相关自定义预测例程代码。请注意,我在客户端中将 instances 设置为我的文本,然后在自定义预测例程中对其进行标记化和预处理。

def __init__(self, model, session, saver, dictionary):
    self.model = model
    self.sess = session

@classmethod
def from_path(cls, model_dir):
    m = Model(learning_rate=0.1)
    session = tf.Session()
    session.run(tf.global_variables_initializer())
    session.run(tf.local_variables_initializer())
    saver = tf.train.Saver(max_to_keep=0)
    saver.restore(session, (os.path.join(model_dir, 'model.ckpt')))
    return cls(m, session)

def predict(self, instances, **kwargs):
    utterance = nltk.word_tokenize(instances)
    utterance = self.preprocess_utterance(utterance)

    preds = self.sess.run([self.model['preds'], feed_dict={'input_data': utterance)
    return preds

这是我的 Node.js 代码:

   text_string = "Hello how are you?"
   google.auth.getApplicationDefault(function (err, authClient, projectId) {
        if (err) {
            console.log('Authentication failed because of ', err);
            return;
        }
        if (authClient.createScopedRequired && authClient.createScopedRequired()) {
            var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
            authClient = authClient.createScoped(scopes);
        }
        var request = {
            name: "projects/" + projectId + "/models/classifier",
            resource: {"instances": [message_string]},

            // This is a "request-level" option
            auth: authClient
        };

        machinelearning.projects.predict(request, function (err, result) {

            console.log(result)

            if (err) {
                console.log(err);
            } else {
                console.log(result);
                res.status(200).send('Hello, world! This is the prediction: ' + JSON.stringify(result)).end();
            }
        });
    });

在此代码中,我只是将文本发送到 google 云模型。请求正文是: body: '{"instances":["Hello how are you?"]}',

有没有人知道它失败的原因?

如果没有,那么有人知道我该如何调试它吗?一个未知的错误消息根本没有用。

编辑:

这是 saved_model_cli 使用 --all 选项的输出。

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['length_input'] tensor_info:
        dtype: DT_INT32
        shape: ()
        name: Placeholder_3:0
    inputs['seqlen'] tensor_info:
        dtype: DT_INT32
        shape: (-1)
        name: Placeholder_2:0
    inputs['indicator'] tensor_info:
        dtype: DT_INT32
        shape: (-1, 2)
        name: Placeholder_1:0
    inputs['input_data'] tensor_info:
        dtype: DT_INT32
        shape: (-1, -1)
        name: Placeholder:0
    inputs['y'] tensor_info:
        dtype: DT_INT32
        shape: (-1, -1)
        name: Placeholder_4:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['preds'] tensor_info:
        dtype: DT_INT32
        shape: (-1, -1)
        name: Cast:0
  Method name is: tensorflow/serving/predict

基于此,我应该提供这本词典作为输入,但它不起作用。

{"instances": [ { "input_data": [138, 30, 66], "length_input": 1, "indicator": [[0, 0]], "seqlen": [3], "y": [138, 30, 66] } ]}

我认为你需要:

{instances: [
 {"input_data": "hello, how are you?"},
 {"input_data": "who is this?"}
]}

但我们可以确认是否可以查看对您的 SavedModel 文件调用 saved_model_cli 的结果。

我想通了这个问题。问题不是输入数据的格式。相反,它在 NLTK 中。 NLTK.word_tokenize 抛出错误,因为它没有进行标记化所需的数据。我不得不将数据上传到 Google Cloud 或使用不需要任何数据文件的标记化方法来解决这个问题。

我不知道为什么这个 Google 云自定义预测例程软件不告诉它的用户正在发生的错误,但通过我所有的努力它总是 returns Unknown error 每当出现问题时。如果我准确地知道错误是什么,这将是一个简单的修复。