我训练了一个Tensorflow NN估计器来预测Python中的某些内容。然后,我通过Google colab将模型保存到了Google驱动器中。
今天,我加载了模型,这是一项相当艰巨的工作。我终于成功地使用tf.compat.v2.saved_model.load
和.signature
方法加载了估算器。似乎WrappedFunction。这是我的代码,直到执行此步骤。
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
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:
ConcreteFunction
的所有输入必须为张量;调用>修剪后,第0个输入(我的数据[2513行x 45列])不是> Tensor。
其次,寻找一些张量流文档,我编写了这段代码
test_predictions = infer(tf.constant(test_data))
test_predictions = np.array([item['predictions'][0] for item in test_predictions])
这次返回
参数“示例”(值Tensor(“ Const_10:0”,shape =(2513,45),> dtype = float64))与使用此函数跟踪的形状不兼容。预期形状(?,),但形状(2513,45)。
如果调用了get_concrete_function,则可能需要传递> tf.TensorSpec(...,shape = ...),其形状不太明确,轴上的None可能会有所不同。
我发现张量流的动态/静态形状。但是我无法完全理解这些概念,因此未能重塑。 如何获得结果?谢谢。