Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何改变LSTM模型中的预测范围?_Python_Tensorflow_Keras_Lstm - Fatal编程技术网

Python 如何改变LSTM模型中的预测范围?

Python 如何改变LSTM模型中的预测范围?,python,tensorflow,keras,lstm,Python,Tensorflow,Keras,Lstm,我有下面的模型来预测价格时间序列。它会为未来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_s

我有下面的模型来预测价格时间序列。它会为未来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\u shape=(1,look\u back)
更改为
input\u shape=(5,look\u 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)
完整示例:

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)

谢谢,你能告诉我在我的例子中应该如何使用这个函数吗?请看更新
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)