|
|
@ -21,7 +21,7 @@ from ..fluid.framework import device_guard, in_dygraph_mode, _varbase_creator, V
|
|
|
|
from ..fluid.layers.layer_function_generator import templatedoc
|
|
|
|
from ..fluid.layers.layer_function_generator import templatedoc
|
|
|
|
from ..fluid.layer_helper import LayerHelper
|
|
|
|
from ..fluid.layer_helper import LayerHelper
|
|
|
|
from ..fluid.data_feeder import convert_dtype, check_variable_and_dtype, check_type, check_dtype
|
|
|
|
from ..fluid.data_feeder import convert_dtype, check_variable_and_dtype, check_type, check_dtype
|
|
|
|
from ..fluid.layers import uniform_random, utils
|
|
|
|
from ..fluid.layers import utils, uniform_random, gaussian_random
|
|
|
|
from ..fluid.layers.tensor import fill_constant
|
|
|
|
from ..fluid.layers.tensor import fill_constant
|
|
|
|
|
|
|
|
|
|
|
|
from ..fluid.io import shuffle #DEFINE_ALIAS
|
|
|
|
from ..fluid.io import shuffle #DEFINE_ALIAS
|
|
|
@ -206,36 +206,23 @@ def randint(low,
|
|
|
|
return out
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def randn(shape,
|
|
|
|
def randn(shape, dtype=None, name=None):
|
|
|
|
out=None,
|
|
|
|
|
|
|
|
dtype=None,
|
|
|
|
|
|
|
|
device=None,
|
|
|
|
|
|
|
|
stop_gradient=True,
|
|
|
|
|
|
|
|
name=None):
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
:alias_main: paddle.randn
|
|
|
|
:alias_main: paddle.randn
|
|
|
|
:alias: paddle.randn,paddle.tensor.randn,paddle.tensor.random.randn
|
|
|
|
:alias: paddle.randn,paddle.tensor.randn,paddle.tensor.random.randn
|
|
|
|
|
|
|
|
|
|
|
|
This function returns a tensor filled with random numbers from a normal
|
|
|
|
This function returns a tensor filled with random numbers from a normal
|
|
|
|
distribution with mean 0 and variance 1 (also called the standard normal
|
|
|
|
distribution with mean 0 and standard deviation 1 (also called the standard normal
|
|
|
|
distribution).
|
|
|
|
distribution).
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
shape(list|tuple): Shape of the generated random tensor.
|
|
|
|
shape(list|tuple|Variable): Shape of the Tensor to be created. The data
|
|
|
|
out(Variable, optional): Optional output which can be any created Variable
|
|
|
|
type is ``int32`` or ``int64`` . If ``shape`` is a list or tuple,
|
|
|
|
that meets the requirements to store the result of operation. If the
|
|
|
|
the elements of it should be integers or Tensors with shape [1]. If
|
|
|
|
out is `None`, a new Variable will be returned to store the result.
|
|
|
|
``shape`` is a Variable, it should be an 1-D Tensor .
|
|
|
|
Default is None.
|
|
|
|
dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of the output
|
|
|
|
dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of the output
|
|
|
|
tensor, which can be float32, float64. If dtype is `None` , the data
|
|
|
|
tensor, which can be float32, float64. if dtype is `None` , the data
|
|
|
|
type of output tensor is `float32` . Default is None.
|
|
|
|
type of output tensor is `float32` .
|
|
|
|
|
|
|
|
Default is None.
|
|
|
|
|
|
|
|
device(str, optional): Specific the output variable to be saved in cpu
|
|
|
|
|
|
|
|
or gpu memory. Supported None, 'cpu', 'gpu'. If it is None, the output
|
|
|
|
|
|
|
|
variable will be automatically assigned devices.
|
|
|
|
|
|
|
|
Default: None.
|
|
|
|
|
|
|
|
stop_gradient(bool, optional): Indicating if we stop gradient from current(out)
|
|
|
|
|
|
|
|
Variable. Default is True.
|
|
|
|
|
|
|
|
name(str, optional): Normally there is no need for user to set this property.
|
|
|
|
name(str, optional): Normally there is no need for user to set this property.
|
|
|
|
For more information, please refer to :ref:`api_guide_Name` .
|
|
|
|
For more information, please refer to :ref:`api_guide_Name` .
|
|
|
|
Default is None.
|
|
|
|
Default is None.
|
|
|
@ -244,75 +231,50 @@ def randn(shape,
|
|
|
|
Random tensor whose data is drawn from a standard normal distribution,
|
|
|
|
Random tensor whose data is drawn from a standard normal distribution,
|
|
|
|
dtype: flaot32 or float64 as specified.
|
|
|
|
dtype: flaot32 or float64 as specified.
|
|
|
|
|
|
|
|
|
|
|
|
Return type:
|
|
|
|
Return type: Variable
|
|
|
|
Variable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
Raises:
|
|
|
|
TypeError: If the type of `shape` is not list or tuple.
|
|
|
|
TypeError: If the type of `shape` is not Variable, list or tuple.
|
|
|
|
TypeError: If the data type of `dtype` is not float32 or float64.
|
|
|
|
TypeError: If the data type of `dtype` is not float32 or float64.
|
|
|
|
ValueError: If the length of `shape` is not bigger than 0.
|
|
|
|
ValueError: If the length of `shape` is not bigger than 0.
|
|
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Examples:
|
|
|
|
.. code-block:: python
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
# declarative mode
|
|
|
|
import paddle
|
|
|
|
import paddle
|
|
|
|
import numpy as np
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = paddle.randn([2, 4])
|
|
|
|
paddle.enable_imperative()
|
|
|
|
place = fluid.CPUPlace()
|
|
|
|
|
|
|
|
exe = fluid.Executor(place)
|
|
|
|
|
|
|
|
res, = exe.run(fluid.default_main_program(), feed={}, fetch_list=[data])
|
|
|
|
|
|
|
|
print(res)
|
|
|
|
|
|
|
|
# [[-1.4187592 0.7368311 -0.53748125 -0.0146909 ]
|
|
|
|
|
|
|
|
# [-0.66294265 -1.3090698 0.1898754 -0.14065823]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
# example 1: attr shape is a list which doesn't contain tensor Variable.
|
|
|
|
|
|
|
|
result_1 = paddle.randn(shape=[2, 3])
|
|
|
|
|
|
|
|
# [[-2.923464 0.11934398 -0.51249987]
|
|
|
|
|
|
|
|
# [ 0.39632758 0.08177969 0.2692008 ]]
|
|
|
|
|
|
|
|
|
|
|
|
# imperative mode
|
|
|
|
# example 2: attr shape is a list which contains tensor Variable.
|
|
|
|
import paddle
|
|
|
|
dim_1 = paddle.fill_constant([1], "int64", 2)
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
dim_2 = paddle.fill_constant([1], "int32", 3)
|
|
|
|
import paddle.fluid.dygraph as dg
|
|
|
|
result_2 = paddle.randn(shape=[dim_1, dim_2, 2])
|
|
|
|
|
|
|
|
# [[[-2.8852394 -0.25898588]
|
|
|
|
place = fluid.CPUPlace()
|
|
|
|
# [-0.47420555 0.17683524]
|
|
|
|
with dg.guard(place) as g:
|
|
|
|
# [-0.7989969 0.00754541]]
|
|
|
|
x = paddle.randn([2, 4])
|
|
|
|
# [[ 0.85201347 0.32320443]
|
|
|
|
x_np = x.numpy()
|
|
|
|
# [ 1.1399018 0.48336947]
|
|
|
|
print(x_np)
|
|
|
|
# [ 0.8086993 0.6868893 ]]]
|
|
|
|
# [[ 1.5149173 -0.26234224 -0.592486 1.4523455 ]
|
|
|
|
|
|
|
|
# [ 0.04581212 -0.85345626 1.1687907 -0.02512913]]
|
|
|
|
# example 3: attr shape is a Variable, the data type must be int64 or int32.
|
|
|
|
"""
|
|
|
|
var_shape = paddle.imperative.to_variable(np.array([2, 3]))
|
|
|
|
helper = LayerHelper("randn", **locals())
|
|
|
|
result_3 = paddle.randn(var_shape)
|
|
|
|
check_type(shape, 'shape', (list, tuple), 'randn')
|
|
|
|
# [[-2.878077 0.17099959 0.05111201]
|
|
|
|
assert len(shape) > 0, ("The size of argument(shape) can't be zero.")
|
|
|
|
# [-0.3761474 -1.044801 1.1870178 ]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
if dtype is None:
|
|
|
|
if dtype is None:
|
|
|
|
dtype = 'float32'
|
|
|
|
dtype = 'float32'
|
|
|
|
|
|
|
|
|
|
|
|
check_dtype(dtype, 'create data type', ['float32', 'float64'], 'randn')
|
|
|
|
out = gaussian_random(
|
|
|
|
|
|
|
|
shape=shape, mean=0.0, std=1.0, seed=0, dtype=dtype, name=name)
|
|
|
|
if out is None:
|
|
|
|
out.stop_gradient = True
|
|
|
|
out = helper.create_variable_for_type_inference(dtype=dtype)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
check_variable_and_dtype(out, 'out', [dtype], 'randn')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out.stop_gradient = stop_gradient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dtype = convert_np_dtype_to_dtype_(dtype)
|
|
|
|
|
|
|
|
seed = np.random.randint(0, 100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with device_guard(device):
|
|
|
|
|
|
|
|
helper.append_op(
|
|
|
|
|
|
|
|
type='gaussian_random',
|
|
|
|
|
|
|
|
outputs={'Out': out},
|
|
|
|
|
|
|
|
attrs={
|
|
|
|
|
|
|
|
'shape': shape,
|
|
|
|
|
|
|
|
'mean': 0.0,
|
|
|
|
|
|
|
|
'std': 1.0,
|
|
|
|
|
|
|
|
'seed': seed,
|
|
|
|
|
|
|
|
'dtype': dtype,
|
|
|
|
|
|
|
|
'use_mkldnn': False
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return out
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -369,6 +331,7 @@ def randperm(n, dtype="int64", name=None):
|
|
|
|
attrs = {'n': n, 'dtype': dtype, 'seed': 0}
|
|
|
|
attrs = {'n': n, 'dtype': dtype, 'seed': 0}
|
|
|
|
helper.append_op(
|
|
|
|
helper.append_op(
|
|
|
|
type='randperm', inputs={}, outputs={'Out': out}, attrs=attrs)
|
|
|
|
type='randperm', inputs={}, outputs={'Out': out}, attrs=attrs)
|
|
|
|
|
|
|
|
out.stop_gradient = True
|
|
|
|
return out
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -439,4 +402,7 @@ def rand(shape, dtype=None, name=None):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if dtype is None:
|
|
|
|
if dtype is None:
|
|
|
|
dtype = 'float32'
|
|
|
|
dtype = 'float32'
|
|
|
|
return uniform_random(shape, dtype, min=0.0, max=1.0, name=name)
|
|
|
|
|
|
|
|
|
|
|
|
out = uniform_random(shape, dtype, min=0.0, max=1.0, name=name)
|
|
|
|
|
|
|
|
out.stop_gradient = True
|
|
|
|
|
|
|
|
return out
|
|
|
|