mindspore.set_seed
- mindspore.set_seed(seed)[源代码]
设置全局种子。
Note
全局种子可用于numpy.random, mindspore.common.Initializer, mindspore.ops.composite.random_ops以及mindspore.nn.probability.distribution。
如果没有设置全局种子,这些包将会各自使用自己的种子,numpy.random和mindspore.common.Initializer将会随机选择种子值,mindspore.ops.composite.random_ops和mindspore.nn.probability.distribution将会使用零作为种子值。
numpy.random.seed()设置的种子仅能被numpy.random使用,而这个API设置的种子也可被numpy.random使用,因此推荐使用这个API设置所有的种子。
在semi_auto_parallel/auto_parallel模式下,使用set_seed时,同一节点具有相同形状和相同切分策略的权重将被初始化为相同的结果,否则,将被初始化为不同的结果。
- 参数:
seed (int) - 设置的全局种子。
- 异常:
ValueError - 种子值非法 (小于0)。
TypeError - 种子值非整型数。
样例:
>>> import numpy as np >>> import mindspore.ops as ops >>> from mindspore import Tensor, set_seed, Parameter >>> from mindspore.common.initializer import initializer >>> import mindspore as ms >>> # Note: (1) Please make sure the code is running in PYNATIVE MODE; >>> # (2) Because Composite-level ops need parameters to be Tensors, for below examples, >>> # when using ops.uniform operator, minval and maxval are initialised as: >>> minval = Tensor(1.0, ms.float32) >>> maxval = Tensor(2.0, ms.float32) >>> >>> # 1. If global seed is not set, numpy.random and initializer will choose a random seed: >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1 >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2 >>> # Rerun the program will get different results: >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A3 >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A4 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W3 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W4 >>> >>> # 2. If global seed is set, numpy.random and initializer will use it: >>> set_seed(1234) >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1 >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2 >>> # Rerun the program will get the same results: >>> set_seed(1234) >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1 >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2 >>> >>> # 3. If neither global seed nor op seed is set, mindspore.ops.composite.random_ops and >>> # mindspore.nn.probability.distribution will choose a random seed: >>> c1 = ops.uniform((1, 4), minval, maxval) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval) # C2 >>> # Rerun the program will get different results: >>> c1 = ops.uniform((1, 4), minval, maxval) # C3 >>> c2 = ops.uniform((1, 4), minval, maxval) # C4 >>> >>> # 4. If global seed is set, but op seed is not set, mindspore.ops.composite.random_ops and >>> # mindspore.nn.probability.distribution will calculate a seed according to global seed and >>> # default op seed. Each call will change the default op seed, thus each call get different >>> # results. >>> set_seed(1234) >>> c1 = ops.uniform((1, 4), minval, maxval) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval) # C2 >>> # Rerun the program will get the same results: >>> set_seed(1234) >>> c1 = ops.uniform((1, 4), minval, maxval) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval) # C2 >>> >>> # 5. If both global seed and op seed are set, mindspore.ops.composite.random_ops and >>> # mindspore.nn.probability.distribution will calculate a seed according to global seed and >>> # op seed counter. Each call will change the op seed counter, thus each call get different >>> # results. >>> set_seed(1234) >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2 >>> # Rerun the program will get the same results: >>> set_seed(1234) >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2 >>> >>> # 6. If op seed is set but global seed is not set, 0 will be used as global seed. Then >>> # mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution act as in >>> # condition 5. >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2 >>> # Rerun the program will get the different results: >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2 >>> >>> # 7. Recall set_seed() in the program will reset numpy seed and op seed counter of >>> # mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution. >>> set_seed(1234) >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1 >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> set_seed(1234) >>> np_2 = np.random.normal(0, 1, [1]).astype(np.float32) # still get A1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # still get C1