torch.distributions.poisson 的源代码
# mypy: 允许未类型化定义
导入
火炬
from 火炬
导入
张量
from torch.distributions 导入
约束
from torch.distributions.exp_family 导入
指数族
from torch.distributions.utils 导入
广播全部
from torch.types 导入
数值
全部 = [
泊松]
[文档]
类
泊松(
指数族):
r""
创建一个由 :attr:`rate` 率参数指定的泊松分布。
样本是非负整数,其概率质量函数(pmf)为
.. math::
\mathrm{rate}^k \frac{e^{-\mathrm{rate}}}{k!}
示例::
>>> # xdoctest: +SKIP("poisson_cpu not implemented for 'Long'")
>>> m = Poisson(torch.tensor([4]))
>>> m.sample()
tensor([ 3.])
参数:
速率(数字,张量):速率参数
""
约束参数 = {
率:
约束.
非负的}
支持 =
约束.
非负整数
@property
def 均值(self)
翻译
张量:
返回 self.
比率
@property
def 模式(self)
翻译
张量:
返回 self.rate.
向下取整()
@property
def 方差(self)
翻译
张量:
返回 self.
比率
def __init__(self, rate, 验证参数=
无):
(self.rate,) = 广播全部(rate)
如果 isinstance(rate,
数):
批量形状 =
火炬.
尺寸()
else:
批量形状 = self.rate.
尺寸()
超级().__init__(
批量形状,
验证参数=
验证参数)
[文档] def expand(self, batch_shape, _instance=None):
new = self._get_checked_instance(Poisson, _instance)
batch_shape = torch.Size(batch_shape)
new.rate = self.rate.expand(batch_shape)
super(Poisson, new).__init__(batch_shape, validate_args=False)
new._validate_args = self._validate_args
返回新的
[文档] def sample(self, sample_shape=torch.Size()):
shape = self._extended_shape(sample_shape)
with torch.no_grad():
return torch.poisson(self.rate.expand(shape))
[文档] def log_prob(self, value)
if self._validate_args:
self._validate_sample(value)
rate, value = 广播_all(self.rate, value)
return value.xlogy(rate) - rate - (value + 1).lgamma()
@property
def 自然参数(self)
翻译
元组[
张量
]
返回 (
火炬.
日志(self.rate),)
def 对数归一化器(self, x):
返回
火炬.exp(x)