Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
84fe0454
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
未验证
提交
84fe0454
编写于
8月 07, 2023
作者:
G
gouzil
提交者:
GitHub
8月 07, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[xdoctest] No.44-47 and No.50-59 doc style (#55813)
上级
30a02d27
变更
9
展开全部
隐藏空白更改
内联
并排
Showing
9 changed file
with
538 addition
and
519 deletion
+538
-519
python/paddle/distribution/cauchy.py
python/paddle/distribution/cauchy.py
+153
-153
python/paddle/distribution/independent.py
python/paddle/distribution/independent.py
+15
-15
python/paddle/distribution/kl.py
python/paddle/distribution/kl.py
+10
-10
python/paddle/distribution/laplace.py
python/paddle/distribution/laplace.py
+47
-48
python/paddle/distribution/lognormal.py
python/paddle/distribution/lognormal.py
+38
-30
python/paddle/distribution/multinomial.py
python/paddle/distribution/multinomial.py
+11
-12
python/paddle/distribution/normal.py
python/paddle/distribution/normal.py
+38
-30
python/paddle/distribution/transform.py
python/paddle/distribution/transform.py
+208
-206
python/paddle/distribution/transformed_distribution.py
python/paddle/distribution/transformed_distribution.py
+18
-15
未找到文件。
python/paddle/distribution/cauchy.py
浏览文件 @
84fe0454
...
...
@@ -39,20 +39,20 @@ class Cauchy(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Cauchy
# init Cauchy with float
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.entropy())
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#
2.71334577)
# init Cauchy with N-Dim tensor
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.entropy())
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[2.53102422, 3.22417140])
>>>
import paddle
>>>
from paddle.distribution import Cauchy
>>>
# init Cauchy with float
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.entropy())
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.71334577)
>>>
# init Cauchy with N-Dim tensor
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.entropy())
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.53102422, 3.22417140])
"""
def
__init__
(
self
,
loc
,
scale
,
name
=
None
):
...
...
@@ -114,32 +114,32 @@ class Cauchy(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Cauchy
>>>
import paddle
>>>
from paddle.distribution import Cauchy
# init Cauchy with float
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.sample([10]).shape)
#
[10]
>>>
# init Cauchy with float
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.sample([10]).shape)
[10]
# init Cauchy with 0-Dim tensor
rv = Cauchy(loc=paddle.full((), 0.1), scale=paddle.full((), 1.2))
print(rv.sample([10]).shape)
#
[10]
>>>
# init Cauchy with 0-Dim tensor
>>>
rv = Cauchy(loc=paddle.full((), 0.1), scale=paddle.full((), 1.2))
>>>
print(rv.sample([10]).shape)
[10]
# init Cauchy with N-Dim tensor
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.sample([10]).shape)
#
[10, 2]
>>>
# init Cauchy with N-Dim tensor
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.sample([10]).shape)
[10, 2]
# sample 2-Dim data
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.sample([10, 2]).shape)
#
[10, 2]
>>>
# sample 2-Dim data
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.sample([10, 2]).shape)
[10, 2]
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.sample([10, 2]).shape)
#
[10, 2, 2]
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.sample([10, 2]).shape)
[10, 2, 2]
"""
name
=
name
if
name
is
not
None
else
(
self
.
name
+
'_sample'
)
with
paddle
.
no_grad
():
...
...
@@ -159,32 +159,32 @@ class Cauchy(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Cauchy
>>>
import paddle
>>>
from paddle.distribution import Cauchy
# init Cauchy with float
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.rsample([10]).shape)
#
[10]
>>>
# init Cauchy with float
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.rsample([10]).shape)
[10]
# init Cauchy with 0-Dim tensor
rv = Cauchy(loc=paddle.full((), 0.1), scale=paddle.full((), 1.2))
print(rv.rsample([10]).shape)
#
[10]
>>>
# init Cauchy with 0-Dim tensor
>>>
rv = Cauchy(loc=paddle.full((), 0.1), scale=paddle.full((), 1.2))
>>>
print(rv.rsample([10]).shape)
[10]
# init Cauchy with N-Dim tensor
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.rsample([10]).shape)
#
[10, 2]
>>>
# init Cauchy with N-Dim tensor
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.rsample([10]).shape)
[10, 2]
# sample 2-Dim data
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.rsample([10, 2]).shape)
#
[10, 2]
>>>
# sample 2-Dim data
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.rsample([10, 2]).shape)
[10, 2]
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.rsample([10, 2]).shape)
#
[10, 2, 2]
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.rsample([10, 2]).shape)
[10, 2, 2]
"""
name
=
name
if
name
is
not
None
else
(
self
.
name
+
'_rsample'
)
...
...
@@ -222,32 +222,32 @@ class Cauchy(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Cauchy
# init Cauchy with float
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.prob(paddle.to_tensor(1.5)))
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#
0.11234467)
# broadcast to value
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.prob(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[0.11234467, 0.01444674])
# init Cauchy with N-Dim tensor
rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.prob(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[0.10753712, 0.02195240])
# init Cauchy with N-Dim tensor with broadcast
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.prob(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[0.10753712, 0.02195240])
>>>
import paddle
>>>
from paddle.distribution import Cauchy
>>>
# init Cauchy with float
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.prob(paddle.to_tensor(1.5)))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.11234467)
>>>
# broadcast to value
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.prob(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.11234467, 0.01444674])
>>>
# init Cauchy with N-Dim tensor
>>>
rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.prob(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.10753712, 0.02195240])
>>>
# init Cauchy with N-Dim tensor with broadcast
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.prob(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.10753712, 0.02195240])
"""
name
=
self
.
name
+
'_prob'
...
...
@@ -271,32 +271,32 @@ class Cauchy(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Cauchy
# init Cauchy with float
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.log_prob(paddle.to_tensor(1.5)))
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#
-2.18618369)
# broadcast to value
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[-2.18618369, -4.23728657])
# init Cauchy with N-Dim tensor
rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[-2.22991920, -3.81887865])
# init Cauchy with N-Dim tensor with broadcast
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[-2.22991920, -3.81887865])
>>>
import paddle
>>>
from paddle.distribution import Cauchy
>>>
# init Cauchy with float
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.log_prob(paddle.to_tensor(1.5)))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
-2.18618369)
>>>
# broadcast to value
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[-2.18618369, -4.23728657])
>>>
# init Cauchy with N-Dim tensor
>>>
rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[-2.22991920, -3.81887865])
>>>
# init Cauchy with N-Dim tensor with broadcast
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[-2.22991920, -3.81887865])
"""
name
=
self
.
name
+
'_log_prob'
...
...
@@ -338,32 +338,32 @@ class Cauchy(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Cauchy
# init Cauchy with float
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.cdf(paddle.to_tensor(1.5)))
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#
0.77443725)
# broadcast to value
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[0.77443725, 0.92502367])
# init Cauchy with N-Dim tensor
rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[0.80256844, 0.87888104])
# init Cauchy with N-Dim tensor with broadcast
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[0.80256844, 0.87888104])
>>>
import paddle
>>>
from paddle.distribution import Cauchy
>>>
# init Cauchy with float
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.cdf(paddle.to_tensor(1.5)))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.77443725)
>>>
# broadcast to value
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.77443725, 0.92502367])
>>>
# init Cauchy with N-Dim tensor
>>>
rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.80256844, 0.87888104])
>>>
# init Cauchy with N-Dim tensor with broadcast
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.80256844, 0.87888104])
"""
name
=
self
.
name
+
'_cdf'
...
...
@@ -399,20 +399,20 @@ class Cauchy(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Cauchy
>>>
import paddle
>>>
from paddle.distribution import Cauchy
# init Cauchy with float
rv = Cauchy(loc=0.1, scale=1.2)
print(rv.entropy())
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#
2.71334577)
>>>
# init Cauchy with float
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
print(rv.entropy())
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.71334577)
# init Cauchy with N-Dim tensor
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
print(rv.entropy())
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[2.53102422, 3.22417140])
>>>
# init Cauchy with N-Dim tensor
>>>
rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
>>>
print(rv.entropy())
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.53102422, 3.22417140])
"""
name
=
self
.
name
+
'_entropy'
...
...
@@ -438,14 +438,14 @@ class Cauchy(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Cauchy
>>>
import paddle
>>>
from paddle.distribution import Cauchy
rv = Cauchy(loc=0.1, scale=1.2)
rv_other = Cauchy(loc=paddle.to_tensor(1.2), scale=paddle.to_tensor([2.3, 3.4]))
print(rv.kl_divergence(rv_other))
#
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[0.19819736, 0.31532931])
>>>
rv = Cauchy(loc=0.1, scale=1.2)
>>>
rv_other = Cauchy(loc=paddle.to_tensor(1.2), scale=paddle.to_tensor([2.3, 3.4]))
>>>
print(rv.kl_divergence(rv_other))
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.19819736, 0.31532931])
"""
name
=
self
.
name
+
'_kl_divergence'
...
...
python/paddle/distribution/independent.py
浏览文件 @
84fe0454
...
...
@@ -31,21 +31,21 @@ class Independent(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import independent
beta = paddle.distribution.Beta(paddle.to_tensor([0.5, 0.5]), paddle.to_tensor([0.5, 0.5]))
print(beta.batch_shape, beta.event_shape)
#
(2,) ()
print(beta.log_prob(paddle.to_tensor(0.2)))
#
Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#
[-0.22843921, -0.22843921])
reinterpreted_beta = independent.Independent(beta, 1)
print(reinterpreted_beta.batch_shape, reinterpreted_beta.event_shape)
#
() (2,)
print(reinterpreted_beta.log_prob(paddle.to_tensor([0.2, 0.2])))
#
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#
-0.45687842)
>>>
import paddle
>>>
from paddle.distribution import independent
>>>
beta = paddle.distribution.Beta(paddle.to_tensor([0.5, 0.5]), paddle.to_tensor([0.5, 0.5]))
>>>
print(beta.batch_shape, beta.event_shape)
(2,) ()
>>>
print(beta.log_prob(paddle.to_tensor(0.2)))
Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[-0.22843921, -0.22843921])
>>>
reinterpreted_beta = independent.Independent(beta, 1)
>>>
print(reinterpreted_beta.batch_shape, reinterpreted_beta.event_shape)
() (2,)
>>>
print(reinterpreted_beta.log_prob(paddle.to_tensor([0.2, 0.2])))
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
-0.45687842)
"""
def
__init__
(
self
,
base
,
reinterpreted_batch_rank
):
...
...
python/paddle/distribution/kl.py
浏览文件 @
84fe0454
...
...
@@ -53,14 +53,14 @@ def kl_divergence(p, q):
.. code-block:: python
import paddle
>>>
import paddle
p = paddle.distribution.Beta(alpha=0.5, beta=0.5)
q = paddle.distribution.Beta(alpha=0.3, beta=0.7)
>>>
p = paddle.distribution.Beta(alpha=0.5, beta=0.5)
>>>
q = paddle.distribution.Beta(alpha=0.3, beta=0.7)
print(paddle.distribution.kl_divergence(p, q))
# Tensor(shape=[], dtype=float32, place=CUDAPlace(0
), stop_gradient=True,
#
0.21193528)
>>>
print(paddle.distribution.kl_divergence(p, q))
Tensor(shape=[], dtype=float32, place=Place(cpu
), stop_gradient=True,
0.21193528)
"""
return
_dispatch
(
type
(
p
),
type
(
q
))(
p
,
q
)
...
...
@@ -82,11 +82,11 @@ def register_kl(cls_p, cls_q):
Examples:
.. code-block:: python
import paddle
>>>
import paddle
@paddle.distribution.register_kl(paddle.distribution.Beta, paddle.distribution.Beta)
def kl_beta_beta():
pass # insert implementation here
>>>
@paddle.distribution.register_kl(paddle.distribution.Beta, paddle.distribution.Beta)
>>>
def kl_beta_beta():
...
pass # insert implementation here
"""
if
not
issubclass
(
cls_p
,
Distribution
)
or
not
issubclass
(
cls_q
,
Distribution
...
...
python/paddle/distribution/laplace.py
浏览文件 @
84fe0454
...
...
@@ -44,12 +44,12 @@ class Laplace(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
m.sample() # Laplace distributed with loc=0, scale=1
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 3.68546247
)
>>>
import paddle
>>> paddle.seed(2023)
>>>
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
>>>
m.sample() # Laplace distributed with loc=0, scale=1
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.31554604
)
"""
...
...
@@ -173,13 +173,13 @@ class Laplace(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
>>>
import paddle
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
value = paddle.to_tensor(0.1)
m.log_prob(value)
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#
-0.79314721)
>>>
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
>>>
value = paddle.to_tensor(0.1)
>>>
m.log_prob(value)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
-0.79314721)
"""
loc
,
scale
,
value
=
self
.
_validate_value
(
value
)
...
...
@@ -205,12 +205,12 @@ class Laplace(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
>>>
import paddle
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
m.entropy()
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#
1.69314718)
>>>
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
>>>
m.entropy()
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.69314718)
"""
return
1
+
paddle
.
log
(
2
*
self
.
scale
)
...
...
@@ -236,13 +236,13 @@ class Laplace(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
>>>
import paddle
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
value = paddle.to_tensor(0.1)
m.cdf(value)
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#
0.54758132)
>>>
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
>>>
value = paddle.to_tensor(0.1)
>>>
m.cdf(value)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.54758132)
"""
loc
,
scale
,
value
=
self
.
_validate_value
(
value
)
iterm
=
(
...
...
@@ -275,13 +275,12 @@ class Laplace(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
value = paddle.to_tensor(0.1)
m.icdf(value)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# -1.60943794)
>>> import paddle
>>> m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
>>> value = paddle.to_tensor(0.1)
>>> m.icdf(value)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
-1.60943794)
"""
loc
,
scale
,
value
=
self
.
_validate_value
(
value
)
term
=
value
-
0.5
...
...
@@ -300,12 +299,12 @@ class Laplace(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
m.sample() # Laplace distributed with loc=0, scale=1
#
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 3.68546247
)
>>>
import paddle
>>> paddle.seed(2023)
>>>
m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
>>>
m.sample() # Laplace distributed with loc=0, scale=1
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.31554604
)
"""
shape
=
shape
if
isinstance
(
shape
,
tuple
)
else
tuple
(
shape
)
with
paddle
.
no_grad
():
...
...
@@ -323,12 +322,12 @@ class Laplace(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
m = paddle.distribution.Laplace(paddle.to_tensor([0.0]), paddle.to_tensor([1.0]))
m.rsample((1,)) # Laplace distributed with loc=0, scale=1
#
Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.04337667
]])
>>>
import paddle
>>> paddle.seed(2023)
>>>
m = paddle.distribution.Laplace(paddle.to_tensor([0.0]), paddle.to_tensor([1.0]))
>>>
m.rsample((1,)) # Laplace distributed with loc=0, scale=1
Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.31554604
]])
"""
eps
=
self
.
_get_eps
()
...
...
@@ -395,13 +394,13 @@ class Laplace(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
>>>
import paddle
m1 = paddle.distribution.Laplace(paddle.to_tensor([0.0]), paddle.to_tensor([1.0]))
m2 = paddle.distribution.Laplace(paddle.to_tensor([1.0]), paddle.to_tensor([0.5]))
m1.kl_divergence(m2)
#
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[1.04261160])
>>>
m1 = paddle.distribution.Laplace(paddle.to_tensor([0.0]), paddle.to_tensor([1.0]))
>>>
m2 = paddle.distribution.Laplace(paddle.to_tensor([1.0]), paddle.to_tensor([0.5]))
>>>
m1.kl_divergence(m2)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.04261160])
"""
var_ratio
=
other
.
scale
/
self
.
scale
...
...
python/paddle/distribution/lognormal.py
浏览文件 @
84fe0454
...
...
@@ -49,36 +49,44 @@ class LogNormal(TransformedDistribution):
Examples:
.. code-block:: python
import paddle
from paddle.distribution import LogNormal
# Define a single scalar LogNormal distribution.
dist = LogNormal(loc=0., scale=3.)
# Define a batch of two scalar valued LogNormals.
# The underlying Normal of first has mean 1 and standard deviation 11, the underlying Normal of second 2 and 22.
dist = LogNormal(loc=[1., 2.], scale=[11., 22.])
# Get 3 samples, returning a 3 x 2 tensor.
dist.sample((3, ))
# Define a batch of two scalar valued LogNormals.
# Their underlying Normal have mean 1, but different standard deviations.
dist = LogNormal(loc=1., scale=[11., 22.])
# Complete example
value_tensor = paddle.to_tensor([0.8], dtype="float32")
lognormal_a = LogNormal([0.], [1.])
lognormal_b = LogNormal([0.5], [2.])
sample = lognormal_a.sample((2, ))
# a random tensor created by lognormal distribution with shape: [2, 1]
entropy = lognormal_a.entropy()
# [1.4189385] with shape: [1]
lp = lognormal_a.log_prob(value_tensor)
# [-0.72069150] with shape: [1]
p = lognormal_a.probs(value_tensor)
# [0.48641577] with shape: [1]
kl = lognormal_a.kl_divergence(lognormal_b)
# [0.34939718] with shape: [1]
>>> import paddle
>>> from paddle.distribution import LogNormal
>>> # Define a single scalar LogNormal distribution.
>>> dist = LogNormal(loc=0., scale=3.)
>>> # Define a batch of two scalar valued LogNormals.
>>> # The underlying Normal of first has mean 1 and standard deviation 11, the underlying Normal of second 2 and 22.
>>> dist = LogNormal(loc=[1., 2.], scale=[11., 22.])
>>> # Get 3 samples, returning a 3 x 2 tensor.
>>> dist.sample((3, ))
>>> # Define a batch of two scalar valued LogNormals.
>>> # Their underlying Normal have mean 1, but different standard deviations.
>>> dist = LogNormal(loc=1., scale=[11., 22.])
>>> # Complete example
>>> value_tensor = paddle.to_tensor([0.8], dtype="float32")
>>> lognormal_a = LogNormal([0.], [1.])
>>> lognormal_b = LogNormal([0.5], [2.])
>>> sample = lognormal_a.sample((2, ))
>>> # a random tensor created by lognormal distribution with shape: [2, 1]
>>> entropy = lognormal_a.entropy()
>>> print(entropy)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.41893852])
>>> lp = lognormal_a.log_prob(value_tensor)
>>> print(lp)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[-0.72069150])
>>> p = lognormal_a.probs(value_tensor)
>>> print(p)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.48641577])
>>> kl = lognormal_a.kl_divergence(lognormal_b)
>>> print(kl)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.34939718])
"""
def
__init__
(
self
,
loc
,
scale
):
...
...
python/paddle/distribution/multinomial.py
浏览文件 @
84fe0454
...
...
@@ -53,18 +53,17 @@ class Multinomial(distribution.Distribution):
.. code-block:: python
import paddle
multinomial = paddle.distribution.Multinomial(10, paddle.to_tensor([0.2, 0.3, 0.5]))
print(multinomial.sample((2, 3)))
# Tensor(shape=[2, 3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [[[1., 4., 5.],
# [0., 2., 8.],
# [2., 4., 4.]],
# [[1., 6., 3.],
# [3., 3., 4.],
# [3., 4., 3.]]])
>>> import paddle
>>> paddle.seed(2023)
>>> multinomial = paddle.distribution.Multinomial(10, paddle.to_tensor([0.2, 0.3, 0.5]))
>>> print(multinomial.sample((2, 3)))
Tensor(shape=[2, 3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[1., 5., 4.],
[0., 4., 6.],
[1., 3., 6.]],
[[2., 2., 6.],
[0., 6., 4.],
[3., 3., 4.]]])
"""
def
__init__
(
self
,
total_count
,
probs
):
...
...
python/paddle/distribution/normal.py
浏览文件 @
84fe0454
...
...
@@ -54,36 +54,44 @@ class Normal(distribution.Distribution):
Examples:
.. code-block:: python
import paddle
from paddle.distribution import Normal
# Define a single scalar Normal distribution.
dist = Normal(loc=0., scale=3.)
# Define a batch of two scalar valued Normals.
# The first has mean 1 and standard deviation 11, the second 2 and 22.
dist = Normal(loc=[1., 2.], scale=[11., 22.])
# Get 3 samples, returning a 3 x 2 tensor.
dist.sample([3])
# Define a batch of two scalar valued Normals.
# Both have mean 1, but different standard deviations.
dist = Normal(loc=1., scale=[11., 22.])
# Complete example
value_tensor = paddle.to_tensor([0.8], dtype="float32")
normal_a = Normal([0.], [1.])
normal_b = Normal([0.5], [2.])
sample = normal_a.sample([2])
# a random tensor created by normal distribution with shape: [2, 1]
entropy = normal_a.entropy()
# [1.4189385] with shape: [1]
lp = normal_a.log_prob(value_tensor)
# [-1.2389386] with shape: [1]
p = normal_a.probs(value_tensor)
# [0.28969154] with shape: [1]
kl = normal_a.kl_divergence(normal_b)
# [0.34939718] with shape: [1]
>>> import paddle
>>> from paddle.distribution import Normal
>>> # Define a single scalar Normal distribution.
>>> dist = Normal(loc=0., scale=3.)
>>> # Define a batch of two scalar valued Normals.
>>> # The first has mean 1 and standard deviation 11, the second 2 and 22.
>>> dist = Normal(loc=[1., 2.], scale=[11., 22.])
>>> # Get 3 samples, returning a 3 x 2 tensor.
>>> dist.sample([3])
>>> # Define a batch of two scalar valued Normals.
>>> # Both have mean 1, but different standard deviations.
>>> dist = Normal(loc=1., scale=[11., 22.])
>>> # Complete example
>>> value_tensor = paddle.to_tensor([0.8], dtype="float32")
>>> normal_a = Normal([0.], [1.])
>>> normal_b = Normal([0.5], [2.])
>>> sample = normal_a.sample([2])
>>> # a random tensor created by normal distribution with shape: [2, 1]
>>> entropy = normal_a.entropy()
>>> print(entropy)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.41893852])
>>> lp = normal_a.log_prob(value_tensor)
>>> print(lp)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[-1.23893857])
>>> p = normal_a.probs(value_tensor)
>>> print(p)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.28969154])
>>> kl = normal_a.kl_divergence(normal_b)
>>> print(kl)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.34939718])
"""
def
__init__
(
self
,
loc
,
scale
,
name
=
None
):
...
...
python/paddle/distribution/transform.py
浏览文件 @
84fe0454
此差异已折叠。
点击以展开。
python/paddle/distribution/transformed_distribution.py
浏览文件 @
84fe0454
...
...
@@ -29,21 +29,24 @@ class TransformedDistribution(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import transformed_distribution
d = transformed_distribution.TransformedDistribution(
paddle.distribution.Normal(0., 1.),
[paddle.distribution.AffineTransform(paddle.to_tensor(1.), paddle.to_tensor(2.))]
)
print(d.sample([10]))
# Tensor(shape=[10], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [-0.10697651, 3.33609009, -0.86234951, 5.07457638, 0.75925219,
# -4.17087793, 2.22579336, -0.93845034, 0.66054249, 1.50957513])
print(d.log_prob(paddle.to_tensor(0.5)))
# Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# -1.64333570)
>>> import paddle
>>> paddle.seed(2023)
>>> from paddle.distribution import transformed_distribution
>>> d = transformed_distribution.TransformedDistribution(
... paddle.distribution.Normal(0., 1.),
... [paddle.distribution.AffineTransform(paddle.to_tensor(1.), paddle.to_tensor(2.))]
... )
>>> # doctest: +SKIP('random sample')
>>> print(d.sample([10]))
Tensor(shape=[10], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 3.22699189, 1.12264419, 0.50283587, 1.83812487, -2.00740123,
-2.70338631, 1.26663208, 4.47909021, -0.11529565, 4.32719326])
>>> print(d.log_prob(paddle.to_tensor(0.5)))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
-1.64333570)
>>> # doctest: -SKIP
"""
def
__init__
(
self
,
base
,
transforms
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录