I want to use the LSTM neural network with keras to predict time series groups, and I'm having trouble getting the model to fit what I want. Sizes of my data:
input tensor: (data length, number of series to train, time steps to look back)
output tensor: (data length, number of series to forecast, time steps to look ahead)
Note. I want to keep the dimensions exactly so there is no transposition.
Dummy data code that reproduces the problem:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, TimeDistributed, LSTM
epoch_number = 100
batch_size = 20
input_dim = 4
output_dim = 3
look_back = 24
look_ahead = 24
n = 100
trainX = np.random.rand(n, input_dim, look_back)
trainY = np.random.rand(n, output_dim, look_ahead)
print('test X:', trainX.shape)
print('test Y:', trainY.shape)
model = Sequential()
model.add(LSTM(10, batch_input_shape=(None, input_dim, look_back), return_sequences=True))
model.add(LSTM(10, return_sequences=True))
model.add(TimeDistributed(Dense(look_ahead)))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainY, nb_epoch=epoch_number, batch_size=batch_size, verbose=1)
These are the arrows:
Exception: error while checking the target model: it is expected timedistributed_1 to have a form (None, 4, 24), but received an array with the form (100, 3, 24)
The problem is determining the layer TimeDistributed.
How to define a layer TimeDistributedso that it compiles and trains?