Keras中的Seq2Seq用于预测矢量序列的下一个元素

问题描述 投票:0回答:1

劳驾。我是神经网络的初学者。我必须在Keras中使用seq2seq模型来预测矢量x [0],x [1],...,x [N-1]序列的下一个元素x [N]。

该矢量序列具有可变长度,即,N不是固定数。序列的元素(向量)具有固定长度m。也就是说,我的数据x [0],x [1],...,x [N-1]具有这种形式

[x [0,0],x [0,1],...,x [0,m-1]],[x [1,0],x [1,1],...,x [ 1,m-1]],...,[x [N-1,0],x [N-1,1],...,x [N-1,m-1]]

我必须预测下一个向量x [N] = [x [N,0],x [N,1],...,x [N,m-1]]。

我是否正确理解我需要的模型是用Keras中的代码段描述的?

data_dim = m

model = Sequential()
model.add(LSTM(1, input_shape=(None, data_dim)))

非常感谢你提前!

python keras seq2seq
1个回答
0
投票

要构建seq-to-seq模型,您需要使用Keras的功能API而不是构建Sequential()模型。在Keras博客上有一个很好的例子:https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html

from keras.models import Model
from keras.layers import Input, LSTM, Dense

encoder_inputs = Input(shape=(None, num_encoder_tokens))
encoder = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
decoder_inputs = Input(shape=(None, num_decoder_tokens))
decoder_lstm = LSTM(latent_dim, 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)
© www.soinside.com 2019 - 2024. All rights reserved.