-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[xdoctest] No.44-47 and No.50-59 doc style #55813
Changes from 4 commits
884f178
d099e35
109d91d
d2a1d81
7fe022e
510ec7f
0521b1a
4a5a2aa
a5c5634
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,12 +44,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.sample() # Laplace distributed with loc=0, scale=1 | ||
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
# 3.68546247) | ||
>>> 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 加个 seed 吧~ >>> import paddle
>>> paddle.seed(2023)
>>> m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
>>> print(m.sample()) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 喵喵喵?我看的是旧版本么? |
||
|
||
""" | ||
|
||
|
@@ -173,13 +173,13 @@ def log_prob(self, value): | |
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 @@ def entropy(self): | |
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 @@ def cdf(self, value): | |
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 @@ def icdf(self, value): | |
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,11 @@ def sample(self, shape=()): | |
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 | ||
>>> 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) | ||
""" | ||
shape = shape if isinstance(shape, tuple) else tuple(shape) | ||
with paddle.no_grad(): | ||
|
@@ -323,12 +321,11 @@ def rsample(self, shape): | |
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 | ||
>>> 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]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 加个 seed ~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
""" | ||
|
||
eps = self._get_eps() | ||
|
@@ -395,13 +392,13 @@ def kl_divergence(self, other): | |
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,36 +49,36 @@ 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() | ||
>>> # [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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这 4 个地方的输出用 >>> entropy = lognormal_a.entropy()
>>> print(entropy)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.41893852]) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里怎么也没看到更新? |
||
""" | ||
|
||
def __init__(self, loc, scale): | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -53,18 +53,18 @@ 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 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
>>> 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., 4., 5.], | ||||||||||||||||||||||||||||||
[0., 2., 8.], | ||||||||||||||||||||||||||||||
[2., 4., 4.]], | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
[[1., 6., 3.], | ||||||||||||||||||||||||||||||
[3., 3., 4.], | ||||||||||||||||||||||||||||||
[3., 4., 3.]]]) | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 输出不要留空行~ 另外,加个 seed ~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个要留空格吧 Python 3.10.8 (v3.10.8:aaaf517424, Oct 11 2022, 10:14:40) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import paddle
>>> paddle.seed(2023)
<paddle.fluid.libpaddle.Generator object at 0x1070bb270>
>>> 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.]]])
>>> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以留空格,美观就行~ 但是不要留空行~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def __init__(self, total_count, probs): | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,36 +54,36 @@ 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() | ||
>>> # [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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上,用 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里有加 print 嘛? |
||
""" | ||
|
||
def __init__(self, loc, scale, name=None): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的
#
要删掉~There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
怎么好像都没更新?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
7fe022e
(#55813) 这次提交里更新了