Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
e6675f4f
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
e6675f4f
编写于
8月 23, 2020
作者:
Z
zhupengyang
提交者:
GitHub
8月 23, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
normal: support mean and std tensor; randn = standard_normal (#26367)
上级
3a9417f4
变更
5
展开全部
隐藏空白更改
内联
并排
Showing
5 changed file
with
501 addition
and
141 deletion
+501
-141
python/paddle/__init__.py
python/paddle/__init__.py
+2
-1
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+1
-0
python/paddle/fluid/tests/unittests/test_normal.py
python/paddle/fluid/tests/unittests/test_normal.py
+197
-0
python/paddle/tensor/__init__.py
python/paddle/tensor/__init__.py
+2
-1
python/paddle/tensor/random.py
python/paddle/tensor/random.py
+299
-139
未找到文件。
python/paddle/__init__.py
浏览文件 @
e6675f4f
...
...
@@ -194,7 +194,8 @@ from .tensor.math import clip #DEFINE_ALIAS
from
.tensor.math
import
trace
#DEFINE_ALIAS
from
.tensor.math
import
kron
#DEFINE_ALIAS
from
.tensor.math
import
prod
#DEFINE_ALIAS
# from .tensor.random import gaussin #DEFINE_ALIAS
from
.tensor.random
import
standard_normal
from
.tensor.random
import
normal
from
.tensor.random
import
uniform
#DEFINE_ALIAS
from
.tensor.random
import
shuffle
#DEFINE_ALIAS
from
.tensor.random
import
randn
#DEFINE_ALIAS
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
e6675f4f
...
...
@@ -10466,6 +10466,7 @@ def uniform_random_batch_size_like(input,
return out
@deprecated(since="2.0.0", update_to="paddle.normal")
@templatedoc()
def gaussian_random(shape,
mean=0.0,
...
...
python/paddle/fluid/tests/unittests/test_normal.py
0 → 100644
浏览文件 @
e6675f4f
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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
unittest
import
numpy
as
np
import
paddle
import
copy
np
.
random
.
seed
(
10
)
class
TestNormalAPI
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
mean
=
1.0
self
.
std
=
0.0
self
.
shape
=
None
self
.
repeat_num
=
1000
self
.
set_attrs
()
self
.
dtype
=
self
.
get_dtype
()
self
.
place
=
paddle
.
CUDAPlace
(
0
)
\
if
paddle
.
fluid
.
core
.
is_compiled_with_cuda
()
\
else
paddle
.
CPUPlace
()
def
set_attrs
(
self
):
self
.
shape
=
[
8
,
12
]
def
get_shape
(
self
):
if
isinstance
(
self
.
mean
,
np
.
ndarray
):
shape
=
self
.
mean
.
shape
elif
isinstance
(
self
.
std
,
np
.
ndarray
):
shape
=
self
.
std
.
shape
else
:
shape
=
self
.
shape
return
list
(
shape
)
def
get_dtype
(
self
):
if
isinstance
(
self
.
mean
,
np
.
ndarray
):
return
self
.
mean
.
dtype
elif
isinstance
(
self
.
std
,
np
.
ndarray
):
return
self
.
std
.
dtype
else
:
return
'float32'
def
static_api
(
self
):
shape
=
self
.
get_shape
()
ret_all_shape
=
copy
.
deepcopy
(
shape
)
ret_all_shape
.
insert
(
0
,
self
.
repeat_num
)
ret_all
=
np
.
zeros
(
ret_all_shape
,
self
.
dtype
)
if
isinstance
(
self
.
mean
,
np
.
ndarray
)
\
and
isinstance
(
self
.
std
,
np
.
ndarray
):
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
()):
mean
=
paddle
.
data
(
'Mean'
,
self
.
mean
.
shape
,
self
.
mean
.
dtype
)
std
=
paddle
.
data
(
'Std'
,
self
.
std
.
shape
,
self
.
std
.
dtype
)
out
=
paddle
.
normal
(
mean
,
std
,
self
.
shape
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
for
i
in
range
(
self
.
repeat_num
):
ret
=
exe
.
run
(
feed
=
{
'Mean'
:
self
.
mean
,
'Std'
:
self
.
std
.
reshape
(
shape
)
},
fetch_list
=
[
out
])
ret_all
[
i
]
=
ret
[
0
]
return
ret_all
elif
isinstance
(
self
.
mean
,
np
.
ndarray
):
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
()):
mean
=
paddle
.
data
(
'Mean'
,
self
.
mean
.
shape
,
self
.
mean
.
dtype
)
out
=
paddle
.
normal
(
mean
,
self
.
std
,
self
.
shape
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
for
i
in
range
(
self
.
repeat_num
):
ret
=
exe
.
run
(
feed
=
{
'Mean'
:
self
.
mean
},
fetch_list
=
[
out
])
ret_all
[
i
]
=
ret
[
0
]
return
ret_all
elif
isinstance
(
self
.
std
,
np
.
ndarray
):
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
()):
std
=
paddle
.
data
(
'Std'
,
self
.
std
.
shape
,
self
.
std
.
dtype
)
out
=
paddle
.
normal
(
self
.
mean
,
std
,
self
.
shape
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
for
i
in
range
(
self
.
repeat_num
):
ret
=
exe
.
run
(
feed
=
{
'Std'
:
self
.
std
},
fetch_list
=
[
out
])
ret_all
[
i
]
=
ret
[
0
]
return
ret_all
else
:
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
()):
out
=
paddle
.
normal
(
self
.
mean
,
self
.
std
,
self
.
shape
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
for
i
in
range
(
self
.
repeat_num
):
ret
=
exe
.
run
(
fetch_list
=
[
out
])
ret_all
[
i
]
=
ret
[
0
]
return
ret_all
def
dygraph_api
(
self
):
paddle
.
disable_static
(
self
.
place
)
shape
=
self
.
get_shape
()
ret_all_shape
=
copy
.
deepcopy
(
shape
)
ret_all_shape
.
insert
(
0
,
self
.
repeat_num
)
ret_all
=
np
.
zeros
(
ret_all_shape
,
self
.
dtype
)
mean
=
paddle
.
to_tensor
(
self
.
mean
)
\
if
isinstance
(
self
.
mean
,
np
.
ndarray
)
else
self
.
mean
std
=
paddle
.
to_tensor
(
self
.
std
)
\
if
isinstance
(
self
.
std
,
np
.
ndarray
)
else
self
.
std
for
i
in
range
(
self
.
repeat_num
):
out
=
paddle
.
normal
(
mean
,
std
,
self
.
shape
)
ret_all
[
i
]
=
out
.
numpy
()
paddle
.
enable_static
()
return
ret_all
def
test_api
(
self
):
ret_static
=
self
.
static_api
()
ret_dygraph
=
self
.
dygraph_api
()
for
ret
in
[
ret_static
,
ret_dygraph
]:
shape_ref
=
self
.
get_shape
()
self
.
assertEqual
(
shape_ref
,
list
(
ret
[
0
].
shape
))
ret
=
ret
.
flatten
().
reshape
([
self
.
repeat_num
,
-
1
])
mean
=
np
.
mean
(
ret
,
axis
=
0
)
std
=
np
.
std
(
ret
,
axis
=
0
)
mean_ref
=
self
.
mean
.
reshape
([
1
,
-
1
])
\
if
isinstance
(
self
.
mean
,
np
.
ndarray
)
else
self
.
mean
std_ref
=
self
.
std
.
reshape
([
1
,
-
1
])
\
if
isinstance
(
self
.
std
,
np
.
ndarray
)
else
self
.
std
self
.
assertTrue
(
np
.
allclose
(
mean_ref
,
mean
,
0.1
,
0.1
))
self
.
assertTrue
(
np
.
allclose
(
std_ref
,
std
,
0.1
,
0.1
))
class
TestNormalAPI_mean_is_tensor
(
TestNormalAPI
):
def
set_attrs
(
self
):
self
.
mean
=
np
.
random
.
uniform
(
-
2
,
-
1
,
[
2
,
3
,
4
,
5
]).
astype
(
'float64'
)
class
TestNormalAPI_std_is_tensor
(
TestNormalAPI
):
def
set_attrs
(
self
):
self
.
std
=
np
.
random
.
uniform
(
0.7
,
1
,
[
2
,
3
,
17
]).
astype
(
'float64'
)
class
TestNormalAPI_mean_std_are_tensor
(
TestNormalAPI
):
def
set_attrs
(
self
):
self
.
mean
=
np
.
random
.
uniform
(
1
,
2
,
[
1
,
100
]).
astype
(
'float64'
)
self
.
std
=
np
.
random
.
uniform
(
0.5
,
1
,
[
1
,
100
]).
astype
(
'float64'
)
class
TestNormalAPI_mean_std_are_tensor_with_different_dtype
(
TestNormalAPI
):
def
set_attrs
(
self
):
self
.
mean
=
np
.
random
.
uniform
(
1
,
2
,
[
100
]).
astype
(
'float64'
)
self
.
std
=
np
.
random
.
uniform
(
1
,
2
,
[
100
]).
astype
(
'float32'
)
class
TestNormalAlias
(
unittest
.
TestCase
):
def
test_alias
(
self
):
paddle
.
disable_static
()
shape
=
[
1
,
2
,
3
]
out1
=
paddle
.
normal
(
shape
=
shape
)
out2
=
paddle
.
tensor
.
normal
(
shape
=
shape
)
out3
=
paddle
.
tensor
.
random
.
normal
(
shape
=
shape
)
paddle
.
enable_static
()
class
TestNormalErrors
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
()):
mean
=
[
1
,
2
,
3
]
self
.
assertRaises
(
TypeError
,
paddle
.
normal
,
mean
)
std
=
[
1
,
2
,
3
]
self
.
assertRaises
(
TypeError
,
paddle
.
normal
,
std
=
std
)
mean
=
paddle
.
data
(
'Mean'
,
[
100
],
'int32'
)
self
.
assertRaises
(
TypeError
,
paddle
.
normal
,
mean
)
std
=
paddle
.
data
(
'Std'
,
[
100
],
'int32'
)
self
.
assertRaises
(
TypeError
,
paddle
.
normal
,
mean
=
1.0
,
std
=
std
)
self
.
assertRaises
(
TypeError
,
paddle
.
normal
,
shape
=
1
)
self
.
assertRaises
(
TypeError
,
paddle
.
normal
,
shape
=
[
1.0
])
shape
=
paddle
.
data
(
'Shape'
,
[
100
],
'float32'
)
self
.
assertRaises
(
TypeError
,
paddle
.
normal
,
shape
=
shape
)
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/tensor/__init__.py
浏览文件 @
e6675f4f
...
...
@@ -162,7 +162,8 @@ from .math import clip #DEFINE_ALIAS
from
.math
import
trace
#DEFINE_ALIAS
from
.math
import
kron
#DEFINE_ALIAS
from
.math
import
prod
#DEFINE_ALIAS
# from .random import gaussin #DEFINE_ALIAS
from
.random
import
standard_normal
from
.random
import
normal
from
.random
import
uniform
#DEFINE_ALIAS
from
.random
import
shuffle
#DEFINE_ALIAS
from
.random
import
randn
#DEFINE_ALIAS
...
...
python/paddle/tensor/random.py
浏览文件 @
e6675f4f
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录