一個通用的概率分布基類。
用法
tf.compat.v1.distributions.Distribution(
    dtype, reparameterization_type, validate_args, allow_nan_stats, parameters=None,
    graph_parents=None, name=None
)參數
- 
dtype事件樣本的類型。None表示沒有 type-enforcement。
- 
reparameterization_typeReparameterizationType的實例。如果distributions.FULLY_REPARAMETERIZED,則此Distribution可以根據某些標準分布重新參數化,該函數的雅可比為常數以支持標準分布。如果distributions.NOT_REPARAMETERIZED,則沒有這樣的重新參數化可用。
- 
validate_argsPythonbool,默認False。盡管可能會降低運行時性能,但檢查True分發參數的有效性時。當False無效輸入可能會默默呈現不正確的輸出。
- 
allow_nan_statsPythonbool,默認True。當True時,統計信息(例如,均值、眾數、方差)使用值“NaN”來指示結果未定義。當False時,如果一個或多個統計數據的批處理成員未定義,則會引發異常。
- 
parametersPythondict用於實例化此Distribution的參數。
- 
graph_parents此Distribution的圖形先決條件的 Pythonlist。
- 
namePythonstr名稱以此類創建的 Ops 為前綴。默認值:子類名稱。
拋出
- 
ValueError如果 graph_parents 的任何成員是None或不是Tensor。
屬性
- 
allow_nan_statsPythonbool說明未定義統計信息時的行為。統計數據在有意義時返回 +/- 無窮大。例如,柯西分布的方差是無窮大的。但是,有時統計數據是未定義的,例如,如果分布的 pdf 在分布的支持範圍內沒有達到最大值,則模式是未定義的。如果均值未定義,則根據定義,方差未定義。例如: df = 1 的 Student's T 的平均值是未定義的(沒有明確的方式說它是 + 或 - 無窮大),因此方差 = E[(X - mean)**2] 也是未定義的。 
- 
batch_shape來自單個事件索引的單個樣本的形狀作為TensorShape.可能部分定義或未知。 批次維度是該分布的獨立、不同參數化的索引。 
- 
dtypeTensor的DType由此Distribution處理。
- 
event_shape單個批次的單個樣品的形狀作為TensorShape.可能部分定義或未知。 
- 
name此Distribution創建的所有操作前的名稱。
- 
parameters用於實例化此Distribution的參數字典。
- 
reparameterization_type說明如何重新參數化分布中的樣本。目前這是靜態實例 distributions.FULLY_REPARAMETERIZED或distributions.NOT_REPARAMETERIZED之一。
- 
validate_argsPythonbool表示啟用了可能昂貴的檢查。
Distribution 是用於構建和組織隨機變量(例如,伯努利、高斯)的屬性(例如,均值、方差)的基類。
子類化
子類應實現same-named 函數的leading-underscore 版本。除了省略 name="..." 之外,參數簽名應該相同。例如,要啟用 log_prob(value,
name="log_prob") 子類應該實現 _log_prob(value) 。
子類可以通過為其方法特化提供文檔字符串來附加到public-level 文檔字符串。例如:
@util.AppendDocstring("Some other details.")
def _log_prob(self, value):
  ...將添加字符串“一些其他詳細信息”。到log_prob 函數文檔字符串。這是作為一個簡單的裝飾器實現的,以避免 python linter 抱怨部分文檔字符串中缺少 Args/Returns/Raises 部分。
廣播、批處理和形狀
所有發行版都支持該類型的成批獨立發行版。批量形狀是通過一起廣播參數來確定的。
__init__ ,  cdf ,  log_cdf ,  prob 和 log_prob 的參數形狀反映了這種廣播,sample 和 sample_n 的返回值也是如此。
sample_n_shape = [n] + batch_shape + event_shape ,其中 sample_n_shape 是從 sample_n ,  n 返回的 Tensor 的形狀,是樣本數,batch_shape 定義有多少個獨立分布,而 event_shape 定義每個樣本的形狀那些獨立的分布。樣本在 batch_shape 維度上是獨立的,但在 event_shape 維度上不一定如此(取決於基礎分布的細節)。
以Uniform 分布為例:
minval = 3.0
maxval = [[4.0, 6.0],
          [10.0, 12.0]]
# Broadcasting:
# This instance represents 4 Uniform distributions. Each has a lower bound at
# 3.0 as the `minval` parameter was broadcasted to match `maxval`'s shape.
u = Uniform(minval, maxval)
# `event_shape` is `TensorShape([])`.
event_shape = u.event_shape
# `event_shape_t` is a `Tensor` which will evaluate to [].
event_shape_t = u.event_shape_tensor()
# Sampling returns a sample per distribution. `samples` has shape
# [5, 2, 2], which is [n] + batch_shape + event_shape, where n=5,
# batch_shape=[2, 2], and event_shape=[].
samples = u.sample_n(5)
# The broadcasting holds across methods. Here we use `cdf` as an example. The
# same holds for `log_cdf` and the likelihood functions.
# `cum_prob` has shape [2, 2] as the `value` argument was broadcasted to the
# shape of the `Uniform` instance.
cum_prob_broadcast = u.cdf(4.0)
# `cum_prob`'s shape is [2, 2], one per distribution. No broadcasting
# occurred.
cum_prob_per_dist = u.cdf([[4.0, 5.0],
                           [6.0, 7.0]])
# INVALID as the `value` argument is not broadcastable to the distribution's
# shape.
cum_prob_invalid = u.cdf([4.0, 5.0, 6.0])形狀
與 TensorFlow 分布形狀相關的三個重要概念:
- 事件形狀說明了從分布中單次抽取的形狀;它可能依賴於各個維度。對於標量分布,事件形狀為 []。對於 5 維 MultivariateNormal,事件形狀為[5]。
- 批量形狀說明了獨立的、不同分布的繪製,也就是 "collection" 或 "bunch" 的分布。
- 樣本形狀說明了來自分布族的獨立、相同分布的批次抽取。
事件形狀和批處理形狀是 Distribution 對象的屬性,而示例形狀與對 sample 或 log_prob 的特定調用相關聯。
有關 TensorFlow Distributions 形狀的詳細使用示例,請參閱本教程
導致未定義統計或分布的參數值。
某些分布沒有針對所有初始化參數值的明確定義的統計信息。例如,beta 分布由正實數 concentration1 和 concentration0 參數化,如果 concentration1 < 1 或 concentration0 < 1 則沒有明確定義的模式。
用戶可以選擇引發異常或返回 NaN 。
a = tf.exp(tf.matmul(logits, weights_a))
b = tf.exp(tf.matmul(logits, weights_b))
# Will raise exception if ANY batch member has a < 1 or b < 1.
dist = distributions.beta(a, b, allow_nan_stats=False)
mode = dist.mode().eval()
# Will return NaN for batch members with either a < 1 or b < 1.
dist = distributions.beta(a, b, allow_nan_stats=True)  # Default behavior
mode = dist.mode().eval()在所有情況下,如果傳遞了無效參數,則會引發異常,例如
# Will raise an exception if any Op is run.
negative_a = -1.0 * a  # beta distribution by definition has a > 0.
dist = distributions.beta(negative_a, b, allow_nan_stats=True)
dist.mean().eval()相關用法
- Python tf.compat.v1.distributions.Distribution.variance用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.cdf用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.quantile用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.log_cdf用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.survival_function用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.log_survival_function用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.covariance用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.stddev用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.cross_entropy用法及代碼示例
- Python tf.compat.v1.distributions.Distribution.kl_divergence用法及代碼示例
- Python tf.compat.v1.distributions.Dirichlet.covariance用法及代碼示例
- Python tf.compat.v1.distributions.Dirichlet.stddev用法及代碼示例
- Python tf.compat.v1.distributions.Dirichlet.kl_divergence用法及代碼示例
- Python tf.compat.v1.distributions.Dirichlet.cross_entropy用法及代碼示例
- Python tf.compat.v1.distributions.DirichletMultinomial.log_survival_function用法及代碼示例
- Python tf.compat.v1.distributions.DirichletMultinomial.survival_function用法及代碼示例
- Python tf.compat.v1.distributions.DirichletMultinomial.quantile用法及代碼示例
- Python tf.compat.v1.distributions.Dirichlet.cdf用法及代碼示例
- Python tf.compat.v1.distributions.Dirichlet.survival_function用法及代碼示例
- Python tf.compat.v1.distributions.DirichletMultinomial.log_cdf用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.distributions.Distribution。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
