PyTorch 概率分布-torch分布

2020-09-15 10:10 更新

原文: PyTorch 概率分布-torch分布

distributions程序包包含可參數(shù)化的概率分布和采樣函數(shù)。 這允許構造用于優(yōu)化的隨機計算圖和隨機梯度估計器。 該軟件包通常遵循 TensorFlow Distributions 軟件包的設計。

無法直接反向傳播隨機樣本。 但是,有兩種主要的方法可以創(chuàng)建可以反向傳播的代理功能。 它們是得分函數(shù)估計器/似然比估計器/ REINFORCE 和路徑導數(shù)估計器。 REINFORCE 通常被認為是強化學習中策略梯度方法的基礎,而路徑派生估計器通常出現(xiàn)在變分自動編碼器的重新參數(shù)化技巧中。 雖然得分函數(shù)僅需要樣本img的值,但路徑導數(shù)需要導數(shù)img。 下一節(jié)將在強化學習示例中討論這兩個方面。

評分功能

當概率密度函數(shù)的參數(shù)可微時,我們只需要sample()log_prob()即可實現(xiàn) REINFORCE:

img

其中img是參數(shù),img是學習率,img是獎勵,img是在給定策略img的狀態(tài)下在img狀態(tài)下采取行動img的概率。

在實踐中,我們將從網絡的輸出中采樣一個動作,將該動作應用于環(huán)境中,然后使用log_prob構造等效的損失函數(shù)。 請注意,由于優(yōu)化程序使用梯度下降,因此我們使用負值,而上述規(guī)則假定梯度上升。 使用分類策略,用于實現(xiàn) REINFORCE 的代碼如下:

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

路徑導數(shù)

實現(xiàn)這些隨機/策略梯度的另一種方法是使用rsample()方法中的重新參數(shù)化技巧,其中可以通過無參數(shù)隨機變量的參數(shù)化確定性函數(shù)構造參數(shù)化隨機變量。 因此,重新參數(shù)化的樣本變得可微。 用于實現(xiàn)按路徑導數(shù)的代碼如下:

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

分配

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

基數(shù):object

分布是概率分布的抽象基類。

  1. property arg_constraints?

返回從參數(shù)名稱到 Constraint 對象的字典,此分布的每個參數(shù)都應滿足該對象。 不是 tensor 的 Args 不必出現(xiàn)在此字典中。

  1. property batch_shape?

返回批處理參數(shù)的形狀。

  1. cdf(value)?

返回以<cite>值</cite>評估的累積密度/質量函數(shù)。

參數(shù)

(tensor)–

  1. entropy()?

返回分配的熵,在 batch_shape 中批處理。

退貨

形狀 batch_shape 的張量。

  1. 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 上迭代。

  1. property event_shape?

返回單個樣品的形狀(不分批)。

  1. expand(batch_shape, _instance=None)?

返回一個新的分發(fā)實例(或填充派生類提供的現(xiàn)有實例),其批次尺寸擴展為 <cite>batchshape</cite> 。 此方法在發(fā)行版的參數(shù)上調用 expand 。 因此,這不會為擴展的分發(fā)實例分配新的內存。 此外,首次創(chuàng)建實例時,此操作不會在 <cite>\_init**.py</cite> 中重復任何參數(shù)檢查或參數(shù)廣播。

Parameters

  • batch_shape (torch尺寸)–所需的擴展尺寸。
  • _instance -子類提供的需要重寫 <cite>.expand</cite> 的新實例。

Returns

具有批次尺寸的新分發(fā)實例已擴展為 <cite>batch_size</cite> 。

  1. icdf(value)?

返回以<cite>值</cite>評估的逆累積密度/質量函數(shù)。

Parameters

value (Tensor) –

  1. log_prob(value)?

返回以<cite>值</cite>評估的概率密度/質量函數(shù)的對數(shù)。

Parameters

value (Tensor) –

  1. property mean?

返回分布的平均值。

  1. perplexity()?

返回分配的復雜性,按 batch_shape 批處理。

Returns

Tensor of shape batch_shape.

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

如果分配了分布參數(shù),則生成一個 sample_shape 形狀的重新參數(shù)化樣本或 sample_shape 形狀的一批重新參數(shù)化樣本。

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

如果分配參數(shù)是批處理的,則生成 sample_shape 形狀的樣本或 sample_shape 形狀的樣本批。

  1. sample_n(n)?

如果分配了分布參數(shù),則生成 n 個樣本或 n 個樣本批次。

  1. property stddev?

返回分布的標準偏差。

  1. property support?

返回表示此發(fā)行版支持的 Constraint 對象。

  1. property variance?

返回分布的方差。

指數(shù)家族

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

堿基: torch.distributions.distribution.Distribution

ExponentialFamily 是屬于指數(shù)族的概率分布的抽象基類,其概率質量/密度函數(shù)的形式如下

img

其中img表示自然參數(shù),img表示足夠的統(tǒng)計量,img是給定族的對數(shù)歸一化函數(shù),img是載波測量。

注意

此類是<cite>分布</cite>類和屬于指數(shù)家族的分布之間的中介,主要是檢查 <cite>.entropy()</cite>和分析性 KL 散度方法的正確性。 我們使用此類使用 AD 框架和 Bregman 散度來計算熵和 KL 散度(由 Frank Nielsen 和 Richard Nock 提供,指數(shù)族的熵和交叉熵)。

  1. entropy()?

使用對數(shù)歸一化器的 Bregman 散度來計算熵的方法。

伯努利

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

堿基: torch.distributions.exp_family.ExponentialFamily

創(chuàng)建一個由 probslogits 參數(shù)化的伯努利分布(但不能同時包含兩者)。

樣本為二進制(0 或 1)。 它們以概率 <cite>p</cite> 取值 <cite>1</cite> ,以概率 <cite>1 -p</cite> 取值 <cite>0</cite> 。

例:

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

Parameters

  • 概率(編號 , tensor)–采樣 <cite>1</cite> 的概率
  • 對數(shù)(編號 tensor)–采樣的對數(shù)奇數(shù) <cite>1</cite>

  1. arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}?

  1. entropy()?

  1. enumerate_support(expand=True)?

  1. expand(batch_shape, _instance=None)?
  2. has_enumerate_support = True?

  1. log_prob(value)?
  2. logits?

  1. property mean?

  1. property param_shape?
  2. probs?

  1. sample(sample_shape=torch.Size([]))?
  2. support = Boolean()?

  1. property variance?

貝塔

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

Bases: torch.distributions.exp_family.ExponentialFamily

concentration1concentration0 參數(shù)化的 Beta 分布。

Example:

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

Parameters

  • 濃度 1 (python:float tensor)–分布的第一濃度參數(shù)(通常稱為濃度參數(shù)) α)
  • 濃度 0 (python:float tensor)–分布的第二個濃度參數(shù)(通常稱為 Beta)

  1. arg_constraints = {'concentration0': GreaterThan(lower_bound=0.0), 'concentration1': GreaterThan(lower_bound=0.0)}?

  1. property concentration0?

  1. property concentration1?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. log_prob(value)?

  1. property mean?

  1. rsample(sample_shape=())?
  2. support = Interval(lower_bound=0.0, upper_bound=1.0)?

  1. property variance?

二項式

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

Bases: torch.distributions.distribution.Distribution

創(chuàng)建由total_countprobslogits (但不是全部)參數(shù)化的二項分布。 total_count必須可與 probs / logits 廣播。

Example:

  1. >>> m = Binomial(100, torch.tensor([0 , .2, .8, 1]))
  2. >>> x = m.sample()
  3. tensor([ 0., 22., 71., 100.])
  4. >>> m = Binomial(torch.tensor([[5.], [10.]]), torch.tensor([0.5, 0.8]))
  5. >>> x = m.sample()
  6. tensor([[ 4., 5.],
  7. [ 7., 6.]])

Parameters

  • total_count (python:int tensor)–伯努利試驗次數(shù)
  • 概率 (tensor)–事件概率
  • logits (tensor)–事件對數(shù)

  1. arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0), 'total_count': IntegerGreaterThan(lower_bound=0)}?

  1. enumerate_support(expand=True)?

  1. expand(batch_shape, _instance=None)?
  2. has_enumerate_support = True?

  1. log_prob(value)?
  2. logits?

  1. property mean?

  1. property param_shape?
  2. probs?

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

  1. property support?

  1. property variance?

分類的

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

Bases: torch.distributions.distribution.Distribution

創(chuàng)建由 probslogits (但不是全部)參數(shù)化的分類分布。

Note

等效于 torch.multinomial() 采樣的分布。

樣本是來自img的整數(shù),其中 <cite>K</cite> 為probs.size(-1)。

如果 probs 為一維且長度為 <cite>K</cite> ,則每個元素都是在該索引處采樣類別的相對概率。

如果 probs 為 2D,則將其視為一批相對概率向量。

Note

probs 必須為非負數(shù),有限且總和為非零,并且將其歸一化為 1。

另請參見: torch.multinomial()

Example:

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

Parameters

  • 概率 (tensor)–事件概率
  • logits (tensor)–事件對數(shù)

  1. arg_constraints = {'logits': Real(), 'probs': Simplex()}?

  1. entropy()?

  1. enumerate_support(expand=True)?

  1. expand(batch_shape, _instance=None)?
  2. has_enumerate_support = True?

  1. log_prob(value)?
  2. logits?

  1. property mean?

  1. property param_shape?
  2. probs?

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

  1. property support?

  1. property variance?

柯西

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

Bases: torch.distributions.distribution.Distribution

來自柯西(洛倫茲)分布的樣本。 具有均值 <cite>0</cite> 的獨立正態(tài)分布隨機變量的比率分布遵循柯西分布。

Example:

  1. >>> m = Cauchy(torch.tensor([0.0]), torch.tensor([1.0]))
  2. >>> m.sample() # sample from a Cauchy distribution with loc=0 and scale=1
  3. tensor([ 2.3214])

Parameters

  • loc (python:float tensor)–分布的中位數(shù)。
  • 比例尺 (python:float tensor)–半寬度為一半的最大值。

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

  1. cdf(value)?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. icdf(value)?

  1. log_prob(value)?

  1. property mean?

  1. rsample(sample_shape=torch.Size([]))?
  2. support = Real()?

  1. property variance?

22

  1. 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:

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

Parameters

df (python:float tensor)–分布的形狀參數(shù)

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

  1. property df?

  1. expand(batch_shape, _instance=None)?

Dirichlet

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

Bases: torch.distributions.exp_family.ExponentialFamily

創(chuàng)建通過濃度concentration參數(shù)化的 Dirichlet 分布。

Example:

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

Parameters

濃度 (tensor)–濃度參數(shù)分布(通常稱為 alpha)

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

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. log_prob(value)?

  1. property mean?

  1. rsample(sample_shape=())?
  2. support = Simplex()?

  1. property variance?

指數(shù)的

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

Bases: torch.distributions.exp_family.ExponentialFamily

創(chuàng)建由rate參數(shù)化的指數(shù)分布。

Example:

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

Parameters

比率 (python:float tensor)–比率= 1 /分布范圍

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

  1. cdf(value)?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. icdf(value)?

  1. log_prob(value)?

  1. property mean?

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

  1. property stddev?
  2. support = GreaterThan(lower_bound=0.0)?

  1. property variance?

FisherSnedecor

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

Bases: torch.distributions.distribution.Distribution

創(chuàng)建由df1df2參數(shù)化的 Fisher-Snedecor 分布。

Example:

  1. >>> m = FisherSnedecor(torch.tensor([1.0]), torch.tensor([2.0]))
  2. >>> m.sample() # Fisher-Snedecor-distributed with df1=1 and df2=2
  3. tensor([ 0.2453])

Parameters

  • df1 (python:float tensor)–自由度參數(shù) 1
  • df2 (python:float tensor)–自由度參數(shù) 2

  1. arg_constraints = {'df1': GreaterThan(lower_bound=0.0), 'df2': GreaterThan(lower_bound=0.0)}?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. log_prob(value)?

  1. property mean?

  1. rsample(sample_shape=torch.Size([]))?
  2. support = GreaterThan(lower_bound=0.0)?

  1. property variance?

伽瑪

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

Bases: torch.distributions.exp_family.ExponentialFamily

創(chuàng)建通過形狀concentrationrate參數(shù)化的 Gamma 分布。

Example:

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

Parameters

  • 濃度 (python:float tensor)–分布形狀參數(shù)(通常稱為 alpha )
  • 比率 (python:float tensor)–比率= 1 /分布比例(通常稱為 到 beta)

  1. arg_constraints = {'concentration': GreaterThan(lower_bound=0.0), 'rate': GreaterThan(lower_bound=0.0)}?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. log_prob(value)?

  1. property mean?

  1. rsample(sample_shape=torch.Size([]))?
  2. support = GreaterThan(lower_bound=0.0)?

  1. property variance?

幾何

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

Bases: torch.distributions.distribution.Distribution

創(chuàng)建由 probs 參數(shù)化的幾何分布,其中 probs 是伯努利試驗成功的概率。 它表示在img Bernoulli 試驗中,第一個img試驗失敗后才看到成功的可能性。

樣本是非負整數(shù)[0,img)。

Example:

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

Parameters

  • 概率(編號 , tensor)–采樣 <cite>1</cite> 的概率。 必須在范圍內(0,1]
  • 對數(shù)(編號 , tensor)–采樣的對數(shù)奇數(shù) <cite>1</cite> 。

  1. arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?

  1. log_prob(value)?
  2. logits?

  1. property mean?
  2. probs?

  1. sample(sample_shape=torch.Size([]))?
  2. support = IntegerGreaterThan(lower_bound=0)?

  1. property variance?

古貝爾

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

堿基: torch.distributions.transformed_distribution.TransformedDistribution

來自 Gumbel 分布的樣本。

例子:

  1. >>> m = Gumbel(torch.tensor([1.0]), torch.tensor([2.0]))
  2. >>> m.sample() # sample from Gumbel distribution with loc=1, scale=2
  3. tensor([ 1.0124])

Parameters

  • loc (python:float tensor)–分布的位置參數(shù)
  • 標度 (python:float tensor)–分布的標度參數(shù)

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

  1. entropy()?

  1. expand(batch_shape, _instance=None)?

  1. log_prob(value)?

  1. property mean?

  1. property stddev?
  2. support = Real()?

  1. property variance?

半漂亮

  1. class torch.distributions.half_cauchy.HalfCauchy(scale, validate_args=None)?

Bases: torch.distributions.transformed_distribution.TransformedDistribution

創(chuàng)建一個以<cite>標度</cite>為參數(shù)的半正態(tài)分布,其中:

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

Example:

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

Parameters

比例尺 (python:float tensor)–完整柯西分布的比例

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

  1. cdf(value)?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. icdf(prob)?

  1. log_prob(value)?

  1. property mean?

  1. property scale?
  2. support = GreaterThan(lower_bound=0.0)?

  1. property variance?

半普通

  1. 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:

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

Example:

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

Parameters

標度 (python:float tensor)–完全正態(tài)分布的標度

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

  1. cdf(value)?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. icdf(prob)?

  1. log_prob(value)?

  1. property mean?

  1. property scale?
  2. support = GreaterThan(lower_bound=0.0)?

  1. property variance?

獨立

  1. 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)分布(因此它們是可互換的),您可以:

  1. >>> loc = torch.zeros(3)
  2. >>> scale = torch.ones(3)
  3. >>> mvn = MultivariateNormal(loc, scale_tril=torch.diag(scale))
  4. >>> [mvn.batch_shape, mvn.event_shape]
  5. [torch.Size(()), torch.Size((3,))]
  6. >>> normal = Normal(loc, scale)
  7. >>> [normal.batch_shape, normal.event_shape]
  8. [torch.Size((3,)), torch.Size(())]
  9. >>> diagn = Independent(normal, 1)
  10. >>> [diagn.batch_shape, diagn.event_shape]
  11. [torch.Size(()), torch.Size((3,))]

Parameters

  • base_distribution (torch.分發(fā)。分發(fā)。分發(fā))–基本分發(fā)
  • reinterpreted_batch_ndims (python:int )–要重新解釋為事件暗淡的批暗淡數(shù)量

  1. arg_constraints = {}?

  1. entropy()?

  1. enumerate_support(expand=True)?

  1. expand(batch_shape, _instance=None)?

  1. property has_enumerate_support?

  1. property has_rsample?

  1. log_prob(value)?

  1. property mean?

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

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

  1. property support?

  1. property variance?

拉普拉斯

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

Bases: torch.distributions.distribution.Distribution

創(chuàng)建由loc和:attr:'scale'參數(shù)化的拉普拉斯分布。

Example:

  1. >>> m = Laplace(torch.tensor([0.0]), torch.tensor([1.0]))
  2. >>> m.sample() # Laplace distributed with loc=0, scale=1
  3. tensor([ 0.1046])

Parameters

  • loc (python:float tensor)–分布的平均值
  • 規(guī)模 (python:float tensor)–分布規(guī)模

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

  1. cdf(value)?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. icdf(value)?

  1. log_prob(value)?

  1. property mean?

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

  1. property stddev?
  2. support = Real()?

  1. property variance?

對數(shù)正態(tài)

  1. class torch.distributions.log_normal.LogNormal(loc, scale, validate_args=None)?

Bases: torch.distributions.transformed_distribution.TransformedDistribution

創(chuàng)建由 locscale 參數(shù)化的對數(shù)正態(tài)分布,其中:

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

Example:

  1. >>> m = LogNormal(torch.tensor([0.0]), torch.tensor([1.0]))
  2. >>> m.sample() # log-normal distributed with mean=0 and stddev=1
  3. tensor([ 0.1046])

Parameters

  • loc (python:float tensor)–分布對數(shù)的平均值
  • 比例尺 (python:float tensor)–分布對數(shù)的標準偏差

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

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. property loc?

  1. property mean?

  1. property scale?
  2. support = GreaterThan(lower_bound=0.0)?

  1. property variance?

LowRankMultivariateNormal

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

Bases: torch.distributions.distribution.Distribution

使用協(xié)方差矩陣創(chuàng)建具有由cov_factorcov_diag參數(shù)化的低秩形式的多元正態(tài)分布:

  1. covariance_matrix = cov_factor @ cov_factor.T + cov_diag

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

Parameters

  • loc (tensor)–形狀為 <cite>batch_shape + event_shape</cite> 的分布平均值
  • cov_factor (tensor)–形狀為<cite>的協(xié)方差矩陣的低秩形式的因子部分 batch_shape + event_shape +(rank,)</cite>
  • cov_diag (tensor)–形狀為 <cite>batch_shape + event_shape</cite> 的協(xié)方差矩陣的低秩形式的對角線部分

Note

由于伍德伯里矩陣恒等式和 矩陣行列式引理。 由于有了這些公式,我們只需要計算小尺寸“電容”矩陣的行列式和逆式:

  1. capacitance = I + cov_factor.T @ inv(cov_diag) @ cov_factor
  2. arg_constraints = {'cov_diag': GreaterThan(lower_bound=0.0), 'cov_factor': Real(), 'loc': Real()}?
  3. covariance_matrix?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. log_prob(value)?

  1. property mean?
  2. precision_matrix?

  1. rsample(sample_shape=torch.Size([]))?
  2. scale_tril?
  3. support = Real()?
  4. variance?

多項式

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

Bases: torch.distributions.distribution.Distribution

創(chuàng)建由total_countprobslogits (但不是全部)參數(shù)化的多項式分布。 probs 的最內維度在類別上進行索引。 所有其他尺寸均按批次編制索引。

請注意,如果僅調用 log_prob() ,則無需指定total_count(請參見下面的示例)

Note

probs 必須為非負數(shù),有限且總和為非零,并且將其歸一化為 1。

  • 對于所有參數(shù)和樣本, sample() 需要一個共享的 <cite>total_count</cite> 。
  • log_prob() 允許每個參數(shù)和樣本使用不同的 <cite>total_count</cite> 。

Example:

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

Parameters

  • total_count (python:int )–試用次數(shù)
  • probs (Tensor) – event probabilities
  • logits (tensor)–事件日志概率

  1. arg_constraints = {'logits': Real(), 'probs': Simplex()}?

  1. expand(batch_shape, _instance=None)?

  1. log_prob(value)?

  1. property logits?

  1. property mean?

  1. property param_shape?

  1. property probs?

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

  1. property support?

  1. property variance?

多元正態(tài)

  1. 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é)方差矩陣img或正定精度矩陣img或具有正值對角線項的下三角矩陣img來參數(shù)化多元正態(tài)分布,例如img。 該三角矩陣可以通過例如 協(xié)方差的 Cholesky 分解。

Example

  1. >>> m = MultivariateNormal(torch.zeros(2), torch.eye(2))
  2. >>> m.sample() # normally distributed with mean=`[0,0]` and covariance_matrix=`I`
  3. tensor([-0.2102, -0.5429])

Parameters

  • loc (tensor)–分布的均值
  • covariance_matrix (tensor)–正定協(xié)方差矩陣
  • precision_matrix (tensor)–正定精度矩陣
  • scale_tril (tensor)–協(xié)方差的下三角因子,對角線為正值

Note

只能指定 covariance_matrixprecision_matrixscale_tril 中的一個。

使用 scale_tril 會更高效:所有內部計算都基于 scale_tril 。 如果改為通過 covariance_matrixprecision_matrix ,則僅用于使用 Cholesky 分解來計算相應的下三角矩陣。

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

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. log_prob(value)?

  1. property mean?
  2. precision_matrix?

  1. rsample(sample_shape=torch.Size([]))?
  2. scale_tril?
  3. support = Real()?

  1. property variance?

負二項式

  1. 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

  • total_count (python:float tensor)–伯努利試驗陰性的非負數(shù)停止, 盡管該分布對于實際值計數(shù)仍然有效
  • 概率 (tensor)–半開放時間間隔[0,1)成功的事件概率
  • logits (tensor)–成功概率的事件對數(shù)

  1. arg_constraints = {'logits': Real(), 'probs': HalfOpenInterval(lower_bound=0.0, upper_bound=1.0), 'total_count': GreaterThanEq(lower_bound=0)}?

  1. expand(batch_shape, _instance=None)?

  1. log_prob(value)?
  2. logits?

  1. property mean?

  1. property param_shape?
  2. probs?

  1. sample(sample_shape=torch.Size([]))?
  2. support = IntegerGreaterThan(lower_bound=0)?

  1. property variance?

正常

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

Bases: torch.distributions.exp_family.ExponentialFamily

創(chuàng)建由locscale參數(shù)化的正態(tài)(也稱為高斯)分布。

Example:

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

Parameters

  • loc (python:float tensor)–分布的平均值(通常稱為 mu)
  • 標度 (python:float tensor)–分布的標準偏差(通常稱為 sigma )

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

  1. cdf(value)?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. icdf(value)?

  1. log_prob(value)?

  1. property mean?

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

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

  1. property stddev?
  2. support = Real()?

  1. property variance?

熱門分類

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

Bases: torch.distributions.distribution.Distribution

創(chuàng)建由 probslogits 參數(shù)化的單熱點分類分布。

樣本是大小為probs.size(-1)的一鍵編碼矢量。

Note

probs 必須為非負數(shù),有限且總和為非零,并且將其歸一化為 1。

另請參見:torch.distributions.Categorical()了解 probslogits 的規(guī)格。

Example:

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

Parameters

  • probs (Tensor) – event probabilities
  • logits (Tensor) – event log probabilities

  1. arg_constraints = {'logits': Real(), 'probs': Simplex()}?

  1. entropy()?

  1. enumerate_support(expand=True)?

  1. expand(batch_shape, _instance=None)?
  2. has_enumerate_support = True?

  1. log_prob(value)?

  1. property logits?

  1. property mean?

  1. property param_shape?

  1. property probs?

  1. sample(sample_shape=torch.Size([]))?
  2. support = Simplex()?

  1. property variance?

帕累托

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

Bases: torch.distributions.transformed_distribution.TransformedDistribution

來自帕累托 1 型分布的樣本。

Example:

  1. >>> m = Pareto(torch.tensor([1.0]), torch.tensor([1.0]))
  2. >>> m.sample() # sample from a Pareto distribution with scale=1 and alpha=1
  3. tensor([ 1.5623])

Parameters

  • scale (python:float or Tensor) – Scale parameter of the distribution
  • alpha (python:float tensor)–分布的形狀參數(shù)

  1. arg_constraints = {'alpha': GreaterThan(lower_bound=0.0), 'scale': GreaterThan(lower_bound=0.0)}?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?

  1. property mean?

  1. property support?

  1. property variance?

泊松

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

Bases: torch.distributions.exp_family.ExponentialFamily

創(chuàng)建由rate(速率參數(shù))參數(shù)化的泊松分布。

樣本是非負整數(shù),pmf 為

img

Example:

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

Parameters

速率(編號 , tensor)–速率參數(shù)

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

  1. expand(batch_shape, _instance=None)?

  1. log_prob(value)?

  1. property mean?

  1. sample(sample_shape=torch.Size([]))?
  2. support = IntegerGreaterThan(lower_bound=0)?

  1. property variance?

輕松的伯努利

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

Bases: torch.distributions.transformed_distribution.TransformedDistribution

創(chuàng)建由 temperature 以及 probslogits (但不是全部)參數(shù)化的 RelaxedBernoulli 分布。 這是 <cite>Bernoulli</cite> 分布的寬松版本,因此值在(0,1)中,并且具有可重新設置參數(shù)的樣本。

Example:

  1. >>> m = RelaxedBernoulli(torch.tensor([2.2]),
  2. torch.tensor([0.1, 0.2, 0.3, 0.99]))
  3. >>> m.sample()
  4. tensor([ 0.2951, 0.3442, 0.8918, 0.9021])

Parameters

  • 溫度 (tensor)–松弛溫度
  • probs (Number__, Tensor) – the probability of sampling <cite>1</cite>
  • logits (Number__, Tensor) – the log-odds of sampling <cite>1</cite>

  1. arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. property logits?

  1. property probs?
  2. support = Interval(lower_bound=0.0, upper_bound=1.0)?

  1. property temperature?

LogitRelaxed 伯努利

  1. class torch.distributions.relaxed_bernoulli.LogitRelaxedBernoulli(temperature, probs=None, logits=None, validate_args=None)?

Bases: torch.distributions.distribution.Distribution

創(chuàng)建一個以 probslogits (但不是兩者)為參數(shù)的 LogitRelaxedBernoulli 分布,這是 RelaxedBernoulli 分布的對數(shù)。

樣本是(0,1)中值的對數(shù)。 有關更多詳細信息,請參見[1]。

Parameters

  • temperature (Tensor) – relaxation temperature
  • probs (Number__, Tensor) – the probability of sampling <cite>1</cite>
  • logits (Number__, Tensor) – the log-odds of sampling <cite>1</cite>

[1]具體分布:離散隨機變量的連續(xù)松弛(Maddison 等,2017)

[2]使用 Gumbel-Softmax 分類重新參數(shù)化(Jang 等,2017)

  1. arg_constraints = {'logits': Real(), 'probs': Interval(lower_bound=0.0, upper_bound=1.0)}?

  1. expand(batch_shape, _instance=None)?

  1. log_prob(value)?
  2. logits?

  1. property param_shape?
  2. probs?

  1. rsample(sample_shape=torch.Size([]))?
  2. support = Real()?

RelaxedOneHot 分類

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

Bases: torch.distributions.transformed_distribution.TransformedDistribution

創(chuàng)建一個由 temperature 以及 probslogits 設置參數(shù)的 RelaxedOneHotCategorical 分布。 這是OneHotCategorical發(fā)行版的寬松版本,因此其示例位于單純形上,并且可以重新設置參數(shù)。

Example:

  1. >>> m = RelaxedOneHotCategorical(torch.tensor([2.2]),
  2. torch.tensor([0.1, 0.2, 0.3, 0.4]))
  3. >>> m.sample()
  4. tensor([ 0.1294, 0.2324, 0.3859, 0.2523])

Parameters

  • temperature (Tensor) – relaxation temperature
  • probs (Tensor) – event probabilities
  • 對數(shù) (tensor)–每個事件的對數(shù)概率。

  1. arg_constraints = {'logits': Real(), 'probs': Simplex()}?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. property logits?

  1. property probs?
  2. support = Simplex()?

  1. property temperature?

學生 T

  1. 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:

  1. >>> m = StudentT(torch.tensor([2.0]))
  2. >>> m.sample() # Student's t-distributed with degrees of freedom=2
  3. tensor([ 0.1046])

Parameters

  • df (python:float tensor)–自由度
  • loc (python:float or Tensor) – mean of the distribution
  • scale (python:float or Tensor) – scale of the distribution

  1. arg_constraints = {'df': GreaterThan(lower_bound=0.0), 'loc': Real(), 'scale': GreaterThan(lower_bound=0.0)}?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. log_prob(value)?

  1. property mean?

  1. rsample(sample_shape=torch.Size([]))?
  2. support = Real()?

  1. property variance?

轉換分布

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

Bases: torch.distributions.distribution.Distribution

分發(fā)類的擴展,該類將一系列 Transforms 應用于基本分發(fā)。 令 f 為所應用轉換的組成:

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

注意, TransformedDistribution.event_shape是其基本分布及其變換的最大形狀,因為變換可以在事件之間引入相關性。

TransformedDistribution 的用法示例為:

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

有關更多示例,請查看 Gumbel , HalfCauchyHalfNormal ,LogNormal 的實現(xiàn), Pareto , Weibull , RelaxedBernoulliRelaxedOneHotCategorical

  1. arg_constraints = {}?

  1. cdf(value)?

通過反轉變換并計算基本分布的分數(shù)來計算累積分布函數(shù)。

  1. expand(batch_shape, _instance=None)?

  1. property has_rsample?

  1. icdf(value)?

使用變換計算逆累積分布函數(shù),并計算基本分布的分數(shù)。

  1. log_prob(value)?

通過反轉變換對樣本進行評分,并使用基本分布的分數(shù)和 log abs det jacobian 計算分數(shù)。

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

如果分配了分布參數(shù),則生成一個 sample_shape 形狀的重新參數(shù)化樣本或 sample_shape 形狀的一批重新參數(shù)化樣本。 首先從基本分布中采樣,并對列表中的每個變換應用 <cite>transform()</cite>。

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

如果分配參數(shù)是批處理的,則生成 sample_shape 形狀的樣本或 sample_shape 形狀的樣本批。 首先從基本分布中采樣,并對列表中的每個變換應用 <cite>transform()</cite>。

  1. property support?

制服

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

Bases: torch.distributions.distribution.Distribution

從半開間隔[low, high)生成均勻分布的隨機樣本。

Example:

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

Parameters

  • (python:float tensor)–較低范圍(含)。
  • (python:float tensor)–上限(不包括)。

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

  1. cdf(value)?

  1. entropy()?

  1. expand(batch_shape, _instance=None)?
  2. has_rsample = True?

  1. icdf(value)?

  1. log_prob(value)?

  1. property mean?

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

  1. property stddev?

  1. property support?

  1. property variance?

威布爾

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

Bases: torch.distributions.transformed_distribution.TransformedDistribution

來自兩參數(shù)威布爾分布的樣本。

Example

  1. >>> m = Weibull(torch.tensor([1.0]), torch.tensor([1.0]))
  2. >>> m.sample() # sample from a Weibull distribution with scale=1, concentration=1
  3. tensor([ 0.4784])

Parameters

  • 標度 (python:float tensor)–分布的標度參數(shù)(lambda)。
  • 濃度 (python:float tensor)–濃度的濃度參數(shù)(k /形狀)。

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

  1. entropy()?

  1. expand(batch_shape, _instance=None)?

  1. property mean?
  2. support = GreaterThan(lower_bound=0.0)?

  1. property variance?

<cite>KL 發(fā)散</cite>

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

計算兩個分布之間的 Kullback-Leibler 散度img。

img

Parameters

  • p (分布)–一個Distribution對象。
  • q (分布)–一個Distribution對象。

Returns

形狀為 <cite>batch_shape</cite> 的一批 KL 散度。

返回類型

張量

  1. Raises

NotImplementedError -如果尚未通過 register_kl() 注冊分發(fā)類型。

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

裝飾器向 kl_divergence() 注冊成對功能。 用法:

  1. @register_kl(Normal, Normal)
  2. def kl_normal_normal(p, q):
  3. # insert implementation here

查找返回按子類排序的最特定的(類型,類型)匹配。 如果匹配不明確,則會引發(fā) <cite>RuntimeWarning</cite> 。 例如解決模棱兩可的情況:

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

您應該注冊第三個最具體的實現(xiàn),例如:

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

Parameters

  • type_p (python:type )– Distribution的子類。
  • type_q (python:type )– Distribution的子類。

<cite>轉換</cite>

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

可計算 log det jacobians 的可逆轉換的抽象類。 它們主要用于torch.distributions.TransformedDistribution中。

高速緩存對于反變換代價昂貴或數(shù)值不穩(wěn)定的變換非常有用。 請注意,記住的值必須小心,因為自動刻度圖可能會顛倒。 例如,在以下情況下可以使用或不使用緩存:

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

但是,由于依賴關系反轉,在緩存時以下內容將出錯:

  1. y = t(x)
  2. z = t.inv(y)
  3. 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。

  1. Variables

  • ?Transform.domain (Constraint)–表示此變換有效輸入的約束。
  • ?Transform.codomain (Constraint)–表示此變換的有效輸出的約束,該約束是逆變換的輸入。
  • ?Transform.bijective (bool )–此變換是否為雙射的。 對于域中的每個x和共域中的y,變換t是雙射 iff t.inv(t(x)) == xt(t.inv(y)) == y。 非雙射的變換至少應保持較弱的偽逆特性t(t.inv(t(x)) == t(x)t.inv(t(t.inv(y))) == t.inv(y)。
  • ?Transform.sign (python:int tensor)–對于雙射單變量變換,應為 +1 或-1,取決于變換是單調遞增還是遞減。
  • ?Transform.event_dim (python:int )–在變換event_shape中相互關聯(lián)的維數(shù)。 對于逐點變換,應為 0;對于聯(lián)合作用于矢量的變換,應為 1;對于聯(lián)合作用于矩陣的變換,其應為 2。

  1. property inv?

返回此變換的逆數(shù) Transform 。 這應該滿足t.inv.inv is t。

  1. property sign?

返回雅可比行列式的符號(如果適用)。 通常,這僅對雙射變換有意義。

  1. log_abs_det_jacobian(x, y)?

計算 log det jacobian <cite>log | dy / dx |</cite> 給定輸入和輸出。

  1. class torch.distributions.transforms.ComposeTransform(parts)?

組成一個鏈中的多個變換。 組成的轉換負責緩存。

Parameters

部分 (Transform 的列表)–組成變換的列表。

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

通過映射img進行轉換。

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

通過映射img進行轉換。

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

通過映射imgimg進行轉換。

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

通過映射img進行轉換。

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

通過逐點仿射映射img進行變換。

Parameters

  • loc (tensor python:float )–位置參數(shù)。
  • 標度 (tensor python:float )–標度參數(shù)。
  • event_dim (python:int )– <cite>event_shape</cite> 的可選大小。 對于單變量隨機變量,它應該為零;對于矢量的分布,它應該為 1;對于矩陣的分布,它應該為 2。

  1. class torch.distributions.transforms.SoftmaxTransform(cache_size=0)?

通過img從不受約束的空間轉換為單純形,然后進行規(guī)范化。

這不是雙射的,不能用于 HMC。 但是,這主要是按坐標方式(最終歸一化除外),因此適用于按坐標優(yōu)化算法。

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

通過不折斷的過程將不受約束的空間轉換為一維的單純形。

該變換以 <cite>Dirichlet</cite> 分布的小節(jié)結構中的迭代 S 形變換形式出現(xiàn):第一個 logit 通過 S 形變換成第一個概率和所有其他概率,然后過程重復進行。

這是雙射的,適合在 HMC 中使用; 但是,它將坐標混合在一起,不太適合優(yōu)化。

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

從非約束矩陣轉換為具有非負對角線項的低三角形矩陣。

這對于根據(jù)正定矩陣的 Cholesky 因式分解參數(shù)化很有用。

  1. class torch.distributions.transforms.CatTransform(tseq, dim=0, lengths=None)?

變換函子,以與兼容的方式,以長度,長度[dim] 為單位,將每個分量的 tseq 變換序列應用于每個子矩陣。 ] torch.cat() 。

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

  1. class torch.distributions.transforms.StackTransform(tseq, dim=0)?

變換函子,以與 torch.stack() 兼容的方式,對<cite>暗淡</cite>處的每個子矩陣按分量進行變換 <cite>tseq</cite> 的序列。

  1. Example::

x = torch.stack([torch.range(1,10),torch.range(1,10)],dim = 1)t = StackTransform([ExpTransform(),identity_transform],dim = 1)y = t (X)

<cite>約束</cite>

實現(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

  1. class torch.distributions.constraints.Constraint?

約束的抽象基類。

約束對象表示變量有效的區(qū)域,例如 在其中可以優(yōu)化變量。

  1. check(value)?

返回 <cite>sample_shape + batch_shape</cite> 的字節(jié)張量,指示值中的每個事件是否滿足此約束。

  1. torch.distributions.constraints.dependent_property?

torch.distributions.constraints._DependentProperty的別名

  1. torch.distributions.constraints.integer_interval?

torch.distributions.constraints._IntegerInterval的別名

  1. torch.distributions.constraints.greater_than?

torch.distributions.constraints._GreaterThan的別名

  1. torch.distributions.constraints.greater_than_eq?

torch.distributions.constraints._GreaterThanEq的別名

  1. torch.distributions.constraints.less_than?

torch.distributions.constraints._LessThan的別名

  1. torch.distributions.constraints.interval?

torch.distributions.constraints._Interval的別名

  1. torch.distributions.constraints.half_open_interval?

torch.distributions.constraints._HalfOpenInterval的別名

  1. torch.distributions.constraints.cat?

torch.distributions.constraints._Cat的別名

  1. torch.distributions.constraints.stack?

torch.distributions.constraints._Stack的別名

<cite>約束注冊表</cite>

PyTorch 提供了兩個全局 ConstraintRegistry 對象,這些對象將 Constraint 對象鏈接到 Transform 對象。 這些對象既有輸入約束又有返回變換,但對雙射性有不同的保證。

  1. biject_to(constraint)查找從constraints.real到給定constraint的雙射 Transform 。 保證返回的轉換具有.bijective = True并應實現(xiàn).log_abs_det_jacobian()。
  2. 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)化算法:

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

biject_to()注冊表對于哈密頓量的蒙特卡洛很有用,其中在.support約束下的概率分布中的樣本在不受約束的空間中傳播,并且算法通常是旋轉不變的。

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

Note

transform_tobiject_to不同的一個例子是constraints.simplextransform_to(constraints.simplex)返回一個 SoftmaxTransform ,該輸入簡單地對輸入進行指數(shù)化和歸一化; 這是一種便宜的方法,通常適合于像 SVI 這樣的算法進行協(xié)調操作。 相反,biject_to(constraints.simplex)返回一個 StickBreakingTransform ,它將其輸入降低到一維空間; 這是一個更昂貴的數(shù)字穩(wěn)定度較低的變換,但對于 HMC 這樣的算法來說是必需的。

可以通過用戶定義的約束擴展biject_totransform_to對象,并使用它們的.register()方法將其轉換為單例約束的函數(shù):

  1. transform_to.register(my_constraint, my_transform)

或作為參數(shù)約束的修飾器:

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

您可以通過創(chuàng)建新的 ConstraintRegistry 對象來創(chuàng)建自己的注冊表。

  1. class torch.distributions.constraint_registry.ConstraintRegistry?

注冊表將約束鏈接到轉換。

  1. register(constraint, factory=None)?

在此注冊表中注冊 Constraint 子類。 用法:

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

Parameters

  • 約束 (Constraint 的子類)– Constraint 的子類,或所需類的單例對象。
  • factory (可調用)–輸入約束對象并返回 Transform 對象的可調用對象。
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號