torch.distributions.kumaraswamy 的源代码
# mypy: 允许未类型化定义
导入
火炬
from 火炬
导入
纳尼,
张量
from torch.distributions 导入
约束
from torch.distributions.transformed_distribution 导入
转换分布
from torch.distributions.transforms 导入
变换矩阵,
Power 变换
from torch.distributions.uniform 导入 Uniform
from torch.distributions.utils 导入
广播全部,
欧拉常数
全部 = [
库马拉斯瓦米]
定义
_矩(a, b, n):
```python
# 假设输入文本为:
input_text = """Immersive Translate"""
# 翻译函数(此处仅为示例,实际翻译功能需要调用真实的翻译 API)
def translate_to_simplified_chinese(text):
# 这里应该调用真实的翻译 API 进行翻译
# 由于示例中不使用真实的 API,以下为模拟翻译结果
return text # 假设翻译结果与原文相同
# 输出翻译结果
translated_text = translate_to_simplified_chinese(input_text)
print(translated_text)
```
输出:
```
Immersive Translate
```
使用 torch.lgamma 计算 Kumaraswamy 的第 n 阶矩
"""
参数 1 = 1 + n / a
对数值 =
火炬.lgamma(
参数 1) +
火炬.lgamma(b) -
火炬.lgamma(arg1 + b)
return b * 火炬.exp(log_value)
[文档]
类
库马尔斯瓦米分布(
转换分布):
r
```python
# 假设输入文本为:
input_text = """Immersive Translate"""
# 翻译函数(此处仅为示例,实际翻译功能需要调用真实的翻译 API)
def translate_to_simplified_chinese(text):
# 这里应该调用真实的翻译 API 进行翻译
# 由于示例中不使用真实的 API,以下为模拟翻译结果
return text # 假设翻译结果与原文相同
# 输出翻译结果
translated_text = translate_to_simplified_chinese(input_text)
print(translated_text)
```
输出:
```
Immersive Translate
```
来自 Kumaraswamy 分布的样本。
示例::
>>> # xdoctest: +IGNORE_WANT("非确定性")
>>> m = Kumaraswamy(torch.tensor([1.0]), torch.tensor([1.0]))
>>> 从浓度 alpha=1 和 beta=1 的 Kumaraswamy 分布中采样 m.sample()
tensor([ 0.1729])
参数:
浓度 1(浮点数或张量):分布的第 1 个浓度参数
(通常称为 alpha)
浓度 0(浮点数或张量):分布的第 2 个浓度参数
(通常称为 beta)
"""
约束参数 = {
浓度 1:
约束.
正的,
浓度 0:
约束.
正的,
}
支持 =
约束.
单位区间
has_rsample = 真实
定义 __init__(self,
集中 1,
集中 0,
验证参数=
无):
self.集中 1, self.
浓度 0 =
广播全部(
集中 1,
浓度 0
)
基础分布 =
均匀分布(
火炬.
完全一样(self.
集中 0, 0),
火炬.
完全一样(self.
集中力 0, 1),
验证参数=
验证参数,
)
转换 = [
功率变换(
指数=
自我.
集中 0.
相互()),
变换矩阵(
位置=1.0,
比例=-1.0),
功率变换(
指数=
自我.
集中 1.
相互()),
]
超级().__init__(
基础分布,
转换,
验证参数=
验证参数)
[文档] def expand(self, batch_shape, _instance=None):
new = self._get_checked_instance(Kumaraswamy, _instance)
new.concentration1 = self.concentration1.expand(batch_shape)
new.concentration0 = self.concentration0.expand(batch_shape)
return super().expand(batch_shape, _instance=new)
@property
定义
均值(
自我)
翻译
张量:
返回
瞬间(
自我.
集中 1,
自我.
集中 0, 1)
@property
定义
模式(
自我)
翻译
张量:
为了数值稳定性,请在对数空间中进行评估。
对数模式 = (
自我.
集中 0.
相互() * (-
自我.
集中 0).log1p()
- (-自我.
浓度 0 *
自我.
集中 1).log1p()
)
对数模式
[
自我.
浓度 0 < 1) | (
自我.
浓度 1 < 1)] =
纳尼
返回
日志模式.exp()
@property
定义
方差(
自我)
翻译
张量:
返回
_瞬间(
自我.
集中 1,
自我.
集中 0, 2) -
火炬.pow(
自我.
均值, 2
)
[文档] def 熵(self):
t1 = 1 - self.concentration1.reciprocal()
t0 = 1 - self.concentration0.reciprocal()
H0 = torch.digamma(self.concentration0 + 1) + euler_constant
return (
t0
+ t1 * H0
- torch.log(self.concentration1)
- torch.log(self.concentration0)
)