Sequence a (numpy) array (Tensorflow Keras Numpy(

Viewed 125

I am having some questions about the sequencing process for LSTM layers etc.

In the keras documentation is an example of how to sequence an array. https://keras.io/examples/timeseries/timeseries_anomaly_detection/

TIME_STEPS = 288
# Generated training sequences for use in the model.
def create_sequences(values, time_steps=TIME_STEPS):
    output = []
    for i in range(len(values) - time_steps + 1):
        output.append(values[i : (i + time_steps)])
    return np.stack(output)
x_train = create_sequences(df_training_value.values)
print("Training input shape: ", x_train.shape)

But when I sequence an array I do it differently like this.

seq_length = 4
sequence = some_array[index-seq_length+1: index+1]

And when I test it with python code it shows my code returns the correct sequence. Yet all tutorials do it this way? What am I missing?

Python test code:

python3
>>> some_array = [0, 1, 2, 3, 4, 5]
>>> seq_length = 2
>>> index = 4
# so the sequenced array should be [3, 4]
>>> some_array[index-seq_length+1: index+1]
[3, 4] # the wanted sequence.
# and using the tutorial methods.
>>> some_array[index-seq_length: index]
[2, 3] # it is missing the index value itself.
0 Answers
Related