05 Tensorflow中變量的初始化

    打開Python Shell,輸入import tensorflow as tf,而後能夠執行如下代碼。html

    一、建立一個2*3的矩陣,並讓全部元素的值爲0.(類型爲tf.float)dom

a = tf.zeros([2,3], dtype = tf.float32)

    二、建立一個3*4的矩陣,並讓全部元素的值爲1.學習

b = tf.ones([3,4])

    三、建立一個1*10的矩陣,使用2來填充。(類型爲tf.int32,可忽略)spa

c = tf.constant(2, dtype=tf.int32, shape=[1,10])

    四、建立一個1*10的矩陣,其中的元素符合正態分佈,平均值是20,標準誤差是3.code

d = tf.random_normal([1,10],mean = 20, stddev = 3)

    上面全部的值均可以用來初始化變量。例如用0.01來填充一個1*2的矩陣來初始化一個叫bias的變量。orm

bias = tf.Variable(tf.zeros([1,2]) + 0.01)

    若是你想查看這些量具體的值,能夠在Session中執行它並輸出。htm

sess = tf.Session()
print(sess.run(d))

    這裏,我獲得瞭如下的值:blog

    [[ 22.44503784  18.19544983  17.89671898  17.67314911  19.45074844
   18.6805439   18.56541443  16.59041977  22.11240005  19.12819099]]。它就是上面4咱們建立的量的值。get

    參考資料io

    《Tensorflow學習筆記(3)》:http://blog.sina.com.cn/s/blog_8b2a28790102wnkh.html

相關文章
相關標籤/搜索