I am using keras to create a LSTM model. While training, I am getting this error.
ValueError: Error when checking target: expected dense_4 to have shape (1,) but got array with shape (34,)
Here is my model
model = Sequential()
model.add(Embedding(max_words, embedding_dim, input_length=maxlen))
model.add(LSTM(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 34 ,activation='softmax'))
model.layers[0].set_weights([embedding_matrix])
model.layers[0].trainable = False
model.compile(optimizer='rmsprop',loss='sparse_categorical_crossentropy',metrics=['acc'])
Model Summary:
Layer (type) Output Shape Param #
=================================================================
embedding_2 (Embedding) (None, 15, 50) 500000
_________________________________________________________________
lstm_2 (LSTM) (None, 128) 91648
_________________________________________________________________
dense_3 (Dense) (None, 64) 8256
_________________________________________________________________
dropout_2 (Dropout) (None, 64) 0
_________________________________________________________________
dense_4 (Dense) (None, 34) 2210
=================================================================
Total params: 602,114
Trainable params: 102,114
Non-trainable params: 500,000
_________________________________________________________________
I am calling fit using
history = model.fit(X_train, y_train,epochs=100,batch_size=128)
y_train
is a one-hot-encoded label with shape (299, 34)
.
X_train
is of shape (299, 15)
.
I am not sure why model is looking for shape(1,) as I can see that dense_4 (Dense)
has an output shape of `(None, 34).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…