Skip to content

Commit

Permalink
[xdoctest] reformat example code with google style in No.48&49 (#55868)
Browse files Browse the repository at this point in the history
* [Doctest]fix No.48&49, test=docs_preview

* remove end of lines

* fix_codestyle
  • Loading branch information
sunzhongkai588 authored Aug 2, 2023
1 parent d13a49d commit 5e96126
Showing 1 changed file with 60 additions and 61 deletions.
121 changes: 60 additions & 61 deletions python/paddle/distribution/geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ class Geometric(distribution.Distribution):
.. code-block:: python
import paddle
from paddle.distribution import Geometric
>>> import paddle
>>> from paddle.distribution import Geometric
geom = Geometric(0.5)
>>> geom = Geometric(0.5)
geom.mean
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 2.)
>>> print(geom.mean)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.)
geom.variance
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 2.)
>>> print(geom.variance)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.)
geom.stddev
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 1.41421354)
>>> print(geom.stddev)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.41421354)
"""

def __init__(self, probs):
Expand Down Expand Up @@ -140,13 +140,13 @@ def pmf(self, k):
.. code-block:: python
import paddle
from paddle.distribution import Geometric
>>> import paddle
>>> from paddle.distribution import Geometric
geom = Geometric(0.5)
geom.pmf(2)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.25000000)
>>> geom = Geometric(0.5)
>>> print(geom.pmf(2))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.25000000)
"""
if isinstance(k, (numbers.Integral, framework.Variable)):
return paddle.pow((1.0 - self.probs), k - 1.0) * self.probs
Expand All @@ -171,13 +171,13 @@ def log_pmf(self, k):
.. code-block:: python
import paddle
from paddle.distribution import Geometric
>>> import paddle
>>> from paddle.distribution import Geometric
geom = Geometric(0.5)
geom.log_pmf(2)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# -1.38629436)
>>> geom = Geometric(0.5)
>>> print(geom.log_pmf(2))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
-1.38629436)
"""
if isinstance(k, (numbers.Integral, framework.Variable)):
return paddle.log(self.pmf(k))
Expand All @@ -199,16 +199,15 @@ def sample(self, shape=()):
.. code-block:: python
import paddle
from paddle.distribution import Geometric
>>> import paddle
>>> from paddle.distribution import Geometric
geom = Geometric(0.5)
geom.sample((2,2))
# Tensor(shape=[2, 2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[[4.28128004],
# [0.53546447]],
# [[0.88012987],
# [0.54070371]]])
>>> paddle.seed(2023)
>>> geom = Geometric(0.5)
>>> print(geom.sample((2,2)))
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.20783406, 0.94300812],
[1.94558561, 0.14360668]])
"""
with paddle.no_grad():
return self.rsample(shape)
Expand All @@ -226,15 +225,15 @@ def rsample(self, shape=()):
.. code-block:: python
import paddle
from paddle.distribution import Geometric
geom = Geometric(0.5)
geom.rsample((2,2))
# Tensor(shape=[2, 2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[[2.90974379],
# [1.28049409]],
# [[4.60141420],
# [2.98836184]]])
>>> import paddle
>>> from paddle.distribution import Geometric
>>> paddle.seed(2023)
>>> geom = Geometric(0.5)
>>> print(geom.rsample((2,2)))
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.20783406, 0.94300812],
[1.94558561, 0.14360668]])
"""
shape = distribution.Distribution._extend_shape(
Expand All @@ -261,13 +260,13 @@ def entropy(self):
.. code-block:: python
import paddle
from paddle.distribution import Geometric
>>> import paddle
>>> from paddle.distribution import Geometric
geom = Geometric(0.5)
geom.entropy()
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 1.38629436)
>>> geom = Geometric(0.5)
>>> print(geom.entropy())
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.38629436)
"""
x = (1.0 - self.probs) * paddle.log(1.0 - self.probs)
y = self.probs * paddle.log(self.probs)
Expand All @@ -291,13 +290,13 @@ def cdf(self, k):
.. code-block:: python
import paddle
from paddle.distribution import Geometric
>>> import paddle
>>> from paddle.distribution import Geometric
geom = Geometric(0.5)
geom.cdf(4)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.93750000)
>>> geom = Geometric(0.5)
>>> print(geom.cdf(4))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.93750000)
"""
if isinstance(k, (numbers.Integral, framework.Variable)):
return 1.0 - paddle.pow((1.0 - self.probs), k)
Expand All @@ -323,14 +322,14 @@ def kl_divergence(self, other):
.. code-block:: python
import paddle
from paddle.distribution import Geometric
>>> import paddle
>>> from paddle.distribution import Geometric
geom_p = Geometric(0.5)
geom_q = Geometric(0.1)
geom_p.kl_divergence(geom_q)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.51082563)
>>> geom_p = Geometric(0.5)
>>> geom_q = Geometric(0.1)
>>> print(geom_p.kl_divergence(geom_q))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.51082563)
"""
if isinstance(other, Geometric):
p, q = self.probs, other.probs
Expand Down

0 comments on commit 5e96126

Please sign in to comment.