distributions
程序包包含可參數(shù)化的概率分布和采樣函數(shù)。 這允許構造用于優(yōu)化的隨機計算圖和隨機梯度估計器。 該軟件包通常遵循 TensorFlow Distributions 軟件包的設計。
無法直接反向傳播隨機樣本。 但是,有兩種主要的方法可以創(chuàng)建可以反向傳播的代理功能。 它們是得分函數(shù)估計器/似然比估計器/ REINFORCE 和路徑導數(shù)估計器。 REINFORCE 通常被認為是強化學習中策略梯度方法的基礎,而路徑派生估計器通常出現(xiàn)在變分自動編碼器的重新參數(shù)化技巧中。 雖然得分函數(shù)僅需要樣本的值,但路徑導數(shù)需要導數(shù)
。 下一節(jié)將在強化學習示例中討論這兩個方面。
當概率密度函數(shù)的參數(shù)可微時,我們只需要sample()
和log_prob()
即可實現(xiàn) REINFORCE:
其中是參數(shù),
是學習率,
是獎勵,
是在給定策略
的狀態(tài)下在
狀態(tài)下采取行動
的概率。
在實踐中,我們將從網絡的輸出中采樣一個動作,將該動作應用于環(huán)境中,然后使用log_prob
構造等效的損失函數(shù)。 請注意,由于優(yōu)化程序使用梯度下降,因此我們使用負值,而上述規(guī)則假定梯度上升。 使用分類策略,用于實現(xiàn) REINFORCE 的代碼如下:
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()
實現(xiàn)這些隨機/策略梯度的另一種方法是使用rsample()
方法中的重新參數(shù)化技巧,其中可以通過無參數(shù)隨機變量的參數(shù)化確定性函數(shù)構造參數(shù)化隨機變量。 因此,重新參數(shù)化的樣本變得可微。 用于實現(xiàn)按路徑導數(shù)的代碼如下:
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)?
基數(shù):object
分布是概率分布的抽象基類。
property arg_constraints?
返回從參數(shù)名稱到 Constraint
對象的字典,此分布的每個參數(shù)都應滿足該對象。 不是 tensor 的 Args 不必出現(xiàn)在此字典中。
property batch_shape?
返回批處理參數(shù)的形狀。
cdf(value)?
返回以<cite>值</cite>評估的累積密度/質量函數(shù)。
參數(shù)
值 (tensor)–
entropy()?
返回分配的熵,在 batch_shape 中批處理。
退貨
形狀 batch_shape 的張量。
enumerate_support(expand=True)?
返回包含離散分布支持的所有值的張量。 結果將在維度 0 上枚舉,因此結果的形狀將為<cite>(基數(shù))+ batch_shape + event_shape</cite> (其中對于單變量分布,其中 <cite>event_shape =()</cite>)。
注意,這枚舉所有以鎖步 <cite>[[0,0],[1,1],…]</cite> 組成的張量。 在 <cite>expand = False</cite> 的情況下,枚舉沿暗淡 0 進行,但其余批次尺寸為單例尺寸 <cite>[[0],[1],..</cite> 。
要遍歷整個笛卡爾積,請使用 <cite>itertools.product(m.enumerate_support())</cite>。
Parameters
擴展 (bool )–是否擴展對批次暗淡的支持以匹配發(fā)行版的 <cite>batch_shape</cite> 。
Returns
張量在維度 0 上迭代。
property event_shape?
返回單個樣品的形狀(不分批)。
expand(batch_shape, _instance=None)?
返回一個新的分發(fā)實例(或填充派生類提供的現(xiàn)有實例),其批次尺寸擴展為 <cite>batchshape</cite> 。 此方法在發(fā)行版的參數(shù)上調用 expand
。 因此,這不會為擴展的分發(fā)實例分配新的內存。 此外,首次創(chuàng)建實例時,此操作不會在 <cite>\_init**.py</cite> 中重復任何參數(shù)檢查或參數(shù)廣播。
Parameters
Returns
具有批次尺寸的新分發(fā)實例已擴展為 <cite>batch_size</cite> 。
icdf(value)?
返回以<cite>值</cite>評估的逆累積密度/質量函數(shù)。
Parameters
value (Tensor) –
log_prob(value)?
返回以<cite>值</cite>評估的概率密度/質量函數(shù)的對數(shù)。
Parameters
value (Tensor) –
property mean?
返回分布的平均值。
perplexity()?
返回分配的復雜性,按 batch_shape 批處理。
Returns
Tensor of shape batch_shape.
rsample(sample_shape=torch.Size([]))?
如果分配了分布參數(shù),則生成一個 sample_shape 形狀的重新參數(shù)化樣本或 sample_shape 形狀的一批重新參數(shù)化樣本。
sample(sample_shape=torch.Size([]))?
如果分配參數(shù)是批處理的,則生成 sample_shape 形狀的樣本或 sample_shape 形狀的樣本批。
sample_n(n)?
如果分配了分布參數(shù),則生成 n 個樣本或 n 個樣本批次。
property stddev?
返回分布的標準偏差。
property support?
返回表示此發(fā)行版支持的 Constraint
對象。
property variance?
返回分布的方差。
class torch.distributions.exp_family.ExponentialFamily(batch_shape=torch.Size([]), event_shape=torch.Size([]), validate_args=None)?
堿基: torch.distributions.distribution.Distribution
ExponentialFamily 是屬于指數(shù)族的概率分布的抽象基類,其概率質量/密度函數(shù)的形式如下
其中表示自然參數(shù),
表示足夠的統(tǒng)計量,
是給定族的對數(shù)歸一化函數(shù),
是載波測量。
注意
此類是<cite>分布</cite>類和屬于指數(shù)家族的分布之間的中介,主要是檢查 <cite>.entropy()</cite>和分析性 KL 散度方法的正確性。 我們使用此類使用 AD 框架和 Bregman 散度來計算熵和 KL 散度(由 Frank Nielsen 和 Richard Nock 提供,指數(shù)族的熵和交叉熵)。
entropy()?
使用對數(shù)歸一化器的 Bregman 散度來計算熵的方法。
class torch.distributions.bernoulli.Bernoulli(probs=None, logits=None, validate_args=None)?
堿基: torch.distributions.exp_family.ExponentialFamily
創(chuàng)建一個由 probs
或 logits
參數(shù)化的伯努利分布(但不能同時包含兩者)。
樣本為二進制(0 或 1)。 它們以概率 <cite>p</cite> 取值 <cite>1</cite> ,以概率 <cite>1 -p</cite> 取值 <cite>0</cite> 。
例:
>>> m = Bernoulli(torch.tensor([0.3]))
>>> m.sample() # 30% chance 1; 70% chance 0
tensor([ 0.])
Parameters
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?
property mean?
property param_shape?
probs?
sample(sample_shape=torch.Size([]))?
support = Boolean()?
property variance?
class torch.distributions.beta.Beta(concentration1, concentration0, validate_args=None)?
Bases: torch.distributions.exp_family.ExponentialFamily
由 concentration1
和 concentration0
參數(shù)化的 Beta 分布。
Example:
>>> m = Beta(torch.tensor([0.5]), torch.tensor([0.5]))
>>> m.sample() # Beta distributed with concentration concentration1 and concentration0
tensor([ 0.1046])
Parameters
arg_constraints = {'concentration0': GreaterThan(lower_bound=0.0), 'concentration1': GreaterThan(lower_bound=0.0)}?
property concentration0?
property concentration1?
entropy()?
expand(batch_shape, _instance=None)?
has_rsample = True?
log_prob(value)?
property mean?
rsample(sample_shape=())?
support = Interval(lower_bound=0.0, upper_bound=1.0)?
property variance?
class torch.distributions.binomial.Binomial(total_count=1, probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建由total_count
和 probs
或 logits
(但不是全部)參數(shù)化的二項分布。 total_count
必須可與 probs
/ logits
廣播。
Example:
>>> 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.]])
Parameters
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?
property mean?
property param_shape?
probs?
sample(sample_shape=torch.Size([]))?
property support?
property variance?
class torch.distributions.categorical.Categorical(probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建由 probs
或 logits
(但不是全部)參數(shù)化的分類分布。
Note
等效于 torch.multinomial()
采樣的分布。
樣本是來自的整數(shù),其中 <cite>K</cite> 為
probs.size(-1)
。
如果 probs
為一維且長度為 <cite>K</cite> ,則每個元素都是在該索引處采樣類別的相對概率。
如果 probs
為 2D,則將其視為一批相對概率向量。
Note
probs
必須為非負數(shù),有限且總和為非零,并且將其歸一化為 1。
另請參見: torch.multinomial()
Example:
>>> m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
>>> m.sample() # equal probability of 0, 1, 2, 3
tensor(3)
Parameters
arg_constraints = {'logits': Real(), 'probs': Simplex()}?
entropy()?
enumerate_support(expand=True)?
expand(batch_shape, _instance=None)?
has_enumerate_support = True?
log_prob(value)?
logits?
property mean?
property param_shape?
probs?
sample(sample_shape=torch.Size([]))?
property support?
property variance?
class torch.distributions.cauchy.Cauchy(loc, scale, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
來自柯西(洛倫茲)分布的樣本。 具有均值 <cite>0</cite> 的獨立正態(tài)分布隨機變量的比率分布遵循柯西分布。
Example:
>>> 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])
Parameters
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)?
property mean?
rsample(sample_shape=torch.Size([]))?
support = Real()?
property variance?
class torch.distributions.chi2.Chi2(df, validate_args=None)?
堿基: torch.distributions.gamma.Gamma
創(chuàng)建通過形狀參數(shù) df
參數(shù)化的 Chi2 分布。 這完全等同于Gamma(alpha=0.5*df, beta=0.5)
Example:
>>> m = Chi2(torch.tensor([1.0]))
>>> m.sample() # Chi2 distributed with shape df=1
tensor([ 0.1046])
Parameters
df (python:float 或 tensor)–分布的形狀參數(shù)
arg_constraints = {'df': GreaterThan(lower_bound=0.0)}?
property df?
expand(batch_shape, _instance=None)?
class torch.distributions.dirichlet.Dirichlet(concentration, validate_args=None)?
Bases: torch.distributions.exp_family.ExponentialFamily
創(chuàng)建通過濃度concentration
參數(shù)化的 Dirichlet 分布。
Example:
>>> m = Dirichlet(torch.tensor([0.5, 0.5]))
>>> m.sample() # Dirichlet distributed with concentrarion concentration
tensor([ 0.1046, 0.8954])
Parameters
濃度 (tensor)–濃度參數(shù)分布(通常稱為 alpha)
arg_constraints = {'concentration': GreaterThan(lower_bound=0.0)}?
entropy()?
expand(batch_shape, _instance=None)?
has_rsample = True?
log_prob(value)?
property mean?
rsample(sample_shape=())?
support = Simplex()?
property variance?
class torch.distributions.exponential.Exponential(rate, validate_args=None)?
Bases: torch.distributions.exp_family.ExponentialFamily
創(chuàng)建由rate
參數(shù)化的指數(shù)分布。
Example:
>>> m = Exponential(torch.tensor([1.0]))
>>> m.sample() # Exponential distributed with rate=1
tensor([ 0.1046])
Parameters
比率 (python:float 或 tensor)–比率= 1 /分布范圍
arg_constraints = {'rate': GreaterThan(lower_bound=0.0)}?
cdf(value)?
entropy()?
expand(batch_shape, _instance=None)?
has_rsample = True?
icdf(value)?
log_prob(value)?
property mean?
rsample(sample_shape=torch.Size([]))?
property stddev?
support = GreaterThan(lower_bound=0.0)?
property variance?
class torch.distributions.fishersnedecor.FisherSnedecor(df1, df2, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建由df1
和df2
參數(shù)化的 Fisher-Snedecor 分布。
Example:
>>> m = FisherSnedecor(torch.tensor([1.0]), torch.tensor([2.0]))
>>> m.sample() # Fisher-Snedecor-distributed with df1=1 and df2=2
tensor([ 0.2453])
Parameters
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)?
property mean?
rsample(sample_shape=torch.Size([]))?
support = GreaterThan(lower_bound=0.0)?
property variance?
class torch.distributions.gamma.Gamma(concentration, rate, validate_args=None)?
Bases: torch.distributions.exp_family.ExponentialFamily
創(chuàng)建通過形狀concentration
和rate
參數(shù)化的 Gamma 分布。
Example:
>>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0]))
>>> m.sample() # Gamma distributed with concentration=1 and rate=1
tensor([ 0.1046])
Parameters
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)?
property mean?
rsample(sample_shape=torch.Size([]))?
support = GreaterThan(lower_bound=0.0)?
property variance?
class torch.distributions.geometric.Geometric(probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建由 probs
參數(shù)化的幾何分布,其中 probs
是伯努利試驗成功的概率。 它表示在 Bernoulli 試驗中,第一個
試驗失敗后才看到成功的可能性。
樣本是非負整數(shù)[0,)。
Example:
>>> m = Geometric(torch.tensor([0.3]))
>>> m.sample() # underlying Bernoulli has 30% chance 1; 70% chance 0
tensor([ 2.])
Parameters
arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}?
entropy()?
expand(batch_shape, _instance=None)?
log_prob(value)?
logits?
property mean?
probs?
sample(sample_shape=torch.Size([]))?
support = IntegerGreaterThan(lower_bound=0)?
property variance?
class torch.distributions.gumbel.Gumbel(loc, scale, validate_args=None)?
堿基: torch.distributions.transformed_distribution.TransformedDistribution
來自 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])
Parameters
arg_constraints = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}?
entropy()?
expand(batch_shape, _instance=None)?
log_prob(value)?
property mean?
property stddev?
support = Real()?
property variance?
class torch.distributions.half_cauchy.HalfCauchy(scale, validate_args=None)?
Bases: torch.distributions.transformed_distribution.TransformedDistribution
創(chuàng)建一個以<cite>標度</cite>為參數(shù)的半正態(tài)分布,其中:
X ~ Cauchy(0, scale)
Y = |X| ~ HalfCauchy(scale)
Example:
>>> m = HalfCauchy(torch.tensor([1.0]))
>>> m.sample() # half-cauchy distributed with scale=1
tensor([ 2.3214])
Parameters
比例尺 (python:float 或 tensor)–完整柯西分布的比例
arg_constraints = {'scale': GreaterThan(lower_bound=0.0)}?
cdf(value)?
entropy()?
expand(batch_shape, _instance=None)?
has_rsample = True?
icdf(prob)?
log_prob(value)?
property mean?
property scale?
support = GreaterThan(lower_bound=0.0)?
property variance?
class torch.distributions.half_normal.HalfNormal(scale, validate_args=None)?
Bases: torch.distributions.transformed_distribution.TransformedDistribution
Creates a half-normal distribution parameterized by <cite>scale</cite> where:
X ~ Normal(0, scale)
Y = |X| ~ HalfNormal(scale)
Example:
>>> m = HalfNormal(torch.tensor([1.0]))
>>> m.sample() # half-normal distributed with scale=1
tensor([ 0.1046])
Parameters
標度 (python:float 或 tensor)–完全正態(tài)分布的標度
arg_constraints = {'scale': GreaterThan(lower_bound=0.0)}?
cdf(value)?
entropy()?
expand(batch_shape, _instance=None)?
has_rsample = True?
icdf(prob)?
log_prob(value)?
property mean?
property scale?
support = GreaterThan(lower_bound=0.0)?
property variance?
class torch.distributions.independent.Independent(base_distribution, reinterpreted_batch_ndims, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
將某個分發(fā)的一些批次模糊重新解釋為事件暗淡。
這對于更改 log_prob()
的結果形狀非常有用。 例如,要創(chuàng)建與多變量正態(tài)分布具有相同形狀的對角正態(tài)分布(因此它們是可互換的),您可以:
>>> 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,))]
Parameters
arg_constraints = {}?
entropy()?
enumerate_support(expand=True)?
expand(batch_shape, _instance=None)?
property has_enumerate_support?
property has_rsample?
log_prob(value)?
property mean?
rsample(sample_shape=torch.Size([]))?
sample(sample_shape=torch.Size([]))?
property support?
property variance?
class torch.distributions.laplace.Laplace(loc, scale, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建由loc
和:attr:'scale'參數(shù)化的拉普拉斯分布。
Example:
>>> m = Laplace(torch.tensor([0.0]), torch.tensor([1.0]))
>>> m.sample() # Laplace distributed with loc=0, scale=1
tensor([ 0.1046])
Parameters
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)?
property mean?
rsample(sample_shape=torch.Size([]))?
property stddev?
support = Real()?
property variance?
class torch.distributions.log_normal.LogNormal(loc, scale, validate_args=None)?
Bases: torch.distributions.transformed_distribution.TransformedDistribution
創(chuàng)建由 loc
和 scale
參數(shù)化的對數(shù)正態(tài)分布,其中:
X ~ Normal(loc, scale)
Y = exp(X) ~ LogNormal(loc, scale)
Example:
>>> m = LogNormal(torch.tensor([0.0]), torch.tensor([1.0]))
>>> m.sample() # log-normal distributed with mean=0 and stddev=1
tensor([ 0.1046])
Parameters
arg_constraints = {'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}?
entropy()?
expand(batch_shape, _instance=None)?
has_rsample = True?
property loc?
property mean?
property scale?
support = GreaterThan(lower_bound=0.0)?
property variance?
class torch.distributions.lowrank_multivariate_normal.LowRankMultivariateNormal(loc, cov_factor, cov_diag, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
使用協(xié)方差矩陣創(chuàng)建具有由cov_factor
和cov_diag
參數(shù)化的低秩形式的多元正態(tài)分布:
covariance_matrix = cov_factor @ cov_factor.T + cov_diag
例
>>> 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])
Parameters
Note
由于伍德伯里矩陣恒等式和 矩陣行列式引理。 由于有了這些公式,我們只需要計算小尺寸“電容”矩陣的行列式和逆式:
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)?
property mean?
precision_matrix?
rsample(sample_shape=torch.Size([]))?
scale_tril?
support = Real()?
variance?
class torch.distributions.multinomial.Multinomial(total_count=1, probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建由total_count
和 probs
或 logits
(但不是全部)參數(shù)化的多項式分布。 probs
的最內維度在類別上進行索引。 所有其他尺寸均按批次編制索引。
請注意,如果僅調用 log_prob()
,則無需指定total_count
(請參見下面的示例)
Note
probs
必須為非負數(shù),有限且總和為非零,并且將其歸一化為 1。
sample()
需要一個共享的 <cite>total_count</cite> 。log_prob()
允許每個參數(shù)和樣本使用不同的 <cite>total_count</cite> 。Example:
>>> 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])
Parameters
arg_constraints = {'logits': Real(), 'probs': Simplex()}?
expand(batch_shape, _instance=None)?
log_prob(value)?
property logits?
property mean?
property param_shape?
property probs?
sample(sample_shape=torch.Size([]))?
property support?
property variance?
class torch.distributions.multivariate_normal.MultivariateNormal(loc, covariance_matrix=None, precision_matrix=None, scale_tril=None, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建由均值向量和協(xié)方差矩陣參數(shù)化的多元正態(tài)(也稱為高斯)分布。
可以使用正定協(xié)方差矩陣或正定精度矩陣
或具有正值對角線項的下三角矩陣
來參數(shù)化多元正態(tài)分布,例如
。 該三角矩陣可以通過例如 協(xié)方差的 Cholesky 分解。
Example
>>> 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])
Parameters
Note
只能指定 covariance_matrix
或 precision_matrix
或 scale_tril
中的一個。
使用 scale_tril
會更高效:所有內部計算都基于 scale_tril
。 如果改為通過 covariance_matrix
或 precision_matrix
,則僅用于使用 Cholesky 分解來計算相應的下三角矩陣。
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)?
property mean?
precision_matrix?
rsample(sample_shape=torch.Size([]))?
scale_tril?
support = Real()?
property variance?
class torch.distributions.negative_binomial.NegativeBinomial(total_count, probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建負二項式分布,即在total_count
失敗之前,成功的獨立且相同的 Bernoulli 試驗次數(shù)的分布。 每個伯努利試驗成功的概率為 probs
。
Parameters
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?
property mean?
property param_shape?
probs?
sample(sample_shape=torch.Size([]))?
support = IntegerGreaterThan(lower_bound=0)?
property variance?
class torch.distributions.normal.Normal(loc, scale, validate_args=None)?
Bases: torch.distributions.exp_family.ExponentialFamily
創(chuàng)建由loc
和scale
參數(shù)化的正態(tài)(也稱為高斯)分布。
Example:
>>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0]))
>>> m.sample() # normally distributed with loc=0 and scale=1
tensor([ 0.1046])
Parameters
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)?
property mean?
rsample(sample_shape=torch.Size([]))?
sample(sample_shape=torch.Size([]))?
property stddev?
support = Real()?
property variance?
class torch.distributions.one_hot_categorical.OneHotCategorical(probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建由 probs
或 logits
參數(shù)化的單熱點分類分布。
樣本是大小為probs.size(-1)
的一鍵編碼矢量。
Note
probs
必須為非負數(shù),有限且總和為非零,并且將其歸一化為 1。
另請參見:torch.distributions.Categorical()
了解 probs
和 logits
的規(guī)格。
Example:
>>> 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.])
Parameters
arg_constraints = {'logits': Real(), 'probs': Simplex()}?
entropy()?
enumerate_support(expand=True)?
expand(batch_shape, _instance=None)?
has_enumerate_support = True?
log_prob(value)?
property logits?
property mean?
property param_shape?
property probs?
sample(sample_shape=torch.Size([]))?
support = Simplex()?
property variance?
class torch.distributions.pareto.Pareto(scale, alpha, validate_args=None)?
Bases: torch.distributions.transformed_distribution.TransformedDistribution
來自帕累托 1 型分布的樣本。
Example:
>>> 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])
Parameters
arg_constraints = {'alpha': GreaterThan(lower_bound=0.0), 'scale': GreaterThan(lower_bound=0.0)}?
entropy()?
expand(batch_shape, _instance=None)?
property mean?
property support?
property variance?
class torch.distributions.poisson.Poisson(rate, validate_args=None)?
Bases: torch.distributions.exp_family.ExponentialFamily
創(chuàng)建由rate
(速率參數(shù))參數(shù)化的泊松分布。
樣本是非負整數(shù),pmf 為
Example:
>>> m = Poisson(torch.tensor([4]))
>>> m.sample()
tensor([ 3.])
Parameters
速率(編號 , tensor)–速率參數(shù)
arg_constraints = {'rate': GreaterThan(lower_bound=0.0)}?
expand(batch_shape, _instance=None)?
log_prob(value)?
property mean?
sample(sample_shape=torch.Size([]))?
support = IntegerGreaterThan(lower_bound=0)?
property variance?
class torch.distributions.relaxed_bernoulli.RelaxedBernoulli(temperature, probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.transformed_distribution.TransformedDistribution
創(chuàng)建由 temperature
以及 probs
或 logits
(但不是全部)參數(shù)化的 RelaxedBernoulli 分布。 這是 <cite>Bernoulli</cite> 分布的寬松版本,因此值在(0,1)中,并且具有可重新設置參數(shù)的樣本。
Example:
>>> 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])
Parameters
arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}?
expand(batch_shape, _instance=None)?
has_rsample = True?
property logits?
property probs?
support = Interval(lower_bound=0.0, upper_bound=1.0)?
property temperature?
class torch.distributions.relaxed_bernoulli.LogitRelaxedBernoulli(temperature, probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建一個以 probs
或 logits
(但不是兩者)為參數(shù)的 LogitRelaxedBernoulli 分布,這是 RelaxedBernoulli 分布的對數(shù)。
樣本是(0,1)中值的對數(shù)。 有關更多詳細信息,請參見[1]。
Parameters
[1]具體分布:離散隨機變量的連續(xù)松弛(Maddison 等,2017)
[2]使用 Gumbel-Softmax 分類重新參數(shù)化(Jang 等,2017)
arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}?
expand(batch_shape, _instance=None)?
log_prob(value)?
logits?
property param_shape?
probs?
rsample(sample_shape=torch.Size([]))?
support = Real()?
class torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature, probs=None, logits=None, validate_args=None)?
Bases: torch.distributions.transformed_distribution.TransformedDistribution
創(chuàng)建一個由 temperature
以及 probs
或 logits
設置參數(shù)的 RelaxedOneHotCategorical 分布。 這是OneHotCategorical
發(fā)行版的寬松版本,因此其示例位于單純形上,并且可以重新設置參數(shù)。
Example:
>>> 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])
Parameters
arg_constraints = {'logits': Real(), 'probs': Simplex()}?
expand(batch_shape, _instance=None)?
has_rsample = True?
property logits?
property probs?
support = Simplex()?
property temperature?
class torch.distributions.studentT.StudentT(df, loc=0.0, scale=1.0, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
創(chuàng)建以自由度df
,均值loc
和小數(shù)位數(shù)scale
為參數(shù)的學生 t 分布。
Example:
>>> m = StudentT(torch.tensor([2.0]))
>>> m.sample() # Student's t-distributed with degrees of freedom=2
tensor([ 0.1046])
Parameters
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)?
property mean?
rsample(sample_shape=torch.Size([]))?
support = Real()?
property variance?
class torch.distributions.transformed_distribution.TransformedDistribution(base_distribution, transforms, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
分發(fā)類的擴展,該類將一系列 Transforms 應用于基本分發(fā)。 令 f 為所應用轉換的組成:
X ~ BaseDistribution
Y = f(X) ~ TransformedDistribution(BaseDistribution, f)
log p(Y) = log p(X) + log |det (dX/dY)|
注意, TransformedDistribution
的.event_shape
是其基本分布及其變換的最大形狀,因為變換可以在事件之間引入相關性。
TransformedDistribution
的用法示例為:
## 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)
有關更多示例,請查看 Gumbel
, HalfCauchy
, HalfNormal
,LogNormal
的實現(xiàn), Pareto
, Weibull
, RelaxedBernoulli
和 RelaxedOneHotCategorical
arg_constraints = {}?
cdf(value)?
通過反轉變換并計算基本分布的分數(shù)來計算累積分布函數(shù)。
expand(batch_shape, _instance=None)?
property has_rsample?
icdf(value)?
使用變換計算逆累積分布函數(shù),并計算基本分布的分數(shù)。
log_prob(value)?
通過反轉變換對樣本進行評分,并使用基本分布的分數(shù)和 log abs det jacobian 計算分數(shù)。
rsample(sample_shape=torch.Size([]))?
如果分配了分布參數(shù),則生成一個 sample_shape 形狀的重新參數(shù)化樣本或 sample_shape 形狀的一批重新參數(shù)化樣本。 首先從基本分布中采樣,并對列表中的每個變換應用 <cite>transform()</cite>。
sample(sample_shape=torch.Size([]))?
如果分配參數(shù)是批處理的,則生成 sample_shape 形狀的樣本或 sample_shape 形狀的樣本批。 首先從基本分布中采樣,并對列表中的每個變換應用 <cite>transform()</cite>。
property support?
class torch.distributions.uniform.Uniform(low, high, validate_args=None)?
Bases: torch.distributions.distribution.Distribution
從半開間隔[low, high)
生成均勻分布的隨機樣本。
Example:
>>> m = Uniform(torch.tensor([0.0]), torch.tensor([5.0]))
>>> m.sample() # uniformly distributed in the range [0.0, 5.0)
tensor([ 2.3418])
Parameters
arg_constraints = {'high': Dependent(), 'low': Dependent()}?
cdf(value)?
entropy()?
expand(batch_shape, _instance=None)?
has_rsample = True?
icdf(value)?
log_prob(value)?
property mean?
rsample(sample_shape=torch.Size([]))?
property stddev?
property support?
property variance?
class torch.distributions.weibull.Weibull(scale, concentration, validate_args=None)?
Bases: torch.distributions.transformed_distribution.TransformedDistribution
來自兩參數(shù)威布爾分布的樣本。
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])
Parameters
arg_constraints = {'concentration': GreaterThan(lower_bound=0.0), 'scale': GreaterThan(lower_bound=0.0)}?
entropy()?
expand(batch_shape, _instance=None)?
property mean?
support = GreaterThan(lower_bound=0.0)?
property variance?
torch.distributions.kl.kl_divergence(p, q)?
計算兩個分布之間的 Kullback-Leibler 散度。
Parameters
Distribution
對象。Distribution
對象。Returns
形狀為 <cite>batch_shape</cite> 的一批 KL 散度。
返回類型
張量
Raises
NotImplementedError -如果尚未通過 register_kl()
注冊分發(fā)類型。
torch.distributions.kl.register_kl(type_p, type_q)?
裝飾器向 kl_divergence()
注冊成對功能。 用法:
@register_kl(Normal, Normal)
def kl_normal_normal(p, q):
# insert implementation here
查找返回按子類排序的最特定的(類型,類型)匹配。 如果匹配不明確,則會引發(fā) <cite>RuntimeWarning</cite> 。 例如解決模棱兩可的情況:
@register_kl(BaseP, DerivedQ)
def kl_version1(p, q): ...
@register_kl(DerivedP, BaseQ)
def kl_version2(p, q): ...
您應該注冊第三個最具體的實現(xiàn),例如:
register_kl(DerivedP, DerivedQ)(kl_version1) # Break the tie.
Parameters
Distribution
的子類。Distribution
的子類。class torch.distributions.transforms.Transform(cache_size=0)?
可計算 log det jacobians 的可逆轉換的抽象類。 它們主要用于torch.distributions.TransformedDistribution
中。
高速緩存對于反變換代價昂貴或數(shù)值不穩(wěn)定的變換非常有用。 請注意,記住的值必須小心,因為自動刻度圖可能會顛倒。 例如,在以下情況下可以使用或不使用緩存:
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
派生類應實現(xiàn)_call()
或_inverse()
中的一個或兩個。 設置 <cite>bijective = True</cite> 的派生類也應實現(xiàn) log_abs_det_jacobian()
。
Parameters
cache_size (python:int )–緩存的大小。 如果為零,則不進行緩存。 如果為 1,則將緩存最新的單個值。 僅支持 0 和 1。
Variables
Constraint
)–表示此變換有效輸入的約束。Constraint
)–表示此變換的有效輸出的約束,該約束是逆變換的輸入。x
和共域中的y
,變換t
是雙射 iff t.inv(t(x)) == x
和t(t.inv(y)) == y
。 非雙射的變換至少應保持較弱的偽逆特性t(t.inv(t(x)) == t(x)
和t.inv(t(t.inv(y))) == t.inv(y)
。event_shape
中相互關聯(lián)的維數(shù)。 對于逐點變換,應為 0;對于聯(lián)合作用于矢量的變換,應為 1;對于聯(lián)合作用于矩陣的變換,其應為 2。property inv?
返回此變換的逆數(shù) Transform
。 這應該滿足t.inv.inv is t
。
property sign?
返回雅可比行列式的符號(如果適用)。 通常,這僅對雙射變換有意義。
log_abs_det_jacobian(x, y)?
計算 log det jacobian <cite>log | dy / dx |</cite> 給定輸入和輸出。
class torch.distributions.transforms.ComposeTransform(parts)?
組成一個鏈中的多個變換。 組成的轉換負責緩存。
Parameters
部分 (Transform
的列表)–組成變換的列表。
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)?
通過逐點仿射映射進行變換。
Parameters
class torch.distributions.transforms.SoftmaxTransform(cache_size=0)?
通過從不受約束的空間轉換為單純形,然后進行規(guī)范化。
這不是雙射的,不能用于 HMC。 但是,這主要是按坐標方式(最終歸一化除外),因此適用于按坐標優(yōu)化算法。
class torch.distributions.transforms.StickBreakingTransform(cache_size=0)?
通過不折斷的過程將不受約束的空間轉換為一維的單純形。
該變換以 <cite>Dirichlet</cite> 分布的小節(jié)結構中的迭代 S 形變換形式出現(xiàn):第一個 logit 通過 S 形變換成第一個概率和所有其他概率,然后過程重復進行。
這是雙射的,適合在 HMC 中使用; 但是,它將坐標混合在一起,不太適合優(yōu)化。
class torch.distributions.transforms.LowerCholeskyTransform(cache_size=0)?
從非約束矩陣轉換為具有非負對角線項的低三角形矩陣。
這對于根據(jù)正定矩陣的 Cholesky 因式分解參數(shù)化很有用。
class torch.distributions.transforms.CatTransform(tseq, dim=0, lengths=None)?
變換函子,以與兼容的方式,以長度,長度[dim] 為單位,將每個分量的 tseq 變換序列應用于每個子矩陣。 ] torch.cat()
。
Example::
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,長度= [10,10])t = CatTransform([t0,t0],dim = 0,長度= [20,20])y = t(x)
class torch.distributions.transforms.StackTransform(tseq, dim=0)?
變換函子,以與 torch.stack()
兼容的方式,對<cite>暗淡</cite>處的每個子矩陣按分量進行變換 <cite>tseq</cite> 的序列。
Example::
x = torch.stack([torch.range(1,10),torch.range(1,10)],dim = 1)t = StackTransform([ExpTransform(),identity_transform],dim = 1)y = t (X)
實現(xiàn)了以下約束:
constraints.boolean
constraints.cat
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.stack
constraints.unit_interval
class torch.distributions.constraints.Constraint?
約束的抽象基類。
約束對象表示變量有效的區(qū)域,例如 在其中可以優(yōu)化變量。
check(value)?
返回 <cite>sample_shape + batch_shape</cite> 的字節(jié)張量,指示值中的每個事件是否滿足此約束。
torch.distributions.constraints.dependent_property?
torch.distributions.constraints._DependentProperty
的別名
torch.distributions.constraints.integer_interval?
torch.distributions.constraints._IntegerInterval
的別名
torch.distributions.constraints.greater_than?
torch.distributions.constraints._GreaterThan
的別名
torch.distributions.constraints.greater_than_eq?
torch.distributions.constraints._GreaterThanEq
的別名
torch.distributions.constraints.less_than?
torch.distributions.constraints._LessThan
的別名
torch.distributions.constraints.interval?
torch.distributions.constraints._Interval
的別名
torch.distributions.constraints.half_open_interval?
torch.distributions.constraints._HalfOpenInterval
的別名
torch.distributions.constraints.cat?
torch.distributions.constraints._Cat
的別名
torch.distributions.constraints.stack?
torch.distributions.constraints._Stack
的別名
PyTorch 提供了兩個全局 ConstraintRegistry
對象,這些對象將 Constraint
對象鏈接到 Transform
對象。 這些對象既有輸入約束又有返回變換,但對雙射性有不同的保證。
biject_to(constraint)
查找從constraints.real
到給定constraint
的雙射 Transform
。 保證返回的轉換具有.bijective = True
并應實現(xiàn).log_abs_det_jacobian()
。transform_to(constraint)
查找從constraints.real
到給定constraint
的不必要的雙射 Transform
。 返回的轉換不能保證實現(xiàn).log_abs_det_jacobian()
。
transform_to()
注冊表可用于對概率分布的受約束參數(shù)執(zhí)行無約束優(yōu)化,該概率分布由每個分布的.arg_constraints
dict 指示。 為了避免旋轉,這些變換通常對空間進行了參數(shù)化; 因此,它們更適合于像 Adam 這樣的坐標優(yōu)化算法:
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()
Note
transform_to
和biject_to
不同的一個例子是constraints.simplex
:transform_to(constraints.simplex)
返回一個 SoftmaxTransform
,該輸入簡單地對輸入進行指數(shù)化和歸一化; 這是一種便宜的方法,通常適合于像 SVI 這樣的算法進行協(xié)調操作。 相反,biject_to(constraints.simplex)
返回一個 StickBreakingTransform
,它將其輸入降低到一維空間; 這是一個更昂貴的數(shù)字穩(wěn)定度較低的變換,但對于 HMC 這樣的算法來說是必需的。
可以通過用戶定義的約束擴展biject_to
和transform_to
對象,并使用它們的.register()
方法將其轉換為單例約束的函數(shù):
transform_to.register(my_constraint, my_transform)
或作為參數(shù)約束的修飾器:
@transform_to.register(MyConstraintClass)
def my_factory(constraint):
assert isinstance(constraint, MyConstraintClass)
return MyTransform(constraint.param1, constraint.param2)
您可以通過創(chuàng)建新的 ConstraintRegistry
對象來創(chuàng)建自己的注冊表。
class torch.distributions.constraint_registry.ConstraintRegistry?
注冊表將約束鏈接到轉換。
register(constraint, factory=None)?
在此注冊表中注冊 Constraint
子類。 用法:
@my_registry.register(MyConstraintClass)
def construct_transform(constraint):
assert isinstance(constraint, MyConstraint)
return MyTransform(constraint.arg_constraints)
Parameters
Constraint
的子類)– Constraint
的子類,或所需類的單例對象。Transform
對象的可調用對象。
更多建議: