• 文档 >
  • 模块代码 >
  • torch >
  • torch.distributions.independent
快捷键

torch.distributions.independent 源代码

# mypy: 允许未类型化定义

导入 火炬
from 火炬 导入 张量
from torch.distributions 导入 约束
from torch.distributions.distribution 导入 分布
from torch.distributions.utils 导入 _sum_rightmost
from torch.types 导入 _大小


全部 = [独立]


[文档] 独立(分发): 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 ``` 重新解释了分布中的一些批维度为事件维度。 这对于改变结果形状主要是有用的 :meth:`log_prob`. 例如,要创建对角正态分布: 与多元正态分布具有相同的形状(因此它们是) 可互换的),你可以: >>> 从 torch.distributions.multivariate_normal 导入 MultivariateNormal >>> 从 torch.distributions.normal 导入 Normal >>> loc = torch.zeros(3) >>> scale = torch.ones(3) >>> mvn = MultivariateNormal(loc, scale_tril=torch.diag(scale)) >>> [mvn.batch_shape, mvn.event_shape] [torch.Size([]), torch.Size([3])] >>> normal = Normal(loc, scale) >>> [normal.batch_shape, normal.event_shape] [torch.Size([3]), torch.Size([])] >>> diagn = Independent(normal, 1) >>> [diagn.batch_shape, diagn.event_shape] [torch.Size([]), torch.Size([3])] 参数: base_distribution (torch.distributions.distribution.Distribution): a base distribution reinterpreted_batch_ndims (int): 批量维度数(整数) reinterpret as event dims 转换为事件维度 """ 参数约束: 字典[字符串, 约束.约束] = {} 定义 __init__( 自我, base_distribution 基础分布, 重新解释的批量维度, 验证参数= ): 如果 重新解释的批量维度 > 长度(base_distribution 基础分布.批量形状): 抛出 值错误( "期望重新解释的批次维度数小于等于基础分布的批次形状长度," f"实际"{重新解释的批量维度}{长度(基础分布.批量形状)}" ) 形状 = 基础分布.批量形状 + 基础分布.事件形状 事件维度 = 重新解释的批量维度 + 长度(基础分布.event_shape) 批量形状 = 形状[ 长度(形状) - 事件维度] 事件形状 = 形状[长度(形状) - 事件维度 ] 自我.base_dist = 基础分布 自我.重新解释的批量维度 = 重新解释的批量维度 超级().__init__(批量形状, event_shape, 验证参数=验证参数)
[文档] def expand(self, batch_shape, _instance=None): new = self._get_checked_instance(Independent, _instance) batch_shape = torch.Size(batch_shape) new.base_dist = self.base_dist.expand( batch_shape + self.event_shape[: self.reinterpreted_batch_ndims] ) new.reinterpreted_batch_ndims = self.reinterpreted_batch_ndims super(Independent, new).__init__( batch_shape, self.event_shape, validate_args=False ) new._validate_args = self._validate_args return new
@property 定义
具有重采样功能(自我) 翻译 布尔: # 类型:忽略[覆盖] 返回 自我.base_dist.has_rsample @property 定义 支持枚举(自我) 翻译 布尔: # 类型:忽略[覆盖] 如果 自我.重新解释的批量维度 > 0: 返回 返回 自我.base_dist.支持枚举 @constraints.依赖属性 定义 支持(自我): 结果 = 自我.base_dist.支持 如果 自我.重新解释的批量维度: 结果 = 约束.独立的(结果, 自我.重新解释的批量维度) 返回 结果 @property 定义 均值(自我) 翻译 张量: 返回 自我.base_dist.平均值 @property 定义 模式(自我) 翻译 张量: 返回 自我.base_dist.模式 @property 定义 方差(自我) 翻译 张量: 返回 自我.base_dist.方差
[文档] def sample(self, sample_shape=torch.Size()) -> Tensor: return self.base_dist.sample(sample_shape)
[文档] def rsample(self, sample_shape: _size = torch.Size()) -> Tensor: return self.base_dist.rsample(sample_shape)
[文档] def log_prob(self, value): log_prob = self.base_dist.log_prob(value) return _sum_rightmost(log_prob, self.reinterpreted_batch_ndims)
[文档] def entropy(self): entropy = self.base_dist.entropy() return _sum_rightmost(entropy, self.reinterpreted_batch_ndims)
[文档] def enumerate_support(self, expand=True): if self.reinterpreted_batch_ndims > 0: 引发未实现错误(NotImplementedError) 枚举笛卡尔积未实现 ) 返回 self.base_dist.enumerate_support(expand=expand)
定义
__repr__(自我): 返回 ( 自我..__name__ + f“(”{自我.base_dist}, {自我.重新解释的批量维度})" )

© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源,获取您的疑问解答

查看资源