在Google ML-Engine上使用Keras模型进行预测失败

我目前正在学习一个教程,该教程介绍了如何在Google的云服务上使用基于Keras的模型与其ML-Engine一起使用。

目前,我已经成功地让模型在本地预测等方面正常工作,并且已经成功地将导出的模型放置到GC-bucket中。我还成功创建了Google云ML-Engine模型。

当我尝试从云端托管的模型上运行预测时,会产生以下错误。

错误:

C:\mydir>gcloud ml-engine predict --model=[mymodel] --json-instances=sample_input_prescaled.json        {      "error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.FAILED_PRECONDITION, details=\"Attempting to use uninitialized value dense_4/bias\n\t [[Node: dense_4/bias/read = Identity[T=DT_FLOAT, _class=[\"loc:@dense_4/bias\"], _output_shapes=[[1]], _device=\"/job:localhost/replica:0/task:0/cpu:0\"](dense_4/bias)]]\")"    }

我可以看到这个错误提到了一个未初始化的值’dense_4’,看起来像是Keras模型中的最后一层,但我不知道为什么/是否这会导致这个过程出错?

有没有人对这个错误信息的原因有见解?

以下是我从教程中使用的Keras模型,以及用于测试预测的json文件。

export_model.py

sample_input_prescaled.json

{ “input”: [0.4999, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5] }


回答:

上面的代码使用以下输入对我来说是有效的。

X = np.random.rand(1000,9)Y = np.random.rand(1000,1)

然后我使用了以下代码。

from keras import backend as K    sess = K.get_session()input_tensor = model.inputoutput_tensor = model.outputoutput_tensor.eval(feed_dict={input_tensor: np.random.rand(1,9)}, session=sess) 

接下来,我导出模型。在使用服务功能之前,请确保导出的模型对您有效。

 export_dir = ...      with tf.Session(graph=tf.Graph()) as sess:      tf.saved_model.loader.load(sess, [tag_constants.TRAINING], export_dir)

这样就成功了。然后,我在task.py中使用了以下服务函数来处理JSON输入,并且再次成功了。

 def json_serving_input_fn():   inputs = {}   for feat in 9:      inputs[feat.name] = tf.placeholder(shape=[None], dtype=feat.dtype)   return tf.estimator.export.ServingInputReceiver(inputs, inputs)

所以,我怀疑您的输入没有正确输入。

Related Posts

这个代码是什么意思?(训练测试集分割 Scikit-learn)

无论我去哪里都能看到这个代码。需要帮助理解这个代码。 …

如何使用LSTM进行序列分类,使用KerasClassifier

我有一个二元分类问题,需要根据2010-2015年间的…

如何使用深度神经网络解决线性逆问题 Ax=b?

已关闭。此问题需要更加集中。目前不接受回答。 想要改进…

如何正确定义Keras循环层中的input_dim

我正在尝试训练一些神经网络来预测时间序列。我使用Seq…

如何计算回归问题的准确率? [duplicate]

这个问题已有答案: 当损失函数为均方误差(MSE)时,…

多变量/特征的Tensorflow

我想使用多层Keras来对人的身高进行分类,如果身高超…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注