简体   繁体   English

LSTM嵌入层形状错误

[英]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) 我想该错误可能与单编码y_train_lsmt ,形状为(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: 我已经部分解决了添加Flatten()层的问题:

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更改为以下类别:

y_train_lstm = keras.utils.to_categorical(y_train_lstm)

Or change your loss to sparse_categorical_entropy: 或将损失更改为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 . 编辑 :基于您的github存储库,由于您没有预处理x_test_lstm ,因此评估无法正常工作。 Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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