Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e9dd763c
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e9dd763c
编写于
9月 16, 2020
作者:
P
pangyoki
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add multinomial python api
上级
dab6fa97
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
64 addition
and
0 deletion
+64
-0
python/paddle/__init__.py
python/paddle/__init__.py
+1
-0
python/paddle/fluid/tests/unittests/test_multinomial_op.py
python/paddle/fluid/tests/unittests/test_multinomial_op.py
+20
-0
python/paddle/tensor/__init__.py
python/paddle/tensor/__init__.py
+1
-0
python/paddle/tensor/random.py
python/paddle/tensor/random.py
+42
-0
未找到文件。
python/paddle/__init__.py
浏览文件 @
e9dd763c
...
@@ -199,6 +199,7 @@ from .tensor.math import isfinite #DEFINE_ALIAS
...
@@ -199,6 +199,7 @@ from .tensor.math import isfinite #DEFINE_ALIAS
from
.tensor.math
import
isinf
#DEFINE_ALIAS
from
.tensor.math
import
isinf
#DEFINE_ALIAS
from
.tensor.math
import
isnan
#DEFINE_ALIAS
from
.tensor.math
import
isnan
#DEFINE_ALIAS
from
.tensor.math
import
prod
#DEFINE_ALIAS
from
.tensor.math
import
prod
#DEFINE_ALIAS
from
.tensor.random
import
multinomial
#DEFINE_ALIAS
from
.tensor.random
import
standard_normal
from
.tensor.random
import
standard_normal
from
.tensor.random
import
normal
from
.tensor.random
import
normal
from
.tensor.random
import
uniform
#DEFINE_ALIAS
from
.tensor.random
import
uniform
#DEFINE_ALIAS
...
...
python/paddle/fluid/tests/unittests/test_multinomial_op.py
浏览文件 @
e9dd763c
...
@@ -102,5 +102,25 @@ class TestReplacementError(unittest.TestCase):
...
@@ -102,5 +102,25 @@ class TestReplacementError(unittest.TestCase):
self.attrs = {"num_samples": 10, "replacement": False}
self.attrs = {"num_samples": 10, "replacement": False}
"""
"""
class
TestMultinomialApi
(
unittest
.
TestCase
):
def
test_dygraph
(
self
):
paddle
.
disable_static
()
x
=
paddle
.
rand
([
4
])
out
=
paddle
.
multinomial
(
x
,
num_samples
=
100000
,
replacement
=
True
)
x_numpy
=
x
.
numpy
()
paddle
.
enable_static
()
sample_prob
=
np
.
unique
(
out
.
numpy
(),
return_counts
=
True
)[
1
].
astype
(
"float32"
)
sample_prob
/=
sample_prob
.
sum
()
prob
=
x_numpy
/
x_numpy
.
sum
(
axis
=-
1
,
keepdims
=
True
)
self
.
assertTrue
(
np
.
allclose
(
sample_prob
,
prob
,
rtol
=
0
,
atol
=
0.01
),
"sample_prob: "
+
str
(
sample_prob
)
+
"
\n
prob: "
+
str
(
prob
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
python/paddle/tensor/__init__.py
浏览文件 @
e9dd763c
...
@@ -166,6 +166,7 @@ from .math import isfinite #DEFINE_ALIAS
...
@@ -166,6 +166,7 @@ from .math import isfinite #DEFINE_ALIAS
from
.math
import
isinf
#DEFINE_ALIAS
from
.math
import
isinf
#DEFINE_ALIAS
from
.math
import
isnan
#DEFINE_ALIAS
from
.math
import
isnan
#DEFINE_ALIAS
from
.math
import
prod
#DEFINE_ALIAS
from
.math
import
prod
#DEFINE_ALIAS
from
.random
import
multinomial
#DEFINE_ALIAS
from
.random
import
standard_normal
from
.random
import
standard_normal
from
.random
import
normal
from
.random
import
normal
from
.random
import
uniform
#DEFINE_ALIAS
from
.random
import
uniform
#DEFINE_ALIAS
...
...
python/paddle/tensor/random.py
浏览文件 @
e9dd763c
...
@@ -25,6 +25,7 @@ from ..fluid.io import shuffle #DEFINE_ALIAS
...
@@ -25,6 +25,7 @@ from ..fluid.io import shuffle #DEFINE_ALIAS
__all__
=
[
__all__
=
[
'bernoulli'
,
'bernoulli'
,
'multinomial'
,
'standard_normal'
,
'standard_normal'
,
'normal'
,
'normal'
,
'uniform'
,
'uniform'
,
...
@@ -88,6 +89,47 @@ def bernoulli(x, name=None):
...
@@ -88,6 +89,47 @@ def bernoulli(x, name=None):
return
out
return
out
def
multinomial
(
x
,
num_samples
=
1
,
replacement
=
False
,
name
=
None
):
"""
Examples:
.. code-block:: python
import paddle
paddle.disable_static()
x = paddle.rand([2, 3])
print(x.numpy())
# [[0.11272584 0.3890902 0.7730957 ]
# [0.10351662 0.8510418 0.63806665]]
out = paddle.bernoulli(x)
print(out.numpy())
# [[0. 0. 1.]
# [0. 0. 1.]]
"""
if
in_dygraph_mode
():
return
core
.
ops
.
multinomial
(
x
,
'num_samples'
,
num_samples
,
'replacement'
,
replacement
)
check_variable_and_dtype
(
x
,
"x"
,
[
"float32"
,
"float64"
],
"multinomial"
)
helper
=
LayerHelper
(
"multinomial"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
convert_np_dtype_to_dtype_
(
'int64'
))
helper
.
append_op
(
type
=
'multinomial'
,
inputs
=
{
"X"
:
x
},
outputs
=
{
'Out'
:
out
},
attrs
=
{
'num_samples'
:
num_samples
,
'replacement'
:
replacement
})
return
out
def
gaussian
(
shape
,
mean
=
0.0
,
std
=
1.0
,
dtype
=
None
,
name
=
None
):
def
gaussian
(
shape
,
mean
=
0.0
,
std
=
1.0
,
dtype
=
None
,
name
=
None
):
"""
"""
This OP returns a Tensor filled with random values sampled from a Gaussian
This OP returns a Tensor filled with random values sampled from a Gaussian
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录