网络构建与训练常见错误分析

  

静态图模式下,网络构建与训练过程的常见的报错类型如下表所示:

context配置问题

执行网络训练时,需要指定后端设备,使用方式是:set_context(device_target=device)。MindSpore支持CPU,GPU和昇腾后端Ascend。如果在GPU设备上,错误指定后端设备为Ascend,即set_context(device_target="Ascend"),会得到如下报错信息:

ValueError: For 'set_context', package type mindspore-gpu support 'device_target' type gpu or cpu, but got Ascend.

脚本设置的运行后端要求与实际的硬件设备相匹配。

参考实例链接:

MindSpore 配置问题 - ‘set_context’配置报错

关于context配置的详细使用说明请参考’set_context’

语法问题

  • construct函数参数错误

    MindSpore中神经网络的基本构成单元为nn.Cell。模型或神经网络层应当继承该基类。基类的成员函数construct是定义要执行的计算逻辑,所有继承类都必须重写此方法。construct函数的定义原型为:

    def construct(self, *inputs, **kwargs):
    

    在重写该函数时,有时会出现下面的错误信息:

    TypeError: The function construct needs 0 positional argument and 0 default argument, but provided 1
    

    这是因为,用户自定义实现construct函数时,函数参数列表错误,缺少self,例如"def construct(*inputs, **kwargs):"。此时,MindSpore在进行语法解析时发生报错。

    参考实例链接:

    MindSpore 语法问题 - ‘construct’ 函数定义报错

  • 控制流语法错误

    静态图模式下,Python代码并不是由Python解释器去执行,而是将代码编译成静态计算图,然后执行静态计算图。MindSpore支持的控制流语法涉及if语句、for语句以及while语句。if语句可能存在不同分支返回对象的属性不一致,导致报错。报错信息如下所示:

    TypeError: Cannot join the return values of different branches, perhaps you need to make them equal.
    Type Join Failed: dtype1 = Float32, dtype2 = Float16.
    

    此时由报错信息可知,报错原因是if语句不同分支返回值的类型不一致:一个是float32,另一个是float16,导致编译报错。

    ValueError: Cannot join the return values of different branches, perhaps you need to make them equal.
    Shape Join Failed: shape1 = (2, 3, 4, 5), shape2 = ().
    

    由报错信息可知,报错原因是if语句不同分支返回值的维度shape不一致:一个是2*3*4*5的四位Tensor,另一个是标量,导致编译报错。

    参考实例链接:

    MindSpore 语法问题 - Type(Shape) Join Failed

    for语句以及while语句可能存在循环次数过大,导致函数调用栈超限的问题。报错信息如下所示:

    RuntimeError: Exceed function call depth limit 1000, (function call depth: 1001, simulate call depth: 997).
    

    超出函数调用栈限制问题,一种解决方式是简化网络的结构,减少循环次数。另一种方式是使用set_context(max_call_depth=value)调大函数调用栈的阈值。

    参考实例链接:

    MindSpore 语法问题 - Exceed function call depth limit

算子编译错误

算子编译错误主要是由于输入参数不符合要求,算子功能不支持等问题。

例如,使用ReduceSum算子时,输入数据超过八维时报错信息如下:

RuntimeError: ({'errCode': 'E80012', 'op_name': 'reduce_sum_d', 'param_name': 'x', 'min_value': 0, 'max_value': 8, 'real_value': 10}, 'In op, the num of dimensions of input/output[x] should be in the range of [0, 8], but actually is [10].')

参考实例链接:

MindSpore 算子编译问题 - ReduceSum算子不支持八维以上输入

例如,Parameter参数不支持类型自动转换,使用Parameter算子时,进行数据类型转换时报错,报错信息如下:

RuntimeError: Data type conversion of 'Parameter' is not supported, so data type int32 cannot be converted to data type float32 automatically.

参考实例链接:

MindSpore 算子编译问题 - ScatterNdUpdate算子参数类型不一致报错

算子执行错误

算子执行问题,发生的原因主要包括输入数据问题、算子实现问题以及算子初始化问题等场景。算子执行错误的分析方法一般可采用类比法。

具体分析可参考实例:

MindSpore 算子执行错误 - nn.GroupNorm算子输出异常

资源不足

在调试网络的时候,经常会遇到Out Of Memory报错,MindSpore在Ascend设备上对内存分成4层进行管理。包括Runtime,Context,双游标和内存复用。

关于MindSpore在昇腾后端(Ascend)上的内存管理及常见问题的具体内容,请参考MindSpore Ascend 内存管理