我正在使用Keras 1.0.我的问题与此问题相同(如何在Keras中实现Mean Pooling层),但对我来说这里的答案似乎不够.
以下代码不起作用:
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)
Run Code Online (Sandbox Code Playgroud)
如果我没有设置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')
Run Code Online (Sandbox Code Playgroud)
否则,我打电话时会收到此错误Dense():
Traceback (most recent call last):
File …Run Code Online (Sandbox Code Playgroud) machine-learning neural-network deep-learning keras recurrent-neural-network