【问题标题】:Get HybridBlock layer shape on runtime在运行时获取 HybridBlock 图层形状
【发布时间】:2018-01-03 03:01:19
【问题描述】:

我正在尝试构建一个自定义池层(用于 ndarray 和 Symbol),我需要在运行时知道输入形状。根据文档,HybridBlock 具有“infer_shape”功能,但我无法使其工作。任何指向我做错了什么的指针?

mxnet 版本

1.0.0 ,从 conda,python3 构建。

最小可重现示例

例如:

import mxnet as mx
import mxnet.ndarray as nd
from mxnet.gluon import HybridBlock

class runtime_shape(HybridBlock):


    def __init__(self,  **kwards):
        HybridBlock.__init__(self,**kwards)


    def hybrid_forward(self,F,_input):

        print (self.infer_shape(_input))

        return _input

xx = nd.random_uniform(shape=[5,5,16,16])

mynet = runtime_shape()
mynet.hybrid_forward(nd,xx)

错误信息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-41-3f539a940958> in <module>()
----> 1 mynet.hybrid_forward(nd,xx)

<ipython-input-38-afc9785b716d> in hybrid_forward(self, F, _input)
     17     def hybrid_forward(self,F,_input):
     18 
---> 19         print (self.infer_shape(_input))
     20 
     21         return _input

 /home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/gluon/block.pyc in infer_shape(self, *args)
    460     def infer_shape(self, *args):
    461         """Infers shape of Parameters from inputs."""
--> 462         self._infer_attrs('infer_shape', 'shape', *args)
    463 
    464     def infer_type(self, *args):

/home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/gluon/block.pyc in _infer_attrs(self, infer_fn, attr, *args)
    448     def _infer_attrs(self, infer_fn, attr, *args):
    449         """Generic infer attributes."""
--> 450         inputs, out = self._get_graph(*args)
    451         args, _ = _flatten(args)
    452         arg_attrs, _, aux_attrs = getattr(out, infer_fn)(

/home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/gluon/block.pyc in _get_graph(self, *args)
    369             params = {i: j.var() for i, j in self._reg_params.items()}
    370             with self.name_scope():
--> 371                 out = self.hybrid_forward(symbol, *grouped_inputs, **params)  # pylint: disable=no-value-for-parameter
    372             out, self._out_format = _flatten(out)
    373 

/home/dia021/anaconda2/lib/python2.7/site-packages/mxnet/gluon/block.pyc in __exit__(self, ptype, value, trace)
     78         if self._block._empty_prefix:
     79             return
---> 80         self._name_scope.__exit__(ptype, value, trace)
     81         self._name_scope = None
     82         _BlockScope._current = self._old_scope

AttributeError: 'NoneType' object has no attribute '__exit__'

【问题讨论】:

    标签: python python-3.x mxnet


    【解决方案1】:

    HybridBlock 的理念是让命令式世界中的调试变得容易,您可以简单地放置一个断点或print 语句,然后查看哪些数据正在流经您的网络。当您确信网络正在执行您想要的操作时,您可以拨打.hybridize() 并享受速度提升。

    在开发网络并使用命令式模式时,您可以简单地打印: print('shape',_input.shape)

    并在使用网络的混合版本时删除此行,因为这仅适用于 NDArrays。

    如果这不能回答您的问题,您能否通过获取输入数据的形状来明确您要达到的目标是什么?

    【讨论】:

    • 嗨@Thomas,我刚看到这个。我想实现 Pyramid Scene Parsing pooling operator 并将其混合。从最初的_input 我需要生成 1x1(全局)、2x2、4x4、8x8 特征(过滤器)。另请参阅github.com/apache/incubator-mxnet/issues/9288我在 mxnet github 上的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    相关资源
    最近更新 更多