You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/03/14 01:51:49 UTC
[GitHub] feevos commented on issue #9822: gluon HybridBlock wrapper of constant nd.array, is it possible?
feevos commented on issue #9822: gluon HybridBlock wrapper of constant nd.array, is it possible?
URL: https://github.com/apache/incubator-mxnet/issues/9822#issuecomment-372877890
Thanks @jmacglashan you are right. On mxnet [discuss forum](https://discuss.mxnet.io/t/custom-layer-infer-shape-after-first-forward-pass/585/7) I was given a solution that does exactly what you describe:
```Python
class CustomConv(HybridBlock):
def __init__(self, const_ndarray, use_bias = True, **kwargs):
super(CustomConv, self).__init__(**kwargs)
self.use_bias = use_bias
with self.name_scope():
self.weight = self.params.get('weight',
shape=(100, 100, 3, 3),
allow_deferred_init=True)
self.bijkl = self.params.get(
'bijkl',
shape=const_ndarray.shape,
init=mx.init.Constant(const_ndarray.asnumpy().tolist()),
differentiable=False)
if self.use_bias:
self.bias = self.params.get(
'bias',
allow_deferred_init=True,
init = mx.init.Zero(),
shape=(100,))
def hybrid_forward(self, F, x, weight, bijkl, bias=None):
proj_weight = F.sum(F.dot(weight, bijkl), axis=[2, 3])
if self.use_bias:
return F.Convolution(data=x, weight=proj_weight, bias=bias, num_filter=100, kernel=(5, 5))
else:
return F.Convolution(data=x, weight=proj_weight, no_bias=True, num_filter=100, kernel=(5, 5))
```
Only issue still left is that I cannot (yet) infer the shape (of some dimension) during first run. Getting there ...
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services