简体   繁体   中英

How can I concatenate two LSTM with Keras?

There are similar questions but they are either outdated or doesn't work for my case.

This is my code:

  left = Sequential()
  left.add(LSTM(units=24,input_shape=(left_X.shape[1], left_X.shape[2])))
  left.add(Dense(1))

  right = Sequential()
  right.add(LSTM(units=24,input_shape=(right_X.shape[1], right_X.shape[2])))
  right.add(Dense(1))

  model = Sequential()
  model.add(Concatenate([left,right]))  
  model.add(Flatten())
  model.add(Dense(1, activation='linear'))

  model.compile(loss='mse',
          optimizer='adam',
          metrics=['mae'])

 history = model.fit([left_X, right_X], train_y, 
                epochs=40,
                validation_split=0.2,
                verbose=1)

It raises an Assertion Error for fit

  585             # since `Sequential` depends on `Model`.
    586             if isinstance(inputs, list):
--> 587                 assert len(inputs) == 1
    588                 inputs = inputs[0]
    589             self.build(input_shape=(None,) + inputs.shape[1:])

I solved the problem using the following code, which uses Keras functional API:

inp1 = Input(shape=(train_X_1.shape[1], train_X_1.shape[2]))
inp2 = Input(shape=(train_X_2.shape[1], train_X_2.shape[2]))
inp3 = Input(shape=(train_X_3.shape[1], train_X_3.shape[2]))

x = SimpleRNN(10)(inp1)
x = Dense(1)(x)

y = LSTM(10)(inp2)
y = Dense(1)(y)

z = LSTM(10)(inp3)
z = Dense(1)(z)

w = concatenate([x, y, z])

# u =  Dense(3)(w)
out =  Dense(1, activation='linear')(w)

model = Model(inputs=[inp1, inp2, inp3], outputs=out)

model.compile(loss='logcosh',
        optimizer='adam',
        metrics=['mae'])

history = model.fit([train_X_1, train_X_2, train_X_3], train_y, 
                epochs=20,
                validation_split=0.1,
                verbose=1)

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