torch.distributions.weibull 的源代码
# mypy: 允许未类型化定义
导入
火炬
from 火炬
导入
张量
from torch.distributions 导入
约束
from torch.distributions.exponential 导入
指数分布
from torch.distributions.gumbel 导入
欧拉常数
from torch.distributions.transformed_distribution 导入
转换分布
from torch.distributions.transforms 导入
变换矩阵,
Power 变换
from torch.distributions.utils 导入
广播全部
全部 = [
威布尔分布]
[文档]
类
威布尔分布(
转换分布):
r""
来自双参数威布尔分布的样本。
示例:
>>> # xdoctest: +IGNORE_WANT("非确定性")
>>> m = Weibull(torch.tensor([1.0]), torch.tensor([1.0]))
>>> m.sample() # 从尺度=1,集中度=1 的 Weibull 分布中采样
tensor([ 0.4784])
参数:
scale (float or Tensor): 分布的尺度参数(λ)。
浓度(浮点数或张量):分布的浓度参数(k/shape)。
""
约束参数 = {
"缩放":
约束.
正的,
浓度:
约束.
正的,
}
支持 =
约束.
正向
def __init__(self, 比例,
浓度,
验证参数=
无):
self.比例, self.concentration =
广播全部(
比例,
浓度)
self.浓度倒数 = self.
浓度.
相互()
基础分布 =
指数(
火炬.
喜欢的(self.
比例),
验证参数=
验证参数
)
转换 = [
功率变换(
指数=self.
浓度倒数),
变换矩阵(
位置=0,
比例=self.
比例),
]
超级().__init__(base_dist,
转换,
验证参数=
验证参数)
[文档] def expand(self, batch_shape, _instance=None):
new = self._get_checked_instance(Weibull, _instance)
new.scale = self.scale.expand(batch_shape)
new.concentration = self.concentration.expand(batch_shape)
new.concentration_reciprocal = new.concentration.reciprocal()
base_dist = self.base_dist.expand(batch_shape)
transforms = [
PowerTransform(exponent=new.concentration_reciprocal),
AffineTransform(loc=0, scale=new.scale),
]
super(Weibull, new).__init__(base_dist, transforms, validate_args=False)
new._validate_args = self._validate_args
return new
@property
def 均值(self)
翻译
张量:
返回 self.
缩放 *
火炬.exp(
火炬.lgamma(1 + self.
浓度倒数))
@property
def 模式(self)
翻译
张量:
返回 (
self.缩放
* ((self.concentration - 1) / self.浓度)
** self.浓度.
相互()
)
@property
def 方差(self)
翻译
张量:
返回 self.
比例.pow(2) * (
火炬.exp(
火炬.lgamma(1 + 2 * self.
浓度倒数))
- 火炬.exp(2 *
火炬.lgamma(1 + self.
浓度倒数))
)
[文档] def 熵(self):
return (
euler_constant * (1 - self.concentration_reciprocal)
+ torch.log(self.scale * self.concentration_reciprocal)
+ 1
)