TensorFlow名稱空間和TensorBoard圖節點例項
阿新 • • 發佈:2020-01-25
一,名稱空間函式
tf.variable_scope tf.name_scope 先以下面的程式碼說明兩者的區別 # 名稱空間管理函式 ''' 說明tf.variable_scope和tf.name_scope的區別 ''' def manage_namespace(): with tf.variable_scope("foo"): # 在名稱空間foo下獲取變數"bar",於是得到的變數名稱為"foo/bar"。 a = tf.get_variable("bar",[1]) #獲取變數名稱為“bar”的變數 print a.name #輸出:foo/bar:0 with tf.variable_scope("bar"): # 在名稱空間bar下獲取變數"bar",於是得到的變數名稱為"bar/bar"。 a = tf.get_variable("bar",[1]) print a.name #輸出:bar/bar:0 with tf.name_scope("a"): # 使用tf.Variable函式生成變數會受tf.name_scope影響,於是得到的變數名稱為"a/Variable"。 a = tf.Variable([1]) #新建變數 print a.name #輸出:a/Variable:0 # 使用tf.get_variable函式生成變數不受tf.name_scope影響,於是變數並不在a這個名稱空間中。 a = tf.get_variable("b",[1]) print a.name #輸出:b:0 with tf.name_scope("b"): # 使用tf.get_variable函式生成變數不受tf.name_scope影響,所以這裡將試圖獲取名稱 # 為“b”的變數。然而這個變數已經被聲明瞭,於是這裡會報重複宣告的錯誤 tf.get_variable("b",[1])#提示錯誤
二,TensorBoard計算圖檢視
1 以以下程式碼例項,為指定任何的名稱空間
def practice_num1(): # 練習1: 構建簡單的計算圖 input1 = tf.constant([1.0,2.0,3.0],name="input1") input2 = tf.Variable(tf.random_uniform([3]),name="input2") output = tf.add_n([input1,input2],name = "add") #生成一個寫日誌的writer,並將當前的tensorflow計算圖寫入日誌 writer = tf.summary.FileWriter(ROOT_DIR + "/log",tf.get_default_graph()) writer.close()
如何使用TensorBoard的過程不再介紹。檢視未指明名稱空間的運算圖
2 修改程式碼制定名稱空間之後的程式碼
def practice_num1_modify(): #將輸入定義放入各自的名稱空間中,從而使得tensorboard可以根據名稱空間來整理視覺化效果圖上的節點 # 練習1: 構建簡單的計算圖 with tf.name_scope("input1"): input1 = tf.constant([1.0,name="input1") with tf.name_scope("input2"): input2 = tf.Variable(tf.random_uniform([3]),tf.get_default_graph()) writer.close()
檢視運算圖
上圖只包含命名的兩個名稱空間的節點,我們可以點選名稱“input2”的圖示上的+號,展開該名稱空間
效果:通過名稱空間可以整理視覺化效果圖上的節點,使視覺化的效果更加清晰。
以上這篇TensorFlow名稱空間和TensorBoard圖節點例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。