![](/img/trans.png)
[英]ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 to have value 8 but received input with shape [None, 1]
[英]Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1)
我正在嘗試使用遺傳算法來自動化神經網絡的設計。 我對神經網絡和 tensorflow 很陌生,所以如果我不能提供信息或正確解釋事情,請原諒我。 我有多個問題需要解決。
我的輸入是一個浮點值數組:
self.data_inputs = np.array([self.car_location, self.car_velocity, self.ball_location]).astype(np.float)
我想要的輸出是這樣的:
self.desired_output = np.asarray([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0])
也就是說,我試圖使使用 softmax 的神經網絡輸出層在此特定實例中生成接近 1 的分數。
第一個問題,我應該如何定義輸出(對於神經網絡)? 目前它被定義為:
output_layer = tensorflow.keras.layers.Dense(13, activation="softmax", name="output")
第二個問題,我將我的網絡定義為這樣生成:
input_layer = tensorflow.keras.layers.InputLayer(3, name="input")
dense_layers = []
output_layer = tensorflow.keras.layers.Dense(13, activation="softmax", name="output")
self.text = f"Creating new generation"
if (len(model_array) == 0): # generate random population
for individual in range(self.population_size):
chosen_input = random.randint(3, 60)
input_for_dense_layer = tensorflow.keras.layers.InputLayer(chosen_input)
dense_layers.append(input_for_dense_layer)
index = 1
for i in range(random.randint(1,5)):
dense_layer = tensorflow.keras.layers.Dense(chosen_input, activation = "relu")
dense_layers.append(dense_layer)
chosen_input = random.randint(3, 60)
index += 1
model = tensorflow.keras.Sequential()
model.add(input_layer)
for dense_layer in dense_layers:
model.add(dense_layer)
model.add(output_layer)
model.compile(optimizer=random.choice(self.optimizer_array), loss=random.choice(self.loss_array), metrics=['accuracy'])
model_array.append(model)
但這會產生一個錯誤:
ValueError: Input 0 of layer dense_1 is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1)
誰能向我解釋我如何沒有正確地將這些層連接在一起? 據我所知(和測試)它似乎可以正常工作,但是當我在此 API 的上下文中啟動它時,我嘗試使用它會引發此錯誤。 我只是測試不夠廣泛嗎?
activation='sigmoid'
來執行您想要的操作。input_layer
和input_for_dense_layer
),這可能會導致第一層的形狀期望混淆。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.