0

我在 Google AI Platform 上部署了一个模型,如下图所示:

在此处输入图像描述

这是一个使用 Keras 构建的模型,并使用save_model带有标准选项的命令保存。

当我去虚拟测试模型以查看它是否有效时,我将输入一个示例 JSON 请求,如下所示:

{"instances": [
  {"values": ["This is my first sentence"], "key": 1}
]}

我正在关注此 URL 中给出的示例: https ://cloud.google.com/ai-platform/prediction/docs/online-predict?hl=en_US#formatting_your_input_for_online_prediction

当我将示例 JSON 请求输入到评估器中时,如下所示:

在此处输入图像描述

我收到以下错误消息:

{"error": "{\n    \"error\": \"Failed to process element: 0 key: values of 'instances' list. Error: Invalid argument: JSON object: does not have named input: values\"\n}"}

经过一番查看,问题似乎出在 tf.Serving 上,因为 JSON 输入期望除“值”键之外的其他内容来进行预测。

我的问题是我不知道如何访问应该是什么。

我最好的尝试是在本地重新加载模型并调用该get_config()方法以查看那里是否有任何东西。

这返回了以下字典:

{'name': 'functional_1',
'layers': [{'class_name': 'InputLayer',
'config': {'batch_input_shape': (None, 1),
'dtype': 'string',
'sparse': False,
'ragged': False,
'name': 'input_1'},
'name': 'input_1',
'inbound_nodes': []},
{'class_name': 'TextVectorization',
'config': {'name': 'text_vectorization',
'trainable': True,
'dtype': 'string',
'max_tokens': 12500,
'standardize': 'lower_and_strip_punctuation',
'split': 'whitespace',
'ngrams': None,
'output_mode': 'int',
'output_sequence_length': 250,
'pad_to_max_tokens': True},
'name': 'text_vectorization',
'inbound_nodes': [[['input_1', 0, 0, {}]]]},
{'class_name': 'Embedding',
'config': {'name': 'embedding',
'trainable': True,
'batch_input_shape': (None, None),
'dtype': 'float32',
'input_dim': 12500,
'output_dim': 25,
'embeddings_initializer': {'class_name': 'RandomUniform',
 'config': {'minval': -0.05, 'maxval': 0.05, 'seed': None}},
'embeddings_regularizer': None,
'activity_regularizer': None,
'embeddings_constraint': None,
'mask_zero': False,
'input_length': None},
'name': 'embedding',
'inbound_nodes': [[['text_vectorization', 0, 0, {}]]]},
{'class_name': 'Flatten',
'config': {'name': 'flatten',
'trainable': True,
'dtype': 'float32',
'data_format': 'channels_last'},
'name': 'flatten',
'inbound_nodes': [[['embedding', 0, 0, {}]]]},
{'class_name': 'Dense',
'config': {'name': 'dense',
'trainable': True,
'dtype': 'float32',
'units': 50,
'activation': 'relu',
'use_bias': True,
'kernel_initializer': {'class_name': 'GlorotUniform',
 'config': {'seed': None}},
'bias_initializer': {'class_name': 'Zeros', 'config': {}},
'kernel_regularizer': None,
'bias_regularizer': None,
'activity_regularizer': None,
'kernel_constraint': None,
'bias_constraint': None},
'name': 'dense',
'inbound_nodes': [[['flatten', 0, 0, {}]]]},
{'class_name': 'Dense',
'config': {'name': 'dense_1',
'trainable': True,
'dtype': 'float32',
'units': 50,
'activation': 'relu',
'use_bias': True,
'kernel_initializer': {'class_name': 'GlorotUniform',
 'config': {'seed': None}},
'bias_initializer': {'class_name': 'Zeros', 'config': {}},
'kernel_regularizer': None,
'bias_regularizer': None,
'activity_regularizer': None,
'kernel_constraint': None,
'bias_constraint': None},
'name': 'dense_1',
'inbound_nodes': [[['dense', 0, 0, {}]]]},
{'class_name': 'Dense',
'config': {'name': 'dense_2',
'trainable': True,
'dtype': 'float32',
'units': 1,
'activation': 'sigmoid',
'use_bias': True,
'kernel_initializer': {'class_name': 'GlorotUniform',
 'config': {'seed': None}},
'bias_initializer': {'class_name': 'Zeros', 'config': {}},
'kernel_regularizer': None,
'bias_regularizer': None,
'activity_regularizer': None,
'kernel_constraint': None,
'bias_constraint': None},
'name': 'dense_2',
'inbound_nodes': [[['dense_1', 0, 0, {}]]]}],
'input_layers': [['input_1', 0, 0]],
'output_layers': [['dense_2', 0, 0]]}

我希望我正在寻找的一些信息会包含在这里,并且我已经尝试过类似'functional_1'input_1作为使用键的东西,但没有成功。

我也尝试过用于X数据集中的原始列,但没有奏效。

如何访问 tf.Serving 的元数据以了解将什么放入我的 JSON 请求中?

4

1 回答 1

0

输入的格式信息必须包含在 Keras 模型的定义中。

例如,在使用Keras 进行训练和预测的快速入门中,使用来自美国人口普查收入数据集的信息创建和训练模型。此示例的代码位于此GitHub 存储库中。

在内部util.py,数据集信息已准备好,其中仅使用这些列:

'age', 'workclass', 'education_num','marital_status', 'occupation', 'relationship', 'race','capital_gain', 'capital_loss', 'hours_per_week', 'native_country'

然后,预处理留下这样的数据(用于训练阶段):

-0.48454606533050537, 3.0, -0.030303768813610077, 2.0, 6.0, 0.0, 4.0, -0.1447920799255371, -0.2171318680047989, 0.6115681529045105, 38.0

因此,预测的输入必须符合相同的格式。同时,如果您想在 Cloud Console 中使用模型 UI 中的功能测试,则需要在 JSON 对象中发送输入,如下所示:

{"instances": [ 
   {"values": ["This is my first sentence"], "key": 1} 
]}

但是,根据此文档,由于每个数据实例都是浮动值的向量(在这种情况下),因此 JSON 必须是这样的:

{"instances": [
   [-0.48454606533050537, 3.0, -0.030303768813610077, 2.0, 6.0, 0.0, 4.0, -0.1447920799255371, -0.2171318680047989, 0.6115681529045105, 38.0],
   [1.1200168132781982, 3.0, -0.41926509141921997, 2.0, 0.0, 5.0, 4.0, -0.1447920799255371, -0.2171318680047989, -0.03403923660516739, 38.0],
   .
   .
   .
   [-0.5574807524681091, 4.0, -0.030303768813610077, 0.0, 3.0, 1.0, 4.0, -0.1447920799255371, -0.2171318680047989, -0.03403923660516739, 38.0]
]}
于 2020-11-26T22:50:25.187 回答