當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.compat.v1.distributions.StudentT用法及代碼示例

學生的 t 分布。

繼承自:Distribution

用法

tf.compat.v1.distributions.StudentT(
    df, loc, scale, validate_args=False, allow_nan_stats=True,
    name='StudentT'
)

參數

  • df 浮點 Tensor 。分布的自由度。 df 必須隻包含正值。
  • loc 浮點 Tensor 。分布的平均值。
  • scale 浮點 Tensor 。分布的比例因子。請注意,scale 在技術上不是此分布的標準差,但其語義更類似於標準差而不是方差。
  • validate_args Python bool ,默認 False 。盡管可能會降低運行時性能,但檢查 True 分發參數的有效性時。當False 無效輸入可能會默默呈現不正確的輸出。
  • allow_nan_stats Python bool ,默認 True 。當 True 時,統計信息(例如,均值、眾數、方差)使用值“NaN”來指示結果未定義。當 False 時,如果一個或多個統計數據的批處理成員未定義,則會引發異常。
  • name Python str 名稱以此類創建的 Ops 為前綴。

拋出

  • TypeError 如果 loc 和 scale 是不同的數據類型。

屬性

  • allow_nan_stats Pythonbool說明未定義統計信息時的行為。

    統計數據在有意義時返回 +/- 無窮大。例如,柯西分布的方差是無窮大的。但是,有時統計數據是未定義的,例如,如果分布的 pdf 在分布的支持範圍內沒有達到最大值,則模式是未定義的。如果均值未定義,則根據定義,方差未定義。例如: df = 1 的 Student's T 的平均值是未定義的(沒有明確的方式說它是 + 或 - 無窮大),因此方差 = E[(X - mean)**2] 也是未定義的。

  • batch_shape 來自單個事件索引的單個樣本的形狀作為TensorShape.

    可能部分定義或未知。

    批次維度是該分布的獨立、不同參數化的索引。

  • df 這些學生 t 分布的自由度。
  • dtype TensorDType 由此 Distribution 處理。
  • event_shape 單個批次的單個樣品的形狀作為TensorShape.

    可能部分定義或未知。

  • loc 這些學生 t 分布的位置。
  • name Distribution 創建的所有操作前的名稱。
  • parameters 用於實例化此 Distribution 的參數字典。
  • reparameterization_type 說明如何重新參數化分布中的樣本。

    目前這是靜態實例 distributions.FULLY_REPARAMETERIZEDdistributions.NOT_REPARAMETERIZED 之一。

  • scale 這些學生 t 分布的比例因子。
  • validate_args Python bool 表示啟用了可能昂貴的檢查。

此分布具有參數:自由度 df 、位置 locscale

數學細節

概率密度函數 (pdf) 是,

pdf(x; df, mu, sigma) = (1 + y**2 / df)**(-0.5 (df + 1)) / Z
where,
y = (x - mu) / sigma
Z = abs(sigma) sqrt(df pi) Gamma(0.5 df) / Gamma(0.5 (df + 1))

其中:

  • loc = mu ,
  • scale = sigma ,並且,
  • Z 是歸一化常數,並且,
  • Gamma 是伽瑪​​函數。

StudentT 分布是location-scale 係列的成員,即它可以構造為,

X ~ StudentT(df, loc=0, scale=1)
Y = loc + scale * X

請注意,scale 的語義更類似於標準差而不是方差。然而,它實際上不是標準。偏差;學生的 t 分布標準。開發。是 scale sqrt(df / (df - 2))df > 2

該分布的樣本被重新參數化(路徑可微)。導數是使用 (Figurnov et al., 2018) 中說明的方法計算的。

例子

一個或一批分布的初始化示例。

import tensorflow_probability as tfp
tfd = tfp.distributions

# Define a single scalar Student t distribution.
single_dist = tfd.StudentT(df=3)

# Evaluate the pdf at 1, returning a scalar Tensor.
single_dist.prob(1.)

# Define a batch of two scalar valued Student t's.
# The first has degrees of freedom 2, mean 1, and scale 11.
# The second 3, 2 and 22.
multi_dist = tfd.StudentT(df=[2, 3], loc=[1, 2.], scale=[11, 22.])

# Evaluate the pdf of the first distribution on 0, and the second on 1.5,
# returning a length two tensor.
multi_dist.prob([0, 1.5])

# Get 3 samples, returning a 3 x 2 tensor.
multi_dist.sample(3)

盡可能廣播參數。

# Define a batch of two Student's t distributions.
# Both have df 2 and mean 1, but different scales.
dist = tfd.StudentT(df=2, loc=1, scale=[11, 22.])

# Evaluate the pdf of both distributions on the same point, 3.0,
# returning a length 2 tensor.
dist.prob(3.0)

計算樣本的梯度 w.r.t.參數:

df = tf.constant(2.0)
loc = tf.constant(2.0)
scale = tf.constant(11.0)
dist = tfd.StudentT(df=df, loc=loc, scale=scale)
samples = dist.sample(5)  # Shape [5]
loss = tf.reduce_mean(tf.square(samples))  # Arbitrary loss function
# Unbiased stochastic gradients of the loss function
grads = tf.gradients(loss, [df, loc, scale])

參考:

隱式重新參數化梯度:Figurnov 等人,2018 (pdf)

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.distributions.StudentT。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。