torch.distributions.inverse_gamma 源代码
# mypy: 允许未类型化定义
导入
火炬
from 火炬
导入
张量
from torch.distributions 导入
约束
from torch.distributions.gamma 导入
伽马分布
from torch.distributions.transformed_distribution 导入
转换分布
from torch.distributions.transforms 导入
Power 变换
全部 = [
InverseGamma]
[文档]
类
逆伽马(
转换分布):
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
```
通过 :attr:`浓度` 和 :attr:`速率` 参数化的逆伽马分布创建
其中:
X ~ Gamma(concentration, rate)
Y = 1 / X ~ InverseGamma(concentration, rate)
示例::
>>> # xdoctest: +IGNORE_WANT("非确定性的")
>>> m = InverseGamma(torch.tensor([2.0]), torch.tensor([3.0]))
>>> m.sample()
tensor([ 1.2953])
参数:
浓度(浮点数或张量):分布的形状参数
(通常称为 alpha)
率(浮点数或张量):rate = 分布的尺度倒数
(通常称为 beta)
"""
约束参数 = {
浓度:
约束.
正的,
率:
约束.
正的,
}
支持 =
约束.
正向
has_rsample = 真实
定义 __init__(
自我,
浓度, rate,
验证参数=
无):
基础分布 =
伽马(
浓度, rate,
验证参数=
验证参数)
负一 = -
基础分布.rate.
新一(())
超级().__init__(
基础分布,
动力转换(
负一),
验证参数=
验证参数
)
[文档] def expand(self, batch_shape, _instance=None):
new = self._get_checked_instance(InverseGamma, _instance)
return super().expand(batch_shape, _instance=new)
@property
定义
浓度(
自我)
翻译
张量:
返回
自我.
基础分布.concentration
@property
定义 rate(
自我)
翻译
张量:
return self.base_dist.速率
@property
定义
均值(
自我)
翻译
张量:
结果 =
自我.rate / (
自我.concentration - 1)
返回
火炬.
哪里(
自我.concentration > 1,
结果,
火炬.
无穷)
@property
定义
模式(
自我)
翻译
张量:
返回
自我.rate / (
自我.concentration + 1)
@property
定义
方差(
自我)
翻译
张量:
结果 =
自我.rate.
平方() / (
(自我.concentration - 1).
平方() * (
自我.concentration - 2)
)
返回
火炬.
哪里(
自我.concentration > 2,
结果,
火炬.
无穷)
[文档] 定义 self.熵(self):
返回 (
self.concentration
+ self.rate.log()
+ self.concentration.lgamma()
- (1 + self.concentration) * self.concentration.digamma()
)