forked from mindspore-Ecosystem/mindspore
!7497 change composite.random_ops.get_seed to internal interface
Merge pull request !7497 from yihuaijie/random
This commit is contained in:
commit
4ef0163963
|
@ -57,73 +57,73 @@ def set_seed(seed):
|
|||
TypeError: If seed isn't a int.
|
||||
|
||||
Examples:
|
||||
1. If global seed is not set, numpy.random and initializer will choose a random seed:
|
||||
>>> # 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 diferent results:
|
||||
>>> # Rerun the program will get diferent 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:
|
||||
>>>
|
||||
>>> 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:
|
||||
>>> # 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:
|
||||
>>>
|
||||
>>> # 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)) # C1
|
||||
>>> c2 = C.uniform((1, 4)) # C2
|
||||
Rerun the program will get different results:
|
||||
>>> Rerun the program will get different results:
|
||||
>>> c1 = C.uniform((1, 4)) # C3
|
||||
>>> c2 = C.uniform((1, 4)) # 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.
|
||||
>>>
|
||||
>>> # 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)) # C1
|
||||
>>> c2 = C.uniform((1, 4)) # C2
|
||||
Rerun the program will get the same results:
|
||||
>>> # Rerun the program will get the same results:
|
||||
>>> set_seed(1234)
|
||||
>>> c1 = C.uniform((1, 4)) # C1
|
||||
>>> c2 = C.uniform((1, 4)) # 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.
|
||||
>>>
|
||||
>>> # 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), seed=2) # C1
|
||||
>>> c2 = C.uniform((1, 4), seed=2) # C2
|
||||
Rerun the program will get the same results:
|
||||
>>> Rerun the program will get the same results:
|
||||
>>> set_seed(1234)
|
||||
>>> c1 = C.uniform((1, 4), seed=2) # C1
|
||||
>>> c2 = C.uniform((1, 4), 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.
|
||||
>>>
|
||||
>>> # 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), seed=2) # C1
|
||||
>>> c2 = C.uniform((1, 4), seed=2) # C2
|
||||
Rerun the program will get the same results:
|
||||
>>> #Rerun the program will get the same results:
|
||||
>>> c1 = C.uniform((1, 4), seed=2) # C1
|
||||
>>> c2 = C.uniform((1, 4), 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.
|
||||
>>>
|
||||
>>> # 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), seed=2) # C1
|
||||
|
|
|
@ -22,7 +22,7 @@ from ...common import dtype as mstype
|
|||
from ...common.seed import _get_graph_seed
|
||||
|
||||
@constexpr
|
||||
def get_seed(op_seed, kernel_name):
|
||||
def _get_seed(op_seed, kernel_name):
|
||||
"Get the graph-level seed."
|
||||
return _get_graph_seed(op_seed, kernel_name)
|
||||
|
||||
|
@ -57,7 +57,7 @@ def normal(shape, mean, stddev, seed=None):
|
|||
stddev_dtype = F.dtype(stddev)
|
||||
const_utils.check_valid_type(mean_dtype, mstype.int_type + (mstype.float16, mstype.float32), 'normal')
|
||||
const_utils.check_valid_type(stddev_dtype, mstype.int_type + (mstype.float16, mstype.float32), 'normal')
|
||||
seed1, seed2 = get_seed(seed, "normal")
|
||||
seed1, seed2 = _get_seed(seed, "normal")
|
||||
stdnormal = P.StandardNormal(seed1, seed2)
|
||||
random_normal = stdnormal(shape)
|
||||
value = random_normal * stddev + mean
|
||||
|
@ -94,7 +94,7 @@ def laplace(shape, mean, lambda_param, seed=None):
|
|||
lambda_param_dtype = F.dtype(lambda_param)
|
||||
const_utils.check_tensors_dtype_same(mean_dtype, mstype.float32, "laplace")
|
||||
const_utils.check_tensors_dtype_same(lambda_param_dtype, mstype.float32, "laplace")
|
||||
seed1, seed2 = get_seed(seed, "laplace")
|
||||
seed1, seed2 = _get_seed(seed, "laplace")
|
||||
stdlaplace = P.StandardLaplace(seed1, seed2)
|
||||
rnd = stdlaplace(shape)
|
||||
value = rnd * lambda_param + mean
|
||||
|
@ -144,7 +144,7 @@ def uniform(shape, minval, maxval, seed=None, dtype=mstype.float32):
|
|||
const_utils.check_valid_type(dtype, [mstype.int32, mstype.float32], 'uniform')
|
||||
const_utils.check_tensors_dtype_same(minval_dtype, dtype, "uniform")
|
||||
const_utils.check_tensors_dtype_same(maxval_dtype, dtype, "uniform")
|
||||
seed1, seed2 = get_seed(seed, "uniform")
|
||||
seed1, seed2 = _get_seed(seed, "uniform")
|
||||
if const_utils.is_same_type(dtype, mstype.int32):
|
||||
random_uniform = P.UniformInt(seed1, seed2)
|
||||
value = random_uniform(shape, minval, maxval)
|
||||
|
@ -176,7 +176,7 @@ def gamma(shape, alpha, beta, seed=None):
|
|||
>>> beta = Tensor(1.0, mstype.float32)
|
||||
>>> output = C.gamma(shape, alpha, beta, seed=5)
|
||||
"""
|
||||
seed1, seed2 = get_seed(seed, "gamma")
|
||||
seed1, seed2 = _get_seed(seed, "gamma")
|
||||
random_gamma = P.Gamma(seed1, seed2)
|
||||
value = random_gamma(shape, alpha, beta)
|
||||
return value
|
||||
|
@ -200,7 +200,7 @@ def poisson(shape, mean, seed=None):
|
|||
>>> mean = Tensor(1.0, mstype.float32)
|
||||
>>> output = C.poisson(shape, mean, seed=5)
|
||||
"""
|
||||
seed1, seed2 = get_seed(seed, "poisson")
|
||||
seed1, seed2 = _get_seed(seed, "poisson")
|
||||
random_poisson = P.Poisson(seed1, seed2)
|
||||
value = random_poisson(shape, mean)
|
||||
return value
|
||||
|
|
Loading…
Reference in New Issue