Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
9b45018d
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
9b45018d
编写于
7月 19, 2020
作者:
P
peixu_ren
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add random normal op at MindSpore front-end
上级
156c42ef
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
214 addition
and
59 deletion
+214
-59
mindspore/nn/distribution/bernoulli.py
mindspore/nn/distribution/bernoulli.py
+3
-2
mindspore/nn/distribution/normal.py
mindspore/nn/distribution/normal.py
+3
-2
mindspore/ops/composite/__init__.py
mindspore/ops/composite/__init__.py
+2
-0
mindspore/ops/composite/random_ops.py
mindspore/ops/composite/random_ops.py
+63
-0
mindspore/ops/operations/__init__.py
mindspore/ops/operations/__init__.py
+2
-2
mindspore/ops/operations/random_ops.py
mindspore/ops/operations/random_ops.py
+42
-43
tests/st/ops/ascend/test_aicpu_ops/test_normal.py
tests/st/ops/ascend/test_aicpu_ops/test_normal.py
+0
-2
tests/st/ops/ascend/test_aicpu_ops/test_standard_normal.py
tests/st/ops/ascend/test_aicpu_ops/test_standard_normal.py
+0
-6
tests/st/ops/gpu/test_normal.py
tests/st/ops/gpu/test_normal.py
+56
-0
tests/st/ops/gpu/test_standard_normal.py
tests/st/ops/gpu/test_standard_normal.py
+41
-0
tests/ut/python/ops/test_ops.py
tests/ut/python/ops/test_ops.py
+2
-2
未找到文件。
mindspore/nn/distribution/bernoulli.py
浏览文件 @
9b45018d
...
...
@@ -14,6 +14,7 @@
# ============================================================================
"""Bernoulli Distribution"""
from
mindspore.ops
import
operations
as
P
from
mindspore.ops
import
composite
as
C
from
.distribution
import
Distribution
from
._utils.utils
import
cast_to_tensor
,
check_prob
from
...common
import
dtype
as
mstype
...
...
@@ -53,6 +54,7 @@ class Bernoulli(Distribution):
check_prob
(
self
.
_probs
)
else
:
self
.
_probs
=
probs
self
.
seed
=
seed
# ops needed for the class
self
.
log
=
P
.
Log
()
...
...
@@ -64,7 +66,6 @@ class Bernoulli(Distribution):
self
.
const
=
P
.
ScalarToArray
()
self
.
less
=
P
.
Less
()
self
.
cast
=
P
.
Cast
()
self
.
normal
=
P
.
Normal
(
seed
=
seed
)
self
.
erf
=
P
.
Erf
()
self
.
sqrt
=
P
.
Sqrt
()
...
...
@@ -159,7 +160,7 @@ class Bernoulli(Distribution):
mean_zero
=
self
.
const
(
0.0
)
sd_one
=
self
.
const
(
1.0
)
sqrt_two
=
self
.
sqrt
(
self
.
const
(
2.0
))
sample_norm
=
self
.
normal
(
sample_shape
,
mean_zero
,
sd_one
)
sample_norm
=
C
.
normal
(
sample_shape
,
mean_zero
,
sd_one
,
self
.
seed
)
sample_uniform
=
0.5
*
(
1
+
self
.
erf
(
self
.
realdiv
(
sample_norm
,
sqrt_two
)))
sample
=
self
.
less
(
sample_uniform
,
probs1
)
sample
=
self
.
cast
(
sample
,
self
.
_dtype
)
...
...
mindspore/nn/distribution/normal.py
浏览文件 @
9b45018d
...
...
@@ -15,6 +15,7 @@
"""Normal Distribution"""
import
numpy
as
np
from
mindspore.ops
import
operations
as
P
from
mindspore.ops
import
composite
as
C
from
.distribution
import
Distribution
from
._utils.utils
import
convert_to_batch
,
check_greater_equal_zero
from
...common
import
dtype
as
mstype
...
...
@@ -60,6 +61,7 @@ class Normal(Distribution):
else
:
self
.
_mean_value
=
mean
self
.
_sd_value
=
sd
self
.
seed
=
seed
#ops needed for the class
self
.
exp
=
P
.
Exp
()
...
...
@@ -70,7 +72,6 @@ class Normal(Distribution):
self
.
sqrt
=
P
.
Sqrt
()
self
.
realdiv
=
P
.
RealDiv
()
self
.
expm1
=
P
.
Expm1
()
if
get_context
(
'device_target'
)
==
'Ascend'
else
self
.
_expm1_by_step
self
.
normal
=
P
.
Normal
(
seed
=
seed
)
self
.
shape
=
P
.
Shape
()
self
.
zeroslike
=
P
.
ZerosLike
()
self
.
const
=
P
.
ScalarToArray
()
...
...
@@ -163,7 +164,7 @@ class Normal(Distribution):
sample_shape
=
shape
+
batch_shape
mean_zero
=
self
.
const
(
0.0
)
sd_one
=
self
.
const
(
1.0
)
sample_norm
=
self
.
normal
(
sample_shape
,
mean_zero
,
sd_one
)
sample_norm
=
C
.
normal
(
sample_shape
,
mean_zero
,
sd_one
,
self
.
seed
)
sample
=
self
.
add
(
mean
,
self
.
mul
(
sample_norm
,
sd
))
return
sample
return
None
mindspore/ops/composite/__init__.py
浏览文件 @
9b45018d
...
...
@@ -27,6 +27,7 @@ from .clip_ops import clip_by_value
from
.multitype_ops.add_impl
import
hyper_add
from
.multitype_ops.ones_like_impl
import
ones_like
from
.multitype_ops.zeros_like_impl
import
zeros_like
from
.random_ops
import
normal
__all__
=
[
...
...
@@ -47,4 +48,5 @@ __all__ = [
'zeros_like'
,
'ones_like'
,
'zip_operation'
,
'normal'
,
'clip_by_value'
,]
mindspore/ops/composite/random_ops.py
0 → 100644
浏览文件 @
9b45018d
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Operations for random number generatos."""
from
mindspore.ops.primitive
import
constexpr
from
..
import
operations
as
P
# set graph-level RNG seed
_GRAPH_SEED
=
0
@
constexpr
def
set_seed
(
seed
):
global
_GRAPH_SEED
_GRAPH_SEED
=
seed
@
constexpr
def
get_seed
():
return
_GRAPH_SEED
def
normal
(
shape
,
mean
,
stddev
,
seed
):
"""
Generates random numbers according to the Normal (or Gaussian) random number distribution.
It is defined as:
Args:
- **shape** (tuple) - The shape of random tensor to be generated.
- **mean** (Tensor) - The mean μ distribution parameter, which specifies the location of the peak.
With float32 data type.
- **stddev** (Tensor) - The deviation σ distribution parameter. With float32 data type.
- **seed** (int): Seed is used as entropy source for Random number engines generating pseudo-random numbers.
Default: 0.
Returns:
Tensor. The shape should be the broadcasted shape of Input "shape" and shapes of mean and stddev.
The dtype is float32.
Examples:
>>> shape = (4, 16)
>>> mean = Tensor(1.0, mstype.float32)
>>> stddev = Tensor(1.0, mstype.float32)
>>> output = C.normal(shape, mean, stddev, seed=5)
"""
set_seed
(
10
)
seed1
=
get_seed
()
seed2
=
seed
stdnormal
=
P
.
StandardNormal
(
seed1
,
seed2
)
rnd
=
stdnormal
(
shape
)
value
=
rnd
*
stddev
+
mean
return
value
mindspore/ops/operations/__init__.py
浏览文件 @
9b45018d
...
...
@@ -54,7 +54,7 @@ from .math_ops import (Abs, ACos, Asin, Asinh, AddN, AccumulateNV2, AssignAdd, A
Sin
,
Sqrt
,
Rsqrt
,
BesselI0e
,
BesselI1e
,
TruncateDiv
,
TruncateMod
,
Square
,
Sub
,
TensorAdd
,
Sign
,
Round
,
SquareSumAll
,
Atan
,
Atanh
,
Cosh
,
Sinh
,
Eps
,
Tan
)
from
.random_ops
import
(
RandomChoiceWithMask
,
Normal
,
Gamma
,
Poisson
,
UniformInt
,
UniformReal
,
from
.random_ops
import
(
RandomChoiceWithMask
,
Standard
Normal
,
Gamma
,
Poisson
,
UniformInt
,
UniformReal
,
RandomCategorical
,
Laplace
)
from
.nn_ops
import
(
LSTM
,
SGD
,
Adam
,
FusedSparseAdam
,
FusedSparseLazyAdam
,
ApplyMomentum
,
BatchNorm
,
BiasAdd
,
Conv2D
,
...
...
@@ -174,7 +174,7 @@ __all__ = [
'HSigmoid'
,
'Tanh'
,
'RandomChoiceWithMask'
,
'Normal'
,
'
Standard
Normal'
,
'Gamma'
,
'Poisson'
,
'UniformInt'
,
...
...
mindspore/ops/operations/random_ops.py
浏览文件 @
9b45018d
...
...
@@ -22,6 +22,48 @@ from ..primitive import PrimitiveWithInfer, prim_attr_register
from
.._utils
import
get_broadcast_shape
class
StandardNormal
(
PrimitiveWithInfer
):
r
"""
Generates random numbers according to the standard Normal (or Gaussian) random number distribution.
Args:
seed (int): Random seed. Default: 0.
seed2 (int): Random seed2. Default: 0.
Inputs:
- **shape** (tuple) - The shape of random tensor to be generated. Only constant value is allowed.
Outputs:
Tensor. The shape should be the broadcasted shape of Input "shape" and shapes of mean and stddev.
The dtype is float32.
Examples:
>>> shape = (4, 16)
>>> stdnormal = P.StandardNormal(seed=2)
>>> output = stdnormal(shape)
"""
@
prim_attr_register
def
__init__
(
self
,
seed
=
0
,
seed2
=
0
):
"""Init StandardNormal"""
self
.
init_prim_io_names
(
inputs
=
[
'shape'
],
outputs
=
[
'output'
])
validator
.
check_value_type
(
'seed'
,
seed
,
[
int
],
self
.
name
)
validator
.
check_value_type
(
'seed2'
,
seed2
,
[
int
],
self
.
name
)
def
__infer__
(
self
,
shape
):
shape_v
=
shape
[
"value"
]
if
shape_v
is
None
:
raise
ValueError
(
f
"For
{
self
.
name
}
, shape must be const."
)
validator
.
check_value_type
(
"shape"
,
shape_v
,
[
tuple
],
self
.
name
)
for
i
,
shape_i
in
enumerate
(
shape_v
):
validator
.
check_integer
(
"shape[%d]"
%
i
,
shape_i
,
0
,
Rel
.
GT
,
self
.
name
)
out
=
{
'shape'
:
shape_v
,
'dtype'
:
mstype
.
float32
,
'value'
:
None
}
return
out
class
Laplace
(
PrimitiveWithInfer
):
r
"""
Generates random numbers according to the Laplace random number distribution.
...
...
@@ -393,46 +435,3 @@ class RandomCategorical(PrimitiveWithInfer):
return
{
'shape'
:
(
x_shape
),
'dtype'
:
(
self
.
dtype
),
'value'
:
None
}
class
Normal
(
PrimitiveWithInfer
):
"""
Generates random samples from a normal(Gaussian) distribution.
Args:
seed (int): Random seed. Default: 0.
Inputs:
- **shape** (tuple[int]) - The shape of output tensor. Only constant value is allowed.
- **mean** (Tensor) - The mean of the distribution, with float32 data type.
- **stddev** (Tensor) - The standard deviation of the distribution, with float32 data type.
Outputs:
Tensor, with the given shape from the specific distribution and float32 data type.
Examples:
>>> normal = P.Normal()
>>> mean = Tensor(0., mstype.float32)
>>> stddev = Tensor(1., mstype.float32)
>>> out = normal((32, 3, 3), mean, stddev)
"""
@
prim_attr_register
def
__init__
(
self
,
seed
=
0
):
"""Init Normal"""
validator
.
check_value_type
(
"seed"
,
seed
,
[
int
],
self
.
name
)
def
__infer__
(
self
,
shape
,
mean
,
stddev
):
shape_value
=
shape
[
"value"
]
if
shape_value
is
None
:
raise
ValueError
(
f
"For
{
self
.
name
}
, shape must be const."
)
validator
.
check_value_type
(
"shape"
,
shape_value
,
[
tuple
],
self
.
name
)
for
i
,
shape_i
in
enumerate
(
shape_value
):
validator
.
check_integer
(
"shape[%d]"
%
i
,
shape_i
,
0
,
Rel
.
GE
,
self
.
name
)
validator
.
check_tensor_type_same
({
"mean"
:
mean
[
"dtype"
]},
[
mstype
.
float32
],
self
.
name
)
validator
.
check_tensor_type_same
({
"stddev"
:
stddev
[
"dtype"
]},
[
mstype
.
float32
],
self
.
name
)
out
=
{
"shape"
:
shape_value
,
"dtype"
:
mstype
.
float32
,
"value"
:
None
}
return
out
tests/st/ops/ascend/test_aicpu_ops/test_normal.py
浏览文件 @
9b45018d
...
...
@@ -43,7 +43,6 @@ def test_net_1D():
net
=
Net
(
shape
,
seed
)
tmean
,
tstddev
=
Tensor
(
mean
,
mstype
.
float32
),
Tensor
(
stddev
,
mstype
.
float32
)
output
=
net
(
tmean
,
tstddev
)
print
(
output
.
asnumpy
())
assert
output
.
shape
==
(
3
,
2
,
4
)
...
...
@@ -55,5 +54,4 @@ def test_net_ND():
net
=
Net
(
shape
,
seed
)
tmean
,
tstddev
=
Tensor
(
mean
,
mstype
.
float32
),
Tensor
(
stddev
,
mstype
.
float32
)
output
=
net
(
tmean
,
tstddev
)
print
(
output
.
asnumpy
())
assert
output
.
shape
==
(
3
,
2
,
2
)
tests/st/ops/ascend/test_aicpu_ops/test_standard_normal.py
浏览文件 @
9b45018d
...
...
@@ -13,13 +13,8 @@
# limitations under the License.
# ============================================================================
import
numpy
as
np
import
pytest
import
mindspore.context
as
context
import
mindspore.nn
as
nn
from
mindspore
import
Tensor
from
mindspore.common
import
dtype
as
mstype
from
mindspore.ops
import
operations
as
P
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"Ascend"
)
...
...
@@ -43,5 +38,4 @@ def test_net():
shape
=
(
3
,
2
,
4
)
net
=
Net
(
shape
,
seed
,
seed2
)
output
=
net
()
print
(
output
.
asnumpy
())
assert
output
.
shape
==
(
3
,
2
,
4
)
tests/st/ops/gpu/test_normal.py
0 → 100644
浏览文件 @
9b45018d
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import
numpy
as
np
import
mindspore.context
as
context
import
mindspore.nn
as
nn
from
mindspore
import
Tensor
from
mindspore.common
import
dtype
as
mstype
from
mindspore.ops
import
composite
as
C
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"GPU"
)
class
Net
(
nn
.
Cell
):
def
__init__
(
self
,
shape
,
seed
=
0
):
super
(
Net
,
self
).
__init__
()
self
.
shape
=
shape
self
.
seed
=
seed
def
construct
(
self
,
mean
,
stddev
):
return
C
.
normal
(
self
.
shape
,
mean
,
stddev
,
self
.
seed
)
def
test_net_1D
():
seed
=
10
shape
=
(
3
,
2
,
4
)
mean
=
1.0
stddev
=
1.0
net
=
Net
(
shape
,
seed
)
tmean
,
tstddev
=
Tensor
(
mean
,
mstype
.
float32
),
Tensor
(
stddev
,
mstype
.
float32
)
output
=
net
(
tmean
,
tstddev
)
assert
output
.
shape
==
(
3
,
2
,
4
)
def
test_net_ND
():
seed
=
10
shape
=
(
3
,
1
,
2
)
mean
=
np
.
array
([[[
1
],
[
2
]],
[[
3
],
[
4
]],
[[
5
],
[
6
]]]).
astype
(
np
.
float32
)
stddev
=
np
.
array
([
1.0
]).
astype
(
np
.
float32
)
net
=
Net
(
shape
,
seed
)
tmean
,
tstddev
=
Tensor
(
mean
,
mstype
.
float32
),
Tensor
(
stddev
,
mstype
.
float32
)
output
=
net
(
tmean
,
tstddev
)
assert
output
.
shape
==
(
3
,
2
,
2
)
tests/st/ops/gpu/test_standard_normal.py
0 → 100644
浏览文件 @
9b45018d
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import
mindspore.context
as
context
import
mindspore.nn
as
nn
from
mindspore.ops
import
operations
as
P
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"GPU"
)
class
Net
(
nn
.
Cell
):
def
__init__
(
self
,
shape
,
seed
=
0
,
seed2
=
0
):
super
(
Net
,
self
).
__init__
()
self
.
shape
=
shape
self
.
seed
=
seed
self
.
seed2
=
seed2
self
.
stdnormal
=
P
.
StandardNormal
(
seed
,
seed2
)
def
construct
(
self
):
return
self
.
stdnormal
(
self
.
shape
,
self
.
seed
,
self
.
seed2
)
def
test_net
():
seed
=
10
seed2
=
10
shape
=
(
3
,
2
,
4
)
net
=
Net
(
shape
,
seed
,
seed2
)
output
=
net
()
assert
output
.
shape
==
(
3
,
2
,
4
)
tests/ut/python/ops/test_ops.py
浏览文件 @
9b45018d
...
...
@@ -533,10 +533,10 @@ class NormalNet(nn.Cell):
def
__init__
(
self
,
shape
=
None
,
seed
=
0
):
super
(
NormalNet
,
self
).
__init__
()
self
.
shape
=
shape
self
.
normal
=
P
.
Normal
(
seed
=
seed
)
self
.
seed
=
seed
def
construct
(
self
,
mean
,
stddev
):
out
=
self
.
normal
(
self
.
shape
,
mean
,
stddev
)
out
=
C
.
normal
(
self
.
shape
,
mean
,
stddev
,
self
.
seed
)
return
out
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录