首页 文章

Keras:我应该如何为RNN准备输入数据?

提问于
浏览
12

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

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

  • 6752:训练数据的数量

  • 600:时间步数

  • 13:特征向量的大小(向量在浮点数)

X_trainY_train 都属于这个维度 .

我想把这些数据准备好送到Keras的 SimpleRNN . 假设我们说我想使用 input_length = 5 ,这意味着我想使用最近的5个输入 . (例如步骤#10,#11,#12,#13,#14 @步骤#14) .

我应该如何重塑 X_train

应该是 (6752, 5, 600, 13) 还是 (6752, 600, 5, 13)

什么形状应该_2446738_?

它应该是 (6752, 600, 13) 还是 (6752, 1, 600, 13)(6752, 600, 1, 13)

1 回答

  • 14

    如果您只想使用最近的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))
    

相关问题