摘要:輸入采用占位符,模型接收任意長度向量,隨時間計算數(shù)據(jù)流圖所有輸出總和,采用名稱作用域合理劃分數(shù)據(jù)流圖,每次運行保存數(shù)據(jù)流圖輸出累加均值到磁盤。與交換工作流分開,獨立名稱作用域包含對象,存儲輸出累加和,記錄數(shù)據(jù)流圖運行次數(shù)。
輸入采用占位符,模型接收任意長度向量,隨時間計算數(shù)據(jù)流圖所有輸出總和,采用名稱作用域合理劃分數(shù)據(jù)流圖,每次運行保存數(shù)據(jù)流圖輸出、累加、均值到磁盤。
[None]代表任意長度向量,[]代表標量。update環(huán)節(jié)更新各Variable對象以及將數(shù)據(jù)傳入TensorBoard匯總Op。與交換工作流分開,獨立名稱作用域包含Variable對象,存儲輸出累加和,記錄數(shù)據(jù)流圖運行次數(shù)。獨立名稱作用域包含TensorBoard匯總數(shù)據(jù),tf.scalar_summary Op。匯總數(shù)據(jù)在Variable對象更新完成后才添加。
構(gòu)建數(shù)據(jù)流圖。
導入TensorFlow庫。Graph類構(gòu)造方法tf.Graph(),顯式創(chuàng)建Graph對象。兩個“全局”Variable對象,追蹤模型運行次數(shù),追蹤模型所有輸出累加和。與其他節(jié)點區(qū)分開,放入獨立名稱作用域。trainable=False設(shè)置明確指定Variable對象只能手工設(shè)置。
模型核心的變換計算,封裝到名稱作用域"transformation",又劃分三個子名稱作用域"input"、"intermediate_layer"、"output"。.multiply、.add只能接收標量參數(shù),.reduce_prod、. reduce_sum可以接收向量參數(shù)。
在"update"名稱作用域內(nèi)更新Variable對象。.assign_add實現(xiàn)Variable對象遞增。
在"summaries"名稱作用域內(nèi)匯總數(shù)據(jù)供TensorBoard用。.cast()做數(shù)據(jù)類型轉(zhuǎn)換。.summary.scalar()做標量數(shù)據(jù)匯總。
在"global_ops"名稱作用域創(chuàng)建全局Operation(Op)。初始化所有Variable對象。合并所有匯總數(shù)據(jù)。
運行數(shù)據(jù)流圖。
.Session()啟動Session對象,graph屬性加載Graph對象,.summary.FileWriter()啟動FileWriter對象,保存匯總數(shù)據(jù)。
初始化Variable對象。
創(chuàng)建運行數(shù)據(jù)流圖輔助函數(shù),傳入向量,運行數(shù)據(jù)流圖,保存匯總數(shù)據(jù)。創(chuàng)建feed_dict參數(shù)字典,以input_tensor替換a句柄的tf.placeholder節(jié)點值。使用feed_dict運行output不關(guān)心存儲,運行increment_step保存到step,運行merged_summaries Op保存到summary。添加匯總數(shù)據(jù)到FileWriter對象,global_step參數(shù)隨時間圖示折線圖橫軸。
變換向量長度多次調(diào)用運行數(shù)據(jù)流圖輔助函數(shù)。.flush()把匯總數(shù)據(jù)寫入磁盤。
查看數(shù)據(jù)流圖。
Graph標簽,變換運算流入update方框,為summaries、variables提供輸入,global_ops包含變換計算非關(guān)鍵運算。輸入層、中間層、輸出層分離。
Scalars標簽,summary.scalar對象標簽查看不同時間點匯總數(shù)據(jù)變化。
import tensorflow as tf#導入TensorFlow庫 #構(gòu)建數(shù)據(jù)流圖 graph = tf.Graph()#顯式創(chuàng)建Graph對象 with graph.as_default():#設(shè)為默認Graph對象 with tf.name_scope("variables"):#創(chuàng)建Variable對象名稱作用域 global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")#記錄數(shù)據(jù)流圖運行次數(shù)的Variable對象,初值為0,數(shù)據(jù)類型為32位整型,不可自動修改,以global_step標識 total_output = tf.Variable(0.0, dtype=tf.float32, trainable=False, name="total_output")#追蹤模型所有輸出累加和的Variable對象,初值為0.0,數(shù)據(jù)類型為32位浮點型,不可自動修改,以total_output標識 with tf.name_scope("transformation"):#創(chuàng)建變換計算Op名稱作用域 with tf.name_scope("input"):#創(chuàng)建獨立輸入層名稱作用域 a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")#創(chuàng)建占位符,接收一個32位浮點型任意長度的向量作為輸入,以input_placeholder_a標識 with tf.name_scope("intermediate_layer"):#創(chuàng)建獨立中間層名稱作用域 b = tf.reduce_prod(a, name="product_b")#創(chuàng)建創(chuàng)建歸約乘積Op,接收張量輸入,輸出張量所有分量(元素)的乘積,以product_b標識 c = tf.reduce_sum(a, name="sum_c")#創(chuàng)建創(chuàng)建歸約求和Op,接收張量輸入,輸出張量所有分量(元素)的求和,以sum_c標識 with tf.name_scope("output"):#創(chuàng)建獨立輸出層名稱作用域 output = tf.add(b, c, name="output")#創(chuàng)建創(chuàng)建求和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,記錄數(shù)據(jù)流圖運行次數(shù) with tf.name_scope("summaries"):#創(chuàng)建數(shù)據(jù)匯總Op名稱作用域 avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")#計算平均值,輸出累加和除以數(shù)據(jù)流圖運行次數(shù),把運行次數(shù)數(shù)據(jù)類型轉(zhuǎn)換為32位浮點型,以average標識 tf.summary.scalar(b"output_summary",output)#創(chuàng)建輸出節(jié)點標量數(shù)據(jù)統(tǒng)計匯總,以output_summary標識 tf.summary.scalar(b"total_summary",update_total)#創(chuàng)建輸出累加求和標量數(shù)據(jù)統(tǒng)計匯總,以total_summary標識 tf.summary.scalar(b"average_summary",avg)#創(chuàng)建平均值標量數(shù)據(jù)統(tǒng)計匯總,以average_summary標識 with tf.name_scope("global_ops"):#創(chuàng)建全局Operation(Op)名稱作用域 init = tf.global_variables_initializer()#創(chuàng)建初始化所有Variable對象的Op merged_summaries = tf.summary.merge_all()#創(chuàng)建合并所有匯總數(shù)據(jù)的Op #運行數(shù)據(jù)流圖 sess = tf.Session(graph=graph)#用顯式創(chuàng)建Graph對象啟動Session會話對象 writer = tf.summary.FileWriter("./improved_graph", graph)#啟動FileWriter對象,保存匯總數(shù)據(jù) sess.run(init)#運行Variable對象初始化Op def run_graph(input_tensor):#定義數(shù)據(jù)注圖運行輔助函數(shù) """ 輔助函數(shù):用給定的輸入張量運行數(shù)據(jù)流圖, 并保存匯總數(shù)據(jù) """ feed_dict = {a: input_tensor}#創(chuàng)建feed_dict參數(shù)字典,以input_tensor替換a句柄的tf.placeholder節(jié)點值 _, step, summary = sess.run([output, increment_step, merged_summaries], feed_dict=feed_dict)#使用feed_dict運行output不關(guān)心存儲,運行increment_step保存到step,運行merged_summaries Op保存到summary writer.add_summary(summary, global_step=step)#添加匯總數(shù)據(jù)到FileWriter對象,global_step參數(shù)時間圖示折線圖橫軸 #用不同的輸入用例運行數(shù)據(jù)流圖 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()#將匯總數(shù)據(jù)寫入磁盤 writer.close()#關(guān)閉FileWriter對象,釋放資源 sess.close()#關(guān)閉Session對象,釋放資源
參考資料:
《面向機器智能的TensorFlow實踐》
歡迎加我微信交流:qingxingfengzi
我的微信公眾號:qingxingfengzigz
我老婆張幸清的微信公眾號:qingqingfeifangz
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/44364.html
摘要:張量的命名形式,為節(jié)點的名稱,表示當前張量來自來自節(jié)點的第幾個輸出。,要求的輸入對象是一個但是它的輸出是一個數(shù)組輸出其他基本概念常量變量占位符常量中使用常量很簡單,如,。返回的的類型返回的的形狀的名字布爾值,用于驗證值的形狀。 showImg(https://segmentfault.com/img/bVbvlKO?w=4938&h=1679);(代碼基于tensorflow 1.14...
好的,下面是一篇關(guān)于TensorFlow編程技術(shù)的文章: TensorFlow是一種非常流行的機器學習框架,它被廣泛應用于各種領(lǐng)域,包括計算機視覺、自然語言處理、語音識別等。如果你想學習TensorFlow,那么你需要掌握一些基本的編程技術(shù)。在這篇文章中,我們將介紹一些TensorFlow編程技術(shù),幫助你更好地理解和使用這個強大的框架。 1. 張量(Tensor) TensorFlow中最基...
閱讀 3064·2021-10-12 10:17
閱讀 1668·2021-09-01 11:38
閱讀 1176·2019-08-30 15:44
閱讀 3549·2019-08-26 18:36
閱讀 569·2019-08-26 13:25
閱讀 1954·2019-08-26 10:29
閱讀 2902·2019-08-23 15:58
閱讀 818·2019-08-23 12:59