【问题标题】:I have trouble with the shape of the input into an Keras model我对 Keras 模型的输入形状有疑问
【发布时间】:2021-02-07 17:51:34
【问题描述】:

我有一个有 5 个输入节点和 1 个输出节点的模型。

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 数组以使其适合模型的帮助。 这是错误消息:

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

【问题讨论】:

  • 你能添加你得到的错误吗?

标签: python tensorflow keras neural-network


【解决方案1】:

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

更改这些行:

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

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 2019-12-12
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2016-11-09
    • 2022-10-18
    • 1970-01-01
    相关资源
    最近更新 更多