【问题标题】:Keras Functional API error in output layer shape输出层形状中的 Keras 功能 API 错误
【发布时间】:2019-05-07 12:33:31
【问题描述】:

所以我正在构建一个神经网络,它将接收 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)

【问题讨论】:

    标签: python keras neural-network keras-layer


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 2018-10-18
      • 1970-01-01
      • 2017-03-01
      • 2022-01-23
      相关资源
      最近更新 更多