I have tried various ways to reshape my data for a LTSM neural network but I continue to the get following error:
ValueError: Error when checking input: expected lstm_29_input to have shape (1, 1) but got array with shape (1, 3841)
My original x_train data is shaped:
array([[-1.86994147, -2.28655553, -2.13504696, ..., 2.38827443,
1.29554594, 46. ],
[-1.99436975, -2.41145158, -1.93463695, ..., 2.57659554,
1.27274537, 68. ],
[-2.19207883, -2.24740434, -1.73407733, ..., 2.66955543,
1.80862379, 50. ]
x_train shape: (1125, 3841)
x_valid shape: (375, 3841)
y_train shape: (1125,)
So I've reshaped both my x_train and x_valid data (for predictions) in order to compile properly the model. I've looked at various examples with similar errors and they reshape it as seen below and they usually get their models working but in my case, I am not sure what is going on.
# reshape input to be [samples, time steps, features]
trainX = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
validX = np.reshape(x_valid, (x_valid.shape[0], 1, x_valid.shape[1]))
Model Construction
def cnn_model():
""""
Creates the model of the CNN.
:param nr_measures: the number of output nodes needed
:return: the model of the CNN
"""
# Initializing the ANN
model = Sequential()
# Adding the input layer and the first hidden layer
model.add(Conv1D(64, 2, activation="relu", input_shape=(x_train.shape[1], 1)))
model.add(Flatten())
model.add(BatchNormalization())
# Adding the second hidden layer
model.add(Dense(128, activation="relu"))
model.add(BatchNormalization())
# Adding the output layer
model.add(Dense(1, activation = "softplus"))
model.compile(loss="mse", optimizer="adam", metrics = ['mse'])
return model
Calling and Training Model
#Call Model
cnn_model= cnn_model(trainX)
#Train Model
history = cnn_model.fit(trainX, y_train, batch_size = 50, epochs = 150, verbose = 0 ,validation_data = (validX, y_valid))