1. 程式人生 > >TensorFlow學習(二):tf.random_normal() 和 tf.random_unform()

TensorFlow學習(二):tf.random_normal() 和 tf.random_unform()

1. tf.random_normal() #正態分佈

tf.random_normal(
    shape,
    mean=0.0,
    stddev=1.0,
    dtype=tf.float32,
    seed=None,
    name=None
)

Args:

  • 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.set_random_seed for behavior. #隨機種子
  • name
    : A name for the operation (optional). 

Returns:

A tensor of the specified shape filled with random normal values.

例子:

import tensorflow as tf

a = tf.random_normal([2,3])

with tf.Session() as sess:
    print(sess.run(a))
    print(sess.run(a))
  • 第一次輸出:

[[ 0.08046694  2.1459296  -0.1951714 ]
 [ 0.11219931 -0.05139215  0.6772044 ]]
[[-0.77863777 -1.0012143   0.64676356]
 [-1.064799   -0.22006823  1.4889574 ]]

  • 第二次輸出結果;

[[ 0.05100911 -1.1073893  -0.5293714 ]
 [ 0.3451144  -0.27878246  0.20632097]]
[[ 0.22588727  0.3073472  -1.1917537 ]
 [ 2.2639475  -0.748026   -1.7960579 ]]

設定隨機種子 a=tf.random_normal([2,3],seed=12)

  • 第一次輸出結果:

[[-0.43663138  0.8449775  -0.01180986]
 [-0.8844008  -0.18527539  0.21195167]]
[[-1.587453  -1.2733358  1.568891 ]
 [ 1.3985995 -0.2998891  0.6742214]]

  • 第二次輸出結果:

[[-0.43663138  0.8449775  -0.01180986]
 [-0.8844008  -0.18527539  0.21195167]]
[[-1.587453  -1.2733358  1.568891 ]
 [ 1.3985995 -0.2998891  0.6742214]]

       從上面的兩次結果我們可以發現,第二次設定隨機種子之後的程式輸出的結果是相同的,第一次則完全不同。一般計算機產生的隨機數都是偽隨機數。隨機數是由計算機依據隨機種子,利用一定的演算法計算而出,所以當隨機種子確定,演算法確定,產生的隨機數便能確定。在呼叫隨機數函式時,根據需要自行確定是否需要設定seed。

2. tf.random_uniform()  #均勻分佈

tf.random_uniform(
    shape,
    minval=0,
    maxval=None,
    dtype=tf.float32,
    seed=None,
    name=None
)

Args:

  • 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. #下限預設為“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. #上限浮點數型別預設為“1”
  • dtype: The type of the output: float16float32float64int32, or int64.
  • seed: A Python integer. Used to create a random seed for the distribution. See tf.set_random_seedfor behavior.
  • name: A name for the operation (optional).

Returns:

A tensor of the specified shape filled with random uniform values.

Raises:

  • ValueError: If dtype is integral and maxval is not specified.

例子:

import tensorflow as tf

b = tf.random_uniform([2,3])

with tf.Session() as sess:
    print(sess.run(b))
    print(sess.run(b))

輸出結果:

[[0.7068434  0.37068224 0.98492336]
 [0.25193918 0.01160133 0.4997362 ]]
[[0.73202145 0.10125887 0.30887377]
 [0.10988557 0.64894116 0.2683978 ]]

例子二:新增資料範圍

import tensorflow as tf

b = tf.random_uniform([2,3],minval=-1,maxval=2)
with tf.Session() as sess:
    print(sess.run(b))
    print(sess.run(b))

輸出結果:

[[-0.40992153  0.90792036 -0.38250148]
 [ 1.6468053   1.5642762   0.7498528 ]]
[[ 0.398026   -0.02267563 -0.9902872 ]
 [ 1.6546245  -0.7437657   1.838615  ]]