繁体   English   中英

我对 Keras model 的输入形状有疑问

[英]I have trouble with the shape of the input into an Keras model

我有一个 model 有 5 个输入节点和 1 个 output 节点。

model = keras.models.Sequential()
model.add(keras.layers.Dense(5, input_shape=(5, ), activation='relu'))
model.add(keras.layers.Dense(5, activation='relu'))
model.add(keras.layers.Dense(1, activation='exponential'))
model.compile(optimizer="sgd", loss="mean_squared_error")

我正在尝试使用这些输入训练一批。

input1 = [1, 4, 2, 4, 5]
input2 = [1, 4, 3, 5, 1]
input3 = [1, 4, 3, 3, 2]
input_batch = np.array([input1, input2, input3])

output1 = 2.5
output2 = 3.9
output3 = 1.3
output_batch = np.array([output1, output2, output3])

model.train_on_batch(input_batch, output_batch)

print(model.predict(np.array([1, 5, 2, 3, 1])))

这似乎不起作用,所以我需要一些关于如何塑造 numpy 阵列以使其适合 model 的帮助。 这是错误消息:

ValueError: Error when checking input: expected dense_input to have shape (5,) but got array with shape (1,)

您的 output 的最后一个维度是 1,因此您必须更改标签。 另一个问题 - predict批量工作。 所以你必须添加一个批次维度。

更改这些行:

model.train_on_batch(input_batch, output_batch[..., tf.newaxis])
print(model.predict(np.array([[1, 5, 2, 3, 1]]))) # <= add brackets

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM