Skip to content

Commit

Permalink
add multinomial doc (PaddlePaddle#2743)
Browse files Browse the repository at this point in the history
* add multinomial doc

* add path

* fix format

* fix little problem
  • Loading branch information
pangyoki authored Sep 30, 2020
1 parent 3f173a3 commit e09b416
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/paddle/api/api_label
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ soft_relu .. _api_paddle_fluid_layers_soft_relu:
no_grad .. _api_paddle_fluid_dygraph_base_no_grad:
dynamic_decode .. _api_paddle_fluid_layers_dynamic_decode:
bernoulli .. _api_paddle_tensor_random_bernoulli:
multinomial .. _api_paddle_tensor_random_multinomial:
ComposeNotAligned .. _api_paddle_reader_ComposeNotAligned:
GRUUnit .. _api_paddle_fluid_dygraph_GRUUnit:
ReplicationPad2d .. _api_paddle_nn_ReplicationPad2d:
Expand Down
56 changes: 56 additions & 0 deletions doc/paddle/api/paddle/tensor/random/multinomial_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.. _cn_api_tensor_multinomial:

multinomial
-------------------------------

.. py:function:: paddle.multinomial(x, num_samples=1, replacement=False, name=None)
该OP以输入 ``x`` 为概率,生成一个多项分布的Tensor。
输入 ``x`` 是用来随机采样的概率分布, ``x`` 中每个元素都应该大于等于0,且不能都为0。
参数 ``replacement`` 表示它是否是一个可放回的采样,如果 ``replacement`` 为True, 能重复对一种类别采样。

参数:
- **x** (Tensor) - 输入的概率值。数据类型为 ``float32`` 、``float64`` .
- **num_samples** (int, 可选) - 采样的次数(可选,默认值为1)。
- **replacement** (bool, 可选) - 是否是可放回的采样(可选,默认值为False)。
- **name** (str, 可选) - 操作的名称(可选,默认值为None)。更多信息请参见 :ref:`api_guide_Name`。

返回:
Tensor:多项分布采样得到的随机Tensor,为 ``num_samples`` 次采样得到的类别下标。


**代码示例**:

.. code-block:: python
import paddle
x = paddle.rand([2,4])
print(x.numpy())
# [[0.7713825 0.4055941 0.433339 0.70706886]
# [0.9223313 0.8519825 0.04574518 0.16560672]]
out1 = paddle.multinomial(x, num_samples=5, replacement=True)
print(out1.numpy())
# [[3, 3, 1, 1, 0]
# [0, 0, 0, 0, 1]]
# out2 = paddle.multinomial(x, num_samples=5)
# OutOfRangeError: When replacement is False, number of samples
# should be less than non-zero categories
out3 = paddle.multinomial(x, num_samples=3)
print(out3.numpy())
# [[0, 2, 3]
# [0, 1, 3]]

0 comments on commit e09b416

Please sign in to comment.