未验证 提交 2a31c9dd 编写于 作者: 2 201716010711 提交者: GitHub

clean fluid task: transfer gaussian random api (#48529)

上级 b731fb82
...@@ -21,7 +21,8 @@ import paddle ...@@ -21,7 +21,8 @@ import paddle
from paddle.distribution import distribution from paddle.distribution import distribution
from paddle.fluid.data_feeder import check_type, convert_dtype from paddle.fluid.data_feeder import check_type, convert_dtype
from paddle.fluid.framework import _non_static_mode 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): class Normal(distribution.Distribution):
...@@ -180,8 +181,9 @@ class Normal(distribution.Distribution): ...@@ -180,8 +181,9 @@ class Normal(distribution.Distribution):
self.loc + self.scale, batch_shape + shape, self.dtype, 0.0 self.loc + self.scale, batch_shape + shape, self.dtype, 0.0
) )
zero_tmp_reshape = paddle.reshape(zero_tmp, output_shape) zero_tmp_reshape = paddle.reshape(zero_tmp, output_shape)
zero_tmp_shape = paddle.shape(zero_tmp_reshape) 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 zero_tmp_shape, mean=0.0, std=1.0, seed=seed, dtype=self.dtype
) )
output = normal_random_tmp * (zero_tmp_reshape + self.scale) output = normal_random_tmp * (zero_tmp_reshape + self.scale)
...@@ -189,7 +191,7 @@ class Normal(distribution.Distribution): ...@@ -189,7 +191,7 @@ class Normal(distribution.Distribution):
return output return output
else: else:
output_shape = shape + batch_shape 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 output_shape, mean=0.0, std=1.0, seed=seed, dtype=self.dtype
) * (tensor.zeros(output_shape, dtype=self.dtype) + self.scale) ) * (tensor.zeros(output_shape, dtype=self.dtype) + self.scale)
output = paddle.add(output, self.loc, name=name) output = paddle.add(output, self.loc, name=name)
......
...@@ -84,7 +84,6 @@ __all__ = [ ...@@ -84,7 +84,6 @@ __all__ = [
'elementwise_div', 'elementwise_div',
'elementwise_sub', 'elementwise_sub',
'elementwise_mul', 'elementwise_mul',
'gaussian_random',
'clip', 'clip',
'clip_by_norm', 'clip_by_norm',
'mean', 'mean',
...@@ -2720,152 +2719,6 @@ def relu(x, name=None): ...@@ -2720,152 +2719,6 @@ def relu(x, name=None):
from paddle.fluid.framework import convert_np_dtype_to_dtype_ 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): def _elementwise_op(helper):
op_type = helper.layer_type op_type = helper.layer_type
x = helper.kwargs.get('x', None) x = helper.kwargs.get('x', None)
......
...@@ -21,6 +21,7 @@ import paddle.fluid as fluid ...@@ -21,6 +21,7 @@ import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid.framework import _test_eager_guard from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.tests.unittests.op_test import OpTest, convert_uint16_to_float from paddle.fluid.tests.unittests.op_test import OpTest, convert_uint16_to_float
from paddle.tensor import random
class TestGaussianRandomOp(OpTest): class TestGaussianRandomOp(OpTest):
...@@ -228,11 +229,11 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -228,11 +229,11 @@ class TestGaussianRandomAPI(unittest.TestCase):
name="shape_tensor_int64", shape=[2], dtype="int64" 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 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], shape=[2000, positive_2_int32],
dtype="float32", dtype="float32",
mean=0.0, mean=0.0,
...@@ -240,7 +241,7 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -240,7 +241,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed=10, seed=10,
) )
out_3 = fluid.layers.gaussian_random( out_3 = random.gaussian(
shape=[2000, positive_2_int64], shape=[2000, positive_2_int64],
dtype="float32", dtype="float32",
mean=0.0, mean=0.0,
...@@ -248,7 +249,7 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -248,7 +249,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed=10, seed=10,
) )
out_4 = fluid.layers.gaussian_random( out_4 = random.gaussian(
shape=shape_tensor_int32, shape=shape_tensor_int32,
dtype="float32", dtype="float32",
mean=0.0, mean=0.0,
...@@ -256,7 +257,7 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -256,7 +257,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed=10, seed=10,
) )
out_5 = fluid.layers.gaussian_random( out_5 = random.gaussian(
shape=shape_tensor_int64, shape=shape_tensor_int64,
dtype="float32", dtype="float32",
mean=0.0, mean=0.0,
...@@ -264,7 +265,7 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -264,7 +265,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed=10, seed=10,
) )
out_6 = fluid.layers.gaussian_random( out_6 = random.gaussian(
shape=shape_tensor_int64, shape=shape_tensor_int64,
dtype=np.float32, dtype=np.float32,
mean=0.0, mean=0.0,
......
...@@ -19,6 +19,7 @@ import numpy as np ...@@ -19,6 +19,7 @@ import numpy as np
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.framework import _test_eager_guard from paddle.fluid.framework import _test_eager_guard
from paddle.tensor import random
class AutoPruneLayer0(fluid.Layer): class AutoPruneLayer0(fluid.Layer):
...@@ -487,7 +488,7 @@ class TestImperativeAutoPrune(unittest.TestCase): ...@@ -487,7 +488,7 @@ class TestImperativeAutoPrune(unittest.TestCase):
def func_case4_with_no_grad_op_maker(self): def func_case4_with_no_grad_op_maker(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
out = fluid.layers.gaussian_random(shape=[20, 30]) out = random.gaussian(shape=[20, 30])
loss = paddle.mean(out) loss = paddle.mean(out)
loss.backward() loss.backward()
self.assertIsNone(out._grad_ivar()) self.assertIsNone(out._grad_ivar())
......
...@@ -2557,7 +2557,7 @@ class TestBook(LayerTest): ...@@ -2557,7 +2557,7 @@ class TestBook(LayerTest):
with program_guard( with program_guard(
fluid.default_main_program(), fluid.default_startup_program() fluid.default_main_program(), fluid.default_startup_program()
): ):
out = layers.gaussian_random(shape=[20, 30]) out = random.gaussian(shape=[20, 30])
return out return out
def make_sum(self): def make_sum(self):
......
...@@ -18,6 +18,7 @@ import numpy as np ...@@ -18,6 +18,7 @@ import numpy as np
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.tensor import random
class TestManualSeed(unittest.TestCase): class TestManualSeed(unittest.TestCase):
...@@ -25,13 +26,13 @@ class TestManualSeed(unittest.TestCase): ...@@ -25,13 +26,13 @@ class TestManualSeed(unittest.TestCase):
fluid.enable_dygraph() fluid.enable_dygraph()
gen = paddle.seed(12312321111) gen = paddle.seed(12312321111)
x = fluid.layers.gaussian_random([10], dtype="float32") x = random.gaussian([10], dtype="float32")
st1 = gen.get_state() st1 = gen.get_state()
x1 = fluid.layers.gaussian_random([10], dtype="float32") x1 = random.gaussian([10], dtype="float32")
gen.set_state(st1) gen.set_state(st1)
x2 = fluid.layers.gaussian_random([10], dtype="float32") x2 = random.gaussian([10], dtype="float32")
gen.manual_seed(12312321111) gen.manual_seed(12312321111)
x3 = fluid.layers.gaussian_random([10], dtype="float32") x3 = random.gaussian([10], dtype="float32")
x_np = x.numpy() x_np = x.numpy()
x1_np = x1.numpy() x1_np = x1.numpy()
x2_np = x2.numpy() x2_np = x2.numpy()
......
...@@ -21,6 +21,7 @@ import paddle ...@@ -21,6 +21,7 @@ import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.fluid.generator as generator import paddle.fluid.generator as generator
from paddle.tensor import random
class TestGeneratorSeed(unittest.TestCase): class TestGeneratorSeed(unittest.TestCase):
...@@ -148,13 +149,13 @@ class TestGeneratorSeed(unittest.TestCase): ...@@ -148,13 +149,13 @@ class TestGeneratorSeed(unittest.TestCase):
fluid.enable_dygraph() fluid.enable_dygraph()
gen = paddle.seed(12312321111) gen = paddle.seed(12312321111)
x = fluid.layers.gaussian_random([10], dtype="float32") x = random.gaussian([10], dtype="float32")
st1 = gen.get_state() st1 = gen.get_state()
x1 = fluid.layers.gaussian_random([10], dtype="float32") x1 = random.gaussian([10], dtype="float32")
gen.set_state(st1) gen.set_state(st1)
x2 = fluid.layers.gaussian_random([10], dtype="float32") x2 = random.gaussian([10], dtype="float32")
gen.manual_seed(12312321111) gen.manual_seed(12312321111)
x3 = fluid.layers.gaussian_random([10], dtype="float32") x3 = random.gaussian([10], dtype="float32")
x_np = x.numpy() x_np = x.numpy()
x1_np = x1.numpy() x1_np = x1.numpy()
x2_np = x2.numpy() x2_np = x2.numpy()
...@@ -175,8 +176,8 @@ class TestGeneratorSeed(unittest.TestCase): ...@@ -175,8 +176,8 @@ class TestGeneratorSeed(unittest.TestCase):
with fluid.program_guard(train_program, startup_program): with fluid.program_guard(train_program, startup_program):
# example 1: # example 1:
# attr shape is a list which doesn't contain tensor Variable. # attr shape is a list which doesn't contain tensor Variable.
result_1 = fluid.layers.gaussian_random(shape=[3, 4]) result_1 = random.gaussian(shape=[3, 4])
result_2 = fluid.layers.gaussian_random(shape=[3, 4]) result_2 = random.gaussian(shape=[3, 4])
exe = fluid.Executor(fluid.CPUPlace()) exe = fluid.Executor(fluid.CPUPlace())
exe.run(startup_program) exe.run(startup_program)
......
...@@ -29,6 +29,7 @@ import paddle ...@@ -29,6 +29,7 @@ import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static() paddle.enable_static()
from paddle.tensor import random
class XPUTestGaussianRandomOp(XPUOpTestWrapper): class XPUTestGaussianRandomOp(XPUOpTestWrapper):
...@@ -192,11 +193,11 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -192,11 +193,11 @@ class TestGaussianRandomAPI(unittest.TestCase):
name="shape_tensor_int64", shape=[2], dtype="int64" 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 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], shape=[2000, positive_2_int32],
dtype="float32", dtype="float32",
mean=0.0, mean=0.0,
...@@ -204,7 +205,7 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -204,7 +205,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed=10, seed=10,
) )
out_3 = fluid.layers.gaussian_random( out_3 = random.gaussian(
shape=[2000, positive_2_int64], shape=[2000, positive_2_int64],
dtype="float32", dtype="float32",
mean=0.0, mean=0.0,
...@@ -212,7 +213,7 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -212,7 +213,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed=10, seed=10,
) )
out_4 = fluid.layers.gaussian_random( out_4 = random.gaussian(
shape=shape_tensor_int32, shape=shape_tensor_int32,
dtype="float32", dtype="float32",
mean=0.0, mean=0.0,
...@@ -220,7 +221,7 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -220,7 +221,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed=10, seed=10,
) )
out_5 = fluid.layers.gaussian_random( out_5 = random.gaussian(
shape=shape_tensor_int64, shape=shape_tensor_int64,
dtype="float32", dtype="float32",
mean=0.0, mean=0.0,
...@@ -228,7 +229,7 @@ class TestGaussianRandomAPI(unittest.TestCase): ...@@ -228,7 +229,7 @@ class TestGaussianRandomAPI(unittest.TestCase):
seed=10, seed=10,
) )
out_6 = fluid.layers.gaussian_random( out_6 = random.gaussian(
shape=shape_tensor_int64, shape=shape_tensor_int64,
dtype=np.float32, dtype=np.float32,
mean=0.0, mean=0.0,
......
...@@ -314,7 +314,7 @@ def uniform_random_batch_size_like( ...@@ -314,7 +314,7 @@ def uniform_random_batch_size_like(
return out 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 Returns a Tensor filled with random values sampled from a Gaussian
distribution, with ``shape`` and ``dtype``. distribution, with ``shape`` and ``dtype``.
...@@ -338,7 +338,6 @@ def gaussian(shape, mean=0.0, std=1.0, dtype=None, name=None): ...@@ -338,7 +338,6 @@ def gaussian(shape, mean=0.0, std=1.0, dtype=None, name=None):
distribution, with ``shape`` and ``dtype``. distribution, with ``shape`` and ``dtype``.
""" """
op_type_for_check = 'gaussian/standard_normal/randn/normal' op_type_for_check = 'gaussian/standard_normal/randn/normal'
seed = 0
if dtype is None: if dtype is None:
dtype = paddle.framework.get_default_dtype() dtype = paddle.framework.get_default_dtype()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册