我已经从pip安装了最新的mxnet版本1.0.0。当我打印层的权重时,它断言一个形状:“KeyError”。我对mxnet和我在mxnet官方教程中所学到的一切都是新手。
import mxnet
from mxnet import nd,gluon,autograd
net=gluon.nn.Dense(10,in_units=30)
print (net.weight)错误是:
Traceback (most recent call last):
File "/home/user/docs/mxnet/mtcnn/tmp/learn_mxnet.py", line 5, in <module>
print (net.weight)
File "/usr/local/lib/python2.7/dist-packages/mxnet/gluon/parameter.py", line 120, in __repr__
return s.format(**self.__dict__)
KeyError: 'shape'我认为这可能是1.0.0版本的问题。
发布于 2017-12-29 02:53:30
要检查权重,首先需要初始化它们,如下所示:net.collect_params().initialize(mx.init.Xavier(), ctx=mx.cpu())
在您的例子中,因为您没有在Net的构造函数中指定层的输入大小,所以此时无法确定参数的形状。因此,如果您现在访问net.weight.data(),将会引发一个异常:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/mxnet-0.12.1-py2.7.egg/mxnet/gluon/parameter.py", line 389, in data return self._check_and_get(self._data, ctx) File "/usr/local/lib/python2.7/dist-packages/mxnet-0.12.1-py2.7.egg/mxnet/gluon/parameter.py", line 189, in _check_and_get "nested child Blocks"%(self.name)) RuntimeError: Parameter dense0_weight has not been initialized. Note that you should initialize parameters and create Trainer with Block.collect_params() instead of Block.params because the later does not include Parameters of nested child Blocks
您可以按如下方式初始化权重:net.collect_params().initialize(mxnet.init.Xavier(), ctx=mxnet.cpu()) print (net.weight.data())
发布于 2017-12-30 03:40:12
您在MXNet 1.0.0上的这种回归是正确的。它已经是fixed了,你可以安装MXNet的最新测试版,直到下一次正式发布。要安装最新的预发布(测试版)版本:pip install -U --pre mxnet。
https://stackoverflow.com/questions/48008998
复制相似问题