2
votes

The input shape of an LSTM layer is (batch_size, timesteps, features). I currently have an input that looks like this:

[0,1,2,3,4,5,6,7,8,9,10]

I use my code to reshape the data so it looks like this

[
[0,1,2,3],
[1,2,3,4],
[2,3,4,5],
[3,4,5,6],
[4,5,6,7],
[5,6,7,8],
[6,7,8,9],
[5,7,8,10]
]

However, it takes a lot of time to reshape this data in Python. Is there some way for the LSTM model in Keras/Tensorflow to learn data purely from [0,1,2,3,4,5,6,7,8,9,10] where I define the timesteps to be 4 in the Keras API. I tried to look for such an option but didn't find any.

This is what I have been using:

numberOfTimesteps = 240
i = 0
lstmFeatures = pd.DataFrame()
while i < features.transpose().shape[0] - numberOfTimesteps:
    temp = features.transpose().iloc[i:i+numberOfTimesteps,:]
    lstmFeatures = lstmFeatures.append(temp)
    if i%100 == 0:
        print(i,end=',')
    i = i + 1        

Does anyone have a better idea of how to reshape or how to use Keras?

1

1 Answers

0
votes

You can use tf.gather

import tensorflow as tf

my_data = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
to_take_inds = tf.range(4)[None, :]
to_take_inds = to_take_inds + tf.range(7)[:, None]

reshaped = tf.gather(my_data, to_take_inds)
with tf.Session() as sess:
    print(sess.run(reshaped))

prints

[[ 1  2  3  4]
 [ 2  3  4  5]
 [ 3  4  5  6]
 [ 4  5  6  7]
 [ 5  6  7  8]
 [ 6  7  8  9]
 [ 7  8  9 10]]