簡體   English   中英

輸出層形狀中的Keras Functional API錯誤

[英]Keras Functional API error in output layer shape

因此,我正在構建一個神經網絡,它將接收2個輸入(圖像)並返回二進制輸出(0或1)。

我已經有了標簽和輸入內容。

標簽和輸入的形狀如下:

--------Labels--------

(8281,)

--------Images--------

(8281, 500, 500, 1) 

這是我的代碼:

input_front_images1 = Input(shape=(500, 500, 1))
input_front_images2 = Input(shape=(500, 500, 1))

x1=Conv2D(32, kernel_size=3,activation='relu')(input_front_images1)
x2=Conv2D(32, kernel_size=3,activation='relu')(input_front_images2)

x = keras.layers.concatenate([x1, x2])
x = Dense(64, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=[input_front_images1,input_front_images2], 
outputs=predictions)


model.compile( optimizer= 'rmsprop' , loss='categorical_crossentropy' , 
metrics=['accuracy'])
model.summary()
model.fit([image_front_pairs1,image_front_pairs2], 
[labels_front_pairs],epochs=2,batch_size=64) 

它給了我這個錯誤:

ValueError: Error when checking target: expected dense_39 to have 4 dimensions, but got array with shape (8281, 1)

您必須將Conv2D圖層的形狀從(H,W,1)更改為(H * W * 1,)

import numpy as np

然后

x = keras.layers.concatenate([x1, x2])
# add this lines to code
dim = np.prod(x._shape[1:])
x = keras.layers.Reshape([dim.value,])(x)
x = Dense(64, activation='relu')(x)

要么

x = Dense(64, activation='relu')(x)
# add this lines to code
dim = np.prod(x._shape[1:])
x = keras.layers.Reshape([dim.value,])(x)
predictions = Dense(1, activation='sigmoid')(x)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM