# 概率分布 - torch.distributions

distributions 包含可参数化的概率分布和采样函数. 这允许构造用于优化的随机计算图和随机梯度估计器. 这个包一般遵循 TensorFlow Distributions 包的设计.

## 得分函数

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 derivative

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()



## 分布

class torch.distributions.distribution.Distribution(batch_shape=torch.Size([]), event_shape=torch.Size([]), validate_args=None)


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

arg_constraints


batch_shape


cdf(value)


| 参数: | value (Tensor) – |

entropy()


| 返回值: | Tensor 形状为 batch_shape. |

enumerate_support(expand=True)


| 参数: | expand (bool) – 是否扩展对批处理dim的支持以匹配分布的 batch_shape. |

| 返回值: | 张量在维上0迭代. |

event_shape


expand(batch_shape, _instance=None)


• batch_shape (torch.Size) – 所需的扩展尺寸.
• _instance – 由需要重写.expand的子类提供的新实例.

| 返回值: | 批处理维度扩展为batch_size的新分布实例. |

icdf(value)


| 参数: | value (Tensor) – |

log_prob(value)


| 参数: | value (Tensor) – |

mean


perplexity()


| 返回值: | 形状为 batch_shape 的张量. |

rsample(sample_shape=torch.Size([]))


sample(sample_shape=torch.Size([]))


sample_n(n)


stddev


support


variance


## ExponentialFamily

class torch.distributions.exp_family.ExponentialFamily(batch_shape=torch.Size([]), event_shape=torch.Size([]), validate_args=None)


entropy()


## Bernoulli

class torch.distributions.bernoulli.Bernoulli(probs=None, logits=None, validate_args=None)


>>> m = Bernoulli(torch.tensor([0.3]))
>>> m.sample()  # 30% chance 1; 70% chance 0
tensor([ 0.])



• probs (Number_,_ Tensor) – the probabilty of sampling 1
• logits (Number_,_ Tensor) – the log-odds of sampling 1
arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}

entropy()

enumerate_support(expand=True)

expand(batch_shape, _instance=None)

has_enumerate_support = True

log_prob(value)

logits

mean

param_shape

probs

sample(sample_shape=torch.Size([]))

support = Boolean()

variance


## Beta

class torch.distributions.beta.Beta(concentration1, concentration0, validate_args=None)


Beta 分布, 参数为 concentration1concentration0.

>>> m = Beta(torch.tensor([0.5]), torch.tensor([0.5]))
>>> m.sample()  # Beta distributed with concentration concentration1 and concentration0
tensor([ 0.1046])



• concentration1 (float or Tensor) – 分布的第一个浓度参数（通常称为alpha）
• concentration0 (float or Tensor) – 分布的第二个浓度参数(通常称为beta)
arg_constraints = {'concentration0': GreaterThan(lower_bound=0.0), 'concentration1': GreaterThan(lower_bound=0.0)}

concentration0

concentration1

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

log_prob(value)

mean

rsample(sample_shape=())

support = Interval(lower_bound=0.0, upper_bound=1.0)

variance


## Binomial

class torch.distributions.binomial.Binomial(total_count=1, probs=None, logits=None, validate_args=None)


>>> 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_count (int or Tensor) – 伯努利试验次数
• probs (Tensor) – 事件概率
• logits (Tensor) – 事件 log-odds
arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0), 'total_count': IntegerGreaterThan(lower_bound=0)}

enumerate_support(expand=True)

expand(batch_shape, _instance=None)

has_enumerate_support = True

log_prob(value)

logits

mean

param_shape

probs

sample(sample_shape=torch.Size([]))

support

variance


## Categorical

class torch.distributions.categorical.Categorical(probs=None, logits=None, validate_args=None)


probs 必须是非负的、有限的并且具有非零和, 并且它将被归一化为和为1.

>>> m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
>>> m.sample()  # equal probability of 0, 1, 2, 3
tensor(3)



• probs (Tensor) – event probabilities
• logits (Tensor) – event log probabilities
arg_constraints = {'logits': Real(), 'probs': Simplex()}

entropy()

enumerate_support(expand=True)

expand(batch_shape, _instance=None)

has_enumerate_support = True

log_prob(value)

logits

mean

param_shape

probs

sample(sample_shape=torch.Size([]))

support

variance


## Cauchy

class torch.distributions.cauchy.Cauchy(loc, scale, validate_args=None)


>>> 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])



arg_constraints = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}

cdf(value)

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

icdf(value)

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

support = Real()

variance


## Chi2

class torch.distributions.chi2.Chi2(df, validate_args=None)


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



| 参数: | df (float or Tensor) – 分布的形状参数 |

arg_constraints = {'df': GreaterThan(lower_bound=0.0)}

df

expand(batch_shape, _instance=None)


## Dirichlet

class torch.distributions.dirichlet.Dirichlet(concentration, validate_args=None)


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



| 参数: | concentration (Tensor) – 分布的浓度参数（通常称为alpha） |

arg_constraints = {'concentration': GreaterThan(lower_bound=0.0)}

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

log_prob(value)

mean

rsample(sample_shape=())

support = Simplex()

variance


## Exponential

class torch.distributions.exponential.Exponential(rate, validate_args=None)


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



| 参数: | rate (float or Tensor) – rate = 1 / 分布的scale |

arg_constraints = {'rate': GreaterThan(lower_bound=0.0)}

cdf(value)

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

icdf(value)

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

stddev

support = GreaterThan(lower_bound=0.0)

variance


## FisherSnedecor

class torch.distributions.fishersnedecor.FisherSnedecor(df1, df2, validate_args=None)


>>> 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)}

expand(batch_shape, _instance=None)

has_rsample = True

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

support = GreaterThan(lower_bound=0.0)

variance


## Gamma

class torch.distributions.gamma.Gamma(concentration, rate, validate_args=None)


>>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0]))
>>> m.sample()  # Gamma distributed with concentration=1 and rate=1
tensor([ 0.1046])



• concentration (float or Tensor) – 分布的形状参数（通常称为alpha）
• rate (float or Tensor) – rate = 1 / 分布scale (通常称为beta )
arg_constraints = {'concentration': GreaterThan(lower_bound=0.0), 'rate': GreaterThan(lower_bound=0.0)}

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

support = GreaterThan(lower_bound=0.0)

variance


## Geometric

class torch.distributions.geometric.Geometric(probs=None, logits=None, validate_args=None)


>>> m = Geometric(torch.tensor([0.3]))
>>> m.sample()  # underlying Bernoulli has 30% chance 1; 70% chance 0
tensor([ 2.])



• probs (Number_,_ Tensor) – 抽样1的概率 . 必须是在范围 (0, 1]
• logits (Number_,_ Tensor) – 抽样 1的log-odds.
arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}

entropy()

expand(batch_shape, _instance=None)

log_prob(value)

logits

mean

probs

sample(sample_shape=torch.Size([]))

support = IntegerGreaterThan(lower_bound=0)

variance


## Gumbel

class torch.distributions.gumbel.Gumbel(loc, scale, validate_args=None)


Examples:

>>> 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 = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}

entropy()

expand(batch_shape, _instance=None)

mean

stddev

support = Real()

variance


## HalfCauchy

class torch.distributions.half_cauchy.HalfCauchy(scale, validate_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])



| 参数: | scale (float or Tensor) – 完全柯西分布的scale |

arg_constraints = {'scale': GreaterThan(lower_bound=0.0)}

cdf(value)

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

icdf(prob)

log_prob(value)

mean

scale

support = GreaterThan(lower_bound=0.0)

variance


## HalfNormal

class torch.distributions.half_normal.HalfNormal(scale, validate_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])



| 参数: | scale (float or Tensor) – 完全正态分布的scale |

arg_constraints = {'scale': GreaterThan(lower_bound=0.0)}

cdf(value)

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

icdf(prob)

log_prob(value)

mean

scale

support = GreaterThan(lower_bound=0.0)

variance


## Independent

class torch.distributions.independent.Independent(base_distribution, reinterpreted_batch_ndims, validate_args=None)


>>> 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 = {}

entropy()

enumerate_support(expand=True)

expand(batch_shape, _instance=None)

has_enumerate_support

has_rsample

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

sample(sample_shape=torch.Size([]))

support

variance


## Laplace

class torch.distributions.laplace.Laplace(loc, scale, validate_args=None)


>>> 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(value)

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

icdf(value)

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

stddev

support = Real()

variance


## LogNormal

class torch.distributions.log_normal.LogNormal(loc, scale, validate_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])



arg_constraints = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

loc

mean

scale

support = GreaterThan(lower_bound=0.0)

variance


## LowRankMultivariateNormal

class torch.distributions.lowrank_multivariate_normal.LowRankMultivariateNormal(loc, cov_factor, cov_diag, validate_args=None)


covariance_matrix = cov_factor @ cov_factor.T + cov_diag



Example

>>> m = LowRankMultivariateNormal(torch.zeros(2), torch.tensor([1, 0]), torch.tensor([1, 1]))
>>> m.sample()  # normally distributed with mean=[0,0], cov_factor=[1,0], cov_diag=[1,1]
tensor([-0.2102, -0.5429])



• loc (Tensor) – 分布的均值, 形状为 batch_shape + event_shape
• cov_factor (Tensor) – 协方差矩阵低秩形式的因子部分, 形状为 batch_shape + event_shape + (rank,)
• cov_diag (Tensor) – 协方差矩阵的低秩形式的对角部分, 形状为 batch_shape + event_shape

capacitance = I + cov_factor.T @ inv(cov_diag) @ cov_factor


arg_constraints = {'cov_diag': GreaterThan(lower_bound=0.0), 'cov_factor': Real(), 'loc': Real()}

covariance_matrix

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

log_prob(value)

mean

precision_matrix

rsample(sample_shape=torch.Size([]))

scale_tril

support = Real()

variance


## Multinomial

class torch.distributions.multinomial.Multinomial(total_count=1, probs=None, logits=None, validate_args=None)


probs 必须是非负的、有限的并且具有非零和, 并且它将被归一化为和为1.

>>> 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_count (int) – 试验次数
• probs (Tensor) – 事件概率
• logits (Tensor) – 事件对数概率
arg_constraints = {'logits': Real(), 'probs': Simplex()}

expand(batch_shape, _instance=None)

log_prob(value)

logits

mean

param_shape

probs

sample(sample_shape=torch.Size([]))

support

variance


## MultivariateNormal

class torch.distributions.multivariate_normal.MultivariateNormal(loc, covariance_matrix=None, precision_matrix=None, scale_tril=None, validate_args=None)


>>> 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])



• loc (Tensor) – 分布的均值
• covariance_matrix (Tensor) – 正定协方差矩阵
• precision_matrix (Tensor) – 正定精度矩阵
• scale_tril (Tensor) – 具有正值对角线的下三角协方差因子

arg_constraints = {'covariance_matrix': PositiveDefinite(), 'loc': RealVector(), 'precision_matrix': PositiveDefinite(), 'scale_tril': LowerCholesky()}

covariance_matrix

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

log_prob(value)

mean

precision_matrix

rsample(sample_shape=torch.Size([]))

scale_tril

support = Real()

variance


## NegativeBinomial

class torch.distributions.negative_binomial.NegativeBinomial(total_count, probs=None, logits=None, validate_args=None)


• total_count (float or Tensor) – 非负数伯努利试验停止的次数, 虽然分布仍然对实数有效
• probs (Tensor) – 事件概率, 区间为 [0, 1)
• logits (Tensor) – 事件对数几率 - 成功概率的几率
arg_constraints = {'logits': Real(), 'probs': HalfOpenInterval(lower_bound=0.0, upper_bound=1.0), 'total_count': GreaterThanEq(lower_bound=0)}

expand(batch_shape, _instance=None)

log_prob(value)

logits

mean

param_shape

probs

sample(sample_shape=torch.Size([]))

support = IntegerGreaterThan(lower_bound=0)

variance


## Normal

class torch.distributions.normal.Normal(loc, scale, validate_args=None)


>>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0]))
>>> m.sample()  # normally distributed with loc=0 and scale=1
tensor([ 0.1046])



• loc (float or Tensor) – 均值 (也被称为 mu)
• scale (float or Tensor) – 标准差(也被称为) sigma)
arg_constraints = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}

cdf(value)

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

icdf(value)

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

sample(sample_shape=torch.Size([]))

stddev

support = Real()

variance


## OneHotCategorical

class torch.distributions.one_hot_categorical.OneHotCategorical(probs=None, logits=None, validate_args=None)


probs必须是非负的, 有限的并且具有非零和, 并且它将被归一化为总和为1.

>>> 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.])



• probs (Tensor) – event probabilities
• logits (Tensor) – event log probabilities
arg_constraints = {'logits': Real(), 'probs': Simplex()}

entropy()

enumerate_support(expand=True)

expand(batch_shape, _instance=None)

has_enumerate_support = True

log_prob(value)

logits

mean

param_shape

probs

sample(sample_shape=torch.Size([]))

support = Simplex()

variance


## Pareto

class torch.distributions.pareto.Pareto(scale, alpha, validate_args=None)


>>> 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 = {'alpha': GreaterThan(lower_bound=0.0), 'scale': GreaterThan(lower_bound=0.0)}

entropy()

expand(batch_shape, _instance=None)

mean

support

variance


## Poisson

class torch.distributions.poisson.Poisson(rate, validate_args=None)


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



| 参数: | rate (Number_,_ Tensor) – rate 参数 |

arg_constraints = {'rate': GreaterThan(lower_bound=0.0)}

expand(batch_shape, _instance=None)

log_prob(value)

mean

sample(sample_shape=torch.Size([]))

support = IntegerGreaterThan(lower_bound=0)

variance


## RelaxedBernoulli

class torch.distributions.relaxed_bernoulli.RelaxedBernoulli(temperature, probs=None, logits=None, validate_args=None)


>>> 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])



• temperature (Tensor) – 松弛 temperature
• probs (Number_,_ Tensor) –采样 1 的概率
• logits (Number_,_ Tensor) – 采样 1 的对数概率
arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}

expand(batch_shape, _instance=None)

has_rsample = True

logits

probs

support = Interval(lower_bound=0.0, upper_bound=1.0)

temperature


## RelaxedOneHotCategorical

class torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature, probs=None, logits=None, validate_args=None)


>>> 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])



• temperature (Tensor) – 松弛 temperature
• probs (Tensor) – 事件概率
• logits (Tensor) –对数事件概率.
arg_constraints = {'logits': Real(), 'probs': Simplex()}

expand(batch_shape, _instance=None)

has_rsample = True

logits

probs

support = Simplex()

temperature


## StudentT

class torch.distributions.studentT.StudentT(df, loc=0.0, scale=1.0, validate_args=None)


>>> 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()

expand(batch_shape, _instance=None)

has_rsample = True

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

support = Real()

variance


## TransformedDistribution

class torch.distributions.transformed_distribution.TransformedDistribution(base_distribution, transforms, validate_args=None)


Distribution类的扩展, 它将一系列变换应用于基本分布. 假设f是所应用变换的组成:

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



# 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 = {}

cdf(value)


expand(batch_shape, _instance=None)

has_rsample

icdf(value)


log_prob(value)


rsample(sample_shape=torch.Size([]))


sample(sample_shape=torch.Size([]))


support


## Uniform

class torch.distributions.uniform.Uniform(low, high, validate_args=None)


>>> m = Uniform(torch.tensor([0.0]), torch.tensor([5.0]))
>>> m.sample()  # uniformly distributed in the range [0.0, 5.0)
tensor([ 2.3418])



arg_constraints = {'high': Dependent(), 'low': Dependent()}

cdf(value)

entropy()

expand(batch_shape, _instance=None)

has_rsample = True

icdf(value)

log_prob(value)

mean

rsample(sample_shape=torch.Size([]))

stddev

support

variance


## Weibull

class torch.distributions.weibull.Weibull(scale, concentration, validate_args=None)


Example

>>> 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])



arg_constraints = {'concentration': GreaterThan(lower_bound=0.0), 'scale': GreaterThan(lower_bound=0.0)}

entropy()

expand(batch_shape, _instance=None)

mean

support = GreaterThan(lower_bound=0.0)

variance


## KL Divergence

torch.distributions.kl.kl_divergence(p, q)


| 返回值: | 批量的 KL 散度, 形状为 batch_shape. |

| 返回类型： | Tensor |

| 异常: | NotImplementedError – 如果分布类型尚未通过注册 register_kl(). |

torch.distributions.kl.register_kl(type_p, type_q)


@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): ...



register_kl(DerivedP, DerivedQ)(kl_version1)  # Break the tie.



• type_p (type) – 子类 Distribution.
• type_q (type) – 子类 Distribution.

## Transforms

class torch.distributions.transforms.Transform(cache_size=0)


y = t(x)



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



| 参数: | cache_size (int) – 缓存大小. 如果为零, 则不进行缓存. 如果是, 则缓存最新的单个值. 仅支持0和1 |

| Variables: |

• domain (Constraint) – 表示该变换有效输入的约束.
• codomain (Constraint) – 表示此转换的有效输出的约束, 这些输出是逆变换的输入.
• bijective (bool) – 这个变换是否是双射的. 变换 t 是双射的 如果 t.inv(t(x)) == x 并且 t(t.inv(y)) == y 对于每一个 xy. 不是双射的变形应该至少保持较弱的伪逆属性 t(t.inv(t(x)) == t(x) and t.inv(t(t.inv(y))) == t.inv(y).
• sign (int or Tensor) – 对于双射单变量变换, 它应该是+1或-1, 这取决于变换是单调递增还是递减.
• event_dim (int) – 变换event_shape中相关的维数. 这对于逐点变换应该是0, 对于在矢量上共同作用的变换是1, 对于在矩阵上共同作用的变换是2, 等等.
inv


sign


log_abs_det_jacobian(x, y)


class torch.distributions.transforms.ComposeTransform(parts)


| 参数: | parts (list of Transform) – 列表 transforms. |

class torch.distributions.transforms.ExpTransform(cache_size=0)


class torch.distributions.transforms.PowerTransform(exponent, cache_size=0)


class torch.distributions.transforms.SigmoidTransform(cache_size=0)


class torch.distributions.transforms.AbsTransform(cache_size=0)


class torch.distributions.transforms.AffineTransform(loc, scale, event_dim=0, cache_size=0)


• loc (Tensor or float) – Location.
• scale (Tensor or float) – Scale.
• event_dim (int) – 可选的 event_shape 大小. T对于单变量随机变量, 该值应为零, 对于矢量分布, 1应为零, 对于矩阵的分布, 应为2.
class torch.distributions.transforms.SoftmaxTransform(cache_size=0)


class torch.distributions.transforms.StickBreakingTransform(cache_size=0)


class torch.distributions.transforms.LowerCholeskyTransform(cache_size=0)


## Constraints

The following constraints are implemented:

• constraints.boolean
• constraints.dependent
• constraints.greater_than(lower_bound)
• constraints.integer_interval(lower_bound, upper_bound)
• constraints.interval(lower_bound, upper_bound)
• constraints.lower_cholesky
• constraints.lower_triangular
• constraints.nonnegative_integer
• constraints.positive
• constraints.positive_definite
• constraints.positive_integer
• constraints.real
• constraints.real_vector
• constraints.simplex
• constraints.unit_interval
class torch.distributions.constraints.Constraint


constraints 的抽象基类.

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

check(value)


torch.distributions.constraints.dependent_property


alias of torch.distributions.constraints._DependentProperty

torch.distributions.constraints.integer_interval


alias of torch.distributions.constraints._IntegerInterval

torch.distributions.constraints.greater_than


alias of torch.distributions.constraints._GreaterThan

torch.distributions.constraints.greater_than_eq


alias of torch.distributions.constraints._GreaterThanEq

torch.distributions.constraints.less_than


alias of torch.distributions.constraints._LessThan

torch.distributions.constraints.interval


alias of torch.distributions.constraints._Interval

torch.distributions.constraints.half_open_interval


alias of torch.distributions.constraints._HalfOpenInterval

## Constraint Registry

PyTorch 提供两个全局 ConstraintRegistry 对象 , 链接 Constraint 对象到 Transform 对象. 这些对象既有输入约束, 也有返回变换, 但是它们对双射性有不同的保证.

1. biject_to(constraint) 查找一个双射的 Transformconstraints.real 到给定的 constraint. 返回的转换保证具有 .bijective = True 并且应该实现了 .log_abs_det_jacobian().
2. transform_to(constraint) 查找一个不一定是双射的 Transformconstraints.real 到给定的 constraint. 返回的转换不保证实现 .log_abs_det_jacobian().

transform_to()注册表对于对概率分布的约束参数执行无约束优化非常有用, 这些参数由每个分布的.arg_constraints指示. 这些变换通常会过度参数化空间以避免旋转; 因此, 它们更适合像Adam那样的坐标优化算法

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



biject_to() 注册表对于Hamiltonian Monte Carlo非常有用, 其中来自具有约束. .support的概率分布的样本在无约束空间中传播, 并且算法通常是旋转不变的

dist = Exponential(rate)
sample = biject_to(dist.support)(unconstrained)
potential_energy = -dist.log_prob(sample).sum()



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)



class torch.distributions.constraint_registry.ConstraintRegistry


register(constraint, factory=None)


@my_registry.register(MyConstraintClass)
def construct_transform(constraint):
assert isinstance(constraint, MyConstraint)
return MyTransform(constraint.arg_constraints)



• constraint (subclass of Constraint) – [Constraint]的子类(#torch.distributions.constraints.Constraint "torch.distributions.constraints.Constraint"), 或者派生类的对象.
• factory (callable) – 可调用对象, 输入 constraint 对象返回 Transform 对象.

• #### Python方向综合面试题

jackfrued python 115页 2019年5月26日
35

• #### Python进阶

东滨社 python 73页 2018年6月8日
2664

• #### 超级棒的"派神"书

zhaoolee python 34页 2018年5月1日
2

• #### Python方向综合面试题

jackfrued python 115页 2019年5月26日
35

• #### 浅入浅出 Android 安全

wizardforcel android 9页 2018年5月3日
59

• #### 关于python的面试题

jackfrued python 271页 2019年5月26日
33