nd.Convolution with bias=None, is it supported?


#1

Dear all,

I am trying to implement a custom convolution operation, following the Gluon tutorial, however I cannot make it work without bias term.

my question: Is it possible to apply nd.Convolution with bias=None?

Thanks

This works:

import mxnet as mx
from mxnet import nd

nbatch=1
nchannels=1
nfilters=2
kernel=[3,3]
xx = nd.random_uniform(shape=[nbatch,nchannels,5,5])
weight = nd.ones(shape=[nfilters,nchannels,kernel[0],kernel[1]])
b1 = nd.zeros(shape=[nfilters,])

conv = nd.Convolution(data=xx,weight=weight,bias=b1,
                                               num_filter=nfilters,kernel=kernel)

however, if I don’t specify a zero bias vector (expecting that bias=None would work too), like this:

conv = nd.Convolution(data=xx,weight=weight, num_filter=nfilters,kernel=kernel)

I get the following error:

---------------------------------------------------------------------------
MXNetError                                Traceback (most recent call last)
<ipython-input-322-dd96dd7d1b3a> in <module>()
----> 1 temp = nd.Convolution(data=xx,weight=weight,num_filter=nfilters,kernel=kernel)

/home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/ndarray/register.pyc in Convolution(data, weight, bias, kernel, stride, dilate, pad, num_filter, num_group, workspace, no_bias, cudnn_tune, cudnn_off, layout, out, name, **kwargs)

/home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/_ctypes/ndarray.pyc in _imperative_invoke(handle, ndargs, keys, vals, out)
     90         c_str_array(keys),
     91         c_str_array([str(s) for s in vals]),
---> 92         ctypes.byref(out_stypes)))
     93 
     94     if original_output is not None:

/home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/base.pyc in check_call(ret)
    144     """
    145     if ret != 0:
--> 146         raise MXNetError(py_str(_LIB.MXGetLastError()))
    147 
    148 

MXNetError: [16:10:11] src/c_api/../imperative/imperative_utils.h:303: Check failed: num_inputs == infered_num_inputs (2 vs. 3) Operator Convolution expects 3 inputs, but got 2 instead.

Stack trace returned 10 entries:
[bt] (0) /home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/libmxnet.so(+0x289a1c) [0x7fa508c26a1c]
[bt] (1) /home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/libmxnet.so(+0x240538f) [0x7fa50ada238f]
[bt] (2) /home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/libmxnet.so(+0x24029a2) [0x7fa50ad9f9a2]
[bt] (3) /home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/libmxnet.so(MXImperativeInvokeEx+0x63) [0x7fa50ad9ffb3]
[bt] (4) /home/dia021/anaconda2/lib/python2.7/lib-dynload/_ctypes.so(ffi_call_unix64+0x4c) [0x7fa54d34857c]
[bt] (5) /home/dia021/anaconda2/lib/python2.7/lib-dynload/_ctypes.so(ffi_call+0x1f5) [0x7fa54d347cd5]
[bt] (6) /home/dia021/anaconda2/lib/python2.7/lib-dynload/_ctypes.so(_ctypes_callproc+0x3e6) [0x7fa54d33f376]
[bt] (7) /home/dia021/anaconda2/lib/python2.7/lib-dynload/_ctypes.so(+0x9db3) [0x7fa54d336db3]
[bt] (8) /home/dia021/anaconda2/bin/../lib/libpython2.7.so.1.0(PyObject_Call+0x53) [0x7fa5523c9e93]
[bt] (9) /home/dia021/anaconda2/bin/../lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x715d) [0x7fa55247c80d]

#2

Found the solution: no_bias=True

This works now:

conv = nd.Convolution(data=xx,weight=weight, num_filter=nfilters,no_bias=True,kernel=kernel)