Python, Keras - ValueError: Cannot feed value of shape (10, 70, 1025) for Tensor u'dense_2_target:0', which has shape '(?, ?)'

Python, Keras - ValueError: Cannot feed value of shape (10, 70, 1025) for Tensor u'dense_2_target:0', which has shape '(?, ?)'

我正在尝试分批训练 RNN。 输入输入尺寸 (10, 70, 3075), 其中 10 是批量大小,70 是时间维度,3075 是频率维度。

有三个输出,大小为 (10, 70, 1025) 每个,基本上是 10 个大小为 (70,1025) 的频谱图。

我想通过回归训练这个RNN,其结构是

input_img = Input(shape=(70,3075 ) )
x = Bidirectional(LSTM(n_hid,return_sequences=True, dropout=0.5,    recurrent_dropout=0.2))(input_img)
x = Dropout(0.2)(x)
x = Bidirectional(LSTM(n_hid,  dropout=0.5, recurrent_dropout=0.2))(x)
x = Dropout(0.2)(x)
o0 = ( Dense(1025, activation='sigmoid'))(x)
o1 = ( Dense(1025, activation='sigmoid'))(x)
o2 = ( Dense(1025, activation='sigmoid'))(x)

问题是输出密集层不能考虑三个维度,他们想要类似 (None, 1025) 的东西,我不知道如何提供,除非我沿着时间维度连接.

出现以下错误:

ValueError: Cannot feed value of shape (10, 70, 1025) for Tensor u'dense_2_target:0', which has shape '(?, ?)'

batch_shape 选项在输入层有用吗?我真的试过了,但我也有同样的错误。

要获得正确的输出形状,您可以使用 Reshape 图层:

o0 = Dense(70 * 1025, activation='sigmoid')(x)
o0 = Reshape((70, 1025)))(o0)

这将输出 (batch_dim, 70, 1025)。您可以对其他两个输出执行完全相同的操作。

在这种情况下,第二个 RNN 将序列折叠为单个向量,因为默认情况下 return_sequences=False。要分别在每个时间步上制作模型 return 序列和 运行 密集层,只需将 return_sequences=True 添加到第二个 RNN:

x = Bidirectional(LSTM(n_hid,  return_sequences=True, dropout=0.5, recurrent_dropout=0.2))(x)

Dense 层自动应用到最后一个维度,因此之后无需重新整形。