首页 > 解决方案 > 如何将 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 = 50000
max_article_len = 20

tokenizer = 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,)

标签: python-3.xtensorflowmachine-learninglstmkeras-layer

解决方案


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

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

如果您仍然有问题,那么问题一定出在您的输入形状上。


推荐阅读