簡體   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