具有DistributionLambda层的TFServ的TensorFlow自定义模型签名

问题描述

问题上下文:

我在第一层有一个非常简单的回归模型: feature_column.dense_features_v2.DenseFeatures 最后一层: tfp.layers.distributionLambda TFProbability层,

具有以下结构:

Mode Summary:

对于分布的训练和预测,一切都很好。由于我将分布作为该模型的输出,因此我的推理需要以其他方式完成:

而不是致电:

yhat = model.predict(input_data)

我需要打电话:

yhat = model(input_data) 获取分发,然后处理我想要的相应输出

median = yhat.quantile(0.5).numpy().ravel()

stddev = yhat.stddev().numpy().ravel()

这意味着我有一个非标准的推理过程,并且使用TF-Serving进行部署无法提供所需的输出

问题

我想使用tensorflow-serving部署模型,但是我不知道如何获取自定义输出

{median,mean}而不是标准TFServing {'prediction':100}方法输出中的predict

我发现我可能需要在save_model步骤中修改模型的签名,因为:

如果我检查它们,我会得到:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['feature_1'] tensor_info:
        dtype: DT_INT32
        shape: (-1,1)
        name: serving_default_id_brand:0
    inputs['feature_1'] tensor_info:
        dtype: DT_INT32
        shape: (-1,1)
        name: serving_default_id_category:0
        ...
The given SavedModel SignatureDef contains the following output(s):
    outputs['output_1'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1,1)
        name: StatefulPartitionedCall_117:0
  Method name is: tensorflow/serving/predict

->我只有output_1输出

但是我在TensorFlow 2中找不到如何执行此操作的任何示例。

我已经找到了一些旧的实现(适用于TF1:https://sthalles.github.io/serving_tensorflow_models/),但是无法弄清楚应该怎么做。

就目前而言,我仍然停留在如何为几个输入(我需要以某种方式定义tensorflow.python.eager.function.ConcreteFunction)和所需的输出中正确定义所有这些内容上。

我的输入是(这是来自Features向量的OrderedDict,所以我认为它甚至更复杂...):

inputs = {
   'feature_1': <tf.Tensor 'feature_1:0' shape=(None,1) dtype=int16>,'feature_2': <tf.Tensor 'feature_2:0' shape=(None,...
}
and I would like:

outputs = {
        "median": median,"stddev": stddev
   }

任何帮助,教程等都将不胜感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)