gpt4 book ai didi

python - Tensorflow 服务返回 400 Bad Request 错误

转载 作者:行者123 更新时间:2023-12-04 17:22:08 25 4
gpt4 key购买 nike

我有 2 个模型正在试验,它们的输入和输出都是相同的。一个是 CNN,另一个只有 Dense 层。我以相同的方式保存了这两个模型,并尝试使用 TF Serving Rest API 从它们中获取预测。

CNN 模型:

inputs = tf.keras.layers.Input(shape=(50,3))
x = tf.keras.layers.Conv1D(filters=12, kernel_size=2, strides=1, padding='valid', activation='relu')(inputs)
x = tf.keras.layers.MaxPooling1D()(x)
x = tf.keras.layers.Conv1D(filters=12, kernel_size=2, strides=1, padding='valid', activation='relu')(x)
x = tf.keras.layers.MaxPooling1D()(x)
x = tf.keras.layers.Conv1D(filters=12, kernel_size=2, strides=1, padding='valid', activation='relu')(x)
x = tf.keras.layers.Flatten()(x)
predictions = tf.keras.layers.Dense(3, activation='softmax')(x)

密集模型:

inputs = tf.keras.layers.Input(shape=(50,3))
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
x = tf.keras.layers.Dense(64, activation='relu')(x)
x = tf.keras.layers.Dense(64, activation='relu')(x)
x = tf.keras.layers.Dense(32, activation='relu')(x)
x = tf.keras.layers.Flatten()(x)
predictions = tf.keras.layers.Dense(3, activation='softmax')(x)

尝试使用 TF 服务向 CNN 模型发送预测请求时,我得到了正确的响应,但发送到密集模型的相同请求返回了 400 Bad Request错误。

reading = np.random.uniform(0, 1, (50, 3))
r = requests.post('http://localhost:8501/v1/models/my_model:predict', data=json.dumps({'instances': reading.tolist()}))

CNN 模型正确返回:

{"predictions": [[1.78464702e-20, 4.44278494e-33, 4.32330665e-07]]}

密集模型返回:

{"error": "indices[0] = 2 is not in [0, 2)\\n\\t [[{{node model/dense/Tensordot/GatherV2_1}}]]"}

知道这里发生了什么吗?

最佳答案

仔细观察,Dense 模型期望输入为 (-1,50,3)

一个简单的 np.reshape(-1,50,3) 解决了我的问题。

关于python - Tensorflow 服务返回 400 Bad Request 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65574746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com