我的网络由LSTM 和密集部分组成,并通过另一个密集部分连接在一起,并且我无法连接大小为 [(1, 8), (None, 32)] 的输入强>。 Reshape
和 Flatten
不起作用。
这是架构:
def build_model_equal(dropout_rate=0.25):
curve_input_1 = Input(batch_shape=(1, None, 1), name='curve_input_1')
lstm_1 = LSTM(256, return_sequences=True, dropout=0.1)(curve_input_1)
lstm_1 = LSTM(64, dropout=0.1)(lstm_1)
lstm_out = Dense(8)(lstm_1)
metadata_input = Input(shape=(31,), name='metadata_input')
dense_1 = Dense(512, activation='relu')(metadata_input)
dense_1 = BatchNormalization()(dense_1)
dense_1 = Dropout(dropout_rate)(dense_1)
dense_out = Dense(32)(dense_1)
x = keras.layers.concatenate([lstm_out, dense_out], axis=1)
output_hidden = Dense(64)(x)
output_hidden = BatchNormalization()(output_hidden)
output_hidden = Dropout(dropout_rate)(output_hidden)
output = Dense(n_classes, activation='softmax', name='output')(output_hidden)
model = Model(inputs=[curve_input_1, metadata_input], outputs=output)
return model
当我通过
训练这个模型时model.fit([x_train, x_metadata], y_train,
validation_data=[[x_valid, x_metadata_val], y_valid],
epochs=n_epoch,
batch_size=n_batch, shuffle=True,
verbose=2, callbacks=[checkPoint]
)
我收到错误
ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(1, 8), (None, 32)]
当我添加Reshape
层时,如
dense_out = Dense(32)(dense_4)
dense_out = Reshape((1, 32))(dense_out)
x = keras.layers.concatenate([lstm_out, dense_out], axis=1)
我明白
ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(1, 8), (None, 1, 32)]
Reshape
层input_shape=(32,)
或input_shape=(None, 32)
参数不改变情况、错误和形状是一样的。
将 Reshape
添加到 LSTM,例如
curve_input_1 = Input(batch_shape=(1, None, 1), name='curve_input_1')
lstm_first_1 = LSTM(256, return_sequences=True, dropout=0.1, name='lstm_first_1')(curve_input_1)
lstm_second_1 = LSTM(64, dropout=0.1, name='lstm_second_1')(lstm_first_1)
lstm_out = Dense(8)(lstm_second_1)
lstm_out = Reshape((None, 8))(lstm_out)
产生错误
ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported.
将 concatenate
axis
参数更改为 0
、1
和 -1
不会没有帮助。
更改Dense
零件输入形状没有帮助。当我执行 metadata_input = Input(shape=(1, 31), name='metadata_input')
而不是metadata_input = Input(shape=(31,), name='metadata_input') 时,它会产生一个[(1, 8), (None, 1, 32)]
尺寸错误。
我的猜测是,我需要将数据转换为 [(1, 8), (1, 32)]
或 [(None, 8), (None, 32 )]
形状,但是 Reshape
和 Flatten
图层没有帮助。
应该有一种简单的方法可以做到这一点,但我错过了。
最佳答案
我认为问题可能是第一个Input
使用了batch_shape
,第二个输入使用了shape
。
对于第一个输入,您的批量大小被硬编码为 1
,并且您的输入数据有 2 个额外维度:None
(未指定)和 1
>。
对于第二个输入,由于您使用的是 shape
,因此您声明您的输入未指定批量大小,并且数据具有一维 31
值。
请注意,使用 shape=(31,)
与使用 batch_shape=(None, 31)
相同(来自 here )。
对齐两者对我来说都是有效的,至少在模型声明时是这样(虽然我无法运行拟合,而且我不确定我是否遗漏了一些东西,并且这个解决方案不适合您的用例。
所以,总而言之,您可以尝试:
curve_input_1 = Input(batch_shape=(1, None, 1), name='curve_input_1')
metadata_input = Input(batch_shape=(1, 31), name='metadata_input')
或者:
curve_input_1 = Input(batch_shape=(1, None, 1), name='curve_input_1')
metadata_input = Input(batch_shape=(1, 31), name='metadata_input')
这相当于:
curve_input_1 = Input(shape=(None, 1, ), name='curve_input_1')
metadata_input = Input(shape=(31, ), name='metadata_input')
请让我知道这有效或引导您走向一个好的方向!
关于python - Keras 连接形状 [(1, 8), (None, 32)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438264/