[英]How to handle two inputs for two neural networks
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.