python - Keras 连接形状 [(1, 8), (None, 32)]

标签 python tensorflow keras

我的网络由LSTM 和密集部分组成,并通过另一个密集部分连接在一起,并且我无法连接大小为 [(1, 8), (None, 32)] 的输入强>。 ReshapeFlatten 不起作用。

这是架构:

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)]

Reshapeinput_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 参数更改为 01-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 )] 形状,但是 ReshapeFlatten 图层没有帮助。

应该有一种简单的方法可以做到这一点,但我错过了。

最佳答案

我认为问题可能是第一个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/

相关文章:

python pandas dataframe groupby 将同一组放在一起

python-3.x - 在 Mask R-CNN 中添加多个类

python - Tensorflow 在使用队列的 sess.run 调用上卡住

machine-learning - Keras:使用批量归一化对同一数据集进行不同的训练和验证结果

tensorflow - "object has no attribute ' _name_scope '" tensorflow /keras 中的错误

python - 我们如何调用需要协程的普通函数?

python - Opencv:BoW计算SURF描述符

Python - 调用存储在字典中的对象的函数

python - 为什么深度学习 Keras 上的准确率总是 0.00%,损失很高

python - Keras 应用程序 - imagenet 上的 VGG16 低精度