Search code examples
python-3.xmachine-learningkerasmismatch

keras multi dimensions input to simpleRNN: dimension mismatch


The input element has 3 rows each having 199 columns and the output has 46 rows and 1 column

Input.shape, output.shape
((204563, 3, 199), (204563, 46, 1))

When the input is given the following error is thrown:

from keras.layers import Dense
from keras.models import Sequential
from keras.layers.recurrent import SimpleRNN

model = Sequential()
model.add(SimpleRNN(100, input_shape = (Input.shape[1], Input.shape[2])))
model.add(Dense(output.shape[1], activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(Input, output, epochs = 20, batch_size = 200)

error thrown:

Epoch 1/20

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-134-378dd431cf45> in <module>()
      3 model.add(Dense(y_target.shape[1], activation = 'softmax'))
      4 model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
----> 5 model.fit(X_input, y_target, epochs = 20, batch_size = 200)
.
.
.
ValueError: Error when checking model target: expected dense_6 to have 2 dimensions, but got array with shape (204563, 46, 1)

Please explain the reason for the problem and possible soution


Solution

  • The problem is that SimpleRNN(100) returns a tensor of shape (204563, 100), hence, the Dense(46) (since output.shape[1]=46) will return a tensor of shape (204563, 46), but your y_target have shape (204563, 46, 1). You need to remove the last dimension with, for example, y_target = np.squeeze(y_target), so that the dimension are consistent