如何将LSTM与Dense连接起来?

时间:2020-01-26 13:48:15

标签: python-3.x tensorflow machine-learning lstm keras-layer

尝试将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 /顺序上有任何解决方案选项吗?

数据转换代码:

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

1 个答案:

答案 0 :(得分:0)

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

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

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