Search code examples
pythontensorflowkeraslstm

How do you define input shape without using your input?


I am training to figure out how to define the input shape of my training data without using X_train in the model creation process. I know that LSTM network accepts only 3D arrays as input and my data is prepared with this for loop

X_train = []
y_train = []
for i in range(10, 400):
    X_train.append(training_set_scaled[i-10:i, 0])
    y_train.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))

Below is the model that I am using.

model = Sequential()
model.add(LSTM(units = 100, return_sequences = True, input_shape = (X_train.shape[1], 1)))
model.add(Dropout(0.2))

model.add(LSTM(units=1000 , return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=1000 , return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=100))
model.add(Dropout(0.2))
model.add(Dense(units=1 ))
model.compile(optimizer=opt, loss='mean_squared_error' , metrics=[tf.keras.metrics.RootMeanSquaredError()])

As you can see the first LSTM layer has X_train included in the input shape is there any way to define the input shape without using X_train, it knows that it needs to use X_train for training because we have model.fit that takes X_train as one of its arguments

If I do the following my model does not train in the same way

model = Sequential()
model.add(LSTM(units = 100, return_sequences = True))
model.add(Dropout(0.2))

model.add(LSTM(units=1000 , return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=1000 , return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=100))
model.add(Dropout(0.2))
model.add(Dense(units=1 ))
model.compile(optimizer=opt, loss='mean_squared_error' , metrics=[tf.keras.metrics.RootMeanSquaredError()])

Solution

  • Did you try using None instead of the X_train? Like this?

    model.add(LSTM(units = 100, return_sequences = True, input_shape = (None , 1)))