Reputation: 1315
I read this article about LSTM:
https://machinelearningmastery.com/how-to-develop-lstm-models-for-time-series-forecasting/
The first basic example is about "Vanilla LSTM": predict next time series
Where the input = [10, 20, 30, 40, 50, 60, 70, 80, 90]
In the article the writer split the input (sequence) into matrix:
X, y
10, 20, 30 40
20, 30, 40 50
30, 40, 50 60
...
I can't understand why the input need to reshape:
reshape from [samples, timesteps] into [samples, timesteps, features]
1. Why do we need this ?
In addition if my input is like (the basic example + ID columns):
ID X, y
1 10, 20, 30 40
1 20, 30, 40 50
2 30, 40, 50 60
2 40, 50, 60, 70
...
2. how we reshapes it ? what we will be the new dimensional ?
Upvotes: 0
Views: 2008
Reputation:
The three dimensional feature input input of an LSTM can be thought of as (# of groups, time steps in each group, # of columns or types of variables). For example (100,10,1) can be though of as 100 groups, and within each group there are 10 rows and one column. The one column menas there is only one type of variable or one x.
Upvotes: 0
Reputation: 398
I think this link will help you understand why.
You always have to give a three-dimensional array as an input to your LSTM network. Where the first dimension represents the batch size, the second dimension represents the number of time-steps you are feeding a sequence. And the third dimension represents the number of units in one input sequence. For example, input shape looks like (batch_size, time_steps, seq_len)
Lets take your example sequence: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Once we do split_sequence as stated in your article, we get a 2 dimensional feature vector X of shape (6, 3). Where 6 is number of samples and 3 is number of steps.
but given that the model only takes in a 3-D vector we must reshape our 2-d vector to 3-d.
so from (6, 3) --> (6, 3, 1).
To answer your second question, you can simply reshape your 2-d feature vector X by doing the following:
# Given that X is a numpy array
samples = X.shape[0]
steps = X.shape[1]
X = X.reshape(samples, steps, 1)
Upvotes: 2
Reputation: 1825
Not sure where the ID comes from, but for LSTM network in Keras you need your input to be 3 dimensional.
Originally you have 2 dimensional matrix as an input where each row is one timestamp so
[samples, timesteps]
.
But since the input is expected to be 3 dimensional you reshape as [samples, timesteps, 1]
. Here 1
indicates number of features, or variables you have in your data. Since this is a univariate time series (you have sequence of just 1 variable) n_features is 1.
This can easily be done by np_array.reshape(np_array.shape[0], np_array.shape[1], 1)
Upvotes: 2