Here is the code that I have written:
Df_input = df_close[input_col]
df_Output = df_close[Output_cols]
def preprocess_multistep_lstm(Input_Sequence, Output_Sequence, n_steps_in, n_steps_out, features, ):
X, y = list(), list()
for i in range(len(Input_Sequence)):
# find the end of this pattern
end_ix = i + n_steps_in
out_end_ix = end_ix + n_steps_out
# check if we are beyond the sequence
if out_end_ix > len(Input_Sequence):
break
# gather input and output parts of the pattern
seq_x, seq_y = Input_Sequence[i:end_ix], Output_Sequence[end_ix:out_end_ix]
X.append(seq_x)
y.append(seq_y)
# if i == 0:
# print(seq_x)
# print(seq_y)
X = np.array(X)
y = np.array(y)
X = X.reshape((X.shape[0], X.shape[1], -n_features))
print(X)
return X, y
# choose the number of days on which to base our predictions
output_needed = 1
Seq_len = 2
n_features = 1
# X, y = preprocess_lstm(df_close_shift.to_numpy(), nb_days, n_features)
X, y = preprocess_multistep_lstm(Df_input.to_numpy(), df_Output.to_numpy(), Seq_len, output_needed, n_features)
The above is the way I am generating data for the training.
#Split the data set b
etween the training set and the test set
test_days = 365
X_train, y_train = X[:-test_days], y[:-test_days]
X_test, y_test = X[-test_days:], y[-test_days:]
model = Sequential()
Test_model = Sequential()
model.add(LSTM(units=50, input_shape=(Seq_len, n_features)))
model.add(Dense(10))
model.summary()
model.compile(optimizer="adam",
loss="mean_squared_error",
metrics=[tf.keras.metrics.MeanAbsoluteError()])
epoch = 1
checkpoint = tf.train.Checkpoint(model=model)
manager = tf.train.CheckpointManager(
checkpoint,
directory="EURUSD/model",
max_to_keep=2,
checkpoint_name="model_1_2_3_4"
)
while epoch < 10:
model.fit(X_train,
y_train,
epochs=1,
batch_size = 32)
# Evaluate the model on the test data using
print("Evaluate on test data")
results = model.evaluate(X_test, y_test, batch_size=32)
print("Test MSE:", results[0])
print("Test MAE:", results[1])
manager.save()
epoch = epoch + 1
I am getting the following error:
ValueError: in user code:
File "C:\Python311\Lib\site-packages\keras\src\engine\training.py", line 1377, in train_function *
return step_function(self, iterator)
File "C:\Python311\Lib\site-packages\keras\src\engine\training.py", line 1360, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Python311\Lib\site-packages\keras\src\engine\training.py", line 1349, in run_step **
outputs = model.train_step(data)
File "C:\Python311\Lib\site-packages\keras\src\engine\training.py", line 1126, in train_step
y_pred = self(x, training=True)
File "C:\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Python311\Lib\site-packages\keras\src\engine\input_spec.py", line 298, in assert_input_compatibility
raise ValueError(
ValueError: Exception encountered when calling layer 'sequential_8' (type Sequential).
Input 0 of layer "lstm_3" is incompatible with the layer: expected shape=(None, None, 1), found shape=(None, 2, 74)
Call arguments received by layer 'sequential_8' (type Sequential):
• inputs=tf.Tensor(shape=(None, 2, 74), dtype=float32)
• training=True
• mask=None
I know I am missing something and not able to understand what exactly I have missed.
please can someone let me know how I can resolve this issue?
If I change the line X = X.reshape((X.shape[0], X.shape[1], -n_features))
to X = X.reshape((X.shape[0], X.shape[1], n_features))
then I am getting the following error:
ValueError: cannot reshape array of size 147704 into shape (998,2,1)