從均勻分布中輸出隨機值。
用法
tf.random.uniform(
shape, minval=0, maxval=None, dtype=tf.dtypes.float32, seed=None, name=None
)參數
-
shape一維整數張量或 Python 數組。輸出張量的形狀。 -
minvaldtype類型的張量或 Python 值,可使用shape廣播(對於整數類型,不支持廣播,因此它需要是標量)。要生成的隨機值範圍的下限(包括)。默認為 0。 -
maxvaldtype類型的張量或 Python 值,可使用shape廣播(對於整數類型,不支持廣播,因此它需要是標量)。要生成的隨機值範圍的上限(不包括)。如果dtype是浮點數,則默認為 1。 -
dtype輸出的類型:float16,bfloat16,float32,float64,int32或int64。默認為float32。 -
seed一個 Python 整數。與tf.random.set_seed結合使用,在多個調用中創建可重現的張量序列。 -
name操作的名稱(可選)。
返回
- 用隨機統一值填充的指定形狀的張量。
拋出
-
ValueError如果dtype是整數且未指定maxval。
生成的值遵循 [minval, maxval) 範圍內的均勻分布。下限 minval 包含在範圍內,而上限 maxval 不包括在內。
對於浮點數,默認範圍是 [0, 1) 。對於整數,至少必須明確指定maxval。
在整數情況下,隨機整數略有偏差,除非 maxval - minval 是 2 的精確冪。對於明顯小於輸出範圍(2**32 或 2**64)的 maxval - minval 的值,偏差很小。
例子:
tf.random.uniform(shape=[2])
<tf.Tensor:shape=(2,), dtype=float32, numpy=array([..., ...], dtype=float32)>
tf.random.uniform(shape=[], minval=-1., maxval=0.)
<tf.Tensor:shape=(), dtype=float32, numpy=-...>
tf.random.uniform(shape=[], minval=5, maxval=10, dtype=tf.int64)
<tf.Tensor:shape=(), dtype=int64, numpy=...>
seed 參數在多個調用中產生一個確定的張量序列。要重複該序列,請使用 tf.random.set_seed :
tf.random.set_seed(5)
tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
<tf.Tensor:shape=(), dtype=int32, numpy=2>
tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
<tf.Tensor:shape=(), dtype=int32, numpy=0>
tf.random.set_seed(5)
tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
<tf.Tensor:shape=(), dtype=int32, numpy=2>
tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
<tf.Tensor:shape=(), dtype=int32, numpy=0>
如果沒有 tf.random.set_seed 但指定了 seed 參數,對函數圖或先前執行的操作的微小更改將更改返回值。有關詳細信息,請參閱tf.random.set_seed。
相關用法
- Python tf.random.truncated_normal用法及代碼示例
- Python tf.random.stateless_uniform用法及代碼示例
- Python tf.random.Generator用法及代碼示例
- Python tf.random.Generator.binomial用法及代碼示例
- Python tf.random.shuffle用法及代碼示例
- Python tf.random.stateless_parameterized_truncated_normal用法及代碼示例
- Python tf.random.normal用法及代碼示例
- Python tf.random.experimental.stateless_split用法及代碼示例
- Python tf.random.stateless_poisson用法及代碼示例
- Python tf.random.set_global_generator用法及代碼示例
- Python tf.random.categorical用法及代碼示例
- Python tf.random.stateless_binomial用法及代碼示例
- Python tf.random.experimental.stateless_fold_in用法及代碼示例
- Python tf.random.stateless_categorical用法及代碼示例
- Python tf.random.set_seed用法及代碼示例
- Python tf.random.Generator.make_seeds用法及代碼示例
- Python tf.random.poisson用法及代碼示例
- Python tf.random.gamma用法及代碼示例
- Python tf.random.create_rng_state用法及代碼示例
- Python tf.random.stateless_gamma用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.random.uniform。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
