Keras中具有屏蔽支持的平均池或最大池 [英] Mean or max pooling with masking support in Keras
问题描述
...
print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 128))
model.add(LSTM(size, return_sequences=True, dropout_W=0.2 dropout_U=0.2))
model.add(GlobalAveragePooling1D())
model.add(Dense(1))
model.add(Activation('sigmoid'))
....
我需要能够将LSTM层之后的样本中所有时间步长的向量的平均值或最大值取下,然后再将该平均值或最大值提供给Keras中的密集层.
I need to be able to take the mean or max of the vectors for all time steps in a sample after LSTM layer before giving this mean or max vector to the dense layer in Keras.
我认为timedistributedmerge
能够做到这一点,但已弃用.使用return_sequences=True
,我可以获取LSTM层之后的样本中所有时间步长的向量.但是,GlobalAveragePooling1D()
与掩码不兼容,它考虑了所有时间步长,而我只需要非掩码时间步长.
I think timedistributedmerge
was able to do this but it was deprecated. Using return_sequences=True
I can obtain the vectors for all time steps in a sample after the LSTM layer. However, GlobalAveragePooling1D()
is not compatible with masking and it considers all time steps whereas I need only the non-masked time steps.
我看到了推荐Lambda
层的帖子,但这些帖子也没有考虑屏蔽.任何帮助将不胜感激.
I saw posts recommending the Lambda
layer but these also do not take masking into account. Any help would be appreciated.
推荐答案
Jacoxu的答案是正确的.但是,如果您使用的是用于keras的tensorflow后端,则Tensor类型不支持dimshuffle函数,请尝试使用此方法.
Jacoxu's answer is right. But if you are using a tensorflow backend for keras, the Tensor type doesn't support dimshuffle function, try this instead.
def call(self, x, mask=None):
if mask is not None:
# mask (batch, time)
mask = K.cast(mask, K.floatx())
# mask (batch, x_dim, time)
mask = K.repeat(mask, x.shape[-1])
# mask (batch, time, x_dim)
mask = tf.transpose(mask, [0,2,1])
x = x * mask
return K.sum(x, axis=1) / K.sum(mask, axis=1)
这篇关于Keras中具有屏蔽支持的平均池或最大池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!