!9246 Add more description to make the examples in docs of seed.py run

From: @peixu_ren
Reviewed-by: @sunnybeike,@zichun_ye
Signed-off-by:
This commit is contained in:
mindspore-ci-bot 2020-12-02 09:50:01 +08:00 committed by Gitee
commit ecd641d02c
1 changed files with 31 additions and 22 deletions

View File

@ -59,6 +59,13 @@ def set_seed(seed):
Examples:
>>> from mindspore.ops import composite as C
>>> from mindspore import Tensor
>>>
>>> # Note: (1) Please make sure the code is running in PYNATIVE MODE;
>>> # (2) Becasuse Composite-level ops need parameters to be Tensors, for below examples,
>>> # when using C.uniform operator, minval and maxval are initialised as:
>>> minval = Tensor(1.0, mstype.float32)
>>> maxval = Tensor(2.0, mstype.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
@ -86,53 +93,53 @@ def set_seed(seed):
>>>
>>> # 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 = C.uniform((1, 4), 1.0, 2.0) # C1
>>> c2 = C.uniform((1, 4), 1.0, 2.0) # C2
>>> c1 = C.uniform((1, 4), minval, maxval) # C1
>>> c2 = C.uniform((1, 4), minval, maxval) # C2
>>> Rerun the program will get different results:
>>> c1 = C.uniform((1, 4), 1.0, 2.0) # C3
>>> c2 = C.uniform((1, 4), 1.0, 2.0) # C4
>>> c1 = C.uniform((1, 4), minval, maxval) # C3
>>> c2 = C.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 caculate 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 = C.uniform((1, 4), 1.0, 2.0) # C1
>>> c2 = C.uniform((1, 4), 1.0, 2.0) # C2
>>> c1 = C.uniform((1, 4), minval, maxval) # C1
>>> c2 = C.uniform((1, 4), minval, maxval) # C2
>>> # Rerun the program will get the same results:
>>> set_seed(1234)
>>> c1 = C.uniform((1, 4), 1.0, 2.0) # C1
>>> c2 = C.uniform((1, 4), 1.0, 2.0) # C2
>>> c1 = C.uniform((1, 4), minval, maxval) # C1
>>> c2 = C.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 caculate 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 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C1
>>> c2 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C2
>>> c1 = C.uniform((1, 4), minval, maxval, seed=2) # C1
>>> c2 = C.uniform((1, 4), minval, maxval, seed=2) # C2
>>> Rerun the program will get the same results:
>>> set_seed(1234)
>>> c1 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C1
>>> c2 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C2
>>> c1 = C.uniform((1, 4), minval, maxval, seed=2) # C1
>>> c2 = C.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 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C1
>>> c2 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C2
>>> c1 = C.uniform((1, 4), minval, maxval, seed=2) # C1
>>> c2 = C.uniform((1, 4), minval, maxval, seed=2) # C2
>>> # Rerun the program will get the same results:
>>> c1 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C1
>>> c2 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C2
>>> c1 = C.uniform((1, 4), minval, maxval, seed=2) # C1
>>> c2 = C.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 = C.uniform((1, 4), 1.0, 2.0, seed=2) # C1
>>> c1 = C.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 = C.uniform((1, 4), 1.0, 2.0, seed=2) # still get C1
>>> c2 = C.uniform((1, 4), minval, maxval, seed=2) # still get C1
"""
if not isinstance(seed, int):
raise TypeError("The seed must be type of int.")
@ -200,11 +207,13 @@ def _get_graph_seed(op_seed, kernel_name):
So, the state of the seed regarding to this op should be recorded.
A simple illustration should be:
If a random op is called twice within one program, the two results should be different:
print(C.uniform((1, 4), seed=1)) # generates 'A1'
print(C.uniform((1, 4), seed=1)) # generates 'A2'
minval = Tensor(1.0, mstype.float32)
maxval = Tensor(2.0, mstype.float32)
print(C.uniform((1, 4), minval, maxval, seed=1)) # generates 'A1'
print(C.uniform((1, 4), minval, maxval, seed=1)) # generates 'A2'
If the same program runs again, it repeat the results:
print(C.uniform((1, 4), seed=1)) # generates 'A1'
print(C.uniform((1, 4), seed=1)) # generates 'A2'
print(C.uniform((1, 4), minval, maxval, seed=1)) # generates 'A1'
print(C.uniform((1, 4), minval, maxval, seed=1)) # generates 'A2'
Returns:
Interger. The current graph-level seed.