Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2a31c9dd
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
2a31c9dd
编写于
12月 08, 2022
作者:
2
201716010711
提交者:
GitHub
12月 08, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
clean fluid task: transfer gaussian random api (#48529)
上级
b731fb82
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
35 addition
and
176 deletion
+35
-176
python/paddle/distribution/normal.py
python/paddle/distribution/normal.py
+5
-3
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+0
-147
python/paddle/fluid/tests/unittests/test_gaussian_random_op.py
...n/paddle/fluid/tests/unittests/test_gaussian_random_op.py
+7
-6
python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py
...addle/fluid/tests/unittests/test_imperative_auto_prune.py
+2
-1
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+1
-1
python/paddle/fluid/tests/unittests/test_manual_seed.py
python/paddle/fluid/tests/unittests/test_manual_seed.py
+5
-4
python/paddle/fluid/tests/unittests/test_random_seed.py
python/paddle/fluid/tests/unittests/test_random_seed.py
+7
-6
python/paddle/fluid/tests/unittests/xpu/test_gaussian_random_op_xpu.py
.../fluid/tests/unittests/xpu/test_gaussian_random_op_xpu.py
+7
-6
python/paddle/tensor/random.py
python/paddle/tensor/random.py
+1
-2
未找到文件。
python/paddle/distribution/normal.py
浏览文件 @
2a31c9dd
...
...
@@ -21,7 +21,8 @@ import paddle
from
paddle.distribution
import
distribution
from
paddle.fluid.data_feeder
import
check_type
,
convert_dtype
from
paddle.fluid.framework
import
_non_static_mode
from
paddle.fluid.layers
import
nn
,
tensor
from
paddle.fluid.layers
import
tensor
from
paddle.tensor
import
random
class
Normal
(
distribution
.
Distribution
):
...
...
@@ -180,8 +181,9 @@ class Normal(distribution.Distribution):
self
.
loc
+
self
.
scale
,
batch_shape
+
shape
,
self
.
dtype
,
0.0
)
zero_tmp_reshape
=
paddle
.
reshape
(
zero_tmp
,
output_shape
)
zero_tmp_shape
=
paddle
.
shape
(
zero_tmp_reshape
)
normal_random_tmp
=
nn
.
gaussian_random
(
normal_random_tmp
=
random
.
gaussian
(
zero_tmp_shape
,
mean
=
0.0
,
std
=
1.0
,
seed
=
seed
,
dtype
=
self
.
dtype
)
output
=
normal_random_tmp
*
(
zero_tmp_reshape
+
self
.
scale
)
...
...
@@ -189,7 +191,7 @@ class Normal(distribution.Distribution):
return
output
else
:
output_shape
=
shape
+
batch_shape
output
=
nn
.
gaussian_random
(
output
=
random
.
gaussian
(
output_shape
,
mean
=
0.0
,
std
=
1.0
,
seed
=
seed
,
dtype
=
self
.
dtype
)
*
(
tensor
.
zeros
(
output_shape
,
dtype
=
self
.
dtype
)
+
self
.
scale
)
output
=
paddle
.
add
(
output
,
self
.
loc
,
name
=
name
)
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
2a31c9dd
...
...
@@ -84,7 +84,6 @@ __all__ = [
'elementwise_div'
,
'elementwise_sub'
,
'elementwise_mul'
,
'gaussian_random'
,
'clip'
,
'clip_by_norm'
,
'mean'
,
...
...
@@ -2720,152 +2719,6 @@ def relu(x, name=None):
from
paddle.fluid.framework
import
convert_np_dtype_to_dtype_
@
deprecated
(
since
=
"2.0.0"
,
update_to
=
"paddle.normal"
)
@
templatedoc
()
def
gaussian_random
(
shape
,
mean
=
0.0
,
std
=
1.0
,
seed
=
0
,
dtype
=
'float32'
,
name
=
None
):
"""
This OP returns a Tensor filled with random values sampled from a Gaussian
distribution, with ``shape`` and ``dtype``.
Args:
shape(list|tuple|Tensor): The shape of the output Tensor. If ``shape``
is a list or tuple, the elements of it should be integers or Tensors
(with the shape [1], and the data type int32 or int64). If ``shape``
is a Tensor, it should be a 1-D Tensor(with the data type int32 or
int64).
mean(float|int, optional): Mean of the output tensor, default is 0.0.
std(float|int, optional): Standard deviation of the output tensor, default
is 1.0.
seed(int, optional): ${seed_comment}
dtype(str|np.dtype|core.VarDesc.VarType, optional): The data type of
the output Tensor. Supported data types: float32, float64.
Default is float32.
name(str, optional): The default value is None. Normally there is no
need for user to set this property. For more information, please
refer to :ref:`api_guide_Name`.
Returns:
Tensor: A Tensor filled with random values sampled from a Gaussian
distribution, with ``shape`` and ``dtype``.
Examples:
.. code-block:: python
import paddle
import paddle.fluid as fluid
paddle.enable_static()
# example 1:
# attr shape is a list which doesn't contain Tensor.
result_1 = fluid.layers.gaussian_random(shape=[3, 4])
# [[-0.31261674, 1.8736548, -0.6274357, 0.96988016],
# [-0.12294637, 0.9554768, 1.5690808, -1.2894802 ],
# [-0.60082096, -0.61138713, 1.5345167, -0.21834975]]
# example 2:
# attr shape is a list which contains Tensor.
dim_1 = fluid.layers.fill_constant([1], "int64", 2)
dim_2 = fluid.layers.fill_constant([1], "int32", 3)
result_2 = fluid.layers.gaussian_random(shape=[dim_1, dim_2])
# [[ 0.51398206, -0.3389769, 0.23597084],
# [ 1.0388143, -1.2015356, -1.0499583 ]]
# example 3:
# attr shape is a Tensor, the data type must be int64 or int32.
var_shape = fluid.data(name='var_shape', shape=[2], dtype="int64")
result_3 = fluid.layers.gaussian_random(var_shape)
# if var_shape's value is [2, 3]
# result_3 is:
# [[-0.12310527, 0.8187662, 1.923219 ]
# [ 0.70721835, 0.5210541, -0.03214082]]
.. code-block:: python
# declarative mode
# required: skiptest
import numpy as np
from paddle import fluid
x = fluid.layers.gaussian_random((2, 3), std=2., seed=10)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
start = fluid.default_startup_program()
main = fluid.default_main_program()
exe.run(start)
x_np, = exe.run(main, feed={}, fetch_list=[x])
x_np
# array([[2.3060477, 2.676496 , 3.9911983],
# [0.9990833, 2.8675377, 2.2279181]], dtype=float32)
.. code-block:: python
# imperative mode
import numpy as np
from paddle import fluid
import paddle.fluid.dygraph as dg
place = fluid.CPUPlace()
with dg.guard(place) as g:
x = fluid.layers.gaussian_random((2, 4), mean=2., dtype="float32", seed=10)
x_np = x.numpy()
x_np
# array([[2.3060477 , 2.676496 , 3.9911983 , 0.9990833 ],
# [2.8675377 , 2.2279181 , 0.79029655, 2.8447366 ]], dtype=float32)
"""
if
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
):
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
if
in_dygraph_mode
():
shape
=
utils
.
convert_shape_to_list
(
shape
)
place
=
_current_expected_place
()
return
_C_ops
.
gaussian
(
shape
,
float
(
mean
),
float
(
std
),
seed
,
dtype
,
place
)
if
_in_legacy_dygraph
():
shape
=
utils
.
convert_shape_to_list
(
shape
)
return
_legacy_C_ops
.
gaussian_random
(
'shape'
,
shape
,
'mean'
,
float
(
mean
),
'std'
,
float
(
std
),
'seed'
,
seed
,
'dtype'
,
dtype
,
)
check_type
(
shape
,
'shape'
,
(
list
,
tuple
,
Variable
),
'gaussian_random/randn'
)
check_dtype
(
dtype
,
'dtype'
,
[
'float32'
,
'float64'
],
'gaussian_random/randn'
)
inputs
=
{}
attrs
=
{
'mean'
:
mean
,
'std'
:
std
,
'seed'
:
seed
,
'dtype'
:
dtype
,
'use_mkldnn'
:
False
,
}
utils
.
get_shape_tensor_inputs
(
inputs
=
inputs
,
attrs
=
attrs
,
shape
=
shape
,
op_type
=
'gaussian_random/randn'
)
helper
=
LayerHelper
(
'gaussian_random'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
)
helper
.
append_op
(
type
=
'gaussian_random'
,
inputs
=
inputs
,
outputs
=
{
'Out'
:
out
},
attrs
=
attrs
)
return
out
def
_elementwise_op
(
helper
):
op_type
=
helper
.
layer_type
x
=
helper
.
kwargs
.
get
(
'x'
,
None
)
...
...
python/paddle/fluid/tests/unittests/test_gaussian_random_op.py
浏览文件 @
2a31c9dd
...
...
@@ -21,6 +21,7 @@ import paddle.fluid as fluid
import
paddle.fluid.core
as
core
from
paddle.fluid.framework
import
_test_eager_guard
from
paddle.fluid.tests.unittests.op_test
import
OpTest
,
convert_uint16_to_float
from
paddle.tensor
import
random
class
TestGaussianRandomOp
(
OpTest
):
...
...
@@ -228,11 +229,11 @@ class TestGaussianRandomAPI(unittest.TestCase):
name
=
"shape_tensor_int64"
,
shape
=
[
2
],
dtype
=
"int64"
)
out_1
=
fluid
.
layers
.
gaussian_random
(
out_1
=
random
.
gaussian
(
shape
=
[
2000
,
500
],
dtype
=
"float32"
,
mean
=
0.0
,
std
=
1.0
,
seed
=
10
)
out_2
=
fluid
.
layers
.
gaussian_random
(
out_2
=
random
.
gaussian
(
shape
=
[
2000
,
positive_2_int32
],
dtype
=
"float32"
,
mean
=
0.0
,
...
...
@@ -240,7 +241,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed
=
10
,
)
out_3
=
fluid
.
layers
.
gaussian_random
(
out_3
=
random
.
gaussian
(
shape
=
[
2000
,
positive_2_int64
],
dtype
=
"float32"
,
mean
=
0.0
,
...
...
@@ -248,7 +249,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed
=
10
,
)
out_4
=
fluid
.
layers
.
gaussian_random
(
out_4
=
random
.
gaussian
(
shape
=
shape_tensor_int32
,
dtype
=
"float32"
,
mean
=
0.0
,
...
...
@@ -256,7 +257,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed
=
10
,
)
out_5
=
fluid
.
layers
.
gaussian_random
(
out_5
=
random
.
gaussian
(
shape
=
shape_tensor_int64
,
dtype
=
"float32"
,
mean
=
0.0
,
...
...
@@ -264,7 +265,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed
=
10
,
)
out_6
=
fluid
.
layers
.
gaussian_random
(
out_6
=
random
.
gaussian
(
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
mean
=
0.0
,
...
...
python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py
浏览文件 @
2a31c9dd
...
...
@@ -19,6 +19,7 @@ import numpy as np
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.framework
import
_test_eager_guard
from
paddle.tensor
import
random
class
AutoPruneLayer0
(
fluid
.
Layer
):
...
...
@@ -487,7 +488,7 @@ class TestImperativeAutoPrune(unittest.TestCase):
def
func_case4_with_no_grad_op_maker
(
self
):
with
fluid
.
dygraph
.
guard
():
out
=
fluid
.
layers
.
gaussian_random
(
shape
=
[
20
,
30
])
out
=
random
.
gaussian
(
shape
=
[
20
,
30
])
loss
=
paddle
.
mean
(
out
)
loss
.
backward
()
self
.
assertIsNone
(
out
.
_grad_ivar
())
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
2a31c9dd
...
...
@@ -2557,7 +2557,7 @@ class TestBook(LayerTest):
with
program_guard
(
fluid
.
default_main_program
(),
fluid
.
default_startup_program
()
):
out
=
layers
.
gaussian_random
(
shape
=
[
20
,
30
])
out
=
random
.
gaussian
(
shape
=
[
20
,
30
])
return
out
def
make_sum
(
self
):
...
...
python/paddle/fluid/tests/unittests/test_manual_seed.py
浏览文件 @
2a31c9dd
...
...
@@ -18,6 +18,7 @@ import numpy as np
import
paddle
import
paddle.fluid
as
fluid
from
paddle.tensor
import
random
class
TestManualSeed
(
unittest
.
TestCase
):
...
...
@@ -25,13 +26,13 @@ class TestManualSeed(unittest.TestCase):
fluid
.
enable_dygraph
()
gen
=
paddle
.
seed
(
12312321111
)
x
=
fluid
.
layers
.
gaussian_random
([
10
],
dtype
=
"float32"
)
x
=
random
.
gaussian
([
10
],
dtype
=
"float32"
)
st1
=
gen
.
get_state
()
x1
=
fluid
.
layers
.
gaussian_random
([
10
],
dtype
=
"float32"
)
x1
=
random
.
gaussian
([
10
],
dtype
=
"float32"
)
gen
.
set_state
(
st1
)
x2
=
fluid
.
layers
.
gaussian_random
([
10
],
dtype
=
"float32"
)
x2
=
random
.
gaussian
([
10
],
dtype
=
"float32"
)
gen
.
manual_seed
(
12312321111
)
x3
=
fluid
.
layers
.
gaussian_random
([
10
],
dtype
=
"float32"
)
x3
=
random
.
gaussian
([
10
],
dtype
=
"float32"
)
x_np
=
x
.
numpy
()
x1_np
=
x1
.
numpy
()
x2_np
=
x2
.
numpy
()
...
...
python/paddle/fluid/tests/unittests/test_random_seed.py
浏览文件 @
2a31c9dd
...
...
@@ -21,6 +21,7 @@ import paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
import
paddle.fluid.generator
as
generator
from
paddle.tensor
import
random
class
TestGeneratorSeed
(
unittest
.
TestCase
):
...
...
@@ -148,13 +149,13 @@ class TestGeneratorSeed(unittest.TestCase):
fluid
.
enable_dygraph
()
gen
=
paddle
.
seed
(
12312321111
)
x
=
fluid
.
layers
.
gaussian_random
([
10
],
dtype
=
"float32"
)
x
=
random
.
gaussian
([
10
],
dtype
=
"float32"
)
st1
=
gen
.
get_state
()
x1
=
fluid
.
layers
.
gaussian_random
([
10
],
dtype
=
"float32"
)
x1
=
random
.
gaussian
([
10
],
dtype
=
"float32"
)
gen
.
set_state
(
st1
)
x2
=
fluid
.
layers
.
gaussian_random
([
10
],
dtype
=
"float32"
)
x2
=
random
.
gaussian
([
10
],
dtype
=
"float32"
)
gen
.
manual_seed
(
12312321111
)
x3
=
fluid
.
layers
.
gaussian_random
([
10
],
dtype
=
"float32"
)
x3
=
random
.
gaussian
([
10
],
dtype
=
"float32"
)
x_np
=
x
.
numpy
()
x1_np
=
x1
.
numpy
()
x2_np
=
x2
.
numpy
()
...
...
@@ -175,8 +176,8 @@ class TestGeneratorSeed(unittest.TestCase):
with
fluid
.
program_guard
(
train_program
,
startup_program
):
# example 1:
# attr shape is a list which doesn't contain tensor Variable.
result_1
=
fluid
.
layers
.
gaussian_random
(
shape
=
[
3
,
4
])
result_2
=
fluid
.
layers
.
gaussian_random
(
shape
=
[
3
,
4
])
result_1
=
random
.
gaussian
(
shape
=
[
3
,
4
])
result_2
=
random
.
gaussian
(
shape
=
[
3
,
4
])
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
exe
.
run
(
startup_program
)
...
...
python/paddle/fluid/tests/unittests/xpu/test_gaussian_random_op_xpu.py
浏览文件 @
2a31c9dd
...
...
@@ -29,6 +29,7 @@ import paddle
import
paddle.fluid
as
fluid
paddle
.
enable_static
()
from
paddle.tensor
import
random
class
XPUTestGaussianRandomOp
(
XPUOpTestWrapper
):
...
...
@@ -192,11 +193,11 @@ class TestGaussianRandomAPI(unittest.TestCase):
name
=
"shape_tensor_int64"
,
shape
=
[
2
],
dtype
=
"int64"
)
out_1
=
fluid
.
layers
.
gaussian_random
(
out_1
=
random
.
gaussian
(
shape
=
[
2000
,
500
],
dtype
=
"float32"
,
mean
=
0.0
,
std
=
1.0
,
seed
=
10
)
out_2
=
fluid
.
layers
.
gaussian_random
(
out_2
=
random
.
gaussian
(
shape
=
[
2000
,
positive_2_int32
],
dtype
=
"float32"
,
mean
=
0.0
,
...
...
@@ -204,7 +205,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed
=
10
,
)
out_3
=
fluid
.
layers
.
gaussian_random
(
out_3
=
random
.
gaussian
(
shape
=
[
2000
,
positive_2_int64
],
dtype
=
"float32"
,
mean
=
0.0
,
...
...
@@ -212,7 +213,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed
=
10
,
)
out_4
=
fluid
.
layers
.
gaussian_random
(
out_4
=
random
.
gaussian
(
shape
=
shape_tensor_int32
,
dtype
=
"float32"
,
mean
=
0.0
,
...
...
@@ -220,7 +221,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed
=
10
,
)
out_5
=
fluid
.
layers
.
gaussian_random
(
out_5
=
random
.
gaussian
(
shape
=
shape_tensor_int64
,
dtype
=
"float32"
,
mean
=
0.0
,
...
...
@@ -228,7 +229,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed
=
10
,
)
out_6
=
fluid
.
layers
.
gaussian_random
(
out_6
=
random
.
gaussian
(
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
mean
=
0.0
,
...
...
python/paddle/tensor/random.py
浏览文件 @
2a31c9dd
...
...
@@ -314,7 +314,7 @@ def uniform_random_batch_size_like(
return
out
def
gaussian
(
shape
,
mean
=
0.0
,
std
=
1.0
,
dtype
=
None
,
name
=
None
):
def
gaussian
(
shape
,
mean
=
0.0
,
std
=
1.0
,
seed
=
0
,
dtype
=
None
,
name
=
None
):
"""
Returns a Tensor filled with random values sampled from a Gaussian
distribution, with ``shape`` and ``dtype``.
...
...
@@ -338,7 +338,6 @@ def gaussian(shape, mean=0.0, std=1.0, dtype=None, name=None):
distribution, with ``shape`` and ``dtype``.
"""
op_type_for_check
=
'gaussian/standard_normal/randn/normal'
seed
=
0
if
dtype
is
None
:
dtype
=
paddle
.
framework
.
get_default_dtype
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录