【发布时间】:2019-10-29 11:01:24
【问题描述】:
我正在尝试在 AI Platform 上编写自定义 ML 预测例程,以从客户端获取文本数据,进行一些自定义预处理,将其传递到模型中,然后运行模型。我能够成功地在谷歌云上打包和部署这段代码。但是,每次我尝试从 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();
}
});
});
在这段代码中,我只是将文本发送到谷歌云模型。请求正文是:
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]
}
]}
【问题讨论】:
-
请在 savedmodel 上运行 saved_model_cli 并发布你得到的东西
-
我不知道如何在我的模型上运行它,因为我用
tf.train.write_graph(tf.get_default_graph(),...)保存它,我收到错误Message type "tensorflow.SavedModel" has no field named "node"..。但是,我可以告诉你我的输入和输出是什么。我的输入是字段input_data, seqlen, indicator, length_input。我更改了我的实例以匹配它,但它仍然失败。{"instances": [{"input_data": "Hello how are you doing?", "seqlen": [5], "indicator": [1], "length_input": 1}]}
标签: google-cloud-platform google-cloud-ml