Keras:我应该如何为 RNN 准备输入数据?
Keras : How should I prepare input data for RNN?
我在为 Keras 上的 RNN 准备输入数据时遇到问题。
目前我的训练数据维度是:(6752, 600, 13)
- 6752:训练数据数量
- 600:时间步数
- 13:特征向量的大小(向量为浮点数)
X_train
和Y_train
都在这个维度
我想准备将此数据输入 Keras 上的 SimpleRNN
。
假设我们正在经历时间步长,从步骤#0 到步骤#599。
假设我想使用 input_length = 5
,这意味着我想使用最近的 5 个输入。 (例如步骤 #10、#11、#12、#13、#14 @ 步骤 #14)。
我应该如何整形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).
这个循环可以重塑您的数据:
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))
这是为 LSTN/RNN 创建 3D 数据的快速过程,无需循环并涉及此简单函数
def create_windows(data, window_shape, step = 1, start_id = None, end_id = None):
data = np.asarray(data)
data = data.reshape(-1,1) if np.prod(data.shape) == max(data.shape) else data
start_id = 0 if start_id is None else start_id
end_id = data.shape[0] if end_id is None else end_id
data = data[int(start_id):int(end_id),:]
window_shape = (int(window_shape), data.shape[-1])
step = (int(step),) * data.ndim
slices = tuple(slice(None, None, st) for st in step)
indexing_strides = data[slices].strides
win_indices_shape = ((np.array(data.shape) - window_shape) // step) + 1
new_shape = tuple(list(win_indices_shape) + list(window_shape))
strides = tuple(list(indexing_strides) + list(data.strides))
window_data = np.lib.stride_tricks.as_strided(data, shape=new_shape, strides=strides)
return np.squeeze(window_data, 1)
从这个示例数据开始:
n_sample = 2000
n_feat_inp = 6
n_feat_out = 1
X = np.asarray([np.arange(n_sample)]*n_feat_inp).T # (n_sample, n_feat_inp)
y = np.asarray([np.arange(n_sample)]*n_feat_out).T # (n_sample, n_feat_out)
如果我们想要一步提前预测
look_back = 5
look_ahead = 1
X_seq = create_windows(X, window_shape = look_back, end_id = -look_ahead)
# X_seq.shape --> (n_sample - look_back, look_back, n_feat_inp)
y_seq = create_windows(y, window_shape = look_ahead, start_id = look_back)
# y_seq.shape --> (n_sample - look_back, look_ahead, n_feat_out)
生成的数据示例:
X_seq[0]: [[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4]]
y_seq[0]: [[5]]
如果我们想要多步提前预测
look_back = 5
look_ahead = 3
X_seq = create_windows(X, window_shape = look_back, end_id = -look_ahead)
# X_seq.shape --> (n_sample - look_back - look_ahead + 1, look_back, n_feat_inp)
y_seq = create_windows(y, window_shape = look_ahead, start_id = look_back)
# y_seq.shape --> (n_sample - look_back - look_ahead + 1, look_ahead, n_feat_out)
生成的数据示例:
X_seq[0]: [[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4]]
y_seq[0]: [[5],
[6],
[7]]
我在为 Keras 上的 RNN 准备输入数据时遇到问题。
目前我的训练数据维度是:(6752, 600, 13)
- 6752:训练数据数量
- 600:时间步数
- 13:特征向量的大小(向量为浮点数)
X_train
和Y_train
都在这个维度
我想准备将此数据输入 Keras 上的 SimpleRNN
。
假设我们正在经历时间步长,从步骤#0 到步骤#599。
假设我想使用 input_length = 5
,这意味着我想使用最近的 5 个输入。 (例如步骤 #10、#11、#12、#13、#14 @ 步骤 #14)。
我应该如何整形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).
这个循环可以重塑您的数据:
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))
这是为 LSTN/RNN 创建 3D 数据的快速过程,无需循环并涉及此简单函数
def create_windows(data, window_shape, step = 1, start_id = None, end_id = None):
data = np.asarray(data)
data = data.reshape(-1,1) if np.prod(data.shape) == max(data.shape) else data
start_id = 0 if start_id is None else start_id
end_id = data.shape[0] if end_id is None else end_id
data = data[int(start_id):int(end_id),:]
window_shape = (int(window_shape), data.shape[-1])
step = (int(step),) * data.ndim
slices = tuple(slice(None, None, st) for st in step)
indexing_strides = data[slices].strides
win_indices_shape = ((np.array(data.shape) - window_shape) // step) + 1
new_shape = tuple(list(win_indices_shape) + list(window_shape))
strides = tuple(list(indexing_strides) + list(data.strides))
window_data = np.lib.stride_tricks.as_strided(data, shape=new_shape, strides=strides)
return np.squeeze(window_data, 1)
从这个示例数据开始:
n_sample = 2000
n_feat_inp = 6
n_feat_out = 1
X = np.asarray([np.arange(n_sample)]*n_feat_inp).T # (n_sample, n_feat_inp)
y = np.asarray([np.arange(n_sample)]*n_feat_out).T # (n_sample, n_feat_out)
如果我们想要一步提前预测
look_back = 5
look_ahead = 1
X_seq = create_windows(X, window_shape = look_back, end_id = -look_ahead)
# X_seq.shape --> (n_sample - look_back, look_back, n_feat_inp)
y_seq = create_windows(y, window_shape = look_ahead, start_id = look_back)
# y_seq.shape --> (n_sample - look_back, look_ahead, n_feat_out)
生成的数据示例:
X_seq[0]: [[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4]]
y_seq[0]: [[5]]
如果我们想要多步提前预测
look_back = 5
look_ahead = 3
X_seq = create_windows(X, window_shape = look_back, end_id = -look_ahead)
# X_seq.shape --> (n_sample - look_back - look_ahead + 1, look_back, n_feat_inp)
y_seq = create_windows(y, window_shape = look_ahead, start_id = look_back)
# y_seq.shape --> (n_sample - look_back - look_ahead + 1, look_ahead, n_feat_out)
生成的数据示例:
X_seq[0]: [[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4]]
y_seq[0]: [[5],
[6],
[7]]