0

我正在尝试在注意力层上执行逐行和逐列最大池化,如下面的链接所述: http ://www.dfki.de/~neumann/ML4QAseminar2016/presentations/Attentive-Pooling-Network.pdf(幻灯片- 15)

我正在使用文本数据集,其中一个句子被馈送到 CNN。句子的每个单词都被嵌入了。它的代码如下:

model.add(Embedding(MAX_NB_WORDS, emb_dim, weights=[embedding_matrix],input_length=MAX_SEQUENCE_LENGTH, trainable=False))
model.add(Conv1D(k, FILTER_LENGTH, border_mode = "valid", activation = "relu"))    

CNN 的输出是有形状的(无,256)。这充当注意力层的输入。谁能建议如何在 keras 中以 tensorflow 作为后端实现行明智或列明智的最大池化?

4

1 回答 1

2

如果您的模型中有带有 shape 的图像(batch, width, height, channels),您可以重塑数据以隐藏其中一个空间维度并使用 1D 池化:

对于宽度:

model.add(Reshape((width, height*channels)))
model.add(MaxPooling1D()) 
model.add(Reshape((width/2, height, channels))) #if you had an odd number, add +1 or -1 (one of them will work) 

对于高度:

#Here, the time distributed will consider that "width" is an extra time dimension, 
#and will simply think of it as an extra "batch" dimension
model.add(TimeDistributed(MaxPooling1D()))

工作示例,具有两个分支的功能性 API 模型,每个分支一个用于池化:

import numpy as np
from keras.layers import *
from keras.models import *

inp = Input((30,50,4))
out1 = Reshape((30,200))(inp)
out1 = MaxPooling1D()(out1)
out1 = Reshape((15,50,4))(out1)
out2 = TimeDistributed(MaxPooling1D())(inp)

model = Model(inp,[out1,out2])
model.summary()

或者Reshape,如果您不想打扰数字:

#swap height and width
model.add(Permute((2,1,3)))

#apply the pooling to width
model.add(TimeDistributed(MaxPooling1D()))

#bring height and width to the correct order
model.add(Permute((2,1,3)))
于 2017-10-23T12:55:31.863 回答