From 5210ad4227fdb0cbfdb148f6d728b375acbdaf04 Mon Sep 17 00:00:00 2001 From: AyaseNana <13659110308@163.com> Date: Wed, 3 Jul 2024 14:05:08 +0800 Subject: [PATCH 1/4] update typing --- python/paddle/distribution/uniform.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/python/paddle/distribution/uniform.py b/python/paddle/distribution/uniform.py index b9b4cf1e334803..ecd1a7f4e1d054 100644 --- a/python/paddle/distribution/uniform.py +++ b/python/paddle/distribution/uniform.py @@ -11,6 +11,9 @@ # 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 import numpy as np @@ -22,6 +25,9 @@ from paddle.framework import in_dynamic_mode from paddle.tensor import random +if TYPE_CHECKING: + from paddle import Tensor + class Uniform(distribution.Distribution): r"""Uniform distribution with `low` and `high` parameters. @@ -100,7 +106,12 @@ class Uniform(distribution.Distribution): [0.50000000]) """ - def __init__(self, low, high, name=None): + def __init__( + self, + low: float | list | tuple | np.ndarray | Tensor, + high: float | list | tuple | np.ndarray | Tensor, + name: str | None = None, + ) -> None: if not in_dynamic_mode(): check_type( low, @@ -165,7 +176,7 @@ def __init__(self, low, high, name=None): super().__init__(self.low.shape) - def sample(self, shape, seed=0): + def sample(self, shape: list, seed: int = 0) -> Tensor: """Generate samples of the specified shape. Args: @@ -218,7 +229,7 @@ def sample(self, shape, seed=0): else: return output - def log_prob(self, value): + def log_prob(self, value: Tensor) -> Tensor: """Log probability density/mass function. Args: @@ -247,7 +258,7 @@ def log_prob(self, value): paddle.log(lb * ub), paddle.log(self.high - self.low), name=name ) - def probs(self, value): + def probs(self, value: Tensor) -> Tensor: """Probability density/mass function. Args: @@ -272,7 +283,7 @@ def probs(self, value): ub = paddle.cast(ub_bool, dtype=value.dtype) return paddle.divide((lb * ub), (self.high - self.low), name=name) - def entropy(self): + def entropy(self) -> Tensor: r"""Shannon entropy in nats. The entropy is From cbff1dfc1cecc7e9e6a68c25aefa43882da6cbe3 Mon Sep 17 00:00:00 2001 From: AyaseNana <13659110308@163.com> Date: Wed, 3 Jul 2024 15:06:50 +0800 Subject: [PATCH 2/4] update --- python/paddle/distribution/uniform.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/python/paddle/distribution/uniform.py b/python/paddle/distribution/uniform.py index ecd1a7f4e1d054..21b12450fe2ada 100644 --- a/python/paddle/distribution/uniform.py +++ b/python/paddle/distribution/uniform.py @@ -13,9 +13,10 @@ # limitations under the License. from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Sequence import numpy as np +import numpy.typing as npt import paddle from paddle import _C_ops @@ -105,11 +106,21 @@ class Uniform(distribution.Distribution): Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.50000000]) """ + low: float | Sequence[float] | npt.NDArray[np.float32 | np.float64] | Tensor + high: float | Sequence[float] | npt.NDArray[ + np.float32 | np.float64 + ] | Tensor def __init__( self, - low: float | list | tuple | np.ndarray | Tensor, - high: float | list | tuple | np.ndarray | Tensor, + low: float + | Sequence[float] + | npt.NDArray[np.float32 | np.float64] + | Tensor, + high: float + | Sequence[float] + | npt.NDArray[np.float32 | np.float64] + | Tensor, name: str | None = None, ) -> None: if not in_dynamic_mode(): @@ -176,7 +187,7 @@ def __init__( super().__init__(self.low.shape) - def sample(self, shape: list, seed: int = 0) -> Tensor: + def sample(self, shape: list[int], seed: int = 0) -> Tensor: """Generate samples of the specified shape. Args: From aa25584cdd227d65686756a1bff7946812e1bd70 Mon Sep 17 00:00:00 2001 From: AyaseNana <49900969+NKNaN@users.noreply.github.com> Date: Wed, 3 Jul 2024 19:59:19 +0800 Subject: [PATCH 3/4] Update python/paddle/distribution/uniform.py Co-authored-by: Nyakku Shigure --- python/paddle/distribution/uniform.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/python/paddle/distribution/uniform.py b/python/paddle/distribution/uniform.py index 21b12450fe2ada..5911c625e28744 100644 --- a/python/paddle/distribution/uniform.py +++ b/python/paddle/distribution/uniform.py @@ -113,14 +113,18 @@ class Uniform(distribution.Distribution): def __init__( self, - low: float - | Sequence[float] - | npt.NDArray[np.float32 | np.float64] - | Tensor, - high: float - | Sequence[float] - | npt.NDArray[np.float32 | np.float64] - | Tensor, + low: ( + float + | Sequence[float] + | npt.NDArray[np.float32 | np.float64] + | Tensor + ), + high: ( + float + | Sequence[float] + | npt.NDArray[np.float32 | np.float64] + | Tensor + ), name: str | None = None, ) -> None: if not in_dynamic_mode(): From 868ca0ce2345b23f4fdea3fe6fd8ad7a6c7657ca Mon Sep 17 00:00:00 2001 From: AyaseNana <13659110308@163.com> Date: Wed, 3 Jul 2024 21:31:47 +0800 Subject: [PATCH 4/4] update class attr --- python/paddle/distribution/uniform.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/paddle/distribution/uniform.py b/python/paddle/distribution/uniform.py index 5911c625e28744..cefbeef9c60433 100644 --- a/python/paddle/distribution/uniform.py +++ b/python/paddle/distribution/uniform.py @@ -106,10 +106,8 @@ class Uniform(distribution.Distribution): Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.50000000]) """ - low: float | Sequence[float] | npt.NDArray[np.float32 | np.float64] | Tensor - high: float | Sequence[float] | npt.NDArray[ - np.float32 | np.float64 - ] | Tensor + low: Tensor + high: Tensor def __init__( self,