[Solved] MindSpore Error: `seed2` in `StandardNormal` should be int and >=0

1 Error description

1.1 System Environment

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 Basic information

1.2.1 Script

The training script generates random numbers conforming to the normal distribution by constructing a single-operator network of StandardNormal. The script is as follows:

 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 Error reporting

The error message here is as follows:

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`.

Cause Analysis

Let’s look at the error message. In ValueError, it is written that seed2 in  StandardNormal should be int and must >= 0, but got  -3 with type  int, which means that the seed2 attribute in the StandardNormal operator must be an integer greater than or equal to 0, but a negative number is obtained. The official website explains the range of the two parameters seed and seed2:

image.png

2 Solutions

For the reasons known above, it is easy to make the following modifications:

 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)

At this point, the execution is successful, and the output is as follows:

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 Summary

Steps to locate the error report:

1. Find the line of user code that reports the error: self.standard_normal = ops.StandardNormal(seed=seed, seed2=seed2) ;

2. According to the keywords in the log error message, narrow the scope of the analysis problem t  seed2 in  StandardNormal should be int and must >= 0, but got  ;

3. It is necessary to focus on the correctness of variable definition and initialization.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *