首页 > 解决方案 > 使用预先训练的加载张量流估计器进行预测的问题

问题描述

我训练了一个 tensorflow NN 估计器来预测 Python 中的某些内容。我通过 Google colab 将模型保存在我的 Google 驱动器中。

今天,我加载了模型,这是一项非常艰苦的工作。tf.compat.v2.saved_model.load我终于成功地使用and.signature方法加载了估算器。似乎是 WrappedFunction。这是我到这一步的代码。

  1. 保存代码
serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
  tf.feature_column.make_parse_example_spec(input_column))
#tf.feature_column.make_parse_example_spec([input_column]))
export_path = dnn_regressor.export_saved_model('/content/gdrive/My Drive',serving_input_fn)
#https://www.tensorflow.org/guide/saved_model#savedmodel%EC%9D%98_%EC%A0%9C%EC%96%B4_%ED%9D%90%EB%A6%84
  1. 加载代码
imported = tf.compat.v2.saved_model.load('/content/gdrive/My Drive/1572596260', tags=None)
infer = imported.signatures["predict"]

但是我仍然无法将测试数据建模以做出预测。

首先,我试过:

test_predictions = infer(test_data)
test_predictions = np.array([item['predictions'][0] for item in test_predictions])

它返回一个错误

ValueError: s 的所有输入ConcreteFunction必须是张量;在调用 >of pruned 时,第 0 个输入(我的数据 [2513 行 x 45 列])不是 >Tensor。

其次,找了一些tensorflow文档,我写了这段代码

test_predictions = infer(tf.constant(test_data))
test_predictions = np.array([item['predictions'][0] for item in test_predictions])

这次又回来了

参数 'examples'(值 Tensor("Const_10:0", shape=(2513, 45), >dtype=float64)) 与跟踪此函数的形状不兼容 >with。预期的形状 (?,),但得到了形状 (2513, 45)。

如果您调用了 get_concrete_function,您可能需要传递一个 >tf.TensorSpec(..., shape=...) 具有不太具体的形状,在轴上具有 None >这可能会有所不同。

我发现了张量流的动态/静态形状。但我无法完全理解这些概念,也未能重塑。我怎样才能得到结果?谢谢。

标签: pythontensorflowgoogle-colaboratory

解决方案


推荐阅读