我的理解是,对于某些类型的 seq2seq 模型,你训练一个编码器和一个解码器,然后你将编码器放在一边,只使用解码器进行预测步骤。例如来自 Uber 的这个 seq2seq 时间序列预测模型:
现在我正在尝试在 Keras 中实现一个 to 版本。
这是香草 LSTM 的 Keras 代码:
# define model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# train model
model.fit(X, y, epochs=200, verbose=0)
# predict
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
这是堆叠 LSTM 模型的 Keras 代码:
# define model
model = Sequential()
model.add(LSTM(50, activation='relu', return_sequences=True, input_shape=(n_steps, n_features)))
model.add(LSTM(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# train model
model.fit(X, y, epochs=200, verbose=0)
# predict
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
这是编码器-解码器模型的 Keras 代码:
# define model
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_steps_in, n_features)))
model.add(RepeatVector(n_steps_out))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(optimizer='adam', loss='mse')
# train model
model.fit(X, y, epochs=100, verbose=0)
# predict
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps_in, n_features))
yhat = model.predict(x_input, verbose=0)
问题是,我看不出编码器-解码器代码与香草和堆叠 LSTM 之间有太大区别。特别是,我没有看到我们如何在预测步骤中仅使用解码器,以及 Keras 中的哪个变量或方法对应于我们将用作预测新时间序列的输入的嵌入?
如何使用 Keras 实现类似于插图中的模型的代码?