Numpy 如何为时间序列预测模型(LSTM)构建tf.data管道

Numpy 如何为时间序列预测模型(LSTM)构建tf.data管道,numpy,tensorflow,time-series,prediction,tensorflow-datasets,Numpy,Tensorflow,Time Series,Prediction,Tensorflow Datasets,我的目标是创建一个用于时间序列预测的LSTM模型(带有tensorflow/keras) 为了训练模型,需要准备时间序列数据(2个特征,几年的小时间隔值) 其想法是使用一定数量的输入值(例如200)来模拟一定数量的输出值(例如24) 虽然使用pandas和numpy进行重塑非常简单,但问题是如何使用tf.data.Dataset函数来更有效地实现这一点 因此,据我所知,keras LSTM模型请求具有以下形状的输入:(nr_的样本,nr_的时间步,特征) 这意味着在我的例子中它是:(X,5,2)

我的目标是创建一个用于时间序列预测的LSTM模型(带有tensorflow/keras)

为了训练模型,需要准备时间序列数据(2个特征,几年的小时间隔值)

其想法是使用一定数量的输入值(例如200)来模拟一定数量的输出值(例如24)

虽然使用pandas和numpy进行重塑非常简单,但问题是如何使用tf.data.Dataset函数来更有效地实现这一点

因此,据我所知,keras LSTM模型请求具有以下形状的输入:(nr_的样本,nr_的时间步,特征)

这意味着在我的例子中它是:(X,5,2)

转换代码示例:

创建数据 重塑数据: 之后,数据被分成训练集和测试集,并在使用之前通过MinMaxScaler进行缩放

为了提高效率(至少我这么认为),我想知道如何使用tensorflow数据集库。使用“窗口”功能可以轻松地移动数据

你有什么建议吗?这值得付出努力吗

其他问题: 此外,我还有一些更一般的问题,因为我对LSTM模型没有太多经验:

  • 包含更多与时间相关的功能(如月、年、小时等)是否常见/有用
  • 与绝对值相比,包含相对值更好吗?(参数随时间的变化)
  • 对于可调模型参数与数据点数量的比率,是否有一些一般的经验法则
import numpy as np

f_1 = np.linspace(1.4, 2000,num=2000, endpoint=True, retstep=False, dtype=None, axis=0)
f_2 = np.linspace(10, 20000,num=2000, endpoint=True, retstep=False, dtype=None, axis=0)

d = np.vstack((f_1, f_2)).T

time_step_back = 200
time_step_forward=2

row_nr_X=np.arange(0, d.shape[0]-time_step_back-time_step_forward)
row_nr_y=np.arange(time_step_back, d.shape[0]-time_step_forward)

nr_features_X = d.shape[1]
nr_features_y = 1 # has to be defined

row_select_X = np.vstack(([[row_nr_X+i for i in np.arange(0,time_step_back)]])).T
row_select_y = np.vstack(([[row_nr_y+i for i in np.arange(0,time_step_forward)]])).T

# create empty input matrix
X = np.zeros((row_select_X.shape[0], time_step_back, nr_features_X))

# fill input matrix with correct values
for f in np.arange(0, nr_features_X):
    X[:,:,f] = d[row_select_X,f]

# create empty result matrix
y = np.zeros((row_select_y.shape[0], time_step_forward, nr_features_y))

# fill result matrix with correct values
for f in np.arange(0,nr_features_y):
    y[:,:,f] = d[row_select_y,f]

print(X[1,:,0])
print(y[1,:,0])

print(X.shape)
print(y.shape)