diff --git a/python/paddle/distribution/normal.py b/python/paddle/distribution/normal.py index 7eb9fb597d3a2763a32e93925a7d34ce86f9f499..06cd726fd4f36715748c9dd80d5361e4c01e9c7f 100644 --- a/python/paddle/distribution/normal.py +++ b/python/paddle/distribution/normal.py @@ -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) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 9d4429ef0468509512f898c93b0e3242d7a97754..bf5853fad88d258166193ad94fdace43b717fe63 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -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) diff --git a/python/paddle/fluid/tests/unittests/test_gaussian_random_op.py b/python/paddle/fluid/tests/unittests/test_gaussian_random_op.py index b0fb623502ed398d0e66f47b95bfb32a72491ba2..e16c22eb12a2b6dde568ccb35315170c7c283a17 100644 --- a/python/paddle/fluid/tests/unittests/test_gaussian_random_op.py +++ b/python/paddle/fluid/tests/unittests/test_gaussian_random_op.py @@ -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, diff --git a/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py b/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py index 522fb24f8fb7a29e6d7172baf38b9b1b54f06b1f..5dbb1ac0a2974bfe1158d97324dfd889e389cb1d 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py @@ -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()) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 52b864648cb7fa6c5c9c6286fad2885affde4c85..f07f8bba97c97d1ae50178de680f94bcd052b477 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -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): diff --git a/python/paddle/fluid/tests/unittests/test_manual_seed.py b/python/paddle/fluid/tests/unittests/test_manual_seed.py index 419ba5dba888df9a4bcdc4be75940f9797388750..a4b2afe521b1e0f097c83f57baa371483a31fde5 100644 --- a/python/paddle/fluid/tests/unittests/test_manual_seed.py +++ b/python/paddle/fluid/tests/unittests/test_manual_seed.py @@ -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() diff --git a/python/paddle/fluid/tests/unittests/test_random_seed.py b/python/paddle/fluid/tests/unittests/test_random_seed.py index 44b368889583d932c95914156f63167e7a9f1cca..1c3c280d2fcbc359f201c45c05b4f8bacac27553 100644 --- a/python/paddle/fluid/tests/unittests/test_random_seed.py +++ b/python/paddle/fluid/tests/unittests/test_random_seed.py @@ -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) diff --git a/python/paddle/fluid/tests/unittests/xpu/test_gaussian_random_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_gaussian_random_op_xpu.py index 62a0180dc876d049278820f4aef18c8da138a83a..2a5f81f58c1e7f944655b108d36675bc7c665ffa 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_gaussian_random_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_gaussian_random_op_xpu.py @@ -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, diff --git a/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index 1b80c872faca521a833bda9b0012e8cba3b3ba62..3bbf7831e8472c0cb4f96f508907ff6c8c7e2627 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -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()