From d6a55156741c0d756d35ccd09ca1fb4e011397a0 Mon Sep 17 00:00:00 2001 From: enkilee Date: Tue, 9 Jul 2024 10:12:19 +0800 Subject: [PATCH 1/4] fix --- python/paddle/distribution/lognormal.py | 33 ++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/python/paddle/distribution/lognormal.py b/python/paddle/distribution/lognormal.py index bcf4a103ee756..592f73835bb73 100644 --- a/python/paddle/distribution/lognormal.py +++ b/python/paddle/distribution/lognormal.py @@ -11,12 +11,31 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + +from typing import TYPE_CHECKING, Sequence, Union import paddle from paddle.distribution.normal import Normal from paddle.distribution.transform import ExpTransform from paddle.distribution.transformed_distribution import TransformedDistribution +if TYPE_CHECKING: + import numpy as np + import numpy.typing as npt + from typing_extensions import TypeAlias + + from paddle import Tensor + from paddle._typing import NestedSequence + + _LognormalBoundary: TypeAlias = Union[ + float, + Sequence[float], + NestedSequence[float], + npt.NDArray[Union[np.float32, np.float64]], + Tensor, + ] + class LogNormal(TransformedDistribution): r"""The LogNormal distribution with location `loc` and `scale` parameters. @@ -89,14 +108,16 @@ class LogNormal(TransformedDistribution): [0.34939718]) """ - def __init__(self, loc, scale): + def __init__( + self, loc: _LognormalBoundary, scale: _LognormalBoundary + ) -> None: self._base = Normal(loc=loc, scale=scale) self.loc = self._base.loc self.scale = self._base.scale super().__init__(self._base, [ExpTransform()]) @property - def mean(self): + def mean(self) -> Tensor: """Mean of lognormal distribution. Returns: @@ -105,7 +126,7 @@ def mean(self): return paddle.exp(self._base.mean + self._base.variance / 2) @property - def variance(self): + def variance(self) -> Tensor: """Variance of lognormal distribution. Returns: @@ -115,7 +136,7 @@ def variance(self): 2 * self._base.mean + self._base.variance ) - def entropy(self): + def entropy(self) -> Tensor: r"""Shannon entropy in nats. The entropy is @@ -135,7 +156,7 @@ def entropy(self): """ return self._base.entropy() + self._base.mean - def probs(self, value): + def probs(self, value: Tensor) -> Tensor: """Probability density/mass function. Args: @@ -147,7 +168,7 @@ def probs(self, value): """ return paddle.exp(self.log_prob(value)) - def kl_divergence(self, other): + def kl_divergence(self, other: LogNormal) -> Tensor: r"""The KL-divergence between two lognormal distributions. The probability density function (pdf) is From bc313d65c074c48694e73d2cec42e5d948560e27 Mon Sep 17 00:00:00 2001 From: enkilee Date: Tue, 9 Jul 2024 12:53:27 +0800 Subject: [PATCH 2/4] fix --- python/paddle/distribution/lognormal.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/distribution/lognormal.py b/python/paddle/distribution/lognormal.py index 592f73835bb73..a848adf40bb45 100644 --- a/python/paddle/distribution/lognormal.py +++ b/python/paddle/distribution/lognormal.py @@ -107,6 +107,8 @@ class LogNormal(TransformedDistribution): Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.34939718]) """ + loc: Tensor + scale: Tensor def __init__( self, loc: _LognormalBoundary, scale: _LognormalBoundary From b7a3ad1606d6fd88d18ac19d5dbd68b4fe69c6c6 Mon Sep 17 00:00:00 2001 From: enkilee Date: Thu, 11 Jul 2024 11:55:10 +0800 Subject: [PATCH 3/4] fix --- python/paddle/distribution/lognormal.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/python/paddle/distribution/lognormal.py b/python/paddle/distribution/lognormal.py index a848adf40bb45..25b05779249bc 100644 --- a/python/paddle/distribution/lognormal.py +++ b/python/paddle/distribution/lognormal.py @@ -28,7 +28,18 @@ from paddle import Tensor from paddle._typing import NestedSequence - _LognormalBoundary: TypeAlias = Union[ + _LognormalLocBase: TypeAlias = Union[float, complex] + _LognormalLocNDArray: TypeAlias = Union[ + np.float32, np.float64, np.complex64, np.complex128 + ] + _LognormalLoc: TypeAlias = Union[ + _LognormalLocBase, + Sequence[float], + NestedSequence[float], + npt.NDArray[_LognormalLocNDArray], + Tensor, + ] + _LognormalScale: TypeAlias = Union[ float, Sequence[float], NestedSequence[float], @@ -62,7 +73,7 @@ class LogNormal(TransformedDistribution): * :math:`scale = \sigma`: is the stddevs of the underlying Normal distribution. Args: - loc(int|float|list|tuple|numpy.ndarray|Tensor): The means of the underlying Normal distribution. + loc(int|float|complex|list|tuple|numpy.ndarray|Tensor): The means of the underlying Normal distribution.The data type is float32, float64, complex64 and complex128. scale(int|float|list|tuple|numpy.ndarray|Tensor): The stddevs of the underlying Normal distribution. Examples: @@ -110,9 +121,7 @@ class LogNormal(TransformedDistribution): loc: Tensor scale: Tensor - def __init__( - self, loc: _LognormalBoundary, scale: _LognormalBoundary - ) -> None: + def __init__(self, loc: _LognormalLoc, scale: _LognormalScale) -> None: self._base = Normal(loc=loc, scale=scale) self.loc = self._base.loc self.scale = self._base.scale From 8d67846cf2ec0988daaef9c037465b9334d531f0 Mon Sep 17 00:00:00 2001 From: enkilee Date: Thu, 11 Jul 2024 14:38:55 +0800 Subject: [PATCH 4/4] fix --- python/paddle/distribution/lognormal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/distribution/lognormal.py b/python/paddle/distribution/lognormal.py index 25b05779249bc..5158e6cc910cb 100644 --- a/python/paddle/distribution/lognormal.py +++ b/python/paddle/distribution/lognormal.py @@ -34,8 +34,8 @@ ] _LognormalLoc: TypeAlias = Union[ _LognormalLocBase, - Sequence[float], - NestedSequence[float], + Sequence[_LognormalLocBase], + NestedSequence[_LognormalLocBase], npt.NDArray[_LognormalLocNDArray], Tensor, ]