我在为 Keras 上的 RNN 准备输入数据时遇到了麻烦。
目前,我的训练数据维度是:(6752, 600, 13)
X_train
和 Y_train
都在这个维度。我想准备这些数据以输入
SimpleRNN
在 Keras 上。假设我们正在经历从第 0 步到第 599 步的时间步长。
假设我想使用
input_length = 5
,这意味着我想使用最近的 5 个输入。 (例如步骤 #10、#11、#12、#13、#14 @ 步骤 #14)。我应该如何 reshape
X_train
?应该是
(6752, 5, 600, 13)
或者应该是(6752, 600, 5, 13)
?应该是什么形状
Y_train
在吗?应该是
(6752, 600, 13)
或 (6752, 1, 600, 13)
或 (6752, 600, 1, 13)
?
最佳答案
如果您只想使用最近的 5 个输入来预测输出,则无需提供任何训练样本的完整 600 个时间步。我的建议是通过以下方式传递训练数据:
t=0 t=1 t=2 t=3 t=4 t=5 ... t=598 t=599
sample0 |---------------------|
sample0 |---------------------|
sample0 |-----------------
...
sample0 ----|
sample0 ----------|
sample1 |---------------------|
sample1 |---------------------|
sample1 |-----------------
....
....
sample6751 ----|
sample6751 ----------|
训练序列的总数将总计为
(600 - 4) * 6752 = 4024192 # (nb_timesteps - discarded_tailing_timesteps) * nb_samples
每个训练序列由 5 个时间步长组成。在每个序列的每个时间步,您都会传递特征向量的所有 13 个元素。随后,训练数据的形状将是 (4024192, 5, 13)。
这个循环可以 reshape 你的数据:
input = np.random.rand(6752,600,13)
nb_timesteps = 5
flag = 0
for sample in range(input.shape[0]):
tmp = np.array([input[sample,i:i+nb_timesteps,:] for i in range(input.shape[1] - nb_timesteps + 1)])
if flag==0:
new_input = tmp
flag = 1
else:
new_input = np.concatenate((new_input,tmp))
关于tensorflow - Keras : How should I prepare input data for RNN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36992855/