Tensorflow 知識點

== 感覺TensorFlow知識 很薄弱,每天花點時間對基礎的TensorFlow 知識進行了解, 同時知道每個函數的作用和其中轉換的數據 ==
參考 TensorFlow 2.0:https://github.com/czy36mengfei/tensorflow2_tutorials_chinese
現階段 項目還在使用TensorFlow 1.5 的工程,現在學習tensorflow 1.5 基礎知識

在tensorflow 採用靜態圖 將定義與運行相分離,先用程序搭建一個結構,讓數據按照圖的結構順序計算,tensorflow2.0 默認動態圖,可以在1.x 可以邊調試邊運算

## 隨機產生batch_size 的 小矩陣數據
def batch_generator(X,y,batch_size):
    size = X.shape[0]
    X_copy = X.copy()
    y_copy = y.copy()
    indices = np.arange(size) # array([0,1,2,3...size])
    X_copy = X_copy[indices]
    print(X_copy)
    y_copy = y_copy[indices]

    i = 0;
    while True:
        if i + batch_size <= size:
            yield X_copy[i:i+batch_size],y_copy[i:i+batch_size]
            i += batch_size
        else:
            i = 0
            indices = np.arange(size)
            np.random.shuffle(indices) ## 隨機的調換indices 的列順序
            X_copy = X_copy[indices]
            y_copy = y_copy[indices]
            continue
## tf.placeholder(dtype,shape=None,name= None)
/** dtype 數據類型,常用數據類型 tf.float32
* shape None 是一維數組 [None, 3]3 行不定
* feed_dict : 回饋矩陣中 輸入初始化數據
example: compute 3*12
*/
	 input1 = tf.placeholder(tf.float32)
    input2 = tf.placeholder(tf.float32)
    output = tf.multiply(input1, input2)

    with tf.Session() as sess:
        print (sess.run(output, feed_dict={input1: [3.], input2: [4.]}))

總結: feed_dict作用給使用placeholder 創建出對象賦予tensor 值,使用sess.run() 並不是計算整個張量,只計算第一個參數中函數值, [loss, output] 只計算這兩部分值,知識計算與fetch_dict 的值相關部分
tf.name_scope 和 tf.variable_scope 在模型中開闢各自空間,其中變量在各自空間內進行管理

  • tf.Vaiable 函數:
    tf.Variable(initilaizer, name), initilaizer 參數 tf.random_normal, tf.constant等參數
 ## tf.random_uniform((4,4), minval= low,maxval=high,dtype=tf.float32): 返回一個在low-high 之間的均值分佈的 4*4 矩陣
     with tf.Session() as sess:
        print(sess.run(tf.random_uniform(
            (4, 4), minval=-0.5,
            maxval=0.5, dtype=tf.float32)))
  • tf.nn.embedding_lookup:
    選取一個張量裏面索引對應的元素
    tf.nn.embedding_lookup(tensor, id):tensor就是輸入張量,id就是張量對應的索引
    如下 id =[1,3] 選取1,3 索引位置上張量
c = np.random.random([10, 1])
    b = tf.nn.embedding_lookup(c, [1, 3])

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

結果:b: [[0.60539241]
 [0.48570814]]
 c: [[0.12925993]
 [0.60539241]
 [0.85519417]
 [0.48570814]
 [0.42811298]
 [0.70807729]
 [0.64743353]
 [0.35472522]
 [0.30595551]
 [0.67203577]]

  • tf.summary.histogram()
    查看一個張量在訓練過程中分佈情況,可視化張量在不同時間點直方圖顯示分佈隨時間變化情況
  • tensorflow 幾種計算交叉熵方式,計算每個樣本loss
    tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,labels=None, logits=None, name=None)
    最重要參數 logits,labels,過程中: 輸入的logits 先通過sigmoid函數計算,在計算器交叉熵,但是對其交叉熵的計算方式進行優化,輸出結果output 是一個batch 中每一個樣本loss 與後面的tf.reduce_mean(loss) 使用 其中與之類似還有:
    1. tf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel=None,labels=None,logits=None, name=None)
    2. tf.nn.weighted_cross_entropy_with_logits(labels,logits, pos_weight, name=None)
    3. tf.nn.softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, dim=-1, name=None)
  • tf.reduce_mean:
    tf.cast(A,tf.float32): 用於改變某個張量的數據類型
    tf.reduce_mean():計算張量tensor 沿着某一維度上平均值,主要用作降溫或者計算tensor的圖像平均值 使用平均損失值代替某一維度上的損失矩陣
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章