未验证 提交 5e96126d 编写于 作者: Z zachary sun 提交者: GitHub

[xdoctest] reformat example code with google style in No.48&49 (#55868)

* [Doctest]fix No.48&49, test=docs_preview

* remove end of lines

* fix_codestyle
上级 d13a49d6
...@@ -49,22 +49,22 @@ class Geometric(distribution.Distribution): ...@@ -49,22 +49,22 @@ class Geometric(distribution.Distribution):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.distribution import Geometric >>> from paddle.distribution import Geometric
geom = Geometric(0.5) >>> geom = Geometric(0.5)
geom.mean >>> print(geom.mean)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 2.) 2.)
geom.variance >>> print(geom.variance)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 2.) 2.)
geom.stddev >>> print(geom.stddev)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 1.41421354) 1.41421354)
""" """
def __init__(self, probs): def __init__(self, probs):
...@@ -140,13 +140,13 @@ class Geometric(distribution.Distribution): ...@@ -140,13 +140,13 @@ class Geometric(distribution.Distribution):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.distribution import Geometric >>> from paddle.distribution import Geometric
geom = Geometric(0.5) >>> geom = Geometric(0.5)
geom.pmf(2) >>> print(geom.pmf(2))
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.25000000) 0.25000000)
""" """
if isinstance(k, (numbers.Integral, framework.Variable)): if isinstance(k, (numbers.Integral, framework.Variable)):
return paddle.pow((1.0 - self.probs), k - 1.0) * self.probs return paddle.pow((1.0 - self.probs), k - 1.0) * self.probs
...@@ -171,13 +171,13 @@ class Geometric(distribution.Distribution): ...@@ -171,13 +171,13 @@ class Geometric(distribution.Distribution):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.distribution import Geometric >>> from paddle.distribution import Geometric
geom = Geometric(0.5) >>> geom = Geometric(0.5)
geom.log_pmf(2) >>> print(geom.log_pmf(2))
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# -1.38629436) -1.38629436)
""" """
if isinstance(k, (numbers.Integral, framework.Variable)): if isinstance(k, (numbers.Integral, framework.Variable)):
return paddle.log(self.pmf(k)) return paddle.log(self.pmf(k))
...@@ -199,16 +199,15 @@ class Geometric(distribution.Distribution): ...@@ -199,16 +199,15 @@ class Geometric(distribution.Distribution):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.distribution import Geometric >>> from paddle.distribution import Geometric
geom = Geometric(0.5) >>> paddle.seed(2023)
geom.sample((2,2)) >>> geom = Geometric(0.5)
# Tensor(shape=[2, 2, 1], dtype=float32, place=Place(cpu), stop_gradient=True, >>> print(geom.sample((2,2)))
# [[[4.28128004], Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.53546447]], [[0.20783406, 0.94300812],
# [[0.88012987], [1.94558561, 0.14360668]])
# [0.54070371]]])
""" """
with paddle.no_grad(): with paddle.no_grad():
return self.rsample(shape) return self.rsample(shape)
...@@ -226,15 +225,15 @@ class Geometric(distribution.Distribution): ...@@ -226,15 +225,15 @@ class Geometric(distribution.Distribution):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.distribution import Geometric >>> from paddle.distribution import Geometric
geom = Geometric(0.5)
geom.rsample((2,2)) >>> paddle.seed(2023)
# Tensor(shape=[2, 2, 1], dtype=float32, place=Place(cpu), stop_gradient=True, >>> geom = Geometric(0.5)
# [[[2.90974379], >>> print(geom.rsample((2,2)))
# [1.28049409]], Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[4.60141420], [[0.20783406, 0.94300812],
# [2.98836184]]]) [1.94558561, 0.14360668]])
""" """
shape = distribution.Distribution._extend_shape( shape = distribution.Distribution._extend_shape(
...@@ -261,13 +260,13 @@ class Geometric(distribution.Distribution): ...@@ -261,13 +260,13 @@ class Geometric(distribution.Distribution):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.distribution import Geometric >>> from paddle.distribution import Geometric
geom = Geometric(0.5) >>> geom = Geometric(0.5)
geom.entropy() >>> print(geom.entropy())
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 1.38629436) 1.38629436)
""" """
x = (1.0 - self.probs) * paddle.log(1.0 - self.probs) x = (1.0 - self.probs) * paddle.log(1.0 - self.probs)
y = self.probs * paddle.log(self.probs) y = self.probs * paddle.log(self.probs)
...@@ -291,13 +290,13 @@ class Geometric(distribution.Distribution): ...@@ -291,13 +290,13 @@ class Geometric(distribution.Distribution):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.distribution import Geometric >>> from paddle.distribution import Geometric
geom = Geometric(0.5) >>> geom = Geometric(0.5)
geom.cdf(4) >>> print(geom.cdf(4))
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.93750000) 0.93750000)
""" """
if isinstance(k, (numbers.Integral, framework.Variable)): if isinstance(k, (numbers.Integral, framework.Variable)):
return 1.0 - paddle.pow((1.0 - self.probs), k) return 1.0 - paddle.pow((1.0 - self.probs), k)
...@@ -323,14 +322,14 @@ class Geometric(distribution.Distribution): ...@@ -323,14 +322,14 @@ class Geometric(distribution.Distribution):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.distribution import Geometric >>> from paddle.distribution import Geometric
geom_p = Geometric(0.5) >>> geom_p = Geometric(0.5)
geom_q = Geometric(0.1) >>> geom_q = Geometric(0.1)
geom_p.kl_divergence(geom_q) >>> print(geom_p.kl_divergence(geom_q))
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.51082563) 0.51082563)
""" """
if isinstance(other, Geometric): if isinstance(other, Geometric):
p, q = self.probs, other.probs p, q = self.probs, other.probs
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册