tensorflow - Google ML-Engine 中的 keras 模型预测失败

标签 tensorflow machine-learning google-cloud-platform keras

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

在这个阶段,我的模型可以很好地进行本地预测等,并已成功将导出的模型放入 GC 存储桶中。我还成功创建了 google cloud 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

import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import *
import tensorflow as tf

training_data_df = pd.read_csv("sales_data_training_scaled.csv")

X = training_data_df.drop('total_earnings', axis=1).values
Y = training_data_df[['total_earnings']].values

# Define the model
model = Sequential()
model.add(Dense(50, input_dim=9, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam')



# Create a TensorBoard logger
logger = keras.callbacks.TensorBoard(
    log_dir='logs',
    histogram_freq=5,
    write_graph=True
)

# Train the model
model.fit(
    X,
    Y,
    epochs=50,
    shuffle=True,
    verbose=2
)

# Load the separate test data set
test_data_df = pd.read_csv("sales_data_test_scaled.csv")

X_test = test_data_df.drop('total_earnings', axis=1).values
Y_test = test_data_df[['total_earnings']].values

test_error_rate = model.evaluate(X_test, Y_test, verbose=0)
print("The mean squared error (MSE) for the test data set is: {}".format(test_error_rate))


model_builder = tf.saved_model.builder.SavedModelBuilder("exported_model")

inputs = {
    'input': tf.saved_model.utils.build_tensor_info(model.input)
}
outputs = {
    'earnings': tf.saved_model.utils.build_tensor_info(model.output)
}

signature_def = tf.saved_model.signature_def_utils.build_signature_def(
    inputs=inputs,
    outputs=outputs,
    method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)

model_builder.add_meta_graph_and_variables(
    K.get_session(),
    tags=[tf.saved_model.tag_constants.SERVING],
    signature_def_map={
        tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def
    }
)

model_builder.save()

sample_input_prescaled.json

{ "输入": [0.4999, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5] }

最佳答案

按照相同的教程,我发现发生了变化:

inputs = {
    'input': tf.saved_model.utils.build_tensor_info(model.input)
}
outputs = {
    'earnings': tf.saved_model.utils.build_tensor_info(model.input)
}

至:

inputs = {
    'input': tf.compat.v1.saved_model.utils.build_tensor_info(model.input)
}
outputs = {
    'earnings': tf.compat.v1.saved_model.utils.build_tensor_info(model.output)
}

导出模型时,为我解决了问题,前者已弃用。

希望这有帮助。

关于tensorflow - Google ML-Engine 中的 keras 模型预测失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48918687/

相关文章:

java - 当应用程序运行时,使用 Apache Mahout 的 TFIDF 有 "Job failed!"

python - 来自 tensorflow /模型的警告 : Please use alternatives such as official/mnist/dataset. py

python - TensorFlow 无法将 apply_gradients 与 AdamOptimizer 一起使用

machine-learning - 根据数据包识别应用程序

c# - LibSVM 将我所有的训练向量都变成了支持向量,为什么?

HTTP Google Cloud Functions 上的 Python 和 "import requests"

python - 使用 JS 后端和 Python 进行机器学习

machine-learning - Tensorflow conv2d_transpose 大小错误 "Number of rows of out_backprop doesn' t 匹配计算”

kubernetes - Google kubernetes引擎上的Pod正在等待?

mysql - 使用 gcloud 命令行创建第二代 Cloud SQL