一、TensorBoard可視化學(xué)習(xí)
1、TensorFlow有一個(gè)亮點(diǎn)就是,我們能看到自己寫的程序的可視化效果,這個(gè)功能就是TensorBoard
2、TensorFlow可用于訓(xùn)練大規(guī)模深度神經(jīng)網(wǎng)絡(luò)所需的計(jì)算,使用該工具涉及的計(jì)算往往復(fù)雜而深?yuàn)W。為了方便TensorFlow程序的理解、調(diào)試和優(yōu)化,TensorFlow提供了TensorBoard可視化工具
二、實(shí)現(xiàn)程序可視化過程
1、數(shù)據(jù)序列化
TensorBoard通過讀取TensorFlow的事件文件來(lái)運(yùn)行,需要將數(shù)據(jù)生成一個(gè)序列化的summary protobuf對(duì)象
將圖序列化到本地events文件,這將在指定目錄中生成一個(gè)events文件,其名稱格式如下:
events.out.tfevents.{timestamp}.{hostname}
2、將可視化的圖寫入事件文件中API
(1)1.x版本:
tf.summary.FileWriter(path, graph=)
說明:
path:路徑
graph:指定的圖
(2)2.x版本:
writer = tf.summary.create_file_writer(path)
說明:創(chuàng)建一個(gè)文件寫入器writer
path:路徑
tf.summary.graph(graph)
說明:寫入圖
3、啟動(dòng)TensorBoard
終端輸入:
tensorboard --logdir="事件文件的地址"
在瀏覽器中打開TensorBoard的圖頁(yè)面http://127.0.0.1:6006,就會(huì)看到圖了
4、修改代碼
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
def tensorflow_demo():
"""
TensorFlow的基本結(jié)構(gòu)
"""
# TensorFlow實(shí)現(xiàn)加減法運(yùn)算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法運(yùn)算結(jié)果:\n", c_t)
print(c_t.numpy())
# 2.0版本不需要開啟會(huì)話,已經(jīng)沒有會(huì)話模塊了
return None
def graph_demo():
"""
圖的演示
"""
# TensorFlow實(shí)現(xiàn)加減法運(yùn)算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法運(yùn)算結(jié)果:\n", c_t)
print(c_t.numpy())
# 查看默認(rèn)圖
# 方法1:調(diào)用方法
default_g = tf.compat.v1.get_default_graph()
print("default_g:\n", default_g)
# 方法2:查看屬性
# print("a_t的圖屬性:\n", a_t.graph)
# print("c_t的圖屬性:\n", c_t.graph)
# 自定義圖
new_g = tf.Graph()
# 在自己的圖中定義數(shù)據(jù)和操作
with new_g.as_default():
a_new = tf.constant(20)
b_new = tf.constant(30)
c_new = a_new + b_new
print("c_new:\n", c_new)
print("a_new的圖屬性:\n", a_new.graph)
print("b_new的圖屬性:\n", b_new.graph)
# 開啟new_g的會(huì)話
with tf.compat.v1.Session(graph=new_g) as sess:
c_new_value = sess.run(c_new)
print("c_new_value:\n", c_new_value)
print("我們自己創(chuàng)建的圖為:\n", sess.graph)
# 可視化自定義圖
# 1)創(chuàng)建一個(gè)文件寫入器writer
writer = tf.summary.create_file_writer("./tmp/summary")
# 2)將圖寫入
with writer.as_default():
tf.summary.graph(new_g)
return None
if __name__ == "__main__":
# 代碼1:TensorFlow的基本結(jié)構(gòu)
# tensorflow_demo()
# 代碼2:圖的演示
graph_demo()
運(yùn)行之后生成:./tmp/summary/events.out.tfevents.1708140220.server001.26046.0.v2
5、運(yùn)行tensorboard
tensorboard --bind_all --logdir="./tmp/summary"
訪問http://127.0.0.1:6006
6、圖例說明
將“Auto-extract high-degree nodes”選項(xiàng)去除
圖例就不是兩個(gè)三角重疊在一起了
橢圓是OpNode,小圓是Constant,箭頭是數(shù)據(jù)流動(dòng)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-834124.html
參考資料:
https://tensorflow.google.cn/versions/r2.6/api_docs/python/tf/summary/graph文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-834124.html
到了這里,關(guān)于深度學(xué)習(xí)基礎(chǔ)之《TensorFlow框架(3)—TensorBoard》的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!