在CNTK中连接具有不同滤波器大小的conv层

时间:2017-05-25 16:40:00

标签: cntk

在CNTK中 - 如何在同一层上使用多种滤镜尺寸(例如滤镜尺寸2,3,4,5)?

完成工作后here(链接到github下面的代码(1)),我想采用文本,使用嵌入层,应用四种不同大小的过滤器(2,3,4,5) ,连接结果并将其提供给完全连接的层。 Network architecture figure

Keras示例代码:

main_input = Input(shape=(100,) 
embedding = Embedding(output_dim=32, input_dim=100, input_length=100, dropout=0)(main_input)

conv1 = getconvmodel(2,256)(embedding)
conv2 = getconvmodel(3,256)(embedding)
conv3 = getconvmodel(4,256)(embedding)
conv4 = getconvmodel(5,256)(embedding)

merged = merge([conv1,conv2,conv3,conv4],mode="concat")

def getconvmodel(filter_length,nb_filter):
    model = Sequential()
    model.add(Convolution1D(nb_filter=nb_filter,
                            `enter code here`input_shape=(100,32),
                            filter_length=filter_length,
                            border_mode='same',
                            activation='relu',
                            subsample_length=1))
    model.add(Lambda(sum_1d, output_shape=(nb_filter,)))
    #model.add(BatchNormalization(mode=0))
    model.add(Dropout(0.5))
    return model

(1):/ joshsaxe/eXposeDeepNeuralNetwork/blob/master/src/modeling/models.py

2 个答案:

答案 0 :(得分:2)

您可以这样做:

import cntk as C
import cntk.layers as cl

def getconvmodel(filter_length,nb_filter):
    @Function
    def model(x):
        f = cl.Convolution(filter_length, nb_filter, activation=C.relu))(x)
        f = C.reduce_sum(f, axis=0)
        f = cl.Dropout(0.5) (f)
    return model

main_input = C.input_variable(100)
embedding = cl.Embedding(32)(main_input)

conv1 = getconvmodel(2,256)(embedding)
conv2 = getconvmodel(3,256)(embedding)
conv3 = getconvmodel(4,256)(embedding)
conv4 = getconvmodel(5,256)(embedding)

merged = C.splice([conv1,conv2,conv3,conv4])  

答案 1 :(得分:0)

Sequential()和lambda:

def getconvmodel(filter_length,nb_filter):
    return Sequential([
        cl.Convolution(filter_length, nb_filter, activation=C.relu)),
        lambda f: C.reduce_sum(f, axis=0),
        cl.Dropout()
    ])