mxnet-the-straight-dope icon indicating copy to clipboard operation
mxnet-the-straight-dope copied to clipboard

Gluon example for multiple filters in CNN?

Open jdchoi77 opened this issue 7 years ago • 2 comments

For the example code provided by https://mxnet.incubator.apache.org/tutorials/nlp/cnn.html

# create convolution + (max) pooling layer for each filter operation
filter_list=[3, 4, 5] # the size of filters to use
print 'convolution filters', filter_list

num_filter=100
pooled_outputs = []
for i, filter_size in enumerate(filter_list):
    convi = mx.sym.Convolution(data=conv_input, kernel=(filter_size, num_embed), num_filter=num_filter)
    relui = mx.sym.Activation(data=convi, act_type='relu')
    pooli = mx.sym.Pooling(data=relui, pool_type='max', kernel=(sentence_size - filter_size + 1, 1), stride=(1,1))
    pooled_outputs.append(pooli)

# combine all pooled outputs
total_filters = num_filter * len(filter_list)
concat = mx.sym.Concat(*pooled_outputs, dim=1)

# reshape for next layer
h_pool = mx.sym.Reshape(data=concat, target_shape=(batch_size, total_filters))

Could you please provide a code using Gluon that is equivalent to the above code? Would it be possible to accomplish this by using net = gluon.nn.Sequential() although those convolutions are performed in parallel for [3, 4, 5]-gram filters? Thank you!

jdchoi77 avatar Nov 04 '17 06:11 jdchoi77

Is there a reason why you have to do this all in Sequential with primitive layers? Why don't you just write a block that does the multiple filters and then stack multiple such blocks with a sequential?

zackchase avatar Nov 16 '17 02:11 zackchase

you can try gluon.Block https://mxnet.incubator.apache.org/api/python/gluon/gluon.html?highlight=block#mxnet.gluon.Block

GaryGaryry avatar Feb 26 '18 15:02 GaryGaryry