【发布时间】:2020-03-10 04:43:53
【问题描述】:
我有一个简单的单变量时间序列预测问题,我的输入是10, 20, 30, 40, 50, 60, 70, 80, 90。我希望模型预测接下来的 3 个值。即输出应该是100, 110, 120。
我使用堆叠 LSTM 来做到这一点。我的代码如下。
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM, Dense
def split_sequence(sequence, n_steps_in, n_steps_out):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps_in
out_end_ix = end_ix + n_steps_out
# check if we are beyond the sequence
if out_end_ix > len(sequence):
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:out_end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps_in, n_steps_out = 5, 3
# split into samples
X, y = split_sequence(raw_seq, n_steps_in, n_steps_out)
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(100, activation='relu'))
model.add(Dense(n_steps_out))
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, epochs=50, verbose=0)
# demonstrate prediction
x_input = array([50, 60, 70, 80, 90])
x_input = x_input.reshape((1, n_steps_in, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
由于我使用的是一个非常简单的示例,因此我希望模型能够准确预测。但是,我得到的输出很糟糕 ([[135.52011 151.59491 175.79674]])。
我想知道在 LSTM 中是否有更有效的方法来做到这一点?
如果需要,我很乐意提供更多详细信息。
【问题讨论】: