輸入採用佔位符,模型接收任意長度向量,隨時間計算數據流圖全部輸出總和,採用名稱做用域合理劃分數據流圖,每次運行保存數據流圖輸出、累加、均值到磁盤。微信
[None]表明任意長度向量,[]表明標量。update環節更新各Variable對象以及將數據傳入TensorBoard彙總Op。與交換工做流分開,獨立名稱做用域包含Variable對象,存儲輸出累加和,記錄數據流圖運行次數。獨立名稱做用域包含TensorBoard彙總數據,tf.scalar_summary Op。彙總數據在Variable對象更新完成後才添加。函數
構建數據流圖。 導入TensorFlow庫。Graph類構造方法tf.Graph(),顯式建立Graph對象。兩個「全局」Variable對象,追蹤模型運行次數,追蹤模型全部輸出累加和。與其餘節點區分開,放入獨立名稱做用域。trainable=False設置明確指定Variable對象只能手工設置。 模型核心的變換計算,封裝到名稱做用域"transformation",又劃分三個子名稱做用域"input"、"intermediate_layer"、"output"。.multiply、.add只能接收標量參數,.reduce_prod、. reduce_sum能夠接收向量參數。 在"update"名稱做用域內更新Variable對象。.assign_add實現Variable對象遞增。 在"summaries"名稱做用域內彙總數據供TensorBoard用。.cast()作數據類型轉換。.summary.scalar()作標量數據彙總。 在"global_ops"名稱做用域建立全局Operation(Op)。初始化全部Variable對象。合併全部彙總數據。scala
運行數據流圖。 .Session()啓動Session對象,graph屬性加載Graph對象,.summary.FileWriter()啓動FileWriter對象,保存彙總數據。 初始化Variable對象。 建立運行數據流圖輔助函數,傳入向量,運行數據流圖,保存彙總數據。建立feed_dict參數字典,以input_tensor替換a句柄的tf.placeholder節點值。使用feed_dict運行output不關心存儲,運行increment_step保存到step,運行merged_summaries Op保存到summary。添加彙總數據到FileWriter對象,global_step參數隨時間圖示折線圖橫軸。 變換向量長度屢次調用運行數據流圖輔助函數。.flush()把彙總數據寫入磁盤。code
查看數據流圖。 Graph標籤,變換運算流入update方框,爲summaries、variables提供輸入,global_ops包含變換計算非關鍵運算。輸入層、中間層、輸出層分離。 Scalars標籤,summary.scalar對象標籤查看不一樣時間點彙總數據變化。orm
import tensorflow as tf#導入TensorFlow庫 #構建數據流圖 graph = tf.Graph()#顯式建立Graph對象 with graph.as_default():#設爲默認Graph對象 with tf.name_scope("variables"):#建立Variable對象名稱做用域 global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")#記錄數據流圖運行次數的Variable對象,初值爲0,數據類型爲32位整型,不可自動修改,以global_step標識 total_output = tf.Variable(0.0, dtype=tf.float32, trainable=False, name="total_output")#追蹤模型全部輸出累加和的Variable對象,初值爲0.0,數據類型爲32位浮點型,不可自動修改,以total_output標識 with tf.name_scope("transformation"):#建立變換計算Op名稱做用域 with tf.name_scope("input"):#建立獨立輸入層名稱做用域 a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")#建立佔位符,接收一個32位浮點型任意長度的向量做爲輸入,以input_placeholder_a標識 with tf.name_scope("intermediate_layer"):#建立獨立中間層名稱做用域 b = tf.reduce_prod(a, name="product_b")#建立建立歸約乘積Op,接收張量輸入,輸出張量全部份量(元素)的乘積,以product_b標識 c = tf.reduce_sum(a, name="sum_c")#建立建立歸約求和Op,接收張量輸入,輸出張量全部份量(元素)的求和,以sum_c標識 with tf.name_scope("output"):#建立獨立輸出層名稱做用域 output = tf.add(b, c, name="output")#建立建立求和Op,接收兩個標量輸入,輸出標量求和,以output標識 with tf.name_scope("update"): update_total = total_output.assign_add(output)#用最新的輸出更新Variable對象total_output increment_step = global_step.assign_add(1)#增1更新Variable對象global_step,記錄數據流圖運行次數 with tf.name_scope("summaries"):#建立數據彙總Op名稱做用域 avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")#計算平均值,輸出累加和除以數據流圖運行次數,把運行次數數據類型轉換爲32位浮點型,以average標識 tf.summary.scalar(b'output_summary',output)#建立輸出節點標量數據統計彙總,以output_summary標識 tf.summary.scalar(b'total_summary',update_total)#建立輸出累加求和標量數據統計彙總,以total_summary標識 tf.summary.scalar(b'average_summary',avg)#建立平均值標量數據統計彙總,以average_summary標識 with tf.name_scope("global_ops"):#建立全局Operation(Op)名稱做用域 init = tf.global_variables_initializer()#建立初始化全部Variable對象的Op merged_summaries = tf.summary.merge_all()#建立合併全部彙總數據的Op #運行數據流圖 sess = tf.Session(graph=graph)#用顯式建立Graph對象啓動Session會話對象 writer = tf.summary.FileWriter('./improved_graph', graph)#啓動FileWriter對象,保存彙總數據 sess.run(init)#運行Variable對象初始化Op def run_graph(input_tensor):#定義數據注圖運行輔助函數 """ 輔助函數:用給定的輸入張量運行數據流圖, 並保存彙總數據 """ feed_dict = {a: input_tensor}#建立feed_dict參數字典,以input_tensor替換a句柄的tf.placeholder節點值 _, step, summary = sess.run([output, increment_step, merged_summaries], feed_dict=feed_dict)#使用feed_dict運行output不關心存儲,運行increment_step保存到step,運行merged_summaries Op保存到summary writer.add_summary(summary, global_step=step)#添加彙總數據到FileWriter對象,global_step參數時間圖示折線圖橫軸 #用不一樣的輸入用例運行數據流圖 run_graph([2,8]) run_graph([3,1,3,3]) run_graph([8]) run_graph([1,2,3]) run_graph([11,4]) run_graph([4,1]) run_graph([7,3,1]) run_graph([6,3]) run_graph([0,2]) run_graph([4,5,6]) writer.flush()#將彙總數據寫入磁盤 writer.close()#關閉FileWriter對象,釋放資源 sess.close()#關閉Session對象,釋放資源
參考資料: 《面向機器智能的TensorFlow實踐》對象
歡迎加我微信交流:qingxingfengziip
個人微信公衆號:qingxingfengzigz資源
我老婆張幸清的微信公衆號:qingqingfeifangz作用域