【问题标题】:on Keras with Tensorflow backend, fitting a LSTM and some dense layers in parallel on different fractions of input在带有 Tensorflow 后端的 Keras 上,在不同的输入部分上并行拟合 LSTM 和一些密集层
【发布时间】:2018-10-09 01:16:40
【问题描述】:

我正在做一个回归预测,其中我有一些复杂的 3D 序列和一些解释序列的一些关键特征的特征。它们被固定在两个这样形状的矩阵上:

X1.shape, X2.shape
((9000, 300, 3), (9000, 106))

我想将它们提供给模型实例,其中 X1 矩阵由 LSTM 处理,X2 矩阵由几个密集层处理。我的计划是在输出层之前合并它们。

我计划通过以下方式训练:

model.fit(zip(X1, X2), y, batch_size=BATCH, epochs=EPOCHS, validation_split=0.2)

如何构建模型以便接收两个矩阵并分别处理?

目前我只有 LSTM 的标准模型:

def model(sample_size=300, axis=3):
    inp=Input(shape=(sample_size, axis))
    x=LSTM(50, return_sequences=True)(inp)
    x=GlobalMaxPool1D(x)
    x=Dense(1)(x)
    model=Model(inputs=inp, ouputs=x)
    model.compile(loss='mean_squared_error', optimizer='adam',
                  metrics= ['mae'])
   return model

【问题讨论】:

    标签: python tensorflow merge keras lstm


    【解决方案1】:

    我认为这应该可行

    # First input
    input1=Input(shape=(300,3))
    x=LSTM(50, return_sequences=True)(input1)
    x=GlobalMaxPool1D(x)
    x=Dense(n)(x)
    
    # Second Input
    input2=Input(shape=(106))
    y=Dense(n)(input2)
    
    # Merge
    merged=Concatenate([x,y])
    merged=Dense(1)(merged)
    
    # Define model with two inputs
    model=Model(inputs=[input1,input2],outputs=merged)
    

    两个模型在合并之前应该有相同的输出空间。然后,你可以传递一个输入列表,Keras 会在适当的地方传递它

    model.fit([X1,X2],....)
    

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2019-02-18
      • 2018-09-26
      • 1970-01-01
      相关资源
      最近更新 更多