目录

概率分布 - torch.distributions

该包包含可参数化的概率分布 和采样函数。这允许构建随机计算 用于优化的图形和随机梯度估计器。此软件包 通常遵循 TensorFlow Distributions 软件包的设计。distributions

无法直接通过随机样本进行反向传播。然而 创建代理函数有两种主要方法,可以是 反向传播通过。这些是分数函数估计量/似然比 estimator/REINFORCE 和 pathwise 导数估计器。REINFORCE 通常 被视为强化学习中策略梯度方法的基础,而 Pathwise Derivative Estimator 常见于 reparameterization 技巧 在变分自动编码器中。而 score 函数只需要 样本数量f(x)f(x),路径导数需要f(x)f'(x).接下来的章节将讨论强化学习中的这两者 例。有关更多详细信息,请参阅 Gradient Estimation Using Stochastic Computation Graphs

Score 函数

当概率密度函数相对于其 parameters 中,我们只需要 和 实现 REINFORCE:sample()log_prob()

Δθ=αr日志p(一个πθ(s))θ\Delta\theta = \alpha r \frac{\partial\log p(a|\pi^\theta(s))}{\partial\theta}

哪里θ\theta是参数,α\alpha是学习率,rr是奖励,而p(一个πθ(s))p(a|\pi^\theta(s))是 采取行动一个a在状态ss给定策略πθ\pi^\theta.

在实践中,我们会从网络的输出中采样一个操作,应用这个 action 中,然后使用 API 来构造等效的 loss 函数。请注意,我们使用负数,因为优化器使用 gradient descent,而上述规则假定 Gradient Ascent。使用分类 policy,则实施 REINFORCE 的代码如下:log_prob

probs = policy_network(state)
# Note that this is equivalent to what used to be called multinomial
m = Categorical(probs)
action = m.sample()
next_state, reward = env.step(action)
loss = -m.log_prob(action) * reward
loss.backward()

Pathwise 导数

实现这些随机/策略梯度的另一种方法是使用 reparameterization 技巧,其中 parameterized random variable 可以通过 parameterized 无参数随机变量的确定性函数。重新参数化的 因此,样本变得可微分。用于实现 pathwise 的代码 衍生数将如下所示:rsample()

params = policy_network(state)
m = Normal(*params)
# Any distribution with .has_rsample == True could work based on the application
action = m.rsample()
next_state, reward = env.step(action)  # Assuming that reward is differentiable
loss = -reward
loss.backward()

分配

torch.distributions.distribution 中。distributionbatch_shape=torch 的Size([])event_shape=Torch。Size([])validate_args=[来源]

基地:

Distribution 是概率分布的抽象基类。

属性arg_constraints:Dict[str Constraint]

返回从参数名称到对象的字典,这些对象 应该由此 distribution 的每个参数满足。Args 的 不是张量,不需要出现在这个 dict 中。

属性 batch_shape: Size

返回对其参数进行批处理的形状。

cdf[来源]

返回计算值为 的累积密度/质量函数。

参数

value张量) –

返回类型

张肌

entropy)[来源]

返回 batch_shape 分批处理的分布熵。

返回

形状为 batch_shape 的张量。

返回类型

张肌

enumerate_supportexpand=True[来源]

返回包含 discrete 支持的所有值的张量 分配。结果将在维度 0 上枚举,因此形状 的结果将是 (cardinality,) + batch_shape + event_shape (其中 event_shape = () 对于单变量分布)。

请注意,这将枚举锁步 [[0, 0], [1, 1], ...] 中的所有批处理张量。使用 expand=False 时,将发生枚举 沿 Dim 0,但其余批次维度为 单例维度, [[0], [1], ...

要迭代完整的笛卡尔积,请使用 itertools.product(m.enumerate_support())。

参数

expandbool) – 是否扩展对 批量变暗以匹配分配的batch_shape

返回

Tensor 迭代维度 0。

返回类型

张肌

属性 event_shape: 大小

返回单个样本的形状(无批处理)。

expandbatch_shape_instance=[来源]

返回新的分发实例(或填充现有实例 由派生类提供),并将批次维度扩展为 batch_shape。此方法调用 分配的参数。因此,这不会分配新的 memory 的 memory 进行扩展的分发实例。此外 这不会在首次创建实例时在 __init__.py 中重复任何 ARGS 检查或参数广播。

参数
  • batch_shapeTorch。Size) – 所需的扩展大小。

  • _instance – 由子类提供的新实例 需要覆盖 .expand

返回

批次维度扩展为 batch_size 的新分配实例。

icdf[来源]

返回在 value 处计算的逆累积密度/质量函数。

参数

value张量) –

返回类型

张肌

log_probvalue[来源]

返回在 value 处计算的概率密度/质量函数的对数。

参数

value张量) –

返回类型

张肌

property mean张量

返回分布的平均值。

property mode张量

返回分布的模式。

perplexity)[来源]

返回分batch_shape批处理的分布的困惑度。

返回

形状为 batch_shape 的张量。

返回类型

张肌

rsamplesample_shape=Torch。Size([])[来源]

生成 sample_shape 形状的重新参数化样品或sample_shape 如果分布参数 进行批处理。

返回类型

张肌

samplesample_shape=torch 的Size([])[来源]

生成 sample_shape 形样品或 sample_shape 形批次 samples (如果分布参数是批处理的)。

返回类型

张肌

sample_nn[来源]

如果分布 参数是批处理的。

返回类型

张肌

static set_default_validate_argsvalue[来源]

设置是启用还是禁用验证。

默认行为模仿 Python 的语句:validation 默认为 on,但如果 Python 以优化模式运行,则将其禁用 (通过 )。验证可能很昂贵,因此您可能需要 一旦模型开始工作,就禁用它。assertpython -O

参数

valuebool) – 是否启用验证。

property stddev张量

返回分布的标准差。

属性支持Optional[Any]

返回一个对象 表示此发行版的支持。

property variance 张量

返回分布的方差。

指数系列

torch.distributions.exp_family 类ExponentialFamilybatch_shape=Torch.Size([])event_shape=Torch。Size([])validate_args=[来源]

基地:

ExponentialFamily 是属于 指数族,其概率质量/密度函数的形式定义如下

pF(x;θ)=exp (英文)(t(x),θF(θ)+k(x))p_{F}(x; \theta) = \exp(\langle t(x), \theta\rangle - F(\theta) + k(x))

哪里θ\theta表示自然参数,t(x)t(x)表示足够的统计数据,F(θ)F(\theta)是给定系列的对数归一化器函数,k(x)k(x)是运维 量。

注意

这个类是 Distribution 类和属于 到指数族,主要是为了检查 .entropy() 和解析 KL 的正确性 Divergence 方法。我们使用这个类来计算使用 AD 的熵和 KL 散度 框架和 Bregman 散度(由:Frank Nielsen 和 Richard Nock、Entropies 和 指数族的交叉熵)。

entropy)[来源]

使用对数归一化器的 Bregman 散度计算熵的方法。

伯努利

torch.distributions.bernoulli 中。伯努利probs=logits=validate_args=[来源]

基地:

创建由 参数化的伯努利分布 (但不能同时创建两者)。

样本是二进制的(0 或 1)。他们取值 1 的概率 p,取值 0 的概率 1 - p

例:

>>> m = Bernoulli(torch.tensor([0.3]))
>>> m.sample()  # 30% chance 1; 70% chance 0
tensor([ 0.])
参数
  • probsNumberTensor) – 采样概率 1

  • logitsNumberTensor) – 采样 1 的对数几率

arg_constraints = {'logits': Real(), 'probs': 区间(lower_bound=0.0, upper_bound=1.0)}
entropy)[来源]
enumerate_supportexpand=True[来源]
expandbatch_shape_instance=[来源]
has_enumerate_support =
log_probvalue[来源]
Property Logits 属性
属性平均值
property mode
属性param_shape
属性问题
samplesample_shape=torch 的Size([])[来源]
support = 布尔值()
属性差异

试用版

torch.distributions.beta 中。Beta浓度1浓度0,validate_args=[来源]

基地:

Beta 分布由 参数化。

例:

>>> m = Beta(torch.tensor([0.5]), torch.tensor([0.5]))
>>> m.sample()  # Beta distributed with concentration concentration1 and concentration0
tensor([ 0.1046])
参数
  • concentration1floatTensor) – 分布的第 1 个浓度参数 (通常称为 Alpha)

  • concentration0floatTensor) – 分布的第 2 个浓度参数 (通常称为 beta)

arg_constraints = {'concentration0': GreaterThan(lower_bound=0.0), 'concentration1': GreaterThan(lower_bound=0.0)}
性状浓度0
属性浓度1
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=()[来源]
返回类型

张肌

support = 区间(lower_bound=0.0, upper_bound=1.0)
属性差异

二项式

torch.distributions.binomial 中。二项式total_count=1probs=logits=validate_args=[来源]

基地:

创建参数化为 和 的二项分布 要么 or (但不能同时) 。 必须是 可通过 / 进行广播。total_counttotal_count

例:

>>> m = Binomial(100, torch.tensor([0 , .2, .8, 1]))
>>> x = m.sample()
tensor([   0.,   22.,   71.,  100.])

>>> m = Binomial(torch.tensor([[5.], [10.]]), torch.tensor([0.5, 0.8]))
>>> x = m.sample()
tensor([[ 4.,  5.],
        [ 7.,  6.]])
参数
  • total_countintTensor) – 伯努利试验数

  • probsTensor) - 事件概率

  • logitsTensor) – 事件对数几率

arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0), 'total_count': IntegerGreaterThan(lower_bound=0)}
entropy)[来源]
enumerate_supportexpand=True[来源]
expandbatch_shape_instance=[来源]
has_enumerate_support =
log_probvalue[来源]
Property Logits 属性
属性平均值
property mode
属性param_shape
属性问题
samplesample_shape=torch 的Size([])[来源]
Property 支持
属性差异

分类

torch.distributions.categorical。Categoricalprobs=logits=validate_args=[来源]

基地:

创建由 OR 参数化的分类分布(但不能同时创建两者)。

注意

它等效于从中采样的分布。

样本是来自{0,,K1}\{0, \ldots, K-1\}其中 K 是 。probs.size(-1)

如果 probs 是长度为 K 的一维,则每个元素都是相对概率 的 Sampling of the Class.

如果 probs 是 N 维的,则前 N-1 维将被视为一批 相对概率向量。

注意

probs 参数必须是非负的、有限的,并且具有非零和, 并且它将沿最后一个维度归一化为 sum 为 1。将返回此标准化值。 logits 参数将被解释为非规范化对数概率 ,因此可以是任何实数。它同样将被标准化,以便 结果概率沿最后一个维度总和 1。将返回此标准化值。

另请参阅:

例:

>>> m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
>>> m.sample()  # equal probability of 0, 1, 2, 3
tensor(3)
参数
  • probsTensor) - 事件概率

  • logitsTensor) – 事件对数概率 (未归一化)

arg_constraints = {'logits': IndependentConstraint(Real(), 1), 'probs': 单纯形()}
entropy)[来源]
enumerate_supportexpand=True[来源]
expandbatch_shape_instance=[来源]
has_enumerate_support =
log_probvalue[来源]
Property Logits 属性
属性平均值
property mode
属性param_shape
属性问题
samplesample_shape=torch 的Size([])[来源]
Property 支持
属性差异

柯 西

torch.distributions.cauchy 的Cauchylocscalevalidate_args=None[来源]

基地:

来自 Cauchy (Lorentz) 分布的样本。比率的分布 均值为 0 的独立正态分布随机变量遵循 a 柯西分布。

例:

>>> m = Cauchy(torch.tensor([0.0]), torch.tensor([1.0]))
>>> m.sample()  # sample from a Cauchy distribution with loc=0 and scale=1
tensor([ 2.3214])
参数
  • locfloat or Tensor) - 分布的众数或中位数。

  • scalefloat or Tensor) - 半宽在半最大值。

arg_constraints = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
icdf[来源]
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

支持 = Real()
属性差异

气2

torch.distributions.chi2 中。Chi2dfvalidate_args=[来源]

基地:

创建由 shape parameter 参数化的卡方分布。 这完全等同于Gamma(alpha=0.5*df, beta=0.5)

例:

>>> m = Chi2(torch.tensor([1.0]))
>>> m.sample()  # Chi2 distributed with shape df=1
tensor([ 0.1046])
参数

dffloat or Tensor) – 分布的形状参数

arg_constraints = {'df': 大于(lower_bound=0.0)}
属性 df
expandbatch_shape_instance=[来源]

连续式伯努利

torch.distributions.continuous_bernoulli 类连续伯努利probs=logits=lims=(0.499,0.501validate_args=[来源]

基地:

创建由 参数化的连续伯努利分布(但不能同时创建两者)。

该分布在 [0, 1] 中受支持,并由 'probs' 参数化(在 (0,1)) 或 'logits' (实值)。请注意,与伯努利不同,“probs” 不对应于概率,并且 'logits' 不对应于 log-odds,但由于与 伯努利。有关详细信息,请参见 [1]。

例:

>>> m = ContinuousBernoulli(torch.tensor([0.3]))
>>> m.sample()
tensor([ 0.2538])
参数
  • probsNumberTensor) – (0,1) 值参数

  • logitsNumberTensor) – sigmoid 与 'probs' 匹配的实值参数

[1] 连续伯努利:修复变分中的普遍误差 自动编码器,Loaiza-Ganem G 和 Cunningham JP,NeurIPS 2019。https://arxiv.org/abs/1907.06845

arg_constraints = {'logits': Real(), 'probs': 区间(lower_bound=0.0, upper_bound=1.0)}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
icdf[来源]
log_probvalue[来源]
Property Logits 属性
属性平均值
属性param_shape
属性问题
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

samplesample_shape=torch 的Size([])[来源]
属性 stddev
support = 区间(lower_bound=0.0, upper_bound=1.0)
属性差异

狄里克莱

torch.distributions.dirichlet 中。狄利克雷浓度validate_args=[来源]

基地:

创建按浓度参数化的狄利克雷分布。concentration

例:

>>> m = Dirichlet(torch.tensor([0.5, 0.5]))
>>> m.sample()  # Dirichlet distributed with concentration [0.5, 0.5]
tensor([ 0.1046,  0.8954])
参数

concentrationTensor) – 分布的浓度参数 (通常称为 Alpha)

arg_constraints = {'concentration': IndependentConstraint(GreaterThan(lower_bound=0.0), 1)}
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=()[来源]
返回类型

张肌

支持 = 单工()
属性差异

指数

torch.distributions.exponential 中。指数ratevalidate_args=None[来源]

基地:

创建一个参数化为 的指数分布。rate

例:

>>> m = Exponential(torch.tensor([1.0]))
>>> m.sample()  # Exponential distributed with rate=1
tensor([ 0.1046])
参数

ratefloatTensor) – rate = 1 / 分布规模

arg_constraints = {'rate': GreaterThan(lower_bound=0.0)}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
icdf[来源]
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

属性 stddev
支持 = GreaterThanEq(lower_bound=0.0)
属性差异

费舍尔斯内德科

torch.distributions.fishersnedecor 中。FisherSnedecordf1df2validate_args=[来源]

基地:

创建由 和 参数化的 Fisher-Snedecor 分布。df1df2

例:

>>> m = FisherSnedecor(torch.tensor([1.0]), torch.tensor([2.0]))
>>> m.sample()  # Fisher-Snedecor-distributed with df1=1 and df2=2
tensor([ 0.2453])
参数
arg_constraints = {'df1': GreaterThan(lower_bound=0.0), 'df2': GreaterThan(lower_bound=0.0)}
expandbatch_shape_instance=[来源]
has_rsample =
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

support = 大于 (lower_bound=0.0)
属性差异

伽马

torch.distributions.gamma 中。Gamma浓度速率validate_args=[来源]

基地:

创建由 shape 和 参数化的 Gamma 分布。concentrationrate

例:

>>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0]))
>>> m.sample()  # Gamma distributed with concentration=1 and rate=1
tensor([ 0.1046])
参数
  • concentrationfloatTensor) – 分布的形状参数 (通常称为 Alpha)

  • ratefloatTensor) – rate = 1 / 分布规模 (通常称为 beta)

arg_constraints = {'concentration': GreaterThan(lower_bound=0.0), 'rate': GreaterThan(lower_bound=0.0)}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

支持 = GreaterThanEq(lower_bound=0.0)
属性差异

几何

torch.distributions.geometric 的 Package。几何probs=logits=validate_args=[来源]

基地:

创建由 参数化的 Geometric 分布 。 其中 是伯努利试验成功的概率。

P(X=k)=(1p)kp,k=0,1,...P(X=k) = (1-p)^{k} p, k = 0, 1, ...

注意

torch.distributions.geometric.Geometric() (k+1)(k+1)-第 1 次试验是第一次成功 因此在{0,1,}\{0, 1, \ldots\},而k 次试验是第一次成功,因此会提取样本{1,2,}\{1, 2, \ldots\}.

例:

>>> m = Geometric(torch.tensor([0.3]))
>>> m.sample()  # underlying Bernoulli has 30% chance 1; 70% chance 0
tensor([ 2.])
参数
  • probsNumberTensor) - 采样 1 的概率。必须在 (0, 1] 范围内

  • logitsNumberTensor) - 采样 1 的对数几率。

arg_constraints = {'logits': Real(), 'probs': 区间(lower_bound=0.0, upper_bound=1.0)}
entropy)[来源]
expandbatch_shape_instance=[来源]
log_probvalue[来源]
Property Logits 属性
属性平均值
property mode
属性问题
samplesample_shape=torch 的Size([])[来源]
支持 = IntegerGreaterThan(lower_bound=0)
属性差异

甘贝

torch.distributions.gumbel 中。Gumbellocscalevalidate_args=None[来源]

基地:

来自 Gumbel 分布的样本。

例子:

>>> m = Gumbel(torch.tensor([1.0]), torch.tensor([2.0]))
>>> m.sample()  # sample from Gumbel distribution with loc=1, scale=2
tensor([ 1.0124])
参数
arg_constraints: dict[str constraint] = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}
entropy)[来源]
expandbatch_shape_instance=[来源]
log_probvalue[来源]
属性平均值
property mode
属性 stddev
支持 = Real()
属性差异

半柯西

torch.distributions.half_cauchy 类HalfCauchyscalevalidate_args=None[来源]

基地:

创建按尺度参数化的半柯西分布,其中:

X ~ Cauchy(0, scale)
Y = |X| ~ HalfCauchy(scale)

例:

>>> m = HalfCauchy(torch.tensor([1.0]))
>>> m.sample()  # half-cauchy distributed with scale=1
tensor([ 2.3214])
参数

scalefloatTensor) – 完整 Cauchy 分布的规模

arg_constraints: dict[str constraint] = {'scale': GreaterThan(lower_bound=0.0)}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
ICDFprob[来源]
log_probvalue[来源]
属性平均值
property mode
物业规模
支持 = GreaterThanEq(lower_bound=0.0)
属性差异

半法线

torch.distributions.half_normal 类HalfNormalscalevalidate_args=None[来源]

基地:

创建按尺度参数化的半正态分布,其中:

X ~ Normal(0, scale)
Y = |X| ~ HalfNormal(scale)

例:

>>> m = HalfNormal(torch.tensor([1.0]))
>>> m.sample()  # half-normal distributed with scale=1
tensor([ 0.1046])
参数

scalefloatTensor) – 完整 Normal 分布的尺度

arg_constraints: dict[str constraint] = {'scale': GreaterThan(lower_bound=0.0)}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
ICDFprob[来源]
log_probvalue[来源]
属性平均值
property mode
物业规模
支持 = GreaterThanEq(lower_bound=0.0)
属性差异

独立

torch.distributions.Independent 中。独立base_distributionreinterpreted_batch_ndimsvalidate_args=[来源]

基地:

将分布的某些批次维度重新解释为事件维度。

这主要用于更改 的结果的形状。例如,要创建对角线正态分布 的形状与多元正态分布相同(因此它们是 可互换),您可以:

>>> from torch.distributions.multivariate_normal import MultivariateNormal
>>> from torch.distributions.normal import 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])]
参数
arg_constraints: Dict[str constraint] = {}
entropy)[来源]
enumerate_supportexpand=True[来源]
expandbatch_shape_instance=[来源]
属性 has_enumerate_support
属性 has_rsample
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

samplesample_shape=torch 的Size([])[来源]
Property 支持
属性差异

逆伽玛

torch.distributions.inverse_gamma 类InverseGamma浓度速率validate_args=[来源]

基地:

创建参数化的逆 Gamma 分布,其中:

X ~ Gamma(concentration, rate)
Y = 1 / X ~ InverseGamma(concentration, rate)

例:

>>> m = InverseGamma(torch.tensor([2.0]), torch.tensor([3.0]))
>>> m.sample()
tensor([ 1.2953])
参数
  • concentrationfloatTensor) – 分布的形状参数 (通常称为 Alpha)

  • ratefloatTensor) – rate = 1 / 分布规模 (通常称为 beta)

arg_constraints: dict[str constraint] = {'concentration': GreaterThan(lower_bound=0.0), 'rate': GreaterThan(lower_bound=0.0)}
属性集中度
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
属性平均值
property mode
物业费率
support = 大于 (lower_bound=0.0)
属性差异

库马拉斯瓦米

torch.distributions.kumaraswamy 中。Kumaraswamy浓度1浓度0,validate_args=[来源]

基地:

来自 Kumaraswamy 分布的样本。

例:

>>> m = Kumaraswamy(torch.tensor([1.0]), torch.tensor([1.0]))
>>> m.sample()  # sample from a Kumaraswamy distribution with concentration alpha=1 and beta=1
tensor([ 0.1729])
参数
  • concentration1floatTensor) – 分布的第 1 个浓度参数 (通常称为 Alpha)

  • concentration0floatTensor) – 分布的第 2 个浓度参数 (通常称为 beta)

arg_constraints: dict[str constraint] = {'concentration0': GreaterThan(lower_bound=0.0), 'concentration1': GreaterThan(lower_bound=0.0)}
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
属性平均值
property mode
support = 区间(lower_bound=0.0, upper_bound=1.0)
属性差异

LKJCholesky

torch.distributions.lkj_cholesky 类LKJCholeskydimconcentration=1.0validate_args=None[来源]

基地:

相关矩阵的较低 Cholesky 因子的 LKJ 分布。 分布由参数控制concentrationη\eta制作相关矩阵的概率MM生成自 与Det(M)η1\det(M)^{\eta - 1}.正因为如此, 当 时,我们在 Cholesky 上呈均匀分布 相关矩阵的因子:concentration == 1

L ~ LKJCholesky(dim, concentration)
X = L @ L' ~ LKJCorr(dim, concentration)

请注意,此发行版对 相关矩阵的 Cholesky 因子,而不是相关矩阵 本身,因此与 [1] 中的推导略有不同,因为 LKJCorr 发行版。对于采样,这将使用 Onion 方法从 [1] 第 3 节。

例:

>>> l = LKJCholesky(3, 0.5)
>>> l.sample()  # l @ l.T is a sample of a correlation 3x3 matrix
tensor([[ 1.0000,  0.0000,  0.0000],
        [ 0.3516,  0.9361,  0.0000],
        [-0.1899,  0.4748,  0.8593]])
参数
  • dimensiondim) – 矩阵的维度

  • concentrationfloat or Tensor) – 浓度/形状参数 分发(通常称为 ETA)

引用

[1] 基于 vines 和扩展洋葱法生成随机相关矩阵 (2009), 丹尼尔·莱万多夫斯基、多罗塔·库罗维卡、哈里·乔。 多变量分析杂志。100. 10.1016/j.jmva.2009.04.008

arg_constraints = {'concentration': GreaterThan(lower_bound=0.0)}
expandbatch_shape_instance=[来源]
log_probvalue[来源]
samplesample_shape=torch 的Size([])[来源]
支持 = CorrCholesky()

拉普拉斯

torch.distributions.laplace 中。拉普拉斯locscalevalidate_args=None[来源]

基地:

创建由 和 参数化的拉普拉斯分布。locscale

例:

>>> m = Laplace(torch.tensor([0.0]), torch.tensor([1.0]))
>>> m.sample()  # Laplace distributed with loc=0, scale=1
tensor([ 0.1046])
参数
arg_constraints = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
icdf[来源]
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

属性 stddev
支持 = Real()
属性差异

对数

torch.distributions.log_normal 中。LogNormallocscalevalidate_args=None[来源]

基地:

创建参数化的对数正态分布,其中:

X ~ Normal(loc, scale)
Y = exp(X) ~ LogNormal(loc, scale)

例:

>>> m = LogNormal(torch.tensor([0.0]), torch.tensor([1.0]))
>>> m.sample()  # log-normal distributed with mean=0 and stddev=1
tensor([ 0.1046])
参数
  • locfloatTensor) – 分布对数的平均值

  • scalefloatTensor) – 分布对数的标准差

arg_constraints: dict[str constraint] = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
Property LOC属性位置)
属性平均值
property mode
物业规模
support = 大于 (lower_bound=0.0)
属性差异

LowRankMultivariateNormal

torch.distributions.lowrank_multivariate_normal 类LowRankMultivariateNormalloccov_factorcov_diagvalidate_args=None[来源]

基地:

创建协方差矩阵具有低秩形式的多元正态分布 参数化为 和 :cov_factorcov_diag

covariance_matrix = cov_factor @ cov_factor.T + cov_diag

>>> m = LowRankMultivariateNormal(torch.zeros(2), torch.tensor([[1.], [0.]]), torch.ones(2))
>>> m.sample()  # normally distributed with mean=`[0,0]`, cov_factor=`[[1],[0]]`, cov_diag=`[1,1]`
tensor([-0.2102, -0.5429])
参数
  • locTensor) – 形状为 batch_shape + event_shape 的分布平均值

  • cov_factorTensor) – 形状为 batch_shape + event_shape + (rank,) 的协方差矩阵的低秩形式的因子部分

  • cov_diagTensor) – 形状为 batch_shape + event_shape 的协方差矩阵的低秩形式的对角线部分

注意

由于 Woodbury 矩阵恒等式和矩阵行列式引理,当 cov_factor.shape[1] << cov_factor.shape[0] 时,避免了行列式和协方差矩阵逆矩阵的计算。 多亏了这些公式,我们只需要计算 小尺寸 “电容” 矩阵:

capacitance = I + cov_factor.T @ inv(cov_diag) @ cov_factor
arg_constraints = {'cov_diag': IndependentConstraint(GreaterThan(lower_bound=0.0), 1), 'cov_factor': IndependentConstraint(Real(), 2), 'loc': IndependentConstraint(Real(), 1)}
属性 covariance_matrix
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
log_probvalue[来源]
属性平均值
property mode
属性 precision_matrix
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

属性 scale_tril
支持 = IndependentConstraint(Real(), 1)
属性差异

MixtureSameFamily

torch.distributions.mixture_same_family 类MixtureSameFamilymixture_distributioncomponent_distributionvalidate_args=[来源]

基地:

MixtureSameFamily 分布实现一个(批次的)混合物 分布,其中所有组件都来自不同的参数化 相同的分发类型。它由一个分类 “选择分布” (超过 k 个分量) 和一个分量参数化 Distribution,即具有最右侧 Batch 形状的 Distribution (等于 [k]),它为每个(批次的)组件编制索引。

例子:

>>> # Construct Gaussian Mixture Model in 1D consisting of 5 equally
>>> # weighted normal distributions
>>> mix = D.Categorical(torch.ones(5,))
>>> comp = D.Normal(torch.randn(5,), torch.rand(5,))
>>> gmm = MixtureSameFamily(mix, comp)

>>> # Construct Gaussian Mixture Model in 2D consisting of 5 equally
>>> # weighted bivariate normal distributions
>>> mix = D.Categorical(torch.ones(5,))
>>> comp = D.Independent(D.Normal(
...          torch.randn(5,2), torch.rand(5,2)), 1)
>>> gmm = MixtureSameFamily(mix, comp)

>>> # Construct a batch of 3 Gaussian Mixture Models in 2D each
>>> # consisting of 5 random weighted bivariate normal distributions
>>> mix = D.Categorical(torch.rand(3,5))
>>> comp = D.Independent(D.Normal(
...         torch.randn(3,5,2), torch.rand(3,5,2)), 1)
>>> gmm = MixtureSameFamily(mix, comp)
参数
  • mixture_distributiontorch.distributions.Categorical-like 实例。管理选择零部件的概率。 类别数必须与最右侧的批次匹配 维度的 component_distribution。必须具有 标量batch_shape匹配batch_shape component_distribution.batch_shape[:-1]

  • component_distribution类似 torch.distributions.Distribution 实例。最右侧的批处理维度索引组件。

arg_constraints: Dict[str constraint] = {}
cdfx[来源]
属性 component_distribution
expandbatch_shape_instance=[来源]
has_rsample = False
log_probx[来源]
属性平均值
属性 mixture_distribution
samplesample_shape=torch 的Size([])[来源]
Property 支持
属性差异

多项式

torch.distributions.multinomial 中。多项式total_count=1probs=Nonelogits=Nonevalidate_args=None[来源]

基地:

创建参数化为 和 的多项式分布 要么 or (但不能同时) 。类别索引的最内层维度。所有其他维度对批次进行索引。

请注意,如果只有 is ,则无需指定 called(见下面的示例)

注意

probs 参数必须是非负的、有限的,并且具有非零和, 并且它将沿最后一个维度归一化为 sum 为 1。将返回此标准化值。 logits 参数将被解释为非规范化对数概率 ,因此可以是任何实数。它同样将被标准化,以便 结果概率沿最后一个维度总和 1。将返回此标准化值。

  • 需要所有人的单个共享total_count 参数和样本。

  • 允许每个参数使用不同的total_count,并且 样本。

例:

>>> m = Multinomial(100, torch.tensor([ 1., 1., 1., 1.]))
>>> x = m.sample()  # equal probability of 0, 1, 2, 3
tensor([ 21.,  24.,  30.,  25.])

>>> Multinomial(probs=torch.tensor([1., 1., 1., 1.])).log_prob(x)
tensor([-4.1338])
参数
  • total_countint) – 试验次数

  • probsTensor) - 事件概率

  • logitsTensor) – 事件对数概率 (未归一化)

arg_constraints = {'logits': IndependentConstraint(Real(), 1), 'probs': 单纯形()}
entropy)[来源]
expandbatch_shape_instance=[来源]
log_probvalue[来源]
Property Logits 属性
属性平均值
属性param_shape
属性问题
samplesample_shape=torch 的Size([])[来源]
Property 支持
total_count: int
属性差异

MultivariateNormal

torch.distributions.multivariate_normal 类MultivariateNormalloccovariance_matrix=precision_matrix=scale_tril=validate_args=没有[来源]

基地:

创建多元正态(也称为高斯)分布 由均值向量和协方差矩阵参数化。

多元正态分布可以参数化 就正定协方差矩阵而言Σ\mathbf{\Sigma}或正定精度矩阵Σ1\mathbf{\Sigma}^{-1}或下三角矩阵L\mathbf{L}为正值 对角线条目,使得Σ=LL\mathbf{\Sigma} = \mathbf{L}\mathbf{L}^\top.这个三角矩阵 可以通过例如协方差的 Cholesky 分解获得。

>>> m = MultivariateNormal(torch.zeros(2), torch.eye(2))
>>> m.sample()  # normally distributed with mean=`[0,0]` and covariance_matrix=`I`
tensor([-0.2102, -0.5429])
参数
  • locTensor) – 分布的平均值

  • covariance_matrixTensor) – 正定协方差矩阵

  • precision_matrixTensor) – 正定精度矩阵

  • scale_trilTensor) – 协方差的下三角因子,对角线为正值

注意

只能指定 or 中的一个。

使用会更有效率:所有计算都在内部进行 基于 。如果传递 or,则它仅用于计算 使用 Cholesky 分解的相应下三角矩阵。

arg_constraints = {'covariance_matrix': PositiveDefinite(), 'loc': IndependentConstraint(Real(), 1), 'precision_matrix': PositiveDefinite(), 'scale_tril': LowerCholesky()}
属性 covariance_matrix
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
log_probvalue[来源]
属性平均值
property mode
属性 precision_matrix
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

属性 scale_tril
支持 = IndependentConstraint(Real(), 1)
属性差异

负双项式

torch.distributions.negative_binomial 类NegativeBinomialtotal_countprobs=Nonelogits=Nonevalidate_args=None[来源]

基地:

创建负二项式分布,即分布 成功的独立和相同的伯努利试验的数量 在实现失败之前。概率 每个伯努利试验的成功率为 total_count

参数
  • total_countfloatTensor) – 负伯努利的非负数 Trials to Stop,尽管该发行版仍然对 Real 有效 值计数

  • probsTensor) - 半开区间 [0, 1] 中成功的事件概率

  • logitsTensor) - 成功概率的事件对数几率

arg_constraints = {'logits': Real(), 'probs': HalfOpenInterval(lower_bound=0.0, upper_bound=1.0), 'total_count': GreaterThanEq(lower_bound=0)}
expandbatch_shape_instance=[来源]
log_probvalue[来源]
Property Logits 属性
属性平均值
property mode
属性param_shape
属性问题
samplesample_shape=torch 的Size([])[来源]
支持 = IntegerGreaterThan(lower_bound=0)
属性差异

正常

torch.distributions.normal 中。正常locscalevalidate_args=None[来源]

基地:

创建由 和 参数化的正态(也称为高斯)分布。locscale

例:

>>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0]))
>>> m.sample()  # normally distributed with loc=0 and scale=1
tensor([ 0.1046])
参数
  • locfloatTensor) – 分布的平均值(通常称为 mu)

  • scalefloatTensor) – 分布的标准差 (通常称为 Sigma)

arg_constraints = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
icdf[来源]
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

samplesample_shape=torch 的Size([])[来源]
属性 stddev
支持 = Real()
属性差异

OneHotCategorical 餐厅

torch.distributions.one_hot_categorical 类OneHotCategoricalprobs=logits=validate_args=[来源]

基地:

创建由 参数化的 one-hot 分类分布。

样本是大小为 的 one-hot 编码向量。probs.size(-1)

注意

probs 参数必须是非负的、有限的,并且具有非零和, 并且它将沿最后一个维度归一化为 sum 为 1。将返回此标准化值。 logits 参数将被解释为非规范化对数概率 ,因此可以是任何实数。它同样将被标准化,以便 结果概率沿最后一个维度总和 1。将返回此标准化值。

另请参阅:有关 和 的规范。torch.distributions.Categorical()

例:

>>> m = OneHotCategorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
>>> m.sample()  # equal probability of 0, 1, 2, 3
tensor([ 0.,  0.,  0.,  1.])
参数
  • probsTensor) - 事件概率

  • logitsTensor) – 事件对数概率 (未归一化)

arg_constraints = {'logits': IndependentConstraint(Real(), 1), 'probs': 单纯形()}
entropy)[来源]
enumerate_supportexpand=True[来源]
expandbatch_shape_instance=[来源]
has_enumerate_support =
log_probvalue[来源]
Property Logits 属性
属性平均值
property mode
属性param_shape
属性问题
samplesample_shape=torch 的Size([])[来源]
支持 = OneHot()
属性差异

帕 累 托

torch.distributions.pareto 中。帕累托图(比例alphavalidate_args=[来源]

基地:

来自 Pareto Type 1 分布的样本。

例:

>>> m = Pareto(torch.tensor([1.0]), torch.tensor([1.0]))
>>> m.sample()  # sample from a Pareto distribution with scale=1 and alpha=1
tensor([ 1.5623])
参数
arg_constraints: dict[str constraint] = {'alpha': GreaterThan(lower_bound=0.0), 'scale': GreaterThan(lower_bound=0.0)}
entropy)[来源]
expandbatch_shape_instance=[来源]
属性平均值
property mode
Property 支持
属性差异

泊 松

torch.distributions.poisson 中。泊松ratevalidate_args=None[来源]

基地:

创建由 rate 参数参数化的泊松分布。rate

样本是非负整数,其中 pmf 由下式给出

r一个teker一个tek!\mathrm{rate}^k \frac{e^{-\mathrm{rate}}}{k!}

例:

>>> m = Poisson(torch.tensor([4]))
>>> m.sample()
tensor([ 3.])
参数

rateNumberTensor) – rate 参数

arg_constraints = {'rate': GreaterThanEq(lower_bound=0.0)}
expandbatch_shape_instance=[来源]
log_probvalue[来源]
属性平均值
property mode
samplesample_shape=torch 的Size([])[来源]
支持 = IntegerGreaterThan(lower_bound=0)
属性差异

松弛伯努利

torch.distributions.relaxed_bernoulli 类松弛伯努利温度probs=logits=validate_args=[来源]

基地:

创建一个 RelaxedBernoulli 分布,由 、 和 (但不能同时用两者) 参数化。这是伯努利分布的简化版本, 所以值在 (0, 1) 中,并且具有可重新参数化的样本。

例:

>>> m = RelaxedBernoulli(torch.tensor([2.2]),
...                      torch.tensor([0.1, 0.2, 0.3, 0.99]))
>>> m.sample()
tensor([ 0.2951,  0.3442,  0.8918,  0.9021])
参数
  • temperatureTensor) – 松弛温度

  • probsNumberTensor) – 采样概率 1

  • logitsNumberTensor) – 采样 1 的对数几率

arg_constraints: dict[str constraint] = {'logits': real(), 'probs': interval(lower_bound=0.0, upper_bound=1.0)}
expandbatch_shape_instance=[来源]
has_rsample =
Property Logits 属性
属性问题
support = 区间(lower_bound=0.0, upper_bound=1.0)
性状温度

LogitRelaxedBernoulli

torch.distributions.relaxed_bernoulli 类LogitRelaxedBernoulli温度probs=logits=validate_args=[来源]

基地:

创建由 (但不能同时由两者) 参数化的 LogitRelaxedBernoulli 分布,该分布是 RelaxedBernoulli 的 Logit 分配。

样本是 (0, 1) 中值的对数。有关详细信息,请参见 [1]。

参数
  • temperatureTensor) – 松弛温度

  • probsNumberTensor) – 采样概率 1

  • logitsNumberTensor) – 采样 1 的对数几率

[1] 具体分布:离散随机的连续松弛 变量(Maddison等人,2017 年)

[2] 使用 Gumbel-Softmax 进行分类参数化 (Jang等人,2017 年)

arg_constraints = {'logits': Real(), 'probs': 区间(lower_bound=0.0, upper_bound=1.0)}
expandbatch_shape_instance=[来源]
log_probvalue[来源]
Property Logits 属性
属性param_shape
属性问题
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

支持 = Real()

RelaxedOneHotCategorical

torch.distributions.relaxed_categorical 类RelaxedOneHotCategorical温度概率=logits=validate_args=[来源]

基地:

创建一个 RelaxedOneHotCategorical 分布,该分布由 、 和 或 参数化。 这是发行版的轻松版本,因此 它的样本是单纯形法,并且可以重新参数化。OneHotCategorical

例:

>>> m = RelaxedOneHotCategorical(torch.tensor([2.2]),
...                              torch.tensor([0.1, 0.2, 0.3, 0.4]))
>>> m.sample()
tensor([ 0.1294,  0.2324,  0.3859,  0.2523])
参数
  • temperatureTensor) – 松弛温度

  • probsTensor) - 事件概率

  • logitsTensor) – 每个事件的非标准化对数概率

arg_constraints: dict[str constraint] = {'logits': IndependentConstraint(Real(), 1), 'probs': Simplex()}
expandbatch_shape_instance=[来源]
has_rsample =
Property Logits 属性
属性问题
支持 = 单工()
性状温度

学生T

torch.distributions.studentT 的StudentTdfloc=0.0scale=1.0validate_args=None[来源]

基地:

创建按度数参数化的 Student t 分布 自由 、 中庸 和 规模 。dflocscale

例:

>>> m = StudentT(torch.tensor([2.0]))
>>> m.sample()  # Student's t-distributed with degrees of freedom=2
tensor([ 0.1046])
参数
arg_constraints = {'df': GreaterThan(lower_bound=0.0), 'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

支持 = Real()
属性差异

TransformedDistribution

torch.distributions.transformed_distribution 类TransformedDistributionbase_distribution转换validate_args=[来源]

基地:

Distribution 类的扩展,它应用一系列 Transform 添加到基本分配。设 f 为应用的转换的组合:

X ~ BaseDistribution
Y = f(X) ~ TransformedDistribution(BaseDistribution, f)
log p(Y) = log p(X) + log |det (dX/dY)|

请注意,a 的 是 其 base 分布及其转换的最大形状,因为 Transforms 可以引入事件之间的关联。.event_shape

的用法示例如下:

# Building a Logistic Distribution
# X ~ Uniform(0, 1)
# f = a + b * logit(X)
# Y ~ f(X) ~ Logistic(a, b)
base_distribution = Uniform(0, 1)
transforms = [SigmoidTransform().inv, AffineTransform(loc=a, scale=b)]
logistic = TransformedDistribution(base_distribution, transforms)

有关更多示例,请查看

arg_constraints: Dict[str constraint] = {}
cdf[来源]

通过反转 transform(s) 并计算碱基分布的分数。

expandbatch_shape_instance=[来源]
属性 has_rsample
icdf[来源]

计算逆累积分布函数 transform(s) 并计算碱基分布的分数。

log_probvalue[来源]

通过反转转换并计算分数来对样本进行评分 使用基本分布和对数 abs det jacobian 的分数。

rsamplesample_shape=Torch。Size([])[来源]

生成 sample_shape 形状的重新参数化样品或sample_shape 如果分布参数 进行批处理。首先从基本分布中采样,然后对列表中的每个转换应用 transform()。

返回类型

张肌

samplesample_shape=torch 的Size([])[来源]

生成 sample_shape 形样品或 sample_shape 形批次 samples (如果分布参数是批处理的)。样本 first from base 分布,并对 列表。

Property 支持

均匀

torch.distributions.uniform 的 Uniform 类。Uniformvalidate_args=[来源]

基地:

从半开区间 生成均匀分布的随机样本。[low, high)

例:

>>> m = Uniform(torch.tensor([0.0]), torch.tensor([5.0]))
>>> m.sample()  # uniformly distributed in the range [0.0, 5.0)
tensor([ 2.3418])
参数
  • lowfloatTensor) – 下限范围 (包括)。

  • highfloatTensor) – 上限 (不包括)。

arg_constraints = {'high': dependent(), 'low': dependent()}
cdf[来源]
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
icdf[来源]
log_probvalue[来源]
属性平均值
property mode
rsamplesample_shape=Torch。Size([])[来源]
返回类型

张肌

属性 stddev
Property 支持
属性差异

冯米塞斯

torch.distributions.von_mises 类VonMiseslocconcentrationvalidate_args=None[来源]

基地:

循环 von Mises 分布。

此实现使用极坐标。和 args 可以是任何实数(以便于不受约束的优化),但 解释为 angles modulo 2 pi。locvalue

例::
>>> m = VonMises(torch.tensor([1.0]), torch.tensor([1.0]))
>>> m.sample()  # von Mises distributed with loc=1 and concentration=1
tensor([1.9777])
参数
arg_constraints = {'concentration': GreaterThan(lower_bound=0.0), 'loc': Real()}
expandbatch_shape[来源]
has_rsample = False
log_probvalue[来源]
属性平均值

提供的平均值是循环平均值。

property mode
samplesample_shape=torch 的Size([])[来源]

von Mises 分布的抽样算法基于 以下论文:D.J. Best 和 N.I. Fisher,“高效模拟 von Mises 分布。应用统计学 (1979):152-157。

采样始终在内部以双精度完成,以避免挂起 in _rejection_sample() 表示浓度值较小,其中 对于 1e-4 左右的单精度开始发生(请参阅问题 #88443)。

支持 = Real()
属性差异

提供的差异是循环的差异。

威布尔

torch.distributions.weibull 的Weibull量表浓度validate_args=[来源]

基地:

来自双参数 Weibull 分布的样本。

>>> m = Weibull(torch.tensor([1.0]), torch.tensor([1.0]))
>>> m.sample()  # sample from a Weibull distribution with scale=1, concentration=1
tensor([ 0.4784])
参数
  • scalefloatTensor) – 分布的 scale 参数 (lambda)。

  • concentrationfloatTensor) - 分布的浓度参数 (k/shape)。

arg_constraints: dict[str constraint] = {'concentration': GreaterThan(lower_bound=0.0), 'scale': GreaterThan(lower_bound=0.0)}
entropy)[来源]
expandbatch_shape_instance=[来源]
属性平均值
property mode
support = 大于 (lower_bound=0.0)
属性差异

威沙特

torch.distributions.wishart 中。Wishartdfcovariance_matrix=precision_matrix=scale_tril=validate_args=无[来源]

基地:

创建由对称正定矩阵参数化的 Wishart 分布Σ\Sigma, 或其 Cholesky 分解Σ=LL\mathbf{\Sigma} = \mathbf{L}\mathbf{L}^\top

>>> m = Wishart(torch.Tensor([2]), covariance_matrix=torch.eye(2))
>>> m.sample()  # Wishart distributed with mean=`df * I` and
>>>             # variance(x_ij)=`df` for i != j and variance(x_ij)=`2 * df` for i == j
参数
  • dffloatTensor) – 大于 (平方矩阵的维度) - 1 的实值参数

  • covariance_matrixTensor) – 正定协方差矩阵

  • precision_matrixTensor) – 正定精度矩阵

  • scale_trilTensor) – 协方差的下三角因子,对角线为正值

注意

只能指定 or 中的一个。 使用会更有效率:所有计算都在内部进行 基于 。如果传递 or,则它仅用于计算 使用 Cholesky 分解的相应下三角矩阵。 'torch.distributions.LKJCholesky' 是受限制的 Wishart 发行版。[1]

引用

[1] Wang, Z., Wu, Y. 和 Chu, H.,2018 年。关于 LKJ 分销和受限 Wishart 分销的等效性。 [2] 索耶,S.,2007 年。Wishart 分布和逆 Wishart 采样。 [3] 安德森,TW,2003 年。多元统计分析简介(第 3 版)。 [4] Odell, P. L. & Feiveson, A. H.,1966年。用于生成 SampleCovariance 矩阵的数值过程。贾萨,61(313):199-203。 [5] 库,Y.-C.& Bloomfield, P., 2010.在 OX 中生成具有分数自由度的随机 Wishart 矩阵

arg_constraints = {'covariance_matrix': PositiveDefinite(), 'df': GreaterThan(lower_bound=0), 'precision_matrix': PositiveDefinite(), 'scale_tril': LowerCholesky()}
属性 covariance_matrix
entropy)[来源]
expandbatch_shape_instance=[来源]
has_rsample =
log_probvalue[来源]
属性平均值
property mode
属性 precision_matrix
rsamplesample_shape=Torch。Size([])max_try_correction=[来源]

警告

在某些情况下,基于 Bartlett 分解的采样算法可能会返回奇异矩阵样本。 默认情况下,会执行多次尝试更正奇异样本,但最终可能会返回 奇异矩阵样本。单个样本可能会在 .log_prob() 中返回 -inf 值。 在这些情况下,用户应验证样本,并相应地修复 df 的值或调整 .rsample 中参数max_try_correction值。

返回类型

张肌

属性 scale_tril
支持 = PositiveDefinite()
属性差异

KL Divergence

torch.distributions.kl 中。kl_divergencepq[来源]

Compute Kullback-Leibler divergenceKL(pq)KL(p \| q)在两个分配之间。

KL(pq)=p(x)日志p(x)q(x)dxKL(p \| q) = \int p(x) \log\frac {p(x)} {q(x)} \,dx
参数
  • pDistribution) (分配) – 对象。Distribution

  • qDistribution) (分配) – 对象。Distribution

返回

形状为 batch_shape 的一批 KL 发散。

返回类型

张肌

提高

NotImplementedError – 如果尚未通过 .

KL 分歧目前对以下通讯对实施:
  • BernoulliBernoulli

  • BernoulliPoisson

  • BetaBeta

  • BetaContinuousBernoulli

  • BetaExponential

  • BetaGamma

  • BetaNormal

  • BetaPareto

  • BetaUniform

  • BinomialBinomial

  • CategoricalCategorical

  • CauchyCauchy

  • ContinuousBernoulliContinuousBernoulli

  • ContinuousBernoulliExponential

  • ContinuousBernoulliNormal

  • ContinuousBernoulliPareto

  • ContinuousBernoulliUniform

  • DirichletDirichlet

  • ExponentialBeta

  • ExponentialContinuousBernoulli

  • ExponentialExponential

  • ExponentialGamma

  • ExponentialGumbel

  • ExponentialNormal

  • ExponentialPareto

  • ExponentialUniform

  • ExponentialFamilyExponentialFamily

  • GammaBeta

  • GammaContinuousBernoulli

  • GammaExponential

  • GammaGamma

  • GammaGumbel

  • GammaNormal

  • GammaPareto

  • GammaUniform

  • GeometricGeometric

  • GumbelBeta

  • GumbelContinuousBernoulli

  • GumbelExponential

  • GumbelGamma

  • GumbelGumbel

  • GumbelNormal

  • GumbelPareto

  • GumbelUniform

  • HalfNormalHalfNormal

  • IndependentIndependent

  • LaplaceBeta

  • LaplaceContinuousBernoulli

  • LaplaceExponential

  • LaplaceGamma

  • LaplaceLaplace

  • LaplaceNormal

  • LaplacePareto

  • LaplaceUniform

  • LowRankMultivariateNormalLowRankMultivariateNormal

  • LowRankMultivariateNormalMultivariateNormal

  • MultivariateNormalLowRankMultivariateNormal

  • MultivariateNormalMultivariateNormal

  • NormalBeta

  • NormalContinuousBernoulli

  • NormalExponential

  • NormalGamma

  • NormalGumbel

  • NormalLaplace

  • NormalNormal

  • NormalPareto

  • NormalUniform

  • OneHotCategoricalOneHotCategorical

  • ParetoBeta

  • ParetoContinuousBernoulli

  • ParetoExponential

  • ParetoGamma

  • ParetoNormal

  • ParetoPareto

  • ParetoUniform

  • PoissonBernoulli

  • PoissonBinomial

  • PoissonPoisson

  • TransformedDistributionTransformedDistribution

  • UniformBeta

  • UniformContinuousBernoulli

  • UniformExponential

  • UniformGamma

  • UniformGumbel

  • UniformNormal

  • UniformPareto

  • UniformUniform

torch.distributions.kl 中。register_kltype_ptype_q[来源]

Decorator 向 . 用法:

@register_kl(Normal, Normal)
def kl_normal_normal(p, q):
    # insert implementation here

Lookup 返回按子类排序的最具体的 (type,type) 匹配项。如果 匹配项不明确,则会引发 RuntimeWarning。例如,将 解决模棱两可的情况:

@register_kl(BaseP, DerivedQ)
def kl_version1(p, q): ...
@register_kl(DerivedP, BaseQ)
def kl_version2(p, q): ...

您应该注册第三个最具体的 implementation,例如:

register_kl(DerivedP, DerivedQ)(kl_version1)  # Break the tie.
参数
  • type_ptype) – 的子类。Distribution

  • type_qtype) – 的子类。Distribution

变换

torch.distributions.transforms。AbsTransformcache_size=0[来源]

通过映射进行变换y=xy = |x|.

torch.distributions.transforms。AffineTransformlocscaleevent_dim=0cache_size=0[来源]

通过逐点仿射映射进行变换y=LOC (本地)+规模×xy = \text{loc} + \text{scale} \times x.

参数
  • locTensor or float) - 位置参数。

  • scaleTensor or float) - Scale 参数。

  • event_dimint) – event_shape的可选大小。此值应为零 对于单变量随机变量,1 表示向量上的分布, 2 表示矩阵上的分布,等等。

torch.distributions.transforms。CatTransformtseqdim=0lengths=Nonecache_size=0[来源]

Transform functor,它按组件将一系列转换 tseq 应用于 dim 处的每个子矩阵,长度为 lengths[dim], 以兼容的方式。

例:

x0 = torch.cat([torch.range(1, 10), torch.range(1, 10)], dim=0)
x = torch.cat([x0, x0], dim=0)
t0 = CatTransform([ExpTransform(), identity_transform], dim=0, lengths=[10, 10])
t = CatTransform([t0, t0], dim=0, lengths=[20, 20])
y = t(x)
torch.distributions.transforms。ComposeTransformpartscache_size=0[来源]

在一个链中组合多个转换。 正在组合的转换负责缓存。

参数
  • parts (list of ) (要组合的转换列表)。

  • cache_sizeint) – 缓存的大小。如果为零,则不执行缓存。如果 1 个,则 缓存最新的单个值。仅支持 0 和 1。

torch.distributions.transforms。CorrCholeskyTransformcache_size=0[来源]

变换一个未约束的实向量xx带 lengthD(D1)/2D*(D-1)/2到 D 维相关矩阵的 Cholesky 因子。这个 Cholesky 因子较低 具有正对角线的三角形矩阵,每行的单位为欧几里得范数。 转换的处理方式如下:

  1. 首先,我们将 x 按行顺序转换为较低的三角矩阵。

  2. 对于每行XX_i的下部三角形部分,我们应用 要转换的类XX_i转换为 单位欧几里得长度向量,使用以下步骤: - 缩放到间隔(1,1)(-1, 1)域:r=(X)r_i = \tanh(X_i). - 转换为未签名的域:z=r2z_i = r_i^2. -适用s=StckBre一个kngTr一个nsform(z)s_i = StickBreakingTransform(z_i). - 转换回签名域:y=sgn(r)sy_i = sign(r_i) * \sqrt{s_i}.

torch.distributions.transforms。CumulativeDistributionTransformdistributioncache_size=0[来源]

通过概率分布的累积分布函数进行变换。

参数

distributionDistribution) – 要用于其累积分布函数的分布 转变。

例:

# Construct a Gaussian copula from a multivariate normal.
base_dist = MultivariateNormal(
    loc=torch.zeros(2),
    scale_tril=LKJCholesky(2).sample(),
)
transform = CumulativeDistributionTransform(Normal(0, 1))
copula = TransformedDistribution(base_dist, [transform])
torch.distributions.transforms。ExpTransformcache_size=0[来源]

通过映射进行变换y=exp (英文)(x)y = \exp(x).

torch.distributions.transforms。IndependentTransformbase_transformreinterpreted_batch_ndimscache_size=0[来源]

包装另一个转换,将 -many extra 的右侧最维度视为 依靠。这对向前或向后转换没有影响,但 求和 - 许多最右边的维度 在。reinterpreted_batch_ndimsreinterpreted_batch_ndimslog_abs_det_jacobian()

参数
  • base_transform) – 基本转换。

  • reinterpreted_batch_ndimsint) – 最右边的额外数量 要视为从属的维度。

torch.distributions.transforms。LowerCholeskyTransformcache_size=0[来源]

从无约束矩阵变换为下三角矩阵 非负对角线条目。

这对于根据 他们的 Cholesky 因式分解。

torch.distributions.transforms。PositiveDefiniteTransformcache_size=0[来源]

从无约束矩阵变换为正定矩阵。

torch.distributions.transforms。PowerTransform指数cache_size=0[来源]

通过映射进行变换y=x指数y = x^{\text{exponent}}.

torch.distributions.transforms。ReshapeTransformin_shapeout_shapecache_size=0[来源]

Unit Jacobian 变换来重塑张量的最右侧部分。

请注意,和 必须具有相同的 元素,就像 .in_shapeout_shape

参数
torch.distributions.transforms。SigmoidTransformcache_size=0[来源]

通过映射进行变换y=11+exp (英文)(x)y = \frac{1}{1 + \exp(-x)}x=罗吉特(y)x = \text{logit}(y).

torch.distributions.transforms。SoftplusTransformcache_size=0[来源]

通过映射进行变换软加(x)=日志(1+exp (英文)(x))\text{Softplus}(x) = \log(1 + \exp(x)). 当x>20x > 20.

torch.distributions.transforms。TanhTransformcache_size=0[来源]

通过映射进行变换y=(x)y = \tanh(x).

它等效于但是,这可能在数值上不稳定,因此建议使用 TanhTransform` ComposeTransform([AffineTransform(0., 2.), SigmoidTransform(), AffineTransform(-1., 2.)]) `

请注意,当涉及到 NaN/Inf 值时,应使用 cache_size=1

torch.distributions.transforms。SoftmaxTransformcache_size=0[来源]

通过从无约束空间变换为单纯形y=exp (英文)(x)y = \exp(x)然后 正火。

这不是双射的,不能用于 HMC。然而,这主要起作用 坐标方向(最终归一化除外),因此 适用于坐标优化算法。

torch.distributions.transforms。StackTransformtseqdim=0cache_size=0[来源]

Transform functor,它以兼容的方式将一系列转换 tseq 按组件应用于 dim 处的每个子矩阵。

例:

x = torch.stack([torch.range(1, 10), torch.range(1, 10)], dim=1)
t = StackTransform([ExpTransform(), identity_transform], dim=1)
y = t(x)
torch.distributions.transforms。StickBreakingTransformcache_size=0[来源]

从不受约束的空间变换到一个附加空间的单纯形 尺寸。

此转换作为折杆中的迭代 sigmoid 转换出现 狄利克雷分布的构造:第一个 logit 是 通过 sigmoid 转换为第一个概率和 其他所有内容,然后进程递归。

这是双射的,适合在 HMC 中使用;然而,它混合在一起 坐标在一起,不太适合进行优化。

torch.distributions.transforms。Transformcache_size=0[来源]

具有可计算对数的可逆转换的抽象类 雅各布人。它们主要用于 。torch.distributions.TransformedDistribution

缓存对于其 inverse 为 cost 或 数值不稳定。请注意,必须注意记忆值 因为 autograd 图表可能是相反的。例如,虽然以下 使用或不使用缓存:

y = t(x)
t.log_abs_det_jacobian(x, y).backward()  # x will receive gradients.

但是,由于依赖关系反转,在缓存时将出现以下错误:

y = t(x)
z = t.inv(y)
grad(z.sum(), [y])  # error because z is x

派生类应实现 或 中的一个或两个。设置 bijective=True 的派生类也应 实施 ._call()_inverse()

参数

cache_sizeint) – 缓存的大小。如果为零,则不执行缓存。如果 1 个,则 缓存最新的单个值。仅支持 0 和 1。

变量
  • domain) – 表示此转换的有效输入的约束。

  • codomain) – 表示此转换的有效输出的约束 它们是逆变换的输入。

  • bijectivebool) - 此变换是否为双射变换。转换是双射的 iff 和 for each in the domain 和 in 共域。非 bijective 的 transform 至少应 保持较弱的伪逆性质 和 。tt.inv(t(x)) == xt(t.inv(y)) == yxyt(t.inv(t(x)) == t(x)t.inv(t(t.inv(y))) == t.inv(y)

  • signint or Tensor) – 对于双射单变量变换,此 应为 +1 或 -1,具体取决于 transform 是否为单调 增加或减少。

Property Inv 属性

返回此转换的逆函数。 这应该满足 。t.inv.inv is t

属性标志

返回雅可比行列式的行列式的符号(如果适用)。 通常,这仅对 bijective 转换有意义。

log_abs_det_jacobianxy[来源]

计算给定输入和输出的 log det jacobian log dy/dx。

forward_shape形状[来源]

在给定输入形状的情况下,推断前向计算的形状。 默认为保留形状。

inverse_shape形状[来源]

在给定输出形状的情况下,推断逆计算的形状。 默认为保留形状。

约束

实施了以下约束:

  • constraints.boolean

  • constraints.cat

  • constraints.corr_cholesky

  • constraints.dependent

  • constraints.greater_than(lower_bound)

  • constraints.greater_than_eq(lower_bound)

  • constraints.independent(constraint, reinterpreted_batch_ndims)

  • constraints.integer_interval(lower_bound, upper_bound)

  • constraints.interval(lower_bound, upper_bound)

  • constraints.less_than(upper_bound)

  • constraints.lower_cholesky

  • constraints.lower_triangular

  • constraints.multinomial

  • constraints.nonnegative

  • constraints.nonnegative_integer

  • constraints.one_hot

  • constraints.positive_integer

  • constraints.positive

  • constraints.positive_semidefinite

  • constraints.positive_definite

  • constraints.real_vector

  • constraints.real

  • constraints.simplex

  • constraints.symmetric

  • constraints.stack

  • constraints.square

  • constraints.symmetric

  • constraints.unit_interval

torch.distributions.constraints。约束[source]

约束的抽象基类。

constraint 对象表示变量有效的区域, 例如,可以在其中优化变量。

变量
  • is_discretebool) - 约束空间是否为离散空间。 默认为 False。

  • event_dimint) – 共同定义的最右侧的维度数 一个事件。该方法将删除这么多维度 计算有效性时。

checkvalue[来源]

返回表示 value 中的每个事件是否满足此约束。sample_shape + batch_shape

torch.distributions.constraints中。

别名为_Cat

torch.distributions.constraints中。dependent_property

别名为_DependentProperty

torch.distributions.constraints中。greater_than

别名为_GreaterThan

torch.distributions.constraints中。greater_than_eq

别名为_GreaterThanEq

torch.distributions.constraints中。独立

别名为_IndependentConstraint

torch.distributions.constraints中。integer_interval

别名为_IntegerInterval

torch.distributions.constraints中。间隔

别名为_Interval

torch.distributions.constraints中。half_open_interval

别名为_HalfOpenInterval

torch.distributions.constraints中。is_dependent约束[来源]

检查 是否为 对象。constraint_Dependent

参数

constraint – 对象。Constraint

返回

如果可以细化为类型,则为 True,否则为 False。constraint_Dependent

返回类型

bool

例子

>>> import torch
>>> from torch.distributions import Bernoulli
>>> from torch.distributions.constraints import is_dependent
>>> dist = Bernoulli(probs = torch.tensor([0.6], requires_grad=True))
>>> constraint1 = dist.arg_constraints["probs"]
>>> constraint2 = dist.arg_constraints["logits"]
>>> for constraint in [constraint1, constraint2]:
>>>     if is_dependent(constraint):
>>>         continue
torch.distributions.constraints中。less_than

别名为_LessThan

torch.distributions.constraints中。多项式

别名为_Multinomial

torch.distributions.constraints中。

别名为_Stack

约束注册表

PyTorch 提供了两个全局对象,用于将对象链接到对象。这些对象都 input constraints 和 return 转换,但它们对 双射度。

  1. biject_to(constraint)查找 bijective from 到 given .返回的转换保证具有并且应该实现 。constraints.realconstraint.bijective = True.log_abs_det_jacobian()

  2. transform_to(constraint)查找一个不一定的双射 from 到给定的 。返回的转换不能保证 实现。constraints.realconstraint.log_abs_det_jacobian()

注册表可用于执行 unconstrained 对概率分布的约束参数进行优化,这些参数是 由每个分配的 dict 指示。这些转换通常 过度参数化 space 以避免旋转;因此,他们是 适用于像 Adam 这样的坐标优化算法:transform_to().arg_constraints

loc = torch.zeros(100, requires_grad=True)
unconstrained = torch.zeros(100, requires_grad=True)
scale = transform_to(Normal.arg_constraints['scale'])(unconstrained)
loss = -Normal(loc, scale).log_prob(data).sum()

注册表对于哈密顿蒙特卡洛很有用,其中 来自具有约束的概率分布的样本为 在不受约束的空间中传播,算法通常是旋转 不变量。:biject_to().support

dist = Exponential(rate)
unconstrained = torch.zeros(100, requires_grad=True)
sample = biject_to(dist.support)(unconstrained)
potential_energy = -dist.log_prob(sample).sum()

注意

其中 和 differ 的示例是 : 返回一个 对其输入进行指数化和归一化;这是一个便宜且大部分 适合 SVI 等算法的坐标运算。在 contrast 的 Sample,返回一个 将其输入向下喷射到少一维的空间;这个更多 成本高昂、数值稳定性较低但算法需要的变换 就像 HMC 一样。transform_tobiject_toconstraints.simplextransform_to(constraints.simplex)biject_to(constraints.simplex)

和 对象可以通过用户定义的 constraints 和 transform,使用它们的方法作为 函数 on singleton constraints:biject_totransform_to.register()

transform_to.register(my_constraint, my_transform)

或作为参数化约束的装饰器:

@transform_to.register(MyConstraintClass)
def my_factory(constraint):
    assert isinstance(constraint, MyConstraintClass)
    return MyTransform(constraint.param1, constraint.param2)

您可以通过创建新对象来创建自己的注册表。

torch.distributions.constraint_registry 类ConstraintRegistry[来源]

Registry 将约束链接到转换。

registerconstraintfactory=None[来源]

在此注册表中注册一个子类。用法:

@my_registry.register(MyConstraintClass)
def construct_transform(constraint):
    assert isinstance(constraint, MyConstraint)
    return MyTransform(constraint.arg_constraints)
参数
  • constraint (subclass of ) – 或 的子类 所需类的 Singleton 对象。

  • factoryCallable) – 一个可调用对象,它输入一个约束对象并返回 一个对象。

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源