【问题标题】:How to handle two inputs for two neural networks如何处理两个神经网络的两个输入
【发布时间】:2019-07-26 08:27:13
【问题描述】:

我想在 Keras 中构建这样的架构。

这里 1D CNN(扁平化)的输出将作为 ANN 的输入,而其他一些额外的输入也将提供给 ANN。因此,整个模型将在两个位置进行输入。如何在 Keras 中处理这个问题?在model.fit 函数中,我们通常使用一个输入。我在 Tensorflow 后端使用 Keras 并使用 Anaconda Python 3.7.3。

(这里的ANN表示普通神经网络)

【问题讨论】:

    标签: python tensorflow keras neural-network anaconda


    【解决方案1】:

    Keras 完全支持multi-input models。

    您的方法是使用fucntional API 并在模型中放置两个Input 层。使用功能 API 构建架构的其余部分,然后使用两个输入定义 Model。在训练期间,您需要记住在model.fit() 中输入两个输入。

    在你的情况下,它看起来像这样:

    from keras.layers import Input, Conv1D, Flatten, Concatenate, Dense
    from keras.models import Model
    
    input1 = Input(shape=(...))  # add the shape of your input (excluding batch dimension)
    
    conv = Conv1D(...)(input1)   # add convolution parameters (e.g. filters, kernel, strides)
    flat = Flatten()(conv)
    
    input2 = Input(shape=(...))  # add the shape of your secondary input
    
    ann_input = Concatenate()([flat, input2])  # concatenate the two inputs of the ANN
    ann = Dense(2)(ann_input)  # 2 because you are doing binary classification
    
    model = Model(inputs=[input1, input2], outputs=[ann])
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    
    # assuming x1 and x2 are numpy arrays with the data for 'input1' and 'input2' 
    # respectively and y is a numpy array containing the labels
    
    model.fit([x1, x2], y)
    

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多