TensorFlow完整程序詳解——構建並運行數據流圖¶
作者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/
所用版本:python3.5.2,tensorflow1.8.0,tensorboard1.8.0
# 通過綜合運用之前提過的所有組件——張量對象、Graph對象、Op、Variable對象、占位符、Session對象以及名稱作用域的練習來結束TensorFlow的學習
# 導入tensorflow庫
import tensorflow as tf
# 顯示構建一個Graph對象並加以使用
graph = tf.Graph()
# 將上述新Graph對象設為默認Graph對象
with graph.as_default():
# 名稱作用域
with tf.name_scope("variables"):
# 記錄數據流圖運行次數的Variable對象
# 初始值為0,數據類型int32,不允許Variable對象使用Optimizer類,而是手工修改,該對象起名為global_step
global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")
# 追蹤該模型的所有輸出隨時間的累加和的Variable對象
total_output = tf.Variable(0.0, dtype=tf.float32, name="total_output")
# 創建模型的核心變換部分
with tf.name_scope("transformation"):
# 獨立的輸入層
with tf.name_scope("input"):
# 創建輸出占位符,用於接收一個向量
a = tf.placeholder(tf.float32, shape=[None], name="input_a")
# 獨立的中間層
with tf.name_scope("intermediate_layer"):
b = tf.reduce_prod(a, name="product_b")
c = tf.reduce_sum(a, name="sum_c")
# 獨立的輸出層
with tf.name_scope("output"):
output = tf.add(b, c, name="output")
# 創建更新模型
with tf.name_scope("update"):
# 用罪行的輸出更新Variable對象total_output
update_total = total_output.assign_add(output)
# 將前面的Variable對象global_step增加1,只要數據流圖運行,該操作會在此基礎上自動增1
increment_step = global_step.assign_add(1)
# 創建匯總數據模型
with tf.name_scope("summary"):
avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")
# 為輸出節點創建匯總數據
tf.summary.scalar('Output', output)
tf.summary.scalar('Sum_of_outputs_over_time', update_total)
tf.summary.scalar('Average_of_outputs_over_time', avg)
# 為完成數據流圖的構建,需要創建Variable對象初始化Op和用於將所有匯總數據組織到一個Op的輔助節點
with tf.name_scope("global_ops"):
# 初始化Op
init = tf.global_variables_initializer()
# 將所有匯總數據合並到一個Op中
merged_summary = tf.summary.merge_all()
# 創建會話
sess = tf.Session(graph=graph)
# 將圖寫入本地文件夾中
writer = tf.summary.FileWriter("./logs/end", graph)
# 初始化Variable對象
sess.run(init)
# 創建輔助函數,將輸入向量傳給該函數,運行數據流圖,並將匯總數據保存下來,以便之后無需反復輸入相同代碼
def run_graph(input_tensor):
# 可重寫之前的數據流圖中a的值
output, summary, step = sess.run([update_total, merged_summary, increment_step], feed_dict={a: input_tensor})
writer.add_summary(summary, global_step=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()
# 關閉SummaryWriter對象
writer.close()
# 關閉Session會話
sess.close()
打開Anaconda Prompt
(base) C:\Users\hp>activate tensorflow
(tensorflow) C:\Users\hp>cd..
(tensorflow) C:\Users>D:
(tensorflow) D:>cd ./Python code
(tensorflow) D:\Python code>tensorboard --logdir=./logs/end
在瀏覽器輸入http://HP:6006 或者http://localhost:6006 即可看到對應的數據流圖。
從圖中可以看到,我們的變換運算流入transformation方框,后者又同時為summary與variables名稱作用域提供輸入,global_ops名稱作用域中包含了一些對於主要的變換計算並不十分重要的運算。

將各個方框展開,可以更細粒度的觀察它們的結構。可以看到transformation中輸入層、中間層與輸出層是彼此分離的。

當切換到Scalars頁面之后,看到三個依據我們賦予各summary.scalar對象的標簽而命名的折疊的標簽頁。

單擊任意標簽頁,都展示了不同時間點上數值的變化情況。



本練習的數據流圖:

參考文獻:人工智能原理與實踐:基於Python語言和TensorFlow / 張明,何艷珊,杜永文編著. —— 北京:人民郵電出版社,2019.8.
