I would like to input time-series data to LSTM prediction model.
In my case, The Starting length of input data is (53,2) and the length of output data is (53,).
Because I wanted to design many-to-one LSTM prediction model,
I would like to input two features. But it has error when I execute this code.
The training result just show
Epoch 1/50
WARNING:tensorflow:Model was constructed with shape (None, 3, 2) for input KerasTensor(type_spec=TensorSpec(shape=(None, 3, 2), dtype=tf.float32, name='lstm_1_input'), name='lstm_1_input', description="created by layer 'lstm_1_input'"), but it was called on an input with incompatible shape (None, 1, 2).
WARNING:tensorflow:Model was constructed with shape (None, 3, 2) for input KerasTensor(type_spec=TensorSpec(shape=(None, 3, 2), dtype=tf.float32, name='lstm_1_input'), name='lstm_1_input', description="created by layer 'lstm_1_input'"), but it was called on an input with incompatible shape (None, 1, 2).
WARNING:tensorflow:Model was constructed with shape (None, 3, 2) for input KerasTensor(type_spec=TensorSpec(shape=(None, 3, 2), dtype=tf.float32, name='lstm_1_input'), name='lstm_1_input', description="created by layer 'lstm_1_input'"), but it was called on an input with incompatible shape (None, 1, 2).
2/2 - 1s - loss: nan - val_loss: nan
Epoch 2/50 ...
And it's seem to have a problem in the shape of data inputted to LSTM.
How can I solve this? When I saw this problem, I've tried rearranging data set. Saying, changing the numbers of reshape code X=np.array(X).reshape(53,1,2)
to X=np.array(X).reshape(53,2,1)
. ...
The code written below is the part of my code.
X = np.array(X).reshape(53, 1, 2)
y = y.reshape(53,1,1)
X_train, X_test, y_train, y_test = X[:42], X[43:53], y[:42], y[43:53]
X_train_valid, y_train_valid = X[33:42], y[33:42]
X_train = X_train.reshape(42,1,2)
y_train = y_train.reshape(42,1,1)
X_train_valid = X_train_valid.reshape(9,1,2)
y_train_valid = y_train_valid.reshape(9,1,1)
X_test = X_test.reshape(10,1,2)
y_test = y_test.reshape(10,1,1)
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(3,2)))
model.add(Dense(1))
model.summary()
model.compile(optimizer='adam', loss='mse')
history = model.fit(X_train, y_train, epochs = 50, verbose = 2, validation_data = (X_train_valid, y_train_valid))
question from:
https://stackoverflow.com/questions/65868104/how-can-i-input-data-to-lstm-prediction-model-in-python-in-an-appropriate-form