簡體   English   中英

如何處理兩個神經網絡的兩個輸入

[英]How to handle two inputs for two neural networks

我想在Keras中建立這樣的架構。

在這里,一維CNN(展平)的輸出將作為ANN的輸入,其他一些附加輸入也將提供給ANN。 因此,整個模型將在兩個位置進行輸入。 如何在Keras中處理此問題? model.fit函數中,我們通常使用一個輸入。 我在Tensorflow后端使用Keras並使用Anaconda Python 3.7.3。

(這里的ANN表示正常的神經網絡)

Keras完全支持多輸入模型

方法是使用功能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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM