我有一个使用自定义估计器的 sagemaker tensorflow 模型,类似于 abalone.py sagemaker tensorflow 示例,在 serving_input_fn 中使用 build_raw_serving_input_receiver_fn:
def serving_input_fn(params):
tensor = tf.placeholder(tf.float32, shape=[1, NUM_FEATURES])
return build_raw_serving_input_receiver_fn({INPUT_TENSOR_NAME: tensor})()
使用 json 从 java-script 请求预测:
response = @client.invoke_endpoint(
endpoint_name: @name,
content_type: "application/json",
accept: "application/json",
body: values.to_json
)
到目前为止一切都很好。现在我想添加一些特征工程(使用从训练数据派生的缩放器对特征进行缩放转换)。按照使用 tensorflow tf-transform 进行数据规范化的答案的模式,我现在得到了 serving_input_fn,如下所示:
def serving_input_fn(params):
feature_placeholders = {
'f1': tf.placeholder(tf.float32, [None]),
'f2': tf.placeholder(tf.float32, [None]),
'f3': tf.placeholder(tf.float32, [None]),
}
features = {
key: tf.expand_dims(tensor, -1)
for key, tensor in feature_placeholders.items()
}
return tf.estimator.export.ServingInputReceiver(add_engineering(features), feature_placeholders)
从 saved_model_cli 显示 --dir 。--所有我可以看到输入签名已经改变:
signature_def['serving_default']:
The given SavedModel SignatureDef contains the following input(s):
inputs['f1'] tensor_info:
dtype: DT_FLOAT
shape: (-1)
name: Placeholder_1:0
inputs['f2'] tensor_info:
dtype: DT_FLOAT
shape: (-1)
name: Placeholder_2:0
inputs['f3'] tensor_info:
dtype: DT_FLOAT
shape: (-1)
name: Placeholder:0
我如何为这个新模型的预测准备特征?在python中,我一直没有成功地尝试过类似的事情
requests = [{'f1':[0.1], 'f2':[0.1], 'f3':[0.2]}]
predictor.predict(requests)
还需要从 java-script 发送预测请求。