I have implemented the following LSTM architecture. I am tring to train it to predict the number sequence but when I test it is not working. I think Iam giving the wrong input and the wrong test data.
import numpy as np
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import LSTM,Dense
X_train = np.array([
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
])
y_train = np.array([
[4, 5, 6],
[13, 14, 15],
])
#X_train = X_train.reshape((X_train.shape[0], 5, 5))
model = keras.Sequential()
model.add(keras.layers.LSTM(3,input_shape =(3, 3))) #### The input_shape has to correspond to the input data
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(X_train, y_train, epochs=100)
X_new = np.array([[1,2,3]])
X_new = np.reshape(X_new, (1,3))
y_pred = model.predict(X_new)
print(y_pred)
Someone can give me the correct input data and test data to train this architecture?
>Solution :
One way to set the input and output sizes for your problem is as follows (not necessarily the only way):
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense
# train input
X_train = np.array([
[[1], [2], [3]],
[[4], [5], [6]],
[[7], [8], [9]],
[[10], [11], [12]],
[[13], [14], [15]],
[[16], [17], [18]],
])
y_train = np.array([
[4],
[5],
[6],
[13],
[14],
[15],
])
# Build the model
model = Sequential()
model.add(LSTM(3, input_shape=(3, 1))) # Input shape should be (sequence_length, input_dimension)
model.add(Dense(1)) # just one neuron for regression
model.compile(loss="mean_squared_error", optimizer="adam")
# Reshape the input data to match the model's input shape
X_train = X_train.reshape((X_train.shape[0], 3, 1))
model.fit(X_train, y_train, epochs=100)
# Test the model
X_new = np.array([[1], [2], [3]]) # Input a sequence of numbers
X_new = X_new.reshape((1, 3, 1))
y_pred = model.predict(X_new)
print(y_pred)