Keras:如何按顺序合并图层而不使用“连接”

时间:2019-08-14 04:58:49

标签: keras merge lstm layer

我正在尝试构建一个将cnn和lstm结合在一起的模型。 我想对cnn的输入进行多元处理,然后将输出顺序放入LSTM的输入中。但是,合并cnn输出时存在问题。如果使用串联,则它将延伸到axis = -1,如图所示。但是我将其放在lstm结构中,因此我想顺序地增加它。但是我没有找到任何要合并的函数。我想要的形状是下图中的(None,6,1904)。我该怎么办?

下面是我的构建代码。

def build_model():
    in_layers, out_layers = [], []
    for i in range(in_len):
        inputs = Input(shape=(row,col, channel))
        conv1 = Conv2D(4, (12, 12), activation='relu')(inputs)
        pool1 = pooling.MaxPooling2D(pool_size=(4,4))(conv1)
        conv2 = Conv2D(4, (7, 7) , activation='relu')(pool1)
        pool2 = pooling.MaxPooling2D(pool_size=(3,3))(conv2)
        conv3 = Conv2D(8, (5, 5) , activation='relu')(pool2)
        pool3 = pooling.MaxPooling2D(pool_size=(2,2))(conv3)
        flat = Flatten()(pool3)
        # store layers
        in_layers.append(inputs)
        out_layers.append(flat)
        print(type(flat))
    merged = concatenate(out_layers)
    model = Model(inputs=in_layers, outputs=merged)
    plot_model(model, show_shapes=True, to_file='cnn_lstm_real.png')

    return model

enter image description here

1 个答案:

答案 0 :(得分:0)

您想要的仍然是串联,但在另一个新轴上。连接层和功能允许指定轴,因此您可以这样操作:

def build_model():
    in_layers, out_layers = [], []
    for i in range(in_len):
        inputs = Input(shape=(row,col, channel))
        conv1 = Conv2D(4, (12, 12), activation='relu')(inputs)
        pool1 = pooling.MaxPooling2D(pool_size=(4,4))(conv1)
        conv2 = Conv2D(4, (7, 7) , activation='relu')(pool1)
        pool2 = pooling.MaxPooling2D(pool_size=(3,3))(conv2)
        conv3 = Conv2D(8, (5, 5) , activation='relu')(pool2)
        pool3 = pooling.MaxPooling2D(pool_size=(2,2))(conv3)
        flat = Flatten()(pool3)
        flat = Reshape((1, -1))(flat)
        # store layers
        in_layers.append(inputs)
        out_layers.append(flat)

    merged = concatenate(out_layers, axis = 1)
    model = Model(inputs=in_layers, outputs=merged)
    plot_model(model, show_shapes=True, to_file='cnn_lstm_real.png')

    return model

唯一的不同是,您需要在每个分支的输出中显式添加新轴(因此Reshape层),以便允许沿该轴进行级联。