分類分布。
繼承自:Distribution
用法
tf.compat.v1.distributions.Categorical(
logits=None, probs=None, dtype=tf.dtypes.int32, validate_args=False,
allow_nan_stats=True, name='Categorical'
)
參數
-
logits
一個 N-DTensor
,N >= 1
,表示一組分類分布的對數概率。第一個N - 1
維度索引到一批獨立分布,最後一個維度表示每個類的 logits 向量。隻應傳入logits
或probs
之一。 -
probs
N-DTensor
,N >= 1
,表示一組分類分布的概率。第一個N - 1
維度索引成一批獨立分布,最後一個維度表示每個類的概率向量。隻應傳入logits
或probs
之一。 -
dtype
事件樣本的類型(默認值:int32)。 -
validate_args
Pythonbool
,默認False
。盡管可能會降低運行時性能,但檢查True
分發參數的有效性時。當False
無效輸入可能會默默呈現不正確的輸出。 -
allow_nan_stats
Pythonbool
,默認True
。當True
時,統計信息(例如,均值、眾數、方差)使用值“NaN
”來指示結果未定義。當False
時,如果一個或多個統計數據的批處理成員未定義,則會引發異常。 -
name
Pythonstr
名稱以此類創建的 Ops 為前綴。
屬性
-
allow_nan_stats
Pythonbool
說明未定義統計信息時的行為。統計數據在有意義時返回 +/- 無窮大。例如,柯西分布的方差是無窮大的。但是,有時統計數據是未定義的,例如,如果分布的 pdf 在分布的支持範圍內沒有達到最大值,則模式是未定義的。如果均值未定義,則根據定義,方差未定義。例如: df = 1 的 Student's T 的平均值是未定義的(沒有明確的方式說它是 + 或 - 無窮大),因此方差 = E[(X - mean)**2] 也是未定義的。
-
batch_shape
來自單個事件索引的單個樣本的形狀作為TensorShape
.可能部分定義或未知。
批次維度是該分布的獨立、不同參數化的索引。
-
dtype
Tensor
的DType
由此Distribution
處理。 -
event_shape
單個批次的單個樣品的形狀作為TensorShape
.可能部分定義或未知。
-
event_size
標量int32
張量:類的數量。 -
logits
坐標邏輯的向量。 -
name
此Distribution
創建的所有操作前的名稱。 -
parameters
用於實例化此Distribution
的參數字典。 -
probs
坐標概率向量。 -
reparameterization_type
說明如何重新參數化分布中的樣本。目前這是靜態實例
distributions.FULLY_REPARAMETERIZED
或distributions.NOT_REPARAMETERIZED
之一。 -
validate_args
Pythonbool
表示啟用了可能昂貴的檢查。
分類分布由一組K
類的概率或log-probabilities 參數化。它是在整數 {0, 1, ..., K}
上定義的。
分類分布與OneHotCategorical
和Multinomial
分布密切相關。分類分布可以直觀地理解為根據 argmax{ OneHotCategorical(probs) }
本身與 argmax{ Multinomial(probs, total_count=1) }
相同來生成樣本。
數學細節
概率質量函數 (pmf) 是,
pmf(k; pi) = prod_j pi_j**[k == j]
陷阱
類數 K
不得超過:
self.dtype
可表示的最大整數,即2**(mantissa_bits+1)
(IEEE 754),- 最大
Tensor
索引,即2**31-1
。
換一種說法,
K <= min(2**31-1, {
tf.float16:2**11,
tf.float32:2**24,
tf.float64:2**53 }[param.dtype])
注意:此條件僅在 self.validate_args = True
時有效。
例子
創建一個 3 類分布,其中 2 類最有可能。
dist = Categorical(probs=[0.1, 0.5, 0.4])
n = 1e4
empirical_prob = tf.cast(
tf.histogram_fixed_width(
dist.sample(int(n)),
[0., 2],
nbins=3),
dtype=tf.float32) / n
# ==> array([ 0.1005, 0.5037, 0.3958], dtype=float32)
創建一個 3 類分布,其中 2 類最有可能。由 logits 而不是概率參數化。
dist = Categorical(logits=np.log([0.1, 0.5, 0.4])
n = 1e4
empirical_prob = tf.cast(
tf.histogram_fixed_width(
dist.sample(int(n)),
[0., 2],
nbins=3),
dtype=tf.float32) / n
# ==> array([0.1045, 0.5047, 0.3908], dtype=float32)
創建一個 3 類分布,其中 3 類最有可能。分布函數可以根據計數進行評估。
# counts is a scalar.
p = [0.1, 0.4, 0.5]
dist = Categorical(probs=p)
dist.prob(0) # Shape []
# p will be broadcast to [[0.1, 0.4, 0.5], [0.1, 0.4, 0.5]] to match counts.
counts = [1, 0]
dist.prob(counts) # Shape [2]
# p will be broadcast to shape [3, 5, 7, 3] to match counts.
counts = [[...]] # Shape [5, 7, 3]
dist.prob(counts) # Shape [5, 7, 3]
相關用法
- Python tf.compat.v1.distributions.Categorical.stddev用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.variance用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.cdf用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.covariance用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.log_cdf用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.cross_entropy用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.log_survival_function用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.survival_function用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.kl_divergence用法及代碼示例
- Python tf.compat.v1.distributions.Categorical.quantile用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.covariance用法及代碼示例
- Python tf.compat.v1.distributions.Normal.log_survival_function用法及代碼示例
- Python tf.compat.v1.distributions.Laplace.stddev用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.quantile用法及代碼示例
- Python tf.compat.v1.distributions.Uniform.log_survival_function用法及代碼示例
- Python tf.compat.v1.distributions.Gamma.cdf用法及代碼示例
- Python tf.compat.v1.distributions.Normal.log_cdf用法及代碼示例
- Python tf.compat.v1.distributions.Gamma.log_cdf用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.distributions.Categorical。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。