21

我在为 Keras 上的 RNN 准备输入数据时遇到了麻烦。

目前,我的训练数据维度是:(6752, 600, 13)

  • 6752:训练数据的数量
  • 600:时间步数
  • 13:特征向量的大小(向量是浮点数)

X_train并且Y_train都在这个维度。

我想准备这些数据以输入到SimpleRNNKeras 上。假设我们正在经历从第 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)or(6752, 1, 600, 13)还是(6752, 600, 1, 13)

4

2 回答 2

21

如果您只想使用最近的 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))
于 2016-05-03T16:38:00.287 回答
7

这是为 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]]

如果我们想要MULTI超前预测

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]]
于 2020-06-25T07:58:25.647 回答