【问题标题】:Concatenating conv layers with different filter sizes in CNTK在 CNTK 中连接具有不同过滤器大小的卷积层
【发布时间】:2017-05-25 16:39:59
【问题描述】:

在 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

【问题讨论】:

    标签: cntk


    【解决方案1】:

    你可以这样做:

    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])  
    

    【讨论】:

      【解决方案2】:

      或者使用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()
          ])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        相关资源
        最近更新 更多