如何在keras中执行按行或按列的最大池化
How to perform row wise or column wise max pooling in keras
我正尝试按照下面 link 中的描述在注意力层上执行按行和按列的最大池化:
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 的输出形状为 (None, 256)。这充当注意力层的输入。
谁能建议如何在 keras 中以 tensorflow 作为后端实现行式或列式最大池化?
如果您的模型中有形状为 (batch, width, height, channels)
的图像,您可以重塑数据以隐藏其中一个空间维度并使用一维池化:
对应宽度:
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)))
我正尝试按照下面 link 中的描述在注意力层上执行按行和按列的最大池化: 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 的输出形状为 (None, 256)。这充当注意力层的输入。 谁能建议如何在 keras 中以 tensorflow 作为后端实现行式或列式最大池化?
如果您的模型中有形状为 (batch, width, height, channels)
的图像,您可以重塑数据以隐藏其中一个空间维度并使用一维池化:
对应宽度:
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)))