TF-day2 神經網絡基礎知識

學習內容:

  1. 計算圖 graph
  2. 張量 tensor
  3. 會話 session
  4. 變量 variable:常用的變量生成函數
  5. 佔位符 placeholder

1.計算圖 graph
tensorflow中的每一個計算都是計算圖中的一個節點,而節點之間的邊描述了計算之間的依賴關係。

g1 = tf.Graph()
with g1.as_default():
    v = tf.get_variable("v",shape=[1],dtype=tf.int32,initializer=tf.zeros_initializer())

with tf.Session(graph=g1) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("",reuse=True):
    print(sess.run(tf.get_variable("v",dtype=tf.int32)))

除了使用默認的圖,tensorflow支持使用tf.Graph函數來生成新的計算圖,不同的計算圖上的張量、運算都不會共享。


2.張量
tensorflow在張量中並沒有真正保存數字,它保存的是如何得到這些數字的計算過程。

import tensorflow as tf

a = False
b = tf.cast(a, tf.float32)
print(b)

c = tf.constant(21,dtype=tf.float32,shape = [1],name = "c")
d = tf.constant([1,2],dtype=tf.float32,shape = [1,2],name = "d")
result1 = tf.add(c,d,name = 'add')
result2 = tf.add(b,d,name = 'add1')
print(result1,result2)

###
Tensor("Cast:0", shape=(), dtype=float32)
Tensor("add:0", shape=(1, 2), dtype=float32) Tensor("add1:0", shape=(1, 2), dtype=float32)

結果並不是返回的具體數字,而是一個張量的結構。三個屬性:名字name,維度shape,類型dtype.
其中名字屬性不僅是張量的唯一標識符,還表示它是怎麼計算出來的。
“add:0”表示result1這個tensor是計算節點”add”輸出的第一個結果。

3.會話session
graph和tensor是用來組織數據和運算過程的。執行定義好的運算需要用到會話session.

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    print(sess.run(result2))
    print(result1.eval())
##
[[ 1.  2.]]
[[ 22.  23.]]

4.變量 variable
在tensorflow中變量作用就是用來保存神經網絡中的參數.Tensorflow通過tf.Variable和tf.get_variable()兩種機制來創建或者獲取變量.

4.1 tf.Variable

weights = tf.Variable(tf.random_normal([2,2],stddev=2.))

with tf.Session() as sess:
    tf.global_variables_initializer().run()   ##對所有變量初始化
    print(sess.run(weights))
##
[[ 0.05168185  2.36352181]
 [ 2.82884121 -2.07777405]]

tensorflow隨機生成函數:

##正太分佈
tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=dtypes.float32,seed=None,name=None) 
##正態分佈
tf.truncated_normal(shape,mean=0.0,stddev=1.0,dtype=dtypes.float32,seed=None,name=None) 
##平均分佈tf.random_uniform(shape,minval=0,maxval=None,dtype=dtypes.float32,seed=None,name=None)
##gamma分佈
tf.random_gamma()

tensorflow常數生成函數:

b1 = tf.Variable(tf.zeros(shape=[3]))
b2 = tf.Variable(tf.ones([2,2]))
b3 = tf.Variable(tf.fill([2,3],3))
b4 = tf.Variable(tf.constant(2,dtype=tf.float32,shape=[1,2]))

4.2 tf.get_variable()
與上面部分功能基本一致,對應也有隨機數生成函數和常數生成函數

tf.constant_initializer()
tf.random_normai_initializer()
tf.truncated_naomal_initializer()
tf.random_uniformal_initializer()
....

兩者最大區別在於:tf.get_variable函數,其參數中變量名字是必須填寫的.當出現與上文中重複的變量名時,就會報錯.如果需要通過tf.get_variable獲取一個已經創建的變量,需要通過tf.variable_scope函數來生成一個上下文管理器,並明確指定,在這個上下文管理器中,tf.get_variable將直接獲得已經生成的變量.

  • 對於 tf.get_variable_scope()
  • 參數 reuse = True時,tf.get_variable將直接獲取已經創建的變量.若變量不存在則報錯
  • 參數 reuse = False/None時,tf.get_variable將創建新的變量.若變量已存在,則報錯.

所有的變量都會自動的加到Graphkeys.TRAINABLE_VARIABLES這個集合(collection)中,通過 tf.all_valiables函數可以拿到當前計算圖上所有的變量。如果聲明變量時,參數trainable =True,則該變量就加入到Graphkeys.TRAINABLE_VARIABLES,若trainable =False,則未加入進去。通過 tf.trainable_variables 函數可以得到所有需要優化的參數。


5.佔位符 placeholder
每生成一個常量,tensorflow都會在計算圖中增加一個節點,這樣計算圖會非常大.爲避免這樣的問題,tensorflow提供了placeholder機制用於提供輸入數據. placeholder相當於定義了一個位置,這個位置中的數據在程序運行時再指定.

import tensorflow as tf

w1 = tf.get_variable("w1",shape=[2,3],initializer = tf.random_normal_initializer())
w2 = tf.Variable(tf.random_normal([3,3],stddev = 1))

x = tf.placeholder(tf.float32,name = 'input')
a = tf.matmul(x,w1)
y = tf.matmul(a,w2)

sess = tf.Session()
init_op = tf.global_variables_initializer()
sess.run(init_op)

print(sess.run(y,feed_dict={x:[[0.7,0.9]]}))

在程序計算時,需要提供一個feed_dict來指定x的取值.

發佈了52 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章