如何将LSTM与Dense层连接?

在尝试将LSTM与Dense层连接时,会在训练过程中报错:

input = Input(shape=(x_train.shape[1], None))X = Embedding(num_words, max_article_len)(input)X = LSTM(128, return_sequences=True, dropout = 0.5)(X)X = LSTM(128)(X)X = Dense(32, activation='softmax')(X)model = Model(inputs=[input], outputs=[X])...>>> ValueError: Error when checking target: expected dense to have shape (32,) but got array with shape (1,)

我尝试了不同的连接方式,但错误依然存在:

X, h, c = LSTM(128, return_sequences=False, return_state=True, dropout = 0.5)(X)X = Dense(32, activation='softmax')(X)>>> ValueError: Error when checking target: expected dense to have shape (32,) but got array with shape (1,)

有什么关于函数式API或Sequential模型的解决方案吗?

数据转换代码:

train = pd.read_csv('train.csv')articles = train['text']y_train = train['lang']num_words = 50000max_article_len = 20tokenizer = Tokenizer(num_words=num_words)tokenizer.fit_on_texts(articles)sequences = tokenizer.texts_to_sequences(articles)x_train = pad_sequences(sequences, maxlen=max_article_len, padding='post')x_train.shape>>> (18974, 100)y_train.shape>>> (18974,)

回答:

最后一个参数必须设置为False;

X = LSTM(128, return_sequences=True, dropout = 0.5)(X)X = LSTM(128, return_sequences=False)(X)

如果问题依然存在,那么问题可能出在你的输入形状上。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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