隨便搞點隨機數

從有到有:從樣本中抽樣

categorical:給定樣本的概率之後隨機取樣

令人迷惑的一點,不同類別的概率加起來居然可以不爲1

  • 輸入:
    • logits: 2-D Tensor with shape [batch_size, num_classes]. Each slice [i, :] represents the unnormalized log-probabilities for all classes.注意要對原始的概率取對數
    • num_samples: 0-D. Number of independent samples to draw for each row slice.
    • dtype: integer type to use for the output. Defaults to int64.
    • seed: A Python integer. Used to create a random seed for the distribution.固定這個參數每次並不能得到相同的採樣,真是令人迷惑
    • name: Optional name for the operation.
  • 返回:
    • The drawn samples of shape [batch_size, num_samples].
import tensorflow as tf
contrbution = tf.math.log([[0.1, 0.9], [0.9, 0.1]])
tf.random.categorical(contrbution, 10, seed=2)
<tf.Tensor: id=33, shape=(2, 10), dtype=int64, numpy=
array([[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]], dtype=int64)>
tf.random.categorical(contrbution, 10, seed=2)
<tf.Tensor: id=57, shape=(2, 10), dtype=int64, numpy=
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64)>

stateless_categorical:給定樣本的概率之後僞隨機取樣

參數和上面是一樣的,但是seed終於能夠派上用場了,只要seed一樣,結果就一樣。

tf.random.stateless_categorical(contrbution, 10, seed=[2,2])
<tf.Tensor: id=62, shape=(2, 10), dtype=int64, numpy=
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64)>
tf.random.stateless_categorical(contrbution, 10, seed=[2,2])
<tf.Tensor: id=83, shape=(2, 10), dtype=int64, numpy=
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64)>

給定樣本的分佈之後從中取樣

  • tf.random.learned_unigram_candidate_sampler
  • tf.random.log_uniform_candidate_sampler
  • tf.random.uniform_candidate_sampler

shuffle:簡單粗暴打亂樣本

除了輸入原始數據就只有一個seed參數了,然鵝seed也沒啥用

data = tf.constant([1,2,3,4,5])
tf.random.shuffle(data, seed=1)
<tf.Tensor: id=91, shape=(5,), dtype=int32, numpy=array([4, 3, 1, 2, 5])>
tf.random.shuffle(data, seed=1)
<tf.Tensor: id=92, shape=(5,), dtype=int32, numpy=array([2, 5, 4, 1, 3])>

從無到有:生成符合某種分佈的樣本

均勻分佈

  • 輸入:
    • shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
    • minval: A 0-D Tensor or Python value of type dtype. The lower bound on the range of random values to generate. Defaults to 0.
    • maxval: A 0-D Tensor or Python value of type dtype. The upper bound on the range of random values to generate. Defaults to 1 if dtype is floating point.
    • dtype: The type of the output: float16, float32, float64, int32, or int64.
    • seed: A Python integer. Used to create a random seed for the distribution. See tf.compat.v1.set_random_seed for behavior.
    • name: A name for the operation (optional).
tf.random.uniform([5,1], 2, 8)
<tf.Tensor: id=129, shape=(5, 1), dtype=float32, numpy=
array([[7.640295 ],
       [3.7949457],
       [2.659438 ],
       [2.4468968],
       [3.9260578]], dtype=float32)>

gamma分佈

  • 輸入:
    • shape: A 1-D integer Tensor or Python array. The shape of the output samples to be drawn per alpha/beta-parameterized distribution.
    • alpha: A Tensor or Python value or N-D array of type dtype. alpha provides the shape parameter(s) describing the gamma distribution(s) to sample. Must be broadcastable with beta.
    • beta: A Tensor or Python value or N-D array of type dtype. Defaults to 1. beta provides the inverse scale parameter(s) of the gamma distribution(s) to sample. Must be broadcastable with alpha.
    • dtype: The type of alpha, beta, and the output: float16, float32, or float64.
    • seed: A Python integer. Used to create a random seed for the distributions.
    • name: Optional name for the operation.
  • 返回:
    • samples: a Tensor of shape tf.concat([shape, tf.shape(alpha + beta)], axis=0) with values of type dtype.
alpha = tf.constant([[1.],[3.],[5.]])
beta = tf.constant([[3., 4.]])
samples = tf.random.gamma([30, 10], alpha=alpha, beta=beta)
samples.shape
TensorShape([30, 10, 3, 2])

正態分佈

  • 輸入
    • shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
    • mean: A 0-D Tensor or Python value of type dtype. The mean of the normal distribution.
    • stddev: A 0-D Tensor or Python value of type dtype. The standard deviation of the normal distribution.
    • dtype: The type of the output.
    • seed: A Python integer. Used to create a random seed for the distribution. See tf.compat.v1.set_random_seed for behavior.
    • name: A name for the operation (optional).
tf.random.normal([1,8],0,1)
<tf.Tensor: id=98, shape=(1, 8), dtype=float32, numpy=
array([[ 0.5147559 , -1.3692017 ,  1.0016686 ,  0.22309661,  1.6738896 ,
        -0.9813146 , -0.25156403,  0.8217341 ]], dtype=float32)>

其實還有個截斷正態分佈:tf.random.truncated_normal

poisson分佈

  • 輸入:
    • shape: A 1-D integer Tensor or Python array. The shape of the output samples to be drawn per “rate”-parameterized distribution.
    • lam: A Tensor or Python value or N-D array of type dtype. lam provides the rate parameter(s) describing the poisson distribution(s) to sample.
    • dtype: The type of the output: float16, float32, float64, int32 or int64.
    • seed: A Python integer. Used to create a random seed for the distributions. See tf.compat.v1.set_random_seed for behavior.
    • name: Optional name for the operation.
tf.random.normal([1,8],1)
<tf.Tensor: id=122, shape=(1, 8), dtype=float32, numpy=
array([[ 2.426499  ,  1.6289309 , -0.37478268,  1.5314589 ,  1.1933439 ,
         0.47056842, -1.0435152 ,  1.9357013 ]], dtype=float32)>

僞隨機分佈(seed好使)

  • tf.random.stateless_uniform
  • tf.random.stateless_truncated_normal
  • tf.random.stateless_normal
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章