Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Add] Paddle 代码 CI 中引入 xdoctest 检查 #55295

Merged
merged 18 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 100 additions & 100 deletions python/paddle/distribution/bernoulli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ class Bernoulli(exponential_family.ExponentialFamily):

.. code-block:: python

import paddle
from paddle.distribution import Bernoulli
>>> import paddle
>>> from paddle.distribution import Bernoulli

# init `probs` with a float
rv = Bernoulli(probs=0.3)
>>> # init `probs` with a float
>>> rv = Bernoulli(probs=0.3)

print(rv.mean)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.30000001)
>>> print(rv.mean)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.30000001)

print(rv.variance)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.21000001)
>>> print(rv.variance)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.21000001)

print(rv.entropy())
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.61086434)
>>> print(rv.entropy())
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.61086434)
"""

def __init__(self, probs, name=None):
Expand Down Expand Up @@ -156,24 +156,24 @@ def sample(self, shape):

.. code-block:: python

import paddle
from paddle.distribution import Bernoulli
>>> import paddle
>>> from paddle.distribution import Bernoulli

rv = Bernoulli(paddle.full((), 0.3))
print(rv.sample([100]).shape)
# [100]
>>> rv = Bernoulli(paddle.full((1), 0.3))
>>> print(rv.sample([100]).shape)
[100, 1]

rv = Bernoulli(paddle.to_tensor(0.3))
print(rv.sample([100]).shape)
# [100, 1]
>>> rv = Bernoulli(paddle.to_tensor(0.3))
>>> print(rv.sample([100]).shape)
[100]

rv = Bernoulli(paddle.to_tensor([0.3, 0.5]))
print(rv.sample([100]).shape)
# [100, 2]
>>> rv = Bernoulli(paddle.to_tensor([0.3, 0.5]))
>>> print(rv.sample([100]).shape)
[100, 2]

rv = Bernoulli(paddle.to_tensor([0.3, 0.5]))
print(rv.sample([100, 2]).shape)
# [100, 2, 2]
>>> rv = Bernoulli(paddle.to_tensor([0.3, 0.5]))
>>> print(rv.sample([100, 2]).shape)
[100, 2, 2]
"""
name = self.name + '_sample'
if not in_dynamic_mode():
Expand Down Expand Up @@ -211,48 +211,48 @@ def rsample(self, shape, temperature=1.0):

.. code-block:: python

import paddle
from paddle.distribution import Bernoulli

paddle.seed(2023)

rv = Bernoulli(paddle.full((), 0.3))
print(rv.sample([100]).shape)
# [100]

rv = Bernoulli(0.3)
print(rv.rsample([100]).shape)
# [100, 1]

rv = Bernoulli(paddle.to_tensor([0.3, 0.5]))
print(rv.rsample([100]).shape)
# [100, 2]

rv = Bernoulli(paddle.to_tensor([0.3, 0.5]))
print(rv.rsample([100, 2]).shape)
# [100, 2, 2]

# `rsample` has to be followed by a `sigmoid`
rv = Bernoulli(0.3)
rsample = rv.rsample([3, ])
rsample_sigmoid = paddle.nn.functional.sigmoid(rsample)
print(rsample, rsample_sigmoid)
# Tensor(shape=[3, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[-0.88315082],
# [-0.62347704],
# [-0.31513220]]) Tensor(shape=[3, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.29252526],
# [0.34899110],
# [0.42186251]])

# The smaller the `temperature`, the distribution of `rsample` closer to `sample`, with `probs` of 0.3.
print(paddle.nn.functional.sigmoid(rv.rsample([1000, ], temperature=1.0)).sum())
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 361.06829834)

print(paddle.nn.functional.sigmoid(rv.rsample([1000, ], temperature=0.1)).sum())
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 288.66418457)
>>> import paddle
>>> from paddle.distribution import Bernoulli

>>> rv = Bernoulli(paddle.full((1), 0.3))
>>> print(rv.sample([100]).shape)
[100, 1]

>>> rv = Bernoulli(0.3)
>>> print(rv.rsample([100]).shape)
[100]

>>> rv = Bernoulli(paddle.to_tensor([0.3, 0.5]))
>>> print(rv.rsample([100]).shape)
[100, 2]

>>> rv = Bernoulli(paddle.to_tensor([0.3, 0.5]))
>>> print(rv.rsample([100, 2]).shape)
[100, 2, 2]

>>> # `rsample` has to be followed by a `sigmoid`
>>> # doctest: +SKIP
>>> rv = Bernoulli(0.3)
>>> rsample = rv.rsample([3, ])
>>> rsample_sigmoid = paddle.nn.functional.sigmoid(rsample)
>>> print(rsample, rsample_sigmoid)
Tensor(shape=[3, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[-0.88315082],
[-0.62347704],
[-0.31513220]])
Tensor(shape=[3, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.29252526],
[0.34899110],
[0.42186251]])

>>> # The smaller the `temperature`, the distribution of `rsample` closer to `sample`, with `probs` of 0.3.
>>> print(paddle.nn.functional.sigmoid(rv.rsample([1000, ], temperature=1.0)).sum())
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
361.06829834)

>>> print(paddle.nn.functional.sigmoid(rv.rsample([1000, ], temperature=0.1)).sum())
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
288.66418457)
"""
name = self.name + '_rsample'
if not in_dynamic_mode():
Expand Down Expand Up @@ -308,13 +308,13 @@ def cdf(self, value):

.. code-block:: python

import paddle
from paddle.distribution import Bernoulli
>>> import paddle
>>> from paddle.distribution import Bernoulli

rv = Bernoulli(0.3)
print(rv.cdf(paddle.to_tensor([1.0])))
# Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1.])
>>> rv = Bernoulli(0.3)
>>> print(rv.cdf(paddle.to_tensor([1.0])))
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.])
"""
name = self.name + '_cdf'
if not in_dynamic_mode():
Expand Down Expand Up @@ -346,13 +346,13 @@ def log_prob(self, value):

.. code-block:: python

import paddle
from paddle.distribution import Bernoulli
>>> import paddle
>>> from paddle.distribution import Bernoulli

rv = Bernoulli(0.3)
print(rv.log_prob(paddle.to_tensor([1.0])))
# Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [-1.20397282])
>>> rv = Bernoulli(0.3)
>>> print(rv.log_prob(paddle.to_tensor([1.0])))
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[-1.20397282])
"""
name = self.name + '_log_prob'
if not in_dynamic_mode():
Expand Down Expand Up @@ -385,13 +385,13 @@ def prob(self, value):

.. code-block:: python

import paddle
from paddle.distribution import Bernoulli
>>> import paddle
>>> from paddle.distribution import Bernoulli

rv = Bernoulli(0.3)
print(rv.prob(paddle.to_tensor([1.0])))
# Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.29999998])
>>> rv = Bernoulli(0.3)
>>> print(rv.prob(paddle.to_tensor([1.0])))
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.29999998])
"""
name = self.name + '_prob'
if not in_dynamic_mode():
Expand All @@ -415,13 +415,13 @@ def entropy(self):

.. code-block:: python

import paddle
from paddle.distribution import Bernoulli
>>> import paddle
>>> from paddle.distribution import Bernoulli

rv = Bernoulli(0.3)
print(rv.entropy())
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.61086434)
>>> rv = Bernoulli(0.3)
>>> print(rv.entropy())
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.61086434)
"""
name = self.name + '_entropy'

Expand All @@ -448,15 +448,15 @@ def kl_divergence(self, other):

.. code-block:: python

import paddle
from paddle.distribution import Bernoulli
>>> import paddle
>>> from paddle.distribution import Bernoulli

rv = Bernoulli(0.3)
rv_other = Bernoulli(0.7)
>>> rv = Bernoulli(0.3)
>>> rv_other = Bernoulli(0.7)

print(rv.kl_divergence(rv_other))
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.33891910)
>>> print(rv.kl_divergence(rv_other))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.33891910)
"""
name = self.name + '_kl_divergence'
if not in_dynamic_mode():
Expand Down
54 changes: 29 additions & 25 deletions python/paddle/distribution/beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,35 @@ class Beta(exponential_family.ExponentialFamily):

.. code-block:: python

import paddle

# scale input
beta = paddle.distribution.Beta(alpha=0.5, beta=0.5)
print(beta.mean)
# Tensor(shape=[], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# 0.50000000)
print(beta.variance)
# Tensor(shape=[], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# 0.12500000)
print(beta.entropy())
# Tensor(shape=[], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# 0.12500000)

# tensor input with broadcast
beta = paddle.distribution.Beta(alpha=paddle.to_tensor([0.2, 0.4]), beta=0.6)
print(beta.mean)
# Tensor(shape=[2], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# [0.25000000, 0.40000001])
print(beta.variance)
# Tensor(shape=[2], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# [0.10416666, 0.12000000])
print(beta.entropy())
# Tensor(shape=[2], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# [-1.91923141, -0.38095069])
>>> import paddle

>>> # scale input
>>> beta = paddle.distribution.Beta(alpha=0.5, beta=0.5)
>>> print(beta.mean)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.50000000)

>>> print(beta.variance)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.12500000)

>>> print(beta.entropy())
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
-0.24156499)

>>> # tensor input with broadcast
>>> beta = paddle.distribution.Beta(alpha=paddle.to_tensor([0.2, 0.4]), beta=0.6)
>>> print(beta.mean)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.25000000, 0.40000001])

>>> print(beta.variance)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.10416666, 0.12000000])

>>> print(beta.entropy())
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[-1.91923141, -0.38095081])
"""

def __init__(self, alpha, beta):
Expand Down
Loading