From 973e81f0fb85f548a99744985098063514d4ee0b Mon Sep 17 00:00:00 2001 From: zhupengyang Date: Mon, 20 Jul 2020 11:16:31 +0800 Subject: [PATCH] add rand doc (#2275) --- doc/fluid/api/tensor/random.rst | 1 + doc/fluid/api_cn/tensor_cn/rand_cn.rst | 60 +++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/doc/fluid/api/tensor/random.rst b/doc/fluid/api/tensor/random.rst index 687c6d5af..fdc985d3d 100644 --- a/doc/fluid/api/tensor/random.rst +++ b/doc/fluid/api/tensor/random.rst @@ -5,6 +5,7 @@ random .. toctree:: :maxdepth: 1 + random/rand.rst random/randint.rst random/randn.rst random/randperm.rst diff --git a/doc/fluid/api_cn/tensor_cn/rand_cn.rst b/doc/fluid/api_cn/tensor_cn/rand_cn.rst index b1c5e32b1..5e85bd16b 100644 --- a/doc/fluid/api_cn/tensor_cn/rand_cn.rst +++ b/doc/fluid/api_cn/tensor_cn/rand_cn.rst @@ -1,3 +1,59 @@ +.. _cn_api_tensor_random_rand: + rand -------------------------------- -**版本升级,文档正在开发中** +---------------------- + +.. py:function:: paddle.rand(shape, dtype=None, name=None) + +:alias_main: paddle.rand +:alias: paddle.tensor.rand, paddle.tensor.random.rand + + + +该OP返回符合均匀分布的,范围在[0, 1)的Tensor。 + +参数 +:::::::::: + - **shape** (list|tuple|Variable) - 生成的随机Tensor的形状。如果 ``shape`` 是list、tuple,则其中的元素可以是int,或者是形状为[1]且数据类型为int32、int64的tensor。如果 ``shape`` 是Variable,则是1D的Tensor,Tensor的数据类型为int32、int64。 + - **dtype** (str|np.dtype|core.VarDesc.VarType, 可选) - 输出Tensor的数据类型,支持float32、float64。当该参数值为None时, 输出Tensor的数据类型为float32。默认值为None. + - **name** (str, 可选) - 输出的名字。一般无需设置,默认值为None。该参数供开发人员打印调试信息时使用,具体用法请参见 :ref:`api_guide_Name` 。 + +返回 +:::::::::: + Variable: 符合均匀分布的范围为[0, 1)的随机Tensor。形状为shape,数据类型为dtype。 + +抛出异常 +:::::::::: + - ``TypeError`` - 如果 ``shape`` 的类型不是list、tuple、Variable。 + - ``TypeError`` - 如果 ``dtype`` 不是float32、float64。 + +示例代码 +:::::::::: + +.. code-block:: python + + import paddle + import numpy as np + + paddle.enable_imperative() + # example 1: attr shape is a list which doesn't contain tensor Variable. + result_1 = paddle.rand(shape=[2, 3]) + # [[0.451152 , 0.55825245, 0.403311 ], + # [0.22550228, 0.22106001, 0.7877319 ]] + + # example 2: attr shape is a list which contains tensor Variable. + dim_1 = paddle.fill_constant([1], "int64", 2) + dim_2 = paddle.fill_constant([1], "int32", 3) + result_2 = paddle.rand(shape=[dim_1, dim_2, 2]) + # [[[0.8879919 0.25788337] + # [0.28826773 0.9712097 ] + # [0.26438272 0.01796806]] + # [[0.33633623 0.28654453] + # [0.79109055 0.7305809 ] + # [0.870881 0.2984597 ]]] + + # 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])) + result_3 = paddle.rand(var_shape) + # [[0.22920267 0.841956 0.05981819] + # [0.4836288 0.24573246 0.7516129 ]] -- GitLab