'How to solve logits and labels must have the same shape ((64, 20, 1440) vs (64, 2))

i'm newbie in machine learning, and i try to run lstm multi layers with keras. here's my code:

from keras.layers import Embedding
embedding_layer = Embedding(vocabulary_size, EMBEDDING_DIM, weights=[embedding_matrix], trainable=False)

from keras.layers import Input, Dense, Embedding, Dropout
from keras.layers.core import Reshape
from tensorflow.keras.optimizers import Adam
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import LSTM

X_train = pad_sequences(sequences_train)
X_test = pad_sequences(sequences_test,maxlen=X_train.shape[1])
y_train = to_categorical(np.asarray(labels[train_data.index]))
y_test = to_categorical(np.asarray(labels[test_data.index]))

embed_dim = 128
lstm_out = 128

sequence_length = X_train.shape[1]
inputs = Input(shape=(sequence_length,))
embedding = embedding_layer(inputs)
reshape = Reshape((sequence_length,EMBEDDING_DIM,1))(embedding)
adam = Adam(lr=1e-1)

model = Sequential()
model.add(Embedding(input_dim = len(word_index)+1, output_dim = EMBEDDING_DIM, input_length = sequence_length, weights=[embedding_matrix], trainable=False))
model.add(LSTM(((lstm_out)), return_sequences=True))
model.add(Dropout(0.5))
model.add(Dense(len(labels), activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics = ['accuracy'])
history = model.fit(X_train, y_train, batch_size=64, epochs=5, validation_data=(X_test, y_test), shuffle = True)

print(model.summary())

and some errors appear

File "E:\Codes\LSTM_multi_Layers.py", line 31, in <module>
history = model.fit(X_train, y_train, batch_size=64, epochs=5, validation_data=(X_test, y_test), shuffle = True)

ValueError: logits and labels must have the same shape ((64, 20, 1440) vs (64, 2))

What does it mean? and what should be changed from my code? can somebody help me, please.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source