如何更改 LSTM 模型中的预测范围?
How to change the forecast horizon in LSTM model?
我有以下模型来预测价格时间序列。它会提前 1 天生成预测。因此,规划范围为 1.
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(trainX, trainY, epochs=10, batch_size=1, verbose=2)
将此模型更改为提前 5 天预测(即计划范围 -> 5)的正确方法是什么。
我是否应该将 input_shape=(1, look_back)
更改为 input_shape=(5, look_back)
,并将 trainY
更改为 trainX
中每行包含 5 个点?还是更诡异?
尝试使用这个函数:
def univariate_data(dataset, start_index, end_index, history_size,
target_size, single_step=False):
data, labels = [], []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = np.arange(i-history_size, i)
data.append(np.reshape(dataset[indices], (history_size, 1)))
if single_step:
labels.append(dataset[i + target_size])
else:
labels.append(dataset[i:i + target_size])
return np.array(data), np.array(labels)
trainX, trainY = univariate_data(X, 0, len(X) - look_ahead, look_back, look_ahead)
完整示例:
import tensorflow as tf
import numpy as np
look_back = 5
look_ahead = 5
X = np.random.rand(100)
y = np.random.rand(100)
def univariate_data(dataset, start_index, end_index, history_size,
target_size, single_step=False):
data, labels = [], []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = np.arange(i-history_size, i)
data.append(np.reshape(dataset[indices], (history_size, 1)))
if single_step:
labels.append(dataset[i + target_size])
else:
labels.append(dataset[i:i + target_size])
return np.array(data), np.array(labels)
trainX, trainY = univariate_data(X, 0, len(X) - look_ahead, look_back, look_ahead)
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(4, input_shape=trainX.shape[1:],
return_sequences=True))
model.add(tf.keras.layers.Dense(1))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(trainX, trainY, epochs=10, batch_size=1, verbose=2)
我有以下模型来预测价格时间序列。它会提前 1 天生成预测。因此,规划范围为 1.
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(trainX, trainY, epochs=10, batch_size=1, verbose=2)
将此模型更改为提前 5 天预测(即计划范围 -> 5)的正确方法是什么。
我是否应该将 input_shape=(1, look_back)
更改为 input_shape=(5, look_back)
,并将 trainY
更改为 trainX
中每行包含 5 个点?还是更诡异?
尝试使用这个函数:
def univariate_data(dataset, start_index, end_index, history_size,
target_size, single_step=False):
data, labels = [], []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = np.arange(i-history_size, i)
data.append(np.reshape(dataset[indices], (history_size, 1)))
if single_step:
labels.append(dataset[i + target_size])
else:
labels.append(dataset[i:i + target_size])
return np.array(data), np.array(labels)
trainX, trainY = univariate_data(X, 0, len(X) - look_ahead, look_back, look_ahead)
完整示例:
import tensorflow as tf
import numpy as np
look_back = 5
look_ahead = 5
X = np.random.rand(100)
y = np.random.rand(100)
def univariate_data(dataset, start_index, end_index, history_size,
target_size, single_step=False):
data, labels = [], []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = np.arange(i-history_size, i)
data.append(np.reshape(dataset[indices], (history_size, 1)))
if single_step:
labels.append(dataset[i + target_size])
else:
labels.append(dataset[i:i + target_size])
return np.array(data), np.array(labels)
trainX, trainY = univariate_data(X, 0, len(X) - look_ahead, look_back, look_ahead)
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(4, input_shape=trainX.shape[1:],
return_sequences=True))
model.add(tf.keras.layers.Dense(1))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(trainX, trainY, epochs=10, batch_size=1, verbose=2)