1

Could someone help me to understand what this error is all about?

model = Sequential()
model.add(Embedding(82, 100, weights=[embedding_matrix], input_length=1000))
model.add(LSTM(100))
model.add(Dense(100, activation = 'sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, epochs = 5, batch_size=64)

When i run this LSTM model, I am getting an error as

ValueError: Error when checking model target: expected dense_16 to have shape (None, 100) but got array with shape (16, 2)

I am not sure how much the below information would be useful:

x_train.shape
Out[959]: (16, 1000)

y_train.shape
Out[962]: (16, 2)

If you need any other information, I am ready to provide

Doubt Dhanabalu
  • 457
  • 4
  • 8
  • 18

2 Answers2

2

you have defined dense layer input shape is 100.

model.add(Dense(100, activation = 'sigmoid'))

so you need to make sure your input should always same shape. here in your case make x_train and and y_train same shape.

try with :

model = Sequential()
# here the batch dimension is None,
# which means any batch size will be accepted by the model.
model.add(Dense(32, batch_input_shape=(None, 500)))
model.add(Dense(32))
  • Thanks this has worked for me. however 32 has not worked, I have chosen Dense(2, batch_input_shape=(None,500))). this has worked. Would it be possible for you to explain me the fundamental behind this please. Thank you – Doubt Dhanabalu Aug 11 '17 at 10:40
  • you are welcome. that it helped you. and hey, i just gave you a prototype or example. parameter you need to make change !!. :) – Achyuta nanda sahoo Aug 11 '17 at 10:49
  • I have raised one more question. I beleive you would be the right person to answer. If you are aware, kindly guide me (https://stackoverflow.com/questions/45634434/different-accuracy-at-every-run-of-lstm-model) – Doubt Dhanabalu Aug 11 '17 at 11:46
1

Your last layer has an output shape of None,100

model.add(Dense(100, activation = 'sigmoid'))

But your data (y_train) has the shape (16,2). It should be

model.add(Dense(2, activation = 'sigmoid'))
ilan
  • 576
  • 3
  • 6