From 16bbdc4eca0c2e5dc450be14831d98083b4ba1ca Mon Sep 17 00:00:00 2001 From: peixu_ren Date: Tue, 8 Dec 2020 21:06:23 -0500 Subject: [PATCH] Add some examples for random ops --- mindspore/ops/composite/random_ops.py | 35 ++++++++++++-------- mindspore/ops/operations/random_ops.py | 45 ++++++++++++-------------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/mindspore/ops/composite/random_ops.py b/mindspore/ops/composite/random_ops.py index 2a97467e5b..aaffe8f189 100644 --- a/mindspore/ops/composite/random_ops.py +++ b/mindspore/ops/composite/random_ops.py @@ -46,13 +46,13 @@ def normal(shape, mean, stddev, seed=None): The dtype is float32. Examples: - >>> shape = (2, 4) - >>> mean = Tensor(1.0, mstype.float32) + >>> shape = (3, 1, 2) + >>> mean = Tensor(np.array([[3, 4], [5, 6]]), mstype.float32) >>> stddev = Tensor(1.0, mstype.float32) >>> output = C.normal(shape, mean, stddev, seed=5) - >>> print(output) - [[ 1.0996436 0.44371283 0.11127508 -0.48055804] - [ 0.31989878 -1.0644426 1.5076542 1.2290289 ]] + >>> result = output.shape + >>> print(result) + (3, 2, 2) """ mean_dtype = F.dtype(mean) stddev_dtype = F.dtype(stddev) @@ -135,10 +135,13 @@ def uniform(shape, minval, maxval, seed=None, dtype=mstype.float32): >>> output = C.uniform(shape, minval, maxval, seed=5, dtype=mstype.int32) >>> >>> # For continuous uniform distribution, minval and maxval can be multi-dimentional: - >>> shape = (4, 2) - >>> minval = Tensor([1.0, 2.0], mstype.float32) - >>> maxval = Tensor([4.0, 5.0], mstype.float32) + >>> shape = (3, 1, 2) + >>> minval = Tensor(np.array([[3, 4], [5, 6]]), mstype.float32) + >>> maxval = Tensor([8.0, 10.0], mstype.float32) >>> output = C.uniform(shape, minval, maxval, seed=5) + >>> result = output.shape + >>> print(result) + (3, 2, 2) """ minval_dtype = F.dtype(minval) maxval_dtype = F.dtype(maxval) @@ -172,10 +175,13 @@ def gamma(shape, alpha, beta, seed=None): The dtype is float32. Examples: - >>> shape = (4, 16) - >>> alpha = Tensor(1.0, mstype.float32) - >>> beta = Tensor(1.0, mstype.float32) + >>> shape = (3, 1, 2) + >>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mstype.float32) + >>> beta = Tensor(np.array([1.0]), mstype.float32) >>> output = C.gamma(shape, alpha, beta, seed=5) + >>> result = output.shape + >>> print(result) + (3, 2, 2) """ seed1, seed2 = _get_seed(seed, "gamma") random_gamma = P.Gamma(seed1, seed2) @@ -197,9 +203,12 @@ def poisson(shape, mean, seed=None): The dtype is float32. Examples: - >>> shape = (4, 16) - >>> mean = Tensor(1.0, mstype.float32) + >>> shape = (4, 1) + >>> mean = Tensor(np.array([5.0, 10.0]), mstype.float32) >>> output = C.poisson(shape, mean, seed=5) + >>> result = output.shape + >>> print(result) + (4, 2) """ seed1, seed2 = _get_seed(seed, "poisson") random_poisson = P.Poisson(seed1, seed2) diff --git a/mindspore/ops/operations/random_ops.py b/mindspore/ops/operations/random_ops.py index 94098d6151..04db0f4571 100644 --- a/mindspore/ops/operations/random_ops.py +++ b/mindspore/ops/operations/random_ops.py @@ -144,14 +144,14 @@ class Gamma(PrimitiveWithInfer): ``Ascend`` Examples: - >>> shape = (2, 2) - >>> alpha = Tensor(1.0, mstype.float32) - >>> beta = Tensor(1.0, mstype.float32) + >>> shape = (3, 1, 2) + >>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mstype.float32) + >>> beta = Tensor(np.array([1.0]), mstype.float32) >>> gamma = ops.Gamma(seed=3) >>> output = gamma(shape, alpha, beta) - >>> print(output) - [[0.21962446 0.33740655] - [1.0859369 0.25875127]] + >>> result = output.shape + >>> print(result) + (3, 2, 2) """ @prim_attr_register @@ -203,10 +203,13 @@ class Poisson(PrimitiveWithInfer): ``Ascend`` Examples: - >>> shape = (4, 16) - >>> mean = Tensor(5.0, mstype.float32) + >>> shape = (4, 1) + >>> mean = Tensor(np.array([5.0, 10.0]), mstype.float32) >>> poisson = ops.Poisson(seed=5) >>> output = poisson(shape, mean) + >>> result = output.shape + >>> print(result) + (4, 2) """ @prim_attr_register @@ -266,9 +269,9 @@ class UniformInt(PrimitiveWithInfer): >>> maxval = Tensor(5, mstype.int32) >>> uniform_int = ops.UniformInt(seed=10) >>> output = uniform_int(shape, minval, maxval) - >>> print(output) - [[4 2 1 3] - [4 3 4 5]] + >>> result = output.shape + >>> print(result) + (2, 4) """ @prim_attr_register @@ -319,9 +322,9 @@ class UniformReal(PrimitiveWithInfer): >>> shape = (2, 2) >>> uniformreal = ops.UniformReal(seed=2) >>> output = uniformreal(shape) - >>> print(output) - [[0.4359949 0.18508208] - [0.02592623 0.93154085]] + >>> result = output.shape + >>> print(result) + (2, 2) """ @prim_attr_register @@ -433,17 +436,9 @@ class RandomCategorical(PrimitiveWithInfer): >>> x = np.random.random((10, 5)).astype(np.float32) >>> net = Net(8) >>> output = net(Tensor(x)) - >>> print(output) - [[0 2 0 3 4 2 0 2] - [0 2 1 3 4 2 0 2] - [0 2 0 3 4 2 0 2] - [0 2 1 3 4 2 0 2] - [0 2 1 3 4 2 0 2] - [0 2 1 3 4 2 0 2] - [0 2 0 3 4 2 0 2] - [0 2 0 3 4 2 0 2] - [0 2 1 3 4 3 0 3] - [0 2 1 3 4 2 0 2]] + >>> result = output.shape + >>> print(result) + (10, 8) """ @prim_attr_register