!9696 Add some examples for random ops

From: @peixu_ren
Reviewed-by: @sunnybeike,@zichun_ye
Signed-off-by: @sunnybeike
This commit is contained in:
mindspore-ci-bot 2020-12-10 00:30:26 +08:00 committed by Gitee
commit 1033166d8a
2 changed files with 42 additions and 38 deletions

View File

@ -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)

View File

@ -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