1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18import mxnet as mx
19
20
21def conv(net,
22         channels,
23         filter_dimension,
24         stride,
25         weight=None,
26         bias=None,
27         act_type="relu",
28         no_bias=False,
29         name=None
30         ):
31    # 2d convolution's input should have the shape of 4D (batch_size,1,seq_len,feat_dim)
32    if weight is None or bias is None:
33        # ex) filter_dimension = (41,11) , stride=(2,2)
34        net = mx.sym.Convolution(data=net, num_filter=channels, kernel=filter_dimension, stride=stride, no_bias=no_bias,
35                                 name=name)
36    elif weight is None or bias is not None:
37        net = mx.sym.Convolution(data=net, num_filter=channels, kernel=filter_dimension, stride=stride, bias=bias,
38                                 no_bias=no_bias, name=name)
39    elif weight is not None or bias is None:
40        net = mx.sym.Convolution(data=net, num_filter=channels, kernel=filter_dimension, stride=stride, weight=weight,
41                                 no_bias=no_bias, name=name)
42    else:
43        net = mx.sym.Convolution(data=net, num_filter=channels, kernel=filter_dimension, stride=stride, weight=weight,
44                                 bias=bias, no_bias=no_bias, name=name)
45    net = mx.sym.Activation(data=net, act_type=act_type)
46    return net
47