代码如下:
from mxnet.gluon import nn
from mxnet import nd
def get_net():
net = nn.Sequential()
with net.name_scope():
net.add(nn.Dense(4, activation="relu"))
net.add(nn.Dense(2))
return net
x = nd.random.uniform(shape=(3, 5))
net = get_net()
net.initialize()
print(net(x))
w = net[0].weight
b = net[0].bias
print('name: ', net[0].name, '\nweight: ', w, '\nbias: ', b)
报错如下:
weight: Traceback (most recent call last):
File "E:/python file/distribution_mpc/test1.py", line 22, in <module>
print('name: ', net[0].name, '\nweight: ', w, '\nbias: ', b)
File "D:\Anaconda\lib\site-packages\mxnet\gluon\parameter.py", line 121, in __repr__
return s.format(**self.__dict__)
KeyError: 'shape'
解决办法:
1,重新安装mxnet,最新版本已经改进了这个问题。
2, 到指定文件去改一行就行了。就是把 D:\Anaconda\lib\site-packages\mxnet\gluon\parameter.py
(具体的路径要看自己的报错信息) 的第121行的 return s.format(**self.__dict__)
改成 return s.format(name=self.name, shape=self.shape, dtype=self.dtype)
就行了
参考:这里