Skip to content
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

[Typing][B-16] Add type annotations for python/paddle/distribution/independent.py #65775

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions python/paddle/distribution/independent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from typing import TYPE_CHECKING, Sequence

from paddle.distribution import distribution

if TYPE_CHECKING:
from paddle import Tensor


class Independent(distribution.Distribution):
r"""
Expand Down Expand Up @@ -48,7 +55,9 @@ class Independent(distribution.Distribution):
-0.45687842)
"""

def __init__(self, base, reinterpreted_batch_rank):
def __init__(
self, base: distribution.Distribution, reinterpreted_batch_rank: int
) -> None:
if not isinstance(base, distribution.Distribution):
raise TypeError(
f"Expected type of 'base' is Distribution, but got {type(base)}"
Expand All @@ -71,28 +80,28 @@ def __init__(self, base, reinterpreted_batch_rank):
)

@property
def mean(self):
def mean(self) -> Tensor:
return self._base.mean

@property
def variance(self):
def variance(self) -> Tensor:
return self._base.variance

def sample(self, shape=()):
def sample(self, shape: Sequence[int] = ()) -> Tensor:
return self._base.sample(shape)

def log_prob(self, value):
def log_prob(self, value: Tensor) -> Tensor:
return self._sum_rightmost(
self._base.log_prob(value), self._reinterpreted_batch_rank
)

def prob(self, value):
def prob(self, value: Tensor) -> Tensor:
return self.log_prob(value).exp()

def entropy(self):
def entropy(self) -> Tensor:
return self._sum_rightmost(
self._base.entropy(), self._reinterpreted_batch_rank
)

def _sum_rightmost(self, value, n):
def _sum_rightmost(self, value: Tensor, n: int) -> Tensor:
return value.sum(list(range(-n, 0))) if n > 0 else value