1 Error description
1.1 System Environment
Hardware Environment(Ascend/GPU/CPU): Ascend
Software Environment:
– MindSpore version (source or binary): 1.8.0
– Python version (eg, Python 3.7.5): 3.7.6
– OS platform and distribution (eg, Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic
– GCC/Compiler version (if compiled from source):
1.2 Basic information
1.2.1 Script
The training script implements the cell list container by constructing a single-operator network of CellList. The script is as follows:
01 class ListNoneExample(nn.Cell):
02 def __init__(self):
03 super(ListNoneExample, self).__init__()
04 self.lst = nn.CellList([nn.ReLU(), None, nn.ReLU()])
05
06 def construct(self, x):
07 output = []
08 for op in self.lst:
09 output.append(op(x))
10 return output
11
12 input = Tensor(np.random.normal(0, 2, (2, 1)).astype(np.float32))
13 example = ListNoneExample()
14 output = example(input)
15 print("Output:", output)
1.2.2 Error reporting
The error message here is as follows:
Traceback (most recent call last):
File "C:/Users/l30026544/PycharmProjects/q2_map/new/I3OGVW.py", line 31, in <module>
example = ListNoneExample()
File "C:/Users/l30026544/PycharmProjects/q2_map/new/I3OGVW.py", line 19, in __init__
self.lst = nn.CellList([nn.ReLU(), None, nn.ReLU()])
File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\layer\container.py", line 310, in __init__
self.extend(args[0])
File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\layer\container.py", line 405, in extend
if _valid_cell(cell, cls_name):
File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\layer\container.py", line 39, in _valid_cell
raise TypeError(f'{msg_prefix} each cell should be subclass of Cell, but got {type(cell).__name__}.')
TypeError: For 'CellList', each cell should be subclass of Cell, but got NoneType.
Cause Analysis
Let’s look at the error message. In TypeError, write For ‘CellList’, each cell should be subclass of Cell, but got NoneType.
, which means that for the CellList operator, each incoming cell should be nn.Cell A subclass of , but gets the None type. Check line 4 of the behavior of initializing CellList in the network, and find that a None is passed in, so an error is reported. In order to solve this problem, just replace the None here with an object that inherits from the base class Cell class, and the same function can be achieved.
2 Solutions
For the reasons known above, it is easy to make the following modifications:
01 class NoneCell(nn.Cell):
02 def __init__(self):
03 super(NoneCell, self).__init__()
04
05 def construct(self, x):
06 return x
07
08 class ListNoneExample(nn.Cell):
09 def __init__(self):
10 super(ListNoneExample, self).__init__()
11 self.lst = nn.CellList([nn.ReLU(), NoneCell(), nn.ReLU()])
12
13 def construct(self, x):
14 output = []
15 for op in self.lst:
16 output.append(op(x))
17 return output
18
19 input = Tensor(np.random.normal(0, 2, (2, 1)).astype(np.float32))
20 example = ListNoneExample()
21 output = example(input)
22 print("Output:", output)
At this point, the execution is successful, and the output is as follows:
Output: (Tensor(shape=[2, 1], dtype=Float32, value=
[[1.09826946e+000],
[0.00000000e+000]]), Tensor(shape=[2, 1], dtype=Float32, value=
[[1.09826946e+000],
[-2.74355006e+000]]), Tensor(shape=[2, 1], dtype=Float32, value=
[[1.09826946e+000],
[0.00000000e+000]]))
3 Summary
Steps to locate the error report:
1. Find the line of user code that reports the error: self.lst = nn.CellList([nn.ReLU(), None, nn.ReLU()]) ;
2. According to the keywords in the log error message, narrow down the scope of the analysis problem each cell should be subclass of Cell, but got NoneType ;
3. It is necessary to focus on the correctness of variable definition and initialization.