I have fitted a lstm model. There are 100 observations for each x and y variables. I used 80 values to train the model and 20 values to evaluate the model.
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
input_features=1
time_stamp=100
out_features=1
x_input=np.random.random((time_stamp,input_features))
y_input=np.random.random((time_stamp,out_features))
train_x=x_input[0:80,:]
test_x=x_input[80:,:]
train_y=y_input[0:80,:]
test_y=y_input[80:,:]
Then I reshaped the data as it necessary to do so before feeding the data into LSTM function.(Ex: For training x:(samples, timesteps, features)=(1,80,1))
dataXtrain = train_x.reshape(1,80, 1)
dataXtest = test_x.reshape(1,20,1)
dataYtrain = train_y.reshape(1,80,1)
dataYtest = test_y.reshape(1,20,1)
dataXtrain.shape
(1, 80, 1)
Then I was able to fit the model successfully using following lines of code:
model = Sequential()
model.add(LSTM(20,activation = 'relu', return_sequences = True,input_shape=(dataXtrain.shape[1],
dataXtrain.shape[2])))
model.add(Dense(1))
model.compile(loss='mean_absolute_error', optimizer='adam')
model.fit(dataXtrain, dataYtrain, epochs=100, batch_size=10, verbose=1)
But when I predict the model for the test data, I am getting this error.
y_pred = model.predict(dataXtest)
Error when checking input: expected lstm_input to have shape (80, 1) but got array with shape (20, 1)
Can anyone help me to figure out what is the problem here?
Thank you
It seems that the problem is with data preparation. I think you should divide your samples (instead of time-steps) to train and test data and the shape of train and test samples should be the same and like (None, time-steps, features)
.
Since you have just one sample with 100 observations (time-steps) you can divide your data into samples containing small size time-step series. For example:
n_samples = 20
input_features = 1
time_stamp = 100
out_features = 1
x_input = np.random.random((1, time_stamp, input_features))
y_input = np.random.random((1, time_stamp, out_features))
new_time_stamp = time_stamp//n_samples
x_input = x_input.reshape(n_samples, new_time_stamp, input_features)
y_input = y_input.reshape(n_samples, new_time_stamp, out_features)
dataXtrain = x_input[:16,...]
dataXtest = x_input[16:,...]
dataYtrain = y_input[:16,...]
dataYtest = y_input[16:,...]
Alternatively, you can collect more data samples each containing 100 time steps (it depends on your application and your existing data).
Also you can look at this and this that explained using LSTM in Keras comprehensively.