在Python中使用Keras构建多对多LSTM模型的正确方法是什么?

1 人关注

我想做一个3序列多对多的LSTM模型,但我对它在Keras中的实现感到困惑。我在网上搜索了多对多模型的例子,但每个网站都给出了不同的方法。这让我更加困惑了。那些正确的方法是什么?我想要一个这样的模型。

我发现的各种方法中,有一些是

  • Using encoder, decoder
  • from keras.layers import RepeatVector
    from keras.layers import TimeDistributed
    model = Sequential()
    # encoder layer
    model.add(LSTM(100, activation='relu', input_shape=(3, 1)))
    # repeat vector
    model.add(RepeatVector(3))
    # decoder layer
    model.add(LSTM(100, activation='relu', return_sequences=True))
    model.add(TimeDistributed(Dense(1)))
    model.compile(optimizer='adam', loss='mse')
    
  • Another with encoder, decoder
  • from keras.models import Model
    from keras.layers import Input, LSTM, Dense
    encoder_inputs = Input(shape=(None, 1))
    encoder = LSTM(100, return_state=True)
    encoder_outputs, state_h, state_c = encoder(encoder_inputs)
    encoder_states = [state_h, state_c]
    decoder_inputs = Input(shape=(None, 1))
    decoder_lstm = LSTM(100, return_sequences=True, return_state=True)
    decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
                                         initial_state=encoder_states)
    decoder_dense = Dense(num_decoder_tokens, activation='softmax')
    decoder_outputs = decoder_dense(decoder_outputs)
    model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
    
    model = Sequential()
    model.add(LSTM(100,input_shape=(3,1),return_sequences=True))
    model.add(TimeDistributed(Dense(2)))
    model.compile(optimizer='adam', loss='mse')
    
    model = Sequential()
    model.add(LSTM(100,input_shape=(3,1),return_sequences=True))
    model.compile(optimizer='adam', loss='mse')