【问题标题】:Keras: Shape Mismatch between Dense and Activation layersKeras:密集层和激活层之间的形状不匹配
【发布时间】:2017-12-19 22:46:27
【问题描述】:

一直在尝试在 Keras 中创建神经网络,但遇到了一个问题,即我的一个密集层和激活层之间的形状不匹配。我错过了一些明显的东西吗?使用 TensorFlow 后端。

print(x_train.shape)
print(y_train.shape)
(1509, 476, 4)
(1509,)

那么我的模型如下:

###Setup Keras to create a bidirectional convolutional recurrent NN based on DanQ NN
###See https://github.com/uci-cbcl/DanQ
model = Sequential()

model.add(Conv1D(filters=320,
                 kernel_size=26,
                 padding="valid",
                 activation="relu",
                 strides=1, 
                 input_shape=(476, 4)
                ))

model.add(MaxPooling1D(pool_size=13, strides=13))

model.add(Dropout(0.2))

model.add(keras.layers.wrappers.Bidirectional(LSTM(320, return_sequences=True, input_shape=(None, 320))))

model.add(Flatten())

model.add(Dense(input_dim=34*640, units=925))
model.add(Activation('relu'))

model.add(Dense(input_dim=925, units=919))
model.add(Activation('sigmoid'))

print('compiling model')
model.compile(loss='binary_crossentropy', optimizer='rmsprop', class_mode="binary")

print('running at most 60 epochs')

model.fit(x_train, y_train.T, batch_size=100, epochs=60, shuffle=True, verbose=2, validation_split=0.1)

tresults = model.evaluate(x_test, y_test, verbose=2)

print(tresults)

print(model.output_shape)

但我收到以下错误:

ValueError: Error when checking target: expected activation_48 to have shape (None, 919) but got array with shape (1509, 1)

错误似乎源于使用 sigmoid 激活输入到第二个激活层。例如:

model.add(Dense(input_dim=925, units=919))
model.add(Activation('sigmoid'))

为什么会出现不匹配?

【问题讨论】:

  • 您不必在密集层中指定 input_dim 和单位。前任。最后一个密集层应该是model.add(Dense(1,activation='sigmoid'))
  • 谢谢!这似乎解决了不匹配问题,但弹出了一个新错误:ValueError: Invalid argument "class_mode" passed to K.function with Tensorflow backend,源自model.fit()。对此有什么想法吗?
  • 是的,你不需要在model.compile() 中有参数class_mode='binary'

标签: python tensorflow neural-network keras reshape


【解决方案1】:

正如@djk47463 的评论中提到的,您的输出现在每个样本有 919 个值,因为这是网络最后一层中的单元数。要纠正此问题,请将最后一层的单位设置为 1,或者添加一个新的最终层,其输出维度为 1。

【讨论】:

    【解决方案2】:

    在您的代码中,

    model.add(Conv1D(filters=320,
                 kernel_size=26,
                 padding="valid",
                 activation="relu",
                 strides=1, 
                 input_shape=(476, 4)
                ))
    

    尝试在input_shape = (476,4) 的位置添加input_dim = 4。 也许它会起作用。

    【讨论】:

    • 这是不正确的文档状态,如果卷积层是网络中的第一层,则必须指定 input_shapedocs
    • input_dim 来自 Keras 1.0。如果我要使用它,我还必须指定input_length = 476,它与input_shape = (476, 4) 基本相同。
    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2019-05-09
    • 2020-05-22
    • 1970-01-01
    相关资源
    最近更新 更多