LSTM嵌入层形状错误

我的网络架构如下:

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'])

当我尝试训练模型时:

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

我得到了以下错误:

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

我认为这个错误可能是由于y_train_lsmt的一热编码,形状为(3133,3)

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

但我对此并不确定。

更新:

我通过添加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'])

但现在在模型评估时得到了同样的错误:

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


回答:

你的代码看起来没问题。将y_train_lstm转换为分类数据:

y_train_lstm = keras.utils.to_categorical(y_train_lstm)

或者将损失函数改为稀疏分类交叉熵:

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

编辑:根据你的GitHub仓库,评估不会成功,因为你没有预处理x_test_lstm。请尝试:

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注