LSTM 后接均值池化

我使用的是 Keras 1.0。我的问题与这个(如何在 Keras 中实现均值池化层)相同,但那里的答案对我来说似乎不够充分。

我想实现这个网络:enter image description here

以下代码不起作用:

sequence = Input(shape=(max_sent_len,), dtype='int32')embedded = Embedding(vocab_size, word_embedding_size)(sequence)lstm = LSTM(hidden_state_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True)(embedded)pool = AveragePooling1D()(lstm)output = Dense(1, activation='sigmoid')(pool)

如果我不设置 return_sequences=True,当我调用 AveragePooling1D() 时会得到这个错误:

Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/PATH/keras/engine/topology.py", line 462, in __call__    self.assert_input_compatibility(x)  File "/PATH/keras/engine/topology.py", line 382, in assert_input_compatibility    str(K.ndim(x)))Exception: ('Input 0 is incompatible with layer averagepooling1d_6: expected ndim=3', ' found ndim=2')

否则,当我调用 Dense() 时会得到这个错误:

Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/PATH/keras/engine/topology.py", line 456, in __call__    self.build(input_shapes[0])  File "/fs/clip-arqat/mossaab/trec/liveqa/cmu/venv/lib/python2.7/site-packages/keras/layers/core.py", line 512, in build    assert len(input_shape) == 2AssertionError

回答:

添加 TimeDistributed(Dense(1)) 起到了帮助:

sequence = Input(shape=(max_sent_len,), dtype='int32')embedded = Embedding(vocab_size, word_embedding_size)(sequence)lstm = LSTM(hidden_state_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True)(embedded)distributed = TimeDistributed(Dense(1))(lstm)pool = AveragePooling1D()(distributed)output = Dense(1, activation='sigmoid')(pool)

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

发表回复

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