简体   繁体   中英

Python Keras Multiple Input Layers - How to Concatenate/Merge?

In python, I am trying to build a neural network model using Sequential in keras to perform binary classification. Note that X is a numpy array of time series data 59x1000x3 (samples x timesteps x features) and D is a numpy array of 59x100 (samples x auxillary features). I want to pass the time series through an lstm layer, and then augment at a later layer with the accompanying features (ie concatenate two layers).

My code to fit the model is below:

def fit_model(X, y, D, neurons, batch_size, nb_epoch):
    model = Sequential()
    model.add(LSTM(units = neurons, input_shape = (X.shape[1], X.shape[2]))
    model.add(Dropout(0.1))
    model.add(Dense(10))
    input1 = Sequential()
    d = K.variable(D)
    d_input = Input(tensor=d)
    input1.add(InputLayer(input_tensor=d_input))
    input1.add(Dropout(0.1))
    input1.add(Dense(10))
    final_model = Sequential()
    merged = Concatenate([model, input1])
    final_model.add(merged)
    final_model.add(Dense(1, activation='sigmoid'))
    final_model.compile(loss = 'binary_crossentropy', optimizer = 'adam')
    final_model.fit(X, y, batch_size = batch_size, epochs = nb_epoch)
    return final_model

I get the following error:

ValueError: A Concatenate layer should be called on a list of at least 2 inputs

I tried using various permutations of merge/concatenate/the functional api/not the functional api, but I keep landing with some sort of error. I've seen answers using Merge from keras.engine.topology. However, it seems to now be deprecated. Any suggestions to fix the error when using Sequential or how to convert the code to the functional API would be appreciated. Thanks.

You are incorrectly passing a Model and an Input as parameters of the Concatenate layer:

merged = Concatenate([model, input1])

Try passing another Input layer instead:

merged = Concatenate([input1, input2])

Concatenate takes only one argument which is axis of concatenation. To call the layer on another tensors you should call concatenate layer object on list of tensors.

merged = Concatenate(axis=-1)([model, input1])

This should fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM