-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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-02,B-04] Add type annotations for python/paddle/distribution/{bernoulli, binomial}.py
#65727
Merged
Merged
[Typing][B-02,B-04] Add type annotations for python/paddle/distribution/{bernoulli, binomial}.py
#65727
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,18 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Sequence | ||
from typing import TYPE_CHECKING | ||
|
||
import paddle | ||
from paddle.distribution import distribution | ||
|
||
if TYPE_CHECKING: | ||
from paddle import Tensor | ||
from paddle._typing.dtype_like import _DTypeLiteral | ||
|
||
|
||
class Binomial(distribution.Distribution): | ||
r""" | ||
|
@@ -67,7 +74,13 @@ class Binomial(distribution.Distribution): | |
[2.94053698, 3.00781751, 2.51124287]) | ||
""" | ||
|
||
def __init__(self, total_count, probs): | ||
dtype: _DTypeLiteral | ||
total_count: Tensor | ||
probs: Tensor | ||
|
||
def __init__( | ||
self, total_count: int | Tensor, probs: float | Tensor | ||
) -> None: | ||
self.dtype = paddle.get_default_dtype() | ||
self.total_count, self.probs = self._to_tensor(total_count, probs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 下面的 另外我目测了下,返回值应该是 |
||
|
||
|
@@ -77,11 +90,13 @@ def __init__(self, total_count, probs): | |
batch_shape = self.total_count.shape | ||
super().__init__(batch_shape) | ||
|
||
def _to_tensor(self, total_count, probs): | ||
def _to_tensor( | ||
self, total_count: int | Tensor, probs: float | Tensor | ||
) -> list[Tensor]: | ||
"""Convert the input parameters into Tensors if they were not and broadcast them | ||
|
||
Returns: | ||
Tuple[Tensor, Tensor]: converted total_count and probs. | ||
list[Tensor]: converted total_count and probs. | ||
""" | ||
# convert type | ||
if isinstance(probs, float): | ||
|
@@ -97,7 +112,7 @@ def _to_tensor(self, total_count, probs): | |
return paddle.broadcast_tensors([total_count, probs]) | ||
|
||
@property | ||
def mean(self): | ||
def mean(self) -> Tensor: | ||
"""Mean of binomial distribution. | ||
|
||
Returns: | ||
|
@@ -106,15 +121,15 @@ def mean(self): | |
return self.total_count * self.probs | ||
|
||
@property | ||
def variance(self): | ||
def variance(self) -> Tensor: | ||
"""Variance of binomial distribution. | ||
|
||
Returns: | ||
Tensor: variance value. | ||
""" | ||
return self.total_count * self.probs * (1 - self.probs) | ||
|
||
def sample(self, shape=()): | ||
def sample(self, shape: Sequence[int] = ()) -> Tensor: | ||
"""Generate binomial samples of the specified shape. The final shape would be ``shape+batch_shape`` . | ||
|
||
Args: | ||
|
@@ -139,7 +154,7 @@ def sample(self, shape=()): | |
) | ||
return paddle.cast(sample, self.dtype) | ||
|
||
def entropy(self): | ||
def entropy(self) -> Tensor: | ||
r"""Shannon entropy in nats. | ||
|
||
The entropy is | ||
|
@@ -159,7 +174,7 @@ def entropy(self): | |
log_prob = self.log_prob(values) | ||
return -(paddle.exp(log_prob) * log_prob).sum(0) | ||
|
||
def _enumerate_support(self): | ||
def _enumerate_support(self) -> Tensor: | ||
"""Return the support of binomial distribution [0, 1, ... ,n] | ||
|
||
Returns: | ||
|
@@ -171,14 +186,14 @@ def _enumerate_support(self): | |
values = values.reshape((-1,) + (1,) * len(self.batch_shape)) | ||
return values | ||
|
||
def log_prob(self, value): | ||
def log_prob(self, value: Tensor) -> Tensor: | ||
"""Log probability density/mass function. | ||
|
||
Args: | ||
value (Tensor): The input tensor. | ||
value (Tensor): The input tensor. | ||
|
||
Returns: | ||
Tensor: log probability. The data type is the same as `probs`. | ||
Tensor: log probability. The data type is the same as `probs`. | ||
""" | ||
value = paddle.cast(value, dtype=self.dtype) | ||
|
||
|
@@ -200,7 +215,7 @@ def log_prob(self, value): | |
neginf=-eps, | ||
) | ||
|
||
def prob(self, value): | ||
def prob(self, value: Tensor) -> Tensor: | ||
"""Probability density/mass function. | ||
|
||
Args: | ||
|
@@ -211,7 +226,7 @@ def prob(self, value): | |
""" | ||
return paddle.exp(self.log_prob(value)) | ||
|
||
def kl_divergence(self, other): | ||
def kl_divergence(self, other: Binomial) -> Tensor: | ||
r"""The KL-divergence between two binomial distributions with the same :attr:`total_count`. | ||
|
||
The probability density function (pdf) is | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看看 dtype 是不是也可以写下?