MindSpore报错AttributeError:‘NoneType‘ has no attribute...

1 报错描述

1.1 系统环境

Hardware Environment(Ascend/GPU/CPU): GPU

Software Environment:

– MindSpore version (source or binary): 1.6.0

– Python version (e.g., Python 3.7.5): 3.7.6

– OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic

– GCC/Compiler version (if compiled from source):

1.2 基本信息

1.2.1 脚本

训练脚本是通过图模式测试自定义Parameter类,构建MatMul单算子网络的例子。脚本如下:

 01 class my_paramter(Parameter):
 02     def __init__(self, data, requires_grad):
 03         super(my_paramter, self).__init__(data, requires_grad)
 04         self.name = None
 05
 06 class Net(nn.Cell):
 07     def __init__(self):
 08         super(Net, self).__init__()
 09         self.matmul = ops.MatMul()
 10         self.weight = my_paramter(Tensor(np.ones((1, 2)), mindspore.float32),name='w', requires_grad=True)
 11
 12     def construct(self, x):
 13         out = self.matmul(self.weight, x)
 14         return out
 15
 16 net = Net()
 17 x = Tensor(np.ones((2, 1)), mindspore.float32)
 18 output = net(x)
 19 print('output:',output)

1.2.2 报错

这里报错信息如下:

Traceback (most recent call last):
  File "/home/test/demo.py", line 16, in <module>
    net = Net()
  File "/home/test/demo.py", line 10, in __init__
    self.weight = my_paramter(Tensor(np.ones((1, 2)), mindspore.float32),name='w', requires_grad=True)
  File "test/lib/python3.9/site-packages/mindspore/nn/cell.py", line 590, in __setattr__
    self._set_attr_for_parameter(name, value)
  File "test/lib/python3.9/site-packages/mindspore/nn/cell.py", line 492, in _set_attr_for_parameter
    self.insert_param_to_cell(name, value)
  File "test/lib/python3.9/site-packages/mindspore/nn/cell.py", line 798, in insert_param_to_cell
    if isinstance(param, Parameter) and param.name == PARAMETER_NAME_DEFAULT:
  File "test/lib/python3.9/site-packages/mindspore/common/parameter.py", line 300, in name
    return self.param_info.name
AttributeError: 'NoneType' object has no attribute 'name'.

原因分析

​ 在MindSpore1.6版本,在construct中创建和使用Tensor。如脚本中第16行代码所示。

​ 接着看报错信息,在AttributeError中,写到*‘NoneType’ object has no attribute ‘name’*,意思是’NoneType’对象没有name属性,这是由于Parameter不允许派生子类,因此不能自行定义类Parameter。建议采用官方Parameter算子进行初始化。

2 解决方法

基于上面已知的原因,很容易做出如下修改:

 01 class Net(nn.Cell):
 02     def __init__(self):
 03         super(Net, self).__init__()
 04         self.matmul = ops.MatMul()
 05         self.weight = Parameter(Tensor(np.ones((1, 2)), mindspore.float32),name='w', requires_grad=True)
 06
 07     def construct(self, x):
 08         out = self.matmul(self.weight, x)
 09         return out
 10
 11 net = Net()
 12 x = Tensor(np.ones((2, 1)), mindspore.float32)
 13 output = net(x)
 14 print('output:',output)

此时执行成功,输出如下:

output: [[2.]]

3 总结

定位报错问题的步骤:

1、 找到报错的用户代码行:self.weight = my_paramter(Tensor(np.ones((1, 2)), mindspore.float32),name=‘w’, requires_grad=True);

2、 根据日志报错信息中的关键字,缩小分析问题的范围: ‘NoneType’ object has no attribute ‘name’;

3、 需要重点关注变量定义、初始化的正确性。

4 参考文档

4.1 API映射

你可能感兴趣的:(python,深度学习,人工智能)