Search code examples
pythonnumpykerascudnn

CudNN Invalid input shape


I am inputing a 1 dimension numpy array into a CuDNNLSTM layer that is 19 integers long. So i set the input shape to input_shape=(19,) however when trying to train the model it is giving me the following error. I can see it is expecting a numpy array with a 3rd dimenstion but not sure why

ValueError: Input 0 of layer cu_dnnlstm is incompatible with the layer: 
expected ndim=3, found ndim=2. Full shape received: [None, 19]

The full code of my model can be seen here, tho the issue is in the first input layer

model = Sequential()
model.add(CuDNNLSTM(HIDDEN_SIZE, input_shape=(19,)))
model.add(Dropout(DROPOUT_VALUE))
for _ in range(HIDDEN_LAYERS):
    model.add(CuDNNLSTM(HIDDEN_SIZE, return_sequences=True))
    model.add(Dropout(DROPOUT_VALUE))
model.add(TimeDistributed(Dense(1, activation='softmax')))
opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics= 
['mse'])

model.fit(x_train, y_train, epochs=EPOCH_COUNT, validation_data=(x_test, 
y_test))

Solution

  • If you have a sequence with 19 integers, then the timesteps dimension should be 19, and the features dimension should be 1, meaning the input shape to your network should be (19, 1).

    You should also reshape your data to match the new input shape.