MindSpore报错: `seed2` in `StandardNormal` should be int and >=0
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 脚本
训练脚本是通过构建StandardNormal的单算子网络,生成符合正态分布的随机数。脚本如下:
01 class Net(nn.Cell):
02 def __init__(self, seed=2, seed2=-3):
03 super(Net, self).__init__()
04 self.standard_normal = ops.StandardNormal(seed=seed, seed2=seed2)
05 def construct(self, output_shape):
06 output = self.standard_normal(output_shape)
07 return output
08
09 output_shape = (2, 3, 4)
10 net = Net()
11 output = net(output_shape)
12 print("OUTPUT: ", output)
1.2.2 报错
这里报错信息如下:
Traceback (most recent call last):
File "C:/Users/l30026544/PycharmProjects/q2_map/new/I4DSWV-standardNormal.py", line 16, in <module>
net = Net()
File "C:/Users/l30026544/PycharmProjects/q2_map/new/I4DSWV-standardNormal.py", line 10, in __init__
self.standard_normal = ops.StandardNormal(seed=seed, seed2=seed2)
File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\ops\primitive.py", line 687, in deco
fn(self, *args, **kwargs)
File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\ops\operations\random_ops.py", line 67, in __init__
Validator.check_non_negative_int(seed2, "seed2", self.name)
File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\_checkparam.py", line 304, in check_non_negative_int
return check_number(arg_value, 0, Rel.GE, int, arg_name, prim_name)
File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\_checkparam.py", line 168, in check_number
raise type_except(f'{prim_info} should be {arg_type.__name__} and must {rel_str}, '
ValueError: `seed2` in `StandardNormal` should be int and must >= 0, but got `-3` with type `int`.
原因分析
我们看报错信息,在ValueError中,写到seed2 in StandardNormal should be int and must >= 0, but got -3 with type int,意思是StandardNormal算子里的seed2属性必须是大于等于0的整数, 但是得到了一个负数。官网对两个参数seed, seed2的范围都有进行说明:

2 解决方法
基于上面已知的原因,很容易做出如下修改:
01 class Net(nn.Cell):
02 def __init__(self, seed=2, seed2=3):
03 super(Net, self).__init__()
04 self.standard_normal = ops.StandardNormal(seed=seed, seed2=seed2)
05 def construct(self, output_shape):
06 output = self.standard_normal(output_shape)
07 return output
08
09 output_shape = (2, 3, 4)
10 net = Net()
11 output = net(output_shape)
12 print("OUTPUT: ", output)
此时执行成功,输出如下:
OUTPUT: [[[-0.7503836 -1.4105444 1.689283 1.0585287 ][ 0.19296396 -1.1577806 -0.9638309 -0.01727804][ 0.3017666 -1.1502111 -0.3734214 -0.4361166 ]]
[[ 0.7154948 0.6556154 2.3681476 1.1285974 ][-0.42168498 -0.2680572 -0.9242947 1.0003353 ][-0.4574307 0.36757398 -0.28976655 0.4996464 ]]]
3 总结
定位报错问题的步骤:
1、找到报错的用户代码行:self.standard_normal = ops.StandardNormal(seed=seed, seed2=seed2);
2、 根据日志报错信息中的关键字,缩小分析问题的范围t seed2 in StandardNormal should be int and must >= 0, but got ;
3、需要重点关注变量定义、初始化的正确性。

浙公网安备 33010602011771号