繁体   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