如何合并多个 LSTM 模型并进行拟合
How to merge multiple LSTM models and to fit them
我正在尝试合并两个 LSTM 序列模型,但没有成功。我正在使用 tensorflow.keras.layers
中的 Concatenate()
方法。每当我尝试连接模型时,它都会显示 ValueError: A Concatenate layer should be called on a list of at least 2 inputs
这没有意义,因为这两个模型在列表中传递。
这是我的模型代码:
# 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)
如何连接和拟合这些模型?我不明白我做错了什么。
你同时使用了 Sequential 和 concatenate,这是错误的。
您应该使用 Input 和 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)
问题是你的fit输入引起的,它应该是两个输入的列表而不是一个输入,因为你的conc_model
需要两个输入
检查 docs of fit function,它说:输入数据可以是 Numpy 数组(或类似数组)或数组列表(如果模型有多个输入)
因此您需要将 X_train 拆分为两个数组的列表,第一个用于 regressor
,第二个用于 lstm_model
我正在尝试合并两个 LSTM 序列模型,但没有成功。我正在使用 tensorflow.keras.layers
中的 Concatenate()
方法。每当我尝试连接模型时,它都会显示 ValueError: A Concatenate layer should be called on a list of at least 2 inputs
这没有意义,因为这两个模型在列表中传递。
这是我的模型代码:
# 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)
如何连接和拟合这些模型?我不明白我做错了什么。
你同时使用了 Sequential 和 concatenate,这是错误的。
您应该使用 Input 和 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)
问题是你的fit输入引起的,它应该是两个输入的列表而不是一个输入,因为你的conc_model
需要两个输入
检查 docs of fit function,它说:输入数据可以是 Numpy 数组(或类似数组)或数组列表(如果模型有多个输入)
因此您需要将 X_train 拆分为两个数组的列表,第一个用于 regressor
,第二个用于 lstm_model