简体   繁体   中英

How to merge multiple LSTM models and to fit them

I am trying to merge two LSTM Sequential models, but without success. I am using the Concatenate() method from tensorflow.keras.layers . Whenever I try to concatenate the models it says ValueError: A Concatenate layer should be called on a list of at least 2 inputs which doesn't make sense, because the two models are passed in the list.

This is the code that I have for the models:

# Initialising the LSTM
regressor = Sequential()

# Adding the first LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))
regressor.add(Dropout(0.2))

# Adding a second LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

# Adding a third LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

# Adding a fourth LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))

regressor.add(Dense(units = 1))

lstm_model = Sequential()


lstm_model.add(LSTM(units = 4, activation = 'relu', input_shape = (X_train.shape[1], 1)))

# returns a sequence of vectors of dimension 4

# Adding the output layer
lstm_model.add(Dense(units = 1))


merge = Concatenate([regressor, lstm_model])
hidden = Dense(1, activation = 'sigmoid')
conc_model = Sequential()
conc_model.add(merge)
conc_model.add(hidden)
conc_model.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics=['mae', 'acc'])

history = conc_model.fit(X_train, y_train, validation_split=0.1, epochs = 50, batch_size = 32, verbose=1, shuffle=False)

How to concatenate and fit those models? I don't understand what I am doing wrong.

you are using Sequential and concatenate together, this is wrong.

you should used Input and Model keyword to define model.

inp_layer1 = Input(shape=(1,))
m1 = Dense(1) (inp_layer1)

inp_layer2 = Input(shape=(1,))
m2 = Dense(1) (inp_layer2)

m3 = concatenate([m1,m2])
model = Model(inputs=[inp_layer1,inp_layer2],outputs=m3)

The problem is cause by your fit input, it should be a list of two inputs instead of one input, because your conc_model require two inputs

Check the docs of fit function , it says: Input data could be a Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs)

So you need to split the X_train into list of two array, first one for regressor second for lstm_model

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