简体   繁体   中英

Error in LSTM embedding layer's shape

I have this network architecture:

model = Sequential()
model.add(Embedding(9761, 100, input_length=longest_period))
model.add(LSTM(30, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

When I try to fit the model:

res = model.fit(X_train_lsmt, np.array(y_train_lsmt), validation_split=0.25, epochs=2, batch_size=128, verbose=0)

I get this error:

ValueError: Error when checking model input: expected
embedding_3_input to have shape (None, 217) but got array 
with shape (3133, 1)

I suppose that the error could be about the one-hot encoded y_train_lsmt , having shape (3133,3)

[[ 0. 1. 0.] [ 0. 1. 0.] [ 0. 0. 1.] ..., [ 1. 0. 0.] [ 1. 0. 0.] [ 0. 1. 0.]]

but I am not sure about this.

Update:

I have partially solved adding a Flatten() layer:

model = Sequential()
model.add(Embedding(9761, 100, input_length=stringa_piu_lunga))
model.add(LSTM(units=10, return_sequences=True))
model.add(Flatten())
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

but now I get the same error during model evaluation:

score = model.evaluate(X_test_lsmt, y_train_lsmt, verbose=0)

Your code seems fine. Change your y_train_lstm to categorical with:

y_train_lstm = keras.utils.to_categorical(y_train_lstm)

Or change your loss to sparse_categorical_entropy:

model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

Edited : Based on your github repository, the evaluation not going to work because you did not preprocess the x_test_lstm . Try:

X_test_lstm = sequence.pad_sequences(X_test_lstm, maxlen=longest_string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM