2017-05-25 27 views
1

在CNTK中 - 如何在同一層上使用多個過濾器尺寸(例如過濾器尺寸2,3,4,5)?在CNTK中連接具有不同過濾器尺寸的conv層

繼完成工作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

你可以做這樣的事情:

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

或用Sequential()和拉姆達:

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