Keras : How should I prepare input data for RNN?

£可爱£侵袭症+ 提交于 2019-11-30 10:49:56

问题


I'm having trouble with preparing input data for RNN on Keras.

Currently, my training data dimension is: (6752, 600, 13)

  • 6752: number of training data
  • 600: number of time steps
  • 13: size of feature vectors (the vector is in float)

X_train and Y_train are both in this dimension.

I want to prepare this data to be fed into SimpleRNN on Keras. Suppose that we're going through time steps, from step #0 to step #599. Let's say I want to use input_length = 5, which means that I want to use recent 5 inputs. (e.g. step #10, #11,#12,#13,#14 @ step #14).

How should I reshape X_train?

should it be (6752, 5, 600, 13) or should it be (6752, 600, 5, 13)?

And what shape should Y_train be in?

Should it be (6752, 600, 13) or (6752, 1, 600, 13) or (6752, 600, 1, 13)?


回答1:


If you only want to predict the output using the most recent 5 inputs, there is no need to ever provide the full 600 time steps of any training sample. My suggestion would be to pass the training data in the following manner:

             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                                      ----------|

The total number of training sequences will sum up to

(600 - 4) * 6752 = 4024192    # (nb_timesteps - discarded_tailing_timesteps) * nb_samples

Each training sequence consists of 5 time steps. At each time step of every sequence you pass all 13 elements of the feature vector. Subsequently, the shape of the training data will be (4024192, 5, 13).

This loop can reshape your data:

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))


来源:https://stackoverflow.com/questions/36992855/keras-how-should-i-prepare-input-data-for-rnn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!