How to set up Keras LSTM for time series forecasting?

I have one training batch of 600 consecutive points (x (t), y (t)) where x (t) is a 25-dimensional vector and y (t) is my target (1 dim). I would like to train the LSTM to predict how the run will continue, given a few extra x (t) [t> 600]. I tried the following model:

    model = Sequential() 
    model.add(LSTM(128, input_shape = (600,25), batch_size = 1, activation= 'tanh', return_sequences = True)) 
    model.add(Dense(1, activation='linear'))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(trainX, trainY, epochs=20 ,verbose=2) prediction

    prediction = model.predict(testX, batch_size = 1)

      

Editing works fine, but during the prediction phase I get the following error:

    Error when checking : expected lstm_46_input to have shape (1, 600, 25) but got array with shape (1, 10, 25)

      

What am I missing?

Here are my forms:

    trainX.shape = (1,600,25)
    trainY.shape = (1,600,1)
    testX.shape = (1,10,25)

      

+3


source to share


2 answers


According to Keras documentation, LSTM (or any RNN) input layers should have a shape (batch_size, timesteps, input_dim)

where your input form is

trainX.shape = (1,600,25)



So this means that for training purposes, you only transmit one information with 600 timestamps and 25 functions at a time. But I got the feeling that you actually have 600 training data , each with 25 timesteps and 1 function in one time slot . I think your input form (trainX) should be 600 x 25 x 1

. The training target (trainY) should be 600 x 1

If my assumption is correct, your test data should be in shape 10 x 25 x 1

. The first LSTM layer should be written as

    model.add(LSTM(128, input_shape = (25,1), batch_size = 1, activation= 'tanh', return_sequences = False)) 

      

+4


source


If your training data is actually (1,600.25), that means you are spinning LSTM feedback 600 times. The first entry affects the 600th entry. If that's what you want, you can use the Keras "pad_sequences" function to add nulls to the test matrix so that it has the shape (1,600,25). The network should be predicting zeros and you will need to add 590 zeros to your test.

If you only want to say that the 10 previous time lapses affect your current Y forecast, then you will want to turn your trainX into shape (590,10,25). The input line will look something like this:

model.add(LSTM(n_hid, stateful=True, return_sequences=False, batch_input_shape=(1,nTS,x_train.shape[2])))

      



The processing to get it in the form you want could be something like this:

def formatTS(XX, yy, window_length):
x_train = np.zeros((XX.shape[0]-window_length,window_length,XX.shape[1]))
for i in range(x_train.shape[0]):
    x_train[i] = XX[i:i+window_length,:]
y_train = yy[window_length:]
return x_train, y_train

      

Then your testing will work great since it is already in the form (1,10,25).

+1


source







All Articles