I thought that with an LSTM you could use sequences of any length as input, but with shape fixed for each time step, but I encountered an anomalous behavior.
The following code gives the error that I expected, since the input shape is different from the one defined on the LSTM model:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM
model = Sequential()
model.add(LSTM(50, input_shape=(None, 15), return_sequences=True))
model.add(Dense(1, activation='linear'))
N_start = 16
inputs = np.zeros((1000, 50, N_start))
model.predict(inputs)
and the error is:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, None, 15), found shape=(None, 50, 16)
But then, if I use the following code, the model works even at different shapes:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM
model = Sequential()
model.add(LSTM(50, input_shape=(None, 15), return_sequences=True))
model.add(Dense(1, activation='linear'))
N_start = 14
inputs = np.zeros((1000, 50, N_start))
add = np.ones((1000, 50, 1))
for i in range(10):
inputs = np.concatenate((inputs,add), axis = 2)
print(np.shape(inputs))
model.predict(inputs)
The output is
(1000, 50, 15) 2023-05-17 10:46:19.392818: I tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:428] Loaded cuDNN version 8401 32/32 [==============================] - 2s 6ms/step (1000, 50, 16) 32/32 [==============================] - 0s 6ms/step (1000, 50, 17) 32/32 [==============================] - 0s 6ms/step
I can't understand why it works for inputs with last dimension different from 15.
Matrix size-incompatible: In[0]: [32,16], In[1]: [15,200]
$\endgroup$