tensorflow - Keras : How should I prepare input data for RNN?

标签 tensorflow keras deep-learning lstm recurrent-neural-network

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

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

  • 6752:训练数据数
  • 600:时间步数
  • 13:特征向量的大小(向量为浮点数)
  • X_trainY_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/

    相关文章:

    python - 使用 Tensorflow 中具有多个 .csv 的大型数据集的时间序列数据的 LSTM 输入管道

    python - TensorFlow 上的 MLP 对训练后的所有观察结果给出相同的预测

    python - 将自动编码器权重绑定(bind)到密集的 Keras 层中

    tensorflow - Keras Batchnormalization,训练数据集的训练和评估结果不同

    python - 在 Google Colab 中运行 DQN pygame 脚本时出错

    optimization - Pytorch - backward() 函数应该在 epoch 或 batch 的循环中吗?

    python - Tensorflow 梯度返回 null

    tensorflow - RASA 初始化错误 : tensorflow. python.framework.errors_impl.InvalidArgumentError:断言失败:[0] [Op:Assert] 名称:EagerVariableNameReuse

    python - ValueError : `validation_split` is only supported for Tensors or NumPy arrays, 发现 : (keras. preprocessing.sequence.TimeseriesGenerator 对象)

    neural-network - 如何按照全局步骤在 Keras 中实现指数衰减学习率