本文介绍了LSTM之后是平均池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Keras 1.0.我的问题与这一问题相同(如何在Keras中实现均值池层),但答案似乎并非如此对我来说足够.
I'm using Keras 1.0. My problem is identical to this one (How to implement a Mean Pooling layer in Keras), but the answer there does not seem to be sufficient for me.
我想实现这个网络:
I want to implement this network:
以下代码不起作用:
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()
时会出现此错误:
If I don't set return_sequences=True
, I get this error when I call 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()
时收到此错误:
Otherwise, I get this error when I call 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) == 2
AssertionError
推荐答案
添加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)
这篇关于LSTM之后是平均池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!